blob: dde582d0954caacf2ff016615b69cc69bcdee062 [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 Georgee67ed5d2014-01-04 13:55:24 +000072#if (MICROPY_EMIT_X64 && N_X64) || (MICROPY_EMIT_THUMB && N_THUMB)
Damien13ed3a62013-10-08 09:05:10 +010073
Damien3ef4abb2013-10-12 16:53:13 +010074#if N_X64
Damien13ed3a62013-10-08 09:05:10 +010075
76// x64 specific stuff
77
78#include "asmx64.h"
79
80#define REG_LOCAL_1 (REG_RBX)
81#define REG_LOCAL_NUM (1)
82
83#define EXPORT_FUN(name) emit_native_x64_##name
84
85#define REG_TEMP0 (REG_RAX)
86#define REG_TEMP1 (REG_RDI)
87#define REG_TEMP2 (REG_RSI)
88#define ASM_MOV_REG_TO_LOCAL(reg, local_num) asm_x64_mov_r64_to_local(emit->as, (reg), (local_num))
89#define ASM_MOV_IMM_TO_REG(imm, reg) asm_x64_mov_i64_to_r64_optimised(emit->as, (imm), (reg))
Damien George36db6bc2014-05-07 17:24:22 +010090#define ASM_MOV_ALIGNED_IMM_TO_REG(imm, reg) asm_x64_mov_i64_to_r64_aligned(emit->as, (imm), (reg))
Damieneb19efb2013-10-10 22:06:54 +010091#define ASM_MOV_IMM_TO_LOCAL_USING(imm, local_num, reg_temp) do { asm_x64_mov_i64_to_r64_optimised(emit->as, (imm), (reg_temp)); asm_x64_mov_r64_to_local(emit->as, (reg_temp), (local_num)); } while (false)
Damien13ed3a62013-10-08 09:05:10 +010092#define ASM_MOV_LOCAL_TO_REG(local_num, reg) asm_x64_mov_local_to_r64(emit->as, (local_num), (reg))
93#define ASM_MOV_REG_TO_REG(reg_src, reg_dest) asm_x64_mov_r64_to_r64(emit->as, (reg_src), (reg_dest))
94#define ASM_MOV_LOCAL_ADDR_TO_REG(local_num, reg) asm_x64_mov_local_addr_to_r64(emit->as, (local_num), (reg))
95
Damien3ef4abb2013-10-12 16:53:13 +010096#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +010097
98// thumb specific stuff
99
100#include "asmthumb.h"
101
102#define REG_LOCAL_1 (REG_R4)
103#define REG_LOCAL_2 (REG_R5)
104#define REG_LOCAL_3 (REG_R6)
105#define REG_LOCAL_NUM (3)
106
107#define EXPORT_FUN(name) emit_native_thumb_##name
108
109#define REG_TEMP0 (REG_R0)
110#define REG_TEMP1 (REG_R1)
111#define REG_TEMP2 (REG_R2)
112#define ASM_MOV_REG_TO_LOCAL(reg, local_num) asm_thumb_mov_local_reg(emit->as, (local_num), (reg))
113#define ASM_MOV_IMM_TO_REG(imm, reg) asm_thumb_mov_reg_i32_optimised(emit->as, (reg), (imm))
Damien George36db6bc2014-05-07 17:24:22 +0100114#define ASM_MOV_ALIGNED_IMM_TO_REG(imm, reg) asm_thumb_mov_reg_i32_aligned(emit->as, (reg), (imm))
Damieneb19efb2013-10-10 22:06:54 +0100115#define ASM_MOV_IMM_TO_LOCAL_USING(imm, local_num, reg_temp) do { asm_thumb_mov_reg_i32_optimised(emit->as, (reg_temp), (imm)); asm_thumb_mov_local_reg(emit->as, (local_num), (reg_temp)); } while (false)
Damien13ed3a62013-10-08 09:05:10 +0100116#define ASM_MOV_LOCAL_TO_REG(local_num, reg) asm_thumb_mov_reg_local(emit->as, (reg), (local_num))
117#define ASM_MOV_REG_TO_REG(reg_src, reg_dest) asm_thumb_mov_reg_reg(emit->as, (reg_dest), (reg_src))
118#define ASM_MOV_LOCAL_ADDR_TO_REG(local_num, reg) asm_thumb_mov_reg_local_addr(emit->as, (reg), (local_num))
119
120#endif
121
122typedef enum {
Damienff8ed772013-10-08 22:18:32 +0100123 STACK_VALUE,
124 STACK_REG,
125 STACK_IMM,
126} stack_info_kind_t;
Damien13ed3a62013-10-08 09:05:10 +0100127
128typedef enum {
129 VTYPE_UNBOUND,
130 VTYPE_PYOBJ,
131 VTYPE_BOOL,
132 VTYPE_INT,
133 VTYPE_PTR,
134 VTYPE_PTR_NONE,
135 VTYPE_BUILTIN_V_INT,
136} vtype_kind_t;
137
Damienff8ed772013-10-08 22:18:32 +0100138typedef struct _stack_info_t {
139 vtype_kind_t vtype;
140 stack_info_kind_t kind;
141 union {
142 int u_reg;
Damien George40f3c022014-07-03 13:25:24 +0100143 mp_int_t u_imm;
Damienff8ed772013-10-08 22:18:32 +0100144 };
145} stack_info_t;
146
Damien13ed3a62013-10-08 09:05:10 +0100147struct _emit_t {
148 int pass;
149
150 bool do_viper_types;
Damienff8ed772013-10-08 22:18:32 +0100151
Damien George36db6bc2014-05-07 17:24:22 +0100152 uint local_vtype_alloc;
Damien13ed3a62013-10-08 09:05:10 +0100153 vtype_kind_t *local_vtype;
Damienff8ed772013-10-08 22:18:32 +0100154
Damien George36db6bc2014-05-07 17:24:22 +0100155 uint stack_info_alloc;
Damienff8ed772013-10-08 22:18:32 +0100156 stack_info_t *stack_info;
157
Damien13ed3a62013-10-08 09:05:10 +0100158 int stack_start;
159 int stack_size;
160
161 bool last_emit_was_return_value;
162
Damien13ed3a62013-10-08 09:05:10 +0100163 scope_t *scope;
164
Damien3ef4abb2013-10-12 16:53:13 +0100165#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100166 asm_x64_t *as;
Damien3ef4abb2013-10-12 16:53:13 +0100167#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100168 asm_thumb_t *as;
169#endif
170};
171
172emit_t *EXPORT_FUN(new)(uint max_num_labels) {
Damien George36db6bc2014-05-07 17:24:22 +0100173 emit_t *emit = m_new0(emit_t, 1);
Damien3ef4abb2013-10-12 16:53:13 +0100174#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100175 emit->as = asm_x64_new(max_num_labels);
Damien3ef4abb2013-10-12 16:53:13 +0100176#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100177 emit->as = asm_thumb_new(max_num_labels);
178#endif
179 return emit;
180}
181
Damien George41d02b62014-01-24 22:42:28 +0000182void EXPORT_FUN(free)(emit_t *emit) {
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200183#if N_X64
184 asm_x64_free(emit->as, false);
185#elif N_THUMB
186 asm_thumb_free(emit->as, false);
187#endif
Damien George36db6bc2014-05-07 17:24:22 +0100188 m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
189 m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200190 m_del_obj(emit_t, emit);
191}
192
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200193STATIC void emit_native_set_viper_types(emit_t *emit, bool do_viper_types) {
Damien13ed3a62013-10-08 09:05:10 +0100194 emit->do_viper_types = do_viper_types;
195}
196
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200197STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien13ed3a62013-10-08 09:05:10 +0100198 emit->pass = pass;
199 emit->stack_start = 0;
200 emit->stack_size = 0;
201 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100202 emit->scope = scope;
203
Damien George36db6bc2014-05-07 17:24:22 +0100204 // allocate memory for keeping track of the types of locals
205 if (emit->local_vtype_alloc < scope->num_locals) {
206 emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
207 emit->local_vtype_alloc = scope->num_locals;
Damienff8ed772013-10-08 22:18:32 +0100208 }
Damien George36db6bc2014-05-07 17:24:22 +0100209
210 // allocate memory for keeping track of the objects on the stack
211 // XXX don't know stack size on entry, and it should be maximum over all scopes
Damienff8ed772013-10-08 22:18:32 +0100212 if (emit->stack_info == NULL) {
Damien George36db6bc2014-05-07 17:24:22 +0100213 emit->stack_info_alloc = scope->stack_size + 50;
Damienff8ed772013-10-08 22:18:32 +0100214 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100215 }
216
217 if (emit->do_viper_types) {
Damien13ed3a62013-10-08 09:05:10 +0100218 // TODO set types of arguments based on type signature
Damienff8ed772013-10-08 22:18:32 +0100219 for (int i = 0; i < emit->local_vtype_alloc; i++) {
220 emit->local_vtype[i] = VTYPE_UNBOUND;
221 }
222 for (int i = 0; i < emit->stack_info_alloc; i++) {
223 emit->stack_info[i].kind = STACK_VALUE;
224 emit->stack_info[i].vtype = VTYPE_UNBOUND;
225 }
Damien13ed3a62013-10-08 09:05:10 +0100226 } else {
Damienff8ed772013-10-08 22:18:32 +0100227 for (int i = 0; i < emit->local_vtype_alloc; i++) {
228 emit->local_vtype[i] = VTYPE_PYOBJ;
229 }
230 for (int i = 0; i < emit->stack_info_alloc; i++) {
231 emit->stack_info[i].kind = STACK_VALUE;
232 emit->stack_info[i].vtype = VTYPE_PYOBJ;
Damien13ed3a62013-10-08 09:05:10 +0100233 }
234 }
235
Damien3ef4abb2013-10-12 16:53:13 +0100236#if N_X64
Damien George36db6bc2014-05-07 17:24:22 +0100237 asm_x64_start_pass(emit->as, pass == MP_PASS_EMIT ? ASM_X64_PASS_EMIT : ASM_X64_PASS_COMPUTE);
Damien3ef4abb2013-10-12 16:53:13 +0100238#elif N_THUMB
Damien George36db6bc2014-05-07 17:24:22 +0100239 asm_thumb_start_pass(emit->as, pass == MP_PASS_EMIT ? ASM_THUMB_PASS_EMIT : ASM_THUMB_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100240#endif
241
242 // entry to function
243 int num_locals = 0;
Damien George36db6bc2014-05-07 17:24:22 +0100244 if (pass > MP_PASS_SCOPE) {
Damien13ed3a62013-10-08 09:05:10 +0100245 num_locals = scope->num_locals - REG_LOCAL_NUM;
246 if (num_locals < 0) {
247 num_locals = 0;
248 }
249 emit->stack_start = num_locals;
250 num_locals += scope->stack_size;
251 }
Damien3ef4abb2013-10-12 16:53:13 +0100252#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100253 asm_x64_entry(emit->as, num_locals);
Damien3ef4abb2013-10-12 16:53:13 +0100254#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100255 asm_thumb_entry(emit->as, num_locals);
256#endif
257
258 // initialise locals from parameters
Damien3ef4abb2013-10-12 16:53:13 +0100259#if N_X64
Damien George2827d622014-04-27 15:50:52 +0100260 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100261 if (i == 0) {
262 asm_x64_mov_r64_to_r64(emit->as, REG_ARG_1, REG_LOCAL_1);
263 } else if (i == 1) {
Damien Georged509ac22014-05-07 23:27:45 +0100264 asm_x64_mov_r64_to_local(emit->as, REG_ARG_2, i - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +0100265 } else if (i == 2) {
Damien Georged509ac22014-05-07 23:27:45 +0100266 asm_x64_mov_r64_to_local(emit->as, REG_ARG_3, i - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +0100267 } else {
268 // TODO not implemented
269 assert(0);
270 }
271 }
Damien3ef4abb2013-10-12 16:53:13 +0100272#elif N_THUMB
Damien George2827d622014-04-27 15:50:52 +0100273 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100274 if (i == 0) {
275 asm_thumb_mov_reg_reg(emit->as, REG_LOCAL_1, REG_ARG_1);
276 } else if (i == 1) {
277 asm_thumb_mov_reg_reg(emit->as, REG_LOCAL_2, REG_ARG_2);
278 } else if (i == 2) {
279 asm_thumb_mov_reg_reg(emit->as, REG_LOCAL_3, REG_ARG_3);
280 } else if (i == 3) {
281 asm_thumb_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
282 } else {
283 // TODO not implemented
284 assert(0);
285 }
286 }
287
Damien George40f3c022014-07-03 13:25:24 +0100288 asm_thumb_mov_reg_i32(emit->as, REG_R7, (mp_uint_t)mp_fun_table);
Damien13ed3a62013-10-08 09:05:10 +0100289#endif
290}
291
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200292STATIC void emit_native_end_pass(emit_t *emit) {
Damien3ef4abb2013-10-12 16:53:13 +0100293#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100294 if (!emit->last_emit_was_return_value) {
295 asm_x64_exit(emit->as);
296 }
297 asm_x64_end_pass(emit->as);
Damien3ef4abb2013-10-12 16:53:13 +0100298#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100299 if (!emit->last_emit_was_return_value) {
300 asm_thumb_exit(emit->as);
301 }
302 asm_thumb_end_pass(emit->as);
303#endif
304
305 // check stack is back to zero size
306 if (emit->stack_size != 0) {
307 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
308 }
309
Damien George36db6bc2014-05-07 17:24:22 +0100310 if (emit->pass == MP_PASS_EMIT) {
Damien3ef4abb2013-10-12 16:53:13 +0100311#if N_X64
Damiend99b0522013-12-21 18:17:45 +0000312 void *f = asm_x64_get_code(emit->as);
Damien Georgeccc85ea2014-05-10 13:40:46 +0100313 mp_emit_glue_assign_native(emit->scope->raw_code, emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY, f, asm_x64_get_code_size(emit->as), emit->scope->num_pos_args);
Damien3ef4abb2013-10-12 16:53:13 +0100314#elif N_THUMB
Damiend99b0522013-12-21 18:17:45 +0000315 void *f = asm_thumb_get_code(emit->as);
Damien Georgeccc85ea2014-05-10 13:40:46 +0100316 mp_emit_glue_assign_native(emit->scope->raw_code, emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY, f, asm_thumb_get_code_size(emit->as), emit->scope->num_pos_args);
Damien13ed3a62013-10-08 09:05:10 +0100317#endif
318 }
319}
320
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200321STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100322 return emit->last_emit_was_return_value;
323}
324
Damien Georged66ae182014-04-10 17:28:54 +0000325STATIC void emit_native_adjust_stack_size(emit_t *emit, int delta) {
326 emit->stack_size += delta;
Damien13ed3a62013-10-08 09:05:10 +0100327}
328
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200329STATIC void emit_native_set_source_line(emit_t *emit, int source_line) {
Damien George08335002014-01-18 23:24:36 +0000330}
331
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200332STATIC void adjust_stack(emit_t *emit, int stack_size_delta) {
Damien Georgecdd96df2014-04-06 12:58:40 +0100333 DEBUG_printf("adjust stack: stack:%d + delta:%d\n", emit->stack_size, stack_size_delta);
Damien George78035b92014-04-09 12:27:39 +0100334 assert((int)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100335 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100336 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100337 emit->scope->stack_size = emit->stack_size;
338 }
339}
340
Damienff8ed772013-10-08 22:18:32 +0100341/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200342STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100343 adjust_stack(emit, stack_size_delta);
344 emit->last_emit_was_return_value = false;
345}
Damienff8ed772013-10-08 22:18:32 +0100346*/
Damien13ed3a62013-10-08 09:05:10 +0100347
Damienff8ed772013-10-08 22:18:32 +0100348// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000349STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100350 emit->last_emit_was_return_value = false;
351 // settle the stack
352 /*
353 if (regs_needed != 0) {
354 for (int i = 0; i < emit->stack_size; i++) {
355 switch (emit->stack_info[i].kind) {
356 case STACK_VALUE:
357 break;
358
359 case STACK_REG:
360 // TODO only push reg if in regs_needed
361 emit->stack_info[i].kind = STACK_VALUE;
362 ASM_MOV_REG_TO_LOCAL(emit->stack_info[i].u_reg, emit->stack_start + i);
363 break;
364
365 case STACK_IMM:
366 // don't think we ever need to push imms for settling
367 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
368 break;
369 }
370 }
371 }
372 */
Damien13ed3a62013-10-08 09:05:10 +0100373}
374
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200375STATIC vtype_kind_t peek_vtype(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100376 return emit->stack_info[emit->stack_size - 1].vtype;
377}
Damien13ed3a62013-10-08 09:05:10 +0100378
Damiend2755ec2013-10-16 23:58:48 +0100379// pos=1 is TOS, pos=2 is next, etc
380// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200381STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100382 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100383 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100384 if (i != skip_stack_pos) {
385 stack_info_t *si = &emit->stack_info[i];
386 if (si->kind == STACK_REG && si->u_reg == reg_needed) {
387 si->kind = STACK_VALUE;
388 ASM_MOV_REG_TO_LOCAL(si->u_reg, emit->stack_start + i);
389 }
Damienff8ed772013-10-08 22:18:32 +0100390 }
391 }
392}
Damien13ed3a62013-10-08 09:05:10 +0100393
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200394STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100395 for (int i = 0; i < emit->stack_size; i++) {
396 stack_info_t *si = &emit->stack_info[i];
397 if (si->kind == STACK_REG) {
398 si->kind = STACK_VALUE;
399 ASM_MOV_REG_TO_LOCAL(si->u_reg, emit->stack_start + i);
400 }
Damien13ed3a62013-10-08 09:05:10 +0100401 }
402}
403
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200404STATIC void need_stack_settled(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +0100405 for (int i = 0; i < emit->stack_size; i++) {
406 stack_info_t *si = &emit->stack_info[i];
407 if (si->kind == STACK_REG) {
408 si->kind = STACK_VALUE;
409 ASM_MOV_REG_TO_LOCAL(si->u_reg, emit->stack_start + i);
410 }
411 }
412 for (int i = 0; i < emit->stack_size; i++) {
413 stack_info_t *si = &emit->stack_info[i];
414 if (si->kind == STACK_IMM) {
415 ASM_MOV_IMM_TO_LOCAL_USING(si->u_imm, emit->stack_start + i, REG_TEMP0);
416 }
417 }
418}
419
420// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200421STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100422 need_reg_single(emit, reg_dest, pos);
423 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +0100424 *vtype = si->vtype;
425 switch (si->kind) {
426 case STACK_VALUE:
Damiend2755ec2013-10-16 23:58:48 +0100427 ASM_MOV_LOCAL_TO_REG(emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100428 break;
429
Damienff8ed772013-10-08 22:18:32 +0100430 case STACK_REG:
431 if (si->u_reg != reg_dest) {
432 ASM_MOV_REG_TO_REG(si->u_reg, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100433 }
434 break;
435
Damienff8ed772013-10-08 22:18:32 +0100436 case STACK_IMM:
437 ASM_MOV_IMM_TO_REG(si->u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100438 break;
439 }
Damien13ed3a62013-10-08 09:05:10 +0100440}
441
Damien Georgeccc85ea2014-05-10 13:40:46 +0100442STATIC void emit_pre_pop_discard(emit_t *emit, vtype_kind_t *vtype) {
443 emit->last_emit_was_return_value = false;
444 adjust_stack(emit, -1);
445}
446
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200447STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100448 emit->last_emit_was_return_value = false;
449 emit_access_stack(emit, 1, vtype, reg_dest);
450 adjust_stack(emit, -1);
451}
452
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200453STATIC 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 +0100454 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100455 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +0100456}
457
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200458STATIC 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 +0100459 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100460 emit_pre_pop_reg(emit, vtypeb, regb);
461 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100462}
463
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200464STATIC void emit_post(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100465}
466
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200467STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +0100468 stack_info_t *si = &emit->stack_info[emit->stack_size];
469 si->vtype = vtype;
470 si->kind = STACK_REG;
471 si->u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +0100472 adjust_stack(emit, 1);
473}
474
Damien George40f3c022014-07-03 13:25:24 +0100475STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +0100476 stack_info_t *si = &emit->stack_info[emit->stack_size];
477 si->vtype = vtype;
478 si->kind = STACK_IMM;
479 si->u_imm = imm;
480 adjust_stack(emit, 1);
481}
482
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200483STATIC 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 +0100484 emit_post_push_reg(emit, vtypea, rega);
485 emit_post_push_reg(emit, vtypeb, regb);
486}
487
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200488STATIC 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 +0100489 emit_post_push_reg(emit, vtypea, rega);
490 emit_post_push_reg(emit, vtypeb, regb);
491 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100492}
493
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200494STATIC 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 +0100495 emit_post_push_reg(emit, vtypea, rega);
496 emit_post_push_reg(emit, vtypeb, regb);
497 emit_post_push_reg(emit, vtypec, regc);
498 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +0100499}
500
501// vtype of all n_pop objects is VTYPE_PYOBJ
Damieneb19efb2013-10-10 22:06:54 +0100502// does not use any temporary registers (but may use reg_dest before loading it with stack pointer)
Damien6d4f3462013-11-16 20:44:39 +0000503// TODO this needs some thinking for viper code
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200504STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, int reg_dest, int n_pop) {
Damieneb19efb2013-10-10 22:06:54 +0100505 need_reg_all(emit);
Damienff8ed772013-10-08 22:18:32 +0100506 for (int i = 0; i < n_pop; i++) {
Damieneb19efb2013-10-10 22:06:54 +0100507 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
508 // must push any imm's to stack
Damien6d4f3462013-11-16 20:44:39 +0000509 // must convert them to VTYPE_PYOBJ for viper code
Damieneb19efb2013-10-10 22:06:54 +0100510 if (si->kind == STACK_IMM) {
511 si->kind = STACK_VALUE;
Damien6d4f3462013-11-16 20:44:39 +0000512 switch (si->vtype) {
513 case VTYPE_PYOBJ:
514 ASM_MOV_IMM_TO_LOCAL_USING(si->u_imm, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
515 break;
516 case VTYPE_BOOL:
517 si->vtype = VTYPE_PYOBJ;
518 if (si->u_imm == 0) {
Damien George40f3c022014-07-03 13:25:24 +0100519 ASM_MOV_IMM_TO_LOCAL_USING((mp_uint_t)mp_const_false, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
Damien6d4f3462013-11-16 20:44:39 +0000520 } else {
Damien George40f3c022014-07-03 13:25:24 +0100521 ASM_MOV_IMM_TO_LOCAL_USING((mp_uint_t)mp_const_true, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
Damien6d4f3462013-11-16 20:44:39 +0000522 }
523 break;
524 case VTYPE_INT:
525 si->vtype = VTYPE_PYOBJ;
526 ASM_MOV_IMM_TO_LOCAL_USING((si->u_imm << 1) | 1, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
527 break;
528 default:
529 // not handled
530 assert(0);
531 }
Damieneb19efb2013-10-10 22:06:54 +0100532 }
533 assert(si->kind == STACK_VALUE);
534 assert(si->vtype == VTYPE_PYOBJ);
Damienff8ed772013-10-08 22:18:32 +0100535 }
Damien13ed3a62013-10-08 09:05:10 +0100536 adjust_stack(emit, -n_pop);
Damien Georgecd82e022014-02-02 13:11:48 +0000537 ASM_MOV_LOCAL_ADDR_TO_REG(emit->stack_start + emit->stack_size, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100538}
539
540// vtype of all n_push objects is VTYPE_PYOBJ
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200541STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, int reg_dest, int n_push) {
Damieneb19efb2013-10-10 22:06:54 +0100542 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +0100543 for (int i = 0; i < n_push; i++) {
Damien7f5dacf2013-10-10 11:24:39 +0100544 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
Damienff8ed772013-10-08 22:18:32 +0100545 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
Damien13ed3a62013-10-08 09:05:10 +0100546 }
Damien Georgecd82e022014-02-02 13:11:48 +0000547 ASM_MOV_LOCAL_ADDR_TO_REG(emit->stack_start + emit->stack_size, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100548 adjust_stack(emit, n_push);
549}
550
Damien Georged17926d2014-03-30 13:35:08 +0100551STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind, void *fun) {
Damiend2755ec2013-10-16 23:58:48 +0100552 need_reg_all(emit);
Damien3ef4abb2013-10-12 16:53:13 +0100553#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100554 asm_x64_call_ind(emit->as, fun, REG_RAX);
Damien3ef4abb2013-10-12 16:53:13 +0100555#elif N_THUMB
Damien Georged17926d2014-03-30 13:35:08 +0100556 asm_thumb_bl_ind(emit->as, mp_fun_table[fun_kind], fun_kind, REG_R3);
Damien13ed3a62013-10-08 09:05:10 +0100557#endif
558}
559
Damien George40f3c022014-07-03 13:25:24 +0100560STATIC void emit_call_with_imm_arg(emit_t *emit, mp_fun_kind_t fun_kind, void *fun, mp_int_t arg_val, int arg_reg) {
Damieneb19efb2013-10-10 22:06:54 +0100561 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +0100562 ASM_MOV_IMM_TO_REG(arg_val, arg_reg);
Damiend2755ec2013-10-16 23:58:48 +0100563#if N_X64
564 asm_x64_call_ind(emit->as, fun, REG_RAX);
565#elif N_THUMB
Damien Georged17926d2014-03-30 13:35:08 +0100566 asm_thumb_bl_ind(emit->as, mp_fun_table[fun_kind], fun_kind, REG_R3);
Damiend2755ec2013-10-16 23:58:48 +0100567#endif
Damien13ed3a62013-10-08 09:05:10 +0100568}
569
Damien George40f3c022014-07-03 13:25:24 +0100570// the first arg is stored in the code aligned on a mp_uint_t boundary
571STATIC void emit_call_with_imm_arg_aligned(emit_t *emit, mp_fun_kind_t fun_kind, void *fun, mp_int_t arg_val, int arg_reg) {
Damien Georgea32c1e42014-05-07 18:30:52 +0100572 need_reg_all(emit);
573 ASM_MOV_ALIGNED_IMM_TO_REG(arg_val, arg_reg);
574#if N_X64
575 asm_x64_call_ind(emit->as, fun, REG_RAX);
576#elif N_THUMB
577 asm_thumb_bl_ind(emit->as, mp_fun_table[fun_kind], fun_kind, REG_R3);
578#endif
579}
580
Damien George40f3c022014-07-03 13:25:24 +0100581STATIC void emit_call_with_2_imm_args(emit_t *emit, mp_fun_kind_t fun_kind, void *fun, mp_int_t arg_val1, int arg_reg1, mp_int_t arg_val2, int arg_reg2) {
Damien Georgecd82e022014-02-02 13:11:48 +0000582 need_reg_all(emit);
583 ASM_MOV_IMM_TO_REG(arg_val1, arg_reg1);
584 ASM_MOV_IMM_TO_REG(arg_val2, arg_reg2);
585#if N_X64
586 asm_x64_call_ind(emit->as, fun, REG_RAX);
587#elif N_THUMB
Damien Georged17926d2014-03-30 13:35:08 +0100588 asm_thumb_bl_ind(emit->as, mp_fun_table[fun_kind], fun_kind, REG_R3);
Damien Georgecd82e022014-02-02 13:11:48 +0000589#endif
590}
591
Damien George40f3c022014-07-03 13:25:24 +0100592// the first arg is stored in the code aligned on a mp_uint_t boundary
593STATIC void emit_call_with_3_imm_args_and_first_aligned(emit_t *emit, mp_fun_kind_t fun_kind, void *fun, 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 +0100594 need_reg_all(emit);
Damien George36db6bc2014-05-07 17:24:22 +0100595 ASM_MOV_ALIGNED_IMM_TO_REG(arg_val1, arg_reg1);
Damien Georgecdd96df2014-04-06 12:58:40 +0100596 ASM_MOV_IMM_TO_REG(arg_val2, arg_reg2);
597 ASM_MOV_IMM_TO_REG(arg_val3, arg_reg3);
598#if N_X64
599 asm_x64_call_ind(emit->as, fun, REG_RAX);
600#elif N_THUMB
601 asm_thumb_bl_ind(emit->as, mp_fun_table[fun_kind], fun_kind, REG_R3);
602#endif
603}
604
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200605STATIC void emit_native_load_id(emit_t *emit, qstr qstr) {
Damien13ed3a62013-10-08 09:05:10 +0100606 // check for built-ins
607 if (strcmp(qstr_str(qstr), "v_int") == 0) {
Damienff8ed772013-10-08 22:18:32 +0100608 assert(0);
Damien Georgece8f07a2014-03-27 23:30:26 +0000609 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +0100610 //emit_post_push_blank(emit, VTYPE_BUILTIN_V_INT);
611
612 // not a built-in, so do usual thing
613 } else {
614 emit_common_load_id(emit, &EXPORT_FUN(method_table), emit->scope, qstr);
615 }
616}
617
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200618STATIC void emit_native_store_id(emit_t *emit, qstr qstr) {
Damien13ed3a62013-10-08 09:05:10 +0100619 // TODO check for built-ins and disallow
620 emit_common_store_id(emit, &EXPORT_FUN(method_table), emit->scope, qstr);
621}
622
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200623STATIC void emit_native_delete_id(emit_t *emit, qstr qstr) {
Damien13ed3a62013-10-08 09:05:10 +0100624 // TODO check for built-ins and disallow
625 emit_common_delete_id(emit, &EXPORT_FUN(method_table), emit->scope, qstr);
626}
627
Damien George6f355fd2014-04-10 14:11:31 +0100628STATIC void emit_native_label_assign(emit_t *emit, uint l) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000629 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +0100630 // need to commit stack because we can jump here from elsewhere
631 need_stack_settled(emit);
Damien3ef4abb2013-10-12 16:53:13 +0100632#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100633 asm_x64_label_assign(emit->as, l);
Damien3ef4abb2013-10-12 16:53:13 +0100634#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100635 asm_thumb_label_assign(emit->as, l);
636#endif
Damien6ba13142013-11-02 20:34:54 +0000637 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +0100638}
639
Damien Georgecdd96df2014-04-06 12:58:40 +0100640STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
641 DEBUG_printf("import_name %s\n", qstr_str(qst));
642 vtype_kind_t vtype_fromlist;
643 vtype_kind_t vtype_level;
644 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
645 assert(vtype_fromlist == VTYPE_PYOBJ);
646 assert(vtype_level == VTYPE_PYOBJ);
647 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, mp_import_name, qst, REG_ARG_1); // arg1 = import name
648 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +0100649}
650
Damien Georgecdd96df2014-04-06 12:58:40 +0100651STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
652 DEBUG_printf("import_from %s\n", qstr_str(qst));
653 emit_native_pre(emit);
654 vtype_kind_t vtype_module;
655 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
656 assert(vtype_module == VTYPE_PYOBJ);
657 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, mp_import_from, qst, REG_ARG_2); // arg2 = import name
658 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +0100659}
660
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200661STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +0100662 DEBUG_printf("import_star\n");
663 vtype_kind_t vtype_module;
664 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
665 assert(vtype_module == VTYPE_PYOBJ);
666 emit_call(emit, MP_F_IMPORT_ALL, mp_import_all);
667 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +0100668}
669
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200670STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georgecdd96df2014-04-06 12:58:40 +0100671 DEBUG_printf("load_const_tok %d\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +0000672 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +0100673 int vtype;
Damien George40f3c022014-07-03 13:25:24 +0100674 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +0100675 if (emit->do_viper_types) {
676 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +0000677 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
678 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
679 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien13ed3a62013-10-08 09:05:10 +0100680 default: assert(0); vtype = 0; val = 0; // shouldn't happen
681 }
682 } else {
683 vtype = VTYPE_PYOBJ;
684 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +0100685 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
686 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
687 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien13ed3a62013-10-08 09:05:10 +0100688 default: assert(0); vtype = 0; val = 0; // shouldn't happen
689 }
690 }
691 emit_post_push_imm(emit, vtype, val);
692}
693
Damien George40f3c022014-07-03 13:25:24 +0100694STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georgecdd96df2014-04-06 12:58:40 +0100695 DEBUG_printf("load_const_small_int %d\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +0000696 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +0100697 if (emit->do_viper_types) {
698 emit_post_push_imm(emit, VTYPE_INT, arg);
699 } else {
700 emit_post_push_imm(emit, VTYPE_PYOBJ, (arg << 1) | 1);
701 }
702}
703
Damien Georgecdd96df2014-04-06 12:58:40 +0100704STATIC void emit_native_load_const_int(emit_t *emit, qstr qst) {
705 DEBUG_printf("load_const_int %s\n", qstr_str(st));
706 // for viper: load integer, check fits in 32 bits
707 emit_native_pre(emit);
Damien George503d6112014-05-28 14:07:21 +0100708 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_INT, mp_load_const_int, qst, REG_ARG_1);
Damien Georgecdd96df2014-04-06 12:58:40 +0100709 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +0100710}
711
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200712STATIC void emit_native_load_const_dec(emit_t *emit, qstr qstr) {
Damien6ba13142013-11-02 20:34:54 +0000713 // for viper, a float/complex is just a Python object
Damien Georgece8f07a2014-03-27 23:30:26 +0000714 emit_native_pre(emit);
Damien Georged17926d2014-03-30 13:35:08 +0100715 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_DEC, mp_load_const_dec, qstr, REG_ARG_1);
Damien6ba13142013-11-02 20:34:54 +0000716 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +0100717}
718
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200719STATIC void emit_native_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000720 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +0100721 if (emit->do_viper_types) {
722 // not implemented properly
723 // load a pointer to the asciiz string?
724 assert(0);
Damien George40f3c022014-07-03 13:25:24 +0100725 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qstr));
Damien13ed3a62013-10-08 09:05:10 +0100726 } else {
Damien Georgeb601d952014-06-30 05:17:25 +0100727 if (bytes) {
728 emit_call_with_imm_arg(emit, 0, mp_load_const_bytes, qstr, REG_ARG_1); // TODO need to add function to runtime table
729 } else {
730 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, mp_load_const_str, qstr, REG_ARG_1);
731 }
Damien13ed3a62013-10-08 09:05:10 +0100732 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
733 }
734}
735
Damien George3558f622014-04-20 17:50:40 +0100736STATIC void emit_native_load_null(emit_t *emit) {
737 emit_native_pre(emit);
738 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
739}
740
Damien George2bf7c092014-04-09 15:26:46 +0100741STATIC void emit_native_load_fast(emit_t *emit, qstr qstr, uint id_flags, int local_num) {
Damien13ed3a62013-10-08 09:05:10 +0100742 vtype_kind_t vtype = emit->local_vtype[local_num];
743 if (vtype == VTYPE_UNBOUND) {
744 printf("ViperTypeError: local %s used before type known\n", qstr_str(qstr));
745 }
Damien Georgece8f07a2014-03-27 23:30:26 +0000746 emit_native_pre(emit);
Damien3ef4abb2013-10-12 16:53:13 +0100747#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100748 if (local_num == 0) {
749 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
750 } else {
Damiend2755ec2013-10-16 23:58:48 +0100751 need_reg_single(emit, REG_RAX, 0);
Damien Georged509ac22014-05-07 23:27:45 +0100752 asm_x64_mov_local_to_r64(emit->as, local_num - REG_LOCAL_NUM, REG_RAX);
Damien13ed3a62013-10-08 09:05:10 +0100753 emit_post_push_reg(emit, vtype, REG_RAX);
754 }
Damien3ef4abb2013-10-12 16:53:13 +0100755#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100756 if (local_num == 0) {
757 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
758 } else if (local_num == 1) {
759 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
760 } else if (local_num == 2) {
761 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
762 } else {
Damiend2755ec2013-10-16 23:58:48 +0100763 need_reg_single(emit, REG_R0, 0);
Damien Georged509ac22014-05-07 23:27:45 +0100764 asm_thumb_mov_reg_local(emit->as, REG_R0, local_num - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +0100765 emit_post_push_reg(emit, vtype, REG_R0);
766 }
767#endif
768}
769
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200770STATIC void emit_native_load_deref(emit_t *emit, qstr qstr, int local_num) {
Damien9ecbcff2013-12-11 00:41:43 +0000771 // not implemented
772 // in principle could support this quite easily (ldr r0, [r0, #0]) and then get closed over variables!
773 assert(0);
774}
775
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200776STATIC void emit_native_load_name(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000777 emit_native_pre(emit);
Damien Georged17926d2014-03-30 13:35:08 +0100778 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, mp_load_name, qstr, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100779 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
780}
781
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200782STATIC void emit_native_load_global(emit_t *emit, qstr qstr) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000783 emit_native_pre(emit);
Damien Georged17926d2014-03-30 13:35:08 +0100784 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, mp_load_global, qstr, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100785 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
786}
787
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200788STATIC void emit_native_load_attr(emit_t *emit, qstr qstr) {
Damien13ed3a62013-10-08 09:05:10 +0100789 // depends on type of subject:
790 // - integer, function, pointer to integers: error
791 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +0100792 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +0100793 vtype_kind_t vtype_base;
794 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
795 assert(vtype_base == VTYPE_PYOBJ);
Damien Georged17926d2014-03-30 13:35:08 +0100796 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, mp_load_attr, qstr, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +0100797 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
798}
799
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200800STATIC void emit_native_load_method(emit_t *emit, qstr qstr) {
Damien13ed3a62013-10-08 09:05:10 +0100801 vtype_kind_t vtype_base;
802 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
803 assert(vtype_base == VTYPE_PYOBJ);
804 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien Georged17926d2014-03-30 13:35:08 +0100805 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, mp_load_method, qstr, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +0100806}
807
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200808STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000809 emit_native_pre(emit);
Damien Georged17926d2014-03-30 13:35:08 +0100810 emit_call(emit, MP_F_LOAD_BUILD_CLASS, mp_load_build_class);
Damien7f5dacf2013-10-10 11:24:39 +0100811 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +0100812}
813
Damien George729f7b42014-04-17 22:10:53 +0100814STATIC void emit_native_load_subscr(emit_t *emit) {
815 vtype_kind_t vtype_lhs, vtype_rhs;
816 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_2, &vtype_lhs, REG_ARG_1);
817 if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George40f3c022014-07-03 13:25:24 +0100818 emit_call_with_imm_arg(emit, MP_F_OBJ_SUBSCR, mp_obj_subscr, (mp_uint_t)MP_OBJ_SENTINEL, REG_ARG_3);
Damien George729f7b42014-04-17 22:10:53 +0100819 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
820 } else {
821 printf("ViperTypeError: can't do subscr of types %d and %d\n", vtype_lhs, vtype_rhs);
822 assert(0);
823 }
824}
825
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200826STATIC void emit_native_store_fast(emit_t *emit, qstr qstr, int local_num) {
Damien13ed3a62013-10-08 09:05:10 +0100827 vtype_kind_t vtype;
Damien3ef4abb2013-10-12 16:53:13 +0100828#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100829 if (local_num == 0) {
830 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
831 } else {
832 emit_pre_pop_reg(emit, &vtype, REG_RAX);
Damien Georged509ac22014-05-07 23:27:45 +0100833 asm_x64_mov_r64_to_local(emit->as, REG_RAX, local_num - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +0100834 }
Damien3ef4abb2013-10-12 16:53:13 +0100835#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100836 if (local_num == 0) {
837 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
838 } else if (local_num == 1) {
839 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
840 } else if (local_num == 2) {
841 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
842 } else {
843 emit_pre_pop_reg(emit, &vtype, REG_R0);
Damien Georged509ac22014-05-07 23:27:45 +0100844 asm_thumb_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_R0);
Damien13ed3a62013-10-08 09:05:10 +0100845 }
846#endif
847
848 emit_post(emit);
849
850 // check types
851 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
852 // first time this local is assigned, so give it a type of the object stored in it
853 emit->local_vtype[local_num] = vtype;
854 } else if (emit->local_vtype[local_num] != vtype) {
855 // type of local is not the same as object stored in it
856 printf("ViperTypeError: type mismatch, local %s has type %d but source object has type %d\n", qstr_str(qstr), emit->local_vtype[local_num], vtype);
857 }
858}
859
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200860STATIC void emit_native_store_deref(emit_t *emit, qstr qstr, int local_num) {
Damien9ecbcff2013-12-11 00:41:43 +0000861 // not implemented
862 assert(0);
863}
864
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200865STATIC void emit_native_store_name(emit_t *emit, qstr qstr) {
Damien Georged17926d2014-03-30 13:35:08 +0100866 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +0100867 vtype_kind_t vtype;
868 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
869 assert(vtype == VTYPE_PYOBJ);
Damien Georged17926d2014-03-30 13:35:08 +0100870 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, mp_store_name, qstr, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +0100871 emit_post(emit);
872}
873
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200874STATIC void emit_native_store_global(emit_t *emit, qstr qstr) {
Damien13ed3a62013-10-08 09:05:10 +0100875 // not implemented
876 assert(0);
877}
878
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200879STATIC void emit_native_store_attr(emit_t *emit, qstr qstr) {
Damien7f5dacf2013-10-10 11:24:39 +0100880 vtype_kind_t vtype_base, vtype_val;
881 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
882 assert(vtype_base == VTYPE_PYOBJ);
883 assert(vtype_val == VTYPE_PYOBJ);
Damien Georged17926d2014-03-30 13:35:08 +0100884 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, mp_store_attr, qstr, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +0100885 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +0100886}
887
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200888STATIC void emit_native_store_subscr(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100889 // depends on type of subject:
890 // - integer, function, pointer to structure: error
891 // - pointer to integers: store as per array
892 // - Python object: call runtime with converted object or type info
893 vtype_kind_t vtype_index, vtype_base, vtype_value;
894 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
895 assert(vtype_index == VTYPE_PYOBJ);
896 assert(vtype_base == VTYPE_PYOBJ);
897 assert(vtype_value == VTYPE_PYOBJ);
Damien George729f7b42014-04-17 22:10:53 +0100898 emit_call(emit, MP_F_OBJ_SUBSCR, mp_obj_subscr);
Damien13ed3a62013-10-08 09:05:10 +0100899}
900
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200901STATIC void emit_native_delete_fast(emit_t *emit, qstr qstr, int local_num) {
Damien13ed3a62013-10-08 09:05:10 +0100902 // not implemented
903 // could support for Python types, just set to None (so GC can reclaim it)
904 assert(0);
905}
906
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200907STATIC void emit_native_delete_deref(emit_t *emit, qstr qstr, int local_num) {
Damien9ecbcff2013-12-11 00:41:43 +0000908 // not supported
909 assert(0);
910}
911
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200912STATIC void emit_native_delete_name(emit_t *emit, qstr qstr) {
Damien13ed3a62013-10-08 09:05:10 +0100913 // not implemented
Damien Georged17926d2014-03-30 13:35:08 +0100914 // use mp_delete_name
Damien13ed3a62013-10-08 09:05:10 +0100915 assert(0);
916}
917
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200918STATIC void emit_native_delete_global(emit_t *emit, qstr qstr) {
Damien13ed3a62013-10-08 09:05:10 +0100919 // not implemented
Damien Georged17926d2014-03-30 13:35:08 +0100920 // use mp_delete_global
Damien13ed3a62013-10-08 09:05:10 +0100921 assert(0);
922}
923
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200924STATIC void emit_native_delete_attr(emit_t *emit, qstr qstr) {
Damien George780e54c2014-06-22 18:35:04 +0100925 vtype_kind_t vtype_base;
926 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
927 assert(vtype_base == VTYPE_PYOBJ);
Damien George40f3c022014-07-03 13:25:24 +0100928 emit_call_with_2_imm_args(emit, MP_F_STORE_ATTR, mp_store_attr, qstr, 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 +0100929 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +0100930}
931
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200932STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +0100933 vtype_kind_t vtype_index, vtype_base;
934 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
935 assert(vtype_index == VTYPE_PYOBJ);
936 assert(vtype_base == VTYPE_PYOBJ);
Damien George40f3c022014-07-03 13:25:24 +0100937 emit_call_with_imm_arg(emit, MP_F_OBJ_SUBSCR, mp_obj_subscr, (mp_uint_t)MP_OBJ_NULL, REG_ARG_3);
Damien13ed3a62013-10-08 09:05:10 +0100938}
939
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200940STATIC void emit_native_dup_top(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100941 vtype_kind_t vtype;
942 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
943 emit_post_push_reg_reg(emit, vtype, REG_TEMP0, vtype, REG_TEMP0);
944}
945
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200946STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100947 vtype_kind_t vtype0, vtype1;
948 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
949 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
950}
951
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200952STATIC void emit_native_pop_top(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100953 vtype_kind_t vtype;
Damien Georgeccc85ea2014-05-10 13:40:46 +0100954 emit_pre_pop_discard(emit, &vtype);
Damien13ed3a62013-10-08 09:05:10 +0100955 emit_post(emit);
956}
957
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200958STATIC void emit_native_rot_two(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100959 vtype_kind_t vtype0, vtype1;
960 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
961 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +0100962}
963
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200964STATIC void emit_native_rot_three(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100965 vtype_kind_t vtype0, vtype1, vtype2;
966 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
967 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
968}
969
Damien George6f355fd2014-04-10 14:11:31 +0100970STATIC void emit_native_jump(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +0000971 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +0100972 // need to commit stack because we are jumping elsewhere
973 need_stack_settled(emit);
Damien3ef4abb2013-10-12 16:53:13 +0100974#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100975 asm_x64_jmp_label(emit->as, label);
Damien3ef4abb2013-10-12 16:53:13 +0100976#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100977 asm_thumb_b_label(emit->as, label);
978#endif
979 emit_post(emit);
980}
981
Damien Georgea32c1e42014-05-07 18:30:52 +0100982STATIC void emit_native_jump_helper(emit_t *emit, uint label, bool pop) {
Damien13ed3a62013-10-08 09:05:10 +0100983 vtype_kind_t vtype = peek_vtype(emit);
984 if (vtype == VTYPE_BOOL) {
985 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien Georgea32c1e42014-05-07 18:30:52 +0100986 if (!pop) {
987 adjust_stack(emit, 1);
988 }
Damien13ed3a62013-10-08 09:05:10 +0100989 } else if (vtype == VTYPE_PYOBJ) {
990 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien Georged17926d2014-03-30 13:35:08 +0100991 emit_call(emit, MP_F_OBJ_IS_TRUE, mp_obj_is_true);
Damien Georgea32c1e42014-05-07 18:30:52 +0100992 if (!pop) {
993 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
994 }
Damien13ed3a62013-10-08 09:05:10 +0100995 } else {
996 printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
997 assert(0);
998 }
Damien Georgea32c1e42014-05-07 18:30:52 +0100999 // need to commit stack because we may jump elsewhere
1000 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001001}
1002
Damien George6f355fd2014-04-10 14:11:31 +01001003STATIC void emit_native_pop_jump_if_true(emit_t *emit, uint label) {
Damien Georgea32c1e42014-05-07 18:30:52 +01001004 emit_native_jump_helper(emit, label, true);
Damien1a6633a2013-11-03 13:58:19 +00001005#if N_X64
1006 asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
1007 asm_x64_jcc_label(emit->as, JCC_JNZ, label);
1008#elif N_THUMB
1009 asm_thumb_cmp_rlo_i8(emit->as, REG_RET, 0);
1010 asm_thumb_bcc_label(emit->as, THUMB_CC_NE, label);
1011#endif
1012 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001013}
Damien1a6633a2013-11-03 13:58:19 +00001014
Damien Georgea32c1e42014-05-07 18:30:52 +01001015STATIC void emit_native_pop_jump_if_false(emit_t *emit, uint label) {
1016 emit_native_jump_helper(emit, label, true);
1017#if N_X64
1018 asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
1019 asm_x64_jcc_label(emit->as, JCC_JZ, label);
1020#elif N_THUMB
1021 asm_thumb_cmp_rlo_i8(emit->as, REG_RET, 0);
1022 asm_thumb_bcc_label(emit->as, THUMB_CC_EQ, label);
1023#endif
1024 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001025}
Damien Georgea32c1e42014-05-07 18:30:52 +01001026
1027STATIC void emit_native_jump_if_true_or_pop(emit_t *emit, uint label) {
1028 emit_native_jump_helper(emit, label, false);
1029#if N_X64
1030 asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
1031 asm_x64_jcc_label(emit->as, JCC_JNZ, label);
1032#elif N_THUMB
1033 asm_thumb_cmp_rlo_i8(emit->as, REG_RET, 0);
1034 asm_thumb_bcc_label(emit->as, THUMB_CC_NE, label);
1035#endif
1036 adjust_stack(emit, -1);
1037 emit_post(emit);
1038}
1039
Damien George6f355fd2014-04-10 14:11:31 +01001040STATIC void emit_native_jump_if_false_or_pop(emit_t *emit, uint label) {
Damien Georgea32c1e42014-05-07 18:30:52 +01001041 emit_native_jump_helper(emit, label, false);
1042#if N_X64
1043 asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
1044 asm_x64_jcc_label(emit->as, JCC_JZ, label);
1045#elif N_THUMB
1046 asm_thumb_cmp_rlo_i8(emit->as, REG_RET, 0);
1047 asm_thumb_bcc_label(emit->as, THUMB_CC_EQ, label);
1048#endif
1049 adjust_stack(emit, -1);
1050 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001051}
1052
Damien George6f355fd2014-04-10 14:11:31 +01001053STATIC void emit_native_break_loop(emit_t *emit, uint label, int except_depth) {
Damien George25c84642014-05-30 15:20:41 +01001054 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001055}
Damien Georgea32c1e42014-05-07 18:30:52 +01001056
Damien George6f355fd2014-04-10 14:11:31 +01001057STATIC void emit_native_continue_loop(emit_t *emit, uint label, int except_depth) {
Damien Georgea32c1e42014-05-07 18:30:52 +01001058 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001059}
Damien Georgea32c1e42014-05-07 18:30:52 +01001060
Damien George6f355fd2014-04-10 14:11:31 +01001061STATIC void emit_native_setup_with(emit_t *emit, uint label) {
Damien13ed3a62013-10-08 09:05:10 +01001062 // not supported, or could be with runtime call
1063 assert(0);
1064}
Damien Georgeb601d952014-06-30 05:17:25 +01001065
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001066STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001067 assert(0);
1068}
Damien Georgeb601d952014-06-30 05:17:25 +01001069
Damien George6f355fd2014-04-10 14:11:31 +01001070STATIC void emit_native_setup_except(emit_t *emit, uint label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001071 emit_native_pre(emit);
1072 // need to commit stack because we may jump elsewhere
1073 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001074 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 Georgeb601d952014-06-30 05:17:25 +01001075 emit_call(emit, 0, nlr_push); // TODO need to add function to runtime table
1076#if N_X64
1077 asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
1078 asm_x64_jcc_label(emit->as, JCC_JNZ, label);
1079#elif N_THUMB
1080 asm_thumb_cmp_rlo_i8(emit->as, REG_RET, 0);
1081 asm_thumb_bcc_label(emit->as, THUMB_CC_NE, label);
1082#endif
1083 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001084}
Damien Georgeb601d952014-06-30 05:17:25 +01001085
Damien George6f355fd2014-04-10 14:11:31 +01001086STATIC void emit_native_setup_finally(emit_t *emit, uint label) {
Damien13ed3a62013-10-08 09:05:10 +01001087 assert(0);
1088}
Damien Georgeb601d952014-06-30 05:17:25 +01001089
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001090STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgeb601d952014-06-30 05:17:25 +01001091 //assert(0);
Damien13ed3a62013-10-08 09:05:10 +01001092}
Damiend2755ec2013-10-16 23:58:48 +01001093
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001094STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001095 // perhaps the difficult one, as we want to rewrite for loops using native code
1096 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001097
1098 vtype_kind_t vtype;
1099 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1100 assert(vtype == VTYPE_PYOBJ);
Damien Georged17926d2014-03-30 13:35:08 +01001101 emit_call(emit, MP_F_GETITER, mp_getiter);
Damiend2755ec2013-10-16 23:58:48 +01001102 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001103}
Damiend2755ec2013-10-16 23:58:48 +01001104
Damien George6f355fd2014-04-10 14:11:31 +01001105STATIC void emit_native_for_iter(emit_t *emit, uint label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001106 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001107 vtype_kind_t vtype;
1108 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1109 assert(vtype == VTYPE_PYOBJ);
Damien Georged17926d2014-03-30 13:35:08 +01001110 emit_call(emit, MP_F_ITERNEXT, mp_iternext);
Damien George40f3c022014-07-03 13:25:24 +01001111 ASM_MOV_IMM_TO_REG((mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
Damiend2755ec2013-10-16 23:58:48 +01001112#if N_X64
1113 asm_x64_cmp_r64_with_r64(emit->as, REG_RET, REG_TEMP1);
1114 asm_x64_jcc_label(emit->as, JCC_JE, label);
1115#elif N_THUMB
Damien George87210872014-04-13 00:30:32 +01001116 asm_thumb_cmp_rlo_rlo(emit->as, REG_RET, REG_TEMP1);
Damien9b9e9962013-11-03 14:25:43 +00001117 asm_thumb_bcc_label(emit->as, THUMB_CC_EQ, label);
Damiend2755ec2013-10-16 23:58:48 +01001118#endif
1119 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1120}
1121
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001122STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001123 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001124 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001125 adjust_stack(emit, -1);
1126 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001127}
1128
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001129STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001130 emit_native_pre(emit);
Damien Georgeb601d952014-06-30 05:17:25 +01001131 emit_call(emit, 0, nlr_pop); // TODO need to add function to runtime table
Damien George40f3c022014-07-03 13:25:24 +01001132 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001133 emit_post(emit);
1134}
1135
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001136STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeb601d952014-06-30 05:17:25 +01001137 /*
1138 emit_native_pre(emit);
1139 emit_call(emit, 0, nlr_pop); // TODO need to add function to runtime table
Damien George40f3c022014-07-03 13:25:24 +01001140 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01001141 emit_post(emit);
1142 */
Damien13ed3a62013-10-08 09:05:10 +01001143}
1144
Damien Georged17926d2014-03-30 13:35:08 +01001145STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georgeb601d952014-06-30 05:17:25 +01001146 if (op == MP_UNARY_OP_NOT) {
1147 // we need to synthesise this operation
1148 assert(0);
1149 } else {
1150 vtype_kind_t vtype;
1151 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1152 assert(vtype == VTYPE_PYOBJ);
1153 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, mp_unary_op, op, REG_ARG_1);
1154 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1155 }
Damien13ed3a62013-10-08 09:05:10 +01001156}
1157
Damien Georged17926d2014-03-30 13:35:08 +01001158STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien13ed3a62013-10-08 09:05:10 +01001159 vtype_kind_t vtype_lhs, vtype_rhs;
1160 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
1161 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien Georged17926d2014-03-30 13:35:08 +01001162 if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
Damien3ef4abb2013-10-12 16:53:13 +01001163#if N_X64
Damien Georgebc1d3692014-01-11 09:47:06 +00001164 asm_x64_add_r64_to_r64(emit->as, REG_ARG_3, REG_ARG_2);
Damien3ef4abb2013-10-12 16:53:13 +01001165#elif N_THUMB
Damien Georgea26dc502014-04-12 17:54:52 +01001166 asm_thumb_add_rlo_rlo_rlo(emit->as, REG_ARG_2, REG_ARG_2, REG_ARG_3);
Damien13ed3a62013-10-08 09:05:10 +01001167#endif
Damien Georgebc1d3692014-01-11 09:47:06 +00001168 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien Georged17926d2014-03-30 13:35:08 +01001169 } else if (op == MP_BINARY_OP_LESS) {
Damien Georgebc1d3692014-01-11 09:47:06 +00001170#if N_X64
1171 asm_x64_xor_r64_to_r64(emit->as, REG_RET, REG_RET);
1172 asm_x64_cmp_r64_with_r64(emit->as, REG_ARG_3, REG_ARG_2);
1173 asm_x64_setcc_r8(emit->as, JCC_JL, REG_RET);
1174#elif N_THUMB
Damien George87210872014-04-13 00:30:32 +01001175 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, REG_ARG_3);
Damien Georgebc1d3692014-01-11 09:47:06 +00001176 asm_thumb_ite_ge(emit->as);
Damien George87210872014-04-13 00:30:32 +01001177 asm_thumb_mov_rlo_i8(emit->as, REG_RET, 0); // if r0 >= r1
1178 asm_thumb_mov_rlo_i8(emit->as, REG_RET, 1); // if r0 < r1
Damien Georgebc1d3692014-01-11 09:47:06 +00001179#endif
1180 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
1181 } else {
1182 // TODO other ops not yet implemented
1183 assert(0);
1184 }
Damien13ed3a62013-10-08 09:05:10 +01001185 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien Georged17926d2014-03-30 13:35:08 +01001186 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, mp_binary_op, op, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001187 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1188 } else {
1189 printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs);
1190 assert(0);
1191 }
1192}
1193
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001194STATIC void emit_native_build_tuple(emit_t *emit, int n_args) {
Damiend2755ec2013-10-16 23:58:48 +01001195 // for viper: call runtime, with types of args
1196 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00001197 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001198 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George15d18062014-03-31 16:28:13 +01001199 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, mp_obj_new_tuple, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01001200 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01001201}
1202
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001203STATIC void emit_native_build_list(emit_t *emit, int n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001204 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001205 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George15d18062014-03-31 16:28:13 +01001206 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, mp_obj_new_list, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001207 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
1208}
1209
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001210STATIC void emit_native_list_append(emit_t *emit, int list_index) {
Damiend2755ec2013-10-16 23:58:48 +01001211 // only used in list comprehension
1212 vtype_kind_t vtype_list, vtype_item;
1213 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
1214 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
1215 assert(vtype_list == VTYPE_PYOBJ);
1216 assert(vtype_item == VTYPE_PYOBJ);
Damien George15d18062014-03-31 16:28:13 +01001217 emit_call(emit, MP_F_LIST_APPEND, mp_obj_list_append);
Damiend2755ec2013-10-16 23:58:48 +01001218 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001219}
1220
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001221STATIC void emit_native_build_map(emit_t *emit, int n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001222 emit_native_pre(emit);
Damien George15d18062014-03-31 16:28:13 +01001223 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, mp_obj_new_dict, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001224 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
1225}
1226
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001227STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001228 vtype_kind_t vtype_key, vtype_value, vtype_map;
1229 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
1230 assert(vtype_key == VTYPE_PYOBJ);
1231 assert(vtype_value == VTYPE_PYOBJ);
1232 assert(vtype_map == VTYPE_PYOBJ);
Damien George15d18062014-03-31 16:28:13 +01001233 emit_call(emit, MP_F_STORE_MAP, mp_obj_dict_store);
Damien13ed3a62013-10-08 09:05:10 +01001234 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
1235}
1236
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001237STATIC void emit_native_map_add(emit_t *emit, int map_index) {
Damiend2755ec2013-10-16 23:58:48 +01001238 // only used in list comprehension
1239 vtype_kind_t vtype_map, vtype_key, vtype_value;
1240 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
1241 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
1242 assert(vtype_map == VTYPE_PYOBJ);
1243 assert(vtype_key == VTYPE_PYOBJ);
1244 assert(vtype_value == VTYPE_PYOBJ);
Damien George15d18062014-03-31 16:28:13 +01001245 emit_call(emit, MP_F_STORE_MAP, mp_obj_dict_store);
Damiend2755ec2013-10-16 23:58:48 +01001246 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001247}
1248
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001249STATIC void emit_native_build_set(emit_t *emit, int n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001250 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001251 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George15d18062014-03-31 16:28:13 +01001252 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, mp_obj_new_set, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001253 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
1254}
1255
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001256STATIC void emit_native_set_add(emit_t *emit, int set_index) {
Damiend2755ec2013-10-16 23:58:48 +01001257 // only used in set comprehension
1258 vtype_kind_t vtype_set, vtype_item;
1259 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
1260 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
1261 assert(vtype_set == VTYPE_PYOBJ);
1262 assert(vtype_item == VTYPE_PYOBJ);
Damien George15d18062014-03-31 16:28:13 +01001263 emit_call(emit, MP_F_STORE_SET, mp_obj_set_store);
Damiend2755ec2013-10-16 23:58:48 +01001264 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001265}
Damiend2755ec2013-10-16 23:58:48 +01001266
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001267STATIC void emit_native_build_slice(emit_t *emit, int n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001268 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01001269 if (n_args == 2) {
1270 vtype_kind_t vtype_start, vtype_stop;
1271 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
1272 assert(vtype_start == VTYPE_PYOBJ);
1273 assert(vtype_stop == VTYPE_PYOBJ);
Damien George40f3c022014-07-03 13:25:24 +01001274 emit_call_with_imm_arg(emit, MP_F_NEW_SLICE, mp_obj_new_slice, (mp_uint_t)mp_const_none, REG_ARG_3); // arg3 = step
Damien Georgeb601d952014-06-30 05:17:25 +01001275 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1276 } else {
1277 assert(n_args == 3);
1278 vtype_kind_t vtype_start, vtype_stop, vtype_step;
1279 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
1280 assert(vtype_start == VTYPE_PYOBJ);
1281 assert(vtype_stop == VTYPE_PYOBJ);
1282 assert(vtype_step == VTYPE_PYOBJ);
1283 emit_call(emit, MP_F_NEW_SLICE, mp_obj_new_slice);
1284 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1285 }
Damien13ed3a62013-10-08 09:05:10 +01001286}
Damien Georgecdd96df2014-04-06 12:58:40 +01001287
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001288STATIC void emit_native_unpack_sequence(emit_t *emit, int n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001289 DEBUG_printf("unpack_sequence %d\n", n_args);
1290 vtype_kind_t vtype_base;
1291 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
1292 assert(vtype_base == VTYPE_PYOBJ);
1293 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
1294 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, mp_unpack_sequence, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01001295}
Damien Georgecdd96df2014-04-06 12:58:40 +01001296
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001297STATIC void emit_native_unpack_ex(emit_t *emit, int n_left, int n_right) {
Damien Georgea32c1e42014-05-07 18:30:52 +01001298 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
1299 vtype_kind_t vtype_base;
1300 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
1301 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001302 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_left + n_right + 1); // arg3 = dest ptr
1303 emit_call_with_imm_arg(emit, MP_F_UNPACK_EX, mp_unpack_ex, n_left | (n_right << 8), REG_ARG_2); // arg2 = n_left + n_right
Damien13ed3a62013-10-08 09:05:10 +01001304}
1305
Damien George30565092014-03-31 11:30:17 +01001306STATIC void emit_native_make_function(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults) {
Damien13ed3a62013-10-08 09:05:10 +01001307 // 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 +00001308 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001309 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George40f3c022014-07-03 13:25:24 +01001310 emit_call_with_3_imm_args_and_first_aligned(emit, MP_F_MAKE_FUNCTION_FROM_RAW_CODE, mp_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 +01001311 } else {
1312 vtype_kind_t vtype_def_tuple, vtype_def_dict;
1313 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
1314 assert(vtype_def_tuple == VTYPE_PYOBJ);
1315 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George40f3c022014-07-03 13:25:24 +01001316 emit_call_with_imm_arg_aligned(emit, MP_F_MAKE_FUNCTION_FROM_RAW_CODE, mp_make_function_from_raw_code, (mp_uint_t)scope->raw_code, REG_ARG_1);
Damien Georgea32c1e42014-05-07 18:30:52 +01001317 }
Damien13ed3a62013-10-08 09:05:10 +01001318 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1319}
1320
Damien George3558f622014-04-20 17:50:40 +01001321STATIC void emit_native_make_closure(emit_t *emit, scope_t *scope, uint n_closed_over, uint n_pos_defaults, uint n_kw_defaults) {
Damien13ed3a62013-10-08 09:05:10 +01001322 assert(0);
1323}
1324
Damien George922ddd62014-04-09 12:43:17 +01001325STATIC void emit_native_call_function(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
Damien13ed3a62013-10-08 09:05:10 +01001326 // call special viper runtime routine with type info for args, and wanted type info for return
Damien George922ddd62014-04-09 12:43:17 +01001327 assert(!star_flags);
Damien Georgecd82e022014-02-02 13:11:48 +00001328
1329 /* we no longer have these _n specific call_function's
1330 * they anyway push args into an array
1331 * and they would take too much room in the native dispatch table
Damien13ed3a62013-10-08 09:05:10 +01001332 if (n_positional == 0) {
1333 vtype_kind_t vtype_fun;
1334 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
1335 assert(vtype_fun == VTYPE_PYOBJ);
Damien Georged17926d2014-03-30 13:35:08 +01001336 emit_call(emit, MP_F_CALL_FUNCTION_0, mp_call_function_0);
Damien13ed3a62013-10-08 09:05:10 +01001337 } else if (n_positional == 1) {
1338 vtype_kind_t vtype_fun, vtype_arg1;
1339 emit_pre_pop_reg_reg(emit, &vtype_arg1, REG_ARG_2, &vtype_fun, REG_ARG_1); // the single argument, the function
1340 assert(vtype_fun == VTYPE_PYOBJ);
1341 assert(vtype_arg1 == VTYPE_PYOBJ);
Damien Georged17926d2014-03-30 13:35:08 +01001342 emit_call(emit, MP_F_CALL_FUNCTION_1, mp_call_function_1);
Damien13ed3a62013-10-08 09:05:10 +01001343 } else if (n_positional == 2) {
1344 vtype_kind_t vtype_fun, vtype_arg1, vtype_arg2;
1345 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
1346 assert(vtype_fun == VTYPE_PYOBJ);
1347 assert(vtype_arg1 == VTYPE_PYOBJ);
1348 assert(vtype_arg2 == VTYPE_PYOBJ);
Damien Georged17926d2014-03-30 13:35:08 +01001349 emit_call(emit, MP_F_CALL_FUNCTION_2, mp_call_function_2);
Damien13ed3a62013-10-08 09:05:10 +01001350 } else {
Damieneb19efb2013-10-10 22:06:54 +01001351 */
Damien Georgecd82e022014-02-02 13:11:48 +00001352
Damien Georgece8f07a2014-03-27 23:30:26 +00001353 emit_native_pre(emit);
Damien Georgecdd96df2014-04-06 12:58:40 +01001354 if (n_positional != 0 || n_keyword != 0) {
1355 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 +00001356 }
1357 vtype_kind_t vtype_fun;
1358 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
1359 assert(vtype_fun == VTYPE_PYOBJ);
Damien Georgecdd96df2014-04-06 12:58:40 +01001360 emit_call_with_imm_arg(emit, MP_F_CALL_FUNCTION_N_KW_FOR_NATIVE, mp_call_function_n_kw_for_native, n_positional | (n_keyword << 8), REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +01001361 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1362}
1363
Damien George922ddd62014-04-09 12:43:17 +01001364STATIC void emit_native_call_method(emit_t *emit, int n_positional, int n_keyword, uint star_flags) {
1365 assert(!star_flags);
Damien Georgecd82e022014-02-02 13:11:48 +00001366
Damieneb19efb2013-10-10 22:06:54 +01001367 /*
Damien13ed3a62013-10-08 09:05:10 +01001368 if (n_positional == 0) {
1369 vtype_kind_t vtype_meth, vtype_self;
1370 emit_pre_pop_reg_reg(emit, &vtype_self, REG_ARG_2, &vtype_meth, REG_ARG_1); // the self object (or NULL), the method
1371 assert(vtype_meth == VTYPE_PYOBJ);
1372 assert(vtype_self == VTYPE_PYOBJ);
Damien Georged17926d2014-03-30 13:35:08 +01001373 emit_call(emit, MP_F_CALL_METHOD_1, mp_call_method_1);
Damien13ed3a62013-10-08 09:05:10 +01001374 } else if (n_positional == 1) {
1375 vtype_kind_t vtype_meth, vtype_self, vtype_arg1;
1376 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
1377 assert(vtype_meth == VTYPE_PYOBJ);
1378 assert(vtype_self == VTYPE_PYOBJ);
1379 assert(vtype_arg1 == VTYPE_PYOBJ);
Damien Georged17926d2014-03-30 13:35:08 +01001380 emit_call(emit, MP_F_CALL_METHOD_2, mp_call_method_2);
Damien13ed3a62013-10-08 09:05:10 +01001381 } else {
Damieneb19efb2013-10-10 22:06:54 +01001382 */
Damien Georgecd82e022014-02-02 13:11:48 +00001383
Damien Georgece8f07a2014-03-27 23:30:26 +00001384 emit_native_pre(emit);
Damien Georgecdd96df2014-04-06 12:58:40 +01001385 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 Georged17926d2014-03-30 13:35:08 +01001386 emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, mp_call_method_n_kw, n_positional, REG_ARG_1, n_keyword, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +01001387 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1388}
1389
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001390STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001391 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01001392 // easy. since we don't know who we return to, just return the raw value.
1393 // runtime needs then to know our type signature, but I think that's possible.
1394 vtype_kind_t vtype;
1395 emit_pre_pop_reg(emit, &vtype, REG_RET);
1396 if (emit->do_viper_types) {
1397 assert(vtype == VTYPE_PTR_NONE);
1398 } else {
1399 assert(vtype == VTYPE_PYOBJ);
1400 }
1401 emit->last_emit_was_return_value = true;
Damien3ef4abb2013-10-12 16:53:13 +01001402#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001403 //asm_x64_call_ind(emit->as, 0, REG_RAX); to seg fault for debugging with gdb
1404 asm_x64_exit(emit->as);
Damien3ef4abb2013-10-12 16:53:13 +01001405#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001406 //asm_thumb_call_ind(emit->as, 0, REG_R0); to seg fault for debugging with gdb
1407 asm_thumb_exit(emit->as);
1408#endif
1409}
1410
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001411STATIC void emit_native_raise_varargs(emit_t *emit, int n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01001412 assert(n_args == 1);
1413 vtype_kind_t vtype_err;
1414 emit_pre_pop_reg(emit, &vtype_err, REG_ARG_1); // arg1 = object to raise
1415 assert(vtype_err == VTYPE_PYOBJ);
1416 emit_call(emit, 0, mp_make_raise_obj); // TODO need to add function to runtime table
1417 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1418 emit_pre_pop_reg(emit, &vtype_err, REG_ARG_1);
1419 emit_call(emit, 0, nlr_jump); // TODO need to add function to runtime table
Damien13ed3a62013-10-08 09:05:10 +01001420}
Damien Georgeb601d952014-06-30 05:17:25 +01001421
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001422STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001423 // not supported (for now)
1424 assert(0);
1425}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001426STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001427 // not supported (for now)
1428 assert(0);
1429}
1430
Damien Georgeb601d952014-06-30 05:17:25 +01001431STATIC void emit_native_start_except_handler(emit_t *emit) {
1432 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
1433 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
1434 // the first 2 elements, so we can get the thrown value.
1435 adjust_stack(emit, 2);
1436 vtype_kind_t vtype_nlr;
1437 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
1438 emit_pre_pop_discard(emit, &vtype_nlr); // discard the linked-list pointer in the nlr_buf
1439 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
1440}
1441
1442STATIC void emit_native_end_except_handler(emit_t *emit) {
1443 adjust_stack(emit, -3); // stack adjust (not sure why it's this much...)
1444}
1445
Damien13ed3a62013-10-08 09:05:10 +01001446const emit_method_table_t EXPORT_FUN(method_table) = {
1447 emit_native_set_viper_types,
1448 emit_native_start_pass,
1449 emit_native_end_pass,
1450 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00001451 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00001452 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01001453
1454 emit_native_load_id,
1455 emit_native_store_id,
1456 emit_native_delete_id,
1457
1458 emit_native_label_assign,
1459 emit_native_import_name,
1460 emit_native_import_from,
1461 emit_native_import_star,
1462 emit_native_load_const_tok,
1463 emit_native_load_const_small_int,
1464 emit_native_load_const_int,
1465 emit_native_load_const_dec,
Damien13ed3a62013-10-08 09:05:10 +01001466 emit_native_load_const_str,
Damien George3558f622014-04-20 17:50:40 +01001467 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01001468 emit_native_load_fast,
Damien13ed3a62013-10-08 09:05:10 +01001469 emit_native_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +00001470 emit_native_load_name,
1471 emit_native_load_global,
Damien13ed3a62013-10-08 09:05:10 +01001472 emit_native_load_attr,
1473 emit_native_load_method,
1474 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01001475 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01001476 emit_native_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +00001477 emit_native_store_deref,
Damien13ed3a62013-10-08 09:05:10 +01001478 emit_native_store_name,
1479 emit_native_store_global,
Damien13ed3a62013-10-08 09:05:10 +01001480 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01001481 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01001482 emit_native_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +00001483 emit_native_delete_deref,
Damien13ed3a62013-10-08 09:05:10 +01001484 emit_native_delete_name,
1485 emit_native_delete_global,
Damien13ed3a62013-10-08 09:05:10 +01001486 emit_native_delete_attr,
1487 emit_native_delete_subscr,
1488 emit_native_dup_top,
1489 emit_native_dup_top_two,
1490 emit_native_pop_top,
1491 emit_native_rot_two,
1492 emit_native_rot_three,
1493 emit_native_jump,
1494 emit_native_pop_jump_if_true,
1495 emit_native_pop_jump_if_false,
1496 emit_native_jump_if_true_or_pop,
1497 emit_native_jump_if_false_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01001498 emit_native_break_loop,
1499 emit_native_continue_loop,
1500 emit_native_setup_with,
1501 emit_native_with_cleanup,
1502 emit_native_setup_except,
1503 emit_native_setup_finally,
1504 emit_native_end_finally,
1505 emit_native_get_iter,
1506 emit_native_for_iter,
1507 emit_native_for_iter_end,
1508 emit_native_pop_block,
1509 emit_native_pop_except,
1510 emit_native_unary_op,
1511 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01001512 emit_native_build_tuple,
1513 emit_native_build_list,
1514 emit_native_list_append,
1515 emit_native_build_map,
1516 emit_native_store_map,
1517 emit_native_map_add,
1518 emit_native_build_set,
1519 emit_native_set_add,
1520 emit_native_build_slice,
1521 emit_native_unpack_sequence,
1522 emit_native_unpack_ex,
1523 emit_native_make_function,
1524 emit_native_make_closure,
1525 emit_native_call_function,
1526 emit_native_call_method,
1527 emit_native_return_value,
1528 emit_native_raise_varargs,
1529 emit_native_yield_value,
1530 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01001531
1532 emit_native_start_except_handler,
1533 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01001534};
1535
Damien Georgee67ed5d2014-01-04 13:55:24 +00001536#endif // (MICROPY_EMIT_X64 && N_X64) || (MICROPY_EMIT_THUMB && N_THUMB)