blob: ea81270dbc38ef379a047fb775ac4f4478dff169 [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;
799 for (int i = 0; i < emit->scope->num_pos_args + emit->scope->num_kwonly_args; i++) {
800 ASM_DATA(emit->as, ASM_WORD_SIZE, (mp_uint_t)MP_OBJ_NEW_QSTR(emit->scope->id_info[i].qst));
801 }
802
803 // bytecode prelude: initialise closed over variables
804 emit->prelude_offset = ASM_GET_CODE_POS(emit->as);
805 for (int i = 0; i < emit->scope->id_info_len; i++) {
806 id_info_t *id = &emit->scope->id_info[i];
807 if (id->kind == ID_INFO_KIND_CELL) {
808 assert(id->local_num < 255);
809 ASM_DATA(emit->as, 1, id->local_num); // write the local which should be converted to a cell
810 }
811 }
812 ASM_DATA(emit->as, 1, 255); // end of list sentinel
813 }
814
Damien Georgec90f59e2014-09-06 23:06:36 +0100815 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100816
817 // check stack is back to zero size
818 if (emit->stack_size != 0) {
Damien Georgee72cda92015-04-11 12:15:47 +0100819 mp_printf(&mp_plat_print, "ERROR: stack size not back to zero; got %d\n", emit->stack_size);
Damien13ed3a62013-10-08 09:05:10 +0100820 }
821
Damien George36db6bc2014-05-07 17:24:22 +0100822 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100823 void *f = ASM_GET_CODE(emit->as);
824 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100825
826 // compute type signature
Damien Georgee9dac3b2014-09-29 22:10:41 +0100827 // note that the lower 2 bits of a vtype are tho correct MP_NATIVE_TYPE_xxx
Damien George2ac4af62014-08-15 16:45:41 +0100828 mp_uint_t type_sig = emit->return_vtype & 3;
829 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
830 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
831 }
832
Damien George99886182015-04-06 22:38:53 +0100833 mp_emit_glue_assign_native(emit->scope->raw_code,
834 emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY,
835 f, f_len, emit->scope->num_pos_args, emit->scope->num_kwonly_args,
836 emit->scope->scope_flags, type_sig);
Damien13ed3a62013-10-08 09:05:10 +0100837 }
838}
839
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200840STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100841 return emit->last_emit_was_return_value;
842}
843
Damien Georged6230f62014-09-23 14:10:03 +0000844STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georged6230f62014-09-23 14:10:03 +0000845 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100846 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100847 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100848 emit->scope->stack_size = emit->stack_size;
849 }
Damien George21ca2d72014-10-19 19:00:51 +0100850#ifdef DEBUG_PRINT
851 DEBUG_printf(" adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
852 for (int i = 0; i < emit->stack_size; i++) {
853 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000854 DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->data.u_reg);
Damien George21ca2d72014-10-19 19:00:51 +0100855 }
856 DEBUG_printf("\n");
857#endif
Damien13ed3a62013-10-08 09:05:10 +0100858}
859
Damien Georged6230f62014-09-23 14:10:03 +0000860STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien George21ca2d72014-10-19 19:00:51 +0100861 DEBUG_printf("adjust_stack_size(" INT_FMT ")\n", delta);
Damien Georged6230f62014-09-23 14:10:03 +0000862 // If we are adjusting the stack in a positive direction (pushing) then we
863 // need to fill in values for the stack kind and vtype of the newly-pushed
864 // entries. These should be set to "value" (ie not reg or imm) because we
865 // should only need to adjust the stack due to a jump to this part in the
866 // code (and hence we have settled the stack before the jump).
867 for (mp_int_t i = 0; i < delta; i++) {
868 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
869 si->kind = STACK_VALUE;
Damien George21ca2d72014-10-19 19:00:51 +0100870 // TODO we don't know the vtype to use here. At the moment this is a
871 // hack to get the case of multi comparison working.
872 if (delta == 1) {
873 si->vtype = emit->saved_stack_vtype;
874 } else {
875 si->vtype = VTYPE_PYOBJ;
876 }
Damien Georged6230f62014-09-23 14:10:03 +0000877 }
878 adjust_stack(emit, delta);
879}
880
881STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000882 (void)emit;
883 (void)source_line;
Damien Georged6230f62014-09-23 14:10:03 +0000884}
885
Damienff8ed772013-10-08 22:18:32 +0100886/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200887STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100888 adjust_stack(emit, stack_size_delta);
889 emit->last_emit_was_return_value = false;
890}
Damienff8ed772013-10-08 22:18:32 +0100891*/
Damien13ed3a62013-10-08 09:05:10 +0100892
Damienff8ed772013-10-08 22:18:32 +0100893// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000894STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100895 emit->last_emit_was_return_value = false;
896 // settle the stack
897 /*
898 if (regs_needed != 0) {
899 for (int i = 0; i < emit->stack_size; i++) {
900 switch (emit->stack_info[i].kind) {
901 case STACK_VALUE:
902 break;
903
904 case STACK_REG:
905 // TODO only push reg if in regs_needed
906 emit->stack_info[i].kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000907 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100908 break;
909
910 case STACK_IMM:
911 // don't think we ever need to push imms for settling
912 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
913 break;
914 }
915 }
916 }
917 */
Damien13ed3a62013-10-08 09:05:10 +0100918}
919
Damien George3112cde2014-09-29 18:45:42 +0100920// depth==0 is top, depth==1 is before top, etc
921STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
922 return &emit->stack_info[emit->stack_size - 1 - depth];
923}
924
925// depth==0 is top, depth==1 is before top, etc
926STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
927 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100928}
Damien13ed3a62013-10-08 09:05:10 +0100929
Damiend2755ec2013-10-16 23:58:48 +0100930// pos=1 is TOS, pos=2 is next, etc
931// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200932STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100933 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100934 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100935 if (i != skip_stack_pos) {
936 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000937 if (si->kind == STACK_REG && si->data.u_reg == reg_needed) {
Damiend2755ec2013-10-16 23:58:48 +0100938 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000939 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100940 }
Damienff8ed772013-10-08 22:18:32 +0100941 }
942 }
943}
Damien13ed3a62013-10-08 09:05:10 +0100944
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200945STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100946 for (int i = 0; i < emit->stack_size; i++) {
947 stack_info_t *si = &emit->stack_info[i];
948 if (si->kind == STACK_REG) {
949 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000950 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100951 }
Damien13ed3a62013-10-08 09:05:10 +0100952 }
953}
954
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200955STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +0000956 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +0100957 for (int i = 0; i < emit->stack_size; i++) {
958 stack_info_t *si = &emit->stack_info[i];
959 if (si->kind == STACK_REG) {
Damien George32444b72015-01-24 23:14:12 +0000960 DEBUG_printf(" reg(%u) to local(%u)\n", si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100961 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000962 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100963 }
964 }
965 for (int i = 0; i < emit->stack_size; i++) {
966 stack_info_t *si = &emit->stack_info[i];
967 if (si->kind == STACK_IMM) {
Damien George32444b72015-01-24 23:14:12 +0000968 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->data.u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +0100969 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000970 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->data.u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +0100971 }
972 }
973}
974
975// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200976STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100977 need_reg_single(emit, reg_dest, pos);
978 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +0100979 *vtype = si->vtype;
980 switch (si->kind) {
981 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +0100982 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100983 break;
984
Damienff8ed772013-10-08 22:18:32 +0100985 case STACK_REG:
Damien George32444b72015-01-24 23:14:12 +0000986 if (si->data.u_reg != reg_dest) {
987 ASM_MOV_REG_REG(emit->as, reg_dest, si->data.u_reg);
Damien13ed3a62013-10-08 09:05:10 +0100988 }
989 break;
990
Damienff8ed772013-10-08 22:18:32 +0100991 case STACK_IMM:
Damien George32444b72015-01-24 23:14:12 +0000992 ASM_MOV_IMM_TO_REG(emit->as, si->data.u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100993 break;
994 }
Damien13ed3a62013-10-08 09:05:10 +0100995}
996
Damien Georgee9dac3b2014-09-29 22:10:41 +0100997// does an efficient X=pop(); discard(); push(X)
998// needs a (non-temp) register in case the poped element was stored in the stack
999STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
1000 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
1001 si[0] = si[1];
1002 if (si->kind == STACK_VALUE) {
1003 // if folded element was on the stack we need to put it in a register
1004 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
1005 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +00001006 si->data.u_reg = reg_dest;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001007 }
1008 adjust_stack(emit, -1);
1009}
1010
1011// If stacked value is in a register and the register is not r1 or r2, then
1012// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
1013STATIC 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 +01001014 emit->last_emit_was_return_value = false;
1015 stack_info_t *si = peek_stack(emit, 0);
Damien George32444b72015-01-24 23:14:12 +00001016 if (si->kind == STACK_REG && si->data.u_reg != not_r1 && si->data.u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +01001017 *vtype = si->vtype;
Damien George32444b72015-01-24 23:14:12 +00001018 *reg_dest = si->data.u_reg;
Damien George3112cde2014-09-29 18:45:42 +01001019 need_reg_single(emit, *reg_dest, 1);
1020 } else {
1021 emit_access_stack(emit, 1, vtype, *reg_dest);
1022 }
1023 adjust_stack(emit, -1);
1024}
1025
Damien Georgee6ce10a2014-09-06 18:38:20 +01001026STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +01001027 emit->last_emit_was_return_value = false;
1028 adjust_stack(emit, -1);
1029}
1030
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001031STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +01001032 emit->last_emit_was_return_value = false;
1033 emit_access_stack(emit, 1, vtype, reg_dest);
1034 adjust_stack(emit, -1);
1035}
1036
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001037STATIC 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 +01001038 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001039 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +01001040}
1041
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001042STATIC 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 +01001043 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001044 emit_pre_pop_reg(emit, vtypeb, regb);
1045 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001046}
1047
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001048STATIC void emit_post(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001049 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001050}
1051
Damien Georgee9dac3b2014-09-29 22:10:41 +01001052STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
1053 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
1054 si->vtype = new_vtype;
1055}
1056
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001057STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +01001058 stack_info_t *si = &emit->stack_info[emit->stack_size];
1059 si->vtype = vtype;
1060 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +00001061 si->data.u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +01001062 adjust_stack(emit, 1);
1063}
1064
Damien George40f3c022014-07-03 13:25:24 +01001065STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +01001066 stack_info_t *si = &emit->stack_info[emit->stack_size];
1067 si->vtype = vtype;
1068 si->kind = STACK_IMM;
Damien George32444b72015-01-24 23:14:12 +00001069 si->data.u_imm = imm;
Damienff8ed772013-10-08 22:18:32 +01001070 adjust_stack(emit, 1);
1071}
1072
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001073STATIC 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 +01001074 emit_post_push_reg(emit, vtypea, rega);
1075 emit_post_push_reg(emit, vtypeb, regb);
1076}
1077
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001078STATIC 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 +01001079 emit_post_push_reg(emit, vtypea, rega);
1080 emit_post_push_reg(emit, vtypeb, regb);
1081 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001082}
1083
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001084STATIC 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 +01001085 emit_post_push_reg(emit, vtypea, rega);
1086 emit_post_push_reg(emit, vtypeb, regb);
1087 emit_post_push_reg(emit, vtypec, regc);
1088 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +01001089}
1090
Damien George7fe21912014-08-16 22:31:57 +01001091STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +01001092 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001093 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001094}
1095
Damien George7fe21912014-08-16 22:31:57 +01001096STATIC 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 +01001097 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001098 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
1099 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001100}
1101
Damien George40f3c022014-07-03 13:25:24 +01001102// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001103STATIC 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 +01001104 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001105 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
1106 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +01001107}
1108
Damien George7fe21912014-08-16 22:31:57 +01001109STATIC 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 +00001110 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001111 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1112 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1113 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +00001114}
1115
Damien George40f3c022014-07-03 13:25:24 +01001116// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001117STATIC 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 +01001118 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001119 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1120 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1121 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
1122 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +01001123}
1124
Damien George86de21b2014-08-16 22:06:11 +01001125// vtype of all n_pop objects is VTYPE_PYOBJ
1126// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
1127// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
1128// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
1129STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
1130 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +01001131
Damien George86de21b2014-08-16 22:06:11 +01001132 // First, store any immediate values to their respective place on the stack.
1133 for (mp_uint_t i = 0; i < n_pop; i++) {
1134 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1135 // must push any imm's to stack
1136 // must convert them to VTYPE_PYOBJ for viper code
1137 if (si->kind == STACK_IMM) {
1138 si->kind = STACK_VALUE;
1139 switch (si->vtype) {
1140 case VTYPE_PYOBJ:
Damien George32444b72015-01-24 23:14:12 +00001141 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 +01001142 break;
1143 case VTYPE_BOOL:
Damien George32444b72015-01-24 23:14:12 +00001144 if (si->data.u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +01001145 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 +01001146 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +01001147 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 +01001148 }
1149 si->vtype = VTYPE_PYOBJ;
1150 break;
1151 case VTYPE_INT:
1152 case VTYPE_UINT:
Damien George32444b72015-01-24 23:14:12 +00001153 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 +01001154 si->vtype = VTYPE_PYOBJ;
1155 break;
1156 default:
1157 // not handled
1158 assert(0);
1159 }
1160 }
1161
1162 // verify that this value is on the stack
1163 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +01001164 }
Damien George86de21b2014-08-16 22:06:11 +01001165
1166 // Second, convert any non-VTYPE_PYOBJ to that type.
1167 for (mp_uint_t i = 0; i < n_pop; i++) {
1168 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1169 if (si->vtype != VTYPE_PYOBJ) {
1170 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +01001171 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001172 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 +01001173 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001174 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001175 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001176 }
1177 }
1178
1179 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1180 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +01001181 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001182}
1183
1184// vtype of all n_push objects is VTYPE_PYOBJ
1185STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1186 need_reg_all(emit);
1187 for (mp_uint_t i = 0; i < n_push; i++) {
1188 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1189 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1190 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001191 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001192 adjust_stack(emit, n_push);
1193}
1194
Damien George7ff996c2014-09-08 23:05:16 +01001195STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001196 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001197 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001198 // need to commit stack because we can jump here from elsewhere
1199 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001200 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001201 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001202}
1203
Damien Georgecdd96df2014-04-06 12:58:40 +01001204STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1205 DEBUG_printf("import_name %s\n", qstr_str(qst));
1206 vtype_kind_t vtype_fromlist;
1207 vtype_kind_t vtype_level;
1208 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1209 assert(vtype_fromlist == VTYPE_PYOBJ);
1210 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001211 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001212 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001213}
1214
Damien Georgecdd96df2014-04-06 12:58:40 +01001215STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1216 DEBUG_printf("import_from %s\n", qstr_str(qst));
1217 emit_native_pre(emit);
1218 vtype_kind_t vtype_module;
1219 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1220 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001221 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001222 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001223}
1224
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001225STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001226 DEBUG_printf("import_star\n");
1227 vtype_kind_t vtype_module;
1228 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1229 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001230 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001231 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001232}
1233
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001234STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001235 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001236 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001237 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001238 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001239 if (emit->do_viper_types) {
1240 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001241 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1242 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1243 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien George3da677e2015-01-29 15:13:40 +00001244 no_other_choice1:
1245 case MP_TOKEN_ELLIPSIS: vtype = VTYPE_PYOBJ; val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1246 default: assert(0); goto no_other_choice1; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001247 }
1248 } else {
1249 vtype = VTYPE_PYOBJ;
1250 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001251 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1252 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1253 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien George3da677e2015-01-29 15:13:40 +00001254 no_other_choice2:
1255 case MP_TOKEN_ELLIPSIS: val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1256 default: assert(0); goto no_other_choice2; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001257 }
1258 }
1259 emit_post_push_imm(emit, vtype, val);
1260}
1261
Damien George40f3c022014-07-03 13:25:24 +01001262STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001263 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001264 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001265 if (emit->do_viper_types) {
1266 emit_post_push_imm(emit, VTYPE_INT, arg);
1267 } else {
Damien George2686f9b2015-04-01 00:12:43 +01001268 emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_SMALL_INT(arg));
Damien13ed3a62013-10-08 09:05:10 +01001269 }
1270}
1271
Damien George7ff996c2014-09-08 23:05:16 +01001272STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001273 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001274 // TODO: Eventually we want to be able to work with raw pointers in viper to
1275 // do native array access. For now we just load them as any other object.
1276 /*
Damien13ed3a62013-10-08 09:05:10 +01001277 if (emit->do_viper_types) {
1278 // not implemented properly
1279 // load a pointer to the asciiz string?
1280 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001281 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001282 } else
1283 */
1284 {
Damien Georgeb601d952014-06-30 05:17:25 +01001285 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +01001286 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_BYTES, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001287 } else {
Damien George7ff996c2014-09-08 23:05:16 +01001288 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001289 }
Damien13ed3a62013-10-08 09:05:10 +01001290 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1291 }
1292}
1293
Damien Georgedab13852015-01-13 15:55:54 +00001294STATIC void emit_native_load_const_obj(emit_t *emit, void *obj) {
1295 emit_native_pre(emit);
Damien George2127e9a2015-01-14 00:11:09 +00001296 need_reg_single(emit, REG_RET, 0);
Damien Georgedab13852015-01-13 15:55:54 +00001297 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)obj, REG_RET);
1298 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1299}
1300
Damien George3558f622014-04-20 17:50:40 +01001301STATIC void emit_native_load_null(emit_t *emit) {
1302 emit_native_pre(emit);
1303 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1304}
1305
Damien George0abb5602015-01-16 12:24:49 +00001306STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
1307 DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
Damien13ed3a62013-10-08 09:05:10 +01001308 vtype_kind_t vtype = emit->local_vtype[local_num];
1309 if (vtype == VTYPE_UNBOUND) {
Damien Georgec8b60f02015-04-20 13:29:31 +00001310 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "local '%q' used before type known", qst);
Damien13ed3a62013-10-08 09:05:10 +01001311 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001312 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001313 if (local_num == 0) {
1314 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001315 } else if (local_num == 1) {
1316 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1317 } else if (local_num == 2) {
1318 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001319 } else {
Damien George0b610de2014-09-29 16:25:04 +01001320 need_reg_single(emit, REG_TEMP0, 0);
Damien George99886182015-04-06 22:38:53 +01001321 if (emit->do_viper_types) {
1322 ASM_MOV_LOCAL_TO_REG(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1323 } else {
1324 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - local_num, REG_TEMP0);
1325 }
Damien George0b610de2014-09-29 16:25:04 +01001326 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001327 }
Damien13ed3a62013-10-08 09:05:10 +01001328}
1329
Damien George7ff996c2014-09-08 23:05:16 +01001330STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001331 DEBUG_printf("load_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1332 need_reg_single(emit, REG_RET, 0);
1333 emit_native_load_fast(emit, qst, local_num);
1334 vtype_kind_t vtype;
1335 int reg_base = REG_RET;
1336 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1337 ASM_LOAD_REG_REG_OFFSET(emit->as, REG_RET, reg_base, 1);
1338 // closed over vars are always Python objects
1339 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien9ecbcff2013-12-11 00:41:43 +00001340}
1341
Damien George7ff996c2014-09-08 23:05:16 +01001342STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001343 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001344 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001345 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001346 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1347}
1348
Damien George7ff996c2014-09-08 23:05:16 +01001349STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001350 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001351 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001352 // check for builtin casting operators
1353 if (emit->do_viper_types && qst == MP_QSTR_int) {
1354 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1355 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1356 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1357 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1358 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1359 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1360 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1361 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1362 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
1363 } else {
1364 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1365 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1366 }
Damien13ed3a62013-10-08 09:05:10 +01001367}
1368
Damien George7ff996c2014-09-08 23:05:16 +01001369STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001370 // depends on type of subject:
1371 // - integer, function, pointer to integers: error
1372 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001373 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001374 vtype_kind_t vtype_base;
1375 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1376 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001377 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001378 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1379}
1380
Damien George7ff996c2014-09-08 23:05:16 +01001381STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001382 vtype_kind_t vtype_base;
1383 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1384 assert(vtype_base == VTYPE_PYOBJ);
1385 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001386 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001387}
1388
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001389STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001390 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001391 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001392 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001393}
1394
Damien George729f7b42014-04-17 22:10:53 +01001395STATIC void emit_native_load_subscr(emit_t *emit) {
Damien George91cfd412014-10-12 16:59:29 +01001396 DEBUG_printf("load_subscr\n");
1397 // need to compile: base[index]
1398
1399 // pop: index, base
1400 // optimise case where index is an immediate
1401 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1402
1403 if (vtype_base == VTYPE_PYOBJ) {
1404 // standard Python call
1405 vtype_kind_t vtype_index;
1406 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1);
1407 assert(vtype_index == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001408 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 +01001409 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1410 } else {
Damien George91cfd412014-10-12 16:59:29 +01001411 // viper load
1412 // TODO The different machine architectures have very different
1413 // capabilities and requirements for loads, so probably best to
1414 // write a completely separate load-optimiser for each one.
1415 stack_info_t *top = peek_stack(emit, 0);
1416 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1417 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001418 mp_int_t index_value = top->data.u_imm;
Damien George91cfd412014-10-12 16:59:29 +01001419 emit_pre_pop_discard(emit); // discard index
1420 int reg_base = REG_ARG_1;
1421 int reg_index = REG_ARG_2;
1422 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
1423 switch (vtype_base) {
1424 case VTYPE_PTR8: {
1425 // pointer to 8-bit memory
1426 // TODO optimise to use thumb ldrb r1, [r2, r3]
1427 if (index_value != 0) {
1428 // index is non-zero
1429 #if N_THUMB
1430 if (index_value > 0 && index_value < 32) {
1431 asm_thumb_ldrb_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1432 break;
1433 }
1434 #endif
1435 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
1436 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1437 reg_base = reg_index;
1438 }
1439 ASM_LOAD8_REG_REG(emit->as, REG_RET, reg_base); // load from (base+index)
1440 break;
1441 }
1442 case VTYPE_PTR16: {
1443 // pointer to 16-bit memory
1444 if (index_value != 0) {
1445 // index is a non-zero immediate
1446 #if N_THUMB
1447 if (index_value > 0 && index_value < 32) {
1448 asm_thumb_ldrh_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1449 break;
1450 }
1451 #endif
1452 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
1453 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1454 reg_base = reg_index;
1455 }
1456 ASM_LOAD16_REG_REG(emit->as, REG_RET, reg_base); // load from (base+2*index)
1457 break;
1458 }
1459 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001460 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1461 "can't load from '%q'", vtype_to_qstr(vtype_base));
Damien George91cfd412014-10-12 16:59:29 +01001462 }
1463 } else {
1464 // index is not an immediate
1465 vtype_kind_t vtype_index;
1466 int reg_index = REG_ARG_2;
1467 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
1468 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1469 switch (vtype_base) {
1470 case VTYPE_PTR8: {
1471 // pointer to 8-bit memory
1472 // TODO optimise to use thumb ldrb r1, [r2, r3]
1473 assert(vtype_index == VTYPE_INT);
1474 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1475 ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
1476 break;
1477 }
1478 case VTYPE_PTR16: {
1479 // pointer to 16-bit memory
1480 assert(vtype_index == VTYPE_INT);
1481 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1482 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1483 ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
1484 break;
1485 }
1486 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001487 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1488 "can't load from '%q'", vtype_to_qstr(vtype_base));
Damien George91cfd412014-10-12 16:59:29 +01001489 }
1490 }
1491 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
Damien George729f7b42014-04-17 22:10:53 +01001492 }
1493}
1494
Damien George7ff996c2014-09-08 23:05:16 +01001495STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001496 vtype_kind_t vtype;
Damien13ed3a62013-10-08 09:05:10 +01001497 if (local_num == 0) {
1498 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001499 } else if (local_num == 1) {
1500 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1501 } else if (local_num == 2) {
1502 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001503 } else {
Damien George0b610de2014-09-29 16:25:04 +01001504 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
Damien George99886182015-04-06 22:38:53 +01001505 if (emit->do_viper_types) {
1506 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1507 } else {
1508 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, STATE_START + emit->n_state - 1 - local_num);
1509 }
Damien13ed3a62013-10-08 09:05:10 +01001510 }
Damien13ed3a62013-10-08 09:05:10 +01001511 emit_post(emit);
1512
1513 // check types
1514 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1515 // first time this local is assigned, so give it a type of the object stored in it
1516 emit->local_vtype[local_num] = vtype;
1517 } else if (emit->local_vtype[local_num] != vtype) {
1518 // type of local is not the same as object stored in it
Damien Georgec8b60f02015-04-20 13:29:31 +00001519 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1520 "local '%q' has type '%q' but source is '%q'",
1521 qst, vtype_to_qstr(emit->local_vtype[local_num]), vtype_to_qstr(vtype));
Damien13ed3a62013-10-08 09:05:10 +01001522 }
1523}
1524
Damien George7ff996c2014-09-08 23:05:16 +01001525STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001526 DEBUG_printf("store_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1527 need_reg_single(emit, REG_TEMP0, 0);
1528 need_reg_single(emit, REG_TEMP1, 0);
1529 emit_native_load_fast(emit, qst, local_num);
1530 vtype_kind_t vtype;
1531 int reg_base = REG_TEMP0;
1532 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1533 int reg_src = REG_TEMP1;
1534 emit_pre_pop_reg_flexible(emit, &vtype, &reg_src, reg_base, reg_base);
1535 ASM_STORE_REG_REG_OFFSET(emit->as, reg_src, reg_base, 1);
1536 emit_post(emit);
Damien9ecbcff2013-12-11 00:41:43 +00001537}
1538
Damien George7ff996c2014-09-08 23:05:16 +01001539STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001540 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001541 vtype_kind_t vtype;
1542 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1543 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001544 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001545 emit_post(emit);
1546}
1547
Damien George7ff996c2014-09-08 23:05:16 +01001548STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001549 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001550 if (vtype == VTYPE_PYOBJ) {
1551 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1552 } else {
1553 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001554 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 +01001555 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001556 }
Damien George7ff996c2014-09-08 23:05:16 +01001557 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001558 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001559}
1560
Damien George7ff996c2014-09-08 23:05:16 +01001561STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001562 vtype_kind_t vtype_base, vtype_val;
1563 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1564 assert(vtype_base == VTYPE_PYOBJ);
1565 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001566 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001567 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001568}
1569
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001570STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001571 DEBUG_printf("store_subscr\n");
1572 // need to compile: base[index] = value
1573
1574 // pop: index, base, value
1575 // optimise case where index is an immediate
1576 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1577
1578 if (vtype_base == VTYPE_PYOBJ) {
1579 // standard Python call
1580 vtype_kind_t vtype_index, vtype_value;
1581 emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3);
1582 assert(vtype_index == VTYPE_PYOBJ);
1583 assert(vtype_value == VTYPE_PYOBJ);
1584 emit_call(emit, MP_F_OBJ_SUBSCR);
1585 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001586 // viper store
1587 // TODO The different machine architectures have very different
1588 // capabilities and requirements for stores, so probably best to
1589 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001590 stack_info_t *top = peek_stack(emit, 0);
1591 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1592 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001593 mp_int_t index_value = top->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001594 emit_pre_pop_discard(emit); // discard index
1595 vtype_kind_t vtype_value;
1596 int reg_base = REG_ARG_1;
1597 int reg_index = REG_ARG_2;
1598 int reg_value = REG_ARG_3;
1599 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001600 #if N_X86
1601 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1602 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1603 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001604 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001605 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001606 switch (vtype_base) {
1607 case VTYPE_PTR8: {
1608 // pointer to 8-bit memory
1609 // TODO optimise to use thumb strb r1, [r2, r3]
1610 if (index_value != 0) {
1611 // index is non-zero
1612 #if N_THUMB
1613 if (index_value > 0 && index_value < 32) {
1614 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1615 break;
1616 }
1617 #endif
1618 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001619 #if N_ARM
1620 asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1621 return;
1622 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001623 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1624 reg_base = reg_index;
1625 }
1626 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1627 break;
1628 }
1629 case VTYPE_PTR16: {
1630 // pointer to 16-bit memory
1631 if (index_value != 0) {
1632 // index is a non-zero immediate
1633 #if N_THUMB
1634 if (index_value > 0 && index_value < 32) {
1635 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1636 break;
1637 }
1638 #endif
1639 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001640 #if N_ARM
1641 asm_arm_strh_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1642 return;
1643 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001644 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1645 reg_base = reg_index;
1646 }
1647 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1648 break;
1649 }
1650 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001651 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1652 "can't store to '%q'", vtype_to_qstr(vtype_base));
Damien Georgee9dac3b2014-09-29 22:10:41 +01001653 }
1654 } else {
1655 // index is not an immediate
1656 vtype_kind_t vtype_index, vtype_value;
1657 int reg_index = REG_ARG_2;
1658 int reg_value = REG_ARG_3;
1659 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1660 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001661 #if N_X86
1662 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1663 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1664 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001665 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001666 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001667 switch (vtype_base) {
1668 case VTYPE_PTR8: {
1669 // pointer to 8-bit memory
1670 // TODO optimise to use thumb strb r1, [r2, r3]
1671 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001672 #if N_ARM
1673 asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1674 break;
1675 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001676 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1677 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1678 break;
1679 }
1680 case VTYPE_PTR16: {
1681 // pointer to 16-bit memory
1682 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001683 #if N_ARM
1684 asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1685 break;
1686 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001687 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1688 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1689 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1690 break;
1691 }
1692 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001693 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1694 "can't store to '%q'", vtype_to_qstr(vtype_base));
Damien Georgee9dac3b2014-09-29 22:10:41 +01001695 }
1696 }
1697
1698 }
Damien13ed3a62013-10-08 09:05:10 +01001699}
1700
Damien George7ff996c2014-09-08 23:05:16 +01001701STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George2cc54732015-04-03 14:29:30 +01001702 // TODO: This is not compliant implementation. We could use MP_OBJ_SENTINEL
1703 // to mark deleted vars but then every var would need to be checked on
1704 // each access. Very inefficient, so just set value to None to enable GC.
1705 emit_native_load_const_tok(emit, MP_TOKEN_KW_NONE);
1706 emit_native_store_fast(emit, qst, local_num);
Damien13ed3a62013-10-08 09:05:10 +01001707}
1708
Damien George7ff996c2014-09-08 23:05:16 +01001709STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001710 // TODO implement me!
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001711 (void)emit;
1712 (void)qst;
1713 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001714}
1715
Damien Georgee6ce10a2014-09-06 18:38:20 +01001716STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1717 emit_native_pre(emit);
1718 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1719 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001720}
1721
Damien Georgee6ce10a2014-09-06 18:38:20 +01001722STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1723 emit_native_pre(emit);
1724 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1725 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001726}
1727
Damien Georgee6ce10a2014-09-06 18:38:20 +01001728STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001729 vtype_kind_t vtype_base;
1730 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1731 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001732 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 +01001733 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001734}
1735
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001736STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001737 vtype_kind_t vtype_index, vtype_base;
1738 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1739 assert(vtype_index == VTYPE_PYOBJ);
1740 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001741 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 +01001742}
1743
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001744STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001745 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001746 vtype_kind_t vtype;
Damien George21ca2d72014-10-19 19:00:51 +01001747 int reg = REG_TEMP0;
1748 emit_pre_pop_reg_flexible(emit, &vtype, &reg, -1, -1);
1749 emit_post_push_reg_reg(emit, vtype, reg, vtype, reg);
Damien13ed3a62013-10-08 09:05:10 +01001750}
1751
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001752STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001753 vtype_kind_t vtype0, vtype1;
1754 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1755 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1756}
1757
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001758STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001759 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001760 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001761 emit_post(emit);
1762}
1763
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001764STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001765 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001766 vtype_kind_t vtype0, vtype1;
1767 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1768 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001769}
1770
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001771STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001772 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001773 vtype_kind_t vtype0, vtype1, vtype2;
1774 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1775 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1776}
1777
Damien George7ff996c2014-09-08 23:05:16 +01001778STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001779 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001780 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001781 // need to commit stack because we are jumping elsewhere
1782 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001783 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001784 emit_post(emit);
1785}
1786
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001787STATIC void emit_native_jump_helper(emit_t *emit, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001788 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgec8b60f02015-04-20 13:29:31 +00001789 if (vtype == VTYPE_PYOBJ) {
1790 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1791 if (!pop) {
1792 adjust_stack(emit, 1);
1793 }
1794 emit_call(emit, MP_F_OBJ_IS_TRUE);
1795 } else {
1796 emit_pre_pop_reg(emit, &vtype, REG_RET);
1797 if (!pop) {
1798 adjust_stack(emit, 1);
1799 }
1800 if (!(vtype == VTYPE_BOOL || vtype == VTYPE_INT || vtype == VTYPE_UINT)) {
1801 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1802 "can't implicitly convert '%q' to 'bool'", vtype_to_qstr(vtype));
1803 }
Damien13ed3a62013-10-08 09:05:10 +01001804 }
Damien George21ca2d72014-10-19 19:00:51 +01001805 // For non-pop need to save the vtype so that emit_native_adjust_stack_size
1806 // can use it. This is a bit of a hack.
1807 if (!pop) {
1808 emit->saved_stack_vtype = vtype;
1809 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001810 // need to commit stack because we may jump elsewhere
1811 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001812}
1813
Damien George63f38322015-02-28 15:04:06 +00001814STATIC void emit_native_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
1815 DEBUG_printf("pop_jump_if(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001816 emit_native_jump_helper(emit, true);
Damien George63f38322015-02-28 15:04:06 +00001817 if (cond) {
1818 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1819 } else {
1820 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1821 }
Damien1a6633a2013-11-03 13:58:19 +00001822 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001823}
Damien1a6633a2013-11-03 13:58:19 +00001824
Damien George63f38322015-02-28 15:04:06 +00001825STATIC void emit_native_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
1826 DEBUG_printf("jump_if_or_pop(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001827 emit_native_jump_helper(emit, false);
Damien George63f38322015-02-28 15:04:06 +00001828 if (cond) {
1829 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1830 } else {
1831 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1832 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001833 adjust_stack(emit, -1);
1834 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001835}
1836
Damien George7ff996c2014-09-08 23:05:16 +01001837STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001838 (void)except_depth;
Damien George25c84642014-05-30 15:20:41 +01001839 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001840}
Damien Georgea32c1e42014-05-07 18:30:52 +01001841
Damien George7ff996c2014-09-08 23:05:16 +01001842STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001843 (void)except_depth;
Damien Georgea32c1e42014-05-07 18:30:52 +01001844 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001845}
Damien Georgea32c1e42014-05-07 18:30:52 +01001846
Damien George7ff996c2014-09-08 23:05:16 +01001847STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001848 // not supported, or could be with runtime call
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001849 (void)emit;
1850 (void)label;
Damien13ed3a62013-10-08 09:05:10 +01001851 assert(0);
1852}
Damien Georgeb601d952014-06-30 05:17:25 +01001853
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001854STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001855 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001856 assert(0);
1857}
Damien Georgeb601d952014-06-30 05:17:25 +01001858
Damien George7ff996c2014-09-08 23:05:16 +01001859STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001860 emit_native_pre(emit);
1861 // need to commit stack because we may jump elsewhere
1862 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001863 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 +01001864 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001865 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001866 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001867}
Damien Georgeb601d952014-06-30 05:17:25 +01001868
Damien George7ff996c2014-09-08 23:05:16 +01001869STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001870 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001871}
Damien Georgeb601d952014-06-30 05:17:25 +01001872
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001873STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgeb6e6b522015-01-21 17:00:01 +00001874 // logic:
1875 // exc = pop_stack
1876 // if exc == None: pass
1877 // else: raise exc
1878 // the check if exc is None is done in the MP_F_NATIVE_RAISE stub
1879 vtype_kind_t vtype;
1880 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1881 emit_call(emit, MP_F_NATIVE_RAISE);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001882 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001883}
Damiend2755ec2013-10-16 23:58:48 +01001884
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001885STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001886 // perhaps the difficult one, as we want to rewrite for loops using native code
1887 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001888
1889 vtype_kind_t vtype;
1890 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1891 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001892 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01001893 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001894}
Damiend2755ec2013-10-16 23:58:48 +01001895
Damien George7ff996c2014-09-08 23:05:16 +01001896STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001897 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001898 vtype_kind_t vtype;
1899 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1900 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001901 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01001902 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
1903 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01001904 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1905}
1906
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001907STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001908 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001909 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001910 adjust_stack(emit, -1);
1911 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001912}
1913
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001914STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001915 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001916 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001917 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001918 emit_post(emit);
1919}
1920
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001921STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001922 (void)emit;
Damien Georgeb601d952014-06-30 05:17:25 +01001923 /*
1924 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)));
Damien Georgeb601d952014-06-30 05:17:25 +01001927 emit_post(emit);
1928 */
Damien13ed3a62013-10-08 09:05:10 +01001929}
1930
Damien Georged17926d2014-03-30 13:35:08 +01001931STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001932 vtype_kind_t vtype;
1933 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1934 assert(vtype == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001935 if (op == MP_UNARY_OP_NOT) {
Damien Georged6230f62014-09-23 14:10:03 +00001936 // we need to synthesise this operation by converting to bool first
1937 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
Damien George3112cde2014-09-29 18:45:42 +01001938 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgeb601d952014-06-30 05:17:25 +01001939 }
Damien Georged6230f62014-09-23 14:10:03 +00001940 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1941 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001942}
1943
Damien Georged17926d2014-03-30 13:35:08 +01001944STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001945 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01001946 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
1947 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01001948 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01001949 #if N_X64 || N_X86
1950 // special cases for x86 and shifting
1951 if (op == MP_BINARY_OP_LSHIFT
1952 || op == MP_BINARY_OP_INPLACE_LSHIFT
1953 || op == MP_BINARY_OP_RSHIFT
1954 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1955 #if N_X64
1956 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
1957 #else
1958 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
1959 #endif
1960 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1961 ASM_LSL_REG(emit->as, REG_RET);
1962 } else {
1963 ASM_ASR_REG(emit->as, REG_RET);
1964 }
1965 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
1966 return;
1967 }
1968 #endif
1969 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001970 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001971 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
1972 if (0) {
1973 // dummy
1974 #if !(N_X64 || N_X86)
1975 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1976 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00001977 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001978 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1979 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1980 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1981 #endif
Damien George1ef23482014-10-12 14:21:06 +01001982 } else if (op == MP_BINARY_OP_OR || op == MP_BINARY_OP_INPLACE_OR) {
1983 ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1984 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1985 } else if (op == MP_BINARY_OP_XOR || op == MP_BINARY_OP_INPLACE_XOR) {
1986 ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1987 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1988 } else if (op == MP_BINARY_OP_AND || op == MP_BINARY_OP_INPLACE_AND) {
1989 ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1990 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001991 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
1992 ASM_ADD_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_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
1995 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1996 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1997 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
1998 // comparison ops are (in enum order):
1999 // MP_BINARY_OP_LESS
2000 // MP_BINARY_OP_MORE
2001 // MP_BINARY_OP_EQUAL
2002 // MP_BINARY_OP_LESS_EQUAL
2003 // MP_BINARY_OP_MORE_EQUAL
2004 // MP_BINARY_OP_NOT_EQUAL
Damien George21ca2d72014-10-19 19:00:51 +01002005 need_reg_single(emit, REG_RET, 0);
Damien George3112cde2014-09-29 18:45:42 +01002006 #if N_X64
2007 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
2008 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
2009 static byte ops[6] = {
2010 ASM_X64_CC_JL,
2011 ASM_X64_CC_JG,
2012 ASM_X64_CC_JE,
2013 ASM_X64_CC_JLE,
2014 ASM_X64_CC_JGE,
2015 ASM_X64_CC_JNE,
2016 };
2017 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
2018 #elif N_X86
2019 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
2020 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
2021 static byte ops[6] = {
2022 ASM_X86_CC_JL,
2023 ASM_X86_CC_JG,
2024 ASM_X86_CC_JE,
2025 ASM_X86_CC_JLE,
2026 ASM_X86_CC_JGE,
2027 ASM_X86_CC_JNE,
2028 };
2029 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
2030 #elif N_THUMB
2031 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
2032 static uint16_t ops[6] = {
2033 ASM_THUMB_OP_ITE_GE,
2034 ASM_THUMB_OP_ITE_GT,
2035 ASM_THUMB_OP_ITE_EQ,
2036 ASM_THUMB_OP_ITE_GT,
2037 ASM_THUMB_OP_ITE_GE,
2038 ASM_THUMB_OP_ITE_EQ,
2039 };
2040 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
2041 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
2042 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
2043 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
2044 #elif N_ARM
Fabian Vogte5268962014-10-04 00:53:46 +02002045 asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
2046 static uint ccs[6] = {
2047 ASM_ARM_CC_LT,
2048 ASM_ARM_CC_GT,
2049 ASM_ARM_CC_EQ,
2050 ASM_ARM_CC_LE,
2051 ASM_ARM_CC_GE,
2052 ASM_ARM_CC_NE,
2053 };
2054 asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
Damien George3112cde2014-09-29 18:45:42 +01002055 #else
2056 #error not implemented
2057 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00002058 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
2059 } else {
2060 // TODO other ops not yet implemented
2061 assert(0);
2062 }
Damien13ed3a62013-10-08 09:05:10 +01002063 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01002064 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00002065 bool invert = false;
2066 if (op == MP_BINARY_OP_NOT_IN) {
2067 invert = true;
2068 op = MP_BINARY_OP_IN;
2069 } else if (op == MP_BINARY_OP_IS_NOT) {
2070 invert = true;
2071 op = MP_BINARY_OP_IS;
2072 }
Damien George7fe21912014-08-16 22:31:57 +01002073 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00002074 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01002075 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00002076 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
2077 }
Damien13ed3a62013-10-08 09:05:10 +01002078 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2079 } else {
Damien George8f6aad22015-04-22 23:16:03 +01002080 adjust_stack(emit, -1);
Damien Georgec8b60f02015-04-20 13:29:31 +00002081 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2082 "can't do binary op between '%q' and '%q'",
2083 vtype_to_qstr(vtype_lhs), vtype_to_qstr(vtype_rhs));
Damien13ed3a62013-10-08 09:05:10 +01002084 }
2085}
2086
Damien George7ff996c2014-09-08 23:05:16 +01002087STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01002088 // for viper: call runtime, with types of args
2089 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00002090 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002091 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002092 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01002093 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01002094}
2095
Damien George7ff996c2014-09-08 23:05:16 +01002096STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002097 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002098 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002099 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002100 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
2101}
2102
Damien George7ff996c2014-09-08 23:05:16 +01002103STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01002104 // only used in list comprehension
2105 vtype_kind_t vtype_list, vtype_item;
2106 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2107 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
2108 assert(vtype_list == VTYPE_PYOBJ);
2109 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002110 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01002111 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002112}
2113
Damien George7ff996c2014-09-08 23:05:16 +01002114STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002115 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002116 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002117 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
2118}
2119
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002120STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002121 vtype_kind_t vtype_key, vtype_value, vtype_map;
2122 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
2123 assert(vtype_key == VTYPE_PYOBJ);
2124 assert(vtype_value == VTYPE_PYOBJ);
2125 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002126 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01002127 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
2128}
2129
Damien George7ff996c2014-09-08 23:05:16 +01002130STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01002131 // only used in list comprehension
2132 vtype_kind_t vtype_map, vtype_key, vtype_value;
2133 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
2134 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
2135 assert(vtype_map == VTYPE_PYOBJ);
2136 assert(vtype_key == VTYPE_PYOBJ);
2137 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002138 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01002139 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002140}
2141
Damien Georgee37dcaa2014-12-27 17:07:16 +00002142#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +01002143STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002144 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002145 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002146 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002147 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
2148}
2149
Damien George7ff996c2014-09-08 23:05:16 +01002150STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01002151 // only used in set comprehension
2152 vtype_kind_t vtype_set, vtype_item;
2153 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2154 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
2155 assert(vtype_set == VTYPE_PYOBJ);
2156 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002157 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01002158 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002159}
Damien Georgee37dcaa2014-12-27 17:07:16 +00002160#endif
Damiend2755ec2013-10-16 23:58:48 +01002161
Damien George83204f32014-12-27 17:20:41 +00002162#if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +01002163STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002164 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01002165 if (n_args == 2) {
2166 vtype_kind_t vtype_start, vtype_stop;
2167 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
2168 assert(vtype_start == VTYPE_PYOBJ);
2169 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002170 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 +01002171 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2172 } else {
2173 assert(n_args == 3);
2174 vtype_kind_t vtype_start, vtype_stop, vtype_step;
2175 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
2176 assert(vtype_start == VTYPE_PYOBJ);
2177 assert(vtype_stop == VTYPE_PYOBJ);
2178 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002179 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01002180 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2181 }
Damien13ed3a62013-10-08 09:05:10 +01002182}
Damien George83204f32014-12-27 17:20:41 +00002183#endif
Damien Georgecdd96df2014-04-06 12:58:40 +01002184
Damien George7ff996c2014-09-08 23:05:16 +01002185STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002186 DEBUG_printf("unpack_sequence %d\n", n_args);
2187 vtype_kind_t vtype_base;
2188 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2189 assert(vtype_base == VTYPE_PYOBJ);
2190 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01002191 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01002192}
Damien Georgecdd96df2014-04-06 12:58:40 +01002193
Damien George7ff996c2014-09-08 23:05:16 +01002194STATIC 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 +01002195 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
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);
Damien Georgeb601d952014-06-30 05:17:25 +01002199 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 +01002200 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 +01002201}
2202
Damien George7ff996c2014-09-08 23:05:16 +01002203STATIC 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 +01002204 // 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 +00002205 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01002206 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01002207 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 +01002208 } else {
2209 vtype_kind_t vtype_def_tuple, vtype_def_dict;
2210 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
2211 assert(vtype_def_tuple == VTYPE_PYOBJ);
2212 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002213 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 +01002214 }
Damien13ed3a62013-10-08 09:05:10 +01002215 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2216}
2217
Damien George7ff996c2014-09-08 23:05:16 +01002218STATIC 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 +00002219 emit_native_pre(emit);
2220 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
2221 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over);
2222 ASM_MOV_IMM_TO_REG(emit->as, n_closed_over, REG_ARG_2);
2223 } else {
2224 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over + 2);
2225 ASM_MOV_IMM_TO_REG(emit->as, 0x100 | n_closed_over, REG_ARG_2);
2226 }
2227 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)scope->raw_code, REG_ARG_1);
2228 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_MAKE_CLOSURE_FROM_RAW_CODE], MP_F_MAKE_CLOSURE_FROM_RAW_CODE);
2229 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002230}
2231
Damien George7ff996c2014-09-08 23:05:16 +01002232STATIC 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 +00002233 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
2234
Damien Georgee9dac3b2014-09-29 22:10:41 +01002235 // TODO: in viper mode, call special runtime routine with type info for args,
2236 // and wanted type info for return, to remove need for boxing/unboxing
2237
Damien Georgee9dac3b2014-09-29 22:10:41 +01002238 emit_native_pre(emit);
2239 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
2240 if (vtype_fun == VTYPE_BUILTIN_CAST) {
2241 // casting operator
2242 assert(n_positional == 1 && n_keyword == 0);
Damien George78772ad2015-04-06 22:48:21 +01002243 assert(!star_flags);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002244 DEBUG_printf(" cast to %d\n", vtype_fun);
Damien George32444b72015-01-24 23:14:12 +00002245 vtype_kind_t vtype_cast = peek_stack(emit, 1)->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01002246 switch (peek_vtype(emit, 0)) {
2247 case VTYPE_PYOBJ: {
2248 vtype_kind_t vtype;
2249 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2250 emit_pre_pop_discard(emit);
2251 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
2252 emit_post_push_reg(emit, vtype_cast, REG_RET);
2253 break;
2254 }
2255 case VTYPE_BOOL:
2256 case VTYPE_INT:
2257 case VTYPE_UINT:
2258 case VTYPE_PTR:
2259 case VTYPE_PTR8:
2260 case VTYPE_PTR16:
2261 case VTYPE_PTR_NONE:
2262 emit_fold_stack_top(emit, REG_ARG_1);
2263 emit_post_top_set_vtype(emit, vtype_cast);
2264 break;
2265 default:
2266 assert(!"TODO: convert obj to int");
2267 }
2268 } else {
Damien13ed3a62013-10-08 09:05:10 +01002269 assert(vtype_fun == VTYPE_PYOBJ);
Damien George78772ad2015-04-06 22:48:21 +01002270 if (star_flags) {
2271 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
2272 // load dummy entry for non-existent pos_seq
2273 emit_native_load_null(emit);
2274 emit_native_rot_two(emit);
2275 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
2276 // load dummy entry for non-existent kw_dict
2277 emit_native_load_null(emit);
2278 }
2279 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 3); // pointer to args
2280 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);
2281 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2282 } else {
2283 if (n_positional != 0 || n_keyword != 0) {
2284 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2285 }
2286 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2287 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2288 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2289 }
Damien Georgecd82e022014-02-02 13:11:48 +00002290 }
Damien13ed3a62013-10-08 09:05:10 +01002291}
2292
Damien George7ff996c2014-09-08 23:05:16 +01002293STATIC 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 +01002294 if (star_flags) {
2295 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
2296 // load dummy entry for non-existent pos_seq
2297 emit_native_load_null(emit);
2298 emit_native_rot_two(emit);
2299 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
2300 // load dummy entry for non-existent kw_dict
2301 emit_native_load_null(emit);
2302 }
2303 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 4); // pointer to args
2304 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);
2305 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2306 } else {
2307 emit_native_pre(emit);
2308 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
2309 emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, n_positional, REG_ARG_1, n_keyword, REG_ARG_2);
2310 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2311 }
Damien13ed3a62013-10-08 09:05:10 +01002312}
2313
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002314STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002315 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002316 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002317 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2318 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002319 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002320 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002321 } else {
2322 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002323 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002324 } else {
2325 vtype_kind_t vtype;
2326 emit_pre_pop_reg(emit, &vtype, REG_RET);
2327 if (vtype != emit->return_vtype) {
Damien Georgec8b60f02015-04-20 13:29:31 +00002328 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2329 "return expected '%q' but got '%q'",
2330 vtype_to_qstr(emit->return_vtype), vtype_to_qstr(vtype));
Damien Georgee9dac3b2014-09-29 22:10:41 +01002331 }
Damien George2ac4af62014-08-15 16:45:41 +01002332 }
Damien13ed3a62013-10-08 09:05:10 +01002333 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002334 vtype_kind_t vtype;
2335 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002336 assert(vtype == VTYPE_PYOBJ);
2337 }
2338 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002339 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2340 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002341}
2342
Damien George7ff996c2014-09-08 23:05:16 +01002343STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002344 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002345 vtype_kind_t vtype_exc;
2346 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2347 if (vtype_exc != VTYPE_PYOBJ) {
Damien Georgec8b60f02015-04-20 13:29:31 +00002348 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "must raise an object");
Damien George86de21b2014-08-16 22:06:11 +01002349 }
2350 // 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 +01002351 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002352}
Damien Georgeb601d952014-06-30 05:17:25 +01002353
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002354STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002355 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002356 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002357 assert(0);
2358}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002359STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002360 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002361 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002362 assert(0);
2363}
2364
Damien Georgeb601d952014-06-30 05:17:25 +01002365STATIC void emit_native_start_except_handler(emit_t *emit) {
2366 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2367 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2368 // the first 2 elements, so we can get the thrown value.
2369 adjust_stack(emit, 2);
2370 vtype_kind_t vtype_nlr;
2371 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002372 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002373 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
2374}
2375
2376STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002377 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002378}
2379
Damien13ed3a62013-10-08 09:05:10 +01002380const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002381 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002382 emit_native_start_pass,
2383 emit_native_end_pass,
2384 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002385 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002386 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002387
Damien George542bd6b2015-03-26 14:42:40 +00002388 {
2389 emit_native_load_fast,
2390 emit_native_load_deref,
2391 emit_native_load_name,
2392 emit_native_load_global,
2393 },
2394 {
2395 emit_native_store_fast,
2396 emit_native_store_deref,
2397 emit_native_store_name,
2398 emit_native_store_global,
2399 },
2400 {
2401 emit_native_delete_fast,
2402 emit_native_delete_deref,
2403 emit_native_delete_name,
2404 emit_native_delete_global,
2405 },
Damien13ed3a62013-10-08 09:05:10 +01002406
2407 emit_native_label_assign,
2408 emit_native_import_name,
2409 emit_native_import_from,
2410 emit_native_import_star,
2411 emit_native_load_const_tok,
2412 emit_native_load_const_small_int,
Damien13ed3a62013-10-08 09:05:10 +01002413 emit_native_load_const_str,
Damien Georgedab13852015-01-13 15:55:54 +00002414 emit_native_load_const_obj,
Damien George3558f622014-04-20 17:50:40 +01002415 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002416 emit_native_load_attr,
2417 emit_native_load_method,
2418 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002419 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002420 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002421 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002422 emit_native_delete_attr,
2423 emit_native_delete_subscr,
2424 emit_native_dup_top,
2425 emit_native_dup_top_two,
2426 emit_native_pop_top,
2427 emit_native_rot_two,
2428 emit_native_rot_three,
2429 emit_native_jump,
Damien George63f38322015-02-28 15:04:06 +00002430 emit_native_pop_jump_if,
2431 emit_native_jump_if_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002432 emit_native_break_loop,
2433 emit_native_continue_loop,
2434 emit_native_setup_with,
2435 emit_native_with_cleanup,
2436 emit_native_setup_except,
2437 emit_native_setup_finally,
2438 emit_native_end_finally,
2439 emit_native_get_iter,
2440 emit_native_for_iter,
2441 emit_native_for_iter_end,
2442 emit_native_pop_block,
2443 emit_native_pop_except,
2444 emit_native_unary_op,
2445 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002446 emit_native_build_tuple,
2447 emit_native_build_list,
2448 emit_native_list_append,
2449 emit_native_build_map,
2450 emit_native_store_map,
2451 emit_native_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002452 #if MICROPY_PY_BUILTINS_SET
Damien13ed3a62013-10-08 09:05:10 +01002453 emit_native_build_set,
2454 emit_native_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002455 #endif
Damien George83204f32014-12-27 17:20:41 +00002456 #if MICROPY_PY_BUILTINS_SLICE
Damien13ed3a62013-10-08 09:05:10 +01002457 emit_native_build_slice,
Damien George83204f32014-12-27 17:20:41 +00002458 #endif
Damien13ed3a62013-10-08 09:05:10 +01002459 emit_native_unpack_sequence,
2460 emit_native_unpack_ex,
2461 emit_native_make_function,
2462 emit_native_make_closure,
2463 emit_native_call_function,
2464 emit_native_call_method,
2465 emit_native_return_value,
2466 emit_native_raise_varargs,
2467 emit_native_yield_value,
2468 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002469
2470 emit_native_start_except_handler,
2471 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002472};
2473
Damien Georgec90f59e2014-09-06 23:06:36 +01002474#endif