blob: c2aa7a76bcfd1e8ce8b9b202cee082828d6f8c18 [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
143#define ASM_MOV_REG_TO_REG asm_x64_mov_r64_to_r64
144#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x64_mov_local_addr_to_r64
145
146#elif N_X86
147
148// x86 specific stuff
149
150#include "asmx86.h"
151
152STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
153 [MP_F_CONVERT_OBJ_TO_NATIVE] = 2,
154 [MP_F_CONVERT_NATIVE_TO_OBJ] = 2,
155 [MP_F_LOAD_CONST_INT] = 1,
156 [MP_F_LOAD_CONST_DEC] = 1,
157 [MP_F_LOAD_CONST_STR] = 1,
158 [MP_F_LOAD_CONST_BYTES] = 1,
159 [MP_F_LOAD_NAME] = 1,
160 [MP_F_LOAD_GLOBAL] = 1,
161 [MP_F_LOAD_BUILD_CLASS] = 0,
162 [MP_F_LOAD_ATTR] = 2,
163 [MP_F_LOAD_METHOD] = 3,
164 [MP_F_STORE_NAME] = 2,
165 [MP_F_STORE_GLOBAL] = 2,
166 [MP_F_STORE_ATTR] = 3,
167 [MP_F_OBJ_SUBSCR] = 3,
168 [MP_F_OBJ_IS_TRUE] = 1,
169 [MP_F_UNARY_OP] = 2,
170 [MP_F_BINARY_OP] = 3,
171 [MP_F_BUILD_TUPLE] = 2,
172 [MP_F_BUILD_LIST] = 2,
173 [MP_F_LIST_APPEND] = 2,
174 [MP_F_BUILD_MAP] = 1,
175 [MP_F_STORE_MAP] = 3,
176#if MICROPY_PY_BUILTINS_SET
177 [MP_F_BUILD_SET] = 2,
178 [MP_F_STORE_SET] = 2,
179#endif
180 [MP_F_MAKE_FUNCTION_FROM_RAW_CODE] = 3,
181 [MP_F_NATIVE_CALL_FUNCTION_N_KW] = 3,
182 [MP_F_CALL_METHOD_N_KW] = 3,
183 [MP_F_GETITER] = 1,
184 [MP_F_ITERNEXT] = 1,
185 [MP_F_NLR_PUSH] = 1,
186 [MP_F_NLR_POP] = 0,
187 [MP_F_NATIVE_RAISE] = 1,
188 [MP_F_IMPORT_NAME] = 3,
189 [MP_F_IMPORT_FROM] = 2,
190 [MP_F_IMPORT_ALL] = 1,
191#if MICROPY_PY_BUILTINS_SLICE
192 [MP_F_NEW_SLICE] = 3,
193#endif
194 [MP_F_UNPACK_SEQUENCE] = 3,
195 [MP_F_UNPACK_EX] = 3,
196 [MP_F_DELETE_NAME] = 1,
197 [MP_F_DELETE_GLOBAL] = 1,
198};
199
200#define EXPORT_FUN(name) emit_native_x86_##name
201
Damien George0b610de2014-09-29 16:25:04 +0100202#define REG_RET ASM_X86_REG_EAX
Damien George6eae8612014-09-08 22:16:35 +0000203#define REG_ARG_1 ASM_X86_REG_ARG_1
204#define REG_ARG_2 ASM_X86_REG_ARG_2
205#define REG_ARG_3 ASM_X86_REG_ARG_3
Damien George81057362014-09-07 01:06:19 +0100206
Damien George25d90412014-09-06 23:24:32 +0000207// caller-save, so can be used as temporaries
Damien George0b610de2014-09-29 16:25:04 +0100208#define REG_TEMP0 ASM_X86_REG_EAX
209#define REG_TEMP1 ASM_X86_REG_ECX
210#define REG_TEMP2 ASM_X86_REG_EDX
Damien Georgec90f59e2014-09-06 23:06:36 +0100211
Damien George25d90412014-09-06 23:24:32 +0000212// callee-save, so can be used as locals
Damien George0b610de2014-09-29 16:25:04 +0100213#define REG_LOCAL_1 ASM_X86_REG_EBX
214#define REG_LOCAL_2 ASM_X86_REG_ESI
215#define REG_LOCAL_3 ASM_X86_REG_EDI
Damien George25d90412014-09-06 23:24:32 +0000216#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100217
218#define ASM_PASS_COMPUTE ASM_X86_PASS_COMPUTE
219#define ASM_PASS_EMIT ASM_X86_PASS_EMIT
220
221#define ASM_T asm_x86_t
222#define ASM_NEW asm_x86_new
223#define ASM_FREE asm_x86_free
224#define ASM_GET_CODE asm_x86_get_code
225#define ASM_GET_CODE_SIZE asm_x86_get_code_size
226#define ASM_START_PASS asm_x86_start_pass
227#define ASM_END_PASS asm_x86_end_pass
228#define ASM_ENTRY asm_x86_entry
229#define ASM_EXIT asm_x86_exit
230
231#define ASM_LABEL_ASSIGN asm_x86_label_assign
232#define ASM_JUMP asm_x86_jmp_label
233#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
234 do { \
235 asm_x86_test_r8_with_r8(as, reg, reg); \
236 asm_x86_jcc_label(as, ASM_X86_CC_JZ, label); \
237 } while (0)
238#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
239 do { \
240 asm_x86_test_r8_with_r8(as, reg, reg); \
241 asm_x86_jcc_label(as, ASM_X86_CC_JNZ, label); \
242 } while (0)
243#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
244 do { \
245 asm_x86_cmp_r32_with_r32(as, reg1, reg2); \
246 asm_x86_jcc_label(as, ASM_X86_CC_JE, label); \
247 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100248#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 +0100249
250#define ASM_MOV_REG_TO_LOCAL asm_x86_mov_r32_to_local
251#define ASM_MOV_IMM_TO_REG asm_x86_mov_i32_to_r32
252#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x86_mov_i32_to_r32_aligned
253#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
254 do { \
255 asm_x86_mov_i32_to_r32(as, (imm), (reg_temp)); \
256 asm_x86_mov_r32_to_local(as, (reg_temp), (local_num)); \
257 } while (false)
258#define ASM_MOV_LOCAL_TO_REG asm_x86_mov_local_to_r32
259#define ASM_MOV_REG_TO_REG asm_x86_mov_r32_to_r32
260#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x86_mov_local_addr_to_r32
Damien13ed3a62013-10-08 09:05:10 +0100261
Damien3ef4abb2013-10-12 16:53:13 +0100262#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100263
264// thumb specific stuff
265
266#include "asmthumb.h"
267
Damien13ed3a62013-10-08 09:05:10 +0100268#define EXPORT_FUN(name) emit_native_thumb_##name
269
Damien George0b610de2014-09-29 16:25:04 +0100270#define REG_RET ASM_THUMB_REG_R0
271#define REG_ARG_1 ASM_THUMB_REG_R0
272#define REG_ARG_2 ASM_THUMB_REG_R1
273#define REG_ARG_3 ASM_THUMB_REG_R2
274#define REG_ARG_4 ASM_THUMB_REG_R3
Damien George81057362014-09-07 01:06:19 +0100275
Damien George0b610de2014-09-29 16:25:04 +0100276#define REG_TEMP0 ASM_THUMB_REG_R0
277#define REG_TEMP1 ASM_THUMB_REG_R1
278#define REG_TEMP2 ASM_THUMB_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100279
Damien George0b610de2014-09-29 16:25:04 +0100280#define REG_LOCAL_1 ASM_THUMB_REG_R4
281#define REG_LOCAL_2 ASM_THUMB_REG_R5
282#define REG_LOCAL_3 ASM_THUMB_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100283#define REG_LOCAL_NUM (3)
284
285#define ASM_PASS_COMPUTE ASM_THUMB_PASS_COMPUTE
286#define ASM_PASS_EMIT ASM_THUMB_PASS_EMIT
287
288#define ASM_T asm_thumb_t
289#define ASM_NEW asm_thumb_new
290#define ASM_FREE asm_thumb_free
291#define ASM_GET_CODE asm_thumb_get_code
292#define ASM_GET_CODE_SIZE asm_thumb_get_code_size
293#define ASM_START_PASS asm_thumb_start_pass
294#define ASM_END_PASS asm_thumb_end_pass
295#define ASM_ENTRY asm_thumb_entry
296#define ASM_EXIT asm_thumb_exit
297
298#define ASM_LABEL_ASSIGN asm_thumb_label_assign
299#define ASM_JUMP asm_thumb_b_label
300#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
301 do { \
302 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100303 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100304 } while (0)
305#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
306 do { \
307 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100308 asm_thumb_bcc_label(as, ASM_THUMB_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100309 } while (0)
310#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
311 do { \
312 asm_thumb_cmp_rlo_rlo(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100313 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100314 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100315#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 +0100316
317#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_thumb_mov_local_reg(as, (local_num), (reg))
318#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_optimised(as, (reg), (imm))
319#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_aligned(as, (reg), (imm))
320#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
321 do { \
322 asm_thumb_mov_reg_i32_optimised(as, (reg_temp), (imm)); \
323 asm_thumb_mov_local_reg(as, (local_num), (reg_temp)); \
324 } while (false)
325#define ASM_MOV_LOCAL_TO_REG(as, local_num, reg) asm_thumb_mov_reg_local(as, (reg), (local_num))
326#define ASM_MOV_REG_TO_REG(as, reg_src, reg_dest) asm_thumb_mov_reg_reg(as, (reg_dest), (reg_src))
327#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 +0100328
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200329#elif N_ARM
330
331// ARM specific stuff
332
333#include "asmarm.h"
334
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200335#define EXPORT_FUN(name) emit_native_arm_##name
336
Damien George0b610de2014-09-29 16:25:04 +0100337#define REG_RET ASM_ARM_REG_R0
338#define REG_ARG_1 ASM_ARM_REG_R0
339#define REG_ARG_2 ASM_ARM_REG_R1
340#define REG_ARG_3 ASM_ARM_REG_R2
341#define REG_ARG_4 ASM_ARM_REG_R3
Damien George81057362014-09-07 01:06:19 +0100342
Damien George0b610de2014-09-29 16:25:04 +0100343#define REG_TEMP0 ASM_ARM_REG_R0
344#define REG_TEMP1 ASM_ARM_REG_R1
345#define REG_TEMP2 ASM_ARM_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100346
Damien George0b610de2014-09-29 16:25:04 +0100347#define REG_LOCAL_1 ASM_ARM_REG_R4
348#define REG_LOCAL_2 ASM_ARM_REG_R5
349#define REG_LOCAL_3 ASM_ARM_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100350#define REG_LOCAL_NUM (3)
351
352#define ASM_PASS_COMPUTE ASM_ARM_PASS_COMPUTE
353#define ASM_PASS_EMIT ASM_ARM_PASS_EMIT
354
355#define ASM_T asm_arm_t
356#define ASM_NEW asm_arm_new
357#define ASM_FREE asm_arm_free
358#define ASM_GET_CODE asm_arm_get_code
359#define ASM_GET_CODE_SIZE asm_arm_get_code_size
360#define ASM_START_PASS asm_arm_start_pass
361#define ASM_END_PASS asm_arm_end_pass
362#define ASM_ENTRY asm_arm_entry
363#define ASM_EXIT asm_arm_exit
364
365#define ASM_LABEL_ASSIGN asm_arm_label_assign
366#define ASM_JUMP asm_arm_b_label
367#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
368 do { \
369 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100370 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100371 } while (0)
372#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
373 do { \
374 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100375 asm_arm_bcc_label(as, ASM_ARM_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100376 } while (0)
377#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
378 do { \
379 asm_arm_cmp_reg_reg(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100380 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100381 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100382#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 +0100383
384#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_arm_mov_local_reg(as, (local_num), (reg))
385#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
386#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
387#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
388 do { \
389 asm_arm_mov_reg_i32(as, (reg_temp), (imm)); \
390 asm_arm_mov_local_reg(as, (local_num), (reg_temp)); \
391 } while (false)
392#define ASM_MOV_LOCAL_TO_REG(as, local_num, reg) asm_arm_mov_reg_local(as, (reg), (local_num))
393#define ASM_MOV_REG_TO_REG(as, reg_src, reg_dest) asm_arm_mov_reg_reg(as, (reg_dest), (reg_src))
394#define ASM_MOV_LOCAL_ADDR_TO_REG(as, local_num, reg) asm_arm_mov_reg_local_addr(as, (reg), (local_num))
395
396#else
397
398#error unknown native emitter
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200399
Damien13ed3a62013-10-08 09:05:10 +0100400#endif
401
402typedef enum {
Damienff8ed772013-10-08 22:18:32 +0100403 STACK_VALUE,
404 STACK_REG,
405 STACK_IMM,
406} stack_info_kind_t;
Damien13ed3a62013-10-08 09:05:10 +0100407
408typedef enum {
Damien George2ac4af62014-08-15 16:45:41 +0100409 VTYPE_PYOBJ = MP_NATIVE_TYPE_OBJ,
410 VTYPE_BOOL = MP_NATIVE_TYPE_BOOL,
411 VTYPE_INT = MP_NATIVE_TYPE_INT,
412 VTYPE_UINT = MP_NATIVE_TYPE_UINT,
Damien13ed3a62013-10-08 09:05:10 +0100413 VTYPE_UNBOUND,
Damien13ed3a62013-10-08 09:05:10 +0100414 VTYPE_PTR,
415 VTYPE_PTR_NONE,
416 VTYPE_BUILTIN_V_INT,
417} vtype_kind_t;
418
Damienff8ed772013-10-08 22:18:32 +0100419typedef struct _stack_info_t {
420 vtype_kind_t vtype;
421 stack_info_kind_t kind;
422 union {
423 int u_reg;
Damien George40f3c022014-07-03 13:25:24 +0100424 mp_int_t u_imm;
Damienff8ed772013-10-08 22:18:32 +0100425 };
426} stack_info_t;
427
Damien13ed3a62013-10-08 09:05:10 +0100428struct _emit_t {
429 int pass;
430
431 bool do_viper_types;
Damienff8ed772013-10-08 22:18:32 +0100432
Damien George2ac4af62014-08-15 16:45:41 +0100433 vtype_kind_t return_vtype;
434
Damien George7ff996c2014-09-08 23:05:16 +0100435 mp_uint_t local_vtype_alloc;
Damien13ed3a62013-10-08 09:05:10 +0100436 vtype_kind_t *local_vtype;
Damienff8ed772013-10-08 22:18:32 +0100437
Damien George7ff996c2014-09-08 23:05:16 +0100438 mp_uint_t stack_info_alloc;
Damienff8ed772013-10-08 22:18:32 +0100439 stack_info_t *stack_info;
440
Damien13ed3a62013-10-08 09:05:10 +0100441 int stack_start;
442 int stack_size;
443
444 bool last_emit_was_return_value;
445
Damien13ed3a62013-10-08 09:05:10 +0100446 scope_t *scope;
447
Damien Georgec90f59e2014-09-06 23:06:36 +0100448 ASM_T *as;
Damien13ed3a62013-10-08 09:05:10 +0100449};
450
Damien George7ff996c2014-09-08 23:05:16 +0100451emit_t *EXPORT_FUN(new)(mp_uint_t max_num_labels) {
Damien George36db6bc2014-05-07 17:24:22 +0100452 emit_t *emit = m_new0(emit_t, 1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100453 emit->as = ASM_NEW(max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100454 return emit;
455}
456
Damien George41d02b62014-01-24 22:42:28 +0000457void EXPORT_FUN(free)(emit_t *emit) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100458 ASM_FREE(emit->as, false);
Damien George36db6bc2014-05-07 17:24:22 +0100459 m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
460 m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200461 m_del_obj(emit_t, emit);
462}
463
Damien George2ac4af62014-08-15 16:45:41 +0100464STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
465 switch (op) {
466 case MP_EMIT_NATIVE_TYPE_ENABLE:
467 emit->do_viper_types = arg1;
468 break;
469
470 default: {
471 vtype_kind_t type;
472 switch (arg2) {
473 case MP_QSTR_object: type = VTYPE_PYOBJ; break;
474 case MP_QSTR_bool: type = VTYPE_BOOL; break;
475 case MP_QSTR_int: type = VTYPE_INT; break;
476 case MP_QSTR_uint: type = VTYPE_UINT; break;
477 default: printf("ViperTypeError: unknown type %s\n", qstr_str(arg2)); return;
478 }
479 if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
480 emit->return_vtype = type;
481 } else {
482 assert(arg1 < emit->local_vtype_alloc);
483 emit->local_vtype[arg1] = type;
484 }
485 break;
486 }
487 }
Damien13ed3a62013-10-08 09:05:10 +0100488}
489
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200490STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien Georged6230f62014-09-23 14:10:03 +0000491 DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope);
492
Damien13ed3a62013-10-08 09:05:10 +0100493 emit->pass = pass;
494 emit->stack_start = 0;
495 emit->stack_size = 0;
496 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100497 emit->scope = scope;
498
Damien George36db6bc2014-05-07 17:24:22 +0100499 // allocate memory for keeping track of the types of locals
500 if (emit->local_vtype_alloc < scope->num_locals) {
501 emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
502 emit->local_vtype_alloc = scope->num_locals;
Damienff8ed772013-10-08 22:18:32 +0100503 }
Damien George36db6bc2014-05-07 17:24:22 +0100504
505 // allocate memory for keeping track of the objects on the stack
506 // XXX don't know stack size on entry, and it should be maximum over all scopes
Damienff8ed772013-10-08 22:18:32 +0100507 if (emit->stack_info == NULL) {
Damien George36db6bc2014-05-07 17:24:22 +0100508 emit->stack_info_alloc = scope->stack_size + 50;
Damienff8ed772013-10-08 22:18:32 +0100509 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100510 }
511
Damien George2ac4af62014-08-15 16:45:41 +0100512 // set default type for return and arguments
513 emit->return_vtype = VTYPE_PYOBJ;
Damien Georgea5190a72014-08-15 22:39:08 +0100514 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100515 emit->local_vtype[i] = VTYPE_PYOBJ;
516 }
Damien Georgea5190a72014-08-15 22:39:08 +0100517
518 // local variables begin unbound, and have unknown type
519 for (mp_uint_t i = emit->scope->num_pos_args; i < emit->local_vtype_alloc; i++) {
520 emit->local_vtype[i] = VTYPE_UNBOUND;
521 }
522
523 // values on stack begin unbound
524 for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100525 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgea5190a72014-08-15 22:39:08 +0100526 emit->stack_info[i].vtype = VTYPE_UNBOUND;
Damien13ed3a62013-10-08 09:05:10 +0100527 }
528
Damien Georgec90f59e2014-09-06 23:06:36 +0100529 ASM_START_PASS(emit->as, pass == MP_PASS_EMIT ? ASM_PASS_EMIT : ASM_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100530
531 // entry to function
532 int num_locals = 0;
Damien George36db6bc2014-05-07 17:24:22 +0100533 if (pass > MP_PASS_SCOPE) {
Damien13ed3a62013-10-08 09:05:10 +0100534 num_locals = scope->num_locals - REG_LOCAL_NUM;
535 if (num_locals < 0) {
536 num_locals = 0;
537 }
538 emit->stack_start = num_locals;
539 num_locals += scope->stack_size;
540 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100541 ASM_ENTRY(emit->as, num_locals);
Damien13ed3a62013-10-08 09:05:10 +0100542
543 // initialise locals from parameters
Damien3ef4abb2013-10-12 16:53:13 +0100544#if N_X64
Damien George2827d622014-04-27 15:50:52 +0100545 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100546 if (i == 0) {
547 asm_x64_mov_r64_to_r64(emit->as, REG_ARG_1, REG_LOCAL_1);
548 } else if (i == 1) {
Damien George81057362014-09-07 01:06:19 +0100549 asm_x64_mov_r64_to_r64(emit->as, REG_ARG_2, REG_LOCAL_2);
Damien13ed3a62013-10-08 09:05:10 +0100550 } else if (i == 2) {
Damien George81057362014-09-07 01:06:19 +0100551 asm_x64_mov_r64_to_r64(emit->as, REG_ARG_3, REG_LOCAL_3);
552 } else if (i == 3) {
553 asm_x64_mov_r64_to_local(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +0100554 } else {
555 // TODO not implemented
556 assert(0);
557 }
558 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100559#elif N_X86
560 for (int i = 0; i < scope->num_pos_args; i++) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100561 if (i == 0) {
Damien George03281b32014-09-06 22:38:50 +0000562 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100563 } else if (i == 1) {
Damien George03281b32014-09-06 22:38:50 +0000564 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +0000565 } else if (i == 2) {
566 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +0100567 } else {
Damien George03281b32014-09-06 22:38:50 +0000568 asm_x86_mov_arg_to_r32(emit->as, i, REG_TEMP0);
569 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, i - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +0100570 }
571 }
Damien3ef4abb2013-10-12 16:53:13 +0100572#elif N_THUMB
Damien George2827d622014-04-27 15:50:52 +0100573 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100574 if (i == 0) {
575 asm_thumb_mov_reg_reg(emit->as, REG_LOCAL_1, REG_ARG_1);
576 } else if (i == 1) {
577 asm_thumb_mov_reg_reg(emit->as, REG_LOCAL_2, REG_ARG_2);
578 } else if (i == 2) {
579 asm_thumb_mov_reg_reg(emit->as, REG_LOCAL_3, REG_ARG_3);
580 } else if (i == 3) {
581 asm_thumb_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
582 } else {
583 // TODO not implemented
584 assert(0);
585 }
586 }
587
Damien George0b610de2014-09-29 16:25:04 +0100588 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200589#elif N_ARM
590 for (int i = 0; i < scope->num_pos_args; i++) {
591 if (i == 0) {
592 asm_arm_mov_reg_reg(emit->as, REG_LOCAL_1, REG_ARG_1);
593 } else if (i == 1) {
594 asm_arm_mov_reg_reg(emit->as, REG_LOCAL_2, REG_ARG_2);
595 } else if (i == 2) {
596 asm_arm_mov_reg_reg(emit->as, REG_LOCAL_3, REG_ARG_3);
597 } else if (i == 3) {
598 asm_arm_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
599 } else {
600 // TODO not implemented
601 assert(0);
602 }
603 }
604
Damien George0b610de2014-09-29 16:25:04 +0100605 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
Damien Georgec90f59e2014-09-06 23:06:36 +0100606#else
607 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +0100608#endif
609}
610
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200611STATIC void emit_native_end_pass(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100612 if (!emit->last_emit_was_return_value) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100613 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100614 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100615 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100616
617 // check stack is back to zero size
618 if (emit->stack_size != 0) {
619 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
620 }
621
Damien George36db6bc2014-05-07 17:24:22 +0100622 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100623 void *f = ASM_GET_CODE(emit->as);
624 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100625
626 // compute type signature
627 // TODO check that viper types here convert correctly to valid types for emit glue
628 mp_uint_t type_sig = emit->return_vtype & 3;
629 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
630 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
631 }
632
633 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 +0100634 }
635}
636
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200637STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100638 return emit->last_emit_was_return_value;
639}
640
Damien Georged6230f62014-09-23 14:10:03 +0000641STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
642 DEBUG_printf(" adjust_stack; stack_size=%d, delta=%d\n", emit->stack_size, stack_size_delta);
643 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100644 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100645 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100646 emit->scope->stack_size = emit->stack_size;
647 }
648}
649
Damien Georged6230f62014-09-23 14:10:03 +0000650STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
651 // If we are adjusting the stack in a positive direction (pushing) then we
652 // need to fill in values for the stack kind and vtype of the newly-pushed
653 // entries. These should be set to "value" (ie not reg or imm) because we
654 // should only need to adjust the stack due to a jump to this part in the
655 // code (and hence we have settled the stack before the jump).
656 for (mp_int_t i = 0; i < delta; i++) {
657 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
658 si->kind = STACK_VALUE;
659 si->vtype = VTYPE_PYOBJ; // XXX we don't know the vtype...
660 }
661 adjust_stack(emit, delta);
662}
663
664STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
665}
666
Damienff8ed772013-10-08 22:18:32 +0100667/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200668STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100669 adjust_stack(emit, stack_size_delta);
670 emit->last_emit_was_return_value = false;
671}
Damienff8ed772013-10-08 22:18:32 +0100672*/
Damien13ed3a62013-10-08 09:05:10 +0100673
Damienff8ed772013-10-08 22:18:32 +0100674// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000675STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100676 emit->last_emit_was_return_value = false;
677 // settle the stack
678 /*
679 if (regs_needed != 0) {
680 for (int i = 0; i < emit->stack_size; i++) {
681 switch (emit->stack_info[i].kind) {
682 case STACK_VALUE:
683 break;
684
685 case STACK_REG:
686 // TODO only push reg if in regs_needed
687 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100688 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100689 break;
690
691 case STACK_IMM:
692 // don't think we ever need to push imms for settling
693 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
694 break;
695 }
696 }
697 }
698 */
Damien13ed3a62013-10-08 09:05:10 +0100699}
700
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200701STATIC vtype_kind_t peek_vtype(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100702 return emit->stack_info[emit->stack_size - 1].vtype;
703}
Damien13ed3a62013-10-08 09:05:10 +0100704
Damiend2755ec2013-10-16 23:58:48 +0100705// pos=1 is TOS, pos=2 is next, etc
706// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200707STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100708 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100709 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100710 if (i != skip_stack_pos) {
711 stack_info_t *si = &emit->stack_info[i];
712 if (si->kind == STACK_REG && si->u_reg == reg_needed) {
713 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100714 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100715 }
Damienff8ed772013-10-08 22:18:32 +0100716 }
717 }
718}
Damien13ed3a62013-10-08 09:05:10 +0100719
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200720STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100721 for (int i = 0; i < emit->stack_size; i++) {
722 stack_info_t *si = &emit->stack_info[i];
723 if (si->kind == STACK_REG) {
724 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100725 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100726 }
Damien13ed3a62013-10-08 09:05:10 +0100727 }
728}
729
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200730STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +0000731 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +0100732 for (int i = 0; i < emit->stack_size; i++) {
733 stack_info_t *si = &emit->stack_info[i];
734 if (si->kind == STACK_REG) {
Damien Georged6230f62014-09-23 14:10:03 +0000735 DEBUG_printf(" reg(%u) to local(%u)\n", si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100736 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100737 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100738 }
739 }
740 for (int i = 0; i < emit->stack_size; i++) {
741 stack_info_t *si = &emit->stack_info[i];
742 if (si->kind == STACK_IMM) {
Damien Georged6230f62014-09-23 14:10:03 +0000743 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +0100744 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100745 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +0100746 }
747 }
748}
749
750// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200751STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100752 need_reg_single(emit, reg_dest, pos);
753 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +0100754 *vtype = si->vtype;
755 switch (si->kind) {
756 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +0100757 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100758 break;
759
Damienff8ed772013-10-08 22:18:32 +0100760 case STACK_REG:
761 if (si->u_reg != reg_dest) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100762 ASM_MOV_REG_TO_REG(emit->as, si->u_reg, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100763 }
764 break;
765
Damienff8ed772013-10-08 22:18:32 +0100766 case STACK_IMM:
Damien Georgec90f59e2014-09-06 23:06:36 +0100767 ASM_MOV_IMM_TO_REG(emit->as, si->u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100768 break;
769 }
Damien13ed3a62013-10-08 09:05:10 +0100770}
771
Damien Georgee6ce10a2014-09-06 18:38:20 +0100772STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +0100773 emit->last_emit_was_return_value = false;
774 adjust_stack(emit, -1);
775}
776
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200777STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100778 emit->last_emit_was_return_value = false;
779 emit_access_stack(emit, 1, vtype, reg_dest);
780 adjust_stack(emit, -1);
781}
782
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200783STATIC 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 +0100784 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100785 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +0100786}
787
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200788STATIC 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 +0100789 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100790 emit_pre_pop_reg(emit, vtypeb, regb);
791 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100792}
793
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200794STATIC void emit_post(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100795}
796
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200797STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +0100798 stack_info_t *si = &emit->stack_info[emit->stack_size];
799 si->vtype = vtype;
800 si->kind = STACK_REG;
801 si->u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +0100802 adjust_stack(emit, 1);
803}
804
Damien George40f3c022014-07-03 13:25:24 +0100805STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +0100806 stack_info_t *si = &emit->stack_info[emit->stack_size];
807 si->vtype = vtype;
808 si->kind = STACK_IMM;
809 si->u_imm = imm;
810 adjust_stack(emit, 1);
811}
812
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200813STATIC 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 +0100814 emit_post_push_reg(emit, vtypea, rega);
815 emit_post_push_reg(emit, vtypeb, regb);
816}
817
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200818STATIC 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 +0100819 emit_post_push_reg(emit, vtypea, rega);
820 emit_post_push_reg(emit, vtypeb, regb);
821 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100822}
823
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200824STATIC 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 +0100825 emit_post_push_reg(emit, vtypea, rega);
826 emit_post_push_reg(emit, vtypeb, regb);
827 emit_post_push_reg(emit, vtypec, regc);
828 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +0100829}
830
Damien George7fe21912014-08-16 22:31:57 +0100831STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +0100832 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100833 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100834}
835
Damien George7fe21912014-08-16 22:31:57 +0100836STATIC 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 +0100837 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100838 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
839 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100840}
841
Damien George40f3c022014-07-03 13:25:24 +0100842// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100843STATIC 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 +0100844 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100845 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
846 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +0100847}
848
Damien George7fe21912014-08-16 22:31:57 +0100849STATIC 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 +0000850 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100851 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
852 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
853 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +0000854}
855
Damien George40f3c022014-07-03 13:25:24 +0100856// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100857STATIC 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 +0100858 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100859 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
860 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
861 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
862 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +0100863}
864
Damien George86de21b2014-08-16 22:06:11 +0100865// vtype of all n_pop objects is VTYPE_PYOBJ
866// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
867// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
868// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
869STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
870 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +0100871
Damien George86de21b2014-08-16 22:06:11 +0100872 // First, store any immediate values to their respective place on the stack.
873 for (mp_uint_t i = 0; i < n_pop; i++) {
874 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
875 // must push any imm's to stack
876 // must convert them to VTYPE_PYOBJ for viper code
877 if (si->kind == STACK_IMM) {
878 si->kind = STACK_VALUE;
879 switch (si->vtype) {
880 case VTYPE_PYOBJ:
Damien Georgec90f59e2014-09-06 23:06:36 +0100881 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 +0100882 break;
883 case VTYPE_BOOL:
884 if (si->u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100885 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 +0100886 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +0100887 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 +0100888 }
889 si->vtype = VTYPE_PYOBJ;
890 break;
891 case VTYPE_INT:
892 case VTYPE_UINT:
Damien Georgec90f59e2014-09-06 23:06:36 +0100893 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 +0100894 si->vtype = VTYPE_PYOBJ;
895 break;
896 default:
897 // not handled
898 assert(0);
899 }
900 }
901
902 // verify that this value is on the stack
903 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +0100904 }
Damien George86de21b2014-08-16 22:06:11 +0100905
906 // Second, convert any non-VTYPE_PYOBJ to that type.
907 for (mp_uint_t i = 0; i < n_pop; i++) {
908 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
909 if (si->vtype != VTYPE_PYOBJ) {
910 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +0100911 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +0100912 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 +0100913 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +0100914 si->vtype = VTYPE_PYOBJ;
915 }
916 }
917
918 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
919 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +0100920 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +0100921}
922
923// vtype of all n_push objects is VTYPE_PYOBJ
924STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
925 need_reg_all(emit);
926 for (mp_uint_t i = 0; i < n_push; i++) {
927 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
928 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
929 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100930 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +0100931 adjust_stack(emit, n_push);
932}
933
Damien George7ff996c2014-09-08 23:05:16 +0100934STATIC void emit_native_load_id(emit_t *emit, qstr qst) {
935 emit_common_load_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +0100936}
937
Damien George7ff996c2014-09-08 23:05:16 +0100938STATIC void emit_native_store_id(emit_t *emit, qstr qst) {
939 emit_common_store_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +0100940}
941
Damien George7ff996c2014-09-08 23:05:16 +0100942STATIC void emit_native_delete_id(emit_t *emit, qstr qst) {
943 emit_common_delete_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +0100944}
945
Damien George7ff996c2014-09-08 23:05:16 +0100946STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +0000947 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +0000948 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +0100949 // need to commit stack because we can jump here from elsewhere
950 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100951 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +0000952 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +0100953}
954
Damien Georgecdd96df2014-04-06 12:58:40 +0100955STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
956 DEBUG_printf("import_name %s\n", qstr_str(qst));
957 vtype_kind_t vtype_fromlist;
958 vtype_kind_t vtype_level;
959 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
960 assert(vtype_fromlist == VTYPE_PYOBJ);
961 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +0100962 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +0100963 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +0100964}
965
Damien Georgecdd96df2014-04-06 12:58:40 +0100966STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
967 DEBUG_printf("import_from %s\n", qstr_str(qst));
968 emit_native_pre(emit);
969 vtype_kind_t vtype_module;
970 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
971 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +0100972 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +0100973 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +0100974}
975
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200976STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +0100977 DEBUG_printf("import_star\n");
978 vtype_kind_t vtype_module;
979 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
980 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +0100981 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +0100982 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +0100983}
984
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200985STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +0000986 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +0000987 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +0100988 int vtype;
Damien George40f3c022014-07-03 13:25:24 +0100989 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +0100990 if (emit->do_viper_types) {
991 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +0000992 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
993 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
994 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien13ed3a62013-10-08 09:05:10 +0100995 default: assert(0); vtype = 0; val = 0; // shouldn't happen
996 }
997 } else {
998 vtype = VTYPE_PYOBJ;
999 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001000 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1001 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1002 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien13ed3a62013-10-08 09:05:10 +01001003 default: assert(0); vtype = 0; val = 0; // shouldn't happen
1004 }
1005 }
1006 emit_post_push_imm(emit, vtype, val);
1007}
1008
Damien George40f3c022014-07-03 13:25:24 +01001009STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001010 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001011 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001012 if (emit->do_viper_types) {
1013 emit_post_push_imm(emit, VTYPE_INT, arg);
1014 } else {
1015 emit_post_push_imm(emit, VTYPE_PYOBJ, (arg << 1) | 1);
1016 }
1017}
1018
Damien Georgecdd96df2014-04-06 12:58:40 +01001019STATIC void emit_native_load_const_int(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001020 DEBUG_printf("load_const_int %s\n", qstr_str(qst));
Damien Georgecdd96df2014-04-06 12:58:40 +01001021 // for viper: load integer, check fits in 32 bits
1022 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001023 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_INT, qst, REG_ARG_1);
Damien Georgecdd96df2014-04-06 12:58:40 +01001024 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001025}
1026
Damien George7ff996c2014-09-08 23:05:16 +01001027STATIC void emit_native_load_const_dec(emit_t *emit, qstr qst) {
Damien6ba13142013-11-02 20:34:54 +00001028 // for viper, a float/complex is just a Python object
Damien Georgece8f07a2014-03-27 23:30:26 +00001029 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001030 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_DEC, qst, REG_ARG_1);
Damien6ba13142013-11-02 20:34:54 +00001031 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001032}
1033
Damien George7ff996c2014-09-08 23:05:16 +01001034STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001035 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001036 // TODO: Eventually we want to be able to work with raw pointers in viper to
1037 // do native array access. For now we just load them as any other object.
1038 /*
Damien13ed3a62013-10-08 09:05:10 +01001039 if (emit->do_viper_types) {
1040 // not implemented properly
1041 // load a pointer to the asciiz string?
1042 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001043 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001044 } else
1045 */
1046 {
Damien Georgeb601d952014-06-30 05:17:25 +01001047 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +01001048 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_BYTES, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001049 } else {
Damien George7ff996c2014-09-08 23:05:16 +01001050 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001051 }
Damien13ed3a62013-10-08 09:05:10 +01001052 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1053 }
1054}
1055
Damien George3558f622014-04-20 17:50:40 +01001056STATIC void emit_native_load_null(emit_t *emit) {
1057 emit_native_pre(emit);
1058 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1059}
1060
Damien George7ff996c2014-09-08 23:05:16 +01001061STATIC 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 +01001062 vtype_kind_t vtype = emit->local_vtype[local_num];
1063 if (vtype == VTYPE_UNBOUND) {
Damien George7ff996c2014-09-08 23:05:16 +01001064 printf("ViperTypeError: local %s used before type known\n", qstr_str(qst));
Damien13ed3a62013-10-08 09:05:10 +01001065 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001066 emit_native_pre(emit);
Damien3ef4abb2013-10-12 16:53:13 +01001067#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001068 if (local_num == 0) {
1069 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001070 } else if (local_num == 1) {
1071 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1072 } else if (local_num == 2) {
1073 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001074 } else {
Damien George0b610de2014-09-29 16:25:04 +01001075 need_reg_single(emit, REG_TEMP0, 0);
1076 asm_x64_mov_local_to_r64(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1077 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001078 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001079#elif N_X86
1080 if (local_num == 0) {
1081 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001082 } else if (local_num == 1) {
1083 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001084 } else if (local_num == 2) {
1085 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001086 } else {
Damien George0b610de2014-09-29 16:25:04 +01001087 need_reg_single(emit, REG_TEMP0, 0);
1088 asm_x86_mov_local_to_r32(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1089 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien Georgec90f59e2014-09-06 23:06:36 +01001090 }
Damien3ef4abb2013-10-12 16:53:13 +01001091#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001092 if (local_num == 0) {
1093 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1094 } else if (local_num == 1) {
1095 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1096 } else if (local_num == 2) {
1097 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1098 } else {
Damien George0b610de2014-09-29 16:25:04 +01001099 need_reg_single(emit, REG_TEMP0, 0);
1100 asm_thumb_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1101 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001102 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001103#elif N_ARM
1104 if (local_num == 0) {
1105 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1106 } else if (local_num == 1) {
1107 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1108 } else if (local_num == 2) {
1109 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1110 } else {
Damien George0b610de2014-09-29 16:25:04 +01001111 need_reg_single(emit, REG_TEMP0, 0);
1112 asm_arm_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1113 emit_post_push_reg(emit, vtype, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001114 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001115#else
1116 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001117#endif
1118}
1119
Damien George7ff996c2014-09-08 23:05:16 +01001120STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001121 // not implemented
1122 // in principle could support this quite easily (ldr r0, [r0, #0]) and then get closed over variables!
1123 assert(0);
1124}
1125
Damien George7ff996c2014-09-08 23:05:16 +01001126STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001127 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001128 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001129 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001130 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1131}
1132
Damien George7ff996c2014-09-08 23:05:16 +01001133STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001134 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001135 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001136 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1137}
1138
Damien George7ff996c2014-09-08 23:05:16 +01001139STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001140 // depends on type of subject:
1141 // - integer, function, pointer to integers: error
1142 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001143 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001144 vtype_kind_t vtype_base;
1145 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1146 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001147 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001148 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1149}
1150
Damien George7ff996c2014-09-08 23:05:16 +01001151STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001152 vtype_kind_t vtype_base;
1153 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1154 assert(vtype_base == VTYPE_PYOBJ);
1155 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001156 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001157}
1158
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001159STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001160 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001161 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001162 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001163}
1164
Damien George729f7b42014-04-17 22:10:53 +01001165STATIC void emit_native_load_subscr(emit_t *emit) {
1166 vtype_kind_t vtype_lhs, vtype_rhs;
1167 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_2, &vtype_lhs, REG_ARG_1);
1168 if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George7fe21912014-08-16 22:31:57 +01001169 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 +01001170 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1171 } else {
1172 printf("ViperTypeError: can't do subscr of types %d and %d\n", vtype_lhs, vtype_rhs);
Damien George729f7b42014-04-17 22:10:53 +01001173 }
1174}
1175
Damien George7ff996c2014-09-08 23:05:16 +01001176STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001177 vtype_kind_t vtype;
Damien3ef4abb2013-10-12 16:53:13 +01001178#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001179 if (local_num == 0) {
1180 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001181 } else if (local_num == 1) {
1182 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1183 } else if (local_num == 2) {
1184 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001185 } else {
Damien George0b610de2014-09-29 16:25:04 +01001186 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1187 asm_x64_mov_r64_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +01001188 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001189#elif N_X86
1190 if (local_num == 0) {
1191 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001192 } else if (local_num == 1) {
1193 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001194 } else if (local_num == 2) {
1195 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001196 } else {
Damien George0b610de2014-09-29 16:25:04 +01001197 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1198 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +01001199 }
Damien3ef4abb2013-10-12 16:53:13 +01001200#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001201 if (local_num == 0) {
1202 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1203 } else if (local_num == 1) {
1204 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1205 } else if (local_num == 2) {
1206 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1207 } else {
Damien George0b610de2014-09-29 16:25:04 +01001208 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1209 asm_thumb_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001210 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001211#elif N_ARM
1212 if (local_num == 0) {
1213 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1214 } else if (local_num == 1) {
1215 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1216 } else if (local_num == 2) {
1217 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1218 } else {
Damien George0b610de2014-09-29 16:25:04 +01001219 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1220 asm_arm_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001221 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001222#else
1223 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001224#endif
1225
1226 emit_post(emit);
1227
1228 // check types
1229 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1230 // first time this local is assigned, so give it a type of the object stored in it
1231 emit->local_vtype[local_num] = vtype;
1232 } else if (emit->local_vtype[local_num] != vtype) {
1233 // type of local is not the same as object stored in it
Damien George7ff996c2014-09-08 23:05:16 +01001234 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 +01001235 }
1236}
1237
Damien George7ff996c2014-09-08 23:05:16 +01001238STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001239 // not implemented
1240 assert(0);
1241}
1242
Damien George7ff996c2014-09-08 23:05:16 +01001243STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001244 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001245 vtype_kind_t vtype;
1246 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1247 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001248 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001249 emit_post(emit);
1250}
1251
Damien George7ff996c2014-09-08 23:05:16 +01001252STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien Georgee6c0dff2014-08-15 23:47:59 +01001253 vtype_kind_t vtype = peek_vtype(emit);
1254 if (vtype == VTYPE_PYOBJ) {
1255 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1256 } else {
1257 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001258 emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, vtype, REG_ARG_2); // arg2 = type
Damien Georgec90f59e2014-09-06 23:06:36 +01001259 ASM_MOV_REG_TO_REG(emit->as, REG_RET, REG_ARG_2);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001260 }
Damien George7ff996c2014-09-08 23:05:16 +01001261 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001262 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001263}
1264
Damien George7ff996c2014-09-08 23:05:16 +01001265STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001266 vtype_kind_t vtype_base, vtype_val;
1267 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1268 assert(vtype_base == VTYPE_PYOBJ);
1269 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001270 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001271 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001272}
1273
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001274STATIC void emit_native_store_subscr(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001275 // depends on type of subject:
1276 // - integer, function, pointer to structure: error
1277 // - pointer to integers: store as per array
1278 // - Python object: call runtime with converted object or type info
1279 vtype_kind_t vtype_index, vtype_base, vtype_value;
1280 emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3); // index, base, value to store
1281 assert(vtype_index == VTYPE_PYOBJ);
1282 assert(vtype_base == VTYPE_PYOBJ);
1283 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001284 emit_call(emit, MP_F_OBJ_SUBSCR);
Damien13ed3a62013-10-08 09:05:10 +01001285}
1286
Damien George7ff996c2014-09-08 23:05:16 +01001287STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001288 // TODO implement me!
Damien13ed3a62013-10-08 09:05:10 +01001289 // could support for Python types, just set to None (so GC can reclaim it)
Damien13ed3a62013-10-08 09:05:10 +01001290}
1291
Damien George7ff996c2014-09-08 23:05:16 +01001292STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001293 // TODO implement me!
Damien9ecbcff2013-12-11 00:41:43 +00001294}
1295
Damien Georgee6ce10a2014-09-06 18:38:20 +01001296STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1297 emit_native_pre(emit);
1298 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1299 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001300}
1301
Damien Georgee6ce10a2014-09-06 18:38:20 +01001302STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1303 emit_native_pre(emit);
1304 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1305 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001306}
1307
Damien Georgee6ce10a2014-09-06 18:38:20 +01001308STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001309 vtype_kind_t vtype_base;
1310 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1311 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001312 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 +01001313 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001314}
1315
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001316STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001317 vtype_kind_t vtype_index, vtype_base;
1318 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1319 assert(vtype_index == VTYPE_PYOBJ);
1320 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001321 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 +01001322}
1323
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001324STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001325 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001326 vtype_kind_t vtype;
1327 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1328 emit_post_push_reg_reg(emit, vtype, REG_TEMP0, vtype, REG_TEMP0);
1329}
1330
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001331STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001332 vtype_kind_t vtype0, vtype1;
1333 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1334 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1335}
1336
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001337STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001338 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001339 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001340 emit_post(emit);
1341}
1342
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001343STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001344 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001345 vtype_kind_t vtype0, vtype1;
1346 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1347 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001348}
1349
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001350STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001351 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001352 vtype_kind_t vtype0, vtype1, vtype2;
1353 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1354 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1355}
1356
Damien George7ff996c2014-09-08 23:05:16 +01001357STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001358 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001359 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001360 // need to commit stack because we are jumping elsewhere
1361 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001362 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001363 emit_post(emit);
1364}
1365
Damien George7ff996c2014-09-08 23:05:16 +01001366STATIC void emit_native_jump_helper(emit_t *emit, mp_uint_t label, bool pop) {
Damien13ed3a62013-10-08 09:05:10 +01001367 vtype_kind_t vtype = peek_vtype(emit);
1368 if (vtype == VTYPE_BOOL) {
1369 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien Georgea32c1e42014-05-07 18:30:52 +01001370 if (!pop) {
1371 adjust_stack(emit, 1);
1372 }
Damien13ed3a62013-10-08 09:05:10 +01001373 } else if (vtype == VTYPE_PYOBJ) {
1374 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien Georgea32c1e42014-05-07 18:30:52 +01001375 if (!pop) {
Damien George02d95d72014-08-29 20:05:32 +01001376 adjust_stack(emit, 1);
Damien Georgea32c1e42014-05-07 18:30:52 +01001377 }
Damien George02d95d72014-08-29 20:05:32 +01001378 emit_call(emit, MP_F_OBJ_IS_TRUE);
Damien13ed3a62013-10-08 09:05:10 +01001379 } else {
1380 printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
1381 assert(0);
1382 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001383 // need to commit stack because we may jump elsewhere
1384 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001385}
1386
Damien George7ff996c2014-09-08 23:05:16 +01001387STATIC void emit_native_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001388 DEBUG_printf("pop_jump_if_true(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001389 emit_native_jump_helper(emit, label, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001390 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien1a6633a2013-11-03 13:58:19 +00001391 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001392}
Damien1a6633a2013-11-03 13:58:19 +00001393
Damien George7ff996c2014-09-08 23:05:16 +01001394STATIC void emit_native_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001395 DEBUG_printf("pop_jump_if_false(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001396 emit_native_jump_helper(emit, label, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001397 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001398 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001399}
Damien Georgea32c1e42014-05-07 18:30:52 +01001400
Damien George7ff996c2014-09-08 23:05:16 +01001401STATIC void emit_native_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001402 DEBUG_printf("jump_if_true_or_pop(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001403 emit_native_jump_helper(emit, label, false);
Damien Georgec90f59e2014-09-06 23:06:36 +01001404 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001405 adjust_stack(emit, -1);
1406 emit_post(emit);
1407}
1408
Damien George7ff996c2014-09-08 23:05:16 +01001409STATIC void emit_native_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001410 DEBUG_printf("jump_if_false_or_pop(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001411 emit_native_jump_helper(emit, label, false);
Damien Georgec90f59e2014-09-06 23:06:36 +01001412 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001413 adjust_stack(emit, -1);
1414 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001415}
1416
Damien George7ff996c2014-09-08 23:05:16 +01001417STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien George25c84642014-05-30 15:20:41 +01001418 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001419}
Damien Georgea32c1e42014-05-07 18:30:52 +01001420
Damien George7ff996c2014-09-08 23:05:16 +01001421STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgea32c1e42014-05-07 18:30:52 +01001422 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001423}
Damien Georgea32c1e42014-05-07 18:30:52 +01001424
Damien George7ff996c2014-09-08 23:05:16 +01001425STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001426 // not supported, or could be with runtime call
1427 assert(0);
1428}
Damien Georgeb601d952014-06-30 05:17:25 +01001429
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001430STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001431 assert(0);
1432}
Damien Georgeb601d952014-06-30 05:17:25 +01001433
Damien George7ff996c2014-09-08 23:05:16 +01001434STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001435 emit_native_pre(emit);
1436 // need to commit stack because we may jump elsewhere
1437 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001438 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 +01001439 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001440 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001441 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001442}
Damien Georgeb601d952014-06-30 05:17:25 +01001443
Damien George7ff996c2014-09-08 23:05:16 +01001444STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001445 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001446}
Damien Georgeb601d952014-06-30 05:17:25 +01001447
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001448STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001449 emit_pre_pop_discard(emit);
1450 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001451}
Damiend2755ec2013-10-16 23:58:48 +01001452
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001453STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001454 // perhaps the difficult one, as we want to rewrite for loops using native code
1455 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001456
1457 vtype_kind_t vtype;
1458 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1459 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001460 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01001461 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001462}
Damiend2755ec2013-10-16 23:58:48 +01001463
Damien George7ff996c2014-09-08 23:05:16 +01001464STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001465 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001466 vtype_kind_t vtype;
1467 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1468 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001469 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01001470 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
1471 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01001472 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1473}
1474
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001475STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001476 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001477 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001478 adjust_stack(emit, -1);
1479 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001480}
1481
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001482STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001483 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001484 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001485 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001486 emit_post(emit);
1487}
1488
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001489STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeb601d952014-06-30 05:17:25 +01001490 /*
1491 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001492 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001493 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01001494 emit_post(emit);
1495 */
Damien13ed3a62013-10-08 09:05:10 +01001496}
1497
Damien Georged17926d2014-03-30 13:35:08 +01001498STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001499 vtype_kind_t vtype;
1500 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1501 assert(vtype == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001502 if (op == MP_UNARY_OP_NOT) {
Damien Georged6230f62014-09-23 14:10:03 +00001503 // we need to synthesise this operation by converting to bool first
1504 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
1505 ASM_MOV_REG_TO_REG(emit->as, REG_RET, REG_ARG_2);
Damien Georgeb601d952014-06-30 05:17:25 +01001506 }
Damien Georged6230f62014-09-23 14:10:03 +00001507 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1508 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001509}
1510
Damien Georged17926d2014-03-30 13:35:08 +01001511STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001512 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien13ed3a62013-10-08 09:05:10 +01001513 vtype_kind_t vtype_lhs, vtype_rhs;
1514 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
1515 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien Georged17926d2014-03-30 13:35:08 +01001516 if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
Damien3ef4abb2013-10-12 16:53:13 +01001517#if N_X64
Damien Georgebc1d3692014-01-11 09:47:06 +00001518 asm_x64_add_r64_to_r64(emit->as, REG_ARG_3, REG_ARG_2);
Damien Georgec90f59e2014-09-06 23:06:36 +01001519#elif N_X86
1520 asm_x86_add_r32_to_r32(emit->as, REG_ARG_3, REG_ARG_2);
Damien3ef4abb2013-10-12 16:53:13 +01001521#elif N_THUMB
Damien Georgea26dc502014-04-12 17:54:52 +01001522 asm_thumb_add_rlo_rlo_rlo(emit->as, REG_ARG_2, REG_ARG_2, REG_ARG_3);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001523#elif N_ARM
1524 asm_arm_add_reg(emit->as, REG_ARG_2, REG_ARG_2, REG_ARG_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001525#else
1526 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001527#endif
Damien Georgebc1d3692014-01-11 09:47:06 +00001528 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien Georged17926d2014-03-30 13:35:08 +01001529 } else if (op == MP_BINARY_OP_LESS) {
Damien Georgebc1d3692014-01-11 09:47:06 +00001530#if N_X64
1531 asm_x64_xor_r64_to_r64(emit->as, REG_RET, REG_RET);
1532 asm_x64_cmp_r64_with_r64(emit->as, REG_ARG_3, REG_ARG_2);
Damien Georgec90f59e2014-09-06 23:06:36 +01001533 asm_x64_setcc_r8(emit->as, ASM_X64_CC_JL, REG_RET);
1534#elif N_X86
1535 asm_x86_xor_r32_to_r32(emit->as, REG_RET, REG_RET);
1536 asm_x86_cmp_r32_with_r32(emit->as, REG_ARG_3, REG_ARG_2);
1537 asm_x86_setcc_r8(emit->as, ASM_X86_CC_JL, REG_RET);
Damien Georgebc1d3692014-01-11 09:47:06 +00001538#elif N_THUMB
Damien George87210872014-04-13 00:30:32 +01001539 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, REG_ARG_3);
Damien George851f15f2014-09-29 10:05:32 +01001540 asm_thumb_op16(emit->as, ASM_THUMB_OP_ITE_GE);
Damien George87210872014-04-13 00:30:32 +01001541 asm_thumb_mov_rlo_i8(emit->as, REG_RET, 0); // if r0 >= r1
1542 asm_thumb_mov_rlo_i8(emit->as, REG_RET, 1); // if r0 < r1
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001543#elif N_ARM
Damien Georged4a799f2014-09-15 16:39:24 +01001544 asm_arm_less_op(emit->as, REG_RET, REG_ARG_2, REG_ARG_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001545#else
1546 #error not implemented
Damien Georgebc1d3692014-01-11 09:47:06 +00001547#endif
1548 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
1549 } else {
1550 // TODO other ops not yet implemented
1551 assert(0);
1552 }
Damien13ed3a62013-10-08 09:05:10 +01001553 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien Georged6230f62014-09-23 14:10:03 +00001554 bool invert = false;
1555 if (op == MP_BINARY_OP_NOT_IN) {
1556 invert = true;
1557 op = MP_BINARY_OP_IN;
1558 } else if (op == MP_BINARY_OP_IS_NOT) {
1559 invert = true;
1560 op = MP_BINARY_OP_IS;
1561 }
Damien George7fe21912014-08-16 22:31:57 +01001562 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00001563 if (invert) {
1564 ASM_MOV_REG_TO_REG(emit->as, REG_RET, REG_ARG_2);
1565 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
1566 }
Damien13ed3a62013-10-08 09:05:10 +01001567 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1568 } else {
1569 printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs);
Damien Georgea5190a72014-08-15 22:39:08 +01001570 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001571 }
1572}
1573
Damien George7ff996c2014-09-08 23:05:16 +01001574STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01001575 // for viper: call runtime, with types of args
1576 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00001577 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001578 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001579 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01001580 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01001581}
1582
Damien George7ff996c2014-09-08 23:05:16 +01001583STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001584 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001585 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001586 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001587 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
1588}
1589
Damien George7ff996c2014-09-08 23:05:16 +01001590STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01001591 // only used in list comprehension
1592 vtype_kind_t vtype_list, vtype_item;
1593 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
1594 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
1595 assert(vtype_list == VTYPE_PYOBJ);
1596 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001597 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01001598 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001599}
1600
Damien George7ff996c2014-09-08 23:05:16 +01001601STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001602 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001603 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001604 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
1605}
1606
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001607STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001608 vtype_kind_t vtype_key, vtype_value, vtype_map;
1609 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
1610 assert(vtype_key == VTYPE_PYOBJ);
1611 assert(vtype_value == VTYPE_PYOBJ);
1612 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001613 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01001614 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
1615}
1616
Damien George7ff996c2014-09-08 23:05:16 +01001617STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01001618 // only used in list comprehension
1619 vtype_kind_t vtype_map, vtype_key, vtype_value;
1620 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
1621 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
1622 assert(vtype_map == VTYPE_PYOBJ);
1623 assert(vtype_key == VTYPE_PYOBJ);
1624 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001625 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01001626 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001627}
1628
Damien George7ff996c2014-09-08 23:05:16 +01001629STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001630 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001631 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001632 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001633 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
1634}
1635
Damien George7ff996c2014-09-08 23:05:16 +01001636STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01001637 // only used in set comprehension
1638 vtype_kind_t vtype_set, vtype_item;
1639 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
1640 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
1641 assert(vtype_set == VTYPE_PYOBJ);
1642 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001643 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01001644 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001645}
Damiend2755ec2013-10-16 23:58:48 +01001646
Damien George7ff996c2014-09-08 23:05:16 +01001647STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001648 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01001649 if (n_args == 2) {
1650 vtype_kind_t vtype_start, vtype_stop;
1651 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
1652 assert(vtype_start == VTYPE_PYOBJ);
1653 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001654 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 +01001655 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1656 } else {
1657 assert(n_args == 3);
1658 vtype_kind_t vtype_start, vtype_stop, vtype_step;
1659 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
1660 assert(vtype_start == VTYPE_PYOBJ);
1661 assert(vtype_stop == VTYPE_PYOBJ);
1662 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001663 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01001664 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1665 }
Damien13ed3a62013-10-08 09:05:10 +01001666}
Damien Georgecdd96df2014-04-06 12:58:40 +01001667
Damien George7ff996c2014-09-08 23:05:16 +01001668STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001669 DEBUG_printf("unpack_sequence %d\n", n_args);
1670 vtype_kind_t vtype_base;
1671 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
1672 assert(vtype_base == VTYPE_PYOBJ);
1673 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01001674 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01001675}
Damien Georgecdd96df2014-04-06 12:58:40 +01001676
Damien George7ff996c2014-09-08 23:05:16 +01001677STATIC 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 +01001678 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
1679 vtype_kind_t vtype_base;
1680 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
1681 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001682 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 +01001683 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 +01001684}
1685
Damien George7ff996c2014-09-08 23:05:16 +01001686STATIC 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 +01001687 // 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 +00001688 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001689 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01001690 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 +01001691 } else {
1692 vtype_kind_t vtype_def_tuple, vtype_def_dict;
1693 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
1694 assert(vtype_def_tuple == VTYPE_PYOBJ);
1695 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001696 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 +01001697 }
Damien13ed3a62013-10-08 09:05:10 +01001698 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1699}
1700
Damien George7ff996c2014-09-08 23:05:16 +01001701STATIC 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 +01001702 assert(0);
1703}
1704
Damien George7ff996c2014-09-08 23:05:16 +01001705STATIC 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 +00001706 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
1707
Damien13ed3a62013-10-08 09:05:10 +01001708 // call special viper runtime routine with type info for args, and wanted type info for return
Damien George922ddd62014-04-09 12:43:17 +01001709 assert(!star_flags);
Damien Georgecd82e022014-02-02 13:11:48 +00001710
1711 /* we no longer have these _n specific call_function's
1712 * they anyway push args into an array
1713 * and they would take too much room in the native dispatch table
Damien13ed3a62013-10-08 09:05:10 +01001714 if (n_positional == 0) {
1715 vtype_kind_t vtype_fun;
1716 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
1717 assert(vtype_fun == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001718 emit_call(emit, MP_F_CALL_FUNCTION_0);
Damien13ed3a62013-10-08 09:05:10 +01001719 } else if (n_positional == 1) {
1720 vtype_kind_t vtype_fun, vtype_arg1;
1721 emit_pre_pop_reg_reg(emit, &vtype_arg1, REG_ARG_2, &vtype_fun, REG_ARG_1); // the single argument, the function
1722 assert(vtype_fun == VTYPE_PYOBJ);
1723 assert(vtype_arg1 == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001724 emit_call(emit, MP_F_CALL_FUNCTION_1);
Damien13ed3a62013-10-08 09:05:10 +01001725 } else if (n_positional == 2) {
1726 vtype_kind_t vtype_fun, vtype_arg1, vtype_arg2;
1727 emit_pre_pop_reg_reg_reg(emit, &vtype_arg2, REG_ARG_3, &vtype_arg1, REG_ARG_2, &vtype_fun, REG_ARG_1); // the second argument, the first argument, the function
1728 assert(vtype_fun == VTYPE_PYOBJ);
1729 assert(vtype_arg1 == VTYPE_PYOBJ);
1730 assert(vtype_arg2 == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001731 emit_call(emit, MP_F_CALL_FUNCTION_2);
Damien13ed3a62013-10-08 09:05:10 +01001732 } else {
Damieneb19efb2013-10-10 22:06:54 +01001733 */
Damien Georgecd82e022014-02-02 13:11:48 +00001734
Damien Georgece8f07a2014-03-27 23:30:26 +00001735 emit_native_pre(emit);
Damien Georgecdd96df2014-04-06 12:58:40 +01001736 if (n_positional != 0 || n_keyword != 0) {
1737 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
Damien Georgecd82e022014-02-02 13:11:48 +00001738 }
1739 vtype_kind_t vtype_fun;
1740 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
1741 assert(vtype_fun == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001742 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +01001743 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1744}
1745
Damien George7ff996c2014-09-08 23:05:16 +01001746STATIC 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 +01001747 assert(!star_flags);
Damien Georgecd82e022014-02-02 13:11:48 +00001748
Damieneb19efb2013-10-10 22:06:54 +01001749 /*
Damien13ed3a62013-10-08 09:05:10 +01001750 if (n_positional == 0) {
1751 vtype_kind_t vtype_meth, vtype_self;
1752 emit_pre_pop_reg_reg(emit, &vtype_self, REG_ARG_2, &vtype_meth, REG_ARG_1); // the self object (or NULL), the method
1753 assert(vtype_meth == VTYPE_PYOBJ);
1754 assert(vtype_self == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001755 emit_call(emit, MP_F_CALL_METHOD_1);
Damien13ed3a62013-10-08 09:05:10 +01001756 } else if (n_positional == 1) {
1757 vtype_kind_t vtype_meth, vtype_self, vtype_arg1;
1758 emit_pre_pop_reg_reg_reg(emit, &vtype_arg1, REG_ARG_3, &vtype_self, REG_ARG_2, &vtype_meth, REG_ARG_1); // the first argument, the self object (or NULL), the method
1759 assert(vtype_meth == VTYPE_PYOBJ);
1760 assert(vtype_self == VTYPE_PYOBJ);
1761 assert(vtype_arg1 == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001762 emit_call(emit, MP_F_CALL_METHOD_2);
Damien13ed3a62013-10-08 09:05:10 +01001763 } else {
Damieneb19efb2013-10-10 22:06:54 +01001764 */
Damien Georgecd82e022014-02-02 13:11:48 +00001765
Damien Georgece8f07a2014-03-27 23:30:26 +00001766 emit_native_pre(emit);
Damien Georgecdd96df2014-04-06 12:58:40 +01001767 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 +01001768 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 +01001769 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1770}
1771
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001772STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001773 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01001774 vtype_kind_t vtype;
1775 emit_pre_pop_reg(emit, &vtype, REG_RET);
1776 if (emit->do_viper_types) {
Damien Georgee6c0dff2014-08-15 23:47:59 +01001777 if (vtype == VTYPE_PTR_NONE) {
1778 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01001779 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001780 }
1781 } else if (vtype != emit->return_vtype) {
Damien George2ac4af62014-08-15 16:45:41 +01001782 printf("ViperTypeError: incompatible return type\n");
1783 }
Damien13ed3a62013-10-08 09:05:10 +01001784 } else {
1785 assert(vtype == VTYPE_PYOBJ);
1786 }
1787 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01001788 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
1789 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01001790}
1791
Damien George7ff996c2014-09-08 23:05:16 +01001792STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01001793 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01001794 vtype_kind_t vtype_exc;
1795 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
1796 if (vtype_exc != VTYPE_PYOBJ) {
1797 printf("ViperTypeError: must raise an object\n");
1798 }
1799 // 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 +01001800 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01001801}
Damien Georgeb601d952014-06-30 05:17:25 +01001802
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001803STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001804 // not supported (for now)
1805 assert(0);
1806}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001807STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001808 // not supported (for now)
1809 assert(0);
1810}
1811
Damien Georgeb601d952014-06-30 05:17:25 +01001812STATIC void emit_native_start_except_handler(emit_t *emit) {
1813 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
1814 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
1815 // the first 2 elements, so we can get the thrown value.
1816 adjust_stack(emit, 2);
1817 vtype_kind_t vtype_nlr;
1818 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01001819 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01001820 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
1821}
1822
1823STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001824 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01001825}
1826
Damien13ed3a62013-10-08 09:05:10 +01001827const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01001828 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01001829 emit_native_start_pass,
1830 emit_native_end_pass,
1831 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00001832 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00001833 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01001834
1835 emit_native_load_id,
1836 emit_native_store_id,
1837 emit_native_delete_id,
1838
1839 emit_native_label_assign,
1840 emit_native_import_name,
1841 emit_native_import_from,
1842 emit_native_import_star,
1843 emit_native_load_const_tok,
1844 emit_native_load_const_small_int,
1845 emit_native_load_const_int,
1846 emit_native_load_const_dec,
Damien13ed3a62013-10-08 09:05:10 +01001847 emit_native_load_const_str,
Damien George3558f622014-04-20 17:50:40 +01001848 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01001849 emit_native_load_fast,
Damien13ed3a62013-10-08 09:05:10 +01001850 emit_native_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +00001851 emit_native_load_name,
1852 emit_native_load_global,
Damien13ed3a62013-10-08 09:05:10 +01001853 emit_native_load_attr,
1854 emit_native_load_method,
1855 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01001856 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01001857 emit_native_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +00001858 emit_native_store_deref,
Damien13ed3a62013-10-08 09:05:10 +01001859 emit_native_store_name,
1860 emit_native_store_global,
Damien13ed3a62013-10-08 09:05:10 +01001861 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01001862 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01001863 emit_native_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +00001864 emit_native_delete_deref,
Damien13ed3a62013-10-08 09:05:10 +01001865 emit_native_delete_name,
1866 emit_native_delete_global,
Damien13ed3a62013-10-08 09:05:10 +01001867 emit_native_delete_attr,
1868 emit_native_delete_subscr,
1869 emit_native_dup_top,
1870 emit_native_dup_top_two,
1871 emit_native_pop_top,
1872 emit_native_rot_two,
1873 emit_native_rot_three,
1874 emit_native_jump,
1875 emit_native_pop_jump_if_true,
1876 emit_native_pop_jump_if_false,
1877 emit_native_jump_if_true_or_pop,
1878 emit_native_jump_if_false_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01001879 emit_native_break_loop,
1880 emit_native_continue_loop,
1881 emit_native_setup_with,
1882 emit_native_with_cleanup,
1883 emit_native_setup_except,
1884 emit_native_setup_finally,
1885 emit_native_end_finally,
1886 emit_native_get_iter,
1887 emit_native_for_iter,
1888 emit_native_for_iter_end,
1889 emit_native_pop_block,
1890 emit_native_pop_except,
1891 emit_native_unary_op,
1892 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01001893 emit_native_build_tuple,
1894 emit_native_build_list,
1895 emit_native_list_append,
1896 emit_native_build_map,
1897 emit_native_store_map,
1898 emit_native_map_add,
1899 emit_native_build_set,
1900 emit_native_set_add,
1901 emit_native_build_slice,
1902 emit_native_unpack_sequence,
1903 emit_native_unpack_ex,
1904 emit_native_make_function,
1905 emit_native_make_closure,
1906 emit_native_call_function,
1907 emit_native_call_method,
1908 emit_native_return_value,
1909 emit_native_raise_varargs,
1910 emit_native_yield_value,
1911 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01001912
1913 emit_native_start_except_handler,
1914 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01001915};
1916
Damien Georgec90f59e2014-09-06 23:06:36 +01001917#endif