blob: 8acce8323623f93c6501cbec3623593df9c47e58 [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
Damien Georgec8b60f02015-04-20 13:29:31 +0000485#define EMIT_NATIVE_VIPER_TYPE_ERROR(emit, ...) do { \
486 *emit->error_slot = mp_obj_new_exception_msg_varg(&mp_type_ViperTypeError, __VA_ARGS__); \
487 } while (0)
488
Damien13ed3a62013-10-08 09:05:10 +0100489typedef enum {
Damienff8ed772013-10-08 22:18:32 +0100490 STACK_VALUE,
491 STACK_REG,
492 STACK_IMM,
493} stack_info_kind_t;
Damien13ed3a62013-10-08 09:05:10 +0100494
Damien Georgee9dac3b2014-09-29 22:10:41 +0100495// these enums must be distinct and the bottom 2 bits
496// must correspond to the correct MP_NATIVE_TYPE_xxx value
Damien13ed3a62013-10-08 09:05:10 +0100497typedef enum {
Damien Georgee9dac3b2014-09-29 22:10:41 +0100498 VTYPE_PYOBJ = 0x00 | MP_NATIVE_TYPE_OBJ,
499 VTYPE_BOOL = 0x00 | MP_NATIVE_TYPE_BOOL,
500 VTYPE_INT = 0x00 | MP_NATIVE_TYPE_INT,
501 VTYPE_UINT = 0x00 | MP_NATIVE_TYPE_UINT,
502
503 VTYPE_PTR = 0x10 | MP_NATIVE_TYPE_UINT, // pointer to word sized entity
504 VTYPE_PTR8 = 0x20 | MP_NATIVE_TYPE_UINT,
505 VTYPE_PTR16 = 0x30 | MP_NATIVE_TYPE_UINT,
506 VTYPE_PTR_NONE = 0x40 | MP_NATIVE_TYPE_UINT,
507
508 VTYPE_UNBOUND = 0x50 | MP_NATIVE_TYPE_OBJ,
509 VTYPE_BUILTIN_CAST = 0x60 | MP_NATIVE_TYPE_OBJ,
Damien13ed3a62013-10-08 09:05:10 +0100510} vtype_kind_t;
511
Damien Georgec8b60f02015-04-20 13:29:31 +0000512STATIC qstr vtype_to_qstr(vtype_kind_t vtype) {
513 switch (vtype) {
514 case VTYPE_PYOBJ: return MP_QSTR_object;
515 case VTYPE_BOOL: return MP_QSTR_bool;
516 case VTYPE_INT: return MP_QSTR_int;
517 case VTYPE_UINT: return MP_QSTR_uint;
518 case VTYPE_PTR: return MP_QSTR_ptr;
519 case VTYPE_PTR8: return MP_QSTR_ptr8;
520 case VTYPE_PTR16: return MP_QSTR_ptr16;
521 case VTYPE_PTR_NONE: default: return MP_QSTR_None;
522 }
523}
524
Damienff8ed772013-10-08 22:18:32 +0100525typedef struct _stack_info_t {
526 vtype_kind_t vtype;
527 stack_info_kind_t kind;
528 union {
529 int u_reg;
Damien George40f3c022014-07-03 13:25:24 +0100530 mp_int_t u_imm;
Damien George32444b72015-01-24 23:14:12 +0000531 } data;
Damienff8ed772013-10-08 22:18:32 +0100532} stack_info_t;
533
Damien13ed3a62013-10-08 09:05:10 +0100534struct _emit_t {
Damien Georgec8b60f02015-04-20 13:29:31 +0000535 mp_obj_t *error_slot;
Damien13ed3a62013-10-08 09:05:10 +0100536 int pass;
537
538 bool do_viper_types;
Damienff8ed772013-10-08 22:18:32 +0100539
Damien George2ac4af62014-08-15 16:45:41 +0100540 vtype_kind_t return_vtype;
541
Damien George7ff996c2014-09-08 23:05:16 +0100542 mp_uint_t local_vtype_alloc;
Damien13ed3a62013-10-08 09:05:10 +0100543 vtype_kind_t *local_vtype;
Damienff8ed772013-10-08 22:18:32 +0100544
Damien George7ff996c2014-09-08 23:05:16 +0100545 mp_uint_t stack_info_alloc;
Damienff8ed772013-10-08 22:18:32 +0100546 stack_info_t *stack_info;
Damien George21ca2d72014-10-19 19:00:51 +0100547 vtype_kind_t saved_stack_vtype;
Damienff8ed772013-10-08 22:18:32 +0100548
Damien George99886182015-04-06 22:38:53 +0100549 int code_info_size;
550 int code_info_offset;
551 int prelude_offset;
552 int n_state;
Damien13ed3a62013-10-08 09:05:10 +0100553 int stack_start;
554 int stack_size;
555
556 bool last_emit_was_return_value;
557
Damien13ed3a62013-10-08 09:05:10 +0100558 scope_t *scope;
559
Damien Georgec90f59e2014-09-06 23:06:36 +0100560 ASM_T *as;
Damien13ed3a62013-10-08 09:05:10 +0100561};
562
Damien Georgec8b60f02015-04-20 13:29:31 +0000563emit_t *EXPORT_FUN(new)(mp_obj_t *error_slot, mp_uint_t max_num_labels) {
Damien George36db6bc2014-05-07 17:24:22 +0100564 emit_t *emit = m_new0(emit_t, 1);
Damien Georgec8b60f02015-04-20 13:29:31 +0000565 emit->error_slot = error_slot;
Damien Georgec90f59e2014-09-06 23:06:36 +0100566 emit->as = ASM_NEW(max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100567 return emit;
568}
569
Damien George41d02b62014-01-24 22:42:28 +0000570void EXPORT_FUN(free)(emit_t *emit) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100571 ASM_FREE(emit->as, false);
Damien George36db6bc2014-05-07 17:24:22 +0100572 m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
573 m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200574 m_del_obj(emit_t, emit);
575}
576
Damien George2ac4af62014-08-15 16:45:41 +0100577STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
578 switch (op) {
579 case MP_EMIT_NATIVE_TYPE_ENABLE:
580 emit->do_viper_types = arg1;
581 break;
582
583 default: {
584 vtype_kind_t type;
585 switch (arg2) {
586 case MP_QSTR_object: type = VTYPE_PYOBJ; break;
587 case MP_QSTR_bool: type = VTYPE_BOOL; break;
588 case MP_QSTR_int: type = VTYPE_INT; break;
589 case MP_QSTR_uint: type = VTYPE_UINT; break;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100590 case MP_QSTR_ptr: type = VTYPE_PTR; break;
591 case MP_QSTR_ptr8: type = VTYPE_PTR8; break;
592 case MP_QSTR_ptr16: type = VTYPE_PTR16; break;
Damien Georgec8b60f02015-04-20 13:29:31 +0000593 default: EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "unknown type '%q'", arg2); return;
Damien George2ac4af62014-08-15 16:45:41 +0100594 }
595 if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
596 emit->return_vtype = type;
597 } else {
598 assert(arg1 < emit->local_vtype_alloc);
599 emit->local_vtype[arg1] = type;
600 }
601 break;
602 }
603 }
Damien13ed3a62013-10-08 09:05:10 +0100604}
605
Damien Georgefa5950e2015-04-03 15:03:24 +0000606STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest);
Damien George4cd9ced2015-01-15 14:41:41 +0000607STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg);
Damien Georgefa5950e2015-04-03 15:03:24 +0000608STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
Damien George4cd9ced2015-01-15 14:41:41 +0000609STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
Damien Georgefa5950e2015-04-03 15:03:24 +0000610
Damien George99886182015-04-06 22:38:53 +0100611#define STATE_START (sizeof(mp_code_state) / sizeof(mp_uint_t))
612
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200613STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien Georged6230f62014-09-23 14:10:03 +0000614 DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope);
615
Damien13ed3a62013-10-08 09:05:10 +0100616 emit->pass = pass;
617 emit->stack_start = 0;
618 emit->stack_size = 0;
619 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100620 emit->scope = scope;
621
Damien George36db6bc2014-05-07 17:24:22 +0100622 // allocate memory for keeping track of the types of locals
623 if (emit->local_vtype_alloc < scope->num_locals) {
624 emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
625 emit->local_vtype_alloc = scope->num_locals;
Damienff8ed772013-10-08 22:18:32 +0100626 }
Damien George36db6bc2014-05-07 17:24:22 +0100627
628 // allocate memory for keeping track of the objects on the stack
629 // XXX don't know stack size on entry, and it should be maximum over all scopes
Damien George51229af2015-03-26 17:54:12 +0000630 // XXX this is such a big hack and really needs to be fixed
Damienff8ed772013-10-08 22:18:32 +0100631 if (emit->stack_info == NULL) {
Damien George51229af2015-03-26 17:54:12 +0000632 emit->stack_info_alloc = scope->stack_size + 200;
Damienff8ed772013-10-08 22:18:32 +0100633 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100634 }
635
Damien George99886182015-04-06 22:38:53 +0100636 // set default type for return
Damien George2ac4af62014-08-15 16:45:41 +0100637 emit->return_vtype = VTYPE_PYOBJ;
Damien George99886182015-04-06 22:38:53 +0100638
639 // set default type for arguments
640 mp_uint_t num_args = emit->scope->num_pos_args + emit->scope->num_kwonly_args;
641 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
642 num_args += 1;
643 }
644 if (scope->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) {
645 num_args += 1;
646 }
647 for (mp_uint_t i = 0; i < num_args; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100648 emit->local_vtype[i] = VTYPE_PYOBJ;
649 }
Damien Georgea5190a72014-08-15 22:39:08 +0100650
651 // local variables begin unbound, and have unknown type
Damien George99886182015-04-06 22:38:53 +0100652 for (mp_uint_t i = num_args; i < emit->local_vtype_alloc; i++) {
Damien Georgea5190a72014-08-15 22:39:08 +0100653 emit->local_vtype[i] = VTYPE_UNBOUND;
654 }
655
656 // values on stack begin unbound
657 for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100658 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgea5190a72014-08-15 22:39:08 +0100659 emit->stack_info[i].vtype = VTYPE_UNBOUND;
Damien13ed3a62013-10-08 09:05:10 +0100660 }
661
Damien Georgec90f59e2014-09-06 23:06:36 +0100662 ASM_START_PASS(emit->as, pass == MP_PASS_EMIT ? ASM_PASS_EMIT : ASM_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100663
Damien George99886182015-04-06 22:38:53 +0100664 // generate code for entry to function
Damien13ed3a62013-10-08 09:05:10 +0100665
Damien George99886182015-04-06 22:38:53 +0100666 if (emit->do_viper_types) {
667
668 // entry to function
669 int num_locals = 0;
670 if (pass > MP_PASS_SCOPE) {
671 num_locals = scope->num_locals - REG_LOCAL_NUM;
672 if (num_locals < 0) {
673 num_locals = 0;
674 }
675 emit->stack_start = num_locals;
676 num_locals += scope->stack_size;
Damien13ed3a62013-10-08 09:05:10 +0100677 }
Damien George99886182015-04-06 22:38:53 +0100678 ASM_ENTRY(emit->as, num_locals);
679
680 #if N_X86
681 for (int i = 0; i < scope->num_pos_args; i++) {
682 if (i == 0) {
683 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_1);
684 } else if (i == 1) {
685 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_2);
686 } else if (i == 2) {
687 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_3);
688 } else {
689 asm_x86_mov_arg_to_r32(emit->as, i, REG_TEMP0);
690 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, i - REG_LOCAL_NUM);
691 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100692 }
Damien George99886182015-04-06 22:38:53 +0100693 #else
694 for (int i = 0; i < scope->num_pos_args; i++) {
695 if (i == 0) {
696 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
697 } else if (i == 1) {
698 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
699 } else if (i == 2) {
700 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
701 } else if (i == 3) {
702 ASM_MOV_REG_TO_LOCAL(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
703 } else {
704 // TODO not implemented
705 assert(0);
706 }
707 }
708 #endif
709
710 } else {
711 // work out size of state (locals plus stack)
712 emit->n_state = scope->num_locals + scope->stack_size;
713
714 // allocate space on C-stack for code_state structure, which includes state
715 ASM_ENTRY(emit->as, STATE_START + emit->n_state);
716
717 // prepare incoming arguments for call to mp_setup_code_state
718 #if N_X86
719 asm_x86_mov_arg_to_r32(emit->as, 0, REG_ARG_2);
720 asm_x86_mov_arg_to_r32(emit->as, 1, REG_ARG_3);
721 asm_x86_mov_arg_to_r32(emit->as, 2, REG_ARG_4);
722 asm_x86_mov_arg_to_r32(emit->as, 3, REG_ARG_5);
723 #else
724 #if N_THUMB
725 ASM_MOV_REG_REG(emit->as, ASM_THUMB_REG_R4, REG_ARG_4);
726 #else
727 ASM_MOV_REG_REG(emit->as, REG_ARG_5, REG_ARG_4);
728 #endif
729 ASM_MOV_REG_REG(emit->as, REG_ARG_4, REG_ARG_3);
730 ASM_MOV_REG_REG(emit->as, REG_ARG_3, REG_ARG_2);
731 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_ARG_1);
732 #endif
733
734 // set code_state.code_info (offset from start of this function to code_info data)
735 // XXX this encoding may change size
736 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);
737
738 // set code_state.ip (offset from start of this function to prelude info)
739 // XXX this encoding may change size
740 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, emit->prelude_offset, offsetof(mp_code_state, ip) / sizeof(mp_uint_t), REG_ARG_1);
741
742 // set code_state.n_state
743 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, emit->n_state, offsetof(mp_code_state, n_state) / sizeof(mp_uint_t), REG_ARG_1);
744
745 // put address of code_state into first arg
746 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, 0, REG_ARG_1);
747
748 // call mp_setup_code_state to prepare code_state structure
749 #if N_THUMB
750 asm_thumb_op16(emit->as, 0xb400 | (1 << ASM_THUMB_REG_R4)); // push 5th arg
751 asm_thumb_bl_ind(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE, ASM_THUMB_REG_R4);
752 asm_thumb_op16(emit->as, 0xbc00 | (1 << REG_RET)); // pop dummy (was 5th arg)
753 #else
754 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE);
755 #endif
756
757 // cache some locals in registers
758 if (scope->num_locals > 0) {
759 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 0, REG_LOCAL_1);
760 if (scope->num_locals > 1) {
761 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 1, REG_LOCAL_2);
762 if (scope->num_locals > 2) {
763 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 2, REG_LOCAL_3);
764 }
765 }
766 }
767
768 // set the type of closed over variables
769 for (mp_uint_t i = 0; i < scope->id_info_len; i++) {
770 id_info_t *id = &scope->id_info[i];
771 if (id->kind == ID_INFO_KIND_CELL) {
772 emit->local_vtype[id->local_num] = VTYPE_PYOBJ;
773 }
Damien13ed3a62013-10-08 09:05:10 +0100774 }
775 }
776
Damien George99886182015-04-06 22:38:53 +0100777 #if N_THUMB
Damien Georgee9dac3b2014-09-29 22:10:41 +0100778 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100779 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
Damien George99886182015-04-06 22:38:53 +0100780 #endif
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200781
Damien George99886182015-04-06 22:38:53 +0100782 #if N_ARM
Damien Georgee9dac3b2014-09-29 22:10:41 +0100783 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100784 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
Damien George99886182015-04-06 22:38:53 +0100785 #endif
Damien13ed3a62013-10-08 09:05:10 +0100786}
787
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200788STATIC void emit_native_end_pass(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100789 if (!emit->last_emit_was_return_value) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100790 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100791 }
Damien George99886182015-04-06 22:38:53 +0100792
793 if (!emit->do_viper_types) {
794 // write dummy code info (for mp_setup_code_state to parse) and arg names
795 emit->code_info_offset = ASM_GET_CODE_POS(emit->as);
796 ASM_DATA(emit->as, 1, emit->code_info_size);
797 ASM_ALIGN(emit->as, ASM_WORD_SIZE);
798 emit->code_info_size = ASM_GET_CODE_POS(emit->as) - emit->code_info_offset;
Damien George9a42eb52015-05-06 13:55:33 +0100799 // see comment in corresponding part of emitbc.c about the logic here
Damien George99886182015-04-06 22:38:53 +0100800 for (int i = 0; i < emit->scope->num_pos_args + emit->scope->num_kwonly_args; i++) {
Damien George9a42eb52015-05-06 13:55:33 +0100801 qstr qst = MP_QSTR__star_;
802 for (int j = 0; j < emit->scope->id_info_len; ++j) {
803 id_info_t *id = &emit->scope->id_info[j];
804 if ((id->flags & ID_FLAG_IS_PARAM) && id->local_num == i) {
805 qst = id->qst;
806 break;
807 }
808 }
809 ASM_DATA(emit->as, ASM_WORD_SIZE, (mp_uint_t)MP_OBJ_NEW_QSTR(qst));
Damien George99886182015-04-06 22:38:53 +0100810 }
811
812 // bytecode prelude: initialise closed over variables
813 emit->prelude_offset = ASM_GET_CODE_POS(emit->as);
814 for (int i = 0; i < emit->scope->id_info_len; i++) {
815 id_info_t *id = &emit->scope->id_info[i];
816 if (id->kind == ID_INFO_KIND_CELL) {
817 assert(id->local_num < 255);
818 ASM_DATA(emit->as, 1, id->local_num); // write the local which should be converted to a cell
819 }
820 }
821 ASM_DATA(emit->as, 1, 255); // end of list sentinel
822 }
823
Damien Georgec90f59e2014-09-06 23:06:36 +0100824 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100825
826 // check stack is back to zero size
827 if (emit->stack_size != 0) {
Damien Georgee72cda92015-04-11 12:15:47 +0100828 mp_printf(&mp_plat_print, "ERROR: stack size not back to zero; got %d\n", emit->stack_size);
Damien13ed3a62013-10-08 09:05:10 +0100829 }
830
Damien George36db6bc2014-05-07 17:24:22 +0100831 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100832 void *f = ASM_GET_CODE(emit->as);
833 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100834
835 // compute type signature
Damien Georgee9dac3b2014-09-29 22:10:41 +0100836 // note that the lower 2 bits of a vtype are tho correct MP_NATIVE_TYPE_xxx
Damien George2ac4af62014-08-15 16:45:41 +0100837 mp_uint_t type_sig = emit->return_vtype & 3;
838 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
839 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
840 }
841
Damien George99886182015-04-06 22:38:53 +0100842 mp_emit_glue_assign_native(emit->scope->raw_code,
843 emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY,
844 f, f_len, emit->scope->num_pos_args, emit->scope->num_kwonly_args,
845 emit->scope->scope_flags, type_sig);
Damien13ed3a62013-10-08 09:05:10 +0100846 }
847}
848
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200849STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100850 return emit->last_emit_was_return_value;
851}
852
Damien Georged6230f62014-09-23 14:10:03 +0000853STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georged6230f62014-09-23 14:10:03 +0000854 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100855 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100856 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100857 emit->scope->stack_size = emit->stack_size;
858 }
Damien George21ca2d72014-10-19 19:00:51 +0100859#ifdef DEBUG_PRINT
860 DEBUG_printf(" adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
861 for (int i = 0; i < emit->stack_size; i++) {
862 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000863 DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->data.u_reg);
Damien George21ca2d72014-10-19 19:00:51 +0100864 }
865 DEBUG_printf("\n");
866#endif
Damien13ed3a62013-10-08 09:05:10 +0100867}
868
Damien Georged6230f62014-09-23 14:10:03 +0000869STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien George21ca2d72014-10-19 19:00:51 +0100870 DEBUG_printf("adjust_stack_size(" INT_FMT ")\n", delta);
Damien Georged6230f62014-09-23 14:10:03 +0000871 // If we are adjusting the stack in a positive direction (pushing) then we
872 // need to fill in values for the stack kind and vtype of the newly-pushed
873 // entries. These should be set to "value" (ie not reg or imm) because we
874 // should only need to adjust the stack due to a jump to this part in the
875 // code (and hence we have settled the stack before the jump).
876 for (mp_int_t i = 0; i < delta; i++) {
877 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
878 si->kind = STACK_VALUE;
Damien George21ca2d72014-10-19 19:00:51 +0100879 // TODO we don't know the vtype to use here. At the moment this is a
880 // hack to get the case of multi comparison working.
881 if (delta == 1) {
882 si->vtype = emit->saved_stack_vtype;
883 } else {
884 si->vtype = VTYPE_PYOBJ;
885 }
Damien Georged6230f62014-09-23 14:10:03 +0000886 }
887 adjust_stack(emit, delta);
888}
889
890STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000891 (void)emit;
892 (void)source_line;
Damien Georged6230f62014-09-23 14:10:03 +0000893}
894
Damienff8ed772013-10-08 22:18:32 +0100895/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200896STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100897 adjust_stack(emit, stack_size_delta);
898 emit->last_emit_was_return_value = false;
899}
Damienff8ed772013-10-08 22:18:32 +0100900*/
Damien13ed3a62013-10-08 09:05:10 +0100901
Damienff8ed772013-10-08 22:18:32 +0100902// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000903STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100904 emit->last_emit_was_return_value = false;
905 // settle the stack
906 /*
907 if (regs_needed != 0) {
908 for (int i = 0; i < emit->stack_size; i++) {
909 switch (emit->stack_info[i].kind) {
910 case STACK_VALUE:
911 break;
912
913 case STACK_REG:
914 // TODO only push reg if in regs_needed
915 emit->stack_info[i].kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000916 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100917 break;
918
919 case STACK_IMM:
920 // don't think we ever need to push imms for settling
921 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
922 break;
923 }
924 }
925 }
926 */
Damien13ed3a62013-10-08 09:05:10 +0100927}
928
Damien George3112cde2014-09-29 18:45:42 +0100929// depth==0 is top, depth==1 is before top, etc
930STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
931 return &emit->stack_info[emit->stack_size - 1 - depth];
932}
933
934// depth==0 is top, depth==1 is before top, etc
935STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
936 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100937}
Damien13ed3a62013-10-08 09:05:10 +0100938
Damiend2755ec2013-10-16 23:58:48 +0100939// pos=1 is TOS, pos=2 is next, etc
940// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200941STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100942 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100943 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100944 if (i != skip_stack_pos) {
945 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000946 if (si->kind == STACK_REG && si->data.u_reg == reg_needed) {
Damiend2755ec2013-10-16 23:58:48 +0100947 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000948 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100949 }
Damienff8ed772013-10-08 22:18:32 +0100950 }
951 }
952}
Damien13ed3a62013-10-08 09:05:10 +0100953
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200954STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100955 for (int i = 0; i < emit->stack_size; i++) {
956 stack_info_t *si = &emit->stack_info[i];
957 if (si->kind == STACK_REG) {
958 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000959 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100960 }
Damien13ed3a62013-10-08 09:05:10 +0100961 }
962}
963
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200964STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +0000965 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +0100966 for (int i = 0; i < emit->stack_size; i++) {
967 stack_info_t *si = &emit->stack_info[i];
968 if (si->kind == STACK_REG) {
Damien George32444b72015-01-24 23:14:12 +0000969 DEBUG_printf(" reg(%u) to local(%u)\n", si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100970 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000971 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100972 }
973 }
974 for (int i = 0; i < emit->stack_size; i++) {
975 stack_info_t *si = &emit->stack_info[i];
976 if (si->kind == STACK_IMM) {
Damien George32444b72015-01-24 23:14:12 +0000977 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->data.u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +0100978 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000979 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->data.u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +0100980 }
981 }
982}
983
984// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200985STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100986 need_reg_single(emit, reg_dest, pos);
987 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +0100988 *vtype = si->vtype;
989 switch (si->kind) {
990 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +0100991 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100992 break;
993
Damienff8ed772013-10-08 22:18:32 +0100994 case STACK_REG:
Damien George32444b72015-01-24 23:14:12 +0000995 if (si->data.u_reg != reg_dest) {
996 ASM_MOV_REG_REG(emit->as, reg_dest, si->data.u_reg);
Damien13ed3a62013-10-08 09:05:10 +0100997 }
998 break;
999
Damienff8ed772013-10-08 22:18:32 +01001000 case STACK_IMM:
Damien George32444b72015-01-24 23:14:12 +00001001 ASM_MOV_IMM_TO_REG(emit->as, si->data.u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +01001002 break;
1003 }
Damien13ed3a62013-10-08 09:05:10 +01001004}
1005
Damien Georgee9dac3b2014-09-29 22:10:41 +01001006// does an efficient X=pop(); discard(); push(X)
1007// needs a (non-temp) register in case the poped element was stored in the stack
1008STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
1009 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
1010 si[0] = si[1];
1011 if (si->kind == STACK_VALUE) {
1012 // if folded element was on the stack we need to put it in a register
1013 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
1014 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +00001015 si->data.u_reg = reg_dest;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001016 }
1017 adjust_stack(emit, -1);
1018}
1019
1020// If stacked value is in a register and the register is not r1 or r2, then
1021// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
1022STATIC 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 +01001023 emit->last_emit_was_return_value = false;
1024 stack_info_t *si = peek_stack(emit, 0);
Damien George32444b72015-01-24 23:14:12 +00001025 if (si->kind == STACK_REG && si->data.u_reg != not_r1 && si->data.u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +01001026 *vtype = si->vtype;
Damien George32444b72015-01-24 23:14:12 +00001027 *reg_dest = si->data.u_reg;
Damien George3112cde2014-09-29 18:45:42 +01001028 need_reg_single(emit, *reg_dest, 1);
1029 } else {
1030 emit_access_stack(emit, 1, vtype, *reg_dest);
1031 }
1032 adjust_stack(emit, -1);
1033}
1034
Damien Georgee6ce10a2014-09-06 18:38:20 +01001035STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +01001036 emit->last_emit_was_return_value = false;
1037 adjust_stack(emit, -1);
1038}
1039
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001040STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +01001041 emit->last_emit_was_return_value = false;
1042 emit_access_stack(emit, 1, vtype, reg_dest);
1043 adjust_stack(emit, -1);
1044}
1045
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001046STATIC 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 +01001047 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001048 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +01001049}
1050
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001051STATIC 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 +01001052 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001053 emit_pre_pop_reg(emit, vtypeb, regb);
1054 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001055}
1056
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001057STATIC void emit_post(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001058 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001059}
1060
Damien Georgee9dac3b2014-09-29 22:10:41 +01001061STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
1062 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
1063 si->vtype = new_vtype;
1064}
1065
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001066STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +01001067 stack_info_t *si = &emit->stack_info[emit->stack_size];
1068 si->vtype = vtype;
1069 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +00001070 si->data.u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +01001071 adjust_stack(emit, 1);
1072}
1073
Damien George40f3c022014-07-03 13:25:24 +01001074STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +01001075 stack_info_t *si = &emit->stack_info[emit->stack_size];
1076 si->vtype = vtype;
1077 si->kind = STACK_IMM;
Damien George32444b72015-01-24 23:14:12 +00001078 si->data.u_imm = imm;
Damienff8ed772013-10-08 22:18:32 +01001079 adjust_stack(emit, 1);
1080}
1081
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001082STATIC 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 +01001083 emit_post_push_reg(emit, vtypea, rega);
1084 emit_post_push_reg(emit, vtypeb, regb);
1085}
1086
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001087STATIC 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 +01001088 emit_post_push_reg(emit, vtypea, rega);
1089 emit_post_push_reg(emit, vtypeb, regb);
1090 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001091}
1092
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001093STATIC 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 +01001094 emit_post_push_reg(emit, vtypea, rega);
1095 emit_post_push_reg(emit, vtypeb, regb);
1096 emit_post_push_reg(emit, vtypec, regc);
1097 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +01001098}
1099
Damien George7fe21912014-08-16 22:31:57 +01001100STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +01001101 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001102 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001103}
1104
Damien George7fe21912014-08-16 22:31:57 +01001105STATIC 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 +01001106 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001107 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
1108 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001109}
1110
Damien George40f3c022014-07-03 13:25:24 +01001111// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001112STATIC 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 +01001113 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001114 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
1115 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +01001116}
1117
Damien George7fe21912014-08-16 22:31:57 +01001118STATIC 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 +00001119 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001120 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1121 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1122 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +00001123}
1124
Damien George40f3c022014-07-03 13:25:24 +01001125// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001126STATIC 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 +01001127 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001128 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1129 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1130 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
1131 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +01001132}
1133
Damien George86de21b2014-08-16 22:06:11 +01001134// vtype of all n_pop objects is VTYPE_PYOBJ
1135// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
1136// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
1137// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
1138STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
1139 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +01001140
Damien George86de21b2014-08-16 22:06:11 +01001141 // First, store any immediate values to their respective place on the stack.
1142 for (mp_uint_t i = 0; i < n_pop; i++) {
1143 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1144 // must push any imm's to stack
1145 // must convert them to VTYPE_PYOBJ for viper code
1146 if (si->kind == STACK_IMM) {
1147 si->kind = STACK_VALUE;
1148 switch (si->vtype) {
1149 case VTYPE_PYOBJ:
Damien George32444b72015-01-24 23:14:12 +00001150 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 +01001151 break;
1152 case VTYPE_BOOL:
Damien George32444b72015-01-24 23:14:12 +00001153 if (si->data.u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +01001154 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 +01001155 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +01001156 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 +01001157 }
1158 si->vtype = VTYPE_PYOBJ;
1159 break;
1160 case VTYPE_INT:
1161 case VTYPE_UINT:
Damien George32444b72015-01-24 23:14:12 +00001162 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 +01001163 si->vtype = VTYPE_PYOBJ;
1164 break;
1165 default:
1166 // not handled
1167 assert(0);
1168 }
1169 }
1170
1171 // verify that this value is on the stack
1172 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +01001173 }
Damien George86de21b2014-08-16 22:06:11 +01001174
1175 // Second, convert any non-VTYPE_PYOBJ to that type.
1176 for (mp_uint_t i = 0; i < n_pop; i++) {
1177 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1178 if (si->vtype != VTYPE_PYOBJ) {
1179 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +01001180 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001181 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 +01001182 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001183 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001184 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001185 }
1186 }
1187
1188 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1189 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +01001190 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001191}
1192
1193// vtype of all n_push objects is VTYPE_PYOBJ
1194STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1195 need_reg_all(emit);
1196 for (mp_uint_t i = 0; i < n_push; i++) {
1197 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1198 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1199 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001200 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001201 adjust_stack(emit, n_push);
1202}
1203
Damien George7ff996c2014-09-08 23:05:16 +01001204STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001205 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001206 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001207 // need to commit stack because we can jump here from elsewhere
1208 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001209 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001210 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001211}
1212
Damien Georgecdd96df2014-04-06 12:58:40 +01001213STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1214 DEBUG_printf("import_name %s\n", qstr_str(qst));
1215 vtype_kind_t vtype_fromlist;
1216 vtype_kind_t vtype_level;
1217 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1218 assert(vtype_fromlist == VTYPE_PYOBJ);
1219 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001220 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001221 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001222}
1223
Damien Georgecdd96df2014-04-06 12:58:40 +01001224STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1225 DEBUG_printf("import_from %s\n", qstr_str(qst));
1226 emit_native_pre(emit);
1227 vtype_kind_t vtype_module;
1228 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1229 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001230 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001231 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001232}
1233
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001234STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001235 DEBUG_printf("import_star\n");
1236 vtype_kind_t vtype_module;
1237 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1238 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001239 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001240 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001241}
1242
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001243STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001244 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001245 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001246 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001247 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001248 if (emit->do_viper_types) {
1249 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001250 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1251 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1252 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien George3da677e2015-01-29 15:13:40 +00001253 no_other_choice1:
1254 case MP_TOKEN_ELLIPSIS: vtype = VTYPE_PYOBJ; val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1255 default: assert(0); goto no_other_choice1; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001256 }
1257 } else {
1258 vtype = VTYPE_PYOBJ;
1259 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001260 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1261 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1262 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien George3da677e2015-01-29 15:13:40 +00001263 no_other_choice2:
1264 case MP_TOKEN_ELLIPSIS: val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1265 default: assert(0); goto no_other_choice2; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001266 }
1267 }
1268 emit_post_push_imm(emit, vtype, val);
1269}
1270
Damien George40f3c022014-07-03 13:25:24 +01001271STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001272 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001273 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001274 if (emit->do_viper_types) {
1275 emit_post_push_imm(emit, VTYPE_INT, arg);
1276 } else {
Damien George2686f9b2015-04-01 00:12:43 +01001277 emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_SMALL_INT(arg));
Damien13ed3a62013-10-08 09:05:10 +01001278 }
1279}
1280
Damien George7ff996c2014-09-08 23:05:16 +01001281STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001282 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001283 // TODO: Eventually we want to be able to work with raw pointers in viper to
1284 // do native array access. For now we just load them as any other object.
1285 /*
Damien13ed3a62013-10-08 09:05:10 +01001286 if (emit->do_viper_types) {
1287 // not implemented properly
1288 // load a pointer to the asciiz string?
1289 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001290 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001291 } else
1292 */
1293 {
Damien Georgeb601d952014-06-30 05:17:25 +01001294 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +01001295 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_BYTES, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001296 } else {
Damien George7ff996c2014-09-08 23:05:16 +01001297 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001298 }
Damien13ed3a62013-10-08 09:05:10 +01001299 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1300 }
1301}
1302
Damien Georgedab13852015-01-13 15:55:54 +00001303STATIC void emit_native_load_const_obj(emit_t *emit, void *obj) {
1304 emit_native_pre(emit);
Damien George2127e9a2015-01-14 00:11:09 +00001305 need_reg_single(emit, REG_RET, 0);
Damien Georgedab13852015-01-13 15:55:54 +00001306 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)obj, REG_RET);
1307 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1308}
1309
Damien George3558f622014-04-20 17:50:40 +01001310STATIC void emit_native_load_null(emit_t *emit) {
1311 emit_native_pre(emit);
1312 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1313}
1314
Damien George0abb5602015-01-16 12:24:49 +00001315STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
1316 DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
Damien13ed3a62013-10-08 09:05:10 +01001317 vtype_kind_t vtype = emit->local_vtype[local_num];
1318 if (vtype == VTYPE_UNBOUND) {
Damien Georgec8b60f02015-04-20 13:29:31 +00001319 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "local '%q' used before type known", qst);
Damien13ed3a62013-10-08 09:05:10 +01001320 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001321 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001322 if (local_num == 0) {
1323 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001324 } else if (local_num == 1) {
1325 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1326 } else if (local_num == 2) {
1327 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001328 } else {
Damien George0b610de2014-09-29 16:25:04 +01001329 need_reg_single(emit, REG_TEMP0, 0);
Damien George99886182015-04-06 22:38:53 +01001330 if (emit->do_viper_types) {
1331 ASM_MOV_LOCAL_TO_REG(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1332 } else {
1333 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - local_num, REG_TEMP0);
1334 }
Damien George0b610de2014-09-29 16:25:04 +01001335 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001336 }
Damien13ed3a62013-10-08 09:05:10 +01001337}
1338
Damien George7ff996c2014-09-08 23:05:16 +01001339STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001340 DEBUG_printf("load_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1341 need_reg_single(emit, REG_RET, 0);
1342 emit_native_load_fast(emit, qst, local_num);
1343 vtype_kind_t vtype;
1344 int reg_base = REG_RET;
1345 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1346 ASM_LOAD_REG_REG_OFFSET(emit->as, REG_RET, reg_base, 1);
1347 // closed over vars are always Python objects
1348 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien9ecbcff2013-12-11 00:41:43 +00001349}
1350
Damien George7ff996c2014-09-08 23:05:16 +01001351STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001352 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001353 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001354 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001355 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1356}
1357
Damien George7ff996c2014-09-08 23:05:16 +01001358STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001359 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001360 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001361 // check for builtin casting operators
1362 if (emit->do_viper_types && qst == MP_QSTR_int) {
1363 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1364 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1365 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1366 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1367 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1368 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1369 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1370 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1371 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
1372 } else {
1373 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1374 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1375 }
Damien13ed3a62013-10-08 09:05:10 +01001376}
1377
Damien George7ff996c2014-09-08 23:05:16 +01001378STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001379 // depends on type of subject:
1380 // - integer, function, pointer to integers: error
1381 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001382 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001383 vtype_kind_t vtype_base;
1384 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1385 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001386 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001387 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1388}
1389
Damien George7ff996c2014-09-08 23:05:16 +01001390STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001391 vtype_kind_t vtype_base;
1392 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1393 assert(vtype_base == VTYPE_PYOBJ);
1394 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001395 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001396}
1397
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001398STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001399 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001400 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001401 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001402}
1403
Damien George729f7b42014-04-17 22:10:53 +01001404STATIC void emit_native_load_subscr(emit_t *emit) {
Damien George91cfd412014-10-12 16:59:29 +01001405 DEBUG_printf("load_subscr\n");
1406 // need to compile: base[index]
1407
1408 // pop: index, base
1409 // optimise case where index is an immediate
1410 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1411
1412 if (vtype_base == VTYPE_PYOBJ) {
1413 // standard Python call
1414 vtype_kind_t vtype_index;
1415 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1);
1416 assert(vtype_index == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001417 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 +01001418 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1419 } else {
Damien George91cfd412014-10-12 16:59:29 +01001420 // viper load
1421 // TODO The different machine architectures have very different
1422 // capabilities and requirements for loads, so probably best to
1423 // write a completely separate load-optimiser for each one.
1424 stack_info_t *top = peek_stack(emit, 0);
1425 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1426 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001427 mp_int_t index_value = top->data.u_imm;
Damien George91cfd412014-10-12 16:59:29 +01001428 emit_pre_pop_discard(emit); // discard index
1429 int reg_base = REG_ARG_1;
1430 int reg_index = REG_ARG_2;
1431 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
1432 switch (vtype_base) {
1433 case VTYPE_PTR8: {
1434 // pointer to 8-bit memory
1435 // TODO optimise to use thumb ldrb r1, [r2, r3]
1436 if (index_value != 0) {
1437 // index is non-zero
1438 #if N_THUMB
1439 if (index_value > 0 && index_value < 32) {
1440 asm_thumb_ldrb_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1441 break;
1442 }
1443 #endif
1444 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
1445 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1446 reg_base = reg_index;
1447 }
1448 ASM_LOAD8_REG_REG(emit->as, REG_RET, reg_base); // load from (base+index)
1449 break;
1450 }
1451 case VTYPE_PTR16: {
1452 // pointer to 16-bit memory
1453 if (index_value != 0) {
1454 // index is a non-zero immediate
1455 #if N_THUMB
1456 if (index_value > 0 && index_value < 32) {
1457 asm_thumb_ldrh_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1458 break;
1459 }
1460 #endif
1461 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
1462 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1463 reg_base = reg_index;
1464 }
1465 ASM_LOAD16_REG_REG(emit->as, REG_RET, reg_base); // load from (base+2*index)
1466 break;
1467 }
1468 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001469 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1470 "can't load from '%q'", vtype_to_qstr(vtype_base));
Damien George91cfd412014-10-12 16:59:29 +01001471 }
1472 } else {
1473 // index is not an immediate
1474 vtype_kind_t vtype_index;
1475 int reg_index = REG_ARG_2;
1476 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
1477 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1478 switch (vtype_base) {
1479 case VTYPE_PTR8: {
1480 // pointer to 8-bit memory
1481 // TODO optimise to use thumb ldrb r1, [r2, r3]
1482 assert(vtype_index == VTYPE_INT);
1483 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1484 ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
1485 break;
1486 }
1487 case VTYPE_PTR16: {
1488 // pointer to 16-bit memory
1489 assert(vtype_index == VTYPE_INT);
1490 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1491 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1492 ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
1493 break;
1494 }
1495 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001496 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1497 "can't load from '%q'", vtype_to_qstr(vtype_base));
Damien George91cfd412014-10-12 16:59:29 +01001498 }
1499 }
1500 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
Damien George729f7b42014-04-17 22:10:53 +01001501 }
1502}
1503
Damien George7ff996c2014-09-08 23:05:16 +01001504STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001505 vtype_kind_t vtype;
Damien13ed3a62013-10-08 09:05:10 +01001506 if (local_num == 0) {
1507 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001508 } else if (local_num == 1) {
1509 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1510 } else if (local_num == 2) {
1511 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001512 } else {
Damien George0b610de2014-09-29 16:25:04 +01001513 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
Damien George99886182015-04-06 22:38:53 +01001514 if (emit->do_viper_types) {
1515 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1516 } else {
1517 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, STATE_START + emit->n_state - 1 - local_num);
1518 }
Damien13ed3a62013-10-08 09:05:10 +01001519 }
Damien13ed3a62013-10-08 09:05:10 +01001520 emit_post(emit);
1521
1522 // check types
1523 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1524 // first time this local is assigned, so give it a type of the object stored in it
1525 emit->local_vtype[local_num] = vtype;
1526 } else if (emit->local_vtype[local_num] != vtype) {
1527 // type of local is not the same as object stored in it
Damien Georgec8b60f02015-04-20 13:29:31 +00001528 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1529 "local '%q' has type '%q' but source is '%q'",
1530 qst, vtype_to_qstr(emit->local_vtype[local_num]), vtype_to_qstr(vtype));
Damien13ed3a62013-10-08 09:05:10 +01001531 }
1532}
1533
Damien George7ff996c2014-09-08 23:05:16 +01001534STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001535 DEBUG_printf("store_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1536 need_reg_single(emit, REG_TEMP0, 0);
1537 need_reg_single(emit, REG_TEMP1, 0);
1538 emit_native_load_fast(emit, qst, local_num);
1539 vtype_kind_t vtype;
1540 int reg_base = REG_TEMP0;
1541 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1542 int reg_src = REG_TEMP1;
1543 emit_pre_pop_reg_flexible(emit, &vtype, &reg_src, reg_base, reg_base);
1544 ASM_STORE_REG_REG_OFFSET(emit->as, reg_src, reg_base, 1);
1545 emit_post(emit);
Damien9ecbcff2013-12-11 00:41:43 +00001546}
1547
Damien George7ff996c2014-09-08 23:05:16 +01001548STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001549 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001550 vtype_kind_t vtype;
1551 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1552 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001553 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001554 emit_post(emit);
1555}
1556
Damien George7ff996c2014-09-08 23:05:16 +01001557STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001558 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001559 if (vtype == VTYPE_PYOBJ) {
1560 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1561 } else {
1562 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001563 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 +01001564 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001565 }
Damien George7ff996c2014-09-08 23:05:16 +01001566 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001567 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001568}
1569
Damien George7ff996c2014-09-08 23:05:16 +01001570STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001571 vtype_kind_t vtype_base, vtype_val;
1572 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1573 assert(vtype_base == VTYPE_PYOBJ);
1574 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001575 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001576 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001577}
1578
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001579STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001580 DEBUG_printf("store_subscr\n");
1581 // need to compile: base[index] = value
1582
1583 // pop: index, base, value
1584 // optimise case where index is an immediate
1585 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1586
1587 if (vtype_base == VTYPE_PYOBJ) {
1588 // standard Python call
1589 vtype_kind_t vtype_index, vtype_value;
1590 emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3);
1591 assert(vtype_index == VTYPE_PYOBJ);
1592 assert(vtype_value == VTYPE_PYOBJ);
1593 emit_call(emit, MP_F_OBJ_SUBSCR);
1594 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001595 // viper store
1596 // TODO The different machine architectures have very different
1597 // capabilities and requirements for stores, so probably best to
1598 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001599 stack_info_t *top = peek_stack(emit, 0);
1600 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1601 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001602 mp_int_t index_value = top->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001603 emit_pre_pop_discard(emit); // discard index
1604 vtype_kind_t vtype_value;
1605 int reg_base = REG_ARG_1;
1606 int reg_index = REG_ARG_2;
1607 int reg_value = REG_ARG_3;
1608 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001609 #if N_X86
1610 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1611 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1612 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001613 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001614 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001615 switch (vtype_base) {
1616 case VTYPE_PTR8: {
1617 // pointer to 8-bit memory
1618 // TODO optimise to use thumb strb r1, [r2, r3]
1619 if (index_value != 0) {
1620 // index is non-zero
1621 #if N_THUMB
1622 if (index_value > 0 && index_value < 32) {
1623 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1624 break;
1625 }
1626 #endif
1627 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001628 #if N_ARM
1629 asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1630 return;
1631 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001632 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1633 reg_base = reg_index;
1634 }
1635 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1636 break;
1637 }
1638 case VTYPE_PTR16: {
1639 // pointer to 16-bit memory
1640 if (index_value != 0) {
1641 // index is a non-zero immediate
1642 #if N_THUMB
1643 if (index_value > 0 && index_value < 32) {
1644 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1645 break;
1646 }
1647 #endif
1648 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001649 #if N_ARM
1650 asm_arm_strh_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1651 return;
1652 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001653 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1654 reg_base = reg_index;
1655 }
1656 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1657 break;
1658 }
1659 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001660 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1661 "can't store to '%q'", vtype_to_qstr(vtype_base));
Damien Georgee9dac3b2014-09-29 22:10:41 +01001662 }
1663 } else {
1664 // index is not an immediate
1665 vtype_kind_t vtype_index, vtype_value;
1666 int reg_index = REG_ARG_2;
1667 int reg_value = REG_ARG_3;
1668 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1669 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001670 #if N_X86
1671 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1672 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1673 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001674 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001675 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001676 switch (vtype_base) {
1677 case VTYPE_PTR8: {
1678 // pointer to 8-bit memory
1679 // TODO optimise to use thumb strb r1, [r2, r3]
1680 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001681 #if N_ARM
1682 asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1683 break;
1684 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001685 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1686 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1687 break;
1688 }
1689 case VTYPE_PTR16: {
1690 // pointer to 16-bit memory
1691 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001692 #if N_ARM
1693 asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1694 break;
1695 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001696 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1697 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1698 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1699 break;
1700 }
1701 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001702 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1703 "can't store to '%q'", vtype_to_qstr(vtype_base));
Damien Georgee9dac3b2014-09-29 22:10:41 +01001704 }
1705 }
1706
1707 }
Damien13ed3a62013-10-08 09:05:10 +01001708}
1709
Damien George7ff996c2014-09-08 23:05:16 +01001710STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George2cc54732015-04-03 14:29:30 +01001711 // TODO: This is not compliant implementation. We could use MP_OBJ_SENTINEL
1712 // to mark deleted vars but then every var would need to be checked on
1713 // each access. Very inefficient, so just set value to None to enable GC.
1714 emit_native_load_const_tok(emit, MP_TOKEN_KW_NONE);
1715 emit_native_store_fast(emit, qst, local_num);
Damien13ed3a62013-10-08 09:05:10 +01001716}
1717
Damien George7ff996c2014-09-08 23:05:16 +01001718STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001719 // TODO implement me!
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001720 (void)emit;
1721 (void)qst;
1722 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001723}
1724
Damien Georgee6ce10a2014-09-06 18:38:20 +01001725STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1726 emit_native_pre(emit);
1727 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1728 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001729}
1730
Damien Georgee6ce10a2014-09-06 18:38:20 +01001731STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1732 emit_native_pre(emit);
1733 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1734 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001735}
1736
Damien Georgee6ce10a2014-09-06 18:38:20 +01001737STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001738 vtype_kind_t vtype_base;
1739 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1740 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001741 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 +01001742 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001743}
1744
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001745STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001746 vtype_kind_t vtype_index, vtype_base;
1747 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1748 assert(vtype_index == VTYPE_PYOBJ);
1749 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001750 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 +01001751}
1752
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001753STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001754 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001755 vtype_kind_t vtype;
Damien George21ca2d72014-10-19 19:00:51 +01001756 int reg = REG_TEMP0;
1757 emit_pre_pop_reg_flexible(emit, &vtype, &reg, -1, -1);
1758 emit_post_push_reg_reg(emit, vtype, reg, vtype, reg);
Damien13ed3a62013-10-08 09:05:10 +01001759}
1760
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001761STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001762 vtype_kind_t vtype0, vtype1;
1763 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1764 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1765}
1766
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001767STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001768 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001769 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001770 emit_post(emit);
1771}
1772
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001773STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001774 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001775 vtype_kind_t vtype0, vtype1;
1776 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1777 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001778}
1779
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001780STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001781 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001782 vtype_kind_t vtype0, vtype1, vtype2;
1783 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1784 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1785}
1786
Damien George7ff996c2014-09-08 23:05:16 +01001787STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001788 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001789 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001790 // need to commit stack because we are jumping elsewhere
1791 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001792 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001793 emit_post(emit);
1794}
1795
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001796STATIC void emit_native_jump_helper(emit_t *emit, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001797 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgec8b60f02015-04-20 13:29:31 +00001798 if (vtype == VTYPE_PYOBJ) {
1799 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1800 if (!pop) {
1801 adjust_stack(emit, 1);
1802 }
1803 emit_call(emit, MP_F_OBJ_IS_TRUE);
1804 } else {
1805 emit_pre_pop_reg(emit, &vtype, REG_RET);
1806 if (!pop) {
1807 adjust_stack(emit, 1);
1808 }
1809 if (!(vtype == VTYPE_BOOL || vtype == VTYPE_INT || vtype == VTYPE_UINT)) {
1810 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1811 "can't implicitly convert '%q' to 'bool'", vtype_to_qstr(vtype));
1812 }
Damien13ed3a62013-10-08 09:05:10 +01001813 }
Damien George21ca2d72014-10-19 19:00:51 +01001814 // For non-pop need to save the vtype so that emit_native_adjust_stack_size
1815 // can use it. This is a bit of a hack.
1816 if (!pop) {
1817 emit->saved_stack_vtype = vtype;
1818 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001819 // need to commit stack because we may jump elsewhere
1820 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001821}
1822
Damien George63f38322015-02-28 15:04:06 +00001823STATIC void emit_native_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
1824 DEBUG_printf("pop_jump_if(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001825 emit_native_jump_helper(emit, true);
Damien George63f38322015-02-28 15:04:06 +00001826 if (cond) {
1827 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1828 } else {
1829 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1830 }
Damien1a6633a2013-11-03 13:58:19 +00001831 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001832}
Damien1a6633a2013-11-03 13:58:19 +00001833
Damien George63f38322015-02-28 15:04:06 +00001834STATIC void emit_native_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
1835 DEBUG_printf("jump_if_or_pop(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001836 emit_native_jump_helper(emit, false);
Damien George63f38322015-02-28 15:04:06 +00001837 if (cond) {
1838 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1839 } else {
1840 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1841 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001842 adjust_stack(emit, -1);
1843 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001844}
1845
Damien George7ff996c2014-09-08 23:05:16 +01001846STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001847 (void)except_depth;
Damien George25c84642014-05-30 15:20:41 +01001848 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001849}
Damien Georgea32c1e42014-05-07 18:30:52 +01001850
Damien George7ff996c2014-09-08 23:05:16 +01001851STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001852 (void)except_depth;
Damien Georgea32c1e42014-05-07 18:30:52 +01001853 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001854}
Damien Georgea32c1e42014-05-07 18:30:52 +01001855
Damien George7ff996c2014-09-08 23:05:16 +01001856STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001857 // not supported, or could be with runtime call
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001858 (void)emit;
1859 (void)label;
Damien13ed3a62013-10-08 09:05:10 +01001860 assert(0);
1861}
Damien Georgeb601d952014-06-30 05:17:25 +01001862
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001863STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001864 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001865 assert(0);
1866}
Damien Georgeb601d952014-06-30 05:17:25 +01001867
Damien George7ff996c2014-09-08 23:05:16 +01001868STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001869 emit_native_pre(emit);
1870 // need to commit stack because we may jump elsewhere
1871 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001872 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 +01001873 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001874 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001875 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001876}
Damien Georgeb601d952014-06-30 05:17:25 +01001877
Damien George7ff996c2014-09-08 23:05:16 +01001878STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001879 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001880}
Damien Georgeb601d952014-06-30 05:17:25 +01001881
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001882STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgeb6e6b522015-01-21 17:00:01 +00001883 // logic:
1884 // exc = pop_stack
1885 // if exc == None: pass
1886 // else: raise exc
1887 // the check if exc is None is done in the MP_F_NATIVE_RAISE stub
1888 vtype_kind_t vtype;
1889 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1890 emit_call(emit, MP_F_NATIVE_RAISE);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001891 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001892}
Damiend2755ec2013-10-16 23:58:48 +01001893
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001894STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001895 // perhaps the difficult one, as we want to rewrite for loops using native code
1896 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001897
1898 vtype_kind_t vtype;
1899 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1900 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001901 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01001902 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001903}
Damiend2755ec2013-10-16 23:58:48 +01001904
Damien George7ff996c2014-09-08 23:05:16 +01001905STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001906 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001907 vtype_kind_t vtype;
1908 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1909 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001910 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01001911 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
1912 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01001913 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1914}
1915
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001916STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001917 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001918 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001919 adjust_stack(emit, -1);
1920 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001921}
1922
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001923STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001924 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001925 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001926 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001927 emit_post(emit);
1928}
1929
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001930STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001931 (void)emit;
Damien Georgeb601d952014-06-30 05:17:25 +01001932 /*
1933 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001934 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001935 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01001936 emit_post(emit);
1937 */
Damien13ed3a62013-10-08 09:05:10 +01001938}
1939
Damien Georged17926d2014-03-30 13:35:08 +01001940STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001941 vtype_kind_t vtype;
1942 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1943 assert(vtype == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001944 if (op == MP_UNARY_OP_NOT) {
Damien Georged6230f62014-09-23 14:10:03 +00001945 // we need to synthesise this operation by converting to bool first
1946 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
Damien George3112cde2014-09-29 18:45:42 +01001947 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgeb601d952014-06-30 05:17:25 +01001948 }
Damien Georged6230f62014-09-23 14:10:03 +00001949 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1950 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001951}
1952
Damien Georged17926d2014-03-30 13:35:08 +01001953STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001954 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01001955 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
1956 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01001957 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01001958 #if N_X64 || N_X86
1959 // special cases for x86 and shifting
1960 if (op == MP_BINARY_OP_LSHIFT
1961 || op == MP_BINARY_OP_INPLACE_LSHIFT
1962 || op == MP_BINARY_OP_RSHIFT
1963 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1964 #if N_X64
1965 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
1966 #else
1967 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
1968 #endif
1969 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1970 ASM_LSL_REG(emit->as, REG_RET);
1971 } else {
1972 ASM_ASR_REG(emit->as, REG_RET);
1973 }
1974 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
1975 return;
1976 }
1977 #endif
1978 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001979 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001980 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
1981 if (0) {
1982 // dummy
1983 #if !(N_X64 || N_X86)
1984 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1985 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00001986 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001987 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1988 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1989 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1990 #endif
Damien George1ef23482014-10-12 14:21:06 +01001991 } else if (op == MP_BINARY_OP_OR || op == MP_BINARY_OP_INPLACE_OR) {
1992 ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1993 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1994 } else if (op == MP_BINARY_OP_XOR || op == MP_BINARY_OP_INPLACE_XOR) {
1995 ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1996 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1997 } else if (op == MP_BINARY_OP_AND || op == MP_BINARY_OP_INPLACE_AND) {
1998 ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1999 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002000 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
2001 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2002 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2003 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
2004 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2005 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2006 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
2007 // comparison ops are (in enum order):
2008 // MP_BINARY_OP_LESS
2009 // MP_BINARY_OP_MORE
2010 // MP_BINARY_OP_EQUAL
2011 // MP_BINARY_OP_LESS_EQUAL
2012 // MP_BINARY_OP_MORE_EQUAL
2013 // MP_BINARY_OP_NOT_EQUAL
Damien George21ca2d72014-10-19 19:00:51 +01002014 need_reg_single(emit, REG_RET, 0);
Damien George3112cde2014-09-29 18:45:42 +01002015 #if N_X64
2016 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
2017 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
2018 static byte ops[6] = {
2019 ASM_X64_CC_JL,
2020 ASM_X64_CC_JG,
2021 ASM_X64_CC_JE,
2022 ASM_X64_CC_JLE,
2023 ASM_X64_CC_JGE,
2024 ASM_X64_CC_JNE,
2025 };
2026 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
2027 #elif N_X86
2028 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
2029 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
2030 static byte ops[6] = {
2031 ASM_X86_CC_JL,
2032 ASM_X86_CC_JG,
2033 ASM_X86_CC_JE,
2034 ASM_X86_CC_JLE,
2035 ASM_X86_CC_JGE,
2036 ASM_X86_CC_JNE,
2037 };
2038 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
2039 #elif N_THUMB
2040 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
2041 static uint16_t ops[6] = {
2042 ASM_THUMB_OP_ITE_GE,
2043 ASM_THUMB_OP_ITE_GT,
2044 ASM_THUMB_OP_ITE_EQ,
2045 ASM_THUMB_OP_ITE_GT,
2046 ASM_THUMB_OP_ITE_GE,
2047 ASM_THUMB_OP_ITE_EQ,
2048 };
2049 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
2050 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
2051 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
2052 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
2053 #elif N_ARM
Fabian Vogte5268962014-10-04 00:53:46 +02002054 asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
2055 static uint ccs[6] = {
2056 ASM_ARM_CC_LT,
2057 ASM_ARM_CC_GT,
2058 ASM_ARM_CC_EQ,
2059 ASM_ARM_CC_LE,
2060 ASM_ARM_CC_GE,
2061 ASM_ARM_CC_NE,
2062 };
2063 asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
Damien George3112cde2014-09-29 18:45:42 +01002064 #else
2065 #error not implemented
2066 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00002067 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
2068 } else {
2069 // TODO other ops not yet implemented
2070 assert(0);
2071 }
Damien13ed3a62013-10-08 09:05:10 +01002072 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01002073 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00002074 bool invert = false;
2075 if (op == MP_BINARY_OP_NOT_IN) {
2076 invert = true;
2077 op = MP_BINARY_OP_IN;
2078 } else if (op == MP_BINARY_OP_IS_NOT) {
2079 invert = true;
2080 op = MP_BINARY_OP_IS;
2081 }
Damien George7fe21912014-08-16 22:31:57 +01002082 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00002083 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01002084 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00002085 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
2086 }
Damien13ed3a62013-10-08 09:05:10 +01002087 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2088 } else {
Damien George8f6aad22015-04-22 23:16:03 +01002089 adjust_stack(emit, -1);
Damien Georgec8b60f02015-04-20 13:29:31 +00002090 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2091 "can't do binary op between '%q' and '%q'",
2092 vtype_to_qstr(vtype_lhs), vtype_to_qstr(vtype_rhs));
Damien13ed3a62013-10-08 09:05:10 +01002093 }
2094}
2095
Damien George7ff996c2014-09-08 23:05:16 +01002096STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01002097 // for viper: call runtime, with types of args
2098 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00002099 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002100 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002101 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01002102 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01002103}
2104
Damien George7ff996c2014-09-08 23:05:16 +01002105STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002106 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002107 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002108 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002109 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
2110}
2111
Damien George7ff996c2014-09-08 23:05:16 +01002112STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01002113 // only used in list comprehension
2114 vtype_kind_t vtype_list, vtype_item;
2115 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2116 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
2117 assert(vtype_list == VTYPE_PYOBJ);
2118 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002119 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01002120 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002121}
2122
Damien George7ff996c2014-09-08 23:05:16 +01002123STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002124 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002125 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002126 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
2127}
2128
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002129STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002130 vtype_kind_t vtype_key, vtype_value, vtype_map;
2131 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
2132 assert(vtype_key == VTYPE_PYOBJ);
2133 assert(vtype_value == VTYPE_PYOBJ);
2134 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002135 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01002136 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
2137}
2138
Damien George7ff996c2014-09-08 23:05:16 +01002139STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01002140 // only used in list comprehension
2141 vtype_kind_t vtype_map, vtype_key, vtype_value;
2142 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
2143 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
2144 assert(vtype_map == VTYPE_PYOBJ);
2145 assert(vtype_key == VTYPE_PYOBJ);
2146 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002147 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01002148 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002149}
2150
Damien Georgee37dcaa2014-12-27 17:07:16 +00002151#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +01002152STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002153 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002154 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002155 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002156 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
2157}
2158
Damien George7ff996c2014-09-08 23:05:16 +01002159STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01002160 // only used in set comprehension
2161 vtype_kind_t vtype_set, vtype_item;
2162 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2163 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
2164 assert(vtype_set == VTYPE_PYOBJ);
2165 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002166 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01002167 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002168}
Damien Georgee37dcaa2014-12-27 17:07:16 +00002169#endif
Damiend2755ec2013-10-16 23:58:48 +01002170
Damien George83204f32014-12-27 17:20:41 +00002171#if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +01002172STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002173 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01002174 if (n_args == 2) {
2175 vtype_kind_t vtype_start, vtype_stop;
2176 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
2177 assert(vtype_start == VTYPE_PYOBJ);
2178 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002179 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 +01002180 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2181 } else {
2182 assert(n_args == 3);
2183 vtype_kind_t vtype_start, vtype_stop, vtype_step;
2184 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
2185 assert(vtype_start == VTYPE_PYOBJ);
2186 assert(vtype_stop == VTYPE_PYOBJ);
2187 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002188 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01002189 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2190 }
Damien13ed3a62013-10-08 09:05:10 +01002191}
Damien George83204f32014-12-27 17:20:41 +00002192#endif
Damien Georgecdd96df2014-04-06 12:58:40 +01002193
Damien George7ff996c2014-09-08 23:05:16 +01002194STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002195 DEBUG_printf("unpack_sequence %d\n", n_args);
2196 vtype_kind_t vtype_base;
2197 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2198 assert(vtype_base == VTYPE_PYOBJ);
2199 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01002200 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01002201}
Damien Georgecdd96df2014-04-06 12:58:40 +01002202
Damien George7ff996c2014-09-08 23:05:16 +01002203STATIC 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 +01002204 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
2205 vtype_kind_t vtype_base;
2206 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2207 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01002208 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 +01002209 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 +01002210}
2211
Damien George7ff996c2014-09-08 23:05:16 +01002212STATIC 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 +01002213 // 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 +00002214 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01002215 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01002216 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 +01002217 } else {
2218 vtype_kind_t vtype_def_tuple, vtype_def_dict;
2219 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
2220 assert(vtype_def_tuple == VTYPE_PYOBJ);
2221 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002222 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 +01002223 }
Damien13ed3a62013-10-08 09:05:10 +01002224 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2225}
2226
Damien George7ff996c2014-09-08 23:05:16 +01002227STATIC 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 +00002228 emit_native_pre(emit);
2229 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
2230 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over);
2231 ASM_MOV_IMM_TO_REG(emit->as, n_closed_over, REG_ARG_2);
2232 } else {
2233 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over + 2);
2234 ASM_MOV_IMM_TO_REG(emit->as, 0x100 | n_closed_over, REG_ARG_2);
2235 }
2236 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)scope->raw_code, REG_ARG_1);
2237 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_MAKE_CLOSURE_FROM_RAW_CODE], MP_F_MAKE_CLOSURE_FROM_RAW_CODE);
2238 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002239}
2240
Damien George7ff996c2014-09-08 23:05:16 +01002241STATIC 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 +00002242 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
2243
Damien Georgee9dac3b2014-09-29 22:10:41 +01002244 // TODO: in viper mode, call special runtime routine with type info for args,
2245 // and wanted type info for return, to remove need for boxing/unboxing
2246
Damien Georgee9dac3b2014-09-29 22:10:41 +01002247 emit_native_pre(emit);
2248 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
2249 if (vtype_fun == VTYPE_BUILTIN_CAST) {
2250 // casting operator
2251 assert(n_positional == 1 && n_keyword == 0);
Damien George78772ad2015-04-06 22:48:21 +01002252 assert(!star_flags);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002253 DEBUG_printf(" cast to %d\n", vtype_fun);
Damien George32444b72015-01-24 23:14:12 +00002254 vtype_kind_t vtype_cast = peek_stack(emit, 1)->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01002255 switch (peek_vtype(emit, 0)) {
2256 case VTYPE_PYOBJ: {
2257 vtype_kind_t vtype;
2258 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2259 emit_pre_pop_discard(emit);
2260 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
2261 emit_post_push_reg(emit, vtype_cast, REG_RET);
2262 break;
2263 }
2264 case VTYPE_BOOL:
2265 case VTYPE_INT:
2266 case VTYPE_UINT:
2267 case VTYPE_PTR:
2268 case VTYPE_PTR8:
2269 case VTYPE_PTR16:
2270 case VTYPE_PTR_NONE:
2271 emit_fold_stack_top(emit, REG_ARG_1);
2272 emit_post_top_set_vtype(emit, vtype_cast);
2273 break;
2274 default:
2275 assert(!"TODO: convert obj to int");
2276 }
2277 } else {
Damien13ed3a62013-10-08 09:05:10 +01002278 assert(vtype_fun == VTYPE_PYOBJ);
Damien George78772ad2015-04-06 22:48:21 +01002279 if (star_flags) {
2280 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
2281 // load dummy entry for non-existent pos_seq
2282 emit_native_load_null(emit);
2283 emit_native_rot_two(emit);
2284 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
2285 // load dummy entry for non-existent kw_dict
2286 emit_native_load_null(emit);
2287 }
2288 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 3); // pointer to args
2289 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);
2290 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2291 } else {
2292 if (n_positional != 0 || n_keyword != 0) {
2293 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2294 }
2295 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2296 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2297 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2298 }
Damien Georgecd82e022014-02-02 13:11:48 +00002299 }
Damien13ed3a62013-10-08 09:05:10 +01002300}
2301
Damien George7ff996c2014-09-08 23:05:16 +01002302STATIC 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 +01002303 if (star_flags) {
2304 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
2305 // load dummy entry for non-existent pos_seq
2306 emit_native_load_null(emit);
2307 emit_native_rot_two(emit);
2308 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
2309 // load dummy entry for non-existent kw_dict
2310 emit_native_load_null(emit);
2311 }
2312 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 4); // pointer to args
2313 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);
2314 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2315 } else {
2316 emit_native_pre(emit);
2317 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
2318 emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, n_positional, REG_ARG_1, n_keyword, REG_ARG_2);
2319 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2320 }
Damien13ed3a62013-10-08 09:05:10 +01002321}
2322
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002323STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002324 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002325 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002326 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2327 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002328 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002329 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002330 } else {
2331 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002332 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002333 } else {
2334 vtype_kind_t vtype;
2335 emit_pre_pop_reg(emit, &vtype, REG_RET);
2336 if (vtype != emit->return_vtype) {
Damien Georgec8b60f02015-04-20 13:29:31 +00002337 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2338 "return expected '%q' but got '%q'",
2339 vtype_to_qstr(emit->return_vtype), vtype_to_qstr(vtype));
Damien Georgee9dac3b2014-09-29 22:10:41 +01002340 }
Damien George2ac4af62014-08-15 16:45:41 +01002341 }
Damien13ed3a62013-10-08 09:05:10 +01002342 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002343 vtype_kind_t vtype;
2344 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002345 assert(vtype == VTYPE_PYOBJ);
2346 }
2347 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002348 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2349 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002350}
2351
Damien George7ff996c2014-09-08 23:05:16 +01002352STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002353 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002354 vtype_kind_t vtype_exc;
2355 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2356 if (vtype_exc != VTYPE_PYOBJ) {
Damien Georgec8b60f02015-04-20 13:29:31 +00002357 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "must raise an object");
Damien George86de21b2014-08-16 22:06:11 +01002358 }
2359 // 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 +01002360 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002361}
Damien Georgeb601d952014-06-30 05:17:25 +01002362
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002363STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002364 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002365 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002366 assert(0);
2367}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002368STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002369 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002370 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002371 assert(0);
2372}
2373
Damien Georgeb601d952014-06-30 05:17:25 +01002374STATIC void emit_native_start_except_handler(emit_t *emit) {
2375 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2376 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2377 // the first 2 elements, so we can get the thrown value.
2378 adjust_stack(emit, 2);
2379 vtype_kind_t vtype_nlr;
2380 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002381 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002382 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
2383}
2384
2385STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002386 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002387}
2388
Damien13ed3a62013-10-08 09:05:10 +01002389const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002390 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002391 emit_native_start_pass,
2392 emit_native_end_pass,
2393 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002394 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002395 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002396
Damien George542bd6b2015-03-26 14:42:40 +00002397 {
2398 emit_native_load_fast,
2399 emit_native_load_deref,
2400 emit_native_load_name,
2401 emit_native_load_global,
2402 },
2403 {
2404 emit_native_store_fast,
2405 emit_native_store_deref,
2406 emit_native_store_name,
2407 emit_native_store_global,
2408 },
2409 {
2410 emit_native_delete_fast,
2411 emit_native_delete_deref,
2412 emit_native_delete_name,
2413 emit_native_delete_global,
2414 },
Damien13ed3a62013-10-08 09:05:10 +01002415
2416 emit_native_label_assign,
2417 emit_native_import_name,
2418 emit_native_import_from,
2419 emit_native_import_star,
2420 emit_native_load_const_tok,
2421 emit_native_load_const_small_int,
Damien13ed3a62013-10-08 09:05:10 +01002422 emit_native_load_const_str,
Damien Georgedab13852015-01-13 15:55:54 +00002423 emit_native_load_const_obj,
Damien George3558f622014-04-20 17:50:40 +01002424 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002425 emit_native_load_attr,
2426 emit_native_load_method,
2427 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002428 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002429 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002430 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002431 emit_native_delete_attr,
2432 emit_native_delete_subscr,
2433 emit_native_dup_top,
2434 emit_native_dup_top_two,
2435 emit_native_pop_top,
2436 emit_native_rot_two,
2437 emit_native_rot_three,
2438 emit_native_jump,
Damien George63f38322015-02-28 15:04:06 +00002439 emit_native_pop_jump_if,
2440 emit_native_jump_if_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002441 emit_native_break_loop,
2442 emit_native_continue_loop,
2443 emit_native_setup_with,
2444 emit_native_with_cleanup,
2445 emit_native_setup_except,
2446 emit_native_setup_finally,
2447 emit_native_end_finally,
2448 emit_native_get_iter,
2449 emit_native_for_iter,
2450 emit_native_for_iter_end,
2451 emit_native_pop_block,
2452 emit_native_pop_except,
2453 emit_native_unary_op,
2454 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002455 emit_native_build_tuple,
2456 emit_native_build_list,
2457 emit_native_list_append,
2458 emit_native_build_map,
2459 emit_native_store_map,
2460 emit_native_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002461 #if MICROPY_PY_BUILTINS_SET
Damien13ed3a62013-10-08 09:05:10 +01002462 emit_native_build_set,
2463 emit_native_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002464 #endif
Damien George83204f32014-12-27 17:20:41 +00002465 #if MICROPY_PY_BUILTINS_SLICE
Damien13ed3a62013-10-08 09:05:10 +01002466 emit_native_build_slice,
Damien George83204f32014-12-27 17:20:41 +00002467 #endif
Damien13ed3a62013-10-08 09:05:10 +01002468 emit_native_unpack_sequence,
2469 emit_native_unpack_ex,
2470 emit_native_make_function,
2471 emit_native_make_closure,
2472 emit_native_call_function,
2473 emit_native_call_method,
2474 emit_native_return_value,
2475 emit_native_raise_varargs,
2476 emit_native_yield_value,
2477 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002478
2479 emit_native_start_except_handler,
2480 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002481};
2482
Damien Georgec90f59e2014-09-06 23:06:36 +01002483#endif