blob: 685c6177e7ecae2243c1c520a6394850762bf8c1 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien13ed3a62013-10-08 09:05:10 +010027// Essentially normal Python has 1 type: Python objects
28// Viper has more than 1 type, and is just a more complicated (a superset of) Python.
29// If you declare everything in Viper as a Python object (ie omit type decls) then
30// it should in principle be exactly the same as Python native.
31// Having types means having more opcodes, like binary_op_nat_nat, binary_op_nat_obj etc.
32// In practice we won't have a VM but rather do this in asm which is actually very minimal.
33
34// Because it breaks strict Python equivalence it should be a completely separate
35// decorator. It breaks equivalence because overflow on integers wraps around.
36// It shouldn't break equivalence if you don't use the new types, but since the
37// type decls might be used in normal Python for other reasons, it's probably safest,
38// cleanest and clearest to make it a separate decorator.
39
40// Actually, it does break equivalence because integers default to native integers,
41// not Python objects.
42
43// for x in l[0:8]: can be compiled into a native loop if l has pointer type
44
xbeefe34222014-03-16 00:14:26 -070045#include <stdbool.h>
Damien13ed3a62013-10-08 09:05:10 +010046#include <stdint.h>
47#include <stdio.h>
48#include <string.h>
49#include <assert.h>
50
Damiend99b0522013-12-21 18:17:45 +000051#include "mpconfig.h"
Damien Georgeb601d952014-06-30 05:17:25 +010052#include "nlr.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030053#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000054#include "qstr.h"
Damien13ed3a62013-10-08 09:05:10 +010055#include "lexer.h"
Damien13ed3a62013-10-08 09:05:10 +010056#include "parse.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010057#include "obj.h"
58#include "emitglue.h"
Damien13ed3a62013-10-08 09:05:10 +010059#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000060#include "runtime0.h"
Damien13ed3a62013-10-08 09:05:10 +010061#include "emit.h"
Damiend99b0522013-12-21 18:17:45 +000062#include "runtime.h"
Damien13ed3a62013-10-08 09:05:10 +010063
Damien Georgecdd96df2014-04-06 12:58:40 +010064#if 0 // print debugging info
65#define DEBUG_PRINT (1)
66#define DEBUG_printf DEBUG_printf
67#else // don't print debugging info
68#define DEBUG_printf(...) (void)0
69#endif
70
Damien13ed3a62013-10-08 09:05:10 +010071// wrapper around everything in this file
Damien Georgec90f59e2014-09-06 23:06:36 +010072#if (MICROPY_EMIT_X64 && N_X64) \
73 || (MICROPY_EMIT_X86 && N_X86) \
74 || (MICROPY_EMIT_THUMB && N_THUMB) \
75 || (MICROPY_EMIT_ARM && N_ARM)
Damien13ed3a62013-10-08 09:05:10 +010076
Damien3ef4abb2013-10-12 16:53:13 +010077#if N_X64
Damien13ed3a62013-10-08 09:05:10 +010078
79// x64 specific stuff
80
81#include "asmx64.h"
82
Damien13ed3a62013-10-08 09:05:10 +010083#define EXPORT_FUN(name) emit_native_x64_##name
84
Damien George81057362014-09-07 01:06:19 +010085#define REG_RET REG_RAX
86#define REG_ARG_1 REG_RDI
87#define REG_ARG_2 REG_RSI
88#define REG_ARG_3 REG_RDX
89#define REG_ARG_4 REG_RCX
Damien Georgec90f59e2014-09-06 23:06:36 +010090
Damien George81057362014-09-07 01:06:19 +010091// caller-save
92#define REG_TEMP0 REG_RAX
93#define REG_TEMP1 REG_RDI
94#define REG_TEMP2 REG_RSI
95
96// callee-save
97#define REG_LOCAL_1 REG_RBX
98#define REG_LOCAL_2 REG_R12
99#define REG_LOCAL_3 REG_R13
100#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100101
102#define ASM_PASS_COMPUTE ASM_X64_PASS_COMPUTE
103#define ASM_PASS_EMIT ASM_X64_PASS_EMIT
104
105#define ASM_T asm_x64_t
106#define ASM_NEW asm_x64_new
107#define ASM_FREE asm_x64_free
108#define ASM_GET_CODE asm_x64_get_code
109#define ASM_GET_CODE_SIZE asm_x64_get_code_size
110#define ASM_START_PASS asm_x64_start_pass
111#define ASM_END_PASS asm_x64_end_pass
112#define ASM_ENTRY asm_x64_entry
113#define ASM_EXIT asm_x64_exit
114
115#define ASM_LABEL_ASSIGN asm_x64_label_assign
116#define ASM_JUMP asm_x64_jmp_label
117#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
118 do { \
119 asm_x64_test_r8_with_r8(as, reg, reg); \
120 asm_x64_jcc_label(as, ASM_X64_CC_JZ, label); \
121 } while (0)
122#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
123 do { \
124 asm_x64_test_r8_with_r8(as, reg, reg); \
125 asm_x64_jcc_label(as, ASM_X64_CC_JNZ, label); \
126 } while (0)
127#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
128 do { \
129 asm_x64_cmp_r64_with_r64(as, reg1, reg2); \
130 asm_x64_jcc_label(as, ASM_X64_CC_JE, label); \
131 } while (0)
132#define ASM_CALL_IND(as, ptr, idx) asm_x64_call_ind(as, ptr, REG_RAX)
133
134#define ASM_MOV_REG_TO_LOCAL asm_x64_mov_r64_to_local
135#define ASM_MOV_IMM_TO_REG asm_x64_mov_i64_to_r64_optimised
136#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x64_mov_i64_to_r64_aligned
137#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
138 do { \
139 asm_x64_mov_i64_to_r64_optimised(as, (imm), (reg_temp)); \
140 asm_x64_mov_r64_to_local(as, (reg_temp), (local_num)); \
141 } while (false)
142#define ASM_MOV_LOCAL_TO_REG asm_x64_mov_local_to_r64
143#define ASM_MOV_REG_TO_REG asm_x64_mov_r64_to_r64
144#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x64_mov_local_addr_to_r64
145
146#elif N_X86
147
148// x86 specific stuff
149
150#include "asmx86.h"
151
152STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
153 [MP_F_CONVERT_OBJ_TO_NATIVE] = 2,
154 [MP_F_CONVERT_NATIVE_TO_OBJ] = 2,
155 [MP_F_LOAD_CONST_INT] = 1,
156 [MP_F_LOAD_CONST_DEC] = 1,
157 [MP_F_LOAD_CONST_STR] = 1,
158 [MP_F_LOAD_CONST_BYTES] = 1,
159 [MP_F_LOAD_NAME] = 1,
160 [MP_F_LOAD_GLOBAL] = 1,
161 [MP_F_LOAD_BUILD_CLASS] = 0,
162 [MP_F_LOAD_ATTR] = 2,
163 [MP_F_LOAD_METHOD] = 3,
164 [MP_F_STORE_NAME] = 2,
165 [MP_F_STORE_GLOBAL] = 2,
166 [MP_F_STORE_ATTR] = 3,
167 [MP_F_OBJ_SUBSCR] = 3,
168 [MP_F_OBJ_IS_TRUE] = 1,
169 [MP_F_UNARY_OP] = 2,
170 [MP_F_BINARY_OP] = 3,
171 [MP_F_BUILD_TUPLE] = 2,
172 [MP_F_BUILD_LIST] = 2,
173 [MP_F_LIST_APPEND] = 2,
174 [MP_F_BUILD_MAP] = 1,
175 [MP_F_STORE_MAP] = 3,
176#if MICROPY_PY_BUILTINS_SET
177 [MP_F_BUILD_SET] = 2,
178 [MP_F_STORE_SET] = 2,
179#endif
180 [MP_F_MAKE_FUNCTION_FROM_RAW_CODE] = 3,
181 [MP_F_NATIVE_CALL_FUNCTION_N_KW] = 3,
182 [MP_F_CALL_METHOD_N_KW] = 3,
183 [MP_F_GETITER] = 1,
184 [MP_F_ITERNEXT] = 1,
185 [MP_F_NLR_PUSH] = 1,
186 [MP_F_NLR_POP] = 0,
187 [MP_F_NATIVE_RAISE] = 1,
188 [MP_F_IMPORT_NAME] = 3,
189 [MP_F_IMPORT_FROM] = 2,
190 [MP_F_IMPORT_ALL] = 1,
191#if MICROPY_PY_BUILTINS_SLICE
192 [MP_F_NEW_SLICE] = 3,
193#endif
194 [MP_F_UNPACK_SEQUENCE] = 3,
195 [MP_F_UNPACK_EX] = 3,
196 [MP_F_DELETE_NAME] = 1,
197 [MP_F_DELETE_GLOBAL] = 1,
198};
199
200#define EXPORT_FUN(name) emit_native_x86_##name
201
Damien George81057362014-09-07 01:06:19 +0100202#define REG_RET REG_EAX
203#define REG_ARG_1 REG_EAX
204#define REG_ARG_2 REG_ECX
205#define REG_ARG_3 REG_EDX
206
Damien George25d90412014-09-06 23:24:32 +0000207// caller-save, so can be used as temporaries
Damien George81057362014-09-07 01:06:19 +0100208#define REG_TEMP0 REG_EAX
209#define REG_TEMP1 REG_ECX
210#define REG_TEMP2 REG_EDX
Damien Georgec90f59e2014-09-06 23:06:36 +0100211
Damien George25d90412014-09-06 23:24:32 +0000212// callee-save, so can be used as locals
Damien George81057362014-09-07 01:06:19 +0100213#define REG_LOCAL_1 REG_EBX
214#define REG_LOCAL_2 REG_ESI
215#define REG_LOCAL_3 REG_EDI
Damien George25d90412014-09-06 23:24:32 +0000216#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100217
218#define ASM_PASS_COMPUTE ASM_X86_PASS_COMPUTE
219#define ASM_PASS_EMIT ASM_X86_PASS_EMIT
220
221#define ASM_T asm_x86_t
222#define ASM_NEW asm_x86_new
223#define ASM_FREE asm_x86_free
224#define ASM_GET_CODE asm_x86_get_code
225#define ASM_GET_CODE_SIZE asm_x86_get_code_size
226#define ASM_START_PASS asm_x86_start_pass
227#define ASM_END_PASS asm_x86_end_pass
228#define ASM_ENTRY asm_x86_entry
229#define ASM_EXIT asm_x86_exit
230
231#define ASM_LABEL_ASSIGN asm_x86_label_assign
232#define ASM_JUMP asm_x86_jmp_label
233#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
234 do { \
235 asm_x86_test_r8_with_r8(as, reg, reg); \
236 asm_x86_jcc_label(as, ASM_X86_CC_JZ, label); \
237 } while (0)
238#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
239 do { \
240 asm_x86_test_r8_with_r8(as, reg, reg); \
241 asm_x86_jcc_label(as, ASM_X86_CC_JNZ, label); \
242 } while (0)
243#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
244 do { \
245 asm_x86_cmp_r32_with_r32(as, reg1, reg2); \
246 asm_x86_jcc_label(as, ASM_X86_CC_JE, label); \
247 } while (0)
248#define ASM_CALL_IND(as, ptr, idx) asm_x86_call_ind(as, ptr, mp_f_n_args[idx], REG_EAX)
249
250#define ASM_MOV_REG_TO_LOCAL asm_x86_mov_r32_to_local
251#define ASM_MOV_IMM_TO_REG asm_x86_mov_i32_to_r32
252#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x86_mov_i32_to_r32_aligned
253#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
254 do { \
255 asm_x86_mov_i32_to_r32(as, (imm), (reg_temp)); \
256 asm_x86_mov_r32_to_local(as, (reg_temp), (local_num)); \
257 } while (false)
258#define ASM_MOV_LOCAL_TO_REG asm_x86_mov_local_to_r32
259#define ASM_MOV_REG_TO_REG asm_x86_mov_r32_to_r32
260#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x86_mov_local_addr_to_r32
Damien13ed3a62013-10-08 09:05:10 +0100261
Damien3ef4abb2013-10-12 16:53:13 +0100262#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100263
264// thumb specific stuff
265
266#include "asmthumb.h"
267
Damien13ed3a62013-10-08 09:05:10 +0100268#define EXPORT_FUN(name) emit_native_thumb_##name
269
Damien George81057362014-09-07 01:06:19 +0100270#define REG_RET REG_R0
271#define REG_ARG_1 REG_R0
272#define REG_ARG_2 REG_R1
273#define REG_ARG_3 REG_R2
274#define REG_ARG_4 REG_R3
275
Damien13ed3a62013-10-08 09:05:10 +0100276#define REG_TEMP0 (REG_R0)
277#define REG_TEMP1 (REG_R1)
278#define REG_TEMP2 (REG_R2)
Damien Georgec90f59e2014-09-06 23:06:36 +0100279
280#define REG_LOCAL_1 (REG_R4)
281#define REG_LOCAL_2 (REG_R5)
282#define REG_LOCAL_3 (REG_R6)
283#define REG_LOCAL_NUM (3)
284
285#define ASM_PASS_COMPUTE ASM_THUMB_PASS_COMPUTE
286#define ASM_PASS_EMIT ASM_THUMB_PASS_EMIT
287
288#define ASM_T asm_thumb_t
289#define ASM_NEW asm_thumb_new
290#define ASM_FREE asm_thumb_free
291#define ASM_GET_CODE asm_thumb_get_code
292#define ASM_GET_CODE_SIZE asm_thumb_get_code_size
293#define ASM_START_PASS asm_thumb_start_pass
294#define ASM_END_PASS asm_thumb_end_pass
295#define ASM_ENTRY asm_thumb_entry
296#define ASM_EXIT asm_thumb_exit
297
298#define ASM_LABEL_ASSIGN asm_thumb_label_assign
299#define ASM_JUMP asm_thumb_b_label
300#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
301 do { \
302 asm_thumb_cmp_rlo_i8(as, reg, 0); \
303 asm_thumb_bcc_label(as, THUMB_CC_EQ, label); \
304 } while (0)
305#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
306 do { \
307 asm_thumb_cmp_rlo_i8(as, reg, 0); \
308 asm_thumb_bcc_label(as, THUMB_CC_NE, label); \
309 } while (0)
310#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
311 do { \
312 asm_thumb_cmp_rlo_rlo(as, reg1, reg2); \
313 asm_thumb_bcc_label(as, THUMB_CC_EQ, label); \
314 } while (0)
315#define ASM_CALL_IND(as, ptr, idx) asm_thumb_bl_ind(as, ptr, idx, REG_R3)
316
317#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_thumb_mov_local_reg(as, (local_num), (reg))
318#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_optimised(as, (reg), (imm))
319#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_aligned(as, (reg), (imm))
320#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
321 do { \
322 asm_thumb_mov_reg_i32_optimised(as, (reg_temp), (imm)); \
323 asm_thumb_mov_local_reg(as, (local_num), (reg_temp)); \
324 } while (false)
325#define ASM_MOV_LOCAL_TO_REG(as, local_num, reg) asm_thumb_mov_reg_local(as, (reg), (local_num))
326#define ASM_MOV_REG_TO_REG(as, reg_src, reg_dest) asm_thumb_mov_reg_reg(as, (reg_dest), (reg_src))
327#define ASM_MOV_LOCAL_ADDR_TO_REG(as, local_num, reg) asm_thumb_mov_reg_local_addr(as, (reg), (local_num))
Damien13ed3a62013-10-08 09:05:10 +0100328
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200329#elif N_ARM
330
331// ARM specific stuff
332
333#include "asmarm.h"
334
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200335#define EXPORT_FUN(name) emit_native_arm_##name
336
Damien George81057362014-09-07 01:06:19 +0100337#define REG_RET REG_R0
338#define REG_ARG_1 REG_R0
339#define REG_ARG_2 REG_R1
340#define REG_ARG_3 REG_R2
341#define REG_ARG_4 REG_R3
342
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200343#define REG_TEMP0 (REG_R0)
344#define REG_TEMP1 (REG_R1)
345#define REG_TEMP2 (REG_R2)
Damien Georgec90f59e2014-09-06 23:06:36 +0100346
347#define REG_LOCAL_1 (REG_R4)
348#define REG_LOCAL_2 (REG_R5)
349#define REG_LOCAL_3 (REG_R6)
350#define REG_LOCAL_NUM (3)
351
352#define ASM_PASS_COMPUTE ASM_ARM_PASS_COMPUTE
353#define ASM_PASS_EMIT ASM_ARM_PASS_EMIT
354
355#define ASM_T asm_arm_t
356#define ASM_NEW asm_arm_new
357#define ASM_FREE asm_arm_free
358#define ASM_GET_CODE asm_arm_get_code
359#define ASM_GET_CODE_SIZE asm_arm_get_code_size
360#define ASM_START_PASS asm_arm_start_pass
361#define ASM_END_PASS asm_arm_end_pass
362#define ASM_ENTRY asm_arm_entry
363#define ASM_EXIT asm_arm_exit
364
365#define ASM_LABEL_ASSIGN asm_arm_label_assign
366#define ASM_JUMP asm_arm_b_label
367#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
368 do { \
369 asm_arm_cmp_reg_i8(as, reg, 0); \
370 asm_arm_bcc_label(as, ARM_CC_EQ, label); \
371 } while (0)
372#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
373 do { \
374 asm_arm_cmp_reg_i8(as, reg, 0); \
375 asm_arm_bcc_label(as, ARM_CC_NE, label); \
376 } while (0)
377#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
378 do { \
379 asm_arm_cmp_reg_reg(as, reg1, reg2); \
380 asm_arm_bcc_label(as, ARM_CC_EQ, label); \
381 } while (0)
382#define ASM_CALL_IND(as, ptr, idx) asm_arm_bl_ind(as, ptr, idx, REG_R3)
383
384#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_arm_mov_local_reg(as, (local_num), (reg))
385#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
386#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
387#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
388 do { \
389 asm_arm_mov_reg_i32(as, (reg_temp), (imm)); \
390 asm_arm_mov_local_reg(as, (local_num), (reg_temp)); \
391 } while (false)
392#define ASM_MOV_LOCAL_TO_REG(as, local_num, reg) asm_arm_mov_reg_local(as, (reg), (local_num))
393#define ASM_MOV_REG_TO_REG(as, reg_src, reg_dest) asm_arm_mov_reg_reg(as, (reg_dest), (reg_src))
394#define ASM_MOV_LOCAL_ADDR_TO_REG(as, local_num, reg) asm_arm_mov_reg_local_addr(as, (reg), (local_num))
395
396#else
397
398#error unknown native emitter
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200399
Damien13ed3a62013-10-08 09:05:10 +0100400#endif
401
402typedef enum {
Damienff8ed772013-10-08 22:18:32 +0100403 STACK_VALUE,
404 STACK_REG,
405 STACK_IMM,
406} stack_info_kind_t;
Damien13ed3a62013-10-08 09:05:10 +0100407
408typedef enum {
Damien George2ac4af62014-08-15 16:45:41 +0100409 VTYPE_PYOBJ = MP_NATIVE_TYPE_OBJ,
410 VTYPE_BOOL = MP_NATIVE_TYPE_BOOL,
411 VTYPE_INT = MP_NATIVE_TYPE_INT,
412 VTYPE_UINT = MP_NATIVE_TYPE_UINT,
Damien13ed3a62013-10-08 09:05:10 +0100413 VTYPE_UNBOUND,
Damien13ed3a62013-10-08 09:05:10 +0100414 VTYPE_PTR,
415 VTYPE_PTR_NONE,
416 VTYPE_BUILTIN_V_INT,
417} vtype_kind_t;
418
Damienff8ed772013-10-08 22:18:32 +0100419typedef struct _stack_info_t {
420 vtype_kind_t vtype;
421 stack_info_kind_t kind;
422 union {
423 int u_reg;
Damien George40f3c022014-07-03 13:25:24 +0100424 mp_int_t u_imm;
Damienff8ed772013-10-08 22:18:32 +0100425 };
426} stack_info_t;
427
Damien13ed3a62013-10-08 09:05:10 +0100428struct _emit_t {
429 int pass;
430
431 bool do_viper_types;
Damienff8ed772013-10-08 22:18:32 +0100432
Damien George2ac4af62014-08-15 16:45:41 +0100433 vtype_kind_t return_vtype;
434
Damien George7ff996c2014-09-08 23:05:16 +0100435 mp_uint_t local_vtype_alloc;
Damien13ed3a62013-10-08 09:05:10 +0100436 vtype_kind_t *local_vtype;
Damienff8ed772013-10-08 22:18:32 +0100437
Damien George7ff996c2014-09-08 23:05:16 +0100438 mp_uint_t stack_info_alloc;
Damienff8ed772013-10-08 22:18:32 +0100439 stack_info_t *stack_info;
440
Damien13ed3a62013-10-08 09:05:10 +0100441 int stack_start;
442 int stack_size;
443
444 bool last_emit_was_return_value;
445
Damien13ed3a62013-10-08 09:05:10 +0100446 scope_t *scope;
447
Damien Georgec90f59e2014-09-06 23:06:36 +0100448 ASM_T *as;
Damien13ed3a62013-10-08 09:05:10 +0100449};
450
Damien George7ff996c2014-09-08 23:05:16 +0100451emit_t *EXPORT_FUN(new)(mp_uint_t max_num_labels) {
Damien George36db6bc2014-05-07 17:24:22 +0100452 emit_t *emit = m_new0(emit_t, 1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100453 emit->as = ASM_NEW(max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100454 return emit;
455}
456
Damien George41d02b62014-01-24 22:42:28 +0000457void EXPORT_FUN(free)(emit_t *emit) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100458 ASM_FREE(emit->as, false);
Damien George36db6bc2014-05-07 17:24:22 +0100459 m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
460 m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200461 m_del_obj(emit_t, emit);
462}
463
Damien George2ac4af62014-08-15 16:45:41 +0100464STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
465 switch (op) {
466 case MP_EMIT_NATIVE_TYPE_ENABLE:
467 emit->do_viper_types = arg1;
468 break;
469
470 default: {
471 vtype_kind_t type;
472 switch (arg2) {
473 case MP_QSTR_object: type = VTYPE_PYOBJ; break;
474 case MP_QSTR_bool: type = VTYPE_BOOL; break;
475 case MP_QSTR_int: type = VTYPE_INT; break;
476 case MP_QSTR_uint: type = VTYPE_UINT; break;
477 default: printf("ViperTypeError: unknown type %s\n", qstr_str(arg2)); return;
478 }
479 if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
480 emit->return_vtype = type;
481 } else {
482 assert(arg1 < emit->local_vtype_alloc);
483 emit->local_vtype[arg1] = type;
484 }
485 break;
486 }
487 }
Damien13ed3a62013-10-08 09:05:10 +0100488}
489
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200490STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien13ed3a62013-10-08 09:05:10 +0100491 emit->pass = pass;
492 emit->stack_start = 0;
493 emit->stack_size = 0;
494 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100495 emit->scope = scope;
496
Damien George36db6bc2014-05-07 17:24:22 +0100497 // allocate memory for keeping track of the types of locals
498 if (emit->local_vtype_alloc < scope->num_locals) {
499 emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
500 emit->local_vtype_alloc = scope->num_locals;
Damienff8ed772013-10-08 22:18:32 +0100501 }
Damien George36db6bc2014-05-07 17:24:22 +0100502
503 // allocate memory for keeping track of the objects on the stack
504 // XXX don't know stack size on entry, and it should be maximum over all scopes
Damienff8ed772013-10-08 22:18:32 +0100505 if (emit->stack_info == NULL) {
Damien George36db6bc2014-05-07 17:24:22 +0100506 emit->stack_info_alloc = scope->stack_size + 50;
Damienff8ed772013-10-08 22:18:32 +0100507 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100508 }
509
Damien George2ac4af62014-08-15 16:45:41 +0100510 // set default type for return and arguments
511 emit->return_vtype = VTYPE_PYOBJ;
Damien Georgea5190a72014-08-15 22:39:08 +0100512 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100513 emit->local_vtype[i] = VTYPE_PYOBJ;
514 }
Damien Georgea5190a72014-08-15 22:39:08 +0100515
516 // local variables begin unbound, and have unknown type
517 for (mp_uint_t i = emit->scope->num_pos_args; i < emit->local_vtype_alloc; i++) {
518 emit->local_vtype[i] = VTYPE_UNBOUND;
519 }
520
521 // values on stack begin unbound
522 for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100523 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgea5190a72014-08-15 22:39:08 +0100524 emit->stack_info[i].vtype = VTYPE_UNBOUND;
Damien13ed3a62013-10-08 09:05:10 +0100525 }
526
Damien Georgec90f59e2014-09-06 23:06:36 +0100527 ASM_START_PASS(emit->as, pass == MP_PASS_EMIT ? ASM_PASS_EMIT : ASM_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100528
529 // entry to function
530 int num_locals = 0;
Damien George36db6bc2014-05-07 17:24:22 +0100531 if (pass > MP_PASS_SCOPE) {
Damien13ed3a62013-10-08 09:05:10 +0100532 num_locals = scope->num_locals - REG_LOCAL_NUM;
533 if (num_locals < 0) {
534 num_locals = 0;
535 }
536 emit->stack_start = num_locals;
537 num_locals += scope->stack_size;
538 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100539 ASM_ENTRY(emit->as, num_locals);
Damien13ed3a62013-10-08 09:05:10 +0100540
541 // initialise locals from parameters
Damien3ef4abb2013-10-12 16:53:13 +0100542#if N_X64
Damien George2827d622014-04-27 15:50:52 +0100543 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100544 if (i == 0) {
545 asm_x64_mov_r64_to_r64(emit->as, REG_ARG_1, REG_LOCAL_1);
546 } else if (i == 1) {
Damien George81057362014-09-07 01:06:19 +0100547 asm_x64_mov_r64_to_r64(emit->as, REG_ARG_2, REG_LOCAL_2);
Damien13ed3a62013-10-08 09:05:10 +0100548 } else if (i == 2) {
Damien George81057362014-09-07 01:06:19 +0100549 asm_x64_mov_r64_to_r64(emit->as, REG_ARG_3, REG_LOCAL_3);
550 } else if (i == 3) {
551 asm_x64_mov_r64_to_local(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +0100552 } else {
553 // TODO not implemented
554 assert(0);
555 }
556 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100557#elif N_X86
558 for (int i = 0; i < scope->num_pos_args; i++) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100559 if (i == 0) {
Damien George03281b32014-09-06 22:38:50 +0000560 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100561 } else if (i == 1) {
Damien George03281b32014-09-06 22:38:50 +0000562 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +0000563 } else if (i == 2) {
564 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +0100565 } else {
Damien George03281b32014-09-06 22:38:50 +0000566 asm_x86_mov_arg_to_r32(emit->as, i, REG_TEMP0);
567 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, i - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +0100568 }
569 }
Damien3ef4abb2013-10-12 16:53:13 +0100570#elif N_THUMB
Damien George2827d622014-04-27 15:50:52 +0100571 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100572 if (i == 0) {
573 asm_thumb_mov_reg_reg(emit->as, REG_LOCAL_1, REG_ARG_1);
574 } else if (i == 1) {
575 asm_thumb_mov_reg_reg(emit->as, REG_LOCAL_2, REG_ARG_2);
576 } else if (i == 2) {
577 asm_thumb_mov_reg_reg(emit->as, REG_LOCAL_3, REG_ARG_3);
578 } else if (i == 3) {
579 asm_thumb_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
580 } else {
581 // TODO not implemented
582 assert(0);
583 }
584 }
585
Damien George40f3c022014-07-03 13:25:24 +0100586 asm_thumb_mov_reg_i32(emit->as, REG_R7, (mp_uint_t)mp_fun_table);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200587#elif N_ARM
588 for (int i = 0; i < scope->num_pos_args; i++) {
589 if (i == 0) {
590 asm_arm_mov_reg_reg(emit->as, REG_LOCAL_1, REG_ARG_1);
591 } else if (i == 1) {
592 asm_arm_mov_reg_reg(emit->as, REG_LOCAL_2, REG_ARG_2);
593 } else if (i == 2) {
594 asm_arm_mov_reg_reg(emit->as, REG_LOCAL_3, REG_ARG_3);
595 } else if (i == 3) {
596 asm_arm_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
597 } else {
598 // TODO not implemented
599 assert(0);
600 }
601 }
602
603 asm_arm_mov_reg_i32(emit->as, REG_R7, (mp_uint_t)mp_fun_table);
Damien Georgec90f59e2014-09-06 23:06:36 +0100604#else
605 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +0100606#endif
607}
608
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200609STATIC void emit_native_end_pass(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100610 if (!emit->last_emit_was_return_value) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100611 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100612 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100613 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100614
615 // check stack is back to zero size
616 if (emit->stack_size != 0) {
617 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
618 }
619
Damien George36db6bc2014-05-07 17:24:22 +0100620 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100621 void *f = ASM_GET_CODE(emit->as);
622 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100623
624 // compute type signature
625 // TODO check that viper types here convert correctly to valid types for emit glue
626 mp_uint_t type_sig = emit->return_vtype & 3;
627 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
628 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
629 }
630
631 mp_emit_glue_assign_native(emit->scope->raw_code, emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY, f, f_len, emit->scope->num_pos_args, type_sig);
Damien13ed3a62013-10-08 09:05:10 +0100632 }
633}
634
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200635STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100636 return emit->last_emit_was_return_value;
637}
638
Damien George7ff996c2014-09-08 23:05:16 +0100639STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien Georged66ae182014-04-10 17:28:54 +0000640 emit->stack_size += delta;
Damien13ed3a62013-10-08 09:05:10 +0100641}
642
Damien George7ff996c2014-09-08 23:05:16 +0100643STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien George08335002014-01-18 23:24:36 +0000644}
645
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200646STATIC void adjust_stack(emit_t *emit, int stack_size_delta) {
Damien Georgecdd96df2014-04-06 12:58:40 +0100647 DEBUG_printf("adjust stack: stack:%d + delta:%d\n", emit->stack_size, stack_size_delta);
Damien George78035b92014-04-09 12:27:39 +0100648 assert((int)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100649 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100650 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100651 emit->scope->stack_size = emit->stack_size;
652 }
653}
654
Damienff8ed772013-10-08 22:18:32 +0100655/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200656STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100657 adjust_stack(emit, stack_size_delta);
658 emit->last_emit_was_return_value = false;
659}
Damienff8ed772013-10-08 22:18:32 +0100660*/
Damien13ed3a62013-10-08 09:05:10 +0100661
Damienff8ed772013-10-08 22:18:32 +0100662// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000663STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100664 emit->last_emit_was_return_value = false;
665 // settle the stack
666 /*
667 if (regs_needed != 0) {
668 for (int i = 0; i < emit->stack_size; i++) {
669 switch (emit->stack_info[i].kind) {
670 case STACK_VALUE:
671 break;
672
673 case STACK_REG:
674 // TODO only push reg if in regs_needed
675 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100676 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100677 break;
678
679 case STACK_IMM:
680 // don't think we ever need to push imms for settling
681 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
682 break;
683 }
684 }
685 }
686 */
Damien13ed3a62013-10-08 09:05:10 +0100687}
688
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200689STATIC vtype_kind_t peek_vtype(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100690 return emit->stack_info[emit->stack_size - 1].vtype;
691}
Damien13ed3a62013-10-08 09:05:10 +0100692
Damiend2755ec2013-10-16 23:58:48 +0100693// pos=1 is TOS, pos=2 is next, etc
694// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200695STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100696 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100697 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100698 if (i != skip_stack_pos) {
699 stack_info_t *si = &emit->stack_info[i];
700 if (si->kind == STACK_REG && si->u_reg == reg_needed) {
701 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100702 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100703 }
Damienff8ed772013-10-08 22:18:32 +0100704 }
705 }
706}
Damien13ed3a62013-10-08 09:05:10 +0100707
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200708STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100709 for (int i = 0; i < emit->stack_size; i++) {
710 stack_info_t *si = &emit->stack_info[i];
711 if (si->kind == STACK_REG) {
712 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100713 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100714 }
Damien13ed3a62013-10-08 09:05:10 +0100715 }
716}
717
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200718STATIC void need_stack_settled(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +0100719 for (int i = 0; i < emit->stack_size; i++) {
720 stack_info_t *si = &emit->stack_info[i];
721 if (si->kind == STACK_REG) {
722 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100723 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100724 }
725 }
726 for (int i = 0; i < emit->stack_size; i++) {
727 stack_info_t *si = &emit->stack_info[i];
728 if (si->kind == STACK_IMM) {
Damien George02d95d72014-08-29 20:05:32 +0100729 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100730 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +0100731 }
732 }
733}
734
735// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200736STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100737 need_reg_single(emit, reg_dest, pos);
738 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +0100739 *vtype = si->vtype;
740 switch (si->kind) {
741 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +0100742 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100743 break;
744
Damienff8ed772013-10-08 22:18:32 +0100745 case STACK_REG:
746 if (si->u_reg != reg_dest) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100747 ASM_MOV_REG_TO_REG(emit->as, si->u_reg, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100748 }
749 break;
750
Damienff8ed772013-10-08 22:18:32 +0100751 case STACK_IMM:
Damien Georgec90f59e2014-09-06 23:06:36 +0100752 ASM_MOV_IMM_TO_REG(emit->as, si->u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100753 break;
754 }
Damien13ed3a62013-10-08 09:05:10 +0100755}
756
Damien Georgee6ce10a2014-09-06 18:38:20 +0100757STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +0100758 emit->last_emit_was_return_value = false;
759 adjust_stack(emit, -1);
760}
761
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200762STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100763 emit->last_emit_was_return_value = false;
764 emit_access_stack(emit, 1, vtype, reg_dest);
765 adjust_stack(emit, -1);
766}
767
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200768STATIC 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 +0100769 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100770 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +0100771}
772
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200773STATIC 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 +0100774 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100775 emit_pre_pop_reg(emit, vtypeb, regb);
776 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100777}
778
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200779STATIC void emit_post(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100780}
781
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200782STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +0100783 stack_info_t *si = &emit->stack_info[emit->stack_size];
784 si->vtype = vtype;
785 si->kind = STACK_REG;
786 si->u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +0100787 adjust_stack(emit, 1);
788}
789
Damien George40f3c022014-07-03 13:25:24 +0100790STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +0100791 stack_info_t *si = &emit->stack_info[emit->stack_size];
792 si->vtype = vtype;
793 si->kind = STACK_IMM;
794 si->u_imm = imm;
795 adjust_stack(emit, 1);
796}
797
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200798STATIC 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 +0100799 emit_post_push_reg(emit, vtypea, rega);
800 emit_post_push_reg(emit, vtypeb, regb);
801}
802
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200803STATIC 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 +0100804 emit_post_push_reg(emit, vtypea, rega);
805 emit_post_push_reg(emit, vtypeb, regb);
806 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100807}
808
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200809STATIC 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 +0100810 emit_post_push_reg(emit, vtypea, rega);
811 emit_post_push_reg(emit, vtypeb, regb);
812 emit_post_push_reg(emit, vtypec, regc);
813 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +0100814}
815
Damien George7fe21912014-08-16 22:31:57 +0100816STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +0100817 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100818 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100819}
820
Damien George7fe21912014-08-16 22:31:57 +0100821STATIC 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 +0100822 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100823 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
824 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100825}
826
Damien George40f3c022014-07-03 13:25:24 +0100827// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100828STATIC 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 +0100829 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100830 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
831 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +0100832}
833
Damien George7fe21912014-08-16 22:31:57 +0100834STATIC 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 +0000835 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100836 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
837 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
838 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +0000839}
840
Damien George40f3c022014-07-03 13:25:24 +0100841// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100842STATIC 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 +0100843 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100844 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
845 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
846 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
847 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +0100848}
849
Damien George86de21b2014-08-16 22:06:11 +0100850// vtype of all n_pop objects is VTYPE_PYOBJ
851// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
852// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
853// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
854STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
855 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +0100856
Damien George86de21b2014-08-16 22:06:11 +0100857 // First, store any immediate values to their respective place on the stack.
858 for (mp_uint_t i = 0; i < n_pop; i++) {
859 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
860 // must push any imm's to stack
861 // must convert them to VTYPE_PYOBJ for viper code
862 if (si->kind == STACK_IMM) {
863 si->kind = STACK_VALUE;
864 switch (si->vtype) {
865 case VTYPE_PYOBJ:
Damien Georgec90f59e2014-09-06 23:06:36 +0100866 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->u_imm, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +0100867 break;
868 case VTYPE_BOOL:
869 if (si->u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100870 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 +0100871 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +0100872 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 +0100873 }
874 si->vtype = VTYPE_PYOBJ;
875 break;
876 case VTYPE_INT:
877 case VTYPE_UINT:
Damien Georgec90f59e2014-09-06 23:06:36 +0100878 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, (si->u_imm << 1) | 1, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +0100879 si->vtype = VTYPE_PYOBJ;
880 break;
881 default:
882 // not handled
883 assert(0);
884 }
885 }
886
887 // verify that this value is on the stack
888 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +0100889 }
Damien George86de21b2014-08-16 22:06:11 +0100890
891 // Second, convert any non-VTYPE_PYOBJ to that type.
892 for (mp_uint_t i = 0; i < n_pop; i++) {
893 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
894 if (si->vtype != VTYPE_PYOBJ) {
895 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +0100896 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +0100897 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 +0100898 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +0100899 si->vtype = VTYPE_PYOBJ;
900 }
901 }
902
903 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
904 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +0100905 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +0100906}
907
908// vtype of all n_push objects is VTYPE_PYOBJ
909STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
910 need_reg_all(emit);
911 for (mp_uint_t i = 0; i < n_push; i++) {
912 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
913 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
914 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100915 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +0100916 adjust_stack(emit, n_push);
917}
918
Damien George7ff996c2014-09-08 23:05:16 +0100919STATIC void emit_native_load_id(emit_t *emit, qstr qst) {
920 emit_common_load_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +0100921}
922
Damien George7ff996c2014-09-08 23:05:16 +0100923STATIC void emit_native_store_id(emit_t *emit, qstr qst) {
924 emit_common_store_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +0100925}
926
Damien George7ff996c2014-09-08 23:05:16 +0100927STATIC void emit_native_delete_id(emit_t *emit, qstr qst) {
928 emit_common_delete_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +0100929}
930
Damien George7ff996c2014-09-08 23:05:16 +0100931STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000932 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +0100933 // need to commit stack because we can jump here from elsewhere
934 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100935 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +0000936 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +0100937}
938
Damien Georgecdd96df2014-04-06 12:58:40 +0100939STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
940 DEBUG_printf("import_name %s\n", qstr_str(qst));
941 vtype_kind_t vtype_fromlist;
942 vtype_kind_t vtype_level;
943 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
944 assert(vtype_fromlist == VTYPE_PYOBJ);
945 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +0100946 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +0100947 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +0100948}
949
Damien Georgecdd96df2014-04-06 12:58:40 +0100950STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
951 DEBUG_printf("import_from %s\n", qstr_str(qst));
952 emit_native_pre(emit);
953 vtype_kind_t vtype_module;
954 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
955 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +0100956 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +0100957 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +0100958}
959
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200960STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +0100961 DEBUG_printf("import_star\n");
962 vtype_kind_t vtype_module;
963 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
964 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +0100965 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +0100966 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +0100967}
968
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200969STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georgecdd96df2014-04-06 12:58:40 +0100970 DEBUG_printf("load_const_tok %d\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +0000971 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +0100972 int vtype;
Damien George40f3c022014-07-03 13:25:24 +0100973 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +0100974 if (emit->do_viper_types) {
975 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +0000976 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
977 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
978 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien13ed3a62013-10-08 09:05:10 +0100979 default: assert(0); vtype = 0; val = 0; // shouldn't happen
980 }
981 } else {
982 vtype = VTYPE_PYOBJ;
983 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +0100984 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
985 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
986 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien13ed3a62013-10-08 09:05:10 +0100987 default: assert(0); vtype = 0; val = 0; // shouldn't happen
988 }
989 }
990 emit_post_push_imm(emit, vtype, val);
991}
992
Damien George40f3c022014-07-03 13:25:24 +0100993STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georgecdd96df2014-04-06 12:58:40 +0100994 DEBUG_printf("load_const_small_int %d\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +0000995 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +0100996 if (emit->do_viper_types) {
997 emit_post_push_imm(emit, VTYPE_INT, arg);
998 } else {
999 emit_post_push_imm(emit, VTYPE_PYOBJ, (arg << 1) | 1);
1000 }
1001}
1002
Damien Georgecdd96df2014-04-06 12:58:40 +01001003STATIC void emit_native_load_const_int(emit_t *emit, qstr qst) {
1004 DEBUG_printf("load_const_int %s\n", qstr_str(st));
1005 // for viper: load integer, check fits in 32 bits
1006 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001007 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_INT, qst, REG_ARG_1);
Damien Georgecdd96df2014-04-06 12:58:40 +01001008 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001009}
1010
Damien George7ff996c2014-09-08 23:05:16 +01001011STATIC void emit_native_load_const_dec(emit_t *emit, qstr qst) {
Damien6ba13142013-11-02 20:34:54 +00001012 // for viper, a float/complex is just a Python object
Damien Georgece8f07a2014-03-27 23:30:26 +00001013 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001014 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_DEC, qst, REG_ARG_1);
Damien6ba13142013-11-02 20:34:54 +00001015 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001016}
1017
Damien George7ff996c2014-09-08 23:05:16 +01001018STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001019 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001020 if (emit->do_viper_types) {
1021 // not implemented properly
1022 // load a pointer to the asciiz string?
1023 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001024 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien13ed3a62013-10-08 09:05:10 +01001025 } else {
Damien Georgeb601d952014-06-30 05:17:25 +01001026 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +01001027 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_BYTES, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001028 } else {
Damien George7ff996c2014-09-08 23:05:16 +01001029 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001030 }
Damien13ed3a62013-10-08 09:05:10 +01001031 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1032 }
1033}
1034
Damien George3558f622014-04-20 17:50:40 +01001035STATIC void emit_native_load_null(emit_t *emit) {
1036 emit_native_pre(emit);
1037 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1038}
1039
Damien George7ff996c2014-09-08 23:05:16 +01001040STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t id_flags, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001041 vtype_kind_t vtype = emit->local_vtype[local_num];
1042 if (vtype == VTYPE_UNBOUND) {
Damien George7ff996c2014-09-08 23:05:16 +01001043 printf("ViperTypeError: local %s used before type known\n", qstr_str(qst));
Damien13ed3a62013-10-08 09:05:10 +01001044 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001045 emit_native_pre(emit);
Damien3ef4abb2013-10-12 16:53:13 +01001046#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001047 if (local_num == 0) {
1048 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001049 } else if (local_num == 1) {
1050 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1051 } else if (local_num == 2) {
1052 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001053 } else {
Damiend2755ec2013-10-16 23:58:48 +01001054 need_reg_single(emit, REG_RAX, 0);
Damien Georged509ac22014-05-07 23:27:45 +01001055 asm_x64_mov_local_to_r64(emit->as, local_num - REG_LOCAL_NUM, REG_RAX);
Damien13ed3a62013-10-08 09:05:10 +01001056 emit_post_push_reg(emit, vtype, REG_RAX);
1057 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001058#elif N_X86
1059 if (local_num == 0) {
1060 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001061 } else if (local_num == 1) {
1062 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001063 } else if (local_num == 2) {
1064 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001065 } else {
1066 need_reg_single(emit, REG_EAX, 0);
1067 asm_x86_mov_local_to_r32(emit->as, local_num - REG_LOCAL_NUM, REG_EAX);
1068 emit_post_push_reg(emit, vtype, REG_EAX);
1069 }
Damien3ef4abb2013-10-12 16:53:13 +01001070#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001071 if (local_num == 0) {
1072 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1073 } else if (local_num == 1) {
1074 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1075 } else if (local_num == 2) {
1076 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1077 } else {
Damiend2755ec2013-10-16 23:58:48 +01001078 need_reg_single(emit, REG_R0, 0);
Damien Georged509ac22014-05-07 23:27:45 +01001079 asm_thumb_mov_reg_local(emit->as, REG_R0, local_num - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +01001080 emit_post_push_reg(emit, vtype, REG_R0);
1081 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001082#elif N_ARM
1083 if (local_num == 0) {
1084 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1085 } else if (local_num == 1) {
1086 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1087 } else if (local_num == 2) {
1088 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1089 } else {
1090 need_reg_single(emit, REG_R0, 0);
1091 asm_arm_mov_reg_local(emit->as, REG_R0, local_num - REG_LOCAL_NUM);
1092 emit_post_push_reg(emit, vtype, REG_R0);
1093 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001094#else
1095 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001096#endif
1097}
1098
Damien George7ff996c2014-09-08 23:05:16 +01001099STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001100 // not implemented
1101 // in principle could support this quite easily (ldr r0, [r0, #0]) and then get closed over variables!
1102 assert(0);
1103}
1104
Damien George7ff996c2014-09-08 23:05:16 +01001105STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001106 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001107 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001108 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1109}
1110
Damien George7ff996c2014-09-08 23:05:16 +01001111STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001112 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001113 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001114 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1115}
1116
Damien George7ff996c2014-09-08 23:05:16 +01001117STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001118 // depends on type of subject:
1119 // - integer, function, pointer to integers: error
1120 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001121 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001122 vtype_kind_t vtype_base;
1123 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1124 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001125 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001126 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1127}
1128
Damien George7ff996c2014-09-08 23:05:16 +01001129STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001130 vtype_kind_t vtype_base;
1131 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1132 assert(vtype_base == VTYPE_PYOBJ);
1133 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001134 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001135}
1136
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001137STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001138 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001139 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001140 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001141}
1142
Damien George729f7b42014-04-17 22:10:53 +01001143STATIC void emit_native_load_subscr(emit_t *emit) {
1144 vtype_kind_t vtype_lhs, vtype_rhs;
1145 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_2, &vtype_lhs, REG_ARG_1);
1146 if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George7fe21912014-08-16 22:31:57 +01001147 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 +01001148 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1149 } else {
1150 printf("ViperTypeError: can't do subscr of types %d and %d\n", vtype_lhs, vtype_rhs);
Damien George729f7b42014-04-17 22:10:53 +01001151 }
1152}
1153
Damien George7ff996c2014-09-08 23:05:16 +01001154STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001155 vtype_kind_t vtype;
Damien3ef4abb2013-10-12 16:53:13 +01001156#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001157 if (local_num == 0) {
1158 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001159 } else if (local_num == 1) {
1160 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1161 } else if (local_num == 2) {
1162 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001163 } else {
1164 emit_pre_pop_reg(emit, &vtype, REG_RAX);
Damien Georged509ac22014-05-07 23:27:45 +01001165 asm_x64_mov_r64_to_local(emit->as, REG_RAX, local_num - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +01001166 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001167#elif N_X86
1168 if (local_num == 0) {
1169 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001170 } else if (local_num == 1) {
1171 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001172 } else if (local_num == 2) {
1173 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001174 } else {
1175 emit_pre_pop_reg(emit, &vtype, REG_EAX);
1176 asm_x86_mov_r32_to_local(emit->as, REG_EAX, local_num - REG_LOCAL_NUM);
1177 }
Damien3ef4abb2013-10-12 16:53:13 +01001178#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001179 if (local_num == 0) {
1180 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1181 } else if (local_num == 1) {
1182 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1183 } else if (local_num == 2) {
1184 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1185 } else {
1186 emit_pre_pop_reg(emit, &vtype, REG_R0);
Damien Georged509ac22014-05-07 23:27:45 +01001187 asm_thumb_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_R0);
Damien13ed3a62013-10-08 09:05:10 +01001188 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001189#elif N_ARM
1190 if (local_num == 0) {
1191 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1192 } else if (local_num == 1) {
1193 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1194 } else if (local_num == 2) {
1195 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1196 } else {
1197 emit_pre_pop_reg(emit, &vtype, REG_R0);
1198 asm_arm_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_R0);
1199 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001200#else
1201 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001202#endif
1203
1204 emit_post(emit);
1205
1206 // check types
1207 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1208 // first time this local is assigned, so give it a type of the object stored in it
1209 emit->local_vtype[local_num] = vtype;
1210 } else if (emit->local_vtype[local_num] != vtype) {
1211 // type of local is not the same as object stored in it
Damien George7ff996c2014-09-08 23:05:16 +01001212 printf("ViperTypeError: type mismatch, local %s has type %d but source object has type %d\n", qstr_str(qst), emit->local_vtype[local_num], vtype);
Damien13ed3a62013-10-08 09:05:10 +01001213 }
1214}
1215
Damien George7ff996c2014-09-08 23:05:16 +01001216STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001217 // not implemented
1218 assert(0);
1219}
1220
Damien George7ff996c2014-09-08 23:05:16 +01001221STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001222 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001223 vtype_kind_t vtype;
1224 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1225 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001226 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001227 emit_post(emit);
1228}
1229
Damien George7ff996c2014-09-08 23:05:16 +01001230STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien Georgee6c0dff2014-08-15 23:47:59 +01001231 vtype_kind_t vtype = peek_vtype(emit);
1232 if (vtype == VTYPE_PYOBJ) {
1233 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1234 } else {
1235 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001236 emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, vtype, REG_ARG_2); // arg2 = type
Damien Georgec90f59e2014-09-06 23:06:36 +01001237 ASM_MOV_REG_TO_REG(emit->as, REG_RET, REG_ARG_2);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001238 }
Damien George7ff996c2014-09-08 23:05:16 +01001239 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001240 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001241}
1242
Damien George7ff996c2014-09-08 23:05:16 +01001243STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001244 vtype_kind_t vtype_base, vtype_val;
1245 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1246 assert(vtype_base == VTYPE_PYOBJ);
1247 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001248 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001249 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001250}
1251
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001252STATIC void emit_native_store_subscr(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001253 // depends on type of subject:
1254 // - integer, function, pointer to structure: error
1255 // - pointer to integers: store as per array
1256 // - Python object: call runtime with converted object or type info
1257 vtype_kind_t vtype_index, vtype_base, vtype_value;
1258 emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3); // index, base, value to store
1259 assert(vtype_index == VTYPE_PYOBJ);
1260 assert(vtype_base == VTYPE_PYOBJ);
1261 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001262 emit_call(emit, MP_F_OBJ_SUBSCR);
Damien13ed3a62013-10-08 09:05:10 +01001263}
1264
Damien George7ff996c2014-09-08 23:05:16 +01001265STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001266 // TODO implement me!
Damien13ed3a62013-10-08 09:05:10 +01001267 // could support for Python types, just set to None (so GC can reclaim it)
Damien13ed3a62013-10-08 09:05:10 +01001268}
1269
Damien George7ff996c2014-09-08 23:05:16 +01001270STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001271 // TODO implement me!
Damien9ecbcff2013-12-11 00:41:43 +00001272}
1273
Damien Georgee6ce10a2014-09-06 18:38:20 +01001274STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1275 emit_native_pre(emit);
1276 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1277 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001278}
1279
Damien Georgee6ce10a2014-09-06 18:38:20 +01001280STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1281 emit_native_pre(emit);
1282 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1283 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001284}
1285
Damien Georgee6ce10a2014-09-06 18:38:20 +01001286STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001287 vtype_kind_t vtype_base;
1288 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1289 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001290 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 +01001291 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001292}
1293
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001294STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001295 vtype_kind_t vtype_index, vtype_base;
1296 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1297 assert(vtype_index == VTYPE_PYOBJ);
1298 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001299 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 +01001300}
1301
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001302STATIC void emit_native_dup_top(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001303 vtype_kind_t vtype;
1304 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1305 emit_post_push_reg_reg(emit, vtype, REG_TEMP0, vtype, REG_TEMP0);
1306}
1307
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001308STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001309 vtype_kind_t vtype0, vtype1;
1310 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1311 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1312}
1313
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001314STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001315 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001316 emit_post(emit);
1317}
1318
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001319STATIC void emit_native_rot_two(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +01001320 vtype_kind_t vtype0, vtype1;
1321 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1322 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001323}
1324
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001325STATIC void emit_native_rot_three(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001326 vtype_kind_t vtype0, vtype1, vtype2;
1327 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1328 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1329}
1330
Damien George7ff996c2014-09-08 23:05:16 +01001331STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001332 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001333 // need to commit stack because we are jumping elsewhere
1334 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001335 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001336 emit_post(emit);
1337}
1338
Damien George7ff996c2014-09-08 23:05:16 +01001339STATIC void emit_native_jump_helper(emit_t *emit, mp_uint_t label, bool pop) {
Damien13ed3a62013-10-08 09:05:10 +01001340 vtype_kind_t vtype = peek_vtype(emit);
1341 if (vtype == VTYPE_BOOL) {
1342 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien Georgea32c1e42014-05-07 18:30:52 +01001343 if (!pop) {
1344 adjust_stack(emit, 1);
1345 }
Damien13ed3a62013-10-08 09:05:10 +01001346 } else if (vtype == VTYPE_PYOBJ) {
1347 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien Georgea32c1e42014-05-07 18:30:52 +01001348 if (!pop) {
Damien George02d95d72014-08-29 20:05:32 +01001349 adjust_stack(emit, 1);
Damien Georgea32c1e42014-05-07 18:30:52 +01001350 }
Damien George02d95d72014-08-29 20:05:32 +01001351 emit_call(emit, MP_F_OBJ_IS_TRUE);
Damien13ed3a62013-10-08 09:05:10 +01001352 } else {
1353 printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
1354 assert(0);
1355 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001356 // need to commit stack because we may jump elsewhere
1357 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001358}
1359
Damien George7ff996c2014-09-08 23:05:16 +01001360STATIC void emit_native_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
Damien Georgea32c1e42014-05-07 18:30:52 +01001361 emit_native_jump_helper(emit, label, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001362 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien1a6633a2013-11-03 13:58:19 +00001363 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001364}
Damien1a6633a2013-11-03 13:58:19 +00001365
Damien George7ff996c2014-09-08 23:05:16 +01001366STATIC void emit_native_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
Damien Georgea32c1e42014-05-07 18:30:52 +01001367 emit_native_jump_helper(emit, label, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001368 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001369 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001370}
Damien Georgea32c1e42014-05-07 18:30:52 +01001371
Damien George7ff996c2014-09-08 23:05:16 +01001372STATIC void emit_native_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georgea32c1e42014-05-07 18:30:52 +01001373 emit_native_jump_helper(emit, label, false);
Damien Georgec90f59e2014-09-06 23:06:36 +01001374 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001375 adjust_stack(emit, -1);
1376 emit_post(emit);
1377}
1378
Damien George7ff996c2014-09-08 23:05:16 +01001379STATIC void emit_native_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georgea32c1e42014-05-07 18:30:52 +01001380 emit_native_jump_helper(emit, label, false);
Damien Georgec90f59e2014-09-06 23:06:36 +01001381 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001382 adjust_stack(emit, -1);
1383 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001384}
1385
Damien George7ff996c2014-09-08 23:05:16 +01001386STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien George25c84642014-05-30 15:20:41 +01001387 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001388}
Damien Georgea32c1e42014-05-07 18:30:52 +01001389
Damien George7ff996c2014-09-08 23:05:16 +01001390STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgea32c1e42014-05-07 18:30:52 +01001391 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001392}
Damien Georgea32c1e42014-05-07 18:30:52 +01001393
Damien George7ff996c2014-09-08 23:05:16 +01001394STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001395 // not supported, or could be with runtime call
1396 assert(0);
1397}
Damien Georgeb601d952014-06-30 05:17:25 +01001398
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001399STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001400 assert(0);
1401}
Damien Georgeb601d952014-06-30 05:17:25 +01001402
Damien George7ff996c2014-09-08 23:05:16 +01001403STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001404 emit_native_pre(emit);
1405 // need to commit stack because we may jump elsewhere
1406 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001407 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 +01001408 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001409 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001410 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001411}
Damien Georgeb601d952014-06-30 05:17:25 +01001412
Damien George7ff996c2014-09-08 23:05:16 +01001413STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001414 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001415}
Damien Georgeb601d952014-06-30 05:17:25 +01001416
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001417STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001418 emit_pre_pop_discard(emit);
1419 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001420}
Damiend2755ec2013-10-16 23:58:48 +01001421
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001422STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001423 // perhaps the difficult one, as we want to rewrite for loops using native code
1424 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001425
1426 vtype_kind_t vtype;
1427 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1428 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001429 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01001430 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001431}
Damiend2755ec2013-10-16 23:58:48 +01001432
Damien George7ff996c2014-09-08 23:05:16 +01001433STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001434 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001435 vtype_kind_t vtype;
1436 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1437 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001438 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01001439 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
1440 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01001441 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1442}
1443
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001444STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001445 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001446 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001447 adjust_stack(emit, -1);
1448 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001449}
1450
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001451STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001452 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001453 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001454 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001455 emit_post(emit);
1456}
1457
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001458STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeb601d952014-06-30 05:17:25 +01001459 /*
1460 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001461 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001462 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01001463 emit_post(emit);
1464 */
Damien13ed3a62013-10-08 09:05:10 +01001465}
1466
Damien Georged17926d2014-03-30 13:35:08 +01001467STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georgeb601d952014-06-30 05:17:25 +01001468 if (op == MP_UNARY_OP_NOT) {
1469 // we need to synthesise this operation
1470 assert(0);
1471 } else {
1472 vtype_kind_t vtype;
1473 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1474 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001475 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001476 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1477 }
Damien13ed3a62013-10-08 09:05:10 +01001478}
1479
Damien Georged17926d2014-03-30 13:35:08 +01001480STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien13ed3a62013-10-08 09:05:10 +01001481 vtype_kind_t vtype_lhs, vtype_rhs;
1482 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
1483 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien Georged17926d2014-03-30 13:35:08 +01001484 if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
Damien3ef4abb2013-10-12 16:53:13 +01001485#if N_X64
Damien Georgebc1d3692014-01-11 09:47:06 +00001486 asm_x64_add_r64_to_r64(emit->as, REG_ARG_3, REG_ARG_2);
Damien Georgec90f59e2014-09-06 23:06:36 +01001487#elif N_X86
1488 asm_x86_add_r32_to_r32(emit->as, REG_ARG_3, REG_ARG_2);
Damien3ef4abb2013-10-12 16:53:13 +01001489#elif N_THUMB
Damien Georgea26dc502014-04-12 17:54:52 +01001490 asm_thumb_add_rlo_rlo_rlo(emit->as, REG_ARG_2, REG_ARG_2, REG_ARG_3);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001491#elif N_ARM
1492 asm_arm_add_reg(emit->as, REG_ARG_2, REG_ARG_2, REG_ARG_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001493#else
1494 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001495#endif
Damien Georgebc1d3692014-01-11 09:47:06 +00001496 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien Georged17926d2014-03-30 13:35:08 +01001497 } else if (op == MP_BINARY_OP_LESS) {
Damien Georgebc1d3692014-01-11 09:47:06 +00001498#if N_X64
1499 asm_x64_xor_r64_to_r64(emit->as, REG_RET, REG_RET);
1500 asm_x64_cmp_r64_with_r64(emit->as, REG_ARG_3, REG_ARG_2);
Damien Georgec90f59e2014-09-06 23:06:36 +01001501 asm_x64_setcc_r8(emit->as, ASM_X64_CC_JL, REG_RET);
1502#elif N_X86
1503 asm_x86_xor_r32_to_r32(emit->as, REG_RET, REG_RET);
1504 asm_x86_cmp_r32_with_r32(emit->as, REG_ARG_3, REG_ARG_2);
1505 asm_x86_setcc_r8(emit->as, ASM_X86_CC_JL, REG_RET);
Damien Georgebc1d3692014-01-11 09:47:06 +00001506#elif N_THUMB
Damien George87210872014-04-13 00:30:32 +01001507 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, REG_ARG_3);
Damien Georgebc1d3692014-01-11 09:47:06 +00001508 asm_thumb_ite_ge(emit->as);
Damien George87210872014-04-13 00:30:32 +01001509 asm_thumb_mov_rlo_i8(emit->as, REG_RET, 0); // if r0 >= r1
1510 asm_thumb_mov_rlo_i8(emit->as, REG_RET, 1); // if r0 < r1
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001511#elif N_ARM
1512 asm_arm_less_op(emit->as, REG_ARG_2, REG_ARG_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001513#else
1514 #error not implemented
Damien Georgebc1d3692014-01-11 09:47:06 +00001515#endif
1516 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
1517 } else {
1518 // TODO other ops not yet implemented
1519 assert(0);
1520 }
Damien13ed3a62013-10-08 09:05:10 +01001521 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George7fe21912014-08-16 22:31:57 +01001522 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001523 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1524 } else {
1525 printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs);
Damien Georgea5190a72014-08-15 22:39:08 +01001526 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001527 }
1528}
1529
Damien George7ff996c2014-09-08 23:05:16 +01001530STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01001531 // for viper: call runtime, with types of args
1532 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00001533 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001534 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001535 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01001536 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01001537}
1538
Damien George7ff996c2014-09-08 23:05:16 +01001539STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001540 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001541 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001542 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001543 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
1544}
1545
Damien George7ff996c2014-09-08 23:05:16 +01001546STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01001547 // only used in list comprehension
1548 vtype_kind_t vtype_list, vtype_item;
1549 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
1550 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
1551 assert(vtype_list == VTYPE_PYOBJ);
1552 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001553 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01001554 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001555}
1556
Damien George7ff996c2014-09-08 23:05:16 +01001557STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001558 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001559 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001560 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
1561}
1562
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001563STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001564 vtype_kind_t vtype_key, vtype_value, vtype_map;
1565 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
1566 assert(vtype_key == VTYPE_PYOBJ);
1567 assert(vtype_value == VTYPE_PYOBJ);
1568 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001569 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01001570 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
1571}
1572
Damien George7ff996c2014-09-08 23:05:16 +01001573STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01001574 // only used in list comprehension
1575 vtype_kind_t vtype_map, vtype_key, vtype_value;
1576 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
1577 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
1578 assert(vtype_map == VTYPE_PYOBJ);
1579 assert(vtype_key == VTYPE_PYOBJ);
1580 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001581 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01001582 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001583}
1584
Damien George7ff996c2014-09-08 23:05:16 +01001585STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001586 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001587 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001588 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001589 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
1590}
1591
Damien George7ff996c2014-09-08 23:05:16 +01001592STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01001593 // only used in set comprehension
1594 vtype_kind_t vtype_set, vtype_item;
1595 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
1596 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
1597 assert(vtype_set == VTYPE_PYOBJ);
1598 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001599 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01001600 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001601}
Damiend2755ec2013-10-16 23:58:48 +01001602
Damien George7ff996c2014-09-08 23:05:16 +01001603STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001604 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01001605 if (n_args == 2) {
1606 vtype_kind_t vtype_start, vtype_stop;
1607 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
1608 assert(vtype_start == VTYPE_PYOBJ);
1609 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001610 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 +01001611 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1612 } else {
1613 assert(n_args == 3);
1614 vtype_kind_t vtype_start, vtype_stop, vtype_step;
1615 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
1616 assert(vtype_start == VTYPE_PYOBJ);
1617 assert(vtype_stop == VTYPE_PYOBJ);
1618 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001619 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01001620 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1621 }
Damien13ed3a62013-10-08 09:05:10 +01001622}
Damien Georgecdd96df2014-04-06 12:58:40 +01001623
Damien George7ff996c2014-09-08 23:05:16 +01001624STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001625 DEBUG_printf("unpack_sequence %d\n", n_args);
1626 vtype_kind_t vtype_base;
1627 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
1628 assert(vtype_base == VTYPE_PYOBJ);
1629 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01001630 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01001631}
Damien Georgecdd96df2014-04-06 12:58:40 +01001632
Damien George7ff996c2014-09-08 23:05:16 +01001633STATIC 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 +01001634 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
1635 vtype_kind_t vtype_base;
1636 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
1637 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001638 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 +01001639 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 +01001640}
1641
Damien George7ff996c2014-09-08 23:05:16 +01001642STATIC 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 +01001643 // 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 +00001644 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001645 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01001646 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 +01001647 } else {
1648 vtype_kind_t vtype_def_tuple, vtype_def_dict;
1649 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
1650 assert(vtype_def_tuple == VTYPE_PYOBJ);
1651 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001652 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 +01001653 }
Damien13ed3a62013-10-08 09:05:10 +01001654 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1655}
1656
Damien George7ff996c2014-09-08 23:05:16 +01001657STATIC void emit_native_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
Damien13ed3a62013-10-08 09:05:10 +01001658 assert(0);
1659}
1660
Damien George7ff996c2014-09-08 23:05:16 +01001661STATIC void emit_native_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
Damien13ed3a62013-10-08 09:05:10 +01001662 // call special viper runtime routine with type info for args, and wanted type info for return
Damien George922ddd62014-04-09 12:43:17 +01001663 assert(!star_flags);
Damien Georgecd82e022014-02-02 13:11:48 +00001664
1665 /* we no longer have these _n specific call_function's
1666 * they anyway push args into an array
1667 * and they would take too much room in the native dispatch table
Damien13ed3a62013-10-08 09:05:10 +01001668 if (n_positional == 0) {
1669 vtype_kind_t vtype_fun;
1670 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
1671 assert(vtype_fun == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001672 emit_call(emit, MP_F_CALL_FUNCTION_0);
Damien13ed3a62013-10-08 09:05:10 +01001673 } else if (n_positional == 1) {
1674 vtype_kind_t vtype_fun, vtype_arg1;
1675 emit_pre_pop_reg_reg(emit, &vtype_arg1, REG_ARG_2, &vtype_fun, REG_ARG_1); // the single argument, the function
1676 assert(vtype_fun == VTYPE_PYOBJ);
1677 assert(vtype_arg1 == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001678 emit_call(emit, MP_F_CALL_FUNCTION_1);
Damien13ed3a62013-10-08 09:05:10 +01001679 } else if (n_positional == 2) {
1680 vtype_kind_t vtype_fun, vtype_arg1, vtype_arg2;
1681 emit_pre_pop_reg_reg_reg(emit, &vtype_arg2, REG_ARG_3, &vtype_arg1, REG_ARG_2, &vtype_fun, REG_ARG_1); // the second argument, the first argument, the function
1682 assert(vtype_fun == VTYPE_PYOBJ);
1683 assert(vtype_arg1 == VTYPE_PYOBJ);
1684 assert(vtype_arg2 == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001685 emit_call(emit, MP_F_CALL_FUNCTION_2);
Damien13ed3a62013-10-08 09:05:10 +01001686 } else {
Damieneb19efb2013-10-10 22:06:54 +01001687 */
Damien Georgecd82e022014-02-02 13:11:48 +00001688
Damien Georgece8f07a2014-03-27 23:30:26 +00001689 emit_native_pre(emit);
Damien Georgecdd96df2014-04-06 12:58:40 +01001690 if (n_positional != 0 || n_keyword != 0) {
1691 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
Damien Georgecd82e022014-02-02 13:11:48 +00001692 }
1693 vtype_kind_t vtype_fun;
1694 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
1695 assert(vtype_fun == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001696 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +01001697 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1698}
1699
Damien George7ff996c2014-09-08 23:05:16 +01001700STATIC void emit_native_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
Damien George922ddd62014-04-09 12:43:17 +01001701 assert(!star_flags);
Damien Georgecd82e022014-02-02 13:11:48 +00001702
Damieneb19efb2013-10-10 22:06:54 +01001703 /*
Damien13ed3a62013-10-08 09:05:10 +01001704 if (n_positional == 0) {
1705 vtype_kind_t vtype_meth, vtype_self;
1706 emit_pre_pop_reg_reg(emit, &vtype_self, REG_ARG_2, &vtype_meth, REG_ARG_1); // the self object (or NULL), the method
1707 assert(vtype_meth == VTYPE_PYOBJ);
1708 assert(vtype_self == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001709 emit_call(emit, MP_F_CALL_METHOD_1);
Damien13ed3a62013-10-08 09:05:10 +01001710 } else if (n_positional == 1) {
1711 vtype_kind_t vtype_meth, vtype_self, vtype_arg1;
1712 emit_pre_pop_reg_reg_reg(emit, &vtype_arg1, REG_ARG_3, &vtype_self, REG_ARG_2, &vtype_meth, REG_ARG_1); // the first argument, the self object (or NULL), the method
1713 assert(vtype_meth == VTYPE_PYOBJ);
1714 assert(vtype_self == VTYPE_PYOBJ);
1715 assert(vtype_arg1 == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001716 emit_call(emit, MP_F_CALL_METHOD_2);
Damien13ed3a62013-10-08 09:05:10 +01001717 } else {
Damieneb19efb2013-10-10 22:06:54 +01001718 */
Damien Georgecd82e022014-02-02 13:11:48 +00001719
Damien Georgece8f07a2014-03-27 23:30:26 +00001720 emit_native_pre(emit);
Damien Georgecdd96df2014-04-06 12:58:40 +01001721 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, 2 + n_positional + 2 * n_keyword); // pointer to items, including meth and self
Damien George7fe21912014-08-16 22:31:57 +01001722 emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, n_positional, REG_ARG_1, n_keyword, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +01001723 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1724}
1725
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001726STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001727 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01001728 vtype_kind_t vtype;
1729 emit_pre_pop_reg(emit, &vtype, REG_RET);
1730 if (emit->do_viper_types) {
Damien Georgee6c0dff2014-08-15 23:47:59 +01001731 if (vtype == VTYPE_PTR_NONE) {
1732 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01001733 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001734 }
1735 } else if (vtype != emit->return_vtype) {
Damien George2ac4af62014-08-15 16:45:41 +01001736 printf("ViperTypeError: incompatible return type\n");
1737 }
Damien13ed3a62013-10-08 09:05:10 +01001738 } else {
1739 assert(vtype == VTYPE_PYOBJ);
1740 }
1741 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01001742 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
1743 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01001744}
1745
Damien George7ff996c2014-09-08 23:05:16 +01001746STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01001747 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01001748 vtype_kind_t vtype_exc;
1749 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
1750 if (vtype_exc != VTYPE_PYOBJ) {
1751 printf("ViperTypeError: must raise an object\n");
1752 }
1753 // 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 +01001754 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01001755}
Damien Georgeb601d952014-06-30 05:17:25 +01001756
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001757STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001758 // not supported (for now)
1759 assert(0);
1760}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001761STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001762 // not supported (for now)
1763 assert(0);
1764}
1765
Damien Georgeb601d952014-06-30 05:17:25 +01001766STATIC void emit_native_start_except_handler(emit_t *emit) {
1767 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
1768 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
1769 // the first 2 elements, so we can get the thrown value.
1770 adjust_stack(emit, 2);
1771 vtype_kind_t vtype_nlr;
1772 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01001773 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01001774 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
1775}
1776
1777STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001778 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01001779}
1780
Damien13ed3a62013-10-08 09:05:10 +01001781const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01001782 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01001783 emit_native_start_pass,
1784 emit_native_end_pass,
1785 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00001786 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00001787 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01001788
1789 emit_native_load_id,
1790 emit_native_store_id,
1791 emit_native_delete_id,
1792
1793 emit_native_label_assign,
1794 emit_native_import_name,
1795 emit_native_import_from,
1796 emit_native_import_star,
1797 emit_native_load_const_tok,
1798 emit_native_load_const_small_int,
1799 emit_native_load_const_int,
1800 emit_native_load_const_dec,
Damien13ed3a62013-10-08 09:05:10 +01001801 emit_native_load_const_str,
Damien George3558f622014-04-20 17:50:40 +01001802 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01001803 emit_native_load_fast,
Damien13ed3a62013-10-08 09:05:10 +01001804 emit_native_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +00001805 emit_native_load_name,
1806 emit_native_load_global,
Damien13ed3a62013-10-08 09:05:10 +01001807 emit_native_load_attr,
1808 emit_native_load_method,
1809 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01001810 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01001811 emit_native_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +00001812 emit_native_store_deref,
Damien13ed3a62013-10-08 09:05:10 +01001813 emit_native_store_name,
1814 emit_native_store_global,
Damien13ed3a62013-10-08 09:05:10 +01001815 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01001816 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01001817 emit_native_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +00001818 emit_native_delete_deref,
Damien13ed3a62013-10-08 09:05:10 +01001819 emit_native_delete_name,
1820 emit_native_delete_global,
Damien13ed3a62013-10-08 09:05:10 +01001821 emit_native_delete_attr,
1822 emit_native_delete_subscr,
1823 emit_native_dup_top,
1824 emit_native_dup_top_two,
1825 emit_native_pop_top,
1826 emit_native_rot_two,
1827 emit_native_rot_three,
1828 emit_native_jump,
1829 emit_native_pop_jump_if_true,
1830 emit_native_pop_jump_if_false,
1831 emit_native_jump_if_true_or_pop,
1832 emit_native_jump_if_false_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01001833 emit_native_break_loop,
1834 emit_native_continue_loop,
1835 emit_native_setup_with,
1836 emit_native_with_cleanup,
1837 emit_native_setup_except,
1838 emit_native_setup_finally,
1839 emit_native_end_finally,
1840 emit_native_get_iter,
1841 emit_native_for_iter,
1842 emit_native_for_iter_end,
1843 emit_native_pop_block,
1844 emit_native_pop_except,
1845 emit_native_unary_op,
1846 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01001847 emit_native_build_tuple,
1848 emit_native_build_list,
1849 emit_native_list_append,
1850 emit_native_build_map,
1851 emit_native_store_map,
1852 emit_native_map_add,
1853 emit_native_build_set,
1854 emit_native_set_add,
1855 emit_native_build_slice,
1856 emit_native_unpack_sequence,
1857 emit_native_unpack_ex,
1858 emit_native_make_function,
1859 emit_native_make_closure,
1860 emit_native_call_function,
1861 emit_native_call_method,
1862 emit_native_return_value,
1863 emit_native_raise_varargs,
1864 emit_native_yield_value,
1865 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01001866
1867 emit_native_start_except_handler,
1868 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01001869};
1870
Damien Georgec90f59e2014-09-06 23:06:36 +01001871#endif