blob: 5d3e0f222e9fdf3bd4062772e80df17eb3a71641 [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))
148#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x64_add_r64_r64((as), (reg_dest), (reg_src))
149#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x64_sub_r64_r64((as), (reg_dest), (reg_src))
150
Damien Georgee9dac3b2014-09-29 22:10:41 +0100151#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_x64_mov_r64_to_disp((as), (reg_src), (reg_base), 0)
152#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x64_mov_r8_to_disp((as), (reg_src), (reg_base), 0)
153#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_x64_mov_r16_to_disp((as), (reg_src), (reg_base), 0)
154
Damien Georgec90f59e2014-09-06 23:06:36 +0100155#elif N_X86
156
157// x86 specific stuff
158
159#include "asmx86.h"
160
161STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
162 [MP_F_CONVERT_OBJ_TO_NATIVE] = 2,
163 [MP_F_CONVERT_NATIVE_TO_OBJ] = 2,
164 [MP_F_LOAD_CONST_INT] = 1,
165 [MP_F_LOAD_CONST_DEC] = 1,
166 [MP_F_LOAD_CONST_STR] = 1,
167 [MP_F_LOAD_CONST_BYTES] = 1,
168 [MP_F_LOAD_NAME] = 1,
169 [MP_F_LOAD_GLOBAL] = 1,
170 [MP_F_LOAD_BUILD_CLASS] = 0,
171 [MP_F_LOAD_ATTR] = 2,
172 [MP_F_LOAD_METHOD] = 3,
173 [MP_F_STORE_NAME] = 2,
174 [MP_F_STORE_GLOBAL] = 2,
175 [MP_F_STORE_ATTR] = 3,
176 [MP_F_OBJ_SUBSCR] = 3,
177 [MP_F_OBJ_IS_TRUE] = 1,
178 [MP_F_UNARY_OP] = 2,
179 [MP_F_BINARY_OP] = 3,
180 [MP_F_BUILD_TUPLE] = 2,
181 [MP_F_BUILD_LIST] = 2,
182 [MP_F_LIST_APPEND] = 2,
183 [MP_F_BUILD_MAP] = 1,
184 [MP_F_STORE_MAP] = 3,
185#if MICROPY_PY_BUILTINS_SET
186 [MP_F_BUILD_SET] = 2,
187 [MP_F_STORE_SET] = 2,
188#endif
189 [MP_F_MAKE_FUNCTION_FROM_RAW_CODE] = 3,
190 [MP_F_NATIVE_CALL_FUNCTION_N_KW] = 3,
191 [MP_F_CALL_METHOD_N_KW] = 3,
192 [MP_F_GETITER] = 1,
193 [MP_F_ITERNEXT] = 1,
194 [MP_F_NLR_PUSH] = 1,
195 [MP_F_NLR_POP] = 0,
196 [MP_F_NATIVE_RAISE] = 1,
197 [MP_F_IMPORT_NAME] = 3,
198 [MP_F_IMPORT_FROM] = 2,
199 [MP_F_IMPORT_ALL] = 1,
200#if MICROPY_PY_BUILTINS_SLICE
201 [MP_F_NEW_SLICE] = 3,
202#endif
203 [MP_F_UNPACK_SEQUENCE] = 3,
204 [MP_F_UNPACK_EX] = 3,
205 [MP_F_DELETE_NAME] = 1,
206 [MP_F_DELETE_GLOBAL] = 1,
207};
208
209#define EXPORT_FUN(name) emit_native_x86_##name
210
Damien George0b610de2014-09-29 16:25:04 +0100211#define REG_RET ASM_X86_REG_EAX
Damien George6eae8612014-09-08 22:16:35 +0000212#define REG_ARG_1 ASM_X86_REG_ARG_1
213#define REG_ARG_2 ASM_X86_REG_ARG_2
214#define REG_ARG_3 ASM_X86_REG_ARG_3
Damien George81057362014-09-07 01:06:19 +0100215
Damien George25d90412014-09-06 23:24:32 +0000216// caller-save, so can be used as temporaries
Damien George0b610de2014-09-29 16:25:04 +0100217#define REG_TEMP0 ASM_X86_REG_EAX
218#define REG_TEMP1 ASM_X86_REG_ECX
219#define REG_TEMP2 ASM_X86_REG_EDX
Damien Georgec90f59e2014-09-06 23:06:36 +0100220
Damien George25d90412014-09-06 23:24:32 +0000221// callee-save, so can be used as locals
Damien George0b610de2014-09-29 16:25:04 +0100222#define REG_LOCAL_1 ASM_X86_REG_EBX
223#define REG_LOCAL_2 ASM_X86_REG_ESI
224#define REG_LOCAL_3 ASM_X86_REG_EDI
Damien George25d90412014-09-06 23:24:32 +0000225#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100226
227#define ASM_PASS_COMPUTE ASM_X86_PASS_COMPUTE
228#define ASM_PASS_EMIT ASM_X86_PASS_EMIT
229
230#define ASM_T asm_x86_t
231#define ASM_NEW asm_x86_new
232#define ASM_FREE asm_x86_free
233#define ASM_GET_CODE asm_x86_get_code
234#define ASM_GET_CODE_SIZE asm_x86_get_code_size
235#define ASM_START_PASS asm_x86_start_pass
236#define ASM_END_PASS asm_x86_end_pass
237#define ASM_ENTRY asm_x86_entry
238#define ASM_EXIT asm_x86_exit
239
240#define ASM_LABEL_ASSIGN asm_x86_label_assign
241#define ASM_JUMP asm_x86_jmp_label
242#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
243 do { \
244 asm_x86_test_r8_with_r8(as, reg, reg); \
245 asm_x86_jcc_label(as, ASM_X86_CC_JZ, label); \
246 } while (0)
247#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
248 do { \
249 asm_x86_test_r8_with_r8(as, reg, reg); \
250 asm_x86_jcc_label(as, ASM_X86_CC_JNZ, label); \
251 } while (0)
252#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
253 do { \
254 asm_x86_cmp_r32_with_r32(as, reg1, reg2); \
255 asm_x86_jcc_label(as, ASM_X86_CC_JE, label); \
256 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100257#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 +0100258
259#define ASM_MOV_REG_TO_LOCAL asm_x86_mov_r32_to_local
260#define ASM_MOV_IMM_TO_REG asm_x86_mov_i32_to_r32
261#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x86_mov_i32_to_r32_aligned
262#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
263 do { \
264 asm_x86_mov_i32_to_r32(as, (imm), (reg_temp)); \
265 asm_x86_mov_r32_to_local(as, (reg_temp), (local_num)); \
266 } while (false)
267#define ASM_MOV_LOCAL_TO_REG asm_x86_mov_local_to_r32
Damien George3112cde2014-09-29 18:45:42 +0100268#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 +0100269#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x86_mov_local_addr_to_r32
Damien13ed3a62013-10-08 09:05:10 +0100270
Damien George3112cde2014-09-29 18:45:42 +0100271#define ASM_LSL_REG(as, reg) asm_x86_shl_r32_cl((as), (reg))
272#define ASM_ASR_REG(as, reg) asm_x86_sar_r32_cl((as), (reg))
273#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x86_add_r32_r32((as), (reg_dest), (reg_src))
274#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x86_sub_r32_r32((as), (reg_dest), (reg_src))
275
Damien Georgee9dac3b2014-09-29 22:10:41 +0100276#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_x86_mov_r32_to_disp((as), (reg_src), (reg_base), 0)
277#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x86_mov_r8_to_disp((as), (reg_src), (reg_base), 0)
278#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_x86_mov_r16_to_disp((as), (reg_src), (reg_base), 0)
279
Damien3ef4abb2013-10-12 16:53:13 +0100280#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100281
282// thumb specific stuff
283
284#include "asmthumb.h"
285
Damien13ed3a62013-10-08 09:05:10 +0100286#define EXPORT_FUN(name) emit_native_thumb_##name
287
Damien George0b610de2014-09-29 16:25:04 +0100288#define REG_RET ASM_THUMB_REG_R0
289#define REG_ARG_1 ASM_THUMB_REG_R0
290#define REG_ARG_2 ASM_THUMB_REG_R1
291#define REG_ARG_3 ASM_THUMB_REG_R2
292#define REG_ARG_4 ASM_THUMB_REG_R3
Damien George81057362014-09-07 01:06:19 +0100293
Damien George0b610de2014-09-29 16:25:04 +0100294#define REG_TEMP0 ASM_THUMB_REG_R0
295#define REG_TEMP1 ASM_THUMB_REG_R1
296#define REG_TEMP2 ASM_THUMB_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100297
Damien George0b610de2014-09-29 16:25:04 +0100298#define REG_LOCAL_1 ASM_THUMB_REG_R4
299#define REG_LOCAL_2 ASM_THUMB_REG_R5
300#define REG_LOCAL_3 ASM_THUMB_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100301#define REG_LOCAL_NUM (3)
302
303#define ASM_PASS_COMPUTE ASM_THUMB_PASS_COMPUTE
304#define ASM_PASS_EMIT ASM_THUMB_PASS_EMIT
305
306#define ASM_T asm_thumb_t
307#define ASM_NEW asm_thumb_new
308#define ASM_FREE asm_thumb_free
309#define ASM_GET_CODE asm_thumb_get_code
310#define ASM_GET_CODE_SIZE asm_thumb_get_code_size
311#define ASM_START_PASS asm_thumb_start_pass
312#define ASM_END_PASS asm_thumb_end_pass
313#define ASM_ENTRY asm_thumb_entry
314#define ASM_EXIT asm_thumb_exit
315
316#define ASM_LABEL_ASSIGN asm_thumb_label_assign
317#define ASM_JUMP asm_thumb_b_label
318#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
319 do { \
320 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100321 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100322 } while (0)
323#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
324 do { \
325 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100326 asm_thumb_bcc_label(as, ASM_THUMB_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100327 } while (0)
328#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
329 do { \
330 asm_thumb_cmp_rlo_rlo(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100331 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100332 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100333#define ASM_CALL_IND(as, ptr, idx) asm_thumb_bl_ind(as, ptr, idx, ASM_THUMB_REG_R3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100334
335#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_thumb_mov_local_reg(as, (local_num), (reg))
336#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_optimised(as, (reg), (imm))
337#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_aligned(as, (reg), (imm))
338#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
339 do { \
340 asm_thumb_mov_reg_i32_optimised(as, (reg_temp), (imm)); \
341 asm_thumb_mov_local_reg(as, (local_num), (reg_temp)); \
342 } while (false)
343#define ASM_MOV_LOCAL_TO_REG(as, local_num, reg) asm_thumb_mov_reg_local(as, (reg), (local_num))
Damien George3112cde2014-09-29 18:45:42 +0100344#define ASM_MOV_REG_REG(as, reg_dest, reg_src) asm_thumb_mov_reg_reg((as), (reg_dest), (reg_src))
Damien Georgec90f59e2014-09-06 23:06:36 +0100345#define ASM_MOV_LOCAL_ADDR_TO_REG(as, local_num, reg) asm_thumb_mov_reg_local_addr(as, (reg), (local_num))
Damien13ed3a62013-10-08 09:05:10 +0100346
Damien George3112cde2014-09-29 18:45:42 +0100347#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_LSL, (reg_dest), (reg_shift))
348#define ASM_ASR_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_ASR, (reg_dest), (reg_shift))
349#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_thumb_add_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
350#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_thumb_sub_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
351
Damien Georgee9dac3b2014-09-29 22:10:41 +0100352#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_thumb_str_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
353#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_thumb_strb_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
354#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_thumb_strh_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
355
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200356#elif N_ARM
357
358// ARM specific stuff
359
360#include "asmarm.h"
361
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200362#define EXPORT_FUN(name) emit_native_arm_##name
363
Damien George0b610de2014-09-29 16:25:04 +0100364#define REG_RET ASM_ARM_REG_R0
365#define REG_ARG_1 ASM_ARM_REG_R0
366#define REG_ARG_2 ASM_ARM_REG_R1
367#define REG_ARG_3 ASM_ARM_REG_R2
368#define REG_ARG_4 ASM_ARM_REG_R3
Damien George81057362014-09-07 01:06:19 +0100369
Damien George0b610de2014-09-29 16:25:04 +0100370#define REG_TEMP0 ASM_ARM_REG_R0
371#define REG_TEMP1 ASM_ARM_REG_R1
372#define REG_TEMP2 ASM_ARM_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100373
Damien George0b610de2014-09-29 16:25:04 +0100374#define REG_LOCAL_1 ASM_ARM_REG_R4
375#define REG_LOCAL_2 ASM_ARM_REG_R5
376#define REG_LOCAL_3 ASM_ARM_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100377#define REG_LOCAL_NUM (3)
378
379#define ASM_PASS_COMPUTE ASM_ARM_PASS_COMPUTE
380#define ASM_PASS_EMIT ASM_ARM_PASS_EMIT
381
382#define ASM_T asm_arm_t
383#define ASM_NEW asm_arm_new
384#define ASM_FREE asm_arm_free
385#define ASM_GET_CODE asm_arm_get_code
386#define ASM_GET_CODE_SIZE asm_arm_get_code_size
387#define ASM_START_PASS asm_arm_start_pass
388#define ASM_END_PASS asm_arm_end_pass
389#define ASM_ENTRY asm_arm_entry
390#define ASM_EXIT asm_arm_exit
391
392#define ASM_LABEL_ASSIGN asm_arm_label_assign
393#define ASM_JUMP asm_arm_b_label
394#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
395 do { \
396 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100397 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100398 } while (0)
399#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
400 do { \
401 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100402 asm_arm_bcc_label(as, ASM_ARM_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100403 } while (0)
404#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
405 do { \
406 asm_arm_cmp_reg_reg(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100407 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100408 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100409#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 +0100410
411#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_arm_mov_local_reg(as, (local_num), (reg))
412#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
413#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
414#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
415 do { \
416 asm_arm_mov_reg_i32(as, (reg_temp), (imm)); \
417 asm_arm_mov_local_reg(as, (local_num), (reg_temp)); \
418 } while (false)
419#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 +0100420#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 +0100421#define ASM_MOV_LOCAL_ADDR_TO_REG(as, local_num, reg) asm_arm_mov_reg_local_addr(as, (reg), (local_num))
422
Damien George3112cde2014-09-29 18:45:42 +0100423// TODO someone please implement lsl and asr
424#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_arm_lsl_((as), (reg_dest), (reg_shift))
425#define ASM_ASR_REG_REG(as, reg_dest, reg_shift) asm_arm_asr_((as), (reg_dest), (reg_shift))
426#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_arm_add_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
427#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_arm_sub_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
428
Damien Georgee9dac3b2014-09-29 22:10:41 +0100429// TODO someone please implement str
430#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_arm_str_reg_reg_i5((as), (reg_src), (reg_base), 0)
431#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_arm_strb_reg_reg_i5((as), (reg_src), (reg_base), 0)
432#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_arm_strh_reg_reg_i5((as), (reg_src), (reg_base), 0)
433
Damien Georgec90f59e2014-09-06 23:06:36 +0100434#else
435
436#error unknown native emitter
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200437
Damien13ed3a62013-10-08 09:05:10 +0100438#endif
439
440typedef enum {
Damienff8ed772013-10-08 22:18:32 +0100441 STACK_VALUE,
442 STACK_REG,
443 STACK_IMM,
444} stack_info_kind_t;
Damien13ed3a62013-10-08 09:05:10 +0100445
Damien Georgee9dac3b2014-09-29 22:10:41 +0100446// these enums must be distinct and the bottom 2 bits
447// must correspond to the correct MP_NATIVE_TYPE_xxx value
Damien13ed3a62013-10-08 09:05:10 +0100448typedef enum {
Damien Georgee9dac3b2014-09-29 22:10:41 +0100449 VTYPE_PYOBJ = 0x00 | MP_NATIVE_TYPE_OBJ,
450 VTYPE_BOOL = 0x00 | MP_NATIVE_TYPE_BOOL,
451 VTYPE_INT = 0x00 | MP_NATIVE_TYPE_INT,
452 VTYPE_UINT = 0x00 | MP_NATIVE_TYPE_UINT,
453
454 VTYPE_PTR = 0x10 | MP_NATIVE_TYPE_UINT, // pointer to word sized entity
455 VTYPE_PTR8 = 0x20 | MP_NATIVE_TYPE_UINT,
456 VTYPE_PTR16 = 0x30 | MP_NATIVE_TYPE_UINT,
457 VTYPE_PTR_NONE = 0x40 | MP_NATIVE_TYPE_UINT,
458
459 VTYPE_UNBOUND = 0x50 | MP_NATIVE_TYPE_OBJ,
460 VTYPE_BUILTIN_CAST = 0x60 | MP_NATIVE_TYPE_OBJ,
Damien13ed3a62013-10-08 09:05:10 +0100461} vtype_kind_t;
462
Damienff8ed772013-10-08 22:18:32 +0100463typedef struct _stack_info_t {
464 vtype_kind_t vtype;
465 stack_info_kind_t kind;
466 union {
467 int u_reg;
Damien George40f3c022014-07-03 13:25:24 +0100468 mp_int_t u_imm;
Damienff8ed772013-10-08 22:18:32 +0100469 };
470} stack_info_t;
471
Damien13ed3a62013-10-08 09:05:10 +0100472struct _emit_t {
473 int pass;
474
475 bool do_viper_types;
Damienff8ed772013-10-08 22:18:32 +0100476
Damien George2ac4af62014-08-15 16:45:41 +0100477 vtype_kind_t return_vtype;
478
Damien George7ff996c2014-09-08 23:05:16 +0100479 mp_uint_t local_vtype_alloc;
Damien13ed3a62013-10-08 09:05:10 +0100480 vtype_kind_t *local_vtype;
Damienff8ed772013-10-08 22:18:32 +0100481
Damien George7ff996c2014-09-08 23:05:16 +0100482 mp_uint_t stack_info_alloc;
Damienff8ed772013-10-08 22:18:32 +0100483 stack_info_t *stack_info;
484
Damien13ed3a62013-10-08 09:05:10 +0100485 int stack_start;
486 int stack_size;
487
488 bool last_emit_was_return_value;
489
Damien13ed3a62013-10-08 09:05:10 +0100490 scope_t *scope;
491
Damien Georgec90f59e2014-09-06 23:06:36 +0100492 ASM_T *as;
Damien13ed3a62013-10-08 09:05:10 +0100493};
494
Damien George7ff996c2014-09-08 23:05:16 +0100495emit_t *EXPORT_FUN(new)(mp_uint_t max_num_labels) {
Damien George36db6bc2014-05-07 17:24:22 +0100496 emit_t *emit = m_new0(emit_t, 1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100497 emit->as = ASM_NEW(max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100498 return emit;
499}
500
Damien George41d02b62014-01-24 22:42:28 +0000501void EXPORT_FUN(free)(emit_t *emit) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100502 ASM_FREE(emit->as, false);
Damien George36db6bc2014-05-07 17:24:22 +0100503 m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
504 m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200505 m_del_obj(emit_t, emit);
506}
507
Damien George2ac4af62014-08-15 16:45:41 +0100508STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
509 switch (op) {
510 case MP_EMIT_NATIVE_TYPE_ENABLE:
511 emit->do_viper_types = arg1;
512 break;
513
514 default: {
515 vtype_kind_t type;
516 switch (arg2) {
517 case MP_QSTR_object: type = VTYPE_PYOBJ; break;
518 case MP_QSTR_bool: type = VTYPE_BOOL; break;
519 case MP_QSTR_int: type = VTYPE_INT; break;
520 case MP_QSTR_uint: type = VTYPE_UINT; break;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100521 case MP_QSTR_ptr: type = VTYPE_PTR; break;
522 case MP_QSTR_ptr8: type = VTYPE_PTR8; break;
523 case MP_QSTR_ptr16: type = VTYPE_PTR16; break;
Damien George2ac4af62014-08-15 16:45:41 +0100524 default: printf("ViperTypeError: unknown type %s\n", qstr_str(arg2)); return;
525 }
526 if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
527 emit->return_vtype = type;
528 } else {
529 assert(arg1 < emit->local_vtype_alloc);
530 emit->local_vtype[arg1] = type;
531 }
532 break;
533 }
534 }
Damien13ed3a62013-10-08 09:05:10 +0100535}
536
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200537STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien Georged6230f62014-09-23 14:10:03 +0000538 DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope);
539
Damien13ed3a62013-10-08 09:05:10 +0100540 emit->pass = pass;
541 emit->stack_start = 0;
542 emit->stack_size = 0;
543 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100544 emit->scope = scope;
545
Damien George36db6bc2014-05-07 17:24:22 +0100546 // allocate memory for keeping track of the types of locals
547 if (emit->local_vtype_alloc < scope->num_locals) {
548 emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
549 emit->local_vtype_alloc = scope->num_locals;
Damienff8ed772013-10-08 22:18:32 +0100550 }
Damien George36db6bc2014-05-07 17:24:22 +0100551
552 // allocate memory for keeping track of the objects on the stack
553 // XXX don't know stack size on entry, and it should be maximum over all scopes
Damienff8ed772013-10-08 22:18:32 +0100554 if (emit->stack_info == NULL) {
Damien George36db6bc2014-05-07 17:24:22 +0100555 emit->stack_info_alloc = scope->stack_size + 50;
Damienff8ed772013-10-08 22:18:32 +0100556 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100557 }
558
Damien George2ac4af62014-08-15 16:45:41 +0100559 // set default type for return and arguments
560 emit->return_vtype = VTYPE_PYOBJ;
Damien Georgea5190a72014-08-15 22:39:08 +0100561 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100562 emit->local_vtype[i] = VTYPE_PYOBJ;
563 }
Damien Georgea5190a72014-08-15 22:39:08 +0100564
565 // local variables begin unbound, and have unknown type
566 for (mp_uint_t i = emit->scope->num_pos_args; i < emit->local_vtype_alloc; i++) {
567 emit->local_vtype[i] = VTYPE_UNBOUND;
568 }
569
570 // values on stack begin unbound
571 for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100572 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgea5190a72014-08-15 22:39:08 +0100573 emit->stack_info[i].vtype = VTYPE_UNBOUND;
Damien13ed3a62013-10-08 09:05:10 +0100574 }
575
Damien Georgec90f59e2014-09-06 23:06:36 +0100576 ASM_START_PASS(emit->as, pass == MP_PASS_EMIT ? ASM_PASS_EMIT : ASM_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100577
578 // entry to function
579 int num_locals = 0;
Damien George36db6bc2014-05-07 17:24:22 +0100580 if (pass > MP_PASS_SCOPE) {
Damien13ed3a62013-10-08 09:05:10 +0100581 num_locals = scope->num_locals - REG_LOCAL_NUM;
582 if (num_locals < 0) {
583 num_locals = 0;
584 }
585 emit->stack_start = num_locals;
586 num_locals += scope->stack_size;
587 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100588 ASM_ENTRY(emit->as, num_locals);
Damien13ed3a62013-10-08 09:05:10 +0100589
590 // initialise locals from parameters
Damien3ef4abb2013-10-12 16:53:13 +0100591#if N_X64
Damien George2827d622014-04-27 15:50:52 +0100592 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100593 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100594 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100595 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100596 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +0100597 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100598 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Damien George81057362014-09-07 01:06:19 +0100599 } else if (i == 3) {
600 asm_x64_mov_r64_to_local(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +0100601 } else {
602 // TODO not implemented
603 assert(0);
604 }
605 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100606#elif N_X86
607 for (int i = 0; i < scope->num_pos_args; i++) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100608 if (i == 0) {
Damien George03281b32014-09-06 22:38:50 +0000609 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100610 } else if (i == 1) {
Damien George03281b32014-09-06 22:38:50 +0000611 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +0000612 } else if (i == 2) {
613 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +0100614 } else {
Damien George03281b32014-09-06 22:38:50 +0000615 asm_x86_mov_arg_to_r32(emit->as, i, REG_TEMP0);
616 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, i - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +0100617 }
618 }
Damien3ef4abb2013-10-12 16:53:13 +0100619#elif N_THUMB
Damien George2827d622014-04-27 15:50:52 +0100620 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100621 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100622 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100623 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100624 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +0100625 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100626 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Damien13ed3a62013-10-08 09:05:10 +0100627 } else if (i == 3) {
628 asm_thumb_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
629 } else {
630 // TODO not implemented
631 assert(0);
632 }
633 }
634
Damien Georgee9dac3b2014-09-29 22:10:41 +0100635 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100636 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200637#elif N_ARM
638 for (int i = 0; i < scope->num_pos_args; i++) {
639 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100640 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200641 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100642 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200643 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100644 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200645 } else if (i == 3) {
646 asm_arm_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
647 } else {
648 // TODO not implemented
649 assert(0);
650 }
651 }
652
Damien Georgee9dac3b2014-09-29 22:10:41 +0100653 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100654 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
Damien Georgec90f59e2014-09-06 23:06:36 +0100655#else
656 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +0100657#endif
658}
659
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200660STATIC void emit_native_end_pass(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100661 if (!emit->last_emit_was_return_value) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100662 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100663 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100664 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100665
666 // check stack is back to zero size
667 if (emit->stack_size != 0) {
668 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
669 }
670
Damien George36db6bc2014-05-07 17:24:22 +0100671 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100672 void *f = ASM_GET_CODE(emit->as);
673 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100674
675 // compute type signature
Damien Georgee9dac3b2014-09-29 22:10:41 +0100676 // note that the lower 2 bits of a vtype are tho correct MP_NATIVE_TYPE_xxx
Damien George2ac4af62014-08-15 16:45:41 +0100677 mp_uint_t type_sig = emit->return_vtype & 3;
678 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
679 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
680 }
681
682 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 +0100683 }
684}
685
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200686STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100687 return emit->last_emit_was_return_value;
688}
689
Damien Georged6230f62014-09-23 14:10:03 +0000690STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
691 DEBUG_printf(" adjust_stack; stack_size=%d, delta=%d\n", emit->stack_size, stack_size_delta);
692 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100693 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100694 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100695 emit->scope->stack_size = emit->stack_size;
696 }
697}
698
Damien Georged6230f62014-09-23 14:10:03 +0000699STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
700 // If we are adjusting the stack in a positive direction (pushing) then we
701 // need to fill in values for the stack kind and vtype of the newly-pushed
702 // entries. These should be set to "value" (ie not reg or imm) because we
703 // should only need to adjust the stack due to a jump to this part in the
704 // code (and hence we have settled the stack before the jump).
705 for (mp_int_t i = 0; i < delta; i++) {
706 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
707 si->kind = STACK_VALUE;
708 si->vtype = VTYPE_PYOBJ; // XXX we don't know the vtype...
709 }
710 adjust_stack(emit, delta);
711}
712
713STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
714}
715
Damienff8ed772013-10-08 22:18:32 +0100716/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200717STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100718 adjust_stack(emit, stack_size_delta);
719 emit->last_emit_was_return_value = false;
720}
Damienff8ed772013-10-08 22:18:32 +0100721*/
Damien13ed3a62013-10-08 09:05:10 +0100722
Damienff8ed772013-10-08 22:18:32 +0100723// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000724STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100725 emit->last_emit_was_return_value = false;
726 // settle the stack
727 /*
728 if (regs_needed != 0) {
729 for (int i = 0; i < emit->stack_size; i++) {
730 switch (emit->stack_info[i].kind) {
731 case STACK_VALUE:
732 break;
733
734 case STACK_REG:
735 // TODO only push reg if in regs_needed
736 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100737 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100738 break;
739
740 case STACK_IMM:
741 // don't think we ever need to push imms for settling
742 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
743 break;
744 }
745 }
746 }
747 */
Damien13ed3a62013-10-08 09:05:10 +0100748}
749
Damien George3112cde2014-09-29 18:45:42 +0100750// depth==0 is top, depth==1 is before top, etc
751STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
752 return &emit->stack_info[emit->stack_size - 1 - depth];
753}
754
755// depth==0 is top, depth==1 is before top, etc
756STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
757 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100758}
Damien13ed3a62013-10-08 09:05:10 +0100759
Damiend2755ec2013-10-16 23:58:48 +0100760// pos=1 is TOS, pos=2 is next, etc
761// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200762STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100763 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100764 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100765 if (i != skip_stack_pos) {
766 stack_info_t *si = &emit->stack_info[i];
767 if (si->kind == STACK_REG && si->u_reg == reg_needed) {
768 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100769 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100770 }
Damienff8ed772013-10-08 22:18:32 +0100771 }
772 }
773}
Damien13ed3a62013-10-08 09:05:10 +0100774
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200775STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100776 for (int i = 0; i < emit->stack_size; i++) {
777 stack_info_t *si = &emit->stack_info[i];
778 if (si->kind == STACK_REG) {
779 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100780 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100781 }
Damien13ed3a62013-10-08 09:05:10 +0100782 }
783}
784
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200785STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +0000786 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +0100787 for (int i = 0; i < emit->stack_size; i++) {
788 stack_info_t *si = &emit->stack_info[i];
789 if (si->kind == STACK_REG) {
Damien Georged6230f62014-09-23 14:10:03 +0000790 DEBUG_printf(" reg(%u) to local(%u)\n", si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100791 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100792 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100793 }
794 }
795 for (int i = 0; i < emit->stack_size; i++) {
796 stack_info_t *si = &emit->stack_info[i];
797 if (si->kind == STACK_IMM) {
Damien Georged6230f62014-09-23 14:10:03 +0000798 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +0100799 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100800 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +0100801 }
802 }
803}
804
805// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200806STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100807 need_reg_single(emit, reg_dest, pos);
808 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +0100809 *vtype = si->vtype;
810 switch (si->kind) {
811 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +0100812 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100813 break;
814
Damienff8ed772013-10-08 22:18:32 +0100815 case STACK_REG:
816 if (si->u_reg != reg_dest) {
Damien George3112cde2014-09-29 18:45:42 +0100817 ASM_MOV_REG_REG(emit->as, reg_dest, si->u_reg);
Damien13ed3a62013-10-08 09:05:10 +0100818 }
819 break;
820
Damienff8ed772013-10-08 22:18:32 +0100821 case STACK_IMM:
Damien Georgec90f59e2014-09-06 23:06:36 +0100822 ASM_MOV_IMM_TO_REG(emit->as, si->u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100823 break;
824 }
Damien13ed3a62013-10-08 09:05:10 +0100825}
826
Damien Georgee9dac3b2014-09-29 22:10:41 +0100827// does an efficient X=pop(); discard(); push(X)
828// needs a (non-temp) register in case the poped element was stored in the stack
829STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
830 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
831 si[0] = si[1];
832 if (si->kind == STACK_VALUE) {
833 // if folded element was on the stack we need to put it in a register
834 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
835 si->kind = STACK_REG;
836 si->u_reg = reg_dest;
837 }
838 adjust_stack(emit, -1);
839}
840
841// If stacked value is in a register and the register is not r1 or r2, then
842// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
843STATIC 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 +0100844 emit->last_emit_was_return_value = false;
845 stack_info_t *si = peek_stack(emit, 0);
Damien Georgee9dac3b2014-09-29 22:10:41 +0100846 if (si->kind == STACK_REG && si->u_reg != not_r1 && si->u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +0100847 *vtype = si->vtype;
848 *reg_dest = si->u_reg;
849 need_reg_single(emit, *reg_dest, 1);
850 } else {
851 emit_access_stack(emit, 1, vtype, *reg_dest);
852 }
853 adjust_stack(emit, -1);
854}
855
Damien Georgee6ce10a2014-09-06 18:38:20 +0100856STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +0100857 emit->last_emit_was_return_value = false;
858 adjust_stack(emit, -1);
859}
860
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200861STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100862 emit->last_emit_was_return_value = false;
863 emit_access_stack(emit, 1, vtype, reg_dest);
864 adjust_stack(emit, -1);
865}
866
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200867STATIC 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 +0100868 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100869 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +0100870}
871
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200872STATIC 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 +0100873 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100874 emit_pre_pop_reg(emit, vtypeb, regb);
875 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100876}
877
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200878STATIC void emit_post(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100879}
880
Damien Georgee9dac3b2014-09-29 22:10:41 +0100881STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
882 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
883 si->vtype = new_vtype;
884}
885
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200886STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +0100887 stack_info_t *si = &emit->stack_info[emit->stack_size];
888 si->vtype = vtype;
889 si->kind = STACK_REG;
890 si->u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +0100891 adjust_stack(emit, 1);
892}
893
Damien George40f3c022014-07-03 13:25:24 +0100894STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +0100895 stack_info_t *si = &emit->stack_info[emit->stack_size];
896 si->vtype = vtype;
897 si->kind = STACK_IMM;
898 si->u_imm = imm;
899 adjust_stack(emit, 1);
900}
901
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200902STATIC 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 +0100903 emit_post_push_reg(emit, vtypea, rega);
904 emit_post_push_reg(emit, vtypeb, regb);
905}
906
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200907STATIC 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 +0100908 emit_post_push_reg(emit, vtypea, rega);
909 emit_post_push_reg(emit, vtypeb, regb);
910 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100911}
912
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200913STATIC 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 +0100914 emit_post_push_reg(emit, vtypea, rega);
915 emit_post_push_reg(emit, vtypeb, regb);
916 emit_post_push_reg(emit, vtypec, regc);
917 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +0100918}
919
Damien George7fe21912014-08-16 22:31:57 +0100920STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +0100921 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100922 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100923}
924
Damien George7fe21912014-08-16 22:31:57 +0100925STATIC 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 +0100926 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100927 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
928 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100929}
930
Damien George40f3c022014-07-03 13:25:24 +0100931// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100932STATIC 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 +0100933 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100934 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
935 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +0100936}
937
Damien George7fe21912014-08-16 22:31:57 +0100938STATIC 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 +0000939 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100940 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
941 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
942 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +0000943}
944
Damien George40f3c022014-07-03 13:25:24 +0100945// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100946STATIC 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 +0100947 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100948 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
949 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
950 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
951 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +0100952}
953
Damien George86de21b2014-08-16 22:06:11 +0100954// vtype of all n_pop objects is VTYPE_PYOBJ
955// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
956// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
957// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
958STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
959 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +0100960
Damien George86de21b2014-08-16 22:06:11 +0100961 // First, store any immediate values to their respective place on the stack.
962 for (mp_uint_t i = 0; i < n_pop; i++) {
963 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
964 // must push any imm's to stack
965 // must convert them to VTYPE_PYOBJ for viper code
966 if (si->kind == STACK_IMM) {
967 si->kind = STACK_VALUE;
968 switch (si->vtype) {
969 case VTYPE_PYOBJ:
Damien Georgec90f59e2014-09-06 23:06:36 +0100970 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 +0100971 break;
972 case VTYPE_BOOL:
973 if (si->u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100974 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 +0100975 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +0100976 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 +0100977 }
978 si->vtype = VTYPE_PYOBJ;
979 break;
980 case VTYPE_INT:
981 case VTYPE_UINT:
Damien Georgec90f59e2014-09-06 23:06:36 +0100982 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 +0100983 si->vtype = VTYPE_PYOBJ;
984 break;
985 default:
986 // not handled
987 assert(0);
988 }
989 }
990
991 // verify that this value is on the stack
992 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +0100993 }
Damien George86de21b2014-08-16 22:06:11 +0100994
995 // Second, convert any non-VTYPE_PYOBJ to that type.
996 for (mp_uint_t i = 0; i < n_pop; i++) {
997 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
998 if (si->vtype != VTYPE_PYOBJ) {
999 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +01001000 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001001 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 +01001002 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001003 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001004 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001005 }
1006 }
1007
1008 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1009 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +01001010 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001011}
1012
1013// vtype of all n_push objects is VTYPE_PYOBJ
1014STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1015 need_reg_all(emit);
1016 for (mp_uint_t i = 0; i < n_push; i++) {
1017 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1018 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1019 }
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 adjust_stack(emit, n_push);
1022}
1023
Damien George7ff996c2014-09-08 23:05:16 +01001024STATIC void emit_native_load_id(emit_t *emit, qstr qst) {
1025 emit_common_load_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001026}
1027
Damien George7ff996c2014-09-08 23:05:16 +01001028STATIC void emit_native_store_id(emit_t *emit, qstr qst) {
1029 emit_common_store_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001030}
1031
Damien George7ff996c2014-09-08 23:05:16 +01001032STATIC void emit_native_delete_id(emit_t *emit, qstr qst) {
1033 emit_common_delete_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001034}
1035
Damien George7ff996c2014-09-08 23:05:16 +01001036STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001037 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001038 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001039 // need to commit stack because we can jump here from elsewhere
1040 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001041 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001042 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001043}
1044
Damien Georgecdd96df2014-04-06 12:58:40 +01001045STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1046 DEBUG_printf("import_name %s\n", qstr_str(qst));
1047 vtype_kind_t vtype_fromlist;
1048 vtype_kind_t vtype_level;
1049 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1050 assert(vtype_fromlist == VTYPE_PYOBJ);
1051 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001052 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001053 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001054}
1055
Damien Georgecdd96df2014-04-06 12:58:40 +01001056STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1057 DEBUG_printf("import_from %s\n", qstr_str(qst));
1058 emit_native_pre(emit);
1059 vtype_kind_t vtype_module;
1060 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1061 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001062 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = 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
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001066STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001067 DEBUG_printf("import_star\n");
1068 vtype_kind_t vtype_module;
1069 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1070 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001071 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001072 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001073}
1074
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001075STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001076 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001077 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001078 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001079 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001080 if (emit->do_viper_types) {
1081 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001082 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1083 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1084 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien13ed3a62013-10-08 09:05:10 +01001085 default: assert(0); vtype = 0; val = 0; // shouldn't happen
1086 }
1087 } else {
1088 vtype = VTYPE_PYOBJ;
1089 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001090 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1091 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1092 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien13ed3a62013-10-08 09:05:10 +01001093 default: assert(0); vtype = 0; val = 0; // shouldn't happen
1094 }
1095 }
1096 emit_post_push_imm(emit, vtype, val);
1097}
1098
Damien George40f3c022014-07-03 13:25:24 +01001099STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001100 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001101 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001102 if (emit->do_viper_types) {
1103 emit_post_push_imm(emit, VTYPE_INT, arg);
1104 } else {
1105 emit_post_push_imm(emit, VTYPE_PYOBJ, (arg << 1) | 1);
1106 }
1107}
1108
Damien Georgecdd96df2014-04-06 12:58:40 +01001109STATIC void emit_native_load_const_int(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001110 DEBUG_printf("load_const_int %s\n", qstr_str(qst));
Damien Georgecdd96df2014-04-06 12:58:40 +01001111 // for viper: load integer, check fits in 32 bits
1112 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001113 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_INT, qst, REG_ARG_1);
Damien Georgecdd96df2014-04-06 12:58:40 +01001114 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001115}
1116
Damien George7ff996c2014-09-08 23:05:16 +01001117STATIC void emit_native_load_const_dec(emit_t *emit, qstr qst) {
Damien6ba13142013-11-02 20:34:54 +00001118 // for viper, a float/complex is just a Python object
Damien Georgece8f07a2014-03-27 23:30:26 +00001119 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001120 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_DEC, qst, REG_ARG_1);
Damien6ba13142013-11-02 20:34:54 +00001121 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001122}
1123
Damien George7ff996c2014-09-08 23:05:16 +01001124STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001125 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001126 // TODO: Eventually we want to be able to work with raw pointers in viper to
1127 // do native array access. For now we just load them as any other object.
1128 /*
Damien13ed3a62013-10-08 09:05:10 +01001129 if (emit->do_viper_types) {
1130 // not implemented properly
1131 // load a pointer to the asciiz string?
1132 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001133 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001134 } else
1135 */
1136 {
Damien Georgeb601d952014-06-30 05:17:25 +01001137 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +01001138 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_BYTES, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001139 } else {
Damien George7ff996c2014-09-08 23:05:16 +01001140 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001141 }
Damien13ed3a62013-10-08 09:05:10 +01001142 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1143 }
1144}
1145
Damien George3558f622014-04-20 17:50:40 +01001146STATIC void emit_native_load_null(emit_t *emit) {
1147 emit_native_pre(emit);
1148 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1149}
1150
Damien George7ff996c2014-09-08 23:05:16 +01001151STATIC 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 +01001152 vtype_kind_t vtype = emit->local_vtype[local_num];
1153 if (vtype == VTYPE_UNBOUND) {
Damien George7ff996c2014-09-08 23:05:16 +01001154 printf("ViperTypeError: local %s used before type known\n", qstr_str(qst));
Damien13ed3a62013-10-08 09:05:10 +01001155 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001156 emit_native_pre(emit);
Damien3ef4abb2013-10-12 16:53:13 +01001157#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001158 if (local_num == 0) {
1159 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001160 } else if (local_num == 1) {
1161 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1162 } else if (local_num == 2) {
1163 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001164 } else {
Damien George0b610de2014-09-29 16:25:04 +01001165 need_reg_single(emit, REG_TEMP0, 0);
1166 asm_x64_mov_local_to_r64(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1167 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001168 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001169#elif N_X86
1170 if (local_num == 0) {
1171 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001172 } else if (local_num == 1) {
1173 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001174 } else if (local_num == 2) {
1175 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001176 } else {
Damien George0b610de2014-09-29 16:25:04 +01001177 need_reg_single(emit, REG_TEMP0, 0);
1178 asm_x86_mov_local_to_r32(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1179 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien Georgec90f59e2014-09-06 23:06:36 +01001180 }
Damien3ef4abb2013-10-12 16:53:13 +01001181#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001182 if (local_num == 0) {
1183 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1184 } else if (local_num == 1) {
1185 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1186 } else if (local_num == 2) {
1187 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1188 } else {
Damien George0b610de2014-09-29 16:25:04 +01001189 need_reg_single(emit, REG_TEMP0, 0);
1190 asm_thumb_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1191 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001192 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001193#elif N_ARM
1194 if (local_num == 0) {
1195 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1196 } else if (local_num == 1) {
1197 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1198 } else if (local_num == 2) {
1199 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1200 } else {
Damien George0b610de2014-09-29 16:25:04 +01001201 need_reg_single(emit, REG_TEMP0, 0);
1202 asm_arm_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1203 emit_post_push_reg(emit, vtype, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001204 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001205#else
1206 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001207#endif
1208}
1209
Damien George7ff996c2014-09-08 23:05:16 +01001210STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001211 // not implemented
1212 // in principle could support this quite easily (ldr r0, [r0, #0]) and then get closed over variables!
1213 assert(0);
1214}
1215
Damien George7ff996c2014-09-08 23:05:16 +01001216STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001217 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001218 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001219 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001220 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1221}
1222
Damien George7ff996c2014-09-08 23:05:16 +01001223STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001224 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001225 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001226 // check for builtin casting operators
1227 if (emit->do_viper_types && qst == MP_QSTR_int) {
1228 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1229 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1230 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1231 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1232 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1233 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1234 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1235 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1236 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
1237 } else {
1238 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1239 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1240 }
Damien13ed3a62013-10-08 09:05:10 +01001241}
1242
Damien George7ff996c2014-09-08 23:05:16 +01001243STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001244 // depends on type of subject:
1245 // - integer, function, pointer to integers: error
1246 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001247 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001248 vtype_kind_t vtype_base;
1249 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1250 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001251 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001252 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1253}
1254
Damien George7ff996c2014-09-08 23:05:16 +01001255STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001256 vtype_kind_t vtype_base;
1257 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1258 assert(vtype_base == VTYPE_PYOBJ);
1259 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001260 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001261}
1262
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001263STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001264 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001265 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001266 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001267}
1268
Damien George729f7b42014-04-17 22:10:53 +01001269STATIC void emit_native_load_subscr(emit_t *emit) {
1270 vtype_kind_t vtype_lhs, vtype_rhs;
1271 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_2, &vtype_lhs, REG_ARG_1);
1272 if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George7fe21912014-08-16 22:31:57 +01001273 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 +01001274 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1275 } else {
1276 printf("ViperTypeError: can't do subscr of types %d and %d\n", vtype_lhs, vtype_rhs);
Damien George729f7b42014-04-17 22:10:53 +01001277 }
1278}
1279
Damien George7ff996c2014-09-08 23:05:16 +01001280STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001281 vtype_kind_t vtype;
Damien3ef4abb2013-10-12 16:53:13 +01001282#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001283 if (local_num == 0) {
1284 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001285 } else if (local_num == 1) {
1286 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1287 } else if (local_num == 2) {
1288 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001289 } else {
Damien George0b610de2014-09-29 16:25:04 +01001290 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1291 asm_x64_mov_r64_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +01001292 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001293#elif N_X86
1294 if (local_num == 0) {
1295 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001296 } else if (local_num == 1) {
1297 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001298 } else if (local_num == 2) {
1299 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001300 } else {
Damien George0b610de2014-09-29 16:25:04 +01001301 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1302 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +01001303 }
Damien3ef4abb2013-10-12 16:53:13 +01001304#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001305 if (local_num == 0) {
1306 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1307 } else if (local_num == 1) {
1308 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1309 } else if (local_num == 2) {
1310 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1311 } else {
Damien George0b610de2014-09-29 16:25:04 +01001312 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1313 asm_thumb_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001314 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001315#elif N_ARM
1316 if (local_num == 0) {
1317 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1318 } else if (local_num == 1) {
1319 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1320 } else if (local_num == 2) {
1321 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1322 } else {
Damien George0b610de2014-09-29 16:25:04 +01001323 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1324 asm_arm_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001325 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001326#else
1327 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001328#endif
1329
1330 emit_post(emit);
1331
1332 // check types
1333 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1334 // first time this local is assigned, so give it a type of the object stored in it
1335 emit->local_vtype[local_num] = vtype;
1336 } else if (emit->local_vtype[local_num] != vtype) {
1337 // type of local is not the same as object stored in it
Damien George7ff996c2014-09-08 23:05:16 +01001338 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 +01001339 }
1340}
1341
Damien George7ff996c2014-09-08 23:05:16 +01001342STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001343 // not implemented
1344 assert(0);
1345}
1346
Damien George7ff996c2014-09-08 23:05:16 +01001347STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001348 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001349 vtype_kind_t vtype;
1350 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1351 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001352 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001353 emit_post(emit);
1354}
1355
Damien George7ff996c2014-09-08 23:05:16 +01001356STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001357 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001358 if (vtype == VTYPE_PYOBJ) {
1359 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1360 } else {
1361 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001362 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 +01001363 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001364 }
Damien George7ff996c2014-09-08 23:05:16 +01001365 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001366 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001367}
1368
Damien George7ff996c2014-09-08 23:05:16 +01001369STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001370 vtype_kind_t vtype_base, vtype_val;
1371 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1372 assert(vtype_base == VTYPE_PYOBJ);
1373 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001374 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001375 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001376}
1377
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001378STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001379 DEBUG_printf("store_subscr\n");
1380 // need to compile: base[index] = value
1381
1382 // pop: index, base, value
1383 // optimise case where index is an immediate
1384 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1385
1386 if (vtype_base == VTYPE_PYOBJ) {
1387 // standard Python call
1388 vtype_kind_t vtype_index, vtype_value;
1389 emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3);
1390 assert(vtype_index == VTYPE_PYOBJ);
1391 assert(vtype_value == VTYPE_PYOBJ);
1392 emit_call(emit, MP_F_OBJ_SUBSCR);
1393 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001394 // viper store
1395 // TODO The different machine architectures have very different
1396 // capabilities and requirements for stores, so probably best to
1397 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001398 stack_info_t *top = peek_stack(emit, 0);
1399 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1400 // index is an immediate
1401 mp_int_t index_value = top->u_imm;
1402 emit_pre_pop_discard(emit); // discard index
1403 vtype_kind_t vtype_value;
1404 int reg_base = REG_ARG_1;
1405 int reg_index = REG_ARG_2;
1406 int reg_value = REG_ARG_3;
1407 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001408 #if N_X86
1409 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1410 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1411 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001412 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001413 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001414 switch (vtype_base) {
1415 case VTYPE_PTR8: {
1416 // pointer to 8-bit memory
1417 // TODO optimise to use thumb strb r1, [r2, r3]
1418 if (index_value != 0) {
1419 // index is non-zero
1420 #if N_THUMB
1421 if (index_value > 0 && index_value < 32) {
1422 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1423 break;
1424 }
1425 #endif
1426 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
1427 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1428 reg_base = reg_index;
1429 }
1430 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1431 break;
1432 }
1433 case VTYPE_PTR16: {
1434 // pointer to 16-bit memory
1435 if (index_value != 0) {
1436 // index is a non-zero immediate
1437 #if N_THUMB
1438 if (index_value > 0 && index_value < 32) {
1439 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1440 break;
1441 }
1442 #endif
1443 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
1444 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1445 reg_base = reg_index;
1446 }
1447 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1448 break;
1449 }
1450 default:
1451 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1452 }
1453 } else {
1454 // index is not an immediate
1455 vtype_kind_t vtype_index, vtype_value;
1456 int reg_index = REG_ARG_2;
1457 int reg_value = REG_ARG_3;
1458 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1459 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001460 #if N_X86
1461 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1462 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1463 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001464 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001465 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001466 switch (vtype_base) {
1467 case VTYPE_PTR8: {
1468 // pointer to 8-bit memory
1469 // TODO optimise to use thumb strb r1, [r2, r3]
1470 assert(vtype_index == VTYPE_INT);
1471 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1472 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1473 break;
1474 }
1475 case VTYPE_PTR16: {
1476 // pointer to 16-bit memory
1477 assert(vtype_index == VTYPE_INT);
1478 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1479 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1480 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1481 break;
1482 }
1483 default:
1484 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1485 }
1486 }
1487
1488 }
Damien13ed3a62013-10-08 09:05:10 +01001489}
1490
Damien George7ff996c2014-09-08 23:05:16 +01001491STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001492 // TODO implement me!
Damien13ed3a62013-10-08 09:05:10 +01001493 // could support for Python types, just set to None (so GC can reclaim it)
Damien13ed3a62013-10-08 09:05:10 +01001494}
1495
Damien George7ff996c2014-09-08 23:05:16 +01001496STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001497 // TODO implement me!
Damien9ecbcff2013-12-11 00:41:43 +00001498}
1499
Damien Georgee6ce10a2014-09-06 18:38:20 +01001500STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1501 emit_native_pre(emit);
1502 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1503 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001504}
1505
Damien Georgee6ce10a2014-09-06 18:38:20 +01001506STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1507 emit_native_pre(emit);
1508 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1509 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001510}
1511
Damien Georgee6ce10a2014-09-06 18:38:20 +01001512STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001513 vtype_kind_t vtype_base;
1514 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1515 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001516 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 +01001517 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001518}
1519
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001520STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001521 vtype_kind_t vtype_index, vtype_base;
1522 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1523 assert(vtype_index == VTYPE_PYOBJ);
1524 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001525 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 +01001526}
1527
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001528STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001529 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001530 vtype_kind_t vtype;
1531 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1532 emit_post_push_reg_reg(emit, vtype, REG_TEMP0, vtype, REG_TEMP0);
1533}
1534
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001535STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001536 vtype_kind_t vtype0, vtype1;
1537 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1538 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1539}
1540
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001541STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001542 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001543 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001544 emit_post(emit);
1545}
1546
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001547STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001548 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001549 vtype_kind_t vtype0, vtype1;
1550 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1551 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001552}
1553
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001554STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001555 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001556 vtype_kind_t vtype0, vtype1, vtype2;
1557 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1558 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1559}
1560
Damien George7ff996c2014-09-08 23:05:16 +01001561STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001562 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001563 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001564 // need to commit stack because we are jumping elsewhere
1565 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001566 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001567 emit_post(emit);
1568}
1569
Damien George7ff996c2014-09-08 23:05:16 +01001570STATIC void emit_native_jump_helper(emit_t *emit, mp_uint_t label, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001571 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien George6f813482014-09-29 16:41:37 +01001572 switch (vtype) {
1573 case VTYPE_PYOBJ:
1574 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1575 if (!pop) {
1576 adjust_stack(emit, 1);
1577 }
1578 emit_call(emit, MP_F_OBJ_IS_TRUE);
1579 break;
1580 case VTYPE_BOOL:
1581 case VTYPE_INT:
1582 case VTYPE_UINT:
1583 emit_pre_pop_reg(emit, &vtype, REG_RET);
1584 if (!pop) {
1585 adjust_stack(emit, 1);
1586 }
1587 break;
1588 default:
1589 printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
1590 assert(0);
Damien13ed3a62013-10-08 09:05:10 +01001591 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001592 // need to commit stack because we may jump elsewhere
1593 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001594}
1595
Damien George7ff996c2014-09-08 23:05:16 +01001596STATIC void emit_native_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001597 DEBUG_printf("pop_jump_if_true(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001598 emit_native_jump_helper(emit, label, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001599 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien1a6633a2013-11-03 13:58:19 +00001600 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001601}
Damien1a6633a2013-11-03 13:58:19 +00001602
Damien George7ff996c2014-09-08 23:05:16 +01001603STATIC void emit_native_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001604 DEBUG_printf("pop_jump_if_false(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001605 emit_native_jump_helper(emit, label, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001606 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001607 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001608}
Damien Georgea32c1e42014-05-07 18:30:52 +01001609
Damien George7ff996c2014-09-08 23:05:16 +01001610STATIC void emit_native_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001611 DEBUG_printf("jump_if_true_or_pop(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001612 emit_native_jump_helper(emit, label, false);
Damien Georgec90f59e2014-09-06 23:06:36 +01001613 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001614 adjust_stack(emit, -1);
1615 emit_post(emit);
1616}
1617
Damien George7ff996c2014-09-08 23:05:16 +01001618STATIC void emit_native_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001619 DEBUG_printf("jump_if_false_or_pop(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001620 emit_native_jump_helper(emit, label, false);
Damien Georgec90f59e2014-09-06 23:06:36 +01001621 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001622 adjust_stack(emit, -1);
1623 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001624}
1625
Damien George7ff996c2014-09-08 23:05:16 +01001626STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien George25c84642014-05-30 15:20:41 +01001627 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001628}
Damien Georgea32c1e42014-05-07 18:30:52 +01001629
Damien George7ff996c2014-09-08 23:05:16 +01001630STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgea32c1e42014-05-07 18:30:52 +01001631 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001632}
Damien Georgea32c1e42014-05-07 18:30:52 +01001633
Damien George7ff996c2014-09-08 23:05:16 +01001634STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001635 // not supported, or could be with runtime call
1636 assert(0);
1637}
Damien Georgeb601d952014-06-30 05:17:25 +01001638
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001639STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001640 assert(0);
1641}
Damien Georgeb601d952014-06-30 05:17:25 +01001642
Damien George7ff996c2014-09-08 23:05:16 +01001643STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001644 emit_native_pre(emit);
1645 // need to commit stack because we may jump elsewhere
1646 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001647 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 +01001648 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001649 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001650 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001651}
Damien Georgeb601d952014-06-30 05:17:25 +01001652
Damien George7ff996c2014-09-08 23:05:16 +01001653STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001654 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001655}
Damien Georgeb601d952014-06-30 05:17:25 +01001656
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001657STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001658 emit_pre_pop_discard(emit);
1659 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001660}
Damiend2755ec2013-10-16 23:58:48 +01001661
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001662STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001663 // perhaps the difficult one, as we want to rewrite for loops using native code
1664 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001665
1666 vtype_kind_t vtype;
1667 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1668 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001669 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01001670 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001671}
Damiend2755ec2013-10-16 23:58:48 +01001672
Damien George7ff996c2014-09-08 23:05:16 +01001673STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001674 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001675 vtype_kind_t vtype;
1676 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1677 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001678 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01001679 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
1680 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01001681 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1682}
1683
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001684STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001685 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001686 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001687 adjust_stack(emit, -1);
1688 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001689}
1690
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001691STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001692 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001693 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001694 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001695 emit_post(emit);
1696}
1697
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001698STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeb601d952014-06-30 05:17:25 +01001699 /*
1700 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001701 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001702 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01001703 emit_post(emit);
1704 */
Damien13ed3a62013-10-08 09:05:10 +01001705}
1706
Damien Georged17926d2014-03-30 13:35:08 +01001707STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001708 vtype_kind_t vtype;
1709 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1710 assert(vtype == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001711 if (op == MP_UNARY_OP_NOT) {
Damien Georged6230f62014-09-23 14:10:03 +00001712 // we need to synthesise this operation by converting to bool first
1713 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
Damien George3112cde2014-09-29 18:45:42 +01001714 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgeb601d952014-06-30 05:17:25 +01001715 }
Damien Georged6230f62014-09-23 14:10:03 +00001716 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1717 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001718}
1719
Damien Georged17926d2014-03-30 13:35:08 +01001720STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001721 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01001722 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
1723 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01001724 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01001725 #if N_X64 || N_X86
1726 // special cases for x86 and shifting
1727 if (op == MP_BINARY_OP_LSHIFT
1728 || op == MP_BINARY_OP_INPLACE_LSHIFT
1729 || op == MP_BINARY_OP_RSHIFT
1730 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1731 #if N_X64
1732 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
1733 #else
1734 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
1735 #endif
1736 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1737 ASM_LSL_REG(emit->as, REG_RET);
1738 } else {
1739 ASM_ASR_REG(emit->as, REG_RET);
1740 }
1741 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
1742 return;
1743 }
1744 #endif
1745 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001746 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001747 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
1748 if (0) {
1749 // dummy
1750 #if !(N_X64 || N_X86)
1751 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1752 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00001753 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001754 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1755 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1756 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1757 #endif
1758 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
1759 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1760 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1761 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
1762 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1763 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1764 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
1765 // comparison ops are (in enum order):
1766 // MP_BINARY_OP_LESS
1767 // MP_BINARY_OP_MORE
1768 // MP_BINARY_OP_EQUAL
1769 // MP_BINARY_OP_LESS_EQUAL
1770 // MP_BINARY_OP_MORE_EQUAL
1771 // MP_BINARY_OP_NOT_EQUAL
1772 #if N_X64
1773 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
1774 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
1775 static byte ops[6] = {
1776 ASM_X64_CC_JL,
1777 ASM_X64_CC_JG,
1778 ASM_X64_CC_JE,
1779 ASM_X64_CC_JLE,
1780 ASM_X64_CC_JGE,
1781 ASM_X64_CC_JNE,
1782 };
1783 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1784 #elif N_X86
1785 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
1786 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
1787 static byte ops[6] = {
1788 ASM_X86_CC_JL,
1789 ASM_X86_CC_JG,
1790 ASM_X86_CC_JE,
1791 ASM_X86_CC_JLE,
1792 ASM_X86_CC_JGE,
1793 ASM_X86_CC_JNE,
1794 };
1795 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1796 #elif N_THUMB
1797 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
1798 static uint16_t ops[6] = {
1799 ASM_THUMB_OP_ITE_GE,
1800 ASM_THUMB_OP_ITE_GT,
1801 ASM_THUMB_OP_ITE_EQ,
1802 ASM_THUMB_OP_ITE_GT,
1803 ASM_THUMB_OP_ITE_GE,
1804 ASM_THUMB_OP_ITE_EQ,
1805 };
1806 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
1807 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
1808 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
1809 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
1810 #elif N_ARM
1811 #error generic comparisons for ARM needs implementing
1812 //asm_arm_less_op(emit->as, REG_RET, REG_ARG_2, reg_rhs);
1813 //asm_arm_more_op(emit->as, REG_RET, REG_ARG_2, reg_rhs);
1814 #else
1815 #error not implemented
1816 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00001817 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
1818 } else {
1819 // TODO other ops not yet implemented
1820 assert(0);
1821 }
Damien13ed3a62013-10-08 09:05:10 +01001822 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01001823 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00001824 bool invert = false;
1825 if (op == MP_BINARY_OP_NOT_IN) {
1826 invert = true;
1827 op = MP_BINARY_OP_IN;
1828 } else if (op == MP_BINARY_OP_IS_NOT) {
1829 invert = true;
1830 op = MP_BINARY_OP_IS;
1831 }
Damien George7fe21912014-08-16 22:31:57 +01001832 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00001833 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01001834 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00001835 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
1836 }
Damien13ed3a62013-10-08 09:05:10 +01001837 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1838 } else {
1839 printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs);
Damien Georgea5190a72014-08-15 22:39:08 +01001840 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001841 }
1842}
1843
Damien George7ff996c2014-09-08 23:05:16 +01001844STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01001845 // for viper: call runtime, with types of args
1846 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00001847 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001848 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001849 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01001850 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01001851}
1852
Damien George7ff996c2014-09-08 23:05:16 +01001853STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001854 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001855 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001856 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001857 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
1858}
1859
Damien George7ff996c2014-09-08 23:05:16 +01001860STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01001861 // only used in list comprehension
1862 vtype_kind_t vtype_list, vtype_item;
1863 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
1864 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
1865 assert(vtype_list == VTYPE_PYOBJ);
1866 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001867 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01001868 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001869}
1870
Damien George7ff996c2014-09-08 23:05:16 +01001871STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001872 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001873 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001874 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
1875}
1876
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001877STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001878 vtype_kind_t vtype_key, vtype_value, vtype_map;
1879 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
1880 assert(vtype_key == VTYPE_PYOBJ);
1881 assert(vtype_value == VTYPE_PYOBJ);
1882 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001883 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01001884 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
1885}
1886
Damien George7ff996c2014-09-08 23:05:16 +01001887STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01001888 // only used in list comprehension
1889 vtype_kind_t vtype_map, vtype_key, vtype_value;
1890 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
1891 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
1892 assert(vtype_map == VTYPE_PYOBJ);
1893 assert(vtype_key == VTYPE_PYOBJ);
1894 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001895 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01001896 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001897}
1898
Damien George7ff996c2014-09-08 23:05:16 +01001899STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001900 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001901 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001902 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001903 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
1904}
1905
Damien George7ff996c2014-09-08 23:05:16 +01001906STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01001907 // only used in set comprehension
1908 vtype_kind_t vtype_set, vtype_item;
1909 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
1910 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
1911 assert(vtype_set == VTYPE_PYOBJ);
1912 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001913 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01001914 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001915}
Damiend2755ec2013-10-16 23:58:48 +01001916
Damien George7ff996c2014-09-08 23:05:16 +01001917STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001918 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01001919 if (n_args == 2) {
1920 vtype_kind_t vtype_start, vtype_stop;
1921 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
1922 assert(vtype_start == VTYPE_PYOBJ);
1923 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001924 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 +01001925 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1926 } else {
1927 assert(n_args == 3);
1928 vtype_kind_t vtype_start, vtype_stop, vtype_step;
1929 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
1930 assert(vtype_start == VTYPE_PYOBJ);
1931 assert(vtype_stop == VTYPE_PYOBJ);
1932 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001933 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01001934 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1935 }
Damien13ed3a62013-10-08 09:05:10 +01001936}
Damien Georgecdd96df2014-04-06 12:58:40 +01001937
Damien George7ff996c2014-09-08 23:05:16 +01001938STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001939 DEBUG_printf("unpack_sequence %d\n", n_args);
1940 vtype_kind_t vtype_base;
1941 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
1942 assert(vtype_base == VTYPE_PYOBJ);
1943 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01001944 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01001945}
Damien Georgecdd96df2014-04-06 12:58:40 +01001946
Damien George7ff996c2014-09-08 23:05:16 +01001947STATIC 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 +01001948 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
1949 vtype_kind_t vtype_base;
1950 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
1951 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001952 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 +01001953 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 +01001954}
1955
Damien George7ff996c2014-09-08 23:05:16 +01001956STATIC 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 +01001957 // 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 +00001958 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001959 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01001960 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 +01001961 } else {
1962 vtype_kind_t vtype_def_tuple, vtype_def_dict;
1963 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
1964 assert(vtype_def_tuple == VTYPE_PYOBJ);
1965 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001966 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 +01001967 }
Damien13ed3a62013-10-08 09:05:10 +01001968 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1969}
1970
Damien George7ff996c2014-09-08 23:05:16 +01001971STATIC 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 +01001972 assert(0);
1973}
1974
Damien George7ff996c2014-09-08 23:05:16 +01001975STATIC 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 +00001976 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
1977
Damien Georgee9dac3b2014-09-29 22:10:41 +01001978 // TODO: in viper mode, call special runtime routine with type info for args,
1979 // and wanted type info for return, to remove need for boxing/unboxing
1980
Damien George922ddd62014-04-09 12:43:17 +01001981 assert(!star_flags);
Damien Georgecd82e022014-02-02 13:11:48 +00001982
Damien Georgee9dac3b2014-09-29 22:10:41 +01001983 emit_native_pre(emit);
1984 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
1985 if (vtype_fun == VTYPE_BUILTIN_CAST) {
1986 // casting operator
1987 assert(n_positional == 1 && n_keyword == 0);
1988 DEBUG_printf(" cast to %d\n", vtype_fun);
1989 vtype_kind_t vtype_cast = peek_stack(emit, 1)->u_imm;
1990 switch (peek_vtype(emit, 0)) {
1991 case VTYPE_PYOBJ: {
1992 vtype_kind_t vtype;
1993 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1994 emit_pre_pop_discard(emit);
1995 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
1996 emit_post_push_reg(emit, vtype_cast, REG_RET);
1997 break;
1998 }
1999 case VTYPE_BOOL:
2000 case VTYPE_INT:
2001 case VTYPE_UINT:
2002 case VTYPE_PTR:
2003 case VTYPE_PTR8:
2004 case VTYPE_PTR16:
2005 case VTYPE_PTR_NONE:
2006 emit_fold_stack_top(emit, REG_ARG_1);
2007 emit_post_top_set_vtype(emit, vtype_cast);
2008 break;
2009 default:
2010 assert(!"TODO: convert obj to int");
2011 }
2012 } else {
2013 if (n_positional != 0 || n_keyword != 0) {
2014 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2015 }
Damien13ed3a62013-10-08 09:05:10 +01002016 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2017 assert(vtype_fun == VTYPE_PYOBJ);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002018 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2019 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien Georgecd82e022014-02-02 13:11:48 +00002020 }
Damien13ed3a62013-10-08 09:05:10 +01002021}
2022
Damien George7ff996c2014-09-08 23:05:16 +01002023STATIC 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 +01002024 assert(!star_flags);
Damien Georgece8f07a2014-03-27 23:30:26 +00002025 emit_native_pre(emit);
Damien Georgecdd96df2014-04-06 12:58:40 +01002026 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 +01002027 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 +01002028 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2029}
2030
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002031STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002032 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002033 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002034 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2035 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002036 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002037 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002038 } else {
2039 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002040 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002041 } else {
2042 vtype_kind_t vtype;
2043 emit_pre_pop_reg(emit, &vtype, REG_RET);
2044 if (vtype != emit->return_vtype) {
2045 printf("ViperTypeError: incompatible return type\n");
2046 }
Damien George2ac4af62014-08-15 16:45:41 +01002047 }
Damien13ed3a62013-10-08 09:05:10 +01002048 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002049 vtype_kind_t vtype;
2050 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002051 assert(vtype == VTYPE_PYOBJ);
2052 }
2053 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002054 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2055 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002056}
2057
Damien George7ff996c2014-09-08 23:05:16 +01002058STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002059 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002060 vtype_kind_t vtype_exc;
2061 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2062 if (vtype_exc != VTYPE_PYOBJ) {
2063 printf("ViperTypeError: must raise an object\n");
2064 }
2065 // 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 +01002066 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002067}
Damien Georgeb601d952014-06-30 05:17:25 +01002068
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002069STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002070 // not supported (for now)
2071 assert(0);
2072}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002073STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002074 // not supported (for now)
2075 assert(0);
2076}
2077
Damien Georgeb601d952014-06-30 05:17:25 +01002078STATIC void emit_native_start_except_handler(emit_t *emit) {
2079 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2080 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2081 // the first 2 elements, so we can get the thrown value.
2082 adjust_stack(emit, 2);
2083 vtype_kind_t vtype_nlr;
2084 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002085 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002086 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
2087}
2088
2089STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002090 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002091}
2092
Damien13ed3a62013-10-08 09:05:10 +01002093const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002094 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002095 emit_native_start_pass,
2096 emit_native_end_pass,
2097 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002098 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002099 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002100
2101 emit_native_load_id,
2102 emit_native_store_id,
2103 emit_native_delete_id,
2104
2105 emit_native_label_assign,
2106 emit_native_import_name,
2107 emit_native_import_from,
2108 emit_native_import_star,
2109 emit_native_load_const_tok,
2110 emit_native_load_const_small_int,
2111 emit_native_load_const_int,
2112 emit_native_load_const_dec,
Damien13ed3a62013-10-08 09:05:10 +01002113 emit_native_load_const_str,
Damien George3558f622014-04-20 17:50:40 +01002114 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002115 emit_native_load_fast,
Damien13ed3a62013-10-08 09:05:10 +01002116 emit_native_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +00002117 emit_native_load_name,
2118 emit_native_load_global,
Damien13ed3a62013-10-08 09:05:10 +01002119 emit_native_load_attr,
2120 emit_native_load_method,
2121 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002122 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002123 emit_native_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +00002124 emit_native_store_deref,
Damien13ed3a62013-10-08 09:05:10 +01002125 emit_native_store_name,
2126 emit_native_store_global,
Damien13ed3a62013-10-08 09:05:10 +01002127 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002128 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002129 emit_native_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +00002130 emit_native_delete_deref,
Damien13ed3a62013-10-08 09:05:10 +01002131 emit_native_delete_name,
2132 emit_native_delete_global,
Damien13ed3a62013-10-08 09:05:10 +01002133 emit_native_delete_attr,
2134 emit_native_delete_subscr,
2135 emit_native_dup_top,
2136 emit_native_dup_top_two,
2137 emit_native_pop_top,
2138 emit_native_rot_two,
2139 emit_native_rot_three,
2140 emit_native_jump,
2141 emit_native_pop_jump_if_true,
2142 emit_native_pop_jump_if_false,
2143 emit_native_jump_if_true_or_pop,
2144 emit_native_jump_if_false_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002145 emit_native_break_loop,
2146 emit_native_continue_loop,
2147 emit_native_setup_with,
2148 emit_native_with_cleanup,
2149 emit_native_setup_except,
2150 emit_native_setup_finally,
2151 emit_native_end_finally,
2152 emit_native_get_iter,
2153 emit_native_for_iter,
2154 emit_native_for_iter_end,
2155 emit_native_pop_block,
2156 emit_native_pop_except,
2157 emit_native_unary_op,
2158 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002159 emit_native_build_tuple,
2160 emit_native_build_list,
2161 emit_native_list_append,
2162 emit_native_build_map,
2163 emit_native_store_map,
2164 emit_native_map_add,
2165 emit_native_build_set,
2166 emit_native_set_add,
2167 emit_native_build_slice,
2168 emit_native_unpack_sequence,
2169 emit_native_unpack_ex,
2170 emit_native_make_function,
2171 emit_native_make_closure,
2172 emit_native_call_function,
2173 emit_native_call_method,
2174 emit_native_return_value,
2175 emit_native_raise_varargs,
2176 emit_native_yield_value,
2177 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002178
2179 emit_native_start_except_handler,
2180 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002181};
2182
Damien Georgec90f59e2014-09-06 23:06:36 +01002183#endif