blob: ff57ef1a36f25377b2197b90acfca1b8d3025d8f [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
Damien13ed3a62013-10-08 09:05:10 +010045#include <stdio.h>
46#include <string.h>
47#include <assert.h>
48
Damien George51dfcb42015-01-01 20:27:54 +000049#include "py/nlr.h"
50#include "py/emit.h"
Damien George99886182015-04-06 22:38:53 +010051#include "py/bc.h"
Damien13ed3a62013-10-08 09:05:10 +010052
Damien Georgecdd96df2014-04-06 12:58:40 +010053#if 0 // print debugging info
54#define DEBUG_PRINT (1)
55#define DEBUG_printf DEBUG_printf
56#else // don't print debugging info
57#define DEBUG_printf(...) (void)0
58#endif
59
Damien13ed3a62013-10-08 09:05:10 +010060// wrapper around everything in this file
Damien Georgec90f59e2014-09-06 23:06:36 +010061#if (MICROPY_EMIT_X64 && N_X64) \
62 || (MICROPY_EMIT_X86 && N_X86) \
63 || (MICROPY_EMIT_THUMB && N_THUMB) \
64 || (MICROPY_EMIT_ARM && N_ARM)
Damien13ed3a62013-10-08 09:05:10 +010065
Damien3ef4abb2013-10-12 16:53:13 +010066#if N_X64
Damien13ed3a62013-10-08 09:05:10 +010067
68// x64 specific stuff
69
Damien George51dfcb42015-01-01 20:27:54 +000070#include "py/asmx64.h"
Damien13ed3a62013-10-08 09:05:10 +010071
Damien13ed3a62013-10-08 09:05:10 +010072#define EXPORT_FUN(name) emit_native_x64_##name
73
Damien George99886182015-04-06 22:38:53 +010074#define ASM_WORD_SIZE (8)
75
Damien George0b610de2014-09-29 16:25:04 +010076#define REG_RET ASM_X64_REG_RAX
77#define REG_ARG_1 ASM_X64_REG_RDI
78#define REG_ARG_2 ASM_X64_REG_RSI
79#define REG_ARG_3 ASM_X64_REG_RDX
80#define REG_ARG_4 ASM_X64_REG_RCX
Damien George99886182015-04-06 22:38:53 +010081#define REG_ARG_5 ASM_X64_REG_R08
Damien Georgec90f59e2014-09-06 23:06:36 +010082
Damien George81057362014-09-07 01:06:19 +010083// caller-save
Damien George0b610de2014-09-29 16:25:04 +010084#define REG_TEMP0 ASM_X64_REG_RAX
85#define REG_TEMP1 ASM_X64_REG_RDI
86#define REG_TEMP2 ASM_X64_REG_RSI
Damien George81057362014-09-07 01:06:19 +010087
88// callee-save
Damien George0b610de2014-09-29 16:25:04 +010089#define REG_LOCAL_1 ASM_X64_REG_RBX
90#define REG_LOCAL_2 ASM_X64_REG_R12
91#define REG_LOCAL_3 ASM_X64_REG_R13
Damien George81057362014-09-07 01:06:19 +010092#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +010093
94#define ASM_PASS_COMPUTE ASM_X64_PASS_COMPUTE
95#define ASM_PASS_EMIT ASM_X64_PASS_EMIT
96
97#define ASM_T asm_x64_t
98#define ASM_NEW asm_x64_new
99#define ASM_FREE asm_x64_free
100#define ASM_GET_CODE asm_x64_get_code
Damien George99886182015-04-06 22:38:53 +0100101#define ASM_GET_CODE_POS asm_x64_get_code_pos
Damien Georgec90f59e2014-09-06 23:06:36 +0100102#define ASM_GET_CODE_SIZE asm_x64_get_code_size
103#define ASM_START_PASS asm_x64_start_pass
104#define ASM_END_PASS asm_x64_end_pass
105#define ASM_ENTRY asm_x64_entry
106#define ASM_EXIT asm_x64_exit
107
Damien George99886182015-04-06 22:38:53 +0100108#define ASM_ALIGN asm_x64_align
109#define ASM_DATA asm_x64_data
110
Damien Georgec90f59e2014-09-06 23:06:36 +0100111#define ASM_LABEL_ASSIGN asm_x64_label_assign
112#define ASM_JUMP asm_x64_jmp_label
113#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
114 do { \
115 asm_x64_test_r8_with_r8(as, reg, reg); \
116 asm_x64_jcc_label(as, ASM_X64_CC_JZ, label); \
117 } while (0)
118#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
119 do { \
120 asm_x64_test_r8_with_r8(as, reg, reg); \
121 asm_x64_jcc_label(as, ASM_X64_CC_JNZ, label); \
122 } while (0)
123#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
124 do { \
125 asm_x64_cmp_r64_with_r64(as, reg1, reg2); \
126 asm_x64_jcc_label(as, ASM_X64_CC_JE, label); \
127 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100128#define ASM_CALL_IND(as, ptr, idx) asm_x64_call_ind(as, ptr, ASM_X64_REG_RAX)
Damien Georgec90f59e2014-09-06 23:06:36 +0100129
130#define ASM_MOV_REG_TO_LOCAL asm_x64_mov_r64_to_local
131#define ASM_MOV_IMM_TO_REG asm_x64_mov_i64_to_r64_optimised
132#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x64_mov_i64_to_r64_aligned
133#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
134 do { \
135 asm_x64_mov_i64_to_r64_optimised(as, (imm), (reg_temp)); \
136 asm_x64_mov_r64_to_local(as, (reg_temp), (local_num)); \
137 } while (false)
138#define ASM_MOV_LOCAL_TO_REG asm_x64_mov_local_to_r64
Damien George3112cde2014-09-29 18:45:42 +0100139#define ASM_MOV_REG_REG(as, reg_dest, reg_src) asm_x64_mov_r64_r64((as), (reg_dest), (reg_src))
Damien Georgec90f59e2014-09-06 23:06:36 +0100140#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x64_mov_local_addr_to_r64
141
Damien George3112cde2014-09-29 18:45:42 +0100142#define ASM_LSL_REG(as, reg) asm_x64_shl_r64_cl((as), (reg))
143#define ASM_ASR_REG(as, reg) asm_x64_sar_r64_cl((as), (reg))
Damien George1ef23482014-10-12 14:21:06 +0100144#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x64_or_r64_r64((as), (reg_dest), (reg_src))
145#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x64_xor_r64_r64((as), (reg_dest), (reg_src))
146#define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_x64_and_r64_r64((as), (reg_dest), (reg_src))
Damien George3112cde2014-09-29 18:45:42 +0100147#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x64_add_r64_r64((as), (reg_dest), (reg_src))
148#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x64_sub_r64_r64((as), (reg_dest), (reg_src))
149
Damien George91cfd412014-10-12 16:59:29 +0100150#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem64_to_r64((as), (reg_base), 0, (reg_dest))
Damien George4cd9ced2015-01-15 14:41:41 +0000151#define ASM_LOAD_REG_REG_OFFSET(as, reg_dest, reg_base, word_offset) asm_x64_mov_mem64_to_r64((as), (reg_base), 8 * (word_offset), (reg_dest))
Damien George91cfd412014-10-12 16:59:29 +0100152#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem8_to_r64zx((as), (reg_base), 0, (reg_dest))
153#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem16_to_r64zx((as), (reg_base), 0, (reg_dest))
154
155#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_x64_mov_r64_to_mem64((as), (reg_src), (reg_base), 0)
Damien George4cd9ced2015-01-15 14:41:41 +0000156#define ASM_STORE_REG_REG_OFFSET(as, reg_src, reg_base, word_offset) asm_x64_mov_r64_to_mem64((as), (reg_src), (reg_base), 8 * (word_offset))
Damien George91cfd412014-10-12 16:59:29 +0100157#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x64_mov_r8_to_mem8((as), (reg_src), (reg_base), 0)
158#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_x64_mov_r16_to_mem16((as), (reg_src), (reg_base), 0)
Damien Georgee9dac3b2014-09-29 22:10:41 +0100159
Damien Georgec90f59e2014-09-06 23:06:36 +0100160#elif N_X86
161
162// x86 specific stuff
163
Damien George51dfcb42015-01-01 20:27:54 +0000164#include "py/asmx86.h"
Damien Georgec90f59e2014-09-06 23:06:36 +0100165
166STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
167 [MP_F_CONVERT_OBJ_TO_NATIVE] = 2,
168 [MP_F_CONVERT_NATIVE_TO_OBJ] = 2,
Damien Georgec90f59e2014-09-06 23:06:36 +0100169 [MP_F_LOAD_CONST_STR] = 1,
170 [MP_F_LOAD_CONST_BYTES] = 1,
171 [MP_F_LOAD_NAME] = 1,
172 [MP_F_LOAD_GLOBAL] = 1,
173 [MP_F_LOAD_BUILD_CLASS] = 0,
174 [MP_F_LOAD_ATTR] = 2,
175 [MP_F_LOAD_METHOD] = 3,
176 [MP_F_STORE_NAME] = 2,
177 [MP_F_STORE_GLOBAL] = 2,
178 [MP_F_STORE_ATTR] = 3,
179 [MP_F_OBJ_SUBSCR] = 3,
180 [MP_F_OBJ_IS_TRUE] = 1,
181 [MP_F_UNARY_OP] = 2,
182 [MP_F_BINARY_OP] = 3,
183 [MP_F_BUILD_TUPLE] = 2,
184 [MP_F_BUILD_LIST] = 2,
185 [MP_F_LIST_APPEND] = 2,
186 [MP_F_BUILD_MAP] = 1,
187 [MP_F_STORE_MAP] = 3,
188#if MICROPY_PY_BUILTINS_SET
189 [MP_F_BUILD_SET] = 2,
190 [MP_F_STORE_SET] = 2,
191#endif
192 [MP_F_MAKE_FUNCTION_FROM_RAW_CODE] = 3,
193 [MP_F_NATIVE_CALL_FUNCTION_N_KW] = 3,
194 [MP_F_CALL_METHOD_N_KW] = 3,
Damien George78772ad2015-04-06 22:48:21 +0100195 [MP_F_CALL_METHOD_N_KW_VAR] = 3,
Damien Georgec90f59e2014-09-06 23:06:36 +0100196 [MP_F_GETITER] = 1,
197 [MP_F_ITERNEXT] = 1,
198 [MP_F_NLR_PUSH] = 1,
199 [MP_F_NLR_POP] = 0,
200 [MP_F_NATIVE_RAISE] = 1,
201 [MP_F_IMPORT_NAME] = 3,
202 [MP_F_IMPORT_FROM] = 2,
203 [MP_F_IMPORT_ALL] = 1,
204#if MICROPY_PY_BUILTINS_SLICE
205 [MP_F_NEW_SLICE] = 3,
206#endif
207 [MP_F_UNPACK_SEQUENCE] = 3,
208 [MP_F_UNPACK_EX] = 3,
209 [MP_F_DELETE_NAME] = 1,
210 [MP_F_DELETE_GLOBAL] = 1,
Damien George99957382015-04-03 14:38:41 +0000211 [MP_F_NEW_CELL] = 1,
212 [MP_F_MAKE_CLOSURE_FROM_RAW_CODE] = 3,
Damien George99886182015-04-06 22:38:53 +0100213 [MP_F_SETUP_CODE_STATE] = 5,
Damien Georgec90f59e2014-09-06 23:06:36 +0100214};
215
216#define EXPORT_FUN(name) emit_native_x86_##name
217
Damien George99886182015-04-06 22:38:53 +0100218#define ASM_WORD_SIZE (4)
219
Damien George0b610de2014-09-29 16:25:04 +0100220#define REG_RET ASM_X86_REG_EAX
Damien George6eae8612014-09-08 22:16:35 +0000221#define REG_ARG_1 ASM_X86_REG_ARG_1
222#define REG_ARG_2 ASM_X86_REG_ARG_2
223#define REG_ARG_3 ASM_X86_REG_ARG_3
Damien George99886182015-04-06 22:38:53 +0100224#define REG_ARG_4 ASM_X86_REG_ARG_4
225#define REG_ARG_5 ASM_X86_REG_ARG_5
Damien George81057362014-09-07 01:06:19 +0100226
Damien George25d90412014-09-06 23:24:32 +0000227// caller-save, so can be used as temporaries
Damien George0b610de2014-09-29 16:25:04 +0100228#define REG_TEMP0 ASM_X86_REG_EAX
229#define REG_TEMP1 ASM_X86_REG_ECX
230#define REG_TEMP2 ASM_X86_REG_EDX
Damien Georgec90f59e2014-09-06 23:06:36 +0100231
Damien George25d90412014-09-06 23:24:32 +0000232// callee-save, so can be used as locals
Damien George0b610de2014-09-29 16:25:04 +0100233#define REG_LOCAL_1 ASM_X86_REG_EBX
234#define REG_LOCAL_2 ASM_X86_REG_ESI
235#define REG_LOCAL_3 ASM_X86_REG_EDI
Damien George25d90412014-09-06 23:24:32 +0000236#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100237
238#define ASM_PASS_COMPUTE ASM_X86_PASS_COMPUTE
239#define ASM_PASS_EMIT ASM_X86_PASS_EMIT
240
241#define ASM_T asm_x86_t
242#define ASM_NEW asm_x86_new
243#define ASM_FREE asm_x86_free
244#define ASM_GET_CODE asm_x86_get_code
Damien George99886182015-04-06 22:38:53 +0100245#define ASM_GET_CODE_POS asm_x86_get_code_pos
Damien Georgec90f59e2014-09-06 23:06:36 +0100246#define ASM_GET_CODE_SIZE asm_x86_get_code_size
247#define ASM_START_PASS asm_x86_start_pass
248#define ASM_END_PASS asm_x86_end_pass
249#define ASM_ENTRY asm_x86_entry
250#define ASM_EXIT asm_x86_exit
251
Damien George99886182015-04-06 22:38:53 +0100252#define ASM_ALIGN asm_x86_align
253#define ASM_DATA asm_x86_data
254
Damien Georgec90f59e2014-09-06 23:06:36 +0100255#define ASM_LABEL_ASSIGN asm_x86_label_assign
256#define ASM_JUMP asm_x86_jmp_label
257#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
258 do { \
259 asm_x86_test_r8_with_r8(as, reg, reg); \
260 asm_x86_jcc_label(as, ASM_X86_CC_JZ, label); \
261 } while (0)
262#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
263 do { \
264 asm_x86_test_r8_with_r8(as, reg, reg); \
265 asm_x86_jcc_label(as, ASM_X86_CC_JNZ, label); \
266 } while (0)
267#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
268 do { \
269 asm_x86_cmp_r32_with_r32(as, reg1, reg2); \
270 asm_x86_jcc_label(as, ASM_X86_CC_JE, label); \
271 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100272#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 +0100273
274#define ASM_MOV_REG_TO_LOCAL asm_x86_mov_r32_to_local
275#define ASM_MOV_IMM_TO_REG asm_x86_mov_i32_to_r32
276#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x86_mov_i32_to_r32_aligned
277#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
278 do { \
279 asm_x86_mov_i32_to_r32(as, (imm), (reg_temp)); \
280 asm_x86_mov_r32_to_local(as, (reg_temp), (local_num)); \
281 } while (false)
282#define ASM_MOV_LOCAL_TO_REG asm_x86_mov_local_to_r32
Damien George3112cde2014-09-29 18:45:42 +0100283#define ASM_MOV_REG_REG(as, reg_dest, reg_src) asm_x86_mov_r32_r32((as), (reg_dest), (reg_src))
Damien Georgec90f59e2014-09-06 23:06:36 +0100284#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x86_mov_local_addr_to_r32
Damien13ed3a62013-10-08 09:05:10 +0100285
Damien George3112cde2014-09-29 18:45:42 +0100286#define ASM_LSL_REG(as, reg) asm_x86_shl_r32_cl((as), (reg))
287#define ASM_ASR_REG(as, reg) asm_x86_sar_r32_cl((as), (reg))
Damien George1ef23482014-10-12 14:21:06 +0100288#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x86_or_r32_r32((as), (reg_dest), (reg_src))
289#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x86_xor_r32_r32((as), (reg_dest), (reg_src))
290#define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_x86_and_r32_r32((as), (reg_dest), (reg_src))
Damien George3112cde2014-09-29 18:45:42 +0100291#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x86_add_r32_r32((as), (reg_dest), (reg_src))
292#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x86_sub_r32_r32((as), (reg_dest), (reg_src))
293
Damien George91cfd412014-10-12 16:59:29 +0100294#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem32_to_r32((as), (reg_base), 0, (reg_dest))
Damien George99957382015-04-03 14:38:41 +0000295#define ASM_LOAD_REG_REG_OFFSET(as, reg_dest, reg_base, word_offset) asm_x86_mov_mem32_to_r32((as), (reg_base), 4 * (word_offset), (reg_dest))
Damien George3c34d412014-10-12 16:10:25 +0000296#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem8_to_r32zx((as), (reg_base), 0, (reg_dest))
297#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem16_to_r32zx((as), (reg_base), 0, (reg_dest))
Damien George91cfd412014-10-12 16:59:29 +0100298
299#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_x86_mov_r32_to_mem32((as), (reg_src), (reg_base), 0)
Damien George99957382015-04-03 14:38:41 +0000300#define ASM_STORE_REG_REG_OFFSET(as, reg_src, reg_base, word_offset) asm_x86_mov_r32_to_mem32((as), (reg_src), (reg_base), 4 * (word_offset))
Damien George91cfd412014-10-12 16:59:29 +0100301#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x86_mov_r8_to_mem8((as), (reg_src), (reg_base), 0)
302#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_x86_mov_r16_to_mem16((as), (reg_src), (reg_base), 0)
Damien Georgee9dac3b2014-09-29 22:10:41 +0100303
Damien3ef4abb2013-10-12 16:53:13 +0100304#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100305
306// thumb specific stuff
307
Damien George51dfcb42015-01-01 20:27:54 +0000308#include "py/asmthumb.h"
Damien13ed3a62013-10-08 09:05:10 +0100309
Damien13ed3a62013-10-08 09:05:10 +0100310#define EXPORT_FUN(name) emit_native_thumb_##name
311
Damien George99886182015-04-06 22:38:53 +0100312#define ASM_WORD_SIZE (4)
313
Damien George0b610de2014-09-29 16:25:04 +0100314#define REG_RET ASM_THUMB_REG_R0
315#define REG_ARG_1 ASM_THUMB_REG_R0
316#define REG_ARG_2 ASM_THUMB_REG_R1
317#define REG_ARG_3 ASM_THUMB_REG_R2
318#define REG_ARG_4 ASM_THUMB_REG_R3
Damien George99886182015-04-06 22:38:53 +0100319// rest of args go on stack
Damien George81057362014-09-07 01:06:19 +0100320
Damien George0b610de2014-09-29 16:25:04 +0100321#define REG_TEMP0 ASM_THUMB_REG_R0
322#define REG_TEMP1 ASM_THUMB_REG_R1
323#define REG_TEMP2 ASM_THUMB_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100324
Damien George0b610de2014-09-29 16:25:04 +0100325#define REG_LOCAL_1 ASM_THUMB_REG_R4
326#define REG_LOCAL_2 ASM_THUMB_REG_R5
327#define REG_LOCAL_3 ASM_THUMB_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100328#define REG_LOCAL_NUM (3)
329
330#define ASM_PASS_COMPUTE ASM_THUMB_PASS_COMPUTE
331#define ASM_PASS_EMIT ASM_THUMB_PASS_EMIT
332
333#define ASM_T asm_thumb_t
334#define ASM_NEW asm_thumb_new
335#define ASM_FREE asm_thumb_free
336#define ASM_GET_CODE asm_thumb_get_code
Damien George99886182015-04-06 22:38:53 +0100337#define ASM_GET_CODE_POS asm_thumb_get_code_pos
Damien Georgec90f59e2014-09-06 23:06:36 +0100338#define ASM_GET_CODE_SIZE asm_thumb_get_code_size
339#define ASM_START_PASS asm_thumb_start_pass
340#define ASM_END_PASS asm_thumb_end_pass
341#define ASM_ENTRY asm_thumb_entry
342#define ASM_EXIT asm_thumb_exit
343
Damien George99886182015-04-06 22:38:53 +0100344#define ASM_ALIGN asm_thumb_align
345#define ASM_DATA asm_thumb_data
346
Damien Georgec90f59e2014-09-06 23:06:36 +0100347#define ASM_LABEL_ASSIGN asm_thumb_label_assign
348#define ASM_JUMP asm_thumb_b_label
349#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
350 do { \
351 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100352 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100353 } while (0)
354#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
355 do { \
356 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100357 asm_thumb_bcc_label(as, ASM_THUMB_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100358 } while (0)
359#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
360 do { \
361 asm_thumb_cmp_rlo_rlo(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100362 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100363 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100364#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 +0100365
366#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_thumb_mov_local_reg(as, (local_num), (reg))
367#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_optimised(as, (reg), (imm))
368#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_aligned(as, (reg), (imm))
369#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
370 do { \
371 asm_thumb_mov_reg_i32_optimised(as, (reg_temp), (imm)); \
372 asm_thumb_mov_local_reg(as, (local_num), (reg_temp)); \
373 } while (false)
374#define ASM_MOV_LOCAL_TO_REG(as, local_num, reg) asm_thumb_mov_reg_local(as, (reg), (local_num))
Damien George3112cde2014-09-29 18:45:42 +0100375#define ASM_MOV_REG_REG(as, reg_dest, reg_src) asm_thumb_mov_reg_reg((as), (reg_dest), (reg_src))
Damien Georgec90f59e2014-09-06 23:06:36 +0100376#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 +0100377
Damien George3112cde2014-09-29 18:45:42 +0100378#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_LSL, (reg_dest), (reg_shift))
379#define ASM_ASR_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_ASR, (reg_dest), (reg_shift))
Damien George1ef23482014-10-12 14:21:06 +0100380#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_ORR, (reg_dest), (reg_src))
381#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_EOR, (reg_dest), (reg_src))
382#define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_AND, (reg_dest), (reg_src))
Damien George3112cde2014-09-29 18:45:42 +0100383#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_thumb_add_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
384#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_thumb_sub_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
385
Damien George91cfd412014-10-12 16:59:29 +0100386#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_thumb_ldr_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
Damien George4cd9ced2015-01-15 14:41:41 +0000387#define ASM_LOAD_REG_REG_OFFSET(as, reg_dest, reg_base, word_offset) asm_thumb_ldr_rlo_rlo_i5((as), (reg_dest), (reg_base), (word_offset))
Damien George91cfd412014-10-12 16:59:29 +0100388#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrb_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
389#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrh_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
390
Damien Georgee9dac3b2014-09-29 22:10:41 +0100391#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_thumb_str_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
Damien George4cd9ced2015-01-15 14:41:41 +0000392#define ASM_STORE_REG_REG_OFFSET(as, reg_src, reg_base, word_offset) asm_thumb_str_rlo_rlo_i5((as), (reg_src), (reg_base), (word_offset))
Damien Georgee9dac3b2014-09-29 22:10:41 +0100393#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_thumb_strb_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
394#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_thumb_strh_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
395
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200396#elif N_ARM
397
398// ARM specific stuff
399
Damien George51dfcb42015-01-01 20:27:54 +0000400#include "py/asmarm.h"
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200401
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200402#define EXPORT_FUN(name) emit_native_arm_##name
403
Damien George0b610de2014-09-29 16:25:04 +0100404#define REG_RET ASM_ARM_REG_R0
405#define REG_ARG_1 ASM_ARM_REG_R0
406#define REG_ARG_2 ASM_ARM_REG_R1
407#define REG_ARG_3 ASM_ARM_REG_R2
408#define REG_ARG_4 ASM_ARM_REG_R3
Damien George81057362014-09-07 01:06:19 +0100409
Damien George0b610de2014-09-29 16:25:04 +0100410#define REG_TEMP0 ASM_ARM_REG_R0
411#define REG_TEMP1 ASM_ARM_REG_R1
412#define REG_TEMP2 ASM_ARM_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100413
Damien George0b610de2014-09-29 16:25:04 +0100414#define REG_LOCAL_1 ASM_ARM_REG_R4
415#define REG_LOCAL_2 ASM_ARM_REG_R5
416#define REG_LOCAL_3 ASM_ARM_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100417#define REG_LOCAL_NUM (3)
418
419#define ASM_PASS_COMPUTE ASM_ARM_PASS_COMPUTE
420#define ASM_PASS_EMIT ASM_ARM_PASS_EMIT
421
422#define ASM_T asm_arm_t
423#define ASM_NEW asm_arm_new
424#define ASM_FREE asm_arm_free
425#define ASM_GET_CODE asm_arm_get_code
426#define ASM_GET_CODE_SIZE asm_arm_get_code_size
427#define ASM_START_PASS asm_arm_start_pass
428#define ASM_END_PASS asm_arm_end_pass
429#define ASM_ENTRY asm_arm_entry
430#define ASM_EXIT asm_arm_exit
431
432#define ASM_LABEL_ASSIGN asm_arm_label_assign
433#define ASM_JUMP asm_arm_b_label
434#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
435 do { \
436 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100437 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100438 } while (0)
439#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
440 do { \
441 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100442 asm_arm_bcc_label(as, ASM_ARM_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100443 } while (0)
444#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
445 do { \
446 asm_arm_cmp_reg_reg(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100447 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100448 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100449#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 +0100450
451#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_arm_mov_local_reg(as, (local_num), (reg))
452#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
453#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
454#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
455 do { \
456 asm_arm_mov_reg_i32(as, (reg_temp), (imm)); \
457 asm_arm_mov_local_reg(as, (local_num), (reg_temp)); \
458 } while (false)
459#define ASM_MOV_LOCAL_TO_REG(as, local_num, reg) asm_arm_mov_reg_local(as, (reg), (local_num))
Damien George3112cde2014-09-29 18:45:42 +0100460#define ASM_MOV_REG_REG(as, reg_dest, reg_src) asm_arm_mov_reg_reg((as), (reg_dest), (reg_src))
Damien Georgec90f59e2014-09-06 23:06:36 +0100461#define ASM_MOV_LOCAL_ADDR_TO_REG(as, local_num, reg) asm_arm_mov_reg_local_addr(as, (reg), (local_num))
462
Fabian Vogte5268962014-10-04 00:53:46 +0200463#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_arm_lsl_reg_reg((as), (reg_dest), (reg_shift))
464#define ASM_ASR_REG_REG(as, reg_dest, reg_shift) asm_arm_asr_reg_reg((as), (reg_dest), (reg_shift))
Damien George1ef23482014-10-12 14:21:06 +0100465#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_arm_orr_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
466#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_arm_eor_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
467#define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_arm_and_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
Damien George3112cde2014-09-29 18:45:42 +0100468#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_arm_add_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
469#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_arm_sub_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
470
Damien George91cfd412014-10-12 16:59:29 +0100471#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_arm_ldr_reg_reg((as), (reg_dest), (reg_base))
472#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_arm_ldrb_reg_reg((as), (reg_dest), (reg_base))
473#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_arm_ldrh_reg_reg((as), (reg_dest), (reg_base))
474
Fabian Vogte5268962014-10-04 00:53:46 +0200475#define ASM_STORE_REG_REG(as, reg_value, reg_base) asm_arm_str_reg_reg((as), (reg_value), (reg_base))
476#define ASM_STORE8_REG_REG(as, reg_value, reg_base) asm_arm_strb_reg_reg((as), (reg_value), (reg_base))
477#define ASM_STORE16_REG_REG(as, reg_value, reg_base) asm_arm_strh_reg_reg((as), (reg_value), (reg_base))
Damien Georgee9dac3b2014-09-29 22:10:41 +0100478
Damien Georgec90f59e2014-09-06 23:06:36 +0100479#else
480
481#error unknown native emitter
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200482
Damien13ed3a62013-10-08 09:05:10 +0100483#endif
484
485typedef enum {
Damienff8ed772013-10-08 22:18:32 +0100486 STACK_VALUE,
487 STACK_REG,
488 STACK_IMM,
489} stack_info_kind_t;
Damien13ed3a62013-10-08 09:05:10 +0100490
Damien Georgee9dac3b2014-09-29 22:10:41 +0100491// these enums must be distinct and the bottom 2 bits
492// must correspond to the correct MP_NATIVE_TYPE_xxx value
Damien13ed3a62013-10-08 09:05:10 +0100493typedef enum {
Damien Georgee9dac3b2014-09-29 22:10:41 +0100494 VTYPE_PYOBJ = 0x00 | MP_NATIVE_TYPE_OBJ,
495 VTYPE_BOOL = 0x00 | MP_NATIVE_TYPE_BOOL,
496 VTYPE_INT = 0x00 | MP_NATIVE_TYPE_INT,
497 VTYPE_UINT = 0x00 | MP_NATIVE_TYPE_UINT,
498
499 VTYPE_PTR = 0x10 | MP_NATIVE_TYPE_UINT, // pointer to word sized entity
500 VTYPE_PTR8 = 0x20 | MP_NATIVE_TYPE_UINT,
501 VTYPE_PTR16 = 0x30 | MP_NATIVE_TYPE_UINT,
502 VTYPE_PTR_NONE = 0x40 | MP_NATIVE_TYPE_UINT,
503
504 VTYPE_UNBOUND = 0x50 | MP_NATIVE_TYPE_OBJ,
505 VTYPE_BUILTIN_CAST = 0x60 | MP_NATIVE_TYPE_OBJ,
Damien13ed3a62013-10-08 09:05:10 +0100506} vtype_kind_t;
507
Damienff8ed772013-10-08 22:18:32 +0100508typedef struct _stack_info_t {
509 vtype_kind_t vtype;
510 stack_info_kind_t kind;
511 union {
512 int u_reg;
Damien George40f3c022014-07-03 13:25:24 +0100513 mp_int_t u_imm;
Damien George32444b72015-01-24 23:14:12 +0000514 } data;
Damienff8ed772013-10-08 22:18:32 +0100515} stack_info_t;
516
Damien13ed3a62013-10-08 09:05:10 +0100517struct _emit_t {
518 int pass;
519
520 bool do_viper_types;
Damienff8ed772013-10-08 22:18:32 +0100521
Damien George2ac4af62014-08-15 16:45:41 +0100522 vtype_kind_t return_vtype;
523
Damien George7ff996c2014-09-08 23:05:16 +0100524 mp_uint_t local_vtype_alloc;
Damien13ed3a62013-10-08 09:05:10 +0100525 vtype_kind_t *local_vtype;
Damienff8ed772013-10-08 22:18:32 +0100526
Damien George7ff996c2014-09-08 23:05:16 +0100527 mp_uint_t stack_info_alloc;
Damienff8ed772013-10-08 22:18:32 +0100528 stack_info_t *stack_info;
Damien George21ca2d72014-10-19 19:00:51 +0100529 vtype_kind_t saved_stack_vtype;
Damienff8ed772013-10-08 22:18:32 +0100530
Damien George99886182015-04-06 22:38:53 +0100531 int code_info_size;
532 int code_info_offset;
533 int prelude_offset;
534 int n_state;
Damien13ed3a62013-10-08 09:05:10 +0100535 int stack_start;
536 int stack_size;
537
538 bool last_emit_was_return_value;
539
Damien13ed3a62013-10-08 09:05:10 +0100540 scope_t *scope;
541
Damien Georgec90f59e2014-09-06 23:06:36 +0100542 ASM_T *as;
Damien13ed3a62013-10-08 09:05:10 +0100543};
544
Damien George7ff996c2014-09-08 23:05:16 +0100545emit_t *EXPORT_FUN(new)(mp_uint_t max_num_labels) {
Damien George36db6bc2014-05-07 17:24:22 +0100546 emit_t *emit = m_new0(emit_t, 1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100547 emit->as = ASM_NEW(max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100548 return emit;
549}
550
Damien George41d02b62014-01-24 22:42:28 +0000551void EXPORT_FUN(free)(emit_t *emit) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100552 ASM_FREE(emit->as, false);
Damien George36db6bc2014-05-07 17:24:22 +0100553 m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
554 m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200555 m_del_obj(emit_t, emit);
556}
557
Damien George2ac4af62014-08-15 16:45:41 +0100558STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
559 switch (op) {
560 case MP_EMIT_NATIVE_TYPE_ENABLE:
561 emit->do_viper_types = arg1;
562 break;
563
564 default: {
565 vtype_kind_t type;
566 switch (arg2) {
567 case MP_QSTR_object: type = VTYPE_PYOBJ; break;
568 case MP_QSTR_bool: type = VTYPE_BOOL; break;
569 case MP_QSTR_int: type = VTYPE_INT; break;
570 case MP_QSTR_uint: type = VTYPE_UINT; break;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100571 case MP_QSTR_ptr: type = VTYPE_PTR; break;
572 case MP_QSTR_ptr8: type = VTYPE_PTR8; break;
573 case MP_QSTR_ptr16: type = VTYPE_PTR16; break;
Damien George2ac4af62014-08-15 16:45:41 +0100574 default: printf("ViperTypeError: unknown type %s\n", qstr_str(arg2)); return;
575 }
576 if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
577 emit->return_vtype = type;
578 } else {
579 assert(arg1 < emit->local_vtype_alloc);
580 emit->local_vtype[arg1] = type;
581 }
582 break;
583 }
584 }
Damien13ed3a62013-10-08 09:05:10 +0100585}
586
Damien Georgefa5950e2015-04-03 15:03:24 +0000587STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest);
Damien George4cd9ced2015-01-15 14:41:41 +0000588STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg);
Damien Georgefa5950e2015-04-03 15:03:24 +0000589STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
Damien George4cd9ced2015-01-15 14:41:41 +0000590STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
Damien Georgefa5950e2015-04-03 15:03:24 +0000591
Damien George99886182015-04-06 22:38:53 +0100592#define STATE_START (sizeof(mp_code_state) / sizeof(mp_uint_t))
593
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200594STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien Georged6230f62014-09-23 14:10:03 +0000595 DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope);
596
Damien13ed3a62013-10-08 09:05:10 +0100597 emit->pass = pass;
598 emit->stack_start = 0;
599 emit->stack_size = 0;
600 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100601 emit->scope = scope;
602
Damien George36db6bc2014-05-07 17:24:22 +0100603 // allocate memory for keeping track of the types of locals
604 if (emit->local_vtype_alloc < scope->num_locals) {
605 emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
606 emit->local_vtype_alloc = scope->num_locals;
Damienff8ed772013-10-08 22:18:32 +0100607 }
Damien George36db6bc2014-05-07 17:24:22 +0100608
609 // allocate memory for keeping track of the objects on the stack
610 // XXX don't know stack size on entry, and it should be maximum over all scopes
Damien George51229af2015-03-26 17:54:12 +0000611 // XXX this is such a big hack and really needs to be fixed
Damienff8ed772013-10-08 22:18:32 +0100612 if (emit->stack_info == NULL) {
Damien George51229af2015-03-26 17:54:12 +0000613 emit->stack_info_alloc = scope->stack_size + 200;
Damienff8ed772013-10-08 22:18:32 +0100614 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100615 }
616
Damien George99886182015-04-06 22:38:53 +0100617 // set default type for return
Damien George2ac4af62014-08-15 16:45:41 +0100618 emit->return_vtype = VTYPE_PYOBJ;
Damien George99886182015-04-06 22:38:53 +0100619
620 // set default type for arguments
621 mp_uint_t num_args = emit->scope->num_pos_args + emit->scope->num_kwonly_args;
622 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
623 num_args += 1;
624 }
625 if (scope->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) {
626 num_args += 1;
627 }
628 for (mp_uint_t i = 0; i < num_args; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100629 emit->local_vtype[i] = VTYPE_PYOBJ;
630 }
Damien Georgea5190a72014-08-15 22:39:08 +0100631
632 // local variables begin unbound, and have unknown type
Damien George99886182015-04-06 22:38:53 +0100633 for (mp_uint_t i = num_args; i < emit->local_vtype_alloc; i++) {
Damien Georgea5190a72014-08-15 22:39:08 +0100634 emit->local_vtype[i] = VTYPE_UNBOUND;
635 }
636
637 // values on stack begin unbound
638 for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100639 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgea5190a72014-08-15 22:39:08 +0100640 emit->stack_info[i].vtype = VTYPE_UNBOUND;
Damien13ed3a62013-10-08 09:05:10 +0100641 }
642
Damien Georgec90f59e2014-09-06 23:06:36 +0100643 ASM_START_PASS(emit->as, pass == MP_PASS_EMIT ? ASM_PASS_EMIT : ASM_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100644
Damien George99886182015-04-06 22:38:53 +0100645 // generate code for entry to function
Damien13ed3a62013-10-08 09:05:10 +0100646
Damien George99886182015-04-06 22:38:53 +0100647 if (emit->do_viper_types) {
648
649 // entry to function
650 int num_locals = 0;
651 if (pass > MP_PASS_SCOPE) {
652 num_locals = scope->num_locals - REG_LOCAL_NUM;
653 if (num_locals < 0) {
654 num_locals = 0;
655 }
656 emit->stack_start = num_locals;
657 num_locals += scope->stack_size;
Damien13ed3a62013-10-08 09:05:10 +0100658 }
Damien George99886182015-04-06 22:38:53 +0100659 ASM_ENTRY(emit->as, num_locals);
660
661 #if N_X86
662 for (int i = 0; i < scope->num_pos_args; i++) {
663 if (i == 0) {
664 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_1);
665 } else if (i == 1) {
666 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_2);
667 } else if (i == 2) {
668 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_3);
669 } else {
670 asm_x86_mov_arg_to_r32(emit->as, i, REG_TEMP0);
671 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, i - REG_LOCAL_NUM);
672 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100673 }
Damien George99886182015-04-06 22:38:53 +0100674 #else
675 for (int i = 0; i < scope->num_pos_args; i++) {
676 if (i == 0) {
677 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
678 } else if (i == 1) {
679 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
680 } else if (i == 2) {
681 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
682 } else if (i == 3) {
683 ASM_MOV_REG_TO_LOCAL(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
684 } else {
685 // TODO not implemented
686 assert(0);
687 }
688 }
689 #endif
690
691 } else {
692 // work out size of state (locals plus stack)
693 emit->n_state = scope->num_locals + scope->stack_size;
694
695 // allocate space on C-stack for code_state structure, which includes state
696 ASM_ENTRY(emit->as, STATE_START + emit->n_state);
697
698 // prepare incoming arguments for call to mp_setup_code_state
699 #if N_X86
700 asm_x86_mov_arg_to_r32(emit->as, 0, REG_ARG_2);
701 asm_x86_mov_arg_to_r32(emit->as, 1, REG_ARG_3);
702 asm_x86_mov_arg_to_r32(emit->as, 2, REG_ARG_4);
703 asm_x86_mov_arg_to_r32(emit->as, 3, REG_ARG_5);
704 #else
705 #if N_THUMB
706 ASM_MOV_REG_REG(emit->as, ASM_THUMB_REG_R4, REG_ARG_4);
707 #else
708 ASM_MOV_REG_REG(emit->as, REG_ARG_5, REG_ARG_4);
709 #endif
710 ASM_MOV_REG_REG(emit->as, REG_ARG_4, REG_ARG_3);
711 ASM_MOV_REG_REG(emit->as, REG_ARG_3, REG_ARG_2);
712 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_ARG_1);
713 #endif
714
715 // set code_state.code_info (offset from start of this function to code_info data)
716 // XXX this encoding may change size
717 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, emit->code_info_offset, offsetof(mp_code_state, code_info) / sizeof(mp_uint_t), REG_ARG_1);
718
719 // set code_state.ip (offset from start of this function to prelude info)
720 // XXX this encoding may change size
721 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, emit->prelude_offset, offsetof(mp_code_state, ip) / sizeof(mp_uint_t), REG_ARG_1);
722
723 // set code_state.n_state
724 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, emit->n_state, offsetof(mp_code_state, n_state) / sizeof(mp_uint_t), REG_ARG_1);
725
726 // put address of code_state into first arg
727 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, 0, REG_ARG_1);
728
729 // call mp_setup_code_state to prepare code_state structure
730 #if N_THUMB
731 asm_thumb_op16(emit->as, 0xb400 | (1 << ASM_THUMB_REG_R4)); // push 5th arg
732 asm_thumb_bl_ind(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE, ASM_THUMB_REG_R4);
733 asm_thumb_op16(emit->as, 0xbc00 | (1 << REG_RET)); // pop dummy (was 5th arg)
734 #else
735 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE);
736 #endif
737
738 // cache some locals in registers
739 if (scope->num_locals > 0) {
740 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 0, REG_LOCAL_1);
741 if (scope->num_locals > 1) {
742 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 1, REG_LOCAL_2);
743 if (scope->num_locals > 2) {
744 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 2, REG_LOCAL_3);
745 }
746 }
747 }
748
749 // set the type of closed over variables
750 for (mp_uint_t i = 0; i < scope->id_info_len; i++) {
751 id_info_t *id = &scope->id_info[i];
752 if (id->kind == ID_INFO_KIND_CELL) {
753 emit->local_vtype[id->local_num] = VTYPE_PYOBJ;
754 }
Damien13ed3a62013-10-08 09:05:10 +0100755 }
756 }
757
Damien George99886182015-04-06 22:38:53 +0100758 #if N_THUMB
Damien Georgee9dac3b2014-09-29 22:10:41 +0100759 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100760 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
Damien George99886182015-04-06 22:38:53 +0100761 #endif
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200762
Damien George99886182015-04-06 22:38:53 +0100763 #if N_ARM
Damien Georgee9dac3b2014-09-29 22:10:41 +0100764 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100765 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
Damien George99886182015-04-06 22:38:53 +0100766 #endif
Damien13ed3a62013-10-08 09:05:10 +0100767}
768
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200769STATIC void emit_native_end_pass(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100770 if (!emit->last_emit_was_return_value) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100771 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100772 }
Damien George99886182015-04-06 22:38:53 +0100773
774 if (!emit->do_viper_types) {
775 // write dummy code info (for mp_setup_code_state to parse) and arg names
776 emit->code_info_offset = ASM_GET_CODE_POS(emit->as);
777 ASM_DATA(emit->as, 1, emit->code_info_size);
778 ASM_ALIGN(emit->as, ASM_WORD_SIZE);
779 emit->code_info_size = ASM_GET_CODE_POS(emit->as) - emit->code_info_offset;
780 for (int i = 0; i < emit->scope->num_pos_args + emit->scope->num_kwonly_args; i++) {
781 ASM_DATA(emit->as, ASM_WORD_SIZE, (mp_uint_t)MP_OBJ_NEW_QSTR(emit->scope->id_info[i].qst));
782 }
783
784 // bytecode prelude: initialise closed over variables
785 emit->prelude_offset = ASM_GET_CODE_POS(emit->as);
786 for (int i = 0; i < emit->scope->id_info_len; i++) {
787 id_info_t *id = &emit->scope->id_info[i];
788 if (id->kind == ID_INFO_KIND_CELL) {
789 assert(id->local_num < 255);
790 ASM_DATA(emit->as, 1, id->local_num); // write the local which should be converted to a cell
791 }
792 }
793 ASM_DATA(emit->as, 1, 255); // end of list sentinel
794 }
795
Damien Georgec90f59e2014-09-06 23:06:36 +0100796 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100797
798 // check stack is back to zero size
799 if (emit->stack_size != 0) {
800 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
801 }
802
Damien George36db6bc2014-05-07 17:24:22 +0100803 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100804 void *f = ASM_GET_CODE(emit->as);
805 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100806
807 // compute type signature
Damien Georgee9dac3b2014-09-29 22:10:41 +0100808 // note that the lower 2 bits of a vtype are tho correct MP_NATIVE_TYPE_xxx
Damien George2ac4af62014-08-15 16:45:41 +0100809 mp_uint_t type_sig = emit->return_vtype & 3;
810 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
811 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
812 }
813
Damien George99886182015-04-06 22:38:53 +0100814 mp_emit_glue_assign_native(emit->scope->raw_code,
815 emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY,
816 f, f_len, emit->scope->num_pos_args, emit->scope->num_kwonly_args,
817 emit->scope->scope_flags, type_sig);
Damien13ed3a62013-10-08 09:05:10 +0100818 }
819}
820
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200821STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100822 return emit->last_emit_was_return_value;
823}
824
Damien Georged6230f62014-09-23 14:10:03 +0000825STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georged6230f62014-09-23 14:10:03 +0000826 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100827 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100828 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100829 emit->scope->stack_size = emit->stack_size;
830 }
Damien George21ca2d72014-10-19 19:00:51 +0100831#ifdef DEBUG_PRINT
832 DEBUG_printf(" adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
833 for (int i = 0; i < emit->stack_size; i++) {
834 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000835 DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->data.u_reg);
Damien George21ca2d72014-10-19 19:00:51 +0100836 }
837 DEBUG_printf("\n");
838#endif
Damien13ed3a62013-10-08 09:05:10 +0100839}
840
Damien Georged6230f62014-09-23 14:10:03 +0000841STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien George21ca2d72014-10-19 19:00:51 +0100842 DEBUG_printf("adjust_stack_size(" INT_FMT ")\n", delta);
Damien Georged6230f62014-09-23 14:10:03 +0000843 // If we are adjusting the stack in a positive direction (pushing) then we
844 // need to fill in values for the stack kind and vtype of the newly-pushed
845 // entries. These should be set to "value" (ie not reg or imm) because we
846 // should only need to adjust the stack due to a jump to this part in the
847 // code (and hence we have settled the stack before the jump).
848 for (mp_int_t i = 0; i < delta; i++) {
849 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
850 si->kind = STACK_VALUE;
Damien George21ca2d72014-10-19 19:00:51 +0100851 // TODO we don't know the vtype to use here. At the moment this is a
852 // hack to get the case of multi comparison working.
853 if (delta == 1) {
854 si->vtype = emit->saved_stack_vtype;
855 } else {
856 si->vtype = VTYPE_PYOBJ;
857 }
Damien Georged6230f62014-09-23 14:10:03 +0000858 }
859 adjust_stack(emit, delta);
860}
861
862STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000863 (void)emit;
864 (void)source_line;
Damien Georged6230f62014-09-23 14:10:03 +0000865}
866
Damienff8ed772013-10-08 22:18:32 +0100867/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200868STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100869 adjust_stack(emit, stack_size_delta);
870 emit->last_emit_was_return_value = false;
871}
Damienff8ed772013-10-08 22:18:32 +0100872*/
Damien13ed3a62013-10-08 09:05:10 +0100873
Damienff8ed772013-10-08 22:18:32 +0100874// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000875STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100876 emit->last_emit_was_return_value = false;
877 // settle the stack
878 /*
879 if (regs_needed != 0) {
880 for (int i = 0; i < emit->stack_size; i++) {
881 switch (emit->stack_info[i].kind) {
882 case STACK_VALUE:
883 break;
884
885 case STACK_REG:
886 // TODO only push reg if in regs_needed
887 emit->stack_info[i].kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000888 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100889 break;
890
891 case STACK_IMM:
892 // don't think we ever need to push imms for settling
893 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
894 break;
895 }
896 }
897 }
898 */
Damien13ed3a62013-10-08 09:05:10 +0100899}
900
Damien George3112cde2014-09-29 18:45:42 +0100901// depth==0 is top, depth==1 is before top, etc
902STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
903 return &emit->stack_info[emit->stack_size - 1 - depth];
904}
905
906// depth==0 is top, depth==1 is before top, etc
907STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
908 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100909}
Damien13ed3a62013-10-08 09:05:10 +0100910
Damiend2755ec2013-10-16 23:58:48 +0100911// pos=1 is TOS, pos=2 is next, etc
912// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200913STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100914 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100915 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100916 if (i != skip_stack_pos) {
917 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000918 if (si->kind == STACK_REG && si->data.u_reg == reg_needed) {
Damiend2755ec2013-10-16 23:58:48 +0100919 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000920 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100921 }
Damienff8ed772013-10-08 22:18:32 +0100922 }
923 }
924}
Damien13ed3a62013-10-08 09:05:10 +0100925
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200926STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100927 for (int i = 0; i < emit->stack_size; i++) {
928 stack_info_t *si = &emit->stack_info[i];
929 if (si->kind == STACK_REG) {
930 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000931 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100932 }
Damien13ed3a62013-10-08 09:05:10 +0100933 }
934}
935
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200936STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +0000937 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +0100938 for (int i = 0; i < emit->stack_size; i++) {
939 stack_info_t *si = &emit->stack_info[i];
940 if (si->kind == STACK_REG) {
Damien George32444b72015-01-24 23:14:12 +0000941 DEBUG_printf(" reg(%u) to local(%u)\n", si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100942 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000943 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100944 }
945 }
946 for (int i = 0; i < emit->stack_size; i++) {
947 stack_info_t *si = &emit->stack_info[i];
948 if (si->kind == STACK_IMM) {
Damien George32444b72015-01-24 23:14:12 +0000949 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->data.u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +0100950 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000951 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->data.u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +0100952 }
953 }
954}
955
956// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200957STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100958 need_reg_single(emit, reg_dest, pos);
959 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +0100960 *vtype = si->vtype;
961 switch (si->kind) {
962 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +0100963 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100964 break;
965
Damienff8ed772013-10-08 22:18:32 +0100966 case STACK_REG:
Damien George32444b72015-01-24 23:14:12 +0000967 if (si->data.u_reg != reg_dest) {
968 ASM_MOV_REG_REG(emit->as, reg_dest, si->data.u_reg);
Damien13ed3a62013-10-08 09:05:10 +0100969 }
970 break;
971
Damienff8ed772013-10-08 22:18:32 +0100972 case STACK_IMM:
Damien George32444b72015-01-24 23:14:12 +0000973 ASM_MOV_IMM_TO_REG(emit->as, si->data.u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100974 break;
975 }
Damien13ed3a62013-10-08 09:05:10 +0100976}
977
Damien Georgee9dac3b2014-09-29 22:10:41 +0100978// does an efficient X=pop(); discard(); push(X)
979// needs a (non-temp) register in case the poped element was stored in the stack
980STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
981 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
982 si[0] = si[1];
983 if (si->kind == STACK_VALUE) {
984 // if folded element was on the stack we need to put it in a register
985 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
986 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +0000987 si->data.u_reg = reg_dest;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100988 }
989 adjust_stack(emit, -1);
990}
991
992// If stacked value is in a register and the register is not r1 or r2, then
993// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
994STATIC void emit_pre_pop_reg_flexible(emit_t *emit, vtype_kind_t *vtype, int *reg_dest, int not_r1, int not_r2) {
Damien George3112cde2014-09-29 18:45:42 +0100995 emit->last_emit_was_return_value = false;
996 stack_info_t *si = peek_stack(emit, 0);
Damien George32444b72015-01-24 23:14:12 +0000997 if (si->kind == STACK_REG && si->data.u_reg != not_r1 && si->data.u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +0100998 *vtype = si->vtype;
Damien George32444b72015-01-24 23:14:12 +0000999 *reg_dest = si->data.u_reg;
Damien George3112cde2014-09-29 18:45:42 +01001000 need_reg_single(emit, *reg_dest, 1);
1001 } else {
1002 emit_access_stack(emit, 1, vtype, *reg_dest);
1003 }
1004 adjust_stack(emit, -1);
1005}
1006
Damien Georgee6ce10a2014-09-06 18:38:20 +01001007STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +01001008 emit->last_emit_was_return_value = false;
1009 adjust_stack(emit, -1);
1010}
1011
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001012STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +01001013 emit->last_emit_was_return_value = false;
1014 emit_access_stack(emit, 1, vtype, reg_dest);
1015 adjust_stack(emit, -1);
1016}
1017
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001018STATIC 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 +01001019 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001020 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +01001021}
1022
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001023STATIC 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 +01001024 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001025 emit_pre_pop_reg(emit, vtypeb, regb);
1026 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001027}
1028
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001029STATIC void emit_post(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001030 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001031}
1032
Damien Georgee9dac3b2014-09-29 22:10:41 +01001033STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
1034 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
1035 si->vtype = new_vtype;
1036}
1037
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001038STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +01001039 stack_info_t *si = &emit->stack_info[emit->stack_size];
1040 si->vtype = vtype;
1041 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +00001042 si->data.u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +01001043 adjust_stack(emit, 1);
1044}
1045
Damien George40f3c022014-07-03 13:25:24 +01001046STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +01001047 stack_info_t *si = &emit->stack_info[emit->stack_size];
1048 si->vtype = vtype;
1049 si->kind = STACK_IMM;
Damien George32444b72015-01-24 23:14:12 +00001050 si->data.u_imm = imm;
Damienff8ed772013-10-08 22:18:32 +01001051 adjust_stack(emit, 1);
1052}
1053
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001054STATIC 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 +01001055 emit_post_push_reg(emit, vtypea, rega);
1056 emit_post_push_reg(emit, vtypeb, regb);
1057}
1058
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001059STATIC 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 +01001060 emit_post_push_reg(emit, vtypea, rega);
1061 emit_post_push_reg(emit, vtypeb, regb);
1062 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001063}
1064
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001065STATIC 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 +01001066 emit_post_push_reg(emit, vtypea, rega);
1067 emit_post_push_reg(emit, vtypeb, regb);
1068 emit_post_push_reg(emit, vtypec, regc);
1069 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +01001070}
1071
Damien George7fe21912014-08-16 22:31:57 +01001072STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +01001073 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001074 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001075}
1076
Damien George7fe21912014-08-16 22:31:57 +01001077STATIC 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 +01001078 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001079 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
1080 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001081}
1082
Damien George40f3c022014-07-03 13:25:24 +01001083// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001084STATIC 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 +01001085 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001086 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
1087 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +01001088}
1089
Damien George7fe21912014-08-16 22:31:57 +01001090STATIC 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 +00001091 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001092 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1093 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1094 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +00001095}
1096
Damien George40f3c022014-07-03 13:25:24 +01001097// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001098STATIC 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 +01001099 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001100 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1101 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1102 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
1103 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +01001104}
1105
Damien George86de21b2014-08-16 22:06:11 +01001106// vtype of all n_pop objects is VTYPE_PYOBJ
1107// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
1108// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
1109// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
1110STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
1111 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +01001112
Damien George86de21b2014-08-16 22:06:11 +01001113 // First, store any immediate values to their respective place on the stack.
1114 for (mp_uint_t i = 0; i < n_pop; i++) {
1115 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1116 // must push any imm's to stack
1117 // must convert them to VTYPE_PYOBJ for viper code
1118 if (si->kind == STACK_IMM) {
1119 si->kind = STACK_VALUE;
1120 switch (si->vtype) {
1121 case VTYPE_PYOBJ:
Damien George32444b72015-01-24 23:14:12 +00001122 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->data.u_imm, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001123 break;
1124 case VTYPE_BOOL:
Damien George32444b72015-01-24 23:14:12 +00001125 if (si->data.u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +01001126 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 +01001127 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +01001128 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 +01001129 }
1130 si->vtype = VTYPE_PYOBJ;
1131 break;
1132 case VTYPE_INT:
1133 case VTYPE_UINT:
Damien George32444b72015-01-24 23:14:12 +00001134 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, (si->data.u_imm << 1) | 1, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001135 si->vtype = VTYPE_PYOBJ;
1136 break;
1137 default:
1138 // not handled
1139 assert(0);
1140 }
1141 }
1142
1143 // verify that this value is on the stack
1144 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +01001145 }
Damien George86de21b2014-08-16 22:06:11 +01001146
1147 // Second, convert any non-VTYPE_PYOBJ to that type.
1148 for (mp_uint_t i = 0; i < n_pop; i++) {
1149 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1150 if (si->vtype != VTYPE_PYOBJ) {
1151 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +01001152 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001153 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 +01001154 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001155 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001156 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001157 }
1158 }
1159
1160 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1161 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +01001162 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001163}
1164
1165// vtype of all n_push objects is VTYPE_PYOBJ
1166STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1167 need_reg_all(emit);
1168 for (mp_uint_t i = 0; i < n_push; i++) {
1169 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1170 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1171 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001172 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001173 adjust_stack(emit, n_push);
1174}
1175
Damien George7ff996c2014-09-08 23:05:16 +01001176STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001177 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001178 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001179 // need to commit stack because we can jump here from elsewhere
1180 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001181 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001182 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001183}
1184
Damien Georgecdd96df2014-04-06 12:58:40 +01001185STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1186 DEBUG_printf("import_name %s\n", qstr_str(qst));
1187 vtype_kind_t vtype_fromlist;
1188 vtype_kind_t vtype_level;
1189 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1190 assert(vtype_fromlist == VTYPE_PYOBJ);
1191 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001192 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001193 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001194}
1195
Damien Georgecdd96df2014-04-06 12:58:40 +01001196STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1197 DEBUG_printf("import_from %s\n", qstr_str(qst));
1198 emit_native_pre(emit);
1199 vtype_kind_t vtype_module;
1200 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1201 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001202 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001203 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001204}
1205
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001206STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001207 DEBUG_printf("import_star\n");
1208 vtype_kind_t vtype_module;
1209 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1210 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001211 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001212 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001213}
1214
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001215STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001216 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001217 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001218 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001219 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001220 if (emit->do_viper_types) {
1221 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001222 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1223 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1224 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien George3da677e2015-01-29 15:13:40 +00001225 no_other_choice1:
1226 case MP_TOKEN_ELLIPSIS: vtype = VTYPE_PYOBJ; val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1227 default: assert(0); goto no_other_choice1; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001228 }
1229 } else {
1230 vtype = VTYPE_PYOBJ;
1231 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001232 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1233 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1234 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien George3da677e2015-01-29 15:13:40 +00001235 no_other_choice2:
1236 case MP_TOKEN_ELLIPSIS: val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1237 default: assert(0); goto no_other_choice2; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001238 }
1239 }
1240 emit_post_push_imm(emit, vtype, val);
1241}
1242
Damien George40f3c022014-07-03 13:25:24 +01001243STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001244 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001245 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001246 if (emit->do_viper_types) {
1247 emit_post_push_imm(emit, VTYPE_INT, arg);
1248 } else {
Damien George2686f9b2015-04-01 00:12:43 +01001249 emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_SMALL_INT(arg));
Damien13ed3a62013-10-08 09:05:10 +01001250 }
1251}
1252
Damien George7ff996c2014-09-08 23:05:16 +01001253STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001254 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001255 // TODO: Eventually we want to be able to work with raw pointers in viper to
1256 // do native array access. For now we just load them as any other object.
1257 /*
Damien13ed3a62013-10-08 09:05:10 +01001258 if (emit->do_viper_types) {
1259 // not implemented properly
1260 // load a pointer to the asciiz string?
1261 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001262 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001263 } else
1264 */
1265 {
Damien Georgeb601d952014-06-30 05:17:25 +01001266 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +01001267 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_BYTES, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001268 } else {
Damien George7ff996c2014-09-08 23:05:16 +01001269 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001270 }
Damien13ed3a62013-10-08 09:05:10 +01001271 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1272 }
1273}
1274
Damien Georgedab13852015-01-13 15:55:54 +00001275STATIC void emit_native_load_const_obj(emit_t *emit, void *obj) {
1276 emit_native_pre(emit);
Damien George2127e9a2015-01-14 00:11:09 +00001277 need_reg_single(emit, REG_RET, 0);
Damien Georgedab13852015-01-13 15:55:54 +00001278 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)obj, REG_RET);
1279 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1280}
1281
Damien George3558f622014-04-20 17:50:40 +01001282STATIC void emit_native_load_null(emit_t *emit) {
1283 emit_native_pre(emit);
1284 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1285}
1286
Damien George0abb5602015-01-16 12:24:49 +00001287STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
1288 DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
Damien13ed3a62013-10-08 09:05:10 +01001289 vtype_kind_t vtype = emit->local_vtype[local_num];
1290 if (vtype == VTYPE_UNBOUND) {
Damien George7ff996c2014-09-08 23:05:16 +01001291 printf("ViperTypeError: local %s used before type known\n", qstr_str(qst));
Damien13ed3a62013-10-08 09:05:10 +01001292 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001293 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001294 if (local_num == 0) {
1295 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001296 } else if (local_num == 1) {
1297 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1298 } else if (local_num == 2) {
1299 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001300 } else {
Damien George0b610de2014-09-29 16:25:04 +01001301 need_reg_single(emit, REG_TEMP0, 0);
Damien George99886182015-04-06 22:38:53 +01001302 if (emit->do_viper_types) {
1303 ASM_MOV_LOCAL_TO_REG(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1304 } else {
1305 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - local_num, REG_TEMP0);
1306 }
Damien George0b610de2014-09-29 16:25:04 +01001307 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001308 }
Damien13ed3a62013-10-08 09:05:10 +01001309}
1310
Damien George7ff996c2014-09-08 23:05:16 +01001311STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001312 DEBUG_printf("load_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1313 need_reg_single(emit, REG_RET, 0);
1314 emit_native_load_fast(emit, qst, local_num);
1315 vtype_kind_t vtype;
1316 int reg_base = REG_RET;
1317 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1318 ASM_LOAD_REG_REG_OFFSET(emit->as, REG_RET, reg_base, 1);
1319 // closed over vars are always Python objects
1320 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien9ecbcff2013-12-11 00:41:43 +00001321}
1322
Damien George7ff996c2014-09-08 23:05:16 +01001323STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001324 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001325 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001326 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001327 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1328}
1329
Damien George7ff996c2014-09-08 23:05:16 +01001330STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001331 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001332 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001333 // check for builtin casting operators
1334 if (emit->do_viper_types && qst == MP_QSTR_int) {
1335 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1336 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1337 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1338 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1339 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1340 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1341 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1342 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1343 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
1344 } else {
1345 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1346 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1347 }
Damien13ed3a62013-10-08 09:05:10 +01001348}
1349
Damien George7ff996c2014-09-08 23:05:16 +01001350STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001351 // depends on type of subject:
1352 // - integer, function, pointer to integers: error
1353 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001354 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001355 vtype_kind_t vtype_base;
1356 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1357 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001358 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001359 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1360}
1361
Damien George7ff996c2014-09-08 23:05:16 +01001362STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001363 vtype_kind_t vtype_base;
1364 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1365 assert(vtype_base == VTYPE_PYOBJ);
1366 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001367 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001368}
1369
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001370STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001371 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001372 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001373 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001374}
1375
Damien George729f7b42014-04-17 22:10:53 +01001376STATIC void emit_native_load_subscr(emit_t *emit) {
Damien George91cfd412014-10-12 16:59:29 +01001377 DEBUG_printf("load_subscr\n");
1378 // need to compile: base[index]
1379
1380 // pop: index, base
1381 // optimise case where index is an immediate
1382 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1383
1384 if (vtype_base == VTYPE_PYOBJ) {
1385 // standard Python call
1386 vtype_kind_t vtype_index;
1387 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1);
1388 assert(vtype_index == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001389 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 +01001390 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1391 } else {
Damien George91cfd412014-10-12 16:59:29 +01001392 // viper load
1393 // TODO The different machine architectures have very different
1394 // capabilities and requirements for loads, so probably best to
1395 // write a completely separate load-optimiser for each one.
1396 stack_info_t *top = peek_stack(emit, 0);
1397 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1398 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001399 mp_int_t index_value = top->data.u_imm;
Damien George91cfd412014-10-12 16:59:29 +01001400 emit_pre_pop_discard(emit); // discard index
1401 int reg_base = REG_ARG_1;
1402 int reg_index = REG_ARG_2;
1403 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
1404 switch (vtype_base) {
1405 case VTYPE_PTR8: {
1406 // pointer to 8-bit memory
1407 // TODO optimise to use thumb ldrb r1, [r2, r3]
1408 if (index_value != 0) {
1409 // index is non-zero
1410 #if N_THUMB
1411 if (index_value > 0 && index_value < 32) {
1412 asm_thumb_ldrb_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1413 break;
1414 }
1415 #endif
1416 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
1417 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1418 reg_base = reg_index;
1419 }
1420 ASM_LOAD8_REG_REG(emit->as, REG_RET, reg_base); // load from (base+index)
1421 break;
1422 }
1423 case VTYPE_PTR16: {
1424 // pointer to 16-bit memory
1425 if (index_value != 0) {
1426 // index is a non-zero immediate
1427 #if N_THUMB
1428 if (index_value > 0 && index_value < 32) {
1429 asm_thumb_ldrh_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1430 break;
1431 }
1432 #endif
1433 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
1434 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1435 reg_base = reg_index;
1436 }
1437 ASM_LOAD16_REG_REG(emit->as, REG_RET, reg_base); // load from (base+2*index)
1438 break;
1439 }
1440 default:
1441 printf("ViperTypeError: can't load from type %d\n", vtype_base);
1442 }
1443 } else {
1444 // index is not an immediate
1445 vtype_kind_t vtype_index;
1446 int reg_index = REG_ARG_2;
1447 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
1448 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1449 switch (vtype_base) {
1450 case VTYPE_PTR8: {
1451 // pointer to 8-bit memory
1452 // TODO optimise to use thumb ldrb r1, [r2, r3]
1453 assert(vtype_index == VTYPE_INT);
1454 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1455 ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
1456 break;
1457 }
1458 case VTYPE_PTR16: {
1459 // pointer to 16-bit memory
1460 assert(vtype_index == VTYPE_INT);
1461 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1462 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1463 ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
1464 break;
1465 }
1466 default:
1467 printf("ViperTypeError: can't load from type %d\n", vtype_base);
1468 }
1469 }
1470 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
Damien George729f7b42014-04-17 22:10:53 +01001471 }
1472}
1473
Damien George7ff996c2014-09-08 23:05:16 +01001474STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001475 vtype_kind_t vtype;
Damien13ed3a62013-10-08 09:05:10 +01001476 if (local_num == 0) {
1477 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001478 } else if (local_num == 1) {
1479 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1480 } else if (local_num == 2) {
1481 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001482 } else {
Damien George0b610de2014-09-29 16:25:04 +01001483 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
Damien George99886182015-04-06 22:38:53 +01001484 if (emit->do_viper_types) {
1485 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1486 } else {
1487 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, STATE_START + emit->n_state - 1 - local_num);
1488 }
Damien13ed3a62013-10-08 09:05:10 +01001489 }
Damien13ed3a62013-10-08 09:05:10 +01001490 emit_post(emit);
1491
1492 // check types
1493 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1494 // first time this local is assigned, so give it a type of the object stored in it
1495 emit->local_vtype[local_num] = vtype;
1496 } else if (emit->local_vtype[local_num] != vtype) {
1497 // type of local is not the same as object stored in it
Damien George7ff996c2014-09-08 23:05:16 +01001498 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 +01001499 }
1500}
1501
Damien George7ff996c2014-09-08 23:05:16 +01001502STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001503 DEBUG_printf("store_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1504 need_reg_single(emit, REG_TEMP0, 0);
1505 need_reg_single(emit, REG_TEMP1, 0);
1506 emit_native_load_fast(emit, qst, local_num);
1507 vtype_kind_t vtype;
1508 int reg_base = REG_TEMP0;
1509 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1510 int reg_src = REG_TEMP1;
1511 emit_pre_pop_reg_flexible(emit, &vtype, &reg_src, reg_base, reg_base);
1512 ASM_STORE_REG_REG_OFFSET(emit->as, reg_src, reg_base, 1);
1513 emit_post(emit);
Damien9ecbcff2013-12-11 00:41:43 +00001514}
1515
Damien George7ff996c2014-09-08 23:05:16 +01001516STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001517 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001518 vtype_kind_t vtype;
1519 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1520 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001521 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001522 emit_post(emit);
1523}
1524
Damien George7ff996c2014-09-08 23:05:16 +01001525STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001526 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001527 if (vtype == VTYPE_PYOBJ) {
1528 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1529 } else {
1530 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001531 emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, vtype, REG_ARG_2); // arg2 = type
Damien George3112cde2014-09-29 18:45:42 +01001532 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001533 }
Damien George7ff996c2014-09-08 23:05:16 +01001534 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001535 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001536}
1537
Damien George7ff996c2014-09-08 23:05:16 +01001538STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001539 vtype_kind_t vtype_base, vtype_val;
1540 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1541 assert(vtype_base == VTYPE_PYOBJ);
1542 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001543 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001544 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001545}
1546
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001547STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001548 DEBUG_printf("store_subscr\n");
1549 // need to compile: base[index] = value
1550
1551 // pop: index, base, value
1552 // optimise case where index is an immediate
1553 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1554
1555 if (vtype_base == VTYPE_PYOBJ) {
1556 // standard Python call
1557 vtype_kind_t vtype_index, vtype_value;
1558 emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3);
1559 assert(vtype_index == VTYPE_PYOBJ);
1560 assert(vtype_value == VTYPE_PYOBJ);
1561 emit_call(emit, MP_F_OBJ_SUBSCR);
1562 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001563 // viper store
1564 // TODO The different machine architectures have very different
1565 // capabilities and requirements for stores, so probably best to
1566 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001567 stack_info_t *top = peek_stack(emit, 0);
1568 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1569 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001570 mp_int_t index_value = top->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001571 emit_pre_pop_discard(emit); // discard index
1572 vtype_kind_t vtype_value;
1573 int reg_base = REG_ARG_1;
1574 int reg_index = REG_ARG_2;
1575 int reg_value = REG_ARG_3;
1576 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001577 #if N_X86
1578 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1579 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1580 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001581 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001582 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001583 switch (vtype_base) {
1584 case VTYPE_PTR8: {
1585 // pointer to 8-bit memory
1586 // TODO optimise to use thumb strb r1, [r2, r3]
1587 if (index_value != 0) {
1588 // index is non-zero
1589 #if N_THUMB
1590 if (index_value > 0 && index_value < 32) {
1591 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1592 break;
1593 }
1594 #endif
1595 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001596 #if N_ARM
1597 asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1598 return;
1599 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001600 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1601 reg_base = reg_index;
1602 }
1603 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1604 break;
1605 }
1606 case VTYPE_PTR16: {
1607 // pointer to 16-bit memory
1608 if (index_value != 0) {
1609 // index is a non-zero immediate
1610 #if N_THUMB
1611 if (index_value > 0 && index_value < 32) {
1612 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1613 break;
1614 }
1615 #endif
1616 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001617 #if N_ARM
1618 asm_arm_strh_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1619 return;
1620 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001621 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1622 reg_base = reg_index;
1623 }
1624 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1625 break;
1626 }
1627 default:
1628 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1629 }
1630 } else {
1631 // index is not an immediate
1632 vtype_kind_t vtype_index, vtype_value;
1633 int reg_index = REG_ARG_2;
1634 int reg_value = REG_ARG_3;
1635 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1636 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001637 #if N_X86
1638 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1639 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1640 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001641 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001642 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001643 switch (vtype_base) {
1644 case VTYPE_PTR8: {
1645 // pointer to 8-bit memory
1646 // TODO optimise to use thumb strb r1, [r2, r3]
1647 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001648 #if N_ARM
1649 asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1650 break;
1651 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001652 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1653 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1654 break;
1655 }
1656 case VTYPE_PTR16: {
1657 // pointer to 16-bit memory
1658 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001659 #if N_ARM
1660 asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1661 break;
1662 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001663 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1664 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1665 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1666 break;
1667 }
1668 default:
1669 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1670 }
1671 }
1672
1673 }
Damien13ed3a62013-10-08 09:05:10 +01001674}
1675
Damien George7ff996c2014-09-08 23:05:16 +01001676STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George2cc54732015-04-03 14:29:30 +01001677 // TODO: This is not compliant implementation. We could use MP_OBJ_SENTINEL
1678 // to mark deleted vars but then every var would need to be checked on
1679 // each access. Very inefficient, so just set value to None to enable GC.
1680 emit_native_load_const_tok(emit, MP_TOKEN_KW_NONE);
1681 emit_native_store_fast(emit, qst, local_num);
Damien13ed3a62013-10-08 09:05:10 +01001682}
1683
Damien George7ff996c2014-09-08 23:05:16 +01001684STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001685 // TODO implement me!
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001686 (void)emit;
1687 (void)qst;
1688 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001689}
1690
Damien Georgee6ce10a2014-09-06 18:38:20 +01001691STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1692 emit_native_pre(emit);
1693 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1694 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001695}
1696
Damien Georgee6ce10a2014-09-06 18:38:20 +01001697STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1698 emit_native_pre(emit);
1699 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1700 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001701}
1702
Damien Georgee6ce10a2014-09-06 18:38:20 +01001703STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001704 vtype_kind_t vtype_base;
1705 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1706 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001707 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 +01001708 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001709}
1710
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001711STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001712 vtype_kind_t vtype_index, vtype_base;
1713 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1714 assert(vtype_index == VTYPE_PYOBJ);
1715 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001716 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 +01001717}
1718
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001719STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001720 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001721 vtype_kind_t vtype;
Damien George21ca2d72014-10-19 19:00:51 +01001722 int reg = REG_TEMP0;
1723 emit_pre_pop_reg_flexible(emit, &vtype, &reg, -1, -1);
1724 emit_post_push_reg_reg(emit, vtype, reg, vtype, reg);
Damien13ed3a62013-10-08 09:05:10 +01001725}
1726
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001727STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001728 vtype_kind_t vtype0, vtype1;
1729 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1730 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1731}
1732
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001733STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001734 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001735 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001736 emit_post(emit);
1737}
1738
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001739STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001740 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001741 vtype_kind_t vtype0, vtype1;
1742 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1743 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001744}
1745
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001746STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001747 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001748 vtype_kind_t vtype0, vtype1, vtype2;
1749 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1750 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1751}
1752
Damien George7ff996c2014-09-08 23:05:16 +01001753STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001754 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001755 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001756 // need to commit stack because we are jumping elsewhere
1757 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001758 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001759 emit_post(emit);
1760}
1761
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001762STATIC void emit_native_jump_helper(emit_t *emit, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001763 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien George6f813482014-09-29 16:41:37 +01001764 switch (vtype) {
1765 case VTYPE_PYOBJ:
1766 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1767 if (!pop) {
1768 adjust_stack(emit, 1);
1769 }
1770 emit_call(emit, MP_F_OBJ_IS_TRUE);
1771 break;
1772 case VTYPE_BOOL:
1773 case VTYPE_INT:
1774 case VTYPE_UINT:
1775 emit_pre_pop_reg(emit, &vtype, REG_RET);
1776 if (!pop) {
1777 adjust_stack(emit, 1);
1778 }
1779 break;
1780 default:
1781 printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
1782 assert(0);
Damien13ed3a62013-10-08 09:05:10 +01001783 }
Damien George21ca2d72014-10-19 19:00:51 +01001784 // For non-pop need to save the vtype so that emit_native_adjust_stack_size
1785 // can use it. This is a bit of a hack.
1786 if (!pop) {
1787 emit->saved_stack_vtype = vtype;
1788 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001789 // need to commit stack because we may jump elsewhere
1790 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001791}
1792
Damien George63f38322015-02-28 15:04:06 +00001793STATIC void emit_native_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
1794 DEBUG_printf("pop_jump_if(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001795 emit_native_jump_helper(emit, true);
Damien George63f38322015-02-28 15:04:06 +00001796 if (cond) {
1797 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1798 } else {
1799 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1800 }
Damien1a6633a2013-11-03 13:58:19 +00001801 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001802}
Damien1a6633a2013-11-03 13:58:19 +00001803
Damien George63f38322015-02-28 15:04:06 +00001804STATIC void emit_native_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
1805 DEBUG_printf("jump_if_or_pop(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001806 emit_native_jump_helper(emit, false);
Damien George63f38322015-02-28 15:04:06 +00001807 if (cond) {
1808 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1809 } else {
1810 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1811 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001812 adjust_stack(emit, -1);
1813 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001814}
1815
Damien George7ff996c2014-09-08 23:05:16 +01001816STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001817 (void)except_depth;
Damien George25c84642014-05-30 15:20:41 +01001818 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001819}
Damien Georgea32c1e42014-05-07 18:30:52 +01001820
Damien George7ff996c2014-09-08 23:05:16 +01001821STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001822 (void)except_depth;
Damien Georgea32c1e42014-05-07 18:30:52 +01001823 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001824}
Damien Georgea32c1e42014-05-07 18:30:52 +01001825
Damien George7ff996c2014-09-08 23:05:16 +01001826STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001827 // not supported, or could be with runtime call
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001828 (void)emit;
1829 (void)label;
Damien13ed3a62013-10-08 09:05:10 +01001830 assert(0);
1831}
Damien Georgeb601d952014-06-30 05:17:25 +01001832
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001833STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001834 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001835 assert(0);
1836}
Damien Georgeb601d952014-06-30 05:17:25 +01001837
Damien George7ff996c2014-09-08 23:05:16 +01001838STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001839 emit_native_pre(emit);
1840 // need to commit stack because we may jump elsewhere
1841 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001842 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 +01001843 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001844 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001845 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001846}
Damien Georgeb601d952014-06-30 05:17:25 +01001847
Damien George7ff996c2014-09-08 23:05:16 +01001848STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001849 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001850}
Damien Georgeb601d952014-06-30 05:17:25 +01001851
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001852STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgeb6e6b522015-01-21 17:00:01 +00001853 // logic:
1854 // exc = pop_stack
1855 // if exc == None: pass
1856 // else: raise exc
1857 // the check if exc is None is done in the MP_F_NATIVE_RAISE stub
1858 vtype_kind_t vtype;
1859 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1860 emit_call(emit, MP_F_NATIVE_RAISE);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001861 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001862}
Damiend2755ec2013-10-16 23:58:48 +01001863
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001864STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001865 // perhaps the difficult one, as we want to rewrite for loops using native code
1866 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001867
1868 vtype_kind_t vtype;
1869 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1870 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001871 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01001872 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001873}
Damiend2755ec2013-10-16 23:58:48 +01001874
Damien George7ff996c2014-09-08 23:05:16 +01001875STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001876 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001877 vtype_kind_t vtype;
1878 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1879 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001880 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01001881 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
1882 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01001883 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1884}
1885
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001886STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001887 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001888 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001889 adjust_stack(emit, -1);
1890 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001891}
1892
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001893STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001894 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001895 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001896 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001897 emit_post(emit);
1898}
1899
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001900STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001901 (void)emit;
Damien Georgeb601d952014-06-30 05:17:25 +01001902 /*
1903 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001904 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001905 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01001906 emit_post(emit);
1907 */
Damien13ed3a62013-10-08 09:05:10 +01001908}
1909
Damien Georged17926d2014-03-30 13:35:08 +01001910STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001911 vtype_kind_t vtype;
1912 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1913 assert(vtype == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001914 if (op == MP_UNARY_OP_NOT) {
Damien Georged6230f62014-09-23 14:10:03 +00001915 // we need to synthesise this operation by converting to bool first
1916 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
Damien George3112cde2014-09-29 18:45:42 +01001917 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgeb601d952014-06-30 05:17:25 +01001918 }
Damien Georged6230f62014-09-23 14:10:03 +00001919 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1920 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001921}
1922
Damien Georged17926d2014-03-30 13:35:08 +01001923STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001924 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01001925 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
1926 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01001927 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01001928 #if N_X64 || N_X86
1929 // special cases for x86 and shifting
1930 if (op == MP_BINARY_OP_LSHIFT
1931 || op == MP_BINARY_OP_INPLACE_LSHIFT
1932 || op == MP_BINARY_OP_RSHIFT
1933 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1934 #if N_X64
1935 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
1936 #else
1937 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
1938 #endif
1939 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1940 ASM_LSL_REG(emit->as, REG_RET);
1941 } else {
1942 ASM_ASR_REG(emit->as, REG_RET);
1943 }
1944 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
1945 return;
1946 }
1947 #endif
1948 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001949 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001950 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
1951 if (0) {
1952 // dummy
1953 #if !(N_X64 || N_X86)
1954 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1955 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00001956 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001957 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1958 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1959 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1960 #endif
Damien George1ef23482014-10-12 14:21:06 +01001961 } else if (op == MP_BINARY_OP_OR || op == MP_BINARY_OP_INPLACE_OR) {
1962 ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1963 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1964 } else if (op == MP_BINARY_OP_XOR || op == MP_BINARY_OP_INPLACE_XOR) {
1965 ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1966 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1967 } else if (op == MP_BINARY_OP_AND || op == MP_BINARY_OP_INPLACE_AND) {
1968 ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1969 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001970 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
1971 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1972 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1973 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
1974 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1975 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1976 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
1977 // comparison ops are (in enum order):
1978 // MP_BINARY_OP_LESS
1979 // MP_BINARY_OP_MORE
1980 // MP_BINARY_OP_EQUAL
1981 // MP_BINARY_OP_LESS_EQUAL
1982 // MP_BINARY_OP_MORE_EQUAL
1983 // MP_BINARY_OP_NOT_EQUAL
Damien George21ca2d72014-10-19 19:00:51 +01001984 need_reg_single(emit, REG_RET, 0);
Damien George3112cde2014-09-29 18:45:42 +01001985 #if N_X64
1986 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
1987 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
1988 static byte ops[6] = {
1989 ASM_X64_CC_JL,
1990 ASM_X64_CC_JG,
1991 ASM_X64_CC_JE,
1992 ASM_X64_CC_JLE,
1993 ASM_X64_CC_JGE,
1994 ASM_X64_CC_JNE,
1995 };
1996 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1997 #elif N_X86
1998 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
1999 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
2000 static byte ops[6] = {
2001 ASM_X86_CC_JL,
2002 ASM_X86_CC_JG,
2003 ASM_X86_CC_JE,
2004 ASM_X86_CC_JLE,
2005 ASM_X86_CC_JGE,
2006 ASM_X86_CC_JNE,
2007 };
2008 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
2009 #elif N_THUMB
2010 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
2011 static uint16_t ops[6] = {
2012 ASM_THUMB_OP_ITE_GE,
2013 ASM_THUMB_OP_ITE_GT,
2014 ASM_THUMB_OP_ITE_EQ,
2015 ASM_THUMB_OP_ITE_GT,
2016 ASM_THUMB_OP_ITE_GE,
2017 ASM_THUMB_OP_ITE_EQ,
2018 };
2019 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
2020 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
2021 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
2022 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
2023 #elif N_ARM
Fabian Vogte5268962014-10-04 00:53:46 +02002024 asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
2025 static uint ccs[6] = {
2026 ASM_ARM_CC_LT,
2027 ASM_ARM_CC_GT,
2028 ASM_ARM_CC_EQ,
2029 ASM_ARM_CC_LE,
2030 ASM_ARM_CC_GE,
2031 ASM_ARM_CC_NE,
2032 };
2033 asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
Damien George3112cde2014-09-29 18:45:42 +01002034 #else
2035 #error not implemented
2036 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00002037 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
2038 } else {
2039 // TODO other ops not yet implemented
2040 assert(0);
2041 }
Damien13ed3a62013-10-08 09:05:10 +01002042 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01002043 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00002044 bool invert = false;
2045 if (op == MP_BINARY_OP_NOT_IN) {
2046 invert = true;
2047 op = MP_BINARY_OP_IN;
2048 } else if (op == MP_BINARY_OP_IS_NOT) {
2049 invert = true;
2050 op = MP_BINARY_OP_IS;
2051 }
Damien George7fe21912014-08-16 22:31:57 +01002052 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00002053 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01002054 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00002055 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
2056 }
Damien13ed3a62013-10-08 09:05:10 +01002057 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2058 } else {
2059 printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs);
Damien Georgea5190a72014-08-15 22:39:08 +01002060 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002061 }
2062}
2063
Damien George7ff996c2014-09-08 23:05:16 +01002064STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01002065 // for viper: call runtime, with types of args
2066 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00002067 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002068 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002069 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01002070 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01002071}
2072
Damien George7ff996c2014-09-08 23:05:16 +01002073STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002074 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002075 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002076 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002077 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
2078}
2079
Damien George7ff996c2014-09-08 23:05:16 +01002080STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01002081 // only used in list comprehension
2082 vtype_kind_t vtype_list, vtype_item;
2083 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2084 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
2085 assert(vtype_list == VTYPE_PYOBJ);
2086 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002087 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01002088 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002089}
2090
Damien George7ff996c2014-09-08 23:05:16 +01002091STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002092 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002093 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002094 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
2095}
2096
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002097STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002098 vtype_kind_t vtype_key, vtype_value, vtype_map;
2099 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
2100 assert(vtype_key == VTYPE_PYOBJ);
2101 assert(vtype_value == VTYPE_PYOBJ);
2102 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002103 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01002104 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
2105}
2106
Damien George7ff996c2014-09-08 23:05:16 +01002107STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01002108 // only used in list comprehension
2109 vtype_kind_t vtype_map, vtype_key, vtype_value;
2110 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
2111 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
2112 assert(vtype_map == VTYPE_PYOBJ);
2113 assert(vtype_key == VTYPE_PYOBJ);
2114 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002115 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01002116 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002117}
2118
Damien Georgee37dcaa2014-12-27 17:07:16 +00002119#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +01002120STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002121 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002122 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002123 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002124 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
2125}
2126
Damien George7ff996c2014-09-08 23:05:16 +01002127STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01002128 // only used in set comprehension
2129 vtype_kind_t vtype_set, vtype_item;
2130 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2131 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
2132 assert(vtype_set == VTYPE_PYOBJ);
2133 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002134 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01002135 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002136}
Damien Georgee37dcaa2014-12-27 17:07:16 +00002137#endif
Damiend2755ec2013-10-16 23:58:48 +01002138
Damien George83204f32014-12-27 17:20:41 +00002139#if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +01002140STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002141 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01002142 if (n_args == 2) {
2143 vtype_kind_t vtype_start, vtype_stop;
2144 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
2145 assert(vtype_start == VTYPE_PYOBJ);
2146 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002147 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 +01002148 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2149 } else {
2150 assert(n_args == 3);
2151 vtype_kind_t vtype_start, vtype_stop, vtype_step;
2152 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
2153 assert(vtype_start == VTYPE_PYOBJ);
2154 assert(vtype_stop == VTYPE_PYOBJ);
2155 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002156 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01002157 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2158 }
Damien13ed3a62013-10-08 09:05:10 +01002159}
Damien George83204f32014-12-27 17:20:41 +00002160#endif
Damien Georgecdd96df2014-04-06 12:58:40 +01002161
Damien George7ff996c2014-09-08 23:05:16 +01002162STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002163 DEBUG_printf("unpack_sequence %d\n", n_args);
2164 vtype_kind_t vtype_base;
2165 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2166 assert(vtype_base == VTYPE_PYOBJ);
2167 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01002168 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01002169}
Damien Georgecdd96df2014-04-06 12:58:40 +01002170
Damien George7ff996c2014-09-08 23:05:16 +01002171STATIC 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 +01002172 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
2173 vtype_kind_t vtype_base;
2174 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2175 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01002176 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 +01002177 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 +01002178}
2179
Damien George7ff996c2014-09-08 23:05:16 +01002180STATIC 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 +01002181 // 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 +00002182 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01002183 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01002184 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 +01002185 } else {
2186 vtype_kind_t vtype_def_tuple, vtype_def_dict;
2187 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
2188 assert(vtype_def_tuple == VTYPE_PYOBJ);
2189 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002190 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 +01002191 }
Damien13ed3a62013-10-08 09:05:10 +01002192 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2193}
2194
Damien George7ff996c2014-09-08 23:05:16 +01002195STATIC 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) {
Damien George4cd9ced2015-01-15 14:41:41 +00002196 emit_native_pre(emit);
2197 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
2198 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over);
2199 ASM_MOV_IMM_TO_REG(emit->as, n_closed_over, REG_ARG_2);
2200 } else {
2201 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over + 2);
2202 ASM_MOV_IMM_TO_REG(emit->as, 0x100 | n_closed_over, REG_ARG_2);
2203 }
2204 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)scope->raw_code, REG_ARG_1);
2205 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_MAKE_CLOSURE_FROM_RAW_CODE], MP_F_MAKE_CLOSURE_FROM_RAW_CODE);
2206 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002207}
2208
Damien George7ff996c2014-09-08 23:05:16 +01002209STATIC 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 +00002210 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
2211
Damien Georgee9dac3b2014-09-29 22:10:41 +01002212 // TODO: in viper mode, call special runtime routine with type info for args,
2213 // and wanted type info for return, to remove need for boxing/unboxing
2214
Damien Georgee9dac3b2014-09-29 22:10:41 +01002215 emit_native_pre(emit);
2216 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
2217 if (vtype_fun == VTYPE_BUILTIN_CAST) {
2218 // casting operator
2219 assert(n_positional == 1 && n_keyword == 0);
Damien George78772ad2015-04-06 22:48:21 +01002220 assert(!star_flags);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002221 DEBUG_printf(" cast to %d\n", vtype_fun);
Damien George32444b72015-01-24 23:14:12 +00002222 vtype_kind_t vtype_cast = peek_stack(emit, 1)->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01002223 switch (peek_vtype(emit, 0)) {
2224 case VTYPE_PYOBJ: {
2225 vtype_kind_t vtype;
2226 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2227 emit_pre_pop_discard(emit);
2228 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
2229 emit_post_push_reg(emit, vtype_cast, REG_RET);
2230 break;
2231 }
2232 case VTYPE_BOOL:
2233 case VTYPE_INT:
2234 case VTYPE_UINT:
2235 case VTYPE_PTR:
2236 case VTYPE_PTR8:
2237 case VTYPE_PTR16:
2238 case VTYPE_PTR_NONE:
2239 emit_fold_stack_top(emit, REG_ARG_1);
2240 emit_post_top_set_vtype(emit, vtype_cast);
2241 break;
2242 default:
2243 assert(!"TODO: convert obj to int");
2244 }
2245 } else {
Damien13ed3a62013-10-08 09:05:10 +01002246 assert(vtype_fun == VTYPE_PYOBJ);
Damien George78772ad2015-04-06 22:48:21 +01002247 if (star_flags) {
2248 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
2249 // load dummy entry for non-existent pos_seq
2250 emit_native_load_null(emit);
2251 emit_native_rot_two(emit);
2252 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
2253 // load dummy entry for non-existent kw_dict
2254 emit_native_load_null(emit);
2255 }
2256 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 3); // pointer to args
2257 emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW_VAR, 0, REG_ARG_1, n_positional | (n_keyword << 8), REG_ARG_2);
2258 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2259 } else {
2260 if (n_positional != 0 || n_keyword != 0) {
2261 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2262 }
2263 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2264 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2265 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2266 }
Damien Georgecd82e022014-02-02 13:11:48 +00002267 }
Damien13ed3a62013-10-08 09:05:10 +01002268}
2269
Damien George7ff996c2014-09-08 23:05:16 +01002270STATIC void emit_native_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
Damien George78772ad2015-04-06 22:48:21 +01002271 if (star_flags) {
2272 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
2273 // load dummy entry for non-existent pos_seq
2274 emit_native_load_null(emit);
2275 emit_native_rot_two(emit);
2276 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
2277 // load dummy entry for non-existent kw_dict
2278 emit_native_load_null(emit);
2279 }
2280 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 4); // pointer to args
2281 emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW_VAR, 1, REG_ARG_1, n_positional | (n_keyword << 8), REG_ARG_2);
2282 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2283 } else {
2284 emit_native_pre(emit);
2285 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
2286 emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, n_positional, REG_ARG_1, n_keyword, REG_ARG_2);
2287 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2288 }
Damien13ed3a62013-10-08 09:05:10 +01002289}
2290
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002291STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002292 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002293 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002294 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2295 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002296 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002297 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002298 } else {
2299 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002300 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002301 } else {
2302 vtype_kind_t vtype;
2303 emit_pre_pop_reg(emit, &vtype, REG_RET);
2304 if (vtype != emit->return_vtype) {
2305 printf("ViperTypeError: incompatible return type\n");
2306 }
Damien George2ac4af62014-08-15 16:45:41 +01002307 }
Damien13ed3a62013-10-08 09:05:10 +01002308 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002309 vtype_kind_t vtype;
2310 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002311 assert(vtype == VTYPE_PYOBJ);
2312 }
2313 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002314 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2315 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002316}
2317
Damien George7ff996c2014-09-08 23:05:16 +01002318STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002319 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002320 vtype_kind_t vtype_exc;
2321 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2322 if (vtype_exc != VTYPE_PYOBJ) {
2323 printf("ViperTypeError: must raise an object\n");
2324 }
2325 // 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 +01002326 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002327}
Damien Georgeb601d952014-06-30 05:17:25 +01002328
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002329STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002330 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002331 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002332 assert(0);
2333}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002334STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002335 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002336 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002337 assert(0);
2338}
2339
Damien Georgeb601d952014-06-30 05:17:25 +01002340STATIC void emit_native_start_except_handler(emit_t *emit) {
2341 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2342 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2343 // the first 2 elements, so we can get the thrown value.
2344 adjust_stack(emit, 2);
2345 vtype_kind_t vtype_nlr;
2346 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002347 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002348 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
2349}
2350
2351STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002352 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002353}
2354
Damien13ed3a62013-10-08 09:05:10 +01002355const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002356 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002357 emit_native_start_pass,
2358 emit_native_end_pass,
2359 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002360 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002361 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002362
Damien George542bd6b2015-03-26 14:42:40 +00002363 {
2364 emit_native_load_fast,
2365 emit_native_load_deref,
2366 emit_native_load_name,
2367 emit_native_load_global,
2368 },
2369 {
2370 emit_native_store_fast,
2371 emit_native_store_deref,
2372 emit_native_store_name,
2373 emit_native_store_global,
2374 },
2375 {
2376 emit_native_delete_fast,
2377 emit_native_delete_deref,
2378 emit_native_delete_name,
2379 emit_native_delete_global,
2380 },
Damien13ed3a62013-10-08 09:05:10 +01002381
2382 emit_native_label_assign,
2383 emit_native_import_name,
2384 emit_native_import_from,
2385 emit_native_import_star,
2386 emit_native_load_const_tok,
2387 emit_native_load_const_small_int,
Damien13ed3a62013-10-08 09:05:10 +01002388 emit_native_load_const_str,
Damien Georgedab13852015-01-13 15:55:54 +00002389 emit_native_load_const_obj,
Damien George3558f622014-04-20 17:50:40 +01002390 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002391 emit_native_load_attr,
2392 emit_native_load_method,
2393 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002394 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002395 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002396 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002397 emit_native_delete_attr,
2398 emit_native_delete_subscr,
2399 emit_native_dup_top,
2400 emit_native_dup_top_two,
2401 emit_native_pop_top,
2402 emit_native_rot_two,
2403 emit_native_rot_three,
2404 emit_native_jump,
Damien George63f38322015-02-28 15:04:06 +00002405 emit_native_pop_jump_if,
2406 emit_native_jump_if_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002407 emit_native_break_loop,
2408 emit_native_continue_loop,
2409 emit_native_setup_with,
2410 emit_native_with_cleanup,
2411 emit_native_setup_except,
2412 emit_native_setup_finally,
2413 emit_native_end_finally,
2414 emit_native_get_iter,
2415 emit_native_for_iter,
2416 emit_native_for_iter_end,
2417 emit_native_pop_block,
2418 emit_native_pop_except,
2419 emit_native_unary_op,
2420 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002421 emit_native_build_tuple,
2422 emit_native_build_list,
2423 emit_native_list_append,
2424 emit_native_build_map,
2425 emit_native_store_map,
2426 emit_native_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002427 #if MICROPY_PY_BUILTINS_SET
Damien13ed3a62013-10-08 09:05:10 +01002428 emit_native_build_set,
2429 emit_native_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002430 #endif
Damien George83204f32014-12-27 17:20:41 +00002431 #if MICROPY_PY_BUILTINS_SLICE
Damien13ed3a62013-10-08 09:05:10 +01002432 emit_native_build_slice,
Damien George83204f32014-12-27 17:20:41 +00002433 #endif
Damien13ed3a62013-10-08 09:05:10 +01002434 emit_native_unpack_sequence,
2435 emit_native_unpack_ex,
2436 emit_native_make_function,
2437 emit_native_make_closure,
2438 emit_native_call_function,
2439 emit_native_call_method,
2440 emit_native_return_value,
2441 emit_native_raise_varargs,
2442 emit_native_yield_value,
2443 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002444
2445 emit_native_start_except_handler,
2446 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002447};
2448
Damien Georgec90f59e2014-09-06 23:06:36 +01002449#endif