blob: 16f0a0824645fe47166e49b9fa20d54a2079a3b4 [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);
Damien13ed3a62013-10-08 09:05:10 +01001078 int 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 {
1394 // viper call
1395 stack_info_t *top = peek_stack(emit, 0);
1396 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1397 // index is an immediate
1398 mp_int_t index_value = top->u_imm;
1399 emit_pre_pop_discard(emit); // discard index
1400 vtype_kind_t vtype_value;
1401 int reg_base = REG_ARG_1;
1402 int reg_index = REG_ARG_2;
1403 int reg_value = REG_ARG_3;
1404 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
1405 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
1406 switch (vtype_base) {
1407 case VTYPE_PTR8: {
1408 // pointer to 8-bit memory
1409 // TODO optimise to use thumb strb r1, [r2, r3]
1410 if (index_value != 0) {
1411 // index is non-zero
1412 #if N_THUMB
1413 if (index_value > 0 && index_value < 32) {
1414 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1415 break;
1416 }
1417 #endif
1418 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
1419 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1420 reg_base = reg_index;
1421 }
1422 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1423 break;
1424 }
1425 case VTYPE_PTR16: {
1426 // pointer to 16-bit memory
1427 if (index_value != 0) {
1428 // index is a non-zero immediate
1429 #if N_THUMB
1430 if (index_value > 0 && index_value < 32) {
1431 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1432 break;
1433 }
1434 #endif
1435 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
1436 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1437 reg_base = reg_index;
1438 }
1439 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1440 break;
1441 }
1442 default:
1443 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1444 }
1445 } else {
1446 // index is not an immediate
1447 vtype_kind_t vtype_index, vtype_value;
1448 int reg_index = REG_ARG_2;
1449 int reg_value = REG_ARG_3;
1450 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1451 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1452 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
1453 switch (vtype_base) {
1454 case VTYPE_PTR8: {
1455 // pointer to 8-bit memory
1456 // TODO optimise to use thumb strb r1, [r2, r3]
1457 assert(vtype_index == VTYPE_INT);
1458 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1459 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1460 break;
1461 }
1462 case VTYPE_PTR16: {
1463 // pointer to 16-bit memory
1464 assert(vtype_index == VTYPE_INT);
1465 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1466 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1467 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1468 break;
1469 }
1470 default:
1471 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1472 }
1473 }
1474
1475 }
Damien13ed3a62013-10-08 09:05:10 +01001476}
1477
Damien George7ff996c2014-09-08 23:05:16 +01001478STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001479 // TODO implement me!
Damien13ed3a62013-10-08 09:05:10 +01001480 // could support for Python types, just set to None (so GC can reclaim it)
Damien13ed3a62013-10-08 09:05:10 +01001481}
1482
Damien George7ff996c2014-09-08 23:05:16 +01001483STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001484 // TODO implement me!
Damien9ecbcff2013-12-11 00:41:43 +00001485}
1486
Damien Georgee6ce10a2014-09-06 18:38:20 +01001487STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1488 emit_native_pre(emit);
1489 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1490 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001491}
1492
Damien Georgee6ce10a2014-09-06 18:38:20 +01001493STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1494 emit_native_pre(emit);
1495 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1496 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001497}
1498
Damien Georgee6ce10a2014-09-06 18:38:20 +01001499STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001500 vtype_kind_t vtype_base;
1501 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1502 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001503 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 +01001504 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001505}
1506
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001507STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001508 vtype_kind_t vtype_index, vtype_base;
1509 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1510 assert(vtype_index == VTYPE_PYOBJ);
1511 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001512 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 +01001513}
1514
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001515STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001516 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001517 vtype_kind_t vtype;
1518 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1519 emit_post_push_reg_reg(emit, vtype, REG_TEMP0, vtype, REG_TEMP0);
1520}
1521
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001522STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001523 vtype_kind_t vtype0, vtype1;
1524 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1525 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1526}
1527
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001528STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001529 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001530 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001531 emit_post(emit);
1532}
1533
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001534STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001535 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +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(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001539}
1540
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001541STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001542 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001543 vtype_kind_t vtype0, vtype1, vtype2;
1544 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1545 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1546}
1547
Damien George7ff996c2014-09-08 23:05:16 +01001548STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001549 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001550 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001551 // need to commit stack because we are jumping elsewhere
1552 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001553 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001554 emit_post(emit);
1555}
1556
Damien George7ff996c2014-09-08 23:05:16 +01001557STATIC void emit_native_jump_helper(emit_t *emit, mp_uint_t label, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001558 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien George6f813482014-09-29 16:41:37 +01001559 switch (vtype) {
1560 case VTYPE_PYOBJ:
1561 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1562 if (!pop) {
1563 adjust_stack(emit, 1);
1564 }
1565 emit_call(emit, MP_F_OBJ_IS_TRUE);
1566 break;
1567 case VTYPE_BOOL:
1568 case VTYPE_INT:
1569 case VTYPE_UINT:
1570 emit_pre_pop_reg(emit, &vtype, REG_RET);
1571 if (!pop) {
1572 adjust_stack(emit, 1);
1573 }
1574 break;
1575 default:
1576 printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
1577 assert(0);
Damien13ed3a62013-10-08 09:05:10 +01001578 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001579 // need to commit stack because we may jump elsewhere
1580 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001581}
1582
Damien George7ff996c2014-09-08 23:05:16 +01001583STATIC void emit_native_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001584 DEBUG_printf("pop_jump_if_true(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001585 emit_native_jump_helper(emit, label, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001586 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien1a6633a2013-11-03 13:58:19 +00001587 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001588}
Damien1a6633a2013-11-03 13:58:19 +00001589
Damien George7ff996c2014-09-08 23:05:16 +01001590STATIC void emit_native_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001591 DEBUG_printf("pop_jump_if_false(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001592 emit_native_jump_helper(emit, label, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001593 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001594 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001595}
Damien Georgea32c1e42014-05-07 18:30:52 +01001596
Damien George7ff996c2014-09-08 23:05:16 +01001597STATIC void emit_native_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001598 DEBUG_printf("jump_if_true_or_pop(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001599 emit_native_jump_helper(emit, label, false);
Damien Georgec90f59e2014-09-06 23:06:36 +01001600 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001601 adjust_stack(emit, -1);
1602 emit_post(emit);
1603}
1604
Damien George7ff996c2014-09-08 23:05:16 +01001605STATIC void emit_native_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001606 DEBUG_printf("jump_if_false_or_pop(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001607 emit_native_jump_helper(emit, label, false);
Damien Georgec90f59e2014-09-06 23:06:36 +01001608 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001609 adjust_stack(emit, -1);
1610 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001611}
1612
Damien George7ff996c2014-09-08 23:05:16 +01001613STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien George25c84642014-05-30 15:20:41 +01001614 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001615}
Damien Georgea32c1e42014-05-07 18:30:52 +01001616
Damien George7ff996c2014-09-08 23:05:16 +01001617STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgea32c1e42014-05-07 18:30:52 +01001618 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001619}
Damien Georgea32c1e42014-05-07 18:30:52 +01001620
Damien George7ff996c2014-09-08 23:05:16 +01001621STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001622 // not supported, or could be with runtime call
1623 assert(0);
1624}
Damien Georgeb601d952014-06-30 05:17:25 +01001625
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001626STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001627 assert(0);
1628}
Damien Georgeb601d952014-06-30 05:17:25 +01001629
Damien George7ff996c2014-09-08 23:05:16 +01001630STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001631 emit_native_pre(emit);
1632 // need to commit stack because we may jump elsewhere
1633 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001634 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 +01001635 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001636 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001637 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001638}
Damien Georgeb601d952014-06-30 05:17:25 +01001639
Damien George7ff996c2014-09-08 23:05:16 +01001640STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001641 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001642}
Damien Georgeb601d952014-06-30 05:17:25 +01001643
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001644STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001645 emit_pre_pop_discard(emit);
1646 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001647}
Damiend2755ec2013-10-16 23:58:48 +01001648
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001649STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001650 // perhaps the difficult one, as we want to rewrite for loops using native code
1651 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001652
1653 vtype_kind_t vtype;
1654 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1655 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001656 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01001657 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001658}
Damiend2755ec2013-10-16 23:58:48 +01001659
Damien George7ff996c2014-09-08 23:05:16 +01001660STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001661 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001662 vtype_kind_t vtype;
1663 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1664 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001665 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01001666 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
1667 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01001668 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1669}
1670
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001671STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001672 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001673 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001674 adjust_stack(emit, -1);
1675 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001676}
1677
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001678STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001679 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001680 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001681 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001682 emit_post(emit);
1683}
1684
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001685STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeb601d952014-06-30 05:17:25 +01001686 /*
1687 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001688 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001689 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01001690 emit_post(emit);
1691 */
Damien13ed3a62013-10-08 09:05:10 +01001692}
1693
Damien Georged17926d2014-03-30 13:35:08 +01001694STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001695 vtype_kind_t vtype;
1696 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1697 assert(vtype == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001698 if (op == MP_UNARY_OP_NOT) {
Damien Georged6230f62014-09-23 14:10:03 +00001699 // we need to synthesise this operation by converting to bool first
1700 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
Damien George3112cde2014-09-29 18:45:42 +01001701 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgeb601d952014-06-30 05:17:25 +01001702 }
Damien Georged6230f62014-09-23 14:10:03 +00001703 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1704 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001705}
1706
Damien Georged17926d2014-03-30 13:35:08 +01001707STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001708 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01001709 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
1710 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01001711 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01001712 #if N_X64 || N_X86
1713 // special cases for x86 and shifting
1714 if (op == MP_BINARY_OP_LSHIFT
1715 || op == MP_BINARY_OP_INPLACE_LSHIFT
1716 || op == MP_BINARY_OP_RSHIFT
1717 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1718 #if N_X64
1719 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
1720 #else
1721 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
1722 #endif
1723 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1724 ASM_LSL_REG(emit->as, REG_RET);
1725 } else {
1726 ASM_ASR_REG(emit->as, REG_RET);
1727 }
1728 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
1729 return;
1730 }
1731 #endif
1732 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001733 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001734 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
1735 if (0) {
1736 // dummy
1737 #if !(N_X64 || N_X86)
1738 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1739 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00001740 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001741 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1742 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1743 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1744 #endif
1745 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
1746 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1747 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1748 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
1749 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1750 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1751 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
1752 // comparison ops are (in enum order):
1753 // MP_BINARY_OP_LESS
1754 // MP_BINARY_OP_MORE
1755 // MP_BINARY_OP_EQUAL
1756 // MP_BINARY_OP_LESS_EQUAL
1757 // MP_BINARY_OP_MORE_EQUAL
1758 // MP_BINARY_OP_NOT_EQUAL
1759 #if N_X64
1760 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
1761 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
1762 static byte ops[6] = {
1763 ASM_X64_CC_JL,
1764 ASM_X64_CC_JG,
1765 ASM_X64_CC_JE,
1766 ASM_X64_CC_JLE,
1767 ASM_X64_CC_JGE,
1768 ASM_X64_CC_JNE,
1769 };
1770 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1771 #elif N_X86
1772 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
1773 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
1774 static byte ops[6] = {
1775 ASM_X86_CC_JL,
1776 ASM_X86_CC_JG,
1777 ASM_X86_CC_JE,
1778 ASM_X86_CC_JLE,
1779 ASM_X86_CC_JGE,
1780 ASM_X86_CC_JNE,
1781 };
1782 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1783 #elif N_THUMB
1784 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
1785 static uint16_t ops[6] = {
1786 ASM_THUMB_OP_ITE_GE,
1787 ASM_THUMB_OP_ITE_GT,
1788 ASM_THUMB_OP_ITE_EQ,
1789 ASM_THUMB_OP_ITE_GT,
1790 ASM_THUMB_OP_ITE_GE,
1791 ASM_THUMB_OP_ITE_EQ,
1792 };
1793 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
1794 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
1795 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
1796 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
1797 #elif N_ARM
1798 #error generic comparisons for ARM needs implementing
1799 //asm_arm_less_op(emit->as, REG_RET, REG_ARG_2, reg_rhs);
1800 //asm_arm_more_op(emit->as, REG_RET, REG_ARG_2, reg_rhs);
1801 #else
1802 #error not implemented
1803 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00001804 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
1805 } else {
1806 // TODO other ops not yet implemented
1807 assert(0);
1808 }
Damien13ed3a62013-10-08 09:05:10 +01001809 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01001810 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00001811 bool invert = false;
1812 if (op == MP_BINARY_OP_NOT_IN) {
1813 invert = true;
1814 op = MP_BINARY_OP_IN;
1815 } else if (op == MP_BINARY_OP_IS_NOT) {
1816 invert = true;
1817 op = MP_BINARY_OP_IS;
1818 }
Damien George7fe21912014-08-16 22:31:57 +01001819 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00001820 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01001821 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00001822 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
1823 }
Damien13ed3a62013-10-08 09:05:10 +01001824 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1825 } else {
1826 printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs);
Damien Georgea5190a72014-08-15 22:39:08 +01001827 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001828 }
1829}
1830
Damien George7ff996c2014-09-08 23:05:16 +01001831STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01001832 // for viper: call runtime, with types of args
1833 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00001834 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001835 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001836 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01001837 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01001838}
1839
Damien George7ff996c2014-09-08 23:05:16 +01001840STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001841 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001842 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001843 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001844 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
1845}
1846
Damien George7ff996c2014-09-08 23:05:16 +01001847STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01001848 // only used in list comprehension
1849 vtype_kind_t vtype_list, vtype_item;
1850 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
1851 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
1852 assert(vtype_list == VTYPE_PYOBJ);
1853 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001854 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01001855 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001856}
1857
Damien George7ff996c2014-09-08 23:05:16 +01001858STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001859 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001860 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001861 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
1862}
1863
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001864STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001865 vtype_kind_t vtype_key, vtype_value, vtype_map;
1866 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
1867 assert(vtype_key == VTYPE_PYOBJ);
1868 assert(vtype_value == VTYPE_PYOBJ);
1869 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001870 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01001871 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
1872}
1873
Damien George7ff996c2014-09-08 23:05:16 +01001874STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01001875 // only used in list comprehension
1876 vtype_kind_t vtype_map, vtype_key, vtype_value;
1877 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
1878 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
1879 assert(vtype_map == VTYPE_PYOBJ);
1880 assert(vtype_key == VTYPE_PYOBJ);
1881 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001882 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01001883 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001884}
1885
Damien George7ff996c2014-09-08 23:05:16 +01001886STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001887 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001888 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001889 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001890 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
1891}
1892
Damien George7ff996c2014-09-08 23:05:16 +01001893STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01001894 // only used in set comprehension
1895 vtype_kind_t vtype_set, vtype_item;
1896 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
1897 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
1898 assert(vtype_set == VTYPE_PYOBJ);
1899 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001900 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01001901 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001902}
Damiend2755ec2013-10-16 23:58:48 +01001903
Damien George7ff996c2014-09-08 23:05:16 +01001904STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001905 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01001906 if (n_args == 2) {
1907 vtype_kind_t vtype_start, vtype_stop;
1908 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
1909 assert(vtype_start == VTYPE_PYOBJ);
1910 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001911 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 +01001912 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1913 } else {
1914 assert(n_args == 3);
1915 vtype_kind_t vtype_start, vtype_stop, vtype_step;
1916 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
1917 assert(vtype_start == VTYPE_PYOBJ);
1918 assert(vtype_stop == VTYPE_PYOBJ);
1919 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001920 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01001921 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1922 }
Damien13ed3a62013-10-08 09:05:10 +01001923}
Damien Georgecdd96df2014-04-06 12:58:40 +01001924
Damien George7ff996c2014-09-08 23:05:16 +01001925STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001926 DEBUG_printf("unpack_sequence %d\n", n_args);
1927 vtype_kind_t vtype_base;
1928 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
1929 assert(vtype_base == VTYPE_PYOBJ);
1930 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01001931 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01001932}
Damien Georgecdd96df2014-04-06 12:58:40 +01001933
Damien George7ff996c2014-09-08 23:05:16 +01001934STATIC 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 +01001935 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
1936 vtype_kind_t vtype_base;
1937 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
1938 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001939 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 +01001940 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 +01001941}
1942
Damien George7ff996c2014-09-08 23:05:16 +01001943STATIC 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 +01001944 // 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 +00001945 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001946 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01001947 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 +01001948 } else {
1949 vtype_kind_t vtype_def_tuple, vtype_def_dict;
1950 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
1951 assert(vtype_def_tuple == VTYPE_PYOBJ);
1952 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001953 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 +01001954 }
Damien13ed3a62013-10-08 09:05:10 +01001955 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1956}
1957
Damien George7ff996c2014-09-08 23:05:16 +01001958STATIC 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 +01001959 assert(0);
1960}
1961
Damien George7ff996c2014-09-08 23:05:16 +01001962STATIC 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 +00001963 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
1964
Damien Georgee9dac3b2014-09-29 22:10:41 +01001965 // TODO: in viper mode, call special runtime routine with type info for args,
1966 // and wanted type info for return, to remove need for boxing/unboxing
1967
Damien George922ddd62014-04-09 12:43:17 +01001968 assert(!star_flags);
Damien Georgecd82e022014-02-02 13:11:48 +00001969
Damien Georgee9dac3b2014-09-29 22:10:41 +01001970 emit_native_pre(emit);
1971 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
1972 if (vtype_fun == VTYPE_BUILTIN_CAST) {
1973 // casting operator
1974 assert(n_positional == 1 && n_keyword == 0);
1975 DEBUG_printf(" cast to %d\n", vtype_fun);
1976 vtype_kind_t vtype_cast = peek_stack(emit, 1)->u_imm;
1977 switch (peek_vtype(emit, 0)) {
1978 case VTYPE_PYOBJ: {
1979 vtype_kind_t vtype;
1980 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1981 emit_pre_pop_discard(emit);
1982 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
1983 emit_post_push_reg(emit, vtype_cast, REG_RET);
1984 break;
1985 }
1986 case VTYPE_BOOL:
1987 case VTYPE_INT:
1988 case VTYPE_UINT:
1989 case VTYPE_PTR:
1990 case VTYPE_PTR8:
1991 case VTYPE_PTR16:
1992 case VTYPE_PTR_NONE:
1993 emit_fold_stack_top(emit, REG_ARG_1);
1994 emit_post_top_set_vtype(emit, vtype_cast);
1995 break;
1996 default:
1997 assert(!"TODO: convert obj to int");
1998 }
1999 } else {
2000 if (n_positional != 0 || n_keyword != 0) {
2001 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2002 }
Damien13ed3a62013-10-08 09:05:10 +01002003 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2004 assert(vtype_fun == VTYPE_PYOBJ);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002005 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2006 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien Georgecd82e022014-02-02 13:11:48 +00002007 }
Damien13ed3a62013-10-08 09:05:10 +01002008}
2009
Damien George7ff996c2014-09-08 23:05:16 +01002010STATIC 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 +01002011 assert(!star_flags);
Damien Georgece8f07a2014-03-27 23:30:26 +00002012 emit_native_pre(emit);
Damien Georgecdd96df2014-04-06 12:58:40 +01002013 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 +01002014 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 +01002015 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2016}
2017
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002018STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002019 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002020 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002021 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2022 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002023 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002024 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002025 } else {
2026 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002027 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002028 } else {
2029 vtype_kind_t vtype;
2030 emit_pre_pop_reg(emit, &vtype, REG_RET);
2031 if (vtype != emit->return_vtype) {
2032 printf("ViperTypeError: incompatible return type\n");
2033 }
Damien George2ac4af62014-08-15 16:45:41 +01002034 }
Damien13ed3a62013-10-08 09:05:10 +01002035 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002036 vtype_kind_t vtype;
2037 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002038 assert(vtype == VTYPE_PYOBJ);
2039 }
2040 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002041 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2042 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002043}
2044
Damien George7ff996c2014-09-08 23:05:16 +01002045STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002046 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002047 vtype_kind_t vtype_exc;
2048 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2049 if (vtype_exc != VTYPE_PYOBJ) {
2050 printf("ViperTypeError: must raise an object\n");
2051 }
2052 // 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 +01002053 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002054}
Damien Georgeb601d952014-06-30 05:17:25 +01002055
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002056STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002057 // not supported (for now)
2058 assert(0);
2059}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002060STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002061 // not supported (for now)
2062 assert(0);
2063}
2064
Damien Georgeb601d952014-06-30 05:17:25 +01002065STATIC void emit_native_start_except_handler(emit_t *emit) {
2066 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2067 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2068 // the first 2 elements, so we can get the thrown value.
2069 adjust_stack(emit, 2);
2070 vtype_kind_t vtype_nlr;
2071 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002072 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002073 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
2074}
2075
2076STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002077 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002078}
2079
Damien13ed3a62013-10-08 09:05:10 +01002080const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002081 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002082 emit_native_start_pass,
2083 emit_native_end_pass,
2084 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002085 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002086 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002087
2088 emit_native_load_id,
2089 emit_native_store_id,
2090 emit_native_delete_id,
2091
2092 emit_native_label_assign,
2093 emit_native_import_name,
2094 emit_native_import_from,
2095 emit_native_import_star,
2096 emit_native_load_const_tok,
2097 emit_native_load_const_small_int,
2098 emit_native_load_const_int,
2099 emit_native_load_const_dec,
Damien13ed3a62013-10-08 09:05:10 +01002100 emit_native_load_const_str,
Damien George3558f622014-04-20 17:50:40 +01002101 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002102 emit_native_load_fast,
Damien13ed3a62013-10-08 09:05:10 +01002103 emit_native_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +00002104 emit_native_load_name,
2105 emit_native_load_global,
Damien13ed3a62013-10-08 09:05:10 +01002106 emit_native_load_attr,
2107 emit_native_load_method,
2108 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002109 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002110 emit_native_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +00002111 emit_native_store_deref,
Damien13ed3a62013-10-08 09:05:10 +01002112 emit_native_store_name,
2113 emit_native_store_global,
Damien13ed3a62013-10-08 09:05:10 +01002114 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002115 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002116 emit_native_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +00002117 emit_native_delete_deref,
Damien13ed3a62013-10-08 09:05:10 +01002118 emit_native_delete_name,
2119 emit_native_delete_global,
Damien13ed3a62013-10-08 09:05:10 +01002120 emit_native_delete_attr,
2121 emit_native_delete_subscr,
2122 emit_native_dup_top,
2123 emit_native_dup_top_two,
2124 emit_native_pop_top,
2125 emit_native_rot_two,
2126 emit_native_rot_three,
2127 emit_native_jump,
2128 emit_native_pop_jump_if_true,
2129 emit_native_pop_jump_if_false,
2130 emit_native_jump_if_true_or_pop,
2131 emit_native_jump_if_false_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002132 emit_native_break_loop,
2133 emit_native_continue_loop,
2134 emit_native_setup_with,
2135 emit_native_with_cleanup,
2136 emit_native_setup_except,
2137 emit_native_setup_finally,
2138 emit_native_end_finally,
2139 emit_native_get_iter,
2140 emit_native_for_iter,
2141 emit_native_for_iter_end,
2142 emit_native_pop_block,
2143 emit_native_pop_except,
2144 emit_native_unary_op,
2145 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002146 emit_native_build_tuple,
2147 emit_native_build_list,
2148 emit_native_list_append,
2149 emit_native_build_map,
2150 emit_native_store_map,
2151 emit_native_map_add,
2152 emit_native_build_set,
2153 emit_native_set_add,
2154 emit_native_build_slice,
2155 emit_native_unpack_sequence,
2156 emit_native_unpack_ex,
2157 emit_native_make_function,
2158 emit_native_make_closure,
2159 emit_native_call_function,
2160 emit_native_call_method,
2161 emit_native_return_value,
2162 emit_native_raise_varargs,
2163 emit_native_yield_value,
2164 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002165
2166 emit_native_start_except_handler,
2167 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002168};
2169
Damien Georgec90f59e2014-09-06 23:06:36 +01002170#endif