blob: 707673f32de0be059de095e7100a0450c2da2ac5 [file] [log] [blame]
Damien13ed3a62013-10-08 09:05:10 +01001// Essentially normal Python has 1 type: Python objects
2// Viper has more than 1 type, and is just a more complicated (a superset of) Python.
3// If you declare everything in Viper as a Python object (ie omit type decls) then
4// it should in principle be exactly the same as Python native.
5// Having types means having more opcodes, like binary_op_nat_nat, binary_op_nat_obj etc.
6// In practice we won't have a VM but rather do this in asm which is actually very minimal.
7
8// Because it breaks strict Python equivalence it should be a completely separate
9// decorator. It breaks equivalence because overflow on integers wraps around.
10// It shouldn't break equivalence if you don't use the new types, but since the
11// type decls might be used in normal Python for other reasons, it's probably safest,
12// cleanest and clearest to make it a separate decorator.
13
14// Actually, it does break equivalence because integers default to native integers,
15// not Python objects.
16
17// for x in l[0:8]: can be compiled into a native loop if l has pointer type
18
19#include <unistd.h>
20#include <stdlib.h>
21#include <stdint.h>
22#include <stdio.h>
23#include <string.h>
24#include <assert.h>
25
26#include "misc.h"
Damienc025ebb2013-10-12 14:30:21 +010027#include "mpyconfig.h"
Damien13ed3a62013-10-08 09:05:10 +010028#include "lexer.h"
Damien13ed3a62013-10-08 09:05:10 +010029#include "parse.h"
30#include "scope.h"
31#include "runtime.h"
32#include "emit.h"
33
Damien13ed3a62013-10-08 09:05:10 +010034// wrapper around everything in this file
Damien3ef4abb2013-10-12 16:53:13 +010035#if N_X64 || N_THUMB
Damien13ed3a62013-10-08 09:05:10 +010036
Damien3ef4abb2013-10-12 16:53:13 +010037#if N_X64
Damien13ed3a62013-10-08 09:05:10 +010038
39// x64 specific stuff
40
41#include "asmx64.h"
42
43#define REG_LOCAL_1 (REG_RBX)
44#define REG_LOCAL_NUM (1)
45
46#define EXPORT_FUN(name) emit_native_x64_##name
47
48#define REG_TEMP0 (REG_RAX)
49#define REG_TEMP1 (REG_RDI)
50#define REG_TEMP2 (REG_RSI)
51#define ASM_MOV_REG_TO_LOCAL(reg, local_num) asm_x64_mov_r64_to_local(emit->as, (reg), (local_num))
52#define ASM_MOV_IMM_TO_REG(imm, reg) asm_x64_mov_i64_to_r64_optimised(emit->as, (imm), (reg))
Damieneb19efb2013-10-10 22:06:54 +010053#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 +010054#define ASM_MOV_LOCAL_TO_REG(local_num, reg) asm_x64_mov_local_to_r64(emit->as, (local_num), (reg))
55#define ASM_MOV_REG_TO_REG(reg_src, reg_dest) asm_x64_mov_r64_to_r64(emit->as, (reg_src), (reg_dest))
56#define ASM_MOV_LOCAL_ADDR_TO_REG(local_num, reg) asm_x64_mov_local_addr_to_r64(emit->as, (local_num), (reg))
57
Damien3ef4abb2013-10-12 16:53:13 +010058#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +010059
60// thumb specific stuff
61
62#include "asmthumb.h"
63
64#define REG_LOCAL_1 (REG_R4)
65#define REG_LOCAL_2 (REG_R5)
66#define REG_LOCAL_3 (REG_R6)
67#define REG_LOCAL_NUM (3)
68
69#define EXPORT_FUN(name) emit_native_thumb_##name
70
71#define REG_TEMP0 (REG_R0)
72#define REG_TEMP1 (REG_R1)
73#define REG_TEMP2 (REG_R2)
74#define ASM_MOV_REG_TO_LOCAL(reg, local_num) asm_thumb_mov_local_reg(emit->as, (local_num), (reg))
75#define ASM_MOV_IMM_TO_REG(imm, reg) asm_thumb_mov_reg_i32_optimised(emit->as, (reg), (imm))
Damieneb19efb2013-10-10 22:06:54 +010076#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 +010077#define ASM_MOV_LOCAL_TO_REG(local_num, reg) asm_thumb_mov_reg_local(emit->as, (reg), (local_num))
78#define ASM_MOV_REG_TO_REG(reg_src, reg_dest) asm_thumb_mov_reg_reg(emit->as, (reg_dest), (reg_src))
79#define ASM_MOV_LOCAL_ADDR_TO_REG(local_num, reg) asm_thumb_mov_reg_local_addr(emit->as, (reg), (local_num))
80
81#endif
82
83typedef enum {
Damienff8ed772013-10-08 22:18:32 +010084 STACK_VALUE,
85 STACK_REG,
86 STACK_IMM,
87} stack_info_kind_t;
Damien13ed3a62013-10-08 09:05:10 +010088
89typedef enum {
90 VTYPE_UNBOUND,
91 VTYPE_PYOBJ,
92 VTYPE_BOOL,
93 VTYPE_INT,
94 VTYPE_PTR,
95 VTYPE_PTR_NONE,
96 VTYPE_BUILTIN_V_INT,
97} vtype_kind_t;
98
Damienff8ed772013-10-08 22:18:32 +010099typedef struct _stack_info_t {
100 vtype_kind_t vtype;
101 stack_info_kind_t kind;
102 union {
103 int u_reg;
104 machine_int_t u_imm;
105 };
106} stack_info_t;
107
Damien13ed3a62013-10-08 09:05:10 +0100108struct _emit_t {
109 int pass;
110
111 bool do_viper_types;
Damienff8ed772013-10-08 22:18:32 +0100112
113 int local_vtype_alloc;
Damien13ed3a62013-10-08 09:05:10 +0100114 vtype_kind_t *local_vtype;
Damienff8ed772013-10-08 22:18:32 +0100115
116 int stack_info_alloc;
117 stack_info_t *stack_info;
118
Damien13ed3a62013-10-08 09:05:10 +0100119 int stack_start;
120 int stack_size;
121
122 bool last_emit_was_return_value;
123
Damien13ed3a62013-10-08 09:05:10 +0100124 scope_t *scope;
125
Damien3ef4abb2013-10-12 16:53:13 +0100126#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100127 asm_x64_t *as;
Damien3ef4abb2013-10-12 16:53:13 +0100128#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100129 asm_thumb_t *as;
130#endif
131};
132
133emit_t *EXPORT_FUN(new)(uint max_num_labels) {
134 emit_t *emit = m_new(emit_t, 1);
135 emit->do_viper_types = false;
Damienff8ed772013-10-08 22:18:32 +0100136 emit->local_vtype = NULL;
137 emit->stack_info = NULL;
Damien3ef4abb2013-10-12 16:53:13 +0100138#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100139 emit->as = asm_x64_new(max_num_labels);
Damien3ef4abb2013-10-12 16:53:13 +0100140#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100141 emit->as = asm_thumb_new(max_num_labels);
142#endif
143 return emit;
144}
145
146static void emit_native_set_viper_types(emit_t *emit, bool do_viper_types) {
147 emit->do_viper_types = do_viper_types;
148}
149
150static void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
151 emit->pass = pass;
152 emit->stack_start = 0;
153 emit->stack_size = 0;
154 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100155 emit->scope = scope;
156
Damienff8ed772013-10-08 22:18:32 +0100157 if (emit->local_vtype == NULL) {
158 emit->local_vtype_alloc = scope->num_locals + 20; // XXX should be maximum over all scopes
159 emit->local_vtype = m_new(vtype_kind_t, emit->local_vtype_alloc);
160 }
161 if (emit->stack_info == NULL) {
162 emit->stack_info_alloc = scope->stack_size + 50; // XXX don't know stack size on entry, should be maximum over all scopes
163 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100164 }
165
166 if (emit->do_viper_types) {
Damien13ed3a62013-10-08 09:05:10 +0100167 // TODO set types of arguments based on type signature
Damienff8ed772013-10-08 22:18:32 +0100168 for (int i = 0; i < emit->local_vtype_alloc; i++) {
169 emit->local_vtype[i] = VTYPE_UNBOUND;
170 }
171 for (int i = 0; i < emit->stack_info_alloc; i++) {
172 emit->stack_info[i].kind = STACK_VALUE;
173 emit->stack_info[i].vtype = VTYPE_UNBOUND;
174 }
Damien13ed3a62013-10-08 09:05:10 +0100175 } else {
Damienff8ed772013-10-08 22:18:32 +0100176 for (int i = 0; i < emit->local_vtype_alloc; i++) {
177 emit->local_vtype[i] = VTYPE_PYOBJ;
178 }
179 for (int i = 0; i < emit->stack_info_alloc; i++) {
180 emit->stack_info[i].kind = STACK_VALUE;
181 emit->stack_info[i].vtype = VTYPE_PYOBJ;
Damien13ed3a62013-10-08 09:05:10 +0100182 }
183 }
184
Damien3ef4abb2013-10-12 16:53:13 +0100185#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100186 asm_x64_start_pass(emit->as, pass);
Damien3ef4abb2013-10-12 16:53:13 +0100187#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100188 asm_thumb_start_pass(emit->as, pass);
189#endif
190
191 // entry to function
192 int num_locals = 0;
193 if (pass > PASS_1) {
194 num_locals = scope->num_locals - REG_LOCAL_NUM;
195 if (num_locals < 0) {
196 num_locals = 0;
197 }
198 emit->stack_start = num_locals;
199 num_locals += scope->stack_size;
200 }
Damien3ef4abb2013-10-12 16:53:13 +0100201#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100202 asm_x64_entry(emit->as, num_locals);
Damien3ef4abb2013-10-12 16:53:13 +0100203#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100204 asm_thumb_entry(emit->as, num_locals);
205#endif
206
207 // initialise locals from parameters
Damien3ef4abb2013-10-12 16:53:13 +0100208#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100209 for (int i = 0; i < scope->num_params; i++) {
210 if (i == 0) {
211 asm_x64_mov_r64_to_r64(emit->as, REG_ARG_1, REG_LOCAL_1);
212 } else if (i == 1) {
213 asm_x64_mov_r64_to_local(emit->as, REG_ARG_2, i - 1);
214 } else if (i == 2) {
215 asm_x64_mov_r64_to_local(emit->as, REG_ARG_3, i - 1);
216 } else {
217 // TODO not implemented
218 assert(0);
219 }
220 }
Damien3ef4abb2013-10-12 16:53:13 +0100221#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100222 for (int i = 0; i < scope->num_params; i++) {
223 if (i == 0) {
224 asm_thumb_mov_reg_reg(emit->as, REG_LOCAL_1, REG_ARG_1);
225 } else if (i == 1) {
226 asm_thumb_mov_reg_reg(emit->as, REG_LOCAL_2, REG_ARG_2);
227 } else if (i == 2) {
228 asm_thumb_mov_reg_reg(emit->as, REG_LOCAL_3, REG_ARG_3);
229 } else if (i == 3) {
230 asm_thumb_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
231 } else {
232 // TODO not implemented
233 assert(0);
234 }
235 }
236
237 asm_thumb_mov_reg_i32(emit->as, REG_R7, (machine_uint_t)rt_fun_table);
238#endif
239}
240
241static void emit_native_end_pass(emit_t *emit) {
Damien3ef4abb2013-10-12 16:53:13 +0100242#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100243 if (!emit->last_emit_was_return_value) {
244 asm_x64_exit(emit->as);
245 }
246 asm_x64_end_pass(emit->as);
Damien3ef4abb2013-10-12 16:53:13 +0100247#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100248 if (!emit->last_emit_was_return_value) {
249 asm_thumb_exit(emit->as);
250 }
251 asm_thumb_end_pass(emit->as);
252#endif
253
254 // check stack is back to zero size
255 if (emit->stack_size != 0) {
256 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
257 }
258
259 if (emit->pass == PASS_3) {
Damien3ef4abb2013-10-12 16:53:13 +0100260#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100261 py_fun_t f = asm_x64_get_code(emit->as);
262 rt_assign_native_code(emit->scope->unique_code_id, f, asm_x64_get_code_size(emit->as), emit->scope->num_params);
Damien3ef4abb2013-10-12 16:53:13 +0100263#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100264 py_fun_t f = asm_thumb_get_code(emit->as);
265 rt_assign_native_code(emit->scope->unique_code_id, f, asm_thumb_get_code_size(emit->as), emit->scope->num_params);
266#endif
267 }
268}
269
270static bool emit_native_last_emit_was_return_value(emit_t *emit) {
271 return emit->last_emit_was_return_value;
272}
273
274static int emit_native_get_stack_size(emit_t *emit) {
275 return emit->stack_size;
276}
277
278static void emit_native_set_stack_size(emit_t *emit, int size) {
279 emit->stack_size = size;
280}
281
282static void adjust_stack(emit_t *emit, int stack_size_delta) {
283 emit->stack_size += stack_size_delta;
284 assert(emit->stack_size >= 0);
285 if (emit->pass > PASS_1 && emit->stack_size > emit->scope->stack_size) {
286 emit->scope->stack_size = emit->stack_size;
287 }
288}
289
Damienff8ed772013-10-08 22:18:32 +0100290/*
Damien13ed3a62013-10-08 09:05:10 +0100291static void emit_pre_raw(emit_t *emit, int stack_size_delta) {
292 adjust_stack(emit, stack_size_delta);
293 emit->last_emit_was_return_value = false;
294}
Damienff8ed772013-10-08 22:18:32 +0100295*/
Damien13ed3a62013-10-08 09:05:10 +0100296
Damienff8ed772013-10-08 22:18:32 +0100297// this must be called at start of emit functions
Damien13ed3a62013-10-08 09:05:10 +0100298static void emit_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100299 emit->last_emit_was_return_value = false;
300 // settle the stack
301 /*
302 if (regs_needed != 0) {
303 for (int i = 0; i < emit->stack_size; i++) {
304 switch (emit->stack_info[i].kind) {
305 case STACK_VALUE:
306 break;
307
308 case STACK_REG:
309 // TODO only push reg if in regs_needed
310 emit->stack_info[i].kind = STACK_VALUE;
311 ASM_MOV_REG_TO_LOCAL(emit->stack_info[i].u_reg, emit->stack_start + i);
312 break;
313
314 case STACK_IMM:
315 // don't think we ever need to push imms for settling
316 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
317 break;
318 }
319 }
320 }
321 */
Damien13ed3a62013-10-08 09:05:10 +0100322}
323
324static vtype_kind_t peek_vtype(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100325 return emit->stack_info[emit->stack_size - 1].vtype;
326}
Damien13ed3a62013-10-08 09:05:10 +0100327
Damiend2755ec2013-10-16 23:58:48 +0100328// pos=1 is TOS, pos=2 is next, etc
329// use pos=0 for no skipping
330static void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
331 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100332 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100333 if (i != skip_stack_pos) {
334 stack_info_t *si = &emit->stack_info[i];
335 if (si->kind == STACK_REG && si->u_reg == reg_needed) {
336 si->kind = STACK_VALUE;
337 ASM_MOV_REG_TO_LOCAL(si->u_reg, emit->stack_start + i);
338 }
Damienff8ed772013-10-08 22:18:32 +0100339 }
340 }
341}
Damien13ed3a62013-10-08 09:05:10 +0100342
Damieneb19efb2013-10-10 22:06:54 +0100343static void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100344 for (int i = 0; i < emit->stack_size; i++) {
345 stack_info_t *si = &emit->stack_info[i];
346 if (si->kind == STACK_REG) {
347 si->kind = STACK_VALUE;
348 ASM_MOV_REG_TO_LOCAL(si->u_reg, emit->stack_start + i);
349 }
Damien13ed3a62013-10-08 09:05:10 +0100350 }
351}
352
Damiend2755ec2013-10-16 23:58:48 +0100353static void need_stack_settled(emit_t *emit) {
354 for (int i = 0; i < emit->stack_size; i++) {
355 stack_info_t *si = &emit->stack_info[i];
356 if (si->kind == STACK_REG) {
357 si->kind = STACK_VALUE;
358 ASM_MOV_REG_TO_LOCAL(si->u_reg, emit->stack_start + i);
359 }
360 }
361 for (int i = 0; i < emit->stack_size; i++) {
362 stack_info_t *si = &emit->stack_info[i];
363 if (si->kind == STACK_IMM) {
364 ASM_MOV_IMM_TO_LOCAL_USING(si->u_imm, emit->stack_start + i, REG_TEMP0);
365 }
366 }
367}
368
369// pos=1 is TOS, pos=2 is next, etc
370static void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
371 need_reg_single(emit, reg_dest, pos);
372 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +0100373 *vtype = si->vtype;
374 switch (si->kind) {
375 case STACK_VALUE:
Damiend2755ec2013-10-16 23:58:48 +0100376 ASM_MOV_LOCAL_TO_REG(emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100377 break;
378
Damienff8ed772013-10-08 22:18:32 +0100379 case STACK_REG:
380 if (si->u_reg != reg_dest) {
381 ASM_MOV_REG_TO_REG(si->u_reg, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100382 }
383 break;
384
Damienff8ed772013-10-08 22:18:32 +0100385 case STACK_IMM:
386 ASM_MOV_IMM_TO_REG(si->u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100387 break;
388 }
Damien13ed3a62013-10-08 09:05:10 +0100389}
390
Damiend2755ec2013-10-16 23:58:48 +0100391static void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
392 emit->last_emit_was_return_value = false;
393 emit_access_stack(emit, 1, vtype, reg_dest);
394 adjust_stack(emit, -1);
395}
396
Damien13ed3a62013-10-08 09:05:10 +0100397static void emit_pre_pop_reg_reg(emit_t *emit, vtype_kind_t *vtypea, int rega, vtype_kind_t *vtypeb, int regb) {
398 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100399 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +0100400}
401
402static 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) {
403 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100404 emit_pre_pop_reg(emit, vtypeb, regb);
405 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100406}
407
408static void emit_post(emit_t *emit) {
409}
410
411static void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +0100412 stack_info_t *si = &emit->stack_info[emit->stack_size];
413 si->vtype = vtype;
414 si->kind = STACK_REG;
415 si->u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +0100416 adjust_stack(emit, 1);
417}
418
Damienff8ed772013-10-08 22:18:32 +0100419static void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, machine_int_t imm) {
420 stack_info_t *si = &emit->stack_info[emit->stack_size];
421 si->vtype = vtype;
422 si->kind = STACK_IMM;
423 si->u_imm = imm;
424 adjust_stack(emit, 1);
425}
426
427static void emit_post_push_reg_reg(emit_t *emit, vtype_kind_t vtypea, int rega, vtype_kind_t vtypeb, int regb) {
428 emit_post_push_reg(emit, vtypea, rega);
429 emit_post_push_reg(emit, vtypeb, regb);
430}
431
Damien13ed3a62013-10-08 09:05:10 +0100432static 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 +0100433 emit_post_push_reg(emit, vtypea, rega);
434 emit_post_push_reg(emit, vtypeb, regb);
435 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100436}
437
438static 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 +0100439 emit_post_push_reg(emit, vtypea, rega);
440 emit_post_push_reg(emit, vtypeb, regb);
441 emit_post_push_reg(emit, vtypec, regc);
442 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +0100443}
444
445// vtype of all n_pop objects is VTYPE_PYOBJ
Damieneb19efb2013-10-10 22:06:54 +0100446// does not use any temporary registers (but may use reg_dest before loading it with stack pointer)
Damien13ed3a62013-10-08 09:05:10 +0100447static void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, int reg_dest, int n_pop) {
Damieneb19efb2013-10-10 22:06:54 +0100448 need_reg_all(emit);
Damienff8ed772013-10-08 22:18:32 +0100449 for (int i = 0; i < n_pop; i++) {
Damieneb19efb2013-10-10 22:06:54 +0100450 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
451 // must push any imm's to stack
452 if (si->kind == STACK_IMM) {
453 si->kind = STACK_VALUE;
454 ASM_MOV_IMM_TO_LOCAL_USING(si->u_imm, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
455 }
456 assert(si->kind == STACK_VALUE);
457 assert(si->vtype == VTYPE_PYOBJ);
Damienff8ed772013-10-08 22:18:32 +0100458 }
Damien13ed3a62013-10-08 09:05:10 +0100459 ASM_MOV_LOCAL_ADDR_TO_REG(emit->stack_start + emit->stack_size - 1, reg_dest);
460 adjust_stack(emit, -n_pop);
461}
462
463// vtype of all n_push objects is VTYPE_PYOBJ
464static void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, int reg_dest, int n_push) {
Damieneb19efb2013-10-10 22:06:54 +0100465 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +0100466 for (int i = 0; i < n_push; i++) {
Damien7f5dacf2013-10-10 11:24:39 +0100467 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
Damienff8ed772013-10-08 22:18:32 +0100468 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
Damien13ed3a62013-10-08 09:05:10 +0100469 }
470 ASM_MOV_LOCAL_ADDR_TO_REG(emit->stack_start + emit->stack_size + n_push - 1, reg_dest);
471 adjust_stack(emit, n_push);
472}
473
474static void emit_call(emit_t *emit, rt_fun_kind_t fun_kind, void *fun) {
Damiend2755ec2013-10-16 23:58:48 +0100475 need_reg_all(emit);
Damien3ef4abb2013-10-12 16:53:13 +0100476#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100477 asm_x64_call_ind(emit->as, fun, REG_RAX);
Damien3ef4abb2013-10-12 16:53:13 +0100478#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100479 asm_thumb_bl_ind(emit->as, rt_fun_table[fun_kind], fun_kind, REG_R3);
480#endif
481}
482
483static void emit_call_with_imm_arg(emit_t *emit, rt_fun_kind_t fun_kind, void *fun, machine_int_t arg_val, int arg_reg) {
Damieneb19efb2013-10-10 22:06:54 +0100484 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +0100485 ASM_MOV_IMM_TO_REG(arg_val, arg_reg);
Damiend2755ec2013-10-16 23:58:48 +0100486#if N_X64
487 asm_x64_call_ind(emit->as, fun, REG_RAX);
488#elif N_THUMB
489 asm_thumb_bl_ind(emit->as, rt_fun_table[fun_kind], fun_kind, REG_R3);
490#endif
Damien13ed3a62013-10-08 09:05:10 +0100491}
492
493static void emit_native_load_id(emit_t *emit, qstr qstr) {
494 // check for built-ins
495 if (strcmp(qstr_str(qstr), "v_int") == 0) {
Damienff8ed772013-10-08 22:18:32 +0100496 assert(0);
Damien13ed3a62013-10-08 09:05:10 +0100497 emit_pre(emit);
498 //emit_post_push_blank(emit, VTYPE_BUILTIN_V_INT);
499
500 // not a built-in, so do usual thing
501 } else {
502 emit_common_load_id(emit, &EXPORT_FUN(method_table), emit->scope, qstr);
503 }
504}
505
506static void emit_native_store_id(emit_t *emit, qstr qstr) {
507 // TODO check for built-ins and disallow
508 emit_common_store_id(emit, &EXPORT_FUN(method_table), emit->scope, qstr);
509}
510
511static void emit_native_delete_id(emit_t *emit, qstr qstr) {
512 // TODO check for built-ins and disallow
513 emit_common_delete_id(emit, &EXPORT_FUN(method_table), emit->scope, qstr);
514}
515
516static void emit_native_label_assign(emit_t *emit, int l) {
Damien6ba13142013-11-02 20:34:54 +0000517 emit_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +0100518 // need to commit stack because we can jump here from elsewhere
519 need_stack_settled(emit);
Damien3ef4abb2013-10-12 16:53:13 +0100520#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100521 asm_x64_label_assign(emit->as, l);
Damien3ef4abb2013-10-12 16:53:13 +0100522#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100523 asm_thumb_label_assign(emit->as, l);
524#endif
Damien6ba13142013-11-02 20:34:54 +0000525 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +0100526}
527
528static void emit_native_import_name(emit_t *emit, qstr qstr) {
529 // not implemented
530 assert(0);
531}
532
533static void emit_native_import_from(emit_t *emit, qstr qstr) {
534 // not implemented
535 assert(0);
536}
537
538static void emit_native_import_star(emit_t *emit) {
539 // not implemented
540 assert(0);
541}
542
543static void emit_native_load_const_tok(emit_t *emit, py_token_kind_t tok) {
544 emit_pre(emit);
545 int vtype;
546 machine_uint_t val;
547 if (emit->do_viper_types) {
548 switch (tok) {
549 case PY_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
550 case PY_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
551 case PY_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
552 default: assert(0); vtype = 0; val = 0; // shouldn't happen
553 }
554 } else {
555 vtype = VTYPE_PYOBJ;
556 switch (tok) {
557 case PY_TOKEN_KW_NONE: val = (machine_uint_t)py_const_none; break;
558 case PY_TOKEN_KW_FALSE: val = (machine_uint_t)py_const_false; break;
559 case PY_TOKEN_KW_TRUE: val = (machine_uint_t)py_const_true; break;
560 default: assert(0); vtype = 0; val = 0; // shouldn't happen
561 }
562 }
563 emit_post_push_imm(emit, vtype, val);
564}
565
566static void emit_native_load_const_small_int(emit_t *emit, int arg) {
567 emit_pre(emit);
568 if (emit->do_viper_types) {
569 emit_post_push_imm(emit, VTYPE_INT, arg);
570 } else {
571 emit_post_push_imm(emit, VTYPE_PYOBJ, (arg << 1) | 1);
572 }
573}
574
575static void emit_native_load_const_int(emit_t *emit, qstr qstr) {
576 // not implemented
577 // load integer, check fits in 32 bits
578 assert(0);
579}
580
581static void emit_native_load_const_dec(emit_t *emit, qstr qstr) {
Damien6ba13142013-11-02 20:34:54 +0000582 // for viper, a float/complex is just a Python object
583 emit_pre(emit);
584 emit_call_with_imm_arg(emit, RT_F_LOAD_CONST_DEC, rt_load_const_dec, qstr, REG_ARG_1);
585 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +0100586}
587
588static void emit_native_load_const_id(emit_t *emit, qstr qstr) {
Damien7f5dacf2013-10-10 11:24:39 +0100589 emit_pre(emit);
590 if (emit->do_viper_types) {
591 assert(0);
592 } else {
593 emit_call_with_imm_arg(emit, RT_F_LOAD_CONST_STR, rt_load_const_str, qstr, REG_ARG_1); // TODO
594 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
595 }
Damien13ed3a62013-10-08 09:05:10 +0100596}
597
598static void emit_native_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
599 emit_pre(emit);
600 if (emit->do_viper_types) {
601 // not implemented properly
602 // load a pointer to the asciiz string?
603 assert(0);
604 emit_post_push_imm(emit, VTYPE_PTR, (machine_uint_t)qstr_str(qstr));
605 } else {
606 emit_call_with_imm_arg(emit, RT_F_LOAD_CONST_STR, rt_load_const_str, qstr, REG_ARG_1);
607 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
608 }
609}
610
611static void emit_native_load_const_verbatim_start(emit_t *emit) {
612 // not supported/needed for viper
613 assert(0);
614}
615
616static void emit_native_load_const_verbatim_int(emit_t *emit, int val) {
617 // not supported/needed for viper
618 assert(0);
619}
620
621static void emit_native_load_const_verbatim_str(emit_t *emit, const char *str) {
622 // not supported/needed for viper
623 assert(0);
624}
625
626static void emit_native_load_const_verbatim_strn(emit_t *emit, const char *str, int len) {
627 // not supported/needed for viper
628 assert(0);
629}
630
631static void emit_native_load_const_verbatim_quoted_str(emit_t *emit, qstr qstr, bool bytes) {
632 // not supported/needed for viper
633 assert(0);
634}
635
636static void emit_native_load_const_verbatim_end(emit_t *emit) {
637 // not supported/needed for viper
638 assert(0);
639}
640
641static void emit_native_load_fast(emit_t *emit, qstr qstr, int local_num) {
642 vtype_kind_t vtype = emit->local_vtype[local_num];
643 if (vtype == VTYPE_UNBOUND) {
644 printf("ViperTypeError: local %s used before type known\n", qstr_str(qstr));
645 }
646 emit_pre(emit);
Damien3ef4abb2013-10-12 16:53:13 +0100647#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100648 if (local_num == 0) {
649 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
650 } else {
Damiend2755ec2013-10-16 23:58:48 +0100651 need_reg_single(emit, REG_RAX, 0);
Damien13ed3a62013-10-08 09:05:10 +0100652 asm_x64_mov_local_to_r64(emit->as, local_num - 1, REG_RAX);
653 emit_post_push_reg(emit, vtype, REG_RAX);
654 }
Damien3ef4abb2013-10-12 16:53:13 +0100655#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100656 if (local_num == 0) {
657 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
658 } else if (local_num == 1) {
659 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
660 } else if (local_num == 2) {
661 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
662 } else {
Damiend2755ec2013-10-16 23:58:48 +0100663 need_reg_single(emit, REG_R0, 0);
Damien13ed3a62013-10-08 09:05:10 +0100664 asm_thumb_mov_reg_local(emit->as, REG_R0, local_num - 1);
665 emit_post_push_reg(emit, vtype, REG_R0);
666 }
667#endif
668}
669
670static void emit_native_load_name(emit_t *emit, qstr qstr) {
671 emit_pre(emit);
672 emit_call_with_imm_arg(emit, RT_F_LOAD_NAME, rt_load_name, qstr, REG_ARG_1);
673 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
674}
675
676static void emit_native_load_global(emit_t *emit, qstr qstr) {
677 emit_pre(emit);
678 emit_call_with_imm_arg(emit, RT_F_LOAD_GLOBAL, rt_load_global, qstr, REG_ARG_1);
679 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
680}
681
Damien27fb45e2013-10-20 15:07:49 +0100682static void emit_native_load_deref(emit_t *emit, qstr qstr, int local_num) {
Damien13ed3a62013-10-08 09:05:10 +0100683 // not implemented
684 // in principle could support this quite easily (ldr r0, [r0, #0]) and then get closed over variables!
685 assert(0);
686}
687
Damien27fb45e2013-10-20 15:07:49 +0100688static void emit_native_load_closure(emit_t *emit, qstr qstr, int local_num) {
Damien13ed3a62013-10-08 09:05:10 +0100689 // not implemented
690 assert(0);
691}
692
693static void emit_native_load_attr(emit_t *emit, qstr qstr) {
694 // depends on type of subject:
695 // - integer, function, pointer to integers: error
696 // - pointer to structure: get member, quite easy
697 // - Python object: call rt_load_attr, and needs to be typed to convert result
698 vtype_kind_t vtype_base;
699 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
700 assert(vtype_base == VTYPE_PYOBJ);
701 emit_call_with_imm_arg(emit, RT_F_LOAD_ATTR, rt_load_attr, qstr, REG_ARG_2); // arg2 = attribute name
702 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
703}
704
705static void emit_native_load_method(emit_t *emit, qstr qstr) {
706 vtype_kind_t vtype_base;
707 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
708 assert(vtype_base == VTYPE_PYOBJ);
709 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
710 emit_call_with_imm_arg(emit, RT_F_LOAD_METHOD, rt_load_method, qstr, REG_ARG_2); // arg2 = method name
711}
712
713static void emit_native_load_build_class(emit_t *emit) {
Damien7f5dacf2013-10-10 11:24:39 +0100714 emit_pre(emit);
715 emit_call(emit, RT_F_LOAD_BUILD_CLASS, rt_load_build_class);
716 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +0100717}
718
719static void emit_native_store_fast(emit_t *emit, qstr qstr, int local_num) {
720 vtype_kind_t vtype;
Damien3ef4abb2013-10-12 16:53:13 +0100721#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100722 if (local_num == 0) {
723 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
724 } else {
725 emit_pre_pop_reg(emit, &vtype, REG_RAX);
726 asm_x64_mov_r64_to_local(emit->as, REG_RAX, local_num - 1);
727 }
Damien3ef4abb2013-10-12 16:53:13 +0100728#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100729 if (local_num == 0) {
730 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
731 } else if (local_num == 1) {
732 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
733 } else if (local_num == 2) {
734 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
735 } else {
736 emit_pre_pop_reg(emit, &vtype, REG_R0);
737 asm_thumb_mov_local_reg(emit->as, local_num - 1, REG_R0);
738 }
739#endif
740
741 emit_post(emit);
742
743 // check types
744 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
745 // first time this local is assigned, so give it a type of the object stored in it
746 emit->local_vtype[local_num] = vtype;
747 } else if (emit->local_vtype[local_num] != vtype) {
748 // type of local is not the same as object stored in it
749 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);
750 }
751}
752
753static void emit_native_store_name(emit_t *emit, qstr qstr) {
754 // rt_store_name, but needs conversion of object (maybe have rt_viper_store_name(obj, type))
755 vtype_kind_t vtype;
756 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
757 assert(vtype == VTYPE_PYOBJ);
758 emit_call_with_imm_arg(emit, RT_F_STORE_NAME, rt_store_name, qstr, REG_ARG_1); // arg1 = name
759 emit_post(emit);
760}
761
762static void emit_native_store_global(emit_t *emit, qstr qstr) {
763 // not implemented
764 assert(0);
765}
766
Damien27fb45e2013-10-20 15:07:49 +0100767static void emit_native_store_deref(emit_t *emit, qstr qstr, int local_num) {
Damien13ed3a62013-10-08 09:05:10 +0100768 // not implemented
769 assert(0);
770}
771
772static void emit_native_store_attr(emit_t *emit, qstr qstr) {
Damien7f5dacf2013-10-10 11:24:39 +0100773 vtype_kind_t vtype_base, vtype_val;
774 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
775 assert(vtype_base == VTYPE_PYOBJ);
776 assert(vtype_val == VTYPE_PYOBJ);
777 emit_call_with_imm_arg(emit, RT_F_STORE_ATTR, rt_store_attr, qstr, REG_ARG_2); // arg2 = attribute name
778 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +0100779}
780
Damien13ed3a62013-10-08 09:05:10 +0100781static void emit_native_store_subscr(emit_t *emit) {
782 // depends on type of subject:
783 // - integer, function, pointer to structure: error
784 // - pointer to integers: store as per array
785 // - Python object: call runtime with converted object or type info
786 vtype_kind_t vtype_index, vtype_base, vtype_value;
787 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
788 assert(vtype_index == VTYPE_PYOBJ);
789 assert(vtype_base == VTYPE_PYOBJ);
790 assert(vtype_value == VTYPE_PYOBJ);
791 emit_call(emit, RT_F_STORE_SUBSCR, rt_store_subscr);
792}
793
Damiena3977762013-10-09 23:10:10 +0100794static void emit_native_store_locals(emit_t *emit) {
795 // not needed
796 vtype_kind_t vtype;
797 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
798 emit_post(emit);
799}
800
Damien13ed3a62013-10-08 09:05:10 +0100801static void emit_native_delete_fast(emit_t *emit, qstr qstr, int local_num) {
802 // not implemented
803 // could support for Python types, just set to None (so GC can reclaim it)
804 assert(0);
805}
806
807static void emit_native_delete_name(emit_t *emit, qstr qstr) {
808 // not implemented
809 // use rt_delete_name
810 assert(0);
811}
812
813static void emit_native_delete_global(emit_t *emit, qstr qstr) {
814 // not implemented
815 // use rt_delete_global
816 assert(0);
817}
818
Damien27fb45e2013-10-20 15:07:49 +0100819static void emit_native_delete_deref(emit_t *emit, qstr qstr, int local_num) {
Damien13ed3a62013-10-08 09:05:10 +0100820 // not supported
821 assert(0);
822}
823
824static void emit_native_delete_attr(emit_t *emit, qstr qstr) {
825 // not supported
826 assert(0);
827}
828
829static void emit_native_delete_subscr(emit_t *emit) {
830 // not supported
831 assert(0);
832}
833
834static void emit_native_dup_top(emit_t *emit) {
835 vtype_kind_t vtype;
836 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
837 emit_post_push_reg_reg(emit, vtype, REG_TEMP0, vtype, REG_TEMP0);
838}
839
840static void emit_native_dup_top_two(emit_t *emit) {
841 vtype_kind_t vtype0, vtype1;
842 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
843 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
844}
845
846static void emit_native_pop_top(emit_t *emit) {
847 vtype_kind_t vtype;
848 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
849 emit_post(emit);
850}
851
852static void emit_native_rot_two(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100853 vtype_kind_t vtype0, vtype1;
854 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
855 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +0100856}
857
858static void emit_native_rot_three(emit_t *emit) {
859 vtype_kind_t vtype0, vtype1, vtype2;
860 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
861 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
862}
863
864static void emit_native_jump(emit_t *emit, int label) {
865 emit_pre(emit);
Damien3ef4abb2013-10-12 16:53:13 +0100866#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100867 asm_x64_jmp_label(emit->as, label);
Damien3ef4abb2013-10-12 16:53:13 +0100868#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100869 asm_thumb_b_label(emit->as, label);
870#endif
871 emit_post(emit);
872}
873
Damien1a6633a2013-11-03 13:58:19 +0000874static void emit_native_pop_jump_pre_helper(emit_t *emit, int label) {
Damien13ed3a62013-10-08 09:05:10 +0100875 vtype_kind_t vtype = peek_vtype(emit);
876 if (vtype == VTYPE_BOOL) {
877 emit_pre_pop_reg(emit, &vtype, REG_RET);
878 } else if (vtype == VTYPE_PYOBJ) {
879 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
880 emit_call(emit, RT_F_IS_TRUE, rt_is_true);
881 } else {
882 printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
883 assert(0);
884 }
Damien1a6633a2013-11-03 13:58:19 +0000885}
886
887static void emit_native_pop_jump_if_false(emit_t *emit, int label) {
888 emit_native_pop_jump_pre_helper(emit, label);
Damien3ef4abb2013-10-12 16:53:13 +0100889#if N_X64
Damien13ed3a62013-10-08 09:05:10 +0100890 asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
891 asm_x64_jcc_label(emit->as, JCC_JZ, label);
Damien3ef4abb2013-10-12 16:53:13 +0100892#elif N_THUMB
Damien1a6633a2013-11-03 13:58:19 +0000893 asm_thumb_cmp_rlo_i8(emit->as, REG_RET, 0);
894 asm_thumb_bcc_label(emit->as, THUMB_CC_EQ, label);
Damien13ed3a62013-10-08 09:05:10 +0100895#endif
896 emit_post(emit);
897}
898
899static void emit_native_pop_jump_if_true(emit_t *emit, int label) {
Damien1a6633a2013-11-03 13:58:19 +0000900 emit_native_pop_jump_pre_helper(emit, label);
901#if N_X64
902 asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
903 asm_x64_jcc_label(emit->as, JCC_JNZ, label);
904#elif N_THUMB
905 asm_thumb_cmp_rlo_i8(emit->as, REG_RET, 0);
906 asm_thumb_bcc_label(emit->as, THUMB_CC_NE, label);
907#endif
908 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +0100909}
Damien1a6633a2013-11-03 13:58:19 +0000910
Damien13ed3a62013-10-08 09:05:10 +0100911static void emit_native_jump_if_true_or_pop(emit_t *emit, int label) {
912 assert(0);
913}
914static void emit_native_jump_if_false_or_pop(emit_t *emit, int label) {
915 assert(0);
916}
917
918static void emit_native_setup_loop(emit_t *emit, int label) {
919 emit_pre(emit);
920 emit_post(emit);
921}
922
923static void emit_native_break_loop(emit_t *emit, int label) {
Damien6ba13142013-11-02 20:34:54 +0000924 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +0100925}
926static void emit_native_continue_loop(emit_t *emit, int label) {
927 assert(0);
928}
929static void emit_native_setup_with(emit_t *emit, int label) {
930 // not supported, or could be with runtime call
931 assert(0);
932}
933static void emit_native_with_cleanup(emit_t *emit) {
934 assert(0);
935}
936static void emit_native_setup_except(emit_t *emit, int label) {
937 assert(0);
938}
939static void emit_native_setup_finally(emit_t *emit, int label) {
940 assert(0);
941}
942static void emit_native_end_finally(emit_t *emit) {
943 assert(0);
944}
Damiend2755ec2013-10-16 23:58:48 +0100945
Damien13ed3a62013-10-08 09:05:10 +0100946static void emit_native_get_iter(emit_t *emit) {
947 // perhaps the difficult one, as we want to rewrite for loops using native code
948 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +0100949
950 vtype_kind_t vtype;
951 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
952 assert(vtype == VTYPE_PYOBJ);
953 emit_call(emit, RT_F_GETITER, rt_getiter);
954 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +0100955}
Damiend2755ec2013-10-16 23:58:48 +0100956
957static void emit_native_for_iter(emit_t *emit, int label) {
958 emit_pre(emit);
959 vtype_kind_t vtype;
960 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
961 assert(vtype == VTYPE_PYOBJ);
962 emit_call(emit, RT_F_ITERNEXT, rt_iternext);
963 ASM_MOV_IMM_TO_REG((machine_uint_t)py_const_stop_iteration, REG_TEMP1);
964#if N_X64
965 asm_x64_cmp_r64_with_r64(emit->as, REG_RET, REG_TEMP1);
966 asm_x64_jcc_label(emit->as, JCC_JE, label);
967#elif N_THUMB
Damiend2755ec2013-10-16 23:58:48 +0100968 asm_thumb_cmp_reg_reg(emit->as, REG_RET, REG_TEMP1);
Damien9b9e9962013-11-03 14:25:43 +0000969 asm_thumb_bcc_label(emit->as, THUMB_CC_EQ, label);
Damiend2755ec2013-10-16 23:58:48 +0100970#endif
971 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
972}
973
Damien13ed3a62013-10-08 09:05:10 +0100974static void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +0100975 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
976 emit_pre(emit);
977 adjust_stack(emit, -1);
978 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +0100979}
980
981static void emit_native_pop_block(emit_t *emit) {
982 emit_pre(emit);
983 emit_post(emit);
984}
985
986static void emit_native_pop_except(emit_t *emit) {
987 assert(0);
988}
989
990static void emit_native_unary_op(emit_t *emit, rt_unary_op_t op) {
991 vtype_kind_t vtype;
992 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
993 assert(vtype == VTYPE_PYOBJ);
994 emit_call_with_imm_arg(emit, RT_F_UNARY_OP, rt_unary_op, op, REG_ARG_1);
995 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
996}
997
998static void emit_native_binary_op(emit_t *emit, rt_binary_op_t op) {
999 vtype_kind_t vtype_lhs, vtype_rhs;
1000 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
1001 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damienae0bc082013-11-06 17:11:07 +00001002 assert(op == RT_BINARY_OP_ADD || op == RT_BINARY_OP_INPLACE_ADD);
Damien3ef4abb2013-10-12 16:53:13 +01001003#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001004 asm_x64_add_r64_to_r64(emit->as, REG_ARG_3, REG_ARG_2);
Damien3ef4abb2013-10-12 16:53:13 +01001005#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001006 asm_thumb_add_reg_reg_reg(emit->as, REG_ARG_2, REG_ARG_2, REG_ARG_3);
1007#endif
1008 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1009 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
1010 emit_call_with_imm_arg(emit, RT_F_BINARY_OP, rt_binary_op, op, REG_ARG_1);
1011 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1012 } else {
1013 printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs);
1014 assert(0);
1015 }
1016}
1017
1018static void emit_native_compare_op(emit_t *emit, rt_compare_op_t op) {
1019 vtype_kind_t vtype_lhs, vtype_rhs;
1020 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
1021 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
1022 assert(op == RT_COMPARE_OP_LESS);
Damien3ef4abb2013-10-12 16:53:13 +01001023#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001024 asm_x64_xor_r64_to_r64(emit->as, REG_RET, REG_RET);
1025 asm_x64_cmp_r64_with_r64(emit->as, REG_ARG_3, REG_ARG_2);
1026 asm_x64_setcc_r8(emit->as, JCC_JL, REG_RET);
Damien3ef4abb2013-10-12 16:53:13 +01001027#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001028 asm_thumb_cmp_reg_reg(emit->as, REG_ARG_2, REG_ARG_3);
1029 asm_thumb_ite_ge(emit->as);
1030 asm_thumb_movs_rlo_i8(emit->as, REG_RET, 0); // if r0 >= r1
1031 asm_thumb_movs_rlo_i8(emit->as, REG_RET, 1); // if r0 < r1
1032#endif
1033 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
1034 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
1035 emit_call_with_imm_arg(emit, RT_F_COMPARE_OP, rt_compare_op, op, REG_ARG_1);
1036 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1037 } else {
1038 printf("ViperTypeError: can't do comparison between types %d and %d\n", vtype_lhs, vtype_rhs);
1039 assert(0);
1040 }
1041}
1042
1043static void emit_native_build_tuple(emit_t *emit, int n_args) {
Damiend2755ec2013-10-16 23:58:48 +01001044 // for viper: call runtime, with types of args
1045 // if wrapped in byte_array, or something, allocates memory and fills it
1046 emit_pre(emit);
1047 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items in reverse order
1048 emit_call_with_imm_arg(emit, RT_F_BUILD_TUPLE, rt_build_tuple, n_args, REG_ARG_1);
1049 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01001050}
1051
1052static void emit_native_build_list(emit_t *emit, int n_args) {
1053 emit_pre(emit);
1054 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items in reverse order
1055 emit_call_with_imm_arg(emit, RT_F_BUILD_LIST, rt_build_list, n_args, REG_ARG_1);
1056 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
1057}
1058
1059static void emit_native_list_append(emit_t *emit, int list_index) {
Damiend2755ec2013-10-16 23:58:48 +01001060 // only used in list comprehension
1061 vtype_kind_t vtype_list, vtype_item;
1062 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
1063 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
1064 assert(vtype_list == VTYPE_PYOBJ);
1065 assert(vtype_item == VTYPE_PYOBJ);
1066 emit_call(emit, RT_F_LIST_APPEND, rt_list_append);
1067 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001068}
1069
1070static void emit_native_build_map(emit_t *emit, int n_args) {
1071 emit_pre(emit);
1072 emit_call_with_imm_arg(emit, RT_F_BUILD_MAP, rt_build_map, n_args, REG_ARG_1);
1073 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
1074}
1075
1076static void emit_native_store_map(emit_t *emit) {
1077 vtype_kind_t vtype_key, vtype_value, vtype_map;
1078 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
1079 assert(vtype_key == VTYPE_PYOBJ);
1080 assert(vtype_value == VTYPE_PYOBJ);
1081 assert(vtype_map == VTYPE_PYOBJ);
1082 emit_call(emit, RT_F_STORE_MAP, rt_store_map);
1083 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
1084}
1085
1086static void emit_native_map_add(emit_t *emit, int map_index) {
Damiend2755ec2013-10-16 23:58:48 +01001087 // only used in list comprehension
1088 vtype_kind_t vtype_map, vtype_key, vtype_value;
1089 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
1090 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
1091 assert(vtype_map == VTYPE_PYOBJ);
1092 assert(vtype_key == VTYPE_PYOBJ);
1093 assert(vtype_value == VTYPE_PYOBJ);
1094 emit_call(emit, RT_F_STORE_MAP, rt_store_map);
1095 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001096}
1097
1098static void emit_native_build_set(emit_t *emit, int n_args) {
1099 emit_pre(emit);
1100 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items in reverse order
1101 emit_call_with_imm_arg(emit, RT_F_BUILD_SET, rt_build_set, n_args, REG_ARG_1);
1102 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
1103}
1104
1105static void emit_native_set_add(emit_t *emit, int set_index) {
Damiend2755ec2013-10-16 23:58:48 +01001106 // only used in set comprehension
1107 vtype_kind_t vtype_set, vtype_item;
1108 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
1109 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
1110 assert(vtype_set == VTYPE_PYOBJ);
1111 assert(vtype_item == VTYPE_PYOBJ);
1112 emit_call(emit, RT_F_STORE_SET, rt_store_set);
1113 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001114}
Damiend2755ec2013-10-16 23:58:48 +01001115
Damien13ed3a62013-10-08 09:05:10 +01001116static void emit_native_build_slice(emit_t *emit, int n_args) {
1117 assert(0);
1118}
1119static void emit_native_unpack_sequence(emit_t *emit, int n_args) {
1120 // call runtime, needs type decl
1121 assert(0);
1122}
1123static void emit_native_unpack_ex(emit_t *emit, int n_left, int n_right) {
1124 assert(0);
1125}
1126
1127static void emit_native_make_function(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
1128 // call runtime, with type info for args, or don't support dict/default params, or only support Python objects for them
1129 assert(n_default_params == 0 && n_dict_params == 0);
1130 emit_pre(emit);
1131 emit_call_with_imm_arg(emit, RT_F_MAKE_FUNCTION_FROM_ID, rt_make_function_from_id, scope->unique_code_id, REG_ARG_1);
1132 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1133}
1134
1135static void emit_native_make_closure(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
1136 assert(0);
1137}
1138
1139static void emit_native_call_function(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
1140 // call special viper runtime routine with type info for args, and wanted type info for return
1141 assert(n_keyword == 0 && !have_star_arg && !have_dbl_star_arg);
Damieneb19efb2013-10-10 22:06:54 +01001142 /*
Damien13ed3a62013-10-08 09:05:10 +01001143 if (n_positional == 0) {
1144 vtype_kind_t vtype_fun;
1145 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
1146 assert(vtype_fun == VTYPE_PYOBJ);
1147 emit_call(emit, RT_F_CALL_FUNCTION_0, rt_call_function_0);
1148 } else if (n_positional == 1) {
1149 vtype_kind_t vtype_fun, vtype_arg1;
1150 emit_pre_pop_reg_reg(emit, &vtype_arg1, REG_ARG_2, &vtype_fun, REG_ARG_1); // the single argument, the function
1151 assert(vtype_fun == VTYPE_PYOBJ);
1152 assert(vtype_arg1 == VTYPE_PYOBJ);
1153 emit_call(emit, RT_F_CALL_FUNCTION_1, rt_call_function_1);
1154 } else if (n_positional == 2) {
1155 vtype_kind_t vtype_fun, vtype_arg1, vtype_arg2;
1156 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
1157 assert(vtype_fun == VTYPE_PYOBJ);
1158 assert(vtype_arg1 == VTYPE_PYOBJ);
1159 assert(vtype_arg2 == VTYPE_PYOBJ);
1160 emit_call(emit, RT_F_CALL_FUNCTION_2, rt_call_function_2);
1161 } else {
Damieneb19efb2013-10-10 22:06:54 +01001162 */
1163 emit_pre(emit);
1164 if (n_positional != 0) {
1165 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional); // pointer to args in reverse order
1166 }
1167 vtype_kind_t vtype_fun;
1168 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
1169 assert(vtype_fun == VTYPE_PYOBJ);
1170 emit_call_with_imm_arg(emit, RT_F_CALL_FUNCTION_N, rt_call_function_n, n_positional, REG_ARG_2);
1171 //}
Damien13ed3a62013-10-08 09:05:10 +01001172 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1173}
1174
1175static void emit_native_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
1176 assert(n_keyword == 0 && !have_star_arg && !have_dbl_star_arg);
Damieneb19efb2013-10-10 22:06:54 +01001177 /*
Damien13ed3a62013-10-08 09:05:10 +01001178 if (n_positional == 0) {
1179 vtype_kind_t vtype_meth, vtype_self;
1180 emit_pre_pop_reg_reg(emit, &vtype_self, REG_ARG_2, &vtype_meth, REG_ARG_1); // the self object (or NULL), the method
1181 assert(vtype_meth == VTYPE_PYOBJ);
1182 assert(vtype_self == VTYPE_PYOBJ);
1183 emit_call(emit, RT_F_CALL_METHOD_1, rt_call_method_1);
1184 } else if (n_positional == 1) {
1185 vtype_kind_t vtype_meth, vtype_self, vtype_arg1;
1186 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
1187 assert(vtype_meth == VTYPE_PYOBJ);
1188 assert(vtype_self == VTYPE_PYOBJ);
1189 assert(vtype_arg1 == VTYPE_PYOBJ);
1190 emit_call(emit, RT_F_CALL_METHOD_2, rt_call_method_2);
1191 } else {
Damieneb19efb2013-10-10 22:06:54 +01001192 */
Damien7f5dacf2013-10-10 11:24:39 +01001193 emit_pre(emit);
1194 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_positional + 2); // pointer to items in reverse order, including meth and self
1195 emit_call_with_imm_arg(emit, RT_F_CALL_METHOD_N, rt_call_method_n, n_positional, REG_ARG_1);
Damieneb19efb2013-10-10 22:06:54 +01001196 //}
Damien13ed3a62013-10-08 09:05:10 +01001197 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1198}
1199
1200static void emit_native_return_value(emit_t *emit) {
1201 // easy. since we don't know who we return to, just return the raw value.
1202 // runtime needs then to know our type signature, but I think that's possible.
1203 vtype_kind_t vtype;
1204 emit_pre_pop_reg(emit, &vtype, REG_RET);
1205 if (emit->do_viper_types) {
1206 assert(vtype == VTYPE_PTR_NONE);
1207 } else {
1208 assert(vtype == VTYPE_PYOBJ);
1209 }
1210 emit->last_emit_was_return_value = true;
Damien3ef4abb2013-10-12 16:53:13 +01001211#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001212 //asm_x64_call_ind(emit->as, 0, REG_RAX); to seg fault for debugging with gdb
1213 asm_x64_exit(emit->as);
Damien3ef4abb2013-10-12 16:53:13 +01001214#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001215 //asm_thumb_call_ind(emit->as, 0, REG_R0); to seg fault for debugging with gdb
1216 asm_thumb_exit(emit->as);
1217#endif
1218}
1219
1220static void emit_native_raise_varargs(emit_t *emit, int n_args) {
1221 // call runtime
1222 assert(0);
1223}
1224static void emit_native_yield_value(emit_t *emit) {
1225 // not supported (for now)
1226 assert(0);
1227}
1228static void emit_native_yield_from(emit_t *emit) {
1229 // not supported (for now)
1230 assert(0);
1231}
1232
1233const emit_method_table_t EXPORT_FUN(method_table) = {
1234 emit_native_set_viper_types,
1235 emit_native_start_pass,
1236 emit_native_end_pass,
1237 emit_native_last_emit_was_return_value,
1238 emit_native_get_stack_size,
1239 emit_native_set_stack_size,
1240
1241 emit_native_load_id,
1242 emit_native_store_id,
1243 emit_native_delete_id,
1244
1245 emit_native_label_assign,
1246 emit_native_import_name,
1247 emit_native_import_from,
1248 emit_native_import_star,
1249 emit_native_load_const_tok,
1250 emit_native_load_const_small_int,
1251 emit_native_load_const_int,
1252 emit_native_load_const_dec,
1253 emit_native_load_const_id,
1254 emit_native_load_const_str,
1255 emit_native_load_const_verbatim_start,
1256 emit_native_load_const_verbatim_int,
1257 emit_native_load_const_verbatim_str,
1258 emit_native_load_const_verbatim_strn,
1259 emit_native_load_const_verbatim_quoted_str,
1260 emit_native_load_const_verbatim_end,
1261 emit_native_load_fast,
1262 emit_native_load_name,
1263 emit_native_load_global,
1264 emit_native_load_deref,
1265 emit_native_load_closure,
1266 emit_native_load_attr,
1267 emit_native_load_method,
1268 emit_native_load_build_class,
1269 emit_native_store_fast,
1270 emit_native_store_name,
1271 emit_native_store_global,
1272 emit_native_store_deref,
1273 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01001274 emit_native_store_subscr,
Damiena3977762013-10-09 23:10:10 +01001275 emit_native_store_locals,
Damien13ed3a62013-10-08 09:05:10 +01001276 emit_native_delete_fast,
1277 emit_native_delete_name,
1278 emit_native_delete_global,
1279 emit_native_delete_deref,
1280 emit_native_delete_attr,
1281 emit_native_delete_subscr,
1282 emit_native_dup_top,
1283 emit_native_dup_top_two,
1284 emit_native_pop_top,
1285 emit_native_rot_two,
1286 emit_native_rot_three,
1287 emit_native_jump,
1288 emit_native_pop_jump_if_true,
1289 emit_native_pop_jump_if_false,
1290 emit_native_jump_if_true_or_pop,
1291 emit_native_jump_if_false_or_pop,
1292 emit_native_setup_loop,
1293 emit_native_break_loop,
1294 emit_native_continue_loop,
1295 emit_native_setup_with,
1296 emit_native_with_cleanup,
1297 emit_native_setup_except,
1298 emit_native_setup_finally,
1299 emit_native_end_finally,
1300 emit_native_get_iter,
1301 emit_native_for_iter,
1302 emit_native_for_iter_end,
1303 emit_native_pop_block,
1304 emit_native_pop_except,
1305 emit_native_unary_op,
1306 emit_native_binary_op,
1307 emit_native_compare_op,
1308 emit_native_build_tuple,
1309 emit_native_build_list,
1310 emit_native_list_append,
1311 emit_native_build_map,
1312 emit_native_store_map,
1313 emit_native_map_add,
1314 emit_native_build_set,
1315 emit_native_set_add,
1316 emit_native_build_slice,
1317 emit_native_unpack_sequence,
1318 emit_native_unpack_ex,
1319 emit_native_make_function,
1320 emit_native_make_closure,
1321 emit_native_call_function,
1322 emit_native_call_method,
1323 emit_native_return_value,
1324 emit_native_raise_varargs,
1325 emit_native_yield_value,
1326 emit_native_yield_from,
1327};
1328
Damien3ef4abb2013-10-12 16:53:13 +01001329#endif // N_X64 || N_THUMB