blob: f435117c41f43799492a08038f407028df48a83f [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
Fabian Vogte5268962014-10-04 00:53:46 +0200423#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_arm_lsl_reg_reg((as), (reg_dest), (reg_shift))
424#define ASM_ASR_REG_REG(as, reg_dest, reg_shift) asm_arm_asr_reg_reg((as), (reg_dest), (reg_shift))
Damien George3112cde2014-09-29 18:45:42 +0100425#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_arm_add_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
426#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_arm_sub_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
427
Fabian Vogte5268962014-10-04 00:53:46 +0200428#define ASM_STORE_REG_REG(as, reg_value, reg_base) asm_arm_str_reg_reg((as), (reg_value), (reg_base))
429#define ASM_STORE8_REG_REG(as, reg_value, reg_base) asm_arm_strb_reg_reg((as), (reg_value), (reg_base))
430#define ASM_STORE16_REG_REG(as, reg_value, reg_base) asm_arm_strh_reg_reg((as), (reg_value), (reg_base))
Damien Georgee9dac3b2014-09-29 22:10:41 +0100431
Damien Georgec90f59e2014-09-06 23:06:36 +0100432#else
433
434#error unknown native emitter
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200435
Damien13ed3a62013-10-08 09:05:10 +0100436#endif
437
438typedef enum {
Damienff8ed772013-10-08 22:18:32 +0100439 STACK_VALUE,
440 STACK_REG,
441 STACK_IMM,
442} stack_info_kind_t;
Damien13ed3a62013-10-08 09:05:10 +0100443
Damien Georgee9dac3b2014-09-29 22:10:41 +0100444// these enums must be distinct and the bottom 2 bits
445// must correspond to the correct MP_NATIVE_TYPE_xxx value
Damien13ed3a62013-10-08 09:05:10 +0100446typedef enum {
Damien Georgee9dac3b2014-09-29 22:10:41 +0100447 VTYPE_PYOBJ = 0x00 | MP_NATIVE_TYPE_OBJ,
448 VTYPE_BOOL = 0x00 | MP_NATIVE_TYPE_BOOL,
449 VTYPE_INT = 0x00 | MP_NATIVE_TYPE_INT,
450 VTYPE_UINT = 0x00 | MP_NATIVE_TYPE_UINT,
451
452 VTYPE_PTR = 0x10 | MP_NATIVE_TYPE_UINT, // pointer to word sized entity
453 VTYPE_PTR8 = 0x20 | MP_NATIVE_TYPE_UINT,
454 VTYPE_PTR16 = 0x30 | MP_NATIVE_TYPE_UINT,
455 VTYPE_PTR_NONE = 0x40 | MP_NATIVE_TYPE_UINT,
456
457 VTYPE_UNBOUND = 0x50 | MP_NATIVE_TYPE_OBJ,
458 VTYPE_BUILTIN_CAST = 0x60 | MP_NATIVE_TYPE_OBJ,
Damien13ed3a62013-10-08 09:05:10 +0100459} vtype_kind_t;
460
Damienff8ed772013-10-08 22:18:32 +0100461typedef struct _stack_info_t {
462 vtype_kind_t vtype;
463 stack_info_kind_t kind;
464 union {
465 int u_reg;
Damien George40f3c022014-07-03 13:25:24 +0100466 mp_int_t u_imm;
Damienff8ed772013-10-08 22:18:32 +0100467 };
468} stack_info_t;
469
Damien13ed3a62013-10-08 09:05:10 +0100470struct _emit_t {
471 int pass;
472
473 bool do_viper_types;
Damienff8ed772013-10-08 22:18:32 +0100474
Damien George2ac4af62014-08-15 16:45:41 +0100475 vtype_kind_t return_vtype;
476
Damien George7ff996c2014-09-08 23:05:16 +0100477 mp_uint_t local_vtype_alloc;
Damien13ed3a62013-10-08 09:05:10 +0100478 vtype_kind_t *local_vtype;
Damienff8ed772013-10-08 22:18:32 +0100479
Damien George7ff996c2014-09-08 23:05:16 +0100480 mp_uint_t stack_info_alloc;
Damienff8ed772013-10-08 22:18:32 +0100481 stack_info_t *stack_info;
482
Damien13ed3a62013-10-08 09:05:10 +0100483 int stack_start;
484 int stack_size;
485
486 bool last_emit_was_return_value;
487
Damien13ed3a62013-10-08 09:05:10 +0100488 scope_t *scope;
489
Damien Georgec90f59e2014-09-06 23:06:36 +0100490 ASM_T *as;
Damien13ed3a62013-10-08 09:05:10 +0100491};
492
Damien George7ff996c2014-09-08 23:05:16 +0100493emit_t *EXPORT_FUN(new)(mp_uint_t max_num_labels) {
Damien George36db6bc2014-05-07 17:24:22 +0100494 emit_t *emit = m_new0(emit_t, 1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100495 emit->as = ASM_NEW(max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100496 return emit;
497}
498
Damien George41d02b62014-01-24 22:42:28 +0000499void EXPORT_FUN(free)(emit_t *emit) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100500 ASM_FREE(emit->as, false);
Damien George36db6bc2014-05-07 17:24:22 +0100501 m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
502 m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200503 m_del_obj(emit_t, emit);
504}
505
Damien George2ac4af62014-08-15 16:45:41 +0100506STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
507 switch (op) {
508 case MP_EMIT_NATIVE_TYPE_ENABLE:
509 emit->do_viper_types = arg1;
510 break;
511
512 default: {
513 vtype_kind_t type;
514 switch (arg2) {
515 case MP_QSTR_object: type = VTYPE_PYOBJ; break;
516 case MP_QSTR_bool: type = VTYPE_BOOL; break;
517 case MP_QSTR_int: type = VTYPE_INT; break;
518 case MP_QSTR_uint: type = VTYPE_UINT; break;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100519 case MP_QSTR_ptr: type = VTYPE_PTR; break;
520 case MP_QSTR_ptr8: type = VTYPE_PTR8; break;
521 case MP_QSTR_ptr16: type = VTYPE_PTR16; break;
Damien George2ac4af62014-08-15 16:45:41 +0100522 default: printf("ViperTypeError: unknown type %s\n", qstr_str(arg2)); return;
523 }
524 if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
525 emit->return_vtype = type;
526 } else {
527 assert(arg1 < emit->local_vtype_alloc);
528 emit->local_vtype[arg1] = type;
529 }
530 break;
531 }
532 }
Damien13ed3a62013-10-08 09:05:10 +0100533}
534
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200535STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien Georged6230f62014-09-23 14:10:03 +0000536 DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope);
537
Damien13ed3a62013-10-08 09:05:10 +0100538 emit->pass = pass;
539 emit->stack_start = 0;
540 emit->stack_size = 0;
541 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100542 emit->scope = scope;
543
Damien George36db6bc2014-05-07 17:24:22 +0100544 // allocate memory for keeping track of the types of locals
545 if (emit->local_vtype_alloc < scope->num_locals) {
546 emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
547 emit->local_vtype_alloc = scope->num_locals;
Damienff8ed772013-10-08 22:18:32 +0100548 }
Damien George36db6bc2014-05-07 17:24:22 +0100549
550 // allocate memory for keeping track of the objects on the stack
551 // XXX don't know stack size on entry, and it should be maximum over all scopes
Damienff8ed772013-10-08 22:18:32 +0100552 if (emit->stack_info == NULL) {
Damien George36db6bc2014-05-07 17:24:22 +0100553 emit->stack_info_alloc = scope->stack_size + 50;
Damienff8ed772013-10-08 22:18:32 +0100554 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100555 }
556
Damien George2ac4af62014-08-15 16:45:41 +0100557 // set default type for return and arguments
558 emit->return_vtype = VTYPE_PYOBJ;
Damien Georgea5190a72014-08-15 22:39:08 +0100559 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100560 emit->local_vtype[i] = VTYPE_PYOBJ;
561 }
Damien Georgea5190a72014-08-15 22:39:08 +0100562
563 // local variables begin unbound, and have unknown type
564 for (mp_uint_t i = emit->scope->num_pos_args; i < emit->local_vtype_alloc; i++) {
565 emit->local_vtype[i] = VTYPE_UNBOUND;
566 }
567
568 // values on stack begin unbound
569 for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100570 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgea5190a72014-08-15 22:39:08 +0100571 emit->stack_info[i].vtype = VTYPE_UNBOUND;
Damien13ed3a62013-10-08 09:05:10 +0100572 }
573
Damien Georgec90f59e2014-09-06 23:06:36 +0100574 ASM_START_PASS(emit->as, pass == MP_PASS_EMIT ? ASM_PASS_EMIT : ASM_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100575
576 // entry to function
577 int num_locals = 0;
Damien George36db6bc2014-05-07 17:24:22 +0100578 if (pass > MP_PASS_SCOPE) {
Damien13ed3a62013-10-08 09:05:10 +0100579 num_locals = scope->num_locals - REG_LOCAL_NUM;
580 if (num_locals < 0) {
581 num_locals = 0;
582 }
583 emit->stack_start = num_locals;
584 num_locals += scope->stack_size;
585 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100586 ASM_ENTRY(emit->as, num_locals);
Damien13ed3a62013-10-08 09:05:10 +0100587
588 // initialise locals from parameters
Damien3ef4abb2013-10-12 16:53:13 +0100589#if N_X64
Damien George2827d622014-04-27 15:50:52 +0100590 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100591 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100592 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100593 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100594 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +0100595 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100596 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Damien George81057362014-09-07 01:06:19 +0100597 } else if (i == 3) {
598 asm_x64_mov_r64_to_local(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +0100599 } else {
600 // TODO not implemented
601 assert(0);
602 }
603 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100604#elif N_X86
605 for (int i = 0; i < scope->num_pos_args; i++) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100606 if (i == 0) {
Damien George03281b32014-09-06 22:38:50 +0000607 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100608 } else if (i == 1) {
Damien George03281b32014-09-06 22:38:50 +0000609 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +0000610 } else if (i == 2) {
611 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +0100612 } else {
Damien George03281b32014-09-06 22:38:50 +0000613 asm_x86_mov_arg_to_r32(emit->as, i, REG_TEMP0);
614 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, i - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +0100615 }
616 }
Damien3ef4abb2013-10-12 16:53:13 +0100617#elif N_THUMB
Damien George2827d622014-04-27 15:50:52 +0100618 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100619 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100620 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100621 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100622 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +0100623 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100624 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Damien13ed3a62013-10-08 09:05:10 +0100625 } else if (i == 3) {
626 asm_thumb_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
627 } else {
628 // TODO not implemented
629 assert(0);
630 }
631 }
632
Damien Georgee9dac3b2014-09-29 22:10:41 +0100633 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100634 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200635#elif N_ARM
636 for (int i = 0; i < scope->num_pos_args; i++) {
637 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100638 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200639 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100640 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200641 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100642 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200643 } else if (i == 3) {
644 asm_arm_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
645 } else {
646 // TODO not implemented
647 assert(0);
648 }
649 }
650
Damien Georgee9dac3b2014-09-29 22:10:41 +0100651 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100652 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
Damien Georgec90f59e2014-09-06 23:06:36 +0100653#else
654 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +0100655#endif
656}
657
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200658STATIC void emit_native_end_pass(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100659 if (!emit->last_emit_was_return_value) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100660 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100661 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100662 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100663
664 // check stack is back to zero size
665 if (emit->stack_size != 0) {
666 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
667 }
668
Damien George36db6bc2014-05-07 17:24:22 +0100669 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100670 void *f = ASM_GET_CODE(emit->as);
671 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100672
673 // compute type signature
Damien Georgee9dac3b2014-09-29 22:10:41 +0100674 // note that the lower 2 bits of a vtype are tho correct MP_NATIVE_TYPE_xxx
Damien George2ac4af62014-08-15 16:45:41 +0100675 mp_uint_t type_sig = emit->return_vtype & 3;
676 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
677 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
678 }
679
680 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 +0100681 }
682}
683
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200684STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100685 return emit->last_emit_was_return_value;
686}
687
Damien Georged6230f62014-09-23 14:10:03 +0000688STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
689 DEBUG_printf(" adjust_stack; stack_size=%d, delta=%d\n", emit->stack_size, stack_size_delta);
690 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100691 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100692 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100693 emit->scope->stack_size = emit->stack_size;
694 }
695}
696
Damien Georged6230f62014-09-23 14:10:03 +0000697STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
698 // If we are adjusting the stack in a positive direction (pushing) then we
699 // need to fill in values for the stack kind and vtype of the newly-pushed
700 // entries. These should be set to "value" (ie not reg or imm) because we
701 // should only need to adjust the stack due to a jump to this part in the
702 // code (and hence we have settled the stack before the jump).
703 for (mp_int_t i = 0; i < delta; i++) {
704 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
705 si->kind = STACK_VALUE;
706 si->vtype = VTYPE_PYOBJ; // XXX we don't know the vtype...
707 }
708 adjust_stack(emit, delta);
709}
710
711STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
712}
713
Damienff8ed772013-10-08 22:18:32 +0100714/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200715STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100716 adjust_stack(emit, stack_size_delta);
717 emit->last_emit_was_return_value = false;
718}
Damienff8ed772013-10-08 22:18:32 +0100719*/
Damien13ed3a62013-10-08 09:05:10 +0100720
Damienff8ed772013-10-08 22:18:32 +0100721// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000722STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100723 emit->last_emit_was_return_value = false;
724 // settle the stack
725 /*
726 if (regs_needed != 0) {
727 for (int i = 0; i < emit->stack_size; i++) {
728 switch (emit->stack_info[i].kind) {
729 case STACK_VALUE:
730 break;
731
732 case STACK_REG:
733 // TODO only push reg if in regs_needed
734 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100735 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100736 break;
737
738 case STACK_IMM:
739 // don't think we ever need to push imms for settling
740 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
741 break;
742 }
743 }
744 }
745 */
Damien13ed3a62013-10-08 09:05:10 +0100746}
747
Damien George3112cde2014-09-29 18:45:42 +0100748// depth==0 is top, depth==1 is before top, etc
749STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
750 return &emit->stack_info[emit->stack_size - 1 - depth];
751}
752
753// depth==0 is top, depth==1 is before top, etc
754STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
755 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100756}
Damien13ed3a62013-10-08 09:05:10 +0100757
Damiend2755ec2013-10-16 23:58:48 +0100758// pos=1 is TOS, pos=2 is next, etc
759// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200760STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100761 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100762 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100763 if (i != skip_stack_pos) {
764 stack_info_t *si = &emit->stack_info[i];
765 if (si->kind == STACK_REG && si->u_reg == reg_needed) {
766 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100767 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100768 }
Damienff8ed772013-10-08 22:18:32 +0100769 }
770 }
771}
Damien13ed3a62013-10-08 09:05:10 +0100772
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200773STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100774 for (int i = 0; i < emit->stack_size; i++) {
775 stack_info_t *si = &emit->stack_info[i];
776 if (si->kind == STACK_REG) {
777 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100778 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100779 }
Damien13ed3a62013-10-08 09:05:10 +0100780 }
781}
782
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200783STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +0000784 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +0100785 for (int i = 0; i < emit->stack_size; i++) {
786 stack_info_t *si = &emit->stack_info[i];
787 if (si->kind == STACK_REG) {
Damien Georged6230f62014-09-23 14:10:03 +0000788 DEBUG_printf(" reg(%u) to local(%u)\n", si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100789 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100790 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100791 }
792 }
793 for (int i = 0; i < emit->stack_size; i++) {
794 stack_info_t *si = &emit->stack_info[i];
795 if (si->kind == STACK_IMM) {
Damien Georged6230f62014-09-23 14:10:03 +0000796 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +0100797 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100798 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +0100799 }
800 }
801}
802
803// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200804STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100805 need_reg_single(emit, reg_dest, pos);
806 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +0100807 *vtype = si->vtype;
808 switch (si->kind) {
809 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +0100810 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100811 break;
812
Damienff8ed772013-10-08 22:18:32 +0100813 case STACK_REG:
814 if (si->u_reg != reg_dest) {
Damien George3112cde2014-09-29 18:45:42 +0100815 ASM_MOV_REG_REG(emit->as, reg_dest, si->u_reg);
Damien13ed3a62013-10-08 09:05:10 +0100816 }
817 break;
818
Damienff8ed772013-10-08 22:18:32 +0100819 case STACK_IMM:
Damien Georgec90f59e2014-09-06 23:06:36 +0100820 ASM_MOV_IMM_TO_REG(emit->as, si->u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100821 break;
822 }
Damien13ed3a62013-10-08 09:05:10 +0100823}
824
Damien Georgee9dac3b2014-09-29 22:10:41 +0100825// does an efficient X=pop(); discard(); push(X)
826// needs a (non-temp) register in case the poped element was stored in the stack
827STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
828 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
829 si[0] = si[1];
830 if (si->kind == STACK_VALUE) {
831 // if folded element was on the stack we need to put it in a register
832 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
833 si->kind = STACK_REG;
834 si->u_reg = reg_dest;
835 }
836 adjust_stack(emit, -1);
837}
838
839// If stacked value is in a register and the register is not r1 or r2, then
840// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
841STATIC 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 +0100842 emit->last_emit_was_return_value = false;
843 stack_info_t *si = peek_stack(emit, 0);
Damien Georgee9dac3b2014-09-29 22:10:41 +0100844 if (si->kind == STACK_REG && si->u_reg != not_r1 && si->u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +0100845 *vtype = si->vtype;
846 *reg_dest = si->u_reg;
847 need_reg_single(emit, *reg_dest, 1);
848 } else {
849 emit_access_stack(emit, 1, vtype, *reg_dest);
850 }
851 adjust_stack(emit, -1);
852}
853
Damien Georgee6ce10a2014-09-06 18:38:20 +0100854STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +0100855 emit->last_emit_was_return_value = false;
856 adjust_stack(emit, -1);
857}
858
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200859STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100860 emit->last_emit_was_return_value = false;
861 emit_access_stack(emit, 1, vtype, reg_dest);
862 adjust_stack(emit, -1);
863}
864
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200865STATIC 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 +0100866 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100867 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +0100868}
869
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200870STATIC 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 +0100871 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100872 emit_pre_pop_reg(emit, vtypeb, regb);
873 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100874}
875
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200876STATIC void emit_post(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100877}
878
Damien Georgee9dac3b2014-09-29 22:10:41 +0100879STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
880 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
881 si->vtype = new_vtype;
882}
883
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200884STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +0100885 stack_info_t *si = &emit->stack_info[emit->stack_size];
886 si->vtype = vtype;
887 si->kind = STACK_REG;
888 si->u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +0100889 adjust_stack(emit, 1);
890}
891
Damien George40f3c022014-07-03 13:25:24 +0100892STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +0100893 stack_info_t *si = &emit->stack_info[emit->stack_size];
894 si->vtype = vtype;
895 si->kind = STACK_IMM;
896 si->u_imm = imm;
897 adjust_stack(emit, 1);
898}
899
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200900STATIC 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 +0100901 emit_post_push_reg(emit, vtypea, rega);
902 emit_post_push_reg(emit, vtypeb, regb);
903}
904
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200905STATIC 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 +0100906 emit_post_push_reg(emit, vtypea, rega);
907 emit_post_push_reg(emit, vtypeb, regb);
908 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100909}
910
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200911STATIC 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 +0100912 emit_post_push_reg(emit, vtypea, rega);
913 emit_post_push_reg(emit, vtypeb, regb);
914 emit_post_push_reg(emit, vtypec, regc);
915 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +0100916}
917
Damien George7fe21912014-08-16 22:31:57 +0100918STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +0100919 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100920 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100921}
922
Damien George7fe21912014-08-16 22:31:57 +0100923STATIC 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 +0100924 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100925 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
926 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100927}
928
Damien George40f3c022014-07-03 13:25:24 +0100929// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100930STATIC 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 +0100931 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100932 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
933 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +0100934}
935
Damien George7fe21912014-08-16 22:31:57 +0100936STATIC 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 +0000937 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100938 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
939 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
940 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +0000941}
942
Damien George40f3c022014-07-03 13:25:24 +0100943// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100944STATIC 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 +0100945 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100946 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
947 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
948 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
949 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +0100950}
951
Damien George86de21b2014-08-16 22:06:11 +0100952// vtype of all n_pop objects is VTYPE_PYOBJ
953// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
954// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
955// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
956STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
957 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +0100958
Damien George86de21b2014-08-16 22:06:11 +0100959 // First, store any immediate values to their respective place on the stack.
960 for (mp_uint_t i = 0; i < n_pop; i++) {
961 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
962 // must push any imm's to stack
963 // must convert them to VTYPE_PYOBJ for viper code
964 if (si->kind == STACK_IMM) {
965 si->kind = STACK_VALUE;
966 switch (si->vtype) {
967 case VTYPE_PYOBJ:
Damien Georgec90f59e2014-09-06 23:06:36 +0100968 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 +0100969 break;
970 case VTYPE_BOOL:
971 if (si->u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100972 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 +0100973 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +0100974 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 +0100975 }
976 si->vtype = VTYPE_PYOBJ;
977 break;
978 case VTYPE_INT:
979 case VTYPE_UINT:
Damien Georgec90f59e2014-09-06 23:06:36 +0100980 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 +0100981 si->vtype = VTYPE_PYOBJ;
982 break;
983 default:
984 // not handled
985 assert(0);
986 }
987 }
988
989 // verify that this value is on the stack
990 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +0100991 }
Damien George86de21b2014-08-16 22:06:11 +0100992
993 // Second, convert any non-VTYPE_PYOBJ to that type.
994 for (mp_uint_t i = 0; i < n_pop; i++) {
995 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
996 if (si->vtype != VTYPE_PYOBJ) {
997 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +0100998 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +0100999 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 +01001000 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001001 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001002 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001003 }
1004 }
1005
1006 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1007 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +01001008 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001009}
1010
1011// vtype of all n_push objects is VTYPE_PYOBJ
1012STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1013 need_reg_all(emit);
1014 for (mp_uint_t i = 0; i < n_push; i++) {
1015 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1016 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1017 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001018 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001019 adjust_stack(emit, n_push);
1020}
1021
Damien George7ff996c2014-09-08 23:05:16 +01001022STATIC void emit_native_load_id(emit_t *emit, qstr qst) {
1023 emit_common_load_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001024}
1025
Damien George7ff996c2014-09-08 23:05:16 +01001026STATIC void emit_native_store_id(emit_t *emit, qstr qst) {
1027 emit_common_store_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001028}
1029
Damien George7ff996c2014-09-08 23:05:16 +01001030STATIC void emit_native_delete_id(emit_t *emit, qstr qst) {
1031 emit_common_delete_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001032}
1033
Damien George7ff996c2014-09-08 23:05:16 +01001034STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001035 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001036 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001037 // need to commit stack because we can jump here from elsewhere
1038 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001039 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001040 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001041}
1042
Damien Georgecdd96df2014-04-06 12:58:40 +01001043STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1044 DEBUG_printf("import_name %s\n", qstr_str(qst));
1045 vtype_kind_t vtype_fromlist;
1046 vtype_kind_t vtype_level;
1047 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1048 assert(vtype_fromlist == VTYPE_PYOBJ);
1049 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001050 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001051 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001052}
1053
Damien Georgecdd96df2014-04-06 12:58:40 +01001054STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1055 DEBUG_printf("import_from %s\n", qstr_str(qst));
1056 emit_native_pre(emit);
1057 vtype_kind_t vtype_module;
1058 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1059 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001060 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001061 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001062}
1063
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001064STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001065 DEBUG_printf("import_star\n");
1066 vtype_kind_t vtype_module;
1067 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1068 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001069 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001070 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001071}
1072
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001073STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001074 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001075 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001076 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001077 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001078 if (emit->do_viper_types) {
1079 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001080 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1081 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1082 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien13ed3a62013-10-08 09:05:10 +01001083 default: assert(0); vtype = 0; val = 0; // shouldn't happen
1084 }
1085 } else {
1086 vtype = VTYPE_PYOBJ;
1087 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001088 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1089 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1090 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien13ed3a62013-10-08 09:05:10 +01001091 default: assert(0); vtype = 0; val = 0; // shouldn't happen
1092 }
1093 }
1094 emit_post_push_imm(emit, vtype, val);
1095}
1096
Damien George40f3c022014-07-03 13:25:24 +01001097STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001098 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001099 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001100 if (emit->do_viper_types) {
1101 emit_post_push_imm(emit, VTYPE_INT, arg);
1102 } else {
1103 emit_post_push_imm(emit, VTYPE_PYOBJ, (arg << 1) | 1);
1104 }
1105}
1106
Damien Georgecdd96df2014-04-06 12:58:40 +01001107STATIC void emit_native_load_const_int(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001108 DEBUG_printf("load_const_int %s\n", qstr_str(qst));
Damien Georgecdd96df2014-04-06 12:58:40 +01001109 // for viper: load integer, check fits in 32 bits
1110 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001111 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_INT, qst, REG_ARG_1);
Damien Georgecdd96df2014-04-06 12:58:40 +01001112 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001113}
1114
Damien George7ff996c2014-09-08 23:05:16 +01001115STATIC void emit_native_load_const_dec(emit_t *emit, qstr qst) {
Damien6ba13142013-11-02 20:34:54 +00001116 // for viper, a float/complex is just a Python object
Damien Georgece8f07a2014-03-27 23:30:26 +00001117 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001118 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_DEC, qst, REG_ARG_1);
Damien6ba13142013-11-02 20:34:54 +00001119 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001120}
1121
Damien George7ff996c2014-09-08 23:05:16 +01001122STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001123 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001124 // TODO: Eventually we want to be able to work with raw pointers in viper to
1125 // do native array access. For now we just load them as any other object.
1126 /*
Damien13ed3a62013-10-08 09:05:10 +01001127 if (emit->do_viper_types) {
1128 // not implemented properly
1129 // load a pointer to the asciiz string?
1130 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001131 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001132 } else
1133 */
1134 {
Damien Georgeb601d952014-06-30 05:17:25 +01001135 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +01001136 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_BYTES, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001137 } else {
Damien George7ff996c2014-09-08 23:05:16 +01001138 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001139 }
Damien13ed3a62013-10-08 09:05:10 +01001140 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1141 }
1142}
1143
Damien George3558f622014-04-20 17:50:40 +01001144STATIC void emit_native_load_null(emit_t *emit) {
1145 emit_native_pre(emit);
1146 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1147}
1148
Damien George7ff996c2014-09-08 23:05:16 +01001149STATIC 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 +01001150 vtype_kind_t vtype = emit->local_vtype[local_num];
1151 if (vtype == VTYPE_UNBOUND) {
Damien George7ff996c2014-09-08 23:05:16 +01001152 printf("ViperTypeError: local %s used before type known\n", qstr_str(qst));
Damien13ed3a62013-10-08 09:05:10 +01001153 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001154 emit_native_pre(emit);
Damien3ef4abb2013-10-12 16:53:13 +01001155#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001156 if (local_num == 0) {
1157 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001158 } else if (local_num == 1) {
1159 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1160 } else if (local_num == 2) {
1161 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001162 } else {
Damien George0b610de2014-09-29 16:25:04 +01001163 need_reg_single(emit, REG_TEMP0, 0);
1164 asm_x64_mov_local_to_r64(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1165 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001166 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001167#elif N_X86
1168 if (local_num == 0) {
1169 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001170 } else if (local_num == 1) {
1171 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001172 } else if (local_num == 2) {
1173 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001174 } else {
Damien George0b610de2014-09-29 16:25:04 +01001175 need_reg_single(emit, REG_TEMP0, 0);
1176 asm_x86_mov_local_to_r32(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1177 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien Georgec90f59e2014-09-06 23:06:36 +01001178 }
Damien3ef4abb2013-10-12 16:53:13 +01001179#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001180 if (local_num == 0) {
1181 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1182 } else if (local_num == 1) {
1183 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1184 } else if (local_num == 2) {
1185 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1186 } else {
Damien George0b610de2014-09-29 16:25:04 +01001187 need_reg_single(emit, REG_TEMP0, 0);
1188 asm_thumb_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1189 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001190 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001191#elif N_ARM
1192 if (local_num == 0) {
1193 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1194 } else if (local_num == 1) {
1195 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1196 } else if (local_num == 2) {
1197 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1198 } else {
Damien George0b610de2014-09-29 16:25:04 +01001199 need_reg_single(emit, REG_TEMP0, 0);
1200 asm_arm_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1201 emit_post_push_reg(emit, vtype, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001202 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001203#else
1204 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001205#endif
1206}
1207
Damien George7ff996c2014-09-08 23:05:16 +01001208STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001209 // not implemented
1210 // in principle could support this quite easily (ldr r0, [r0, #0]) and then get closed over variables!
1211 assert(0);
1212}
1213
Damien George7ff996c2014-09-08 23:05:16 +01001214STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001215 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001216 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001217 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001218 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1219}
1220
Damien George7ff996c2014-09-08 23:05:16 +01001221STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001222 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001223 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001224 // check for builtin casting operators
1225 if (emit->do_viper_types && qst == MP_QSTR_int) {
1226 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1227 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1228 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1229 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1230 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1231 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1232 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1233 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1234 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
1235 } else {
1236 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1237 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1238 }
Damien13ed3a62013-10-08 09:05:10 +01001239}
1240
Damien George7ff996c2014-09-08 23:05:16 +01001241STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001242 // depends on type of subject:
1243 // - integer, function, pointer to integers: error
1244 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001245 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001246 vtype_kind_t vtype_base;
1247 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1248 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001249 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001250 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1251}
1252
Damien George7ff996c2014-09-08 23:05:16 +01001253STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001254 vtype_kind_t vtype_base;
1255 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1256 assert(vtype_base == VTYPE_PYOBJ);
1257 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001258 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001259}
1260
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001261STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001262 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001263 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001264 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001265}
1266
Damien George729f7b42014-04-17 22:10:53 +01001267STATIC void emit_native_load_subscr(emit_t *emit) {
1268 vtype_kind_t vtype_lhs, vtype_rhs;
1269 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_2, &vtype_lhs, REG_ARG_1);
1270 if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George7fe21912014-08-16 22:31:57 +01001271 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 +01001272 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1273 } else {
1274 printf("ViperTypeError: can't do subscr of types %d and %d\n", vtype_lhs, vtype_rhs);
Damien George729f7b42014-04-17 22:10:53 +01001275 }
1276}
1277
Damien George7ff996c2014-09-08 23:05:16 +01001278STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001279 vtype_kind_t vtype;
Damien3ef4abb2013-10-12 16:53:13 +01001280#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001281 if (local_num == 0) {
1282 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001283 } else if (local_num == 1) {
1284 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1285 } else if (local_num == 2) {
1286 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001287 } else {
Damien George0b610de2014-09-29 16:25:04 +01001288 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1289 asm_x64_mov_r64_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +01001290 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001291#elif N_X86
1292 if (local_num == 0) {
1293 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001294 } else if (local_num == 1) {
1295 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001296 } else if (local_num == 2) {
1297 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001298 } else {
Damien George0b610de2014-09-29 16:25:04 +01001299 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1300 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +01001301 }
Damien3ef4abb2013-10-12 16:53:13 +01001302#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001303 if (local_num == 0) {
1304 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1305 } else if (local_num == 1) {
1306 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1307 } else if (local_num == 2) {
1308 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1309 } else {
Damien George0b610de2014-09-29 16:25:04 +01001310 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1311 asm_thumb_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001312 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001313#elif N_ARM
1314 if (local_num == 0) {
1315 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1316 } else if (local_num == 1) {
1317 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1318 } else if (local_num == 2) {
1319 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1320 } else {
Damien George0b610de2014-09-29 16:25:04 +01001321 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1322 asm_arm_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001323 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001324#else
1325 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001326#endif
1327
1328 emit_post(emit);
1329
1330 // check types
1331 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1332 // first time this local is assigned, so give it a type of the object stored in it
1333 emit->local_vtype[local_num] = vtype;
1334 } else if (emit->local_vtype[local_num] != vtype) {
1335 // type of local is not the same as object stored in it
Damien George7ff996c2014-09-08 23:05:16 +01001336 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 +01001337 }
1338}
1339
Damien George7ff996c2014-09-08 23:05:16 +01001340STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001341 // not implemented
1342 assert(0);
1343}
1344
Damien George7ff996c2014-09-08 23:05:16 +01001345STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001346 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001347 vtype_kind_t vtype;
1348 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1349 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001350 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001351 emit_post(emit);
1352}
1353
Damien George7ff996c2014-09-08 23:05:16 +01001354STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001355 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001356 if (vtype == VTYPE_PYOBJ) {
1357 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1358 } else {
1359 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001360 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 +01001361 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001362 }
Damien George7ff996c2014-09-08 23:05:16 +01001363 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001364 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001365}
1366
Damien George7ff996c2014-09-08 23:05:16 +01001367STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001368 vtype_kind_t vtype_base, vtype_val;
1369 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1370 assert(vtype_base == VTYPE_PYOBJ);
1371 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001372 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001373 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001374}
1375
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001376STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001377 DEBUG_printf("store_subscr\n");
1378 // need to compile: base[index] = value
1379
1380 // pop: index, base, value
1381 // optimise case where index is an immediate
1382 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1383
1384 if (vtype_base == VTYPE_PYOBJ) {
1385 // standard Python call
1386 vtype_kind_t vtype_index, vtype_value;
1387 emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3);
1388 assert(vtype_index == VTYPE_PYOBJ);
1389 assert(vtype_value == VTYPE_PYOBJ);
1390 emit_call(emit, MP_F_OBJ_SUBSCR);
1391 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001392 // viper store
1393 // TODO The different machine architectures have very different
1394 // capabilities and requirements for stores, so probably best to
1395 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001396 stack_info_t *top = peek_stack(emit, 0);
1397 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1398 // index is an immediate
1399 mp_int_t index_value = top->u_imm;
1400 emit_pre_pop_discard(emit); // discard index
1401 vtype_kind_t vtype_value;
1402 int reg_base = REG_ARG_1;
1403 int reg_index = REG_ARG_2;
1404 int reg_value = REG_ARG_3;
1405 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001406 #if N_X86
1407 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1408 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1409 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001410 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001411 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001412 switch (vtype_base) {
1413 case VTYPE_PTR8: {
1414 // pointer to 8-bit memory
1415 // TODO optimise to use thumb strb r1, [r2, r3]
1416 if (index_value != 0) {
1417 // index is non-zero
1418 #if N_THUMB
1419 if (index_value > 0 && index_value < 32) {
1420 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1421 break;
1422 }
1423 #endif
1424 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001425 #if N_ARM
1426 asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1427 return;
1428 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001429 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1430 reg_base = reg_index;
1431 }
1432 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1433 break;
1434 }
1435 case VTYPE_PTR16: {
1436 // pointer to 16-bit memory
1437 if (index_value != 0) {
1438 // index is a non-zero immediate
1439 #if N_THUMB
1440 if (index_value > 0 && index_value < 32) {
1441 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1442 break;
1443 }
1444 #endif
1445 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001446 #if N_ARM
1447 asm_arm_strh_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1448 return;
1449 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001450 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1451 reg_base = reg_index;
1452 }
1453 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1454 break;
1455 }
1456 default:
1457 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1458 }
1459 } else {
1460 // index is not an immediate
1461 vtype_kind_t vtype_index, vtype_value;
1462 int reg_index = REG_ARG_2;
1463 int reg_value = REG_ARG_3;
1464 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1465 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001466 #if N_X86
1467 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1468 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1469 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001470 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001471 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001472 switch (vtype_base) {
1473 case VTYPE_PTR8: {
1474 // pointer to 8-bit memory
1475 // TODO optimise to use thumb strb r1, [r2, r3]
1476 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001477 #if N_ARM
1478 asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1479 break;
1480 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001481 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1482 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1483 break;
1484 }
1485 case VTYPE_PTR16: {
1486 // pointer to 16-bit memory
1487 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001488 #if N_ARM
1489 asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1490 break;
1491 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001492 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1493 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1494 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1495 break;
1496 }
1497 default:
1498 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1499 }
1500 }
1501
1502 }
Damien13ed3a62013-10-08 09:05:10 +01001503}
1504
Damien George7ff996c2014-09-08 23:05:16 +01001505STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001506 // TODO implement me!
Damien13ed3a62013-10-08 09:05:10 +01001507 // could support for Python types, just set to None (so GC can reclaim it)
Damien13ed3a62013-10-08 09:05:10 +01001508}
1509
Damien George7ff996c2014-09-08 23:05:16 +01001510STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001511 // TODO implement me!
Damien9ecbcff2013-12-11 00:41:43 +00001512}
1513
Damien Georgee6ce10a2014-09-06 18:38:20 +01001514STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1515 emit_native_pre(emit);
1516 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1517 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001518}
1519
Damien Georgee6ce10a2014-09-06 18:38:20 +01001520STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1521 emit_native_pre(emit);
1522 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1523 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001524}
1525
Damien Georgee6ce10a2014-09-06 18:38:20 +01001526STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001527 vtype_kind_t vtype_base;
1528 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1529 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001530 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 +01001531 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001532}
1533
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001534STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001535 vtype_kind_t vtype_index, vtype_base;
1536 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1537 assert(vtype_index == VTYPE_PYOBJ);
1538 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001539 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 +01001540}
1541
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001542STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001543 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001544 vtype_kind_t vtype;
1545 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1546 emit_post_push_reg_reg(emit, vtype, REG_TEMP0, vtype, REG_TEMP0);
1547}
1548
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001549STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001550 vtype_kind_t vtype0, vtype1;
1551 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1552 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1553}
1554
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001555STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001556 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001557 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001558 emit_post(emit);
1559}
1560
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001561STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001562 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001563 vtype_kind_t vtype0, vtype1;
1564 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1565 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001566}
1567
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001568STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001569 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001570 vtype_kind_t vtype0, vtype1, vtype2;
1571 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1572 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1573}
1574
Damien George7ff996c2014-09-08 23:05:16 +01001575STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001576 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001577 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001578 // need to commit stack because we are jumping elsewhere
1579 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001580 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001581 emit_post(emit);
1582}
1583
Damien George7ff996c2014-09-08 23:05:16 +01001584STATIC void emit_native_jump_helper(emit_t *emit, mp_uint_t label, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001585 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien George6f813482014-09-29 16:41:37 +01001586 switch (vtype) {
1587 case VTYPE_PYOBJ:
1588 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1589 if (!pop) {
1590 adjust_stack(emit, 1);
1591 }
1592 emit_call(emit, MP_F_OBJ_IS_TRUE);
1593 break;
1594 case VTYPE_BOOL:
1595 case VTYPE_INT:
1596 case VTYPE_UINT:
1597 emit_pre_pop_reg(emit, &vtype, REG_RET);
1598 if (!pop) {
1599 adjust_stack(emit, 1);
1600 }
1601 break;
1602 default:
1603 printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
1604 assert(0);
Damien13ed3a62013-10-08 09:05:10 +01001605 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001606 // need to commit stack because we may jump elsewhere
1607 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001608}
1609
Damien George7ff996c2014-09-08 23:05:16 +01001610STATIC void emit_native_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001611 DEBUG_printf("pop_jump_if_true(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001612 emit_native_jump_helper(emit, label, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001613 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien1a6633a2013-11-03 13:58:19 +00001614 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001615}
Damien1a6633a2013-11-03 13:58:19 +00001616
Damien George7ff996c2014-09-08 23:05:16 +01001617STATIC void emit_native_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001618 DEBUG_printf("pop_jump_if_false(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001619 emit_native_jump_helper(emit, label, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001620 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001621 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001622}
Damien Georgea32c1e42014-05-07 18:30:52 +01001623
Damien George7ff996c2014-09-08 23:05:16 +01001624STATIC void emit_native_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001625 DEBUG_printf("jump_if_true_or_pop(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001626 emit_native_jump_helper(emit, label, false);
Damien Georgec90f59e2014-09-06 23:06:36 +01001627 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001628 adjust_stack(emit, -1);
1629 emit_post(emit);
1630}
1631
Damien George7ff996c2014-09-08 23:05:16 +01001632STATIC void emit_native_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001633 DEBUG_printf("jump_if_false_or_pop(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001634 emit_native_jump_helper(emit, label, false);
Damien Georgec90f59e2014-09-06 23:06:36 +01001635 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001636 adjust_stack(emit, -1);
1637 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001638}
1639
Damien George7ff996c2014-09-08 23:05:16 +01001640STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien George25c84642014-05-30 15:20:41 +01001641 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001642}
Damien Georgea32c1e42014-05-07 18:30:52 +01001643
Damien George7ff996c2014-09-08 23:05:16 +01001644STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgea32c1e42014-05-07 18:30:52 +01001645 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001646}
Damien Georgea32c1e42014-05-07 18:30:52 +01001647
Damien George7ff996c2014-09-08 23:05:16 +01001648STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001649 // not supported, or could be with runtime call
1650 assert(0);
1651}
Damien Georgeb601d952014-06-30 05:17:25 +01001652
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001653STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001654 assert(0);
1655}
Damien Georgeb601d952014-06-30 05:17:25 +01001656
Damien George7ff996c2014-09-08 23:05:16 +01001657STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001658 emit_native_pre(emit);
1659 // need to commit stack because we may jump elsewhere
1660 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001661 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 +01001662 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001663 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001664 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001665}
Damien Georgeb601d952014-06-30 05:17:25 +01001666
Damien George7ff996c2014-09-08 23:05:16 +01001667STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001668 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001669}
Damien Georgeb601d952014-06-30 05:17:25 +01001670
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001671STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001672 emit_pre_pop_discard(emit);
1673 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001674}
Damiend2755ec2013-10-16 23:58:48 +01001675
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001676STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001677 // perhaps the difficult one, as we want to rewrite for loops using native code
1678 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001679
1680 vtype_kind_t vtype;
1681 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1682 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001683 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01001684 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001685}
Damiend2755ec2013-10-16 23:58:48 +01001686
Damien George7ff996c2014-09-08 23:05:16 +01001687STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001688 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001689 vtype_kind_t vtype;
1690 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1691 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001692 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01001693 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
1694 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01001695 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1696}
1697
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001698STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001699 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001700 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001701 adjust_stack(emit, -1);
1702 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001703}
1704
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001705STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001706 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001707 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001708 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001709 emit_post(emit);
1710}
1711
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001712STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeb601d952014-06-30 05:17:25 +01001713 /*
1714 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001715 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001716 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01001717 emit_post(emit);
1718 */
Damien13ed3a62013-10-08 09:05:10 +01001719}
1720
Damien Georged17926d2014-03-30 13:35:08 +01001721STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001722 vtype_kind_t vtype;
1723 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1724 assert(vtype == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001725 if (op == MP_UNARY_OP_NOT) {
Damien Georged6230f62014-09-23 14:10:03 +00001726 // we need to synthesise this operation by converting to bool first
1727 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
Damien George3112cde2014-09-29 18:45:42 +01001728 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgeb601d952014-06-30 05:17:25 +01001729 }
Damien Georged6230f62014-09-23 14:10:03 +00001730 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1731 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001732}
1733
Damien Georged17926d2014-03-30 13:35:08 +01001734STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001735 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01001736 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
1737 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01001738 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01001739 #if N_X64 || N_X86
1740 // special cases for x86 and shifting
1741 if (op == MP_BINARY_OP_LSHIFT
1742 || op == MP_BINARY_OP_INPLACE_LSHIFT
1743 || op == MP_BINARY_OP_RSHIFT
1744 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1745 #if N_X64
1746 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
1747 #else
1748 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
1749 #endif
1750 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1751 ASM_LSL_REG(emit->as, REG_RET);
1752 } else {
1753 ASM_ASR_REG(emit->as, REG_RET);
1754 }
1755 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
1756 return;
1757 }
1758 #endif
1759 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001760 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001761 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
1762 if (0) {
1763 // dummy
1764 #if !(N_X64 || N_X86)
1765 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1766 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00001767 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001768 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1769 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1770 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1771 #endif
1772 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
1773 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1774 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1775 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
1776 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1777 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1778 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
1779 // comparison ops are (in enum order):
1780 // MP_BINARY_OP_LESS
1781 // MP_BINARY_OP_MORE
1782 // MP_BINARY_OP_EQUAL
1783 // MP_BINARY_OP_LESS_EQUAL
1784 // MP_BINARY_OP_MORE_EQUAL
1785 // MP_BINARY_OP_NOT_EQUAL
1786 #if N_X64
1787 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
1788 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
1789 static byte ops[6] = {
1790 ASM_X64_CC_JL,
1791 ASM_X64_CC_JG,
1792 ASM_X64_CC_JE,
1793 ASM_X64_CC_JLE,
1794 ASM_X64_CC_JGE,
1795 ASM_X64_CC_JNE,
1796 };
1797 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1798 #elif N_X86
1799 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
1800 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
1801 static byte ops[6] = {
1802 ASM_X86_CC_JL,
1803 ASM_X86_CC_JG,
1804 ASM_X86_CC_JE,
1805 ASM_X86_CC_JLE,
1806 ASM_X86_CC_JGE,
1807 ASM_X86_CC_JNE,
1808 };
1809 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1810 #elif N_THUMB
1811 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
1812 static uint16_t ops[6] = {
1813 ASM_THUMB_OP_ITE_GE,
1814 ASM_THUMB_OP_ITE_GT,
1815 ASM_THUMB_OP_ITE_EQ,
1816 ASM_THUMB_OP_ITE_GT,
1817 ASM_THUMB_OP_ITE_GE,
1818 ASM_THUMB_OP_ITE_EQ,
1819 };
1820 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
1821 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
1822 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
1823 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
1824 #elif N_ARM
Fabian Vogte5268962014-10-04 00:53:46 +02001825 asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
1826 static uint ccs[6] = {
1827 ASM_ARM_CC_LT,
1828 ASM_ARM_CC_GT,
1829 ASM_ARM_CC_EQ,
1830 ASM_ARM_CC_LE,
1831 ASM_ARM_CC_GE,
1832 ASM_ARM_CC_NE,
1833 };
1834 asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
Damien George3112cde2014-09-29 18:45:42 +01001835 #else
1836 #error not implemented
1837 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00001838 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
1839 } else {
1840 // TODO other ops not yet implemented
1841 assert(0);
1842 }
Damien13ed3a62013-10-08 09:05:10 +01001843 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01001844 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00001845 bool invert = false;
1846 if (op == MP_BINARY_OP_NOT_IN) {
1847 invert = true;
1848 op = MP_BINARY_OP_IN;
1849 } else if (op == MP_BINARY_OP_IS_NOT) {
1850 invert = true;
1851 op = MP_BINARY_OP_IS;
1852 }
Damien George7fe21912014-08-16 22:31:57 +01001853 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00001854 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01001855 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00001856 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
1857 }
Damien13ed3a62013-10-08 09:05:10 +01001858 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1859 } else {
1860 printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs);
Damien Georgea5190a72014-08-15 22:39:08 +01001861 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001862 }
1863}
1864
Damien George7ff996c2014-09-08 23:05:16 +01001865STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01001866 // for viper: call runtime, with types of args
1867 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00001868 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001869 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001870 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01001871 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01001872}
1873
Damien George7ff996c2014-09-08 23:05:16 +01001874STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001875 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001876 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001877 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001878 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
1879}
1880
Damien George7ff996c2014-09-08 23:05:16 +01001881STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01001882 // only used in list comprehension
1883 vtype_kind_t vtype_list, vtype_item;
1884 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
1885 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
1886 assert(vtype_list == VTYPE_PYOBJ);
1887 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001888 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01001889 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001890}
1891
Damien George7ff996c2014-09-08 23:05:16 +01001892STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001893 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001894 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001895 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
1896}
1897
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001898STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001899 vtype_kind_t vtype_key, vtype_value, vtype_map;
1900 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
1901 assert(vtype_key == VTYPE_PYOBJ);
1902 assert(vtype_value == VTYPE_PYOBJ);
1903 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001904 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01001905 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
1906}
1907
Damien George7ff996c2014-09-08 23:05:16 +01001908STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01001909 // only used in list comprehension
1910 vtype_kind_t vtype_map, vtype_key, vtype_value;
1911 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
1912 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
1913 assert(vtype_map == VTYPE_PYOBJ);
1914 assert(vtype_key == VTYPE_PYOBJ);
1915 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001916 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01001917 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001918}
1919
Damien George7ff996c2014-09-08 23:05:16 +01001920STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001921 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001922 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001923 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001924 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
1925}
1926
Damien George7ff996c2014-09-08 23:05:16 +01001927STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01001928 // only used in set comprehension
1929 vtype_kind_t vtype_set, vtype_item;
1930 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
1931 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
1932 assert(vtype_set == VTYPE_PYOBJ);
1933 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001934 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01001935 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001936}
Damiend2755ec2013-10-16 23:58:48 +01001937
Damien George7ff996c2014-09-08 23:05:16 +01001938STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001939 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01001940 if (n_args == 2) {
1941 vtype_kind_t vtype_start, vtype_stop;
1942 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
1943 assert(vtype_start == VTYPE_PYOBJ);
1944 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001945 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 +01001946 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1947 } else {
1948 assert(n_args == 3);
1949 vtype_kind_t vtype_start, vtype_stop, vtype_step;
1950 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
1951 assert(vtype_start == VTYPE_PYOBJ);
1952 assert(vtype_stop == VTYPE_PYOBJ);
1953 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001954 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01001955 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1956 }
Damien13ed3a62013-10-08 09:05:10 +01001957}
Damien Georgecdd96df2014-04-06 12:58:40 +01001958
Damien George7ff996c2014-09-08 23:05:16 +01001959STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001960 DEBUG_printf("unpack_sequence %d\n", n_args);
1961 vtype_kind_t vtype_base;
1962 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
1963 assert(vtype_base == VTYPE_PYOBJ);
1964 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01001965 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01001966}
Damien Georgecdd96df2014-04-06 12:58:40 +01001967
Damien George7ff996c2014-09-08 23:05:16 +01001968STATIC 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 +01001969 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
1970 vtype_kind_t vtype_base;
1971 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
1972 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001973 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 +01001974 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 +01001975}
1976
Damien George7ff996c2014-09-08 23:05:16 +01001977STATIC 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 +01001978 // 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 +00001979 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001980 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01001981 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 +01001982 } else {
1983 vtype_kind_t vtype_def_tuple, vtype_def_dict;
1984 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
1985 assert(vtype_def_tuple == VTYPE_PYOBJ);
1986 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001987 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 +01001988 }
Damien13ed3a62013-10-08 09:05:10 +01001989 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1990}
1991
Damien George7ff996c2014-09-08 23:05:16 +01001992STATIC 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 +01001993 assert(0);
1994}
1995
Damien George7ff996c2014-09-08 23:05:16 +01001996STATIC 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 +00001997 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
1998
Damien Georgee9dac3b2014-09-29 22:10:41 +01001999 // TODO: in viper mode, call special runtime routine with type info for args,
2000 // and wanted type info for return, to remove need for boxing/unboxing
2001
Damien George922ddd62014-04-09 12:43:17 +01002002 assert(!star_flags);
Damien Georgecd82e022014-02-02 13:11:48 +00002003
Damien Georgee9dac3b2014-09-29 22:10:41 +01002004 emit_native_pre(emit);
2005 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
2006 if (vtype_fun == VTYPE_BUILTIN_CAST) {
2007 // casting operator
2008 assert(n_positional == 1 && n_keyword == 0);
2009 DEBUG_printf(" cast to %d\n", vtype_fun);
2010 vtype_kind_t vtype_cast = peek_stack(emit, 1)->u_imm;
2011 switch (peek_vtype(emit, 0)) {
2012 case VTYPE_PYOBJ: {
2013 vtype_kind_t vtype;
2014 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2015 emit_pre_pop_discard(emit);
2016 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
2017 emit_post_push_reg(emit, vtype_cast, REG_RET);
2018 break;
2019 }
2020 case VTYPE_BOOL:
2021 case VTYPE_INT:
2022 case VTYPE_UINT:
2023 case VTYPE_PTR:
2024 case VTYPE_PTR8:
2025 case VTYPE_PTR16:
2026 case VTYPE_PTR_NONE:
2027 emit_fold_stack_top(emit, REG_ARG_1);
2028 emit_post_top_set_vtype(emit, vtype_cast);
2029 break;
2030 default:
2031 assert(!"TODO: convert obj to int");
2032 }
2033 } else {
2034 if (n_positional != 0 || n_keyword != 0) {
2035 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2036 }
Damien13ed3a62013-10-08 09:05:10 +01002037 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2038 assert(vtype_fun == VTYPE_PYOBJ);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002039 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2040 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien Georgecd82e022014-02-02 13:11:48 +00002041 }
Damien13ed3a62013-10-08 09:05:10 +01002042}
2043
Damien George7ff996c2014-09-08 23:05:16 +01002044STATIC 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 +01002045 assert(!star_flags);
Damien Georgece8f07a2014-03-27 23:30:26 +00002046 emit_native_pre(emit);
Damien Georgecdd96df2014-04-06 12:58:40 +01002047 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 +01002048 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 +01002049 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2050}
2051
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002052STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002053 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002054 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002055 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2056 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002057 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002058 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002059 } else {
2060 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002061 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002062 } else {
2063 vtype_kind_t vtype;
2064 emit_pre_pop_reg(emit, &vtype, REG_RET);
2065 if (vtype != emit->return_vtype) {
2066 printf("ViperTypeError: incompatible return type\n");
2067 }
Damien George2ac4af62014-08-15 16:45:41 +01002068 }
Damien13ed3a62013-10-08 09:05:10 +01002069 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002070 vtype_kind_t vtype;
2071 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002072 assert(vtype == VTYPE_PYOBJ);
2073 }
2074 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002075 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2076 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002077}
2078
Damien George7ff996c2014-09-08 23:05:16 +01002079STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002080 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002081 vtype_kind_t vtype_exc;
2082 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2083 if (vtype_exc != VTYPE_PYOBJ) {
2084 printf("ViperTypeError: must raise an object\n");
2085 }
2086 // 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 +01002087 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002088}
Damien Georgeb601d952014-06-30 05:17:25 +01002089
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002090STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002091 // not supported (for now)
2092 assert(0);
2093}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002094STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002095 // not supported (for now)
2096 assert(0);
2097}
2098
Damien Georgeb601d952014-06-30 05:17:25 +01002099STATIC void emit_native_start_except_handler(emit_t *emit) {
2100 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2101 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2102 // the first 2 elements, so we can get the thrown value.
2103 adjust_stack(emit, 2);
2104 vtype_kind_t vtype_nlr;
2105 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002106 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002107 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
2108}
2109
2110STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002111 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002112}
2113
Damien13ed3a62013-10-08 09:05:10 +01002114const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002115 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002116 emit_native_start_pass,
2117 emit_native_end_pass,
2118 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002119 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002120 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002121
2122 emit_native_load_id,
2123 emit_native_store_id,
2124 emit_native_delete_id,
2125
2126 emit_native_label_assign,
2127 emit_native_import_name,
2128 emit_native_import_from,
2129 emit_native_import_star,
2130 emit_native_load_const_tok,
2131 emit_native_load_const_small_int,
2132 emit_native_load_const_int,
2133 emit_native_load_const_dec,
Damien13ed3a62013-10-08 09:05:10 +01002134 emit_native_load_const_str,
Damien George3558f622014-04-20 17:50:40 +01002135 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002136 emit_native_load_fast,
Damien13ed3a62013-10-08 09:05:10 +01002137 emit_native_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +00002138 emit_native_load_name,
2139 emit_native_load_global,
Damien13ed3a62013-10-08 09:05:10 +01002140 emit_native_load_attr,
2141 emit_native_load_method,
2142 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002143 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002144 emit_native_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +00002145 emit_native_store_deref,
Damien13ed3a62013-10-08 09:05:10 +01002146 emit_native_store_name,
2147 emit_native_store_global,
Damien13ed3a62013-10-08 09:05:10 +01002148 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002149 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002150 emit_native_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +00002151 emit_native_delete_deref,
Damien13ed3a62013-10-08 09:05:10 +01002152 emit_native_delete_name,
2153 emit_native_delete_global,
Damien13ed3a62013-10-08 09:05:10 +01002154 emit_native_delete_attr,
2155 emit_native_delete_subscr,
2156 emit_native_dup_top,
2157 emit_native_dup_top_two,
2158 emit_native_pop_top,
2159 emit_native_rot_two,
2160 emit_native_rot_three,
2161 emit_native_jump,
2162 emit_native_pop_jump_if_true,
2163 emit_native_pop_jump_if_false,
2164 emit_native_jump_if_true_or_pop,
2165 emit_native_jump_if_false_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002166 emit_native_break_loop,
2167 emit_native_continue_loop,
2168 emit_native_setup_with,
2169 emit_native_with_cleanup,
2170 emit_native_setup_except,
2171 emit_native_setup_finally,
2172 emit_native_end_finally,
2173 emit_native_get_iter,
2174 emit_native_for_iter,
2175 emit_native_for_iter_end,
2176 emit_native_pop_block,
2177 emit_native_pop_except,
2178 emit_native_unary_op,
2179 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002180 emit_native_build_tuple,
2181 emit_native_build_list,
2182 emit_native_list_append,
2183 emit_native_build_map,
2184 emit_native_store_map,
2185 emit_native_map_add,
2186 emit_native_build_set,
2187 emit_native_set_add,
2188 emit_native_build_slice,
2189 emit_native_unpack_sequence,
2190 emit_native_unpack_ex,
2191 emit_native_make_function,
2192 emit_native_make_closure,
2193 emit_native_call_function,
2194 emit_native_call_method,
2195 emit_native_return_value,
2196 emit_native_raise_varargs,
2197 emit_native_yield_value,
2198 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002199
2200 emit_native_start_except_handler,
2201 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002202};
2203
Damien Georgec90f59e2014-09-06 23:06:36 +01002204#endif