blob: 2687e4fba659c368ba3c82abf51d1ed8d86f222c [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"
27#include "lexer.h"
28#include "machine.h"
29#include "parse.h"
30#include "scope.h"
31#include "runtime.h"
32#include "emit.h"
33
34// select a machine architecture
35#if 0
36#if defined(EMIT_ENABLE_NATIVE_X64)
37#define N_X64
38#elif defined(EMIT_ENABLE_NATIVE_THUMB)
39#define N_THUMB
40#endif
41#endif
42
43// wrapper around everything in this file
44#if defined(N_X64) || defined(N_THUMB)
45
46#if defined(N_X64)
47
48// x64 specific stuff
49
50#include "asmx64.h"
51
52#define REG_LOCAL_1 (REG_RBX)
53#define REG_LOCAL_NUM (1)
54
55#define EXPORT_FUN(name) emit_native_x64_##name
56
57#define REG_TEMP0 (REG_RAX)
58#define REG_TEMP1 (REG_RDI)
59#define REG_TEMP2 (REG_RSI)
60#define ASM_MOV_REG_TO_LOCAL(reg, local_num) asm_x64_mov_r64_to_local(emit->as, (reg), (local_num))
61#define ASM_MOV_IMM_TO_REG(imm, reg) asm_x64_mov_i64_to_r64_optimised(emit->as, (imm), (reg))
62#define ASM_MOV_IMM_TO_LOCAL(imm, local_num) do { asm_x64_mov_i64_to_r64_optimised(emit->as, (imm), REG_RAX); asm_x64_mov_r64_to_local(emit->as, REG_RAX, (local_num)); } while (false)
63#define ASM_MOV_LOCAL_TO_REG(local_num, reg) asm_x64_mov_local_to_r64(emit->as, (local_num), (reg))
64#define ASM_MOV_REG_TO_REG(reg_src, reg_dest) asm_x64_mov_r64_to_r64(emit->as, (reg_src), (reg_dest))
65#define ASM_MOV_LOCAL_ADDR_TO_REG(local_num, reg) asm_x64_mov_local_addr_to_r64(emit->as, (local_num), (reg))
66
67#elif defined(N_THUMB)
68
69// thumb specific stuff
70
71#include "asmthumb.h"
72
73#define REG_LOCAL_1 (REG_R4)
74#define REG_LOCAL_2 (REG_R5)
75#define REG_LOCAL_3 (REG_R6)
76#define REG_LOCAL_NUM (3)
77
78#define EXPORT_FUN(name) emit_native_thumb_##name
79
80#define REG_TEMP0 (REG_R0)
81#define REG_TEMP1 (REG_R1)
82#define REG_TEMP2 (REG_R2)
83#define ASM_MOV_REG_TO_LOCAL(reg, local_num) asm_thumb_mov_local_reg(emit->as, (local_num), (reg))
84#define ASM_MOV_IMM_TO_REG(imm, reg) asm_thumb_mov_reg_i32_optimised(emit->as, (reg), (imm))
85#define ASM_MOV_IMM_TO_LOCAL(imm, local_num) do { asm_thumb_mov_reg_i32_optimised(emit->as, REG_R0, (imm)); asm_thumb_mov_local_reg(emit->as, (local_num), REG_R0); } while (false)
86#define ASM_MOV_LOCAL_TO_REG(local_num, reg) asm_thumb_mov_reg_local(emit->as, (reg), (local_num))
87#define ASM_MOV_REG_TO_REG(reg_src, reg_dest) asm_thumb_mov_reg_reg(emit->as, (reg_dest), (reg_src))
88#define ASM_MOV_LOCAL_ADDR_TO_REG(local_num, reg) asm_thumb_mov_reg_local_addr(emit->as, (reg), (local_num))
89
90#endif
91
92typedef enum {
Damienff8ed772013-10-08 22:18:32 +010093 STACK_VALUE,
94 STACK_REG,
95 STACK_IMM,
96} stack_info_kind_t;
Damien13ed3a62013-10-08 09:05:10 +010097
98typedef enum {
99 VTYPE_UNBOUND,
100 VTYPE_PYOBJ,
101 VTYPE_BOOL,
102 VTYPE_INT,
103 VTYPE_PTR,
104 VTYPE_PTR_NONE,
105 VTYPE_BUILTIN_V_INT,
106} vtype_kind_t;
107
Damienff8ed772013-10-08 22:18:32 +0100108typedef struct _stack_info_t {
109 vtype_kind_t vtype;
110 stack_info_kind_t kind;
111 union {
112 int u_reg;
113 machine_int_t u_imm;
114 };
115} stack_info_t;
116
Damien13ed3a62013-10-08 09:05:10 +0100117struct _emit_t {
118 int pass;
119
120 bool do_viper_types;
Damienff8ed772013-10-08 22:18:32 +0100121
122 int local_vtype_alloc;
Damien13ed3a62013-10-08 09:05:10 +0100123 vtype_kind_t *local_vtype;
Damienff8ed772013-10-08 22:18:32 +0100124
125 int stack_info_alloc;
126 stack_info_t *stack_info;
127
Damien13ed3a62013-10-08 09:05:10 +0100128 int stack_start;
129 int stack_size;
130
131 bool last_emit_was_return_value;
132
Damien13ed3a62013-10-08 09:05:10 +0100133 scope_t *scope;
134
135#if defined(N_X64)
136 asm_x64_t *as;
137#elif defined(N_THUMB)
138 asm_thumb_t *as;
139#endif
140};
141
142emit_t *EXPORT_FUN(new)(uint max_num_labels) {
143 emit_t *emit = m_new(emit_t, 1);
144 emit->do_viper_types = false;
Damienff8ed772013-10-08 22:18:32 +0100145 emit->local_vtype = NULL;
146 emit->stack_info = NULL;
Damien13ed3a62013-10-08 09:05:10 +0100147#if defined(N_X64)
148 emit->as = asm_x64_new(max_num_labels);
149#elif defined(N_THUMB)
150 emit->as = asm_thumb_new(max_num_labels);
151#endif
152 return emit;
153}
154
155static void emit_native_set_viper_types(emit_t *emit, bool do_viper_types) {
156 emit->do_viper_types = do_viper_types;
157}
158
159static void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
160 emit->pass = pass;
161 emit->stack_start = 0;
162 emit->stack_size = 0;
163 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100164 emit->scope = scope;
165
Damienff8ed772013-10-08 22:18:32 +0100166 if (emit->local_vtype == NULL) {
167 emit->local_vtype_alloc = scope->num_locals + 20; // XXX should be maximum over all scopes
168 emit->local_vtype = m_new(vtype_kind_t, emit->local_vtype_alloc);
169 }
170 if (emit->stack_info == NULL) {
171 emit->stack_info_alloc = scope->stack_size + 50; // XXX don't know stack size on entry, should be maximum over all scopes
172 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100173 }
174
175 if (emit->do_viper_types) {
Damien13ed3a62013-10-08 09:05:10 +0100176 // TODO set types of arguments based on type signature
Damienff8ed772013-10-08 22:18:32 +0100177 for (int i = 0; i < emit->local_vtype_alloc; i++) {
178 emit->local_vtype[i] = VTYPE_UNBOUND;
179 }
180 for (int i = 0; i < emit->stack_info_alloc; i++) {
181 emit->stack_info[i].kind = STACK_VALUE;
182 emit->stack_info[i].vtype = VTYPE_UNBOUND;
183 }
Damien13ed3a62013-10-08 09:05:10 +0100184 } else {
Damienff8ed772013-10-08 22:18:32 +0100185 for (int i = 0; i < emit->local_vtype_alloc; i++) {
186 emit->local_vtype[i] = VTYPE_PYOBJ;
187 }
188 for (int i = 0; i < emit->stack_info_alloc; i++) {
189 emit->stack_info[i].kind = STACK_VALUE;
190 emit->stack_info[i].vtype = VTYPE_PYOBJ;
Damien13ed3a62013-10-08 09:05:10 +0100191 }
192 }
193
194#if defined(N_X64)
195 asm_x64_start_pass(emit->as, pass);
196#elif defined(N_THUMB)
197 asm_thumb_start_pass(emit->as, pass);
198#endif
199
200 // entry to function
201 int num_locals = 0;
202 if (pass > PASS_1) {
203 num_locals = scope->num_locals - REG_LOCAL_NUM;
204 if (num_locals < 0) {
205 num_locals = 0;
206 }
207 emit->stack_start = num_locals;
208 num_locals += scope->stack_size;
209 }
210#if defined(N_X64)
211 asm_x64_entry(emit->as, num_locals);
212#elif defined(N_THUMB)
213 asm_thumb_entry(emit->as, num_locals);
214#endif
215
216 // initialise locals from parameters
217#if defined(N_X64)
218 for (int i = 0; i < scope->num_params; i++) {
219 if (i == 0) {
220 asm_x64_mov_r64_to_r64(emit->as, REG_ARG_1, REG_LOCAL_1);
221 } else if (i == 1) {
222 asm_x64_mov_r64_to_local(emit->as, REG_ARG_2, i - 1);
223 } else if (i == 2) {
224 asm_x64_mov_r64_to_local(emit->as, REG_ARG_3, i - 1);
225 } else {
226 // TODO not implemented
227 assert(0);
228 }
229 }
230#elif defined(N_THUMB)
231 for (int i = 0; i < scope->num_params; i++) {
232 if (i == 0) {
233 asm_thumb_mov_reg_reg(emit->as, REG_LOCAL_1, REG_ARG_1);
234 } else if (i == 1) {
235 asm_thumb_mov_reg_reg(emit->as, REG_LOCAL_2, REG_ARG_2);
236 } else if (i == 2) {
237 asm_thumb_mov_reg_reg(emit->as, REG_LOCAL_3, REG_ARG_3);
238 } else if (i == 3) {
239 asm_thumb_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
240 } else {
241 // TODO not implemented
242 assert(0);
243 }
244 }
245
246 asm_thumb_mov_reg_i32(emit->as, REG_R7, (machine_uint_t)rt_fun_table);
247#endif
248}
249
250static void emit_native_end_pass(emit_t *emit) {
251#if defined(N_X64)
252 if (!emit->last_emit_was_return_value) {
253 asm_x64_exit(emit->as);
254 }
255 asm_x64_end_pass(emit->as);
256#elif defined(N_THUMB)
257 if (!emit->last_emit_was_return_value) {
258 asm_thumb_exit(emit->as);
259 }
260 asm_thumb_end_pass(emit->as);
261#endif
262
263 // check stack is back to zero size
264 if (emit->stack_size != 0) {
265 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
266 }
267
268 if (emit->pass == PASS_3) {
269#if defined(N_X64)
270 py_fun_t f = asm_x64_get_code(emit->as);
271 rt_assign_native_code(emit->scope->unique_code_id, f, asm_x64_get_code_size(emit->as), emit->scope->num_params);
272#elif defined(N_THUMB)
273 py_fun_t f = asm_thumb_get_code(emit->as);
274 rt_assign_native_code(emit->scope->unique_code_id, f, asm_thumb_get_code_size(emit->as), emit->scope->num_params);
275#endif
276 }
277}
278
279static bool emit_native_last_emit_was_return_value(emit_t *emit) {
280 return emit->last_emit_was_return_value;
281}
282
283static int emit_native_get_stack_size(emit_t *emit) {
284 return emit->stack_size;
285}
286
287static void emit_native_set_stack_size(emit_t *emit, int size) {
288 emit->stack_size = size;
289}
290
291static void adjust_stack(emit_t *emit, int stack_size_delta) {
292 emit->stack_size += stack_size_delta;
293 assert(emit->stack_size >= 0);
294 if (emit->pass > PASS_1 && emit->stack_size > emit->scope->stack_size) {
295 emit->scope->stack_size = emit->stack_size;
296 }
297}
298
Damienff8ed772013-10-08 22:18:32 +0100299/*
Damien13ed3a62013-10-08 09:05:10 +0100300static void emit_pre_raw(emit_t *emit, int stack_size_delta) {
301 adjust_stack(emit, stack_size_delta);
302 emit->last_emit_was_return_value = false;
303}
Damienff8ed772013-10-08 22:18:32 +0100304*/
Damien13ed3a62013-10-08 09:05:10 +0100305
Damienff8ed772013-10-08 22:18:32 +0100306// this must be called at start of emit functions
Damien13ed3a62013-10-08 09:05:10 +0100307static void emit_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100308 emit->last_emit_was_return_value = false;
309 // settle the stack
310 /*
311 if (regs_needed != 0) {
312 for (int i = 0; i < emit->stack_size; i++) {
313 switch (emit->stack_info[i].kind) {
314 case STACK_VALUE:
315 break;
316
317 case STACK_REG:
318 // TODO only push reg if in regs_needed
319 emit->stack_info[i].kind = STACK_VALUE;
320 ASM_MOV_REG_TO_LOCAL(emit->stack_info[i].u_reg, emit->stack_start + i);
321 break;
322
323 case STACK_IMM:
324 // don't think we ever need to push imms for settling
325 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
326 break;
327 }
328 }
329 }
330 */
Damien13ed3a62013-10-08 09:05:10 +0100331}
332
333static vtype_kind_t peek_vtype(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100334 return emit->stack_info[emit->stack_size - 1].vtype;
335}
Damien13ed3a62013-10-08 09:05:10 +0100336
Damienff8ed772013-10-08 22:18:32 +0100337static void need_reg_single(emit_t *emit, int reg_needed) {
338 for (int i = 0; i < emit->stack_size; i++) {
339 stack_info_t *si = &emit->stack_info[i];
340 if (si->kind == STACK_REG && si->u_reg == reg_needed) {
341 si->kind = STACK_VALUE;
342 ASM_MOV_REG_TO_LOCAL(si->u_reg, emit->stack_start + i);
343 }
344 }
345}
Damien13ed3a62013-10-08 09:05:10 +0100346
Damien7f5dacf2013-10-10 11:24:39 +0100347static void need_reg_all(emit_t *emit, int num_stack_top_that_must_be_value) {
Damienff8ed772013-10-08 22:18:32 +0100348 for (int i = 0; i < emit->stack_size; i++) {
349 stack_info_t *si = &emit->stack_info[i];
350 if (si->kind == STACK_REG) {
351 si->kind = STACK_VALUE;
352 ASM_MOV_REG_TO_LOCAL(si->u_reg, emit->stack_start + i);
353 }
Damien13ed3a62013-10-08 09:05:10 +0100354 }
Damien7f5dacf2013-10-10 11:24:39 +0100355 // must do this after making all registers available because ASM_MOV_IMM_TO_LOCAL uses a temporary register
356 for (int i = 0; i < num_stack_top_that_must_be_value; i++) {
357 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
358 if (si->kind == STACK_IMM) {
359 si->kind = STACK_VALUE;
360 ASM_MOV_IMM_TO_LOCAL(si->u_imm, emit->stack_start + emit->stack_size - 1 - i);
361 }
362 }
Damien13ed3a62013-10-08 09:05:10 +0100363}
364
365static void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damienff8ed772013-10-08 22:18:32 +0100366 emit->last_emit_was_return_value = false;
367 adjust_stack(emit, -1);
368 need_reg_single(emit, reg_dest);
369 stack_info_t *si = &emit->stack_info[emit->stack_size];
370 *vtype = si->vtype;
371 switch (si->kind) {
372 case STACK_VALUE:
373 ASM_MOV_LOCAL_TO_REG(emit->stack_start + emit->stack_size, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100374 break;
375
Damienff8ed772013-10-08 22:18:32 +0100376 case STACK_REG:
377 if (si->u_reg != reg_dest) {
378 ASM_MOV_REG_TO_REG(si->u_reg, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100379 }
380 break;
381
Damienff8ed772013-10-08 22:18:32 +0100382 case STACK_IMM:
383 ASM_MOV_IMM_TO_REG(si->u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100384 break;
385 }
Damien13ed3a62013-10-08 09:05:10 +0100386}
387
388static void emit_pre_pop_reg_reg(emit_t *emit, vtype_kind_t *vtypea, int rega, vtype_kind_t *vtypeb, int regb) {
389 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100390 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +0100391}
392
393static 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) {
394 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100395 emit_pre_pop_reg(emit, vtypeb, regb);
396 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100397}
398
399static void emit_post(emit_t *emit) {
400}
401
402static void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +0100403 stack_info_t *si = &emit->stack_info[emit->stack_size];
404 si->vtype = vtype;
405 si->kind = STACK_REG;
406 si->u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +0100407 adjust_stack(emit, 1);
408}
409
Damienff8ed772013-10-08 22:18:32 +0100410static void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, machine_int_t imm) {
411 stack_info_t *si = &emit->stack_info[emit->stack_size];
412 si->vtype = vtype;
413 si->kind = STACK_IMM;
414 si->u_imm = imm;
415 adjust_stack(emit, 1);
416}
417
418static void emit_post_push_reg_reg(emit_t *emit, vtype_kind_t vtypea, int rega, vtype_kind_t vtypeb, int regb) {
419 emit_post_push_reg(emit, vtypea, rega);
420 emit_post_push_reg(emit, vtypeb, regb);
421}
422
Damien13ed3a62013-10-08 09:05:10 +0100423static 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 +0100424 emit_post_push_reg(emit, vtypea, rega);
425 emit_post_push_reg(emit, vtypeb, regb);
426 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100427}
428
429static 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 +0100430 emit_post_push_reg(emit, vtypea, rega);
431 emit_post_push_reg(emit, vtypeb, regb);
432 emit_post_push_reg(emit, vtypec, regc);
433 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +0100434}
435
436// vtype of all n_pop objects is VTYPE_PYOBJ
437static void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, int reg_dest, int n_pop) {
Damien7f5dacf2013-10-10 11:24:39 +0100438 need_reg_all(emit, n_pop);
Damienff8ed772013-10-08 22:18:32 +0100439 for (int i = 0; i < n_pop; i++) {
Damien7f5dacf2013-10-10 11:24:39 +0100440 assert(emit->stack_info[emit->stack_size - 1 - i].kind == STACK_VALUE);
441 assert(emit->stack_info[emit->stack_size - 1 - i].vtype == VTYPE_PYOBJ);
Damienff8ed772013-10-08 22:18:32 +0100442 }
Damien13ed3a62013-10-08 09:05:10 +0100443 ASM_MOV_LOCAL_ADDR_TO_REG(emit->stack_start + emit->stack_size - 1, reg_dest);
444 adjust_stack(emit, -n_pop);
445}
446
447// vtype of all n_push objects is VTYPE_PYOBJ
448static void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, int reg_dest, int n_push) {
Damien7f5dacf2013-10-10 11:24:39 +0100449 need_reg_all(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +0100450 for (int i = 0; i < n_push; i++) {
Damien7f5dacf2013-10-10 11:24:39 +0100451 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
Damienff8ed772013-10-08 22:18:32 +0100452 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
Damien13ed3a62013-10-08 09:05:10 +0100453 }
454 ASM_MOV_LOCAL_ADDR_TO_REG(emit->stack_start + emit->stack_size + n_push - 1, reg_dest);
455 adjust_stack(emit, n_push);
456}
457
458static void emit_call(emit_t *emit, rt_fun_kind_t fun_kind, void *fun) {
459#if defined(N_X64)
460 asm_x64_call_ind(emit->as, fun, REG_RAX);
461#elif defined(N_THUMB)
462 asm_thumb_bl_ind(emit->as, rt_fun_table[fun_kind], fun_kind, REG_R3);
463#endif
464}
465
466static 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) {
Damien7f5dacf2013-10-10 11:24:39 +0100467 need_reg_all(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +0100468 ASM_MOV_IMM_TO_REG(arg_val, arg_reg);
469 emit_call(emit, fun_kind, fun);
470}
471
472static void emit_native_load_id(emit_t *emit, qstr qstr) {
473 // check for built-ins
474 if (strcmp(qstr_str(qstr), "v_int") == 0) {
Damienff8ed772013-10-08 22:18:32 +0100475 assert(0);
Damien13ed3a62013-10-08 09:05:10 +0100476 emit_pre(emit);
477 //emit_post_push_blank(emit, VTYPE_BUILTIN_V_INT);
478
479 // not a built-in, so do usual thing
480 } else {
481 emit_common_load_id(emit, &EXPORT_FUN(method_table), emit->scope, qstr);
482 }
483}
484
485static void emit_native_store_id(emit_t *emit, qstr qstr) {
486 // TODO check for built-ins and disallow
487 emit_common_store_id(emit, &EXPORT_FUN(method_table), emit->scope, qstr);
488}
489
490static void emit_native_delete_id(emit_t *emit, qstr qstr) {
491 // TODO check for built-ins and disallow
492 emit_common_delete_id(emit, &EXPORT_FUN(method_table), emit->scope, qstr);
493}
494
495static void emit_native_label_assign(emit_t *emit, int l) {
496#if defined(N_X64)
497 asm_x64_label_assign(emit->as, l);
498#elif defined(N_THUMB)
499 asm_thumb_label_assign(emit->as, l);
500#endif
501}
502
503static void emit_native_import_name(emit_t *emit, qstr qstr) {
504 // not implemented
505 assert(0);
506}
507
508static void emit_native_import_from(emit_t *emit, qstr qstr) {
509 // not implemented
510 assert(0);
511}
512
513static void emit_native_import_star(emit_t *emit) {
514 // not implemented
515 assert(0);
516}
517
518static void emit_native_load_const_tok(emit_t *emit, py_token_kind_t tok) {
519 emit_pre(emit);
520 int vtype;
521 machine_uint_t val;
522 if (emit->do_viper_types) {
523 switch (tok) {
524 case PY_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
525 case PY_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
526 case PY_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
527 default: assert(0); vtype = 0; val = 0; // shouldn't happen
528 }
529 } else {
530 vtype = VTYPE_PYOBJ;
531 switch (tok) {
532 case PY_TOKEN_KW_NONE: val = (machine_uint_t)py_const_none; break;
533 case PY_TOKEN_KW_FALSE: val = (machine_uint_t)py_const_false; break;
534 case PY_TOKEN_KW_TRUE: val = (machine_uint_t)py_const_true; break;
535 default: assert(0); vtype = 0; val = 0; // shouldn't happen
536 }
537 }
538 emit_post_push_imm(emit, vtype, val);
539}
540
541static void emit_native_load_const_small_int(emit_t *emit, int arg) {
542 emit_pre(emit);
543 if (emit->do_viper_types) {
544 emit_post_push_imm(emit, VTYPE_INT, arg);
545 } else {
546 emit_post_push_imm(emit, VTYPE_PYOBJ, (arg << 1) | 1);
547 }
548}
549
550static void emit_native_load_const_int(emit_t *emit, qstr qstr) {
551 // not implemented
552 // load integer, check fits in 32 bits
553 assert(0);
554}
555
556static void emit_native_load_const_dec(emit_t *emit, qstr qstr) {
557 // not supported for viper (although, could support floats in future)
558 assert(0);
559}
560
561static void emit_native_load_const_id(emit_t *emit, qstr qstr) {
Damien7f5dacf2013-10-10 11:24:39 +0100562 emit_pre(emit);
563 if (emit->do_viper_types) {
564 assert(0);
565 } else {
566 emit_call_with_imm_arg(emit, RT_F_LOAD_CONST_STR, rt_load_const_str, qstr, REG_ARG_1); // TODO
567 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
568 }
Damien13ed3a62013-10-08 09:05:10 +0100569}
570
571static void emit_native_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
572 emit_pre(emit);
573 if (emit->do_viper_types) {
574 // not implemented properly
575 // load a pointer to the asciiz string?
576 assert(0);
577 emit_post_push_imm(emit, VTYPE_PTR, (machine_uint_t)qstr_str(qstr));
578 } else {
579 emit_call_with_imm_arg(emit, RT_F_LOAD_CONST_STR, rt_load_const_str, qstr, REG_ARG_1);
580 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
581 }
582}
583
584static void emit_native_load_const_verbatim_start(emit_t *emit) {
585 // not supported/needed for viper
586 assert(0);
587}
588
589static void emit_native_load_const_verbatim_int(emit_t *emit, int val) {
590 // not supported/needed for viper
591 assert(0);
592}
593
594static void emit_native_load_const_verbatim_str(emit_t *emit, const char *str) {
595 // not supported/needed for viper
596 assert(0);
597}
598
599static void emit_native_load_const_verbatim_strn(emit_t *emit, const char *str, int len) {
600 // not supported/needed for viper
601 assert(0);
602}
603
604static void emit_native_load_const_verbatim_quoted_str(emit_t *emit, qstr qstr, bool bytes) {
605 // not supported/needed for viper
606 assert(0);
607}
608
609static void emit_native_load_const_verbatim_end(emit_t *emit) {
610 // not supported/needed for viper
611 assert(0);
612}
613
614static void emit_native_load_fast(emit_t *emit, qstr qstr, int local_num) {
615 vtype_kind_t vtype = emit->local_vtype[local_num];
616 if (vtype == VTYPE_UNBOUND) {
617 printf("ViperTypeError: local %s used before type known\n", qstr_str(qstr));
618 }
619 emit_pre(emit);
620#if defined(N_X64)
621 if (local_num == 0) {
622 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
623 } else {
Damienff8ed772013-10-08 22:18:32 +0100624 need_reg_single(emit, REG_RAX);
Damien13ed3a62013-10-08 09:05:10 +0100625 asm_x64_mov_local_to_r64(emit->as, local_num - 1, REG_RAX);
626 emit_post_push_reg(emit, vtype, REG_RAX);
627 }
628#elif defined(N_THUMB)
629 if (local_num == 0) {
630 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
631 } else if (local_num == 1) {
632 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
633 } else if (local_num == 2) {
634 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
635 } else {
Damienff8ed772013-10-08 22:18:32 +0100636 need_reg_single(emit, REG_R0);
Damien13ed3a62013-10-08 09:05:10 +0100637 asm_thumb_mov_reg_local(emit->as, REG_R0, local_num - 1);
638 emit_post_push_reg(emit, vtype, REG_R0);
639 }
640#endif
641}
642
643static void emit_native_load_name(emit_t *emit, qstr qstr) {
644 emit_pre(emit);
645 emit_call_with_imm_arg(emit, RT_F_LOAD_NAME, rt_load_name, qstr, REG_ARG_1);
646 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
647}
648
649static void emit_native_load_global(emit_t *emit, qstr qstr) {
650 emit_pre(emit);
651 emit_call_with_imm_arg(emit, RT_F_LOAD_GLOBAL, rt_load_global, qstr, REG_ARG_1);
652 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
653}
654
655static void emit_native_load_deref(emit_t *emit, qstr qstr) {
656 // not implemented
657 // in principle could support this quite easily (ldr r0, [r0, #0]) and then get closed over variables!
658 assert(0);
659}
660
661static void emit_native_load_closure(emit_t *emit, qstr qstr) {
662 // not implemented
663 assert(0);
664}
665
666static void emit_native_load_attr(emit_t *emit, qstr qstr) {
667 // depends on type of subject:
668 // - integer, function, pointer to integers: error
669 // - pointer to structure: get member, quite easy
670 // - Python object: call rt_load_attr, and needs to be typed to convert result
671 vtype_kind_t vtype_base;
672 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
673 assert(vtype_base == VTYPE_PYOBJ);
674 emit_call_with_imm_arg(emit, RT_F_LOAD_ATTR, rt_load_attr, qstr, REG_ARG_2); // arg2 = attribute name
675 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
676}
677
678static void emit_native_load_method(emit_t *emit, qstr qstr) {
679 vtype_kind_t vtype_base;
680 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
681 assert(vtype_base == VTYPE_PYOBJ);
682 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
683 emit_call_with_imm_arg(emit, RT_F_LOAD_METHOD, rt_load_method, qstr, REG_ARG_2); // arg2 = method name
684}
685
686static void emit_native_load_build_class(emit_t *emit) {
Damien7f5dacf2013-10-10 11:24:39 +0100687 emit_pre(emit);
688 emit_call(emit, RT_F_LOAD_BUILD_CLASS, rt_load_build_class);
689 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +0100690}
691
692static void emit_native_store_fast(emit_t *emit, qstr qstr, int local_num) {
693 vtype_kind_t vtype;
694#if defined(N_X64)
695 if (local_num == 0) {
696 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
697 } else {
698 emit_pre_pop_reg(emit, &vtype, REG_RAX);
699 asm_x64_mov_r64_to_local(emit->as, REG_RAX, local_num - 1);
700 }
701#elif defined(N_THUMB)
702 if (local_num == 0) {
703 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
704 } else if (local_num == 1) {
705 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
706 } else if (local_num == 2) {
707 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
708 } else {
709 emit_pre_pop_reg(emit, &vtype, REG_R0);
710 asm_thumb_mov_local_reg(emit->as, local_num - 1, REG_R0);
711 }
712#endif
713
714 emit_post(emit);
715
716 // check types
717 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
718 // first time this local is assigned, so give it a type of the object stored in it
719 emit->local_vtype[local_num] = vtype;
720 } else if (emit->local_vtype[local_num] != vtype) {
721 // type of local is not the same as object stored in it
722 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);
723 }
724}
725
726static void emit_native_store_name(emit_t *emit, qstr qstr) {
727 // rt_store_name, but needs conversion of object (maybe have rt_viper_store_name(obj, type))
728 vtype_kind_t vtype;
729 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
730 assert(vtype == VTYPE_PYOBJ);
731 emit_call_with_imm_arg(emit, RT_F_STORE_NAME, rt_store_name, qstr, REG_ARG_1); // arg1 = name
732 emit_post(emit);
733}
734
735static void emit_native_store_global(emit_t *emit, qstr qstr) {
736 // not implemented
737 assert(0);
738}
739
740static void emit_native_store_deref(emit_t *emit, qstr qstr) {
741 // not implemented
742 assert(0);
743}
744
745static void emit_native_store_attr(emit_t *emit, qstr qstr) {
Damien7f5dacf2013-10-10 11:24:39 +0100746 vtype_kind_t vtype_base, vtype_val;
747 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
748 assert(vtype_base == VTYPE_PYOBJ);
749 assert(vtype_val == VTYPE_PYOBJ);
750 emit_call_with_imm_arg(emit, RT_F_STORE_ATTR, rt_store_attr, qstr, REG_ARG_2); // arg2 = attribute name
751 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +0100752}
753
Damien13ed3a62013-10-08 09:05:10 +0100754static void emit_native_store_subscr(emit_t *emit) {
755 // depends on type of subject:
756 // - integer, function, pointer to structure: error
757 // - pointer to integers: store as per array
758 // - Python object: call runtime with converted object or type info
759 vtype_kind_t vtype_index, vtype_base, vtype_value;
760 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
761 assert(vtype_index == VTYPE_PYOBJ);
762 assert(vtype_base == VTYPE_PYOBJ);
763 assert(vtype_value == VTYPE_PYOBJ);
764 emit_call(emit, RT_F_STORE_SUBSCR, rt_store_subscr);
765}
766
Damiena3977762013-10-09 23:10:10 +0100767static void emit_native_store_locals(emit_t *emit) {
768 // not needed
769 vtype_kind_t vtype;
770 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
771 emit_post(emit);
772}
773
Damien13ed3a62013-10-08 09:05:10 +0100774static void emit_native_delete_fast(emit_t *emit, qstr qstr, int local_num) {
775 // not implemented
776 // could support for Python types, just set to None (so GC can reclaim it)
777 assert(0);
778}
779
780static void emit_native_delete_name(emit_t *emit, qstr qstr) {
781 // not implemented
782 // use rt_delete_name
783 assert(0);
784}
785
786static void emit_native_delete_global(emit_t *emit, qstr qstr) {
787 // not implemented
788 // use rt_delete_global
789 assert(0);
790}
791
792static void emit_native_delete_deref(emit_t *emit, qstr qstr) {
793 // not supported
794 assert(0);
795}
796
797static void emit_native_delete_attr(emit_t *emit, qstr qstr) {
798 // not supported
799 assert(0);
800}
801
802static void emit_native_delete_subscr(emit_t *emit) {
803 // not supported
804 assert(0);
805}
806
807static void emit_native_dup_top(emit_t *emit) {
808 vtype_kind_t vtype;
809 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
810 emit_post_push_reg_reg(emit, vtype, REG_TEMP0, vtype, REG_TEMP0);
811}
812
813static void emit_native_dup_top_two(emit_t *emit) {
814 vtype_kind_t vtype0, vtype1;
815 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
816 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
817}
818
819static void emit_native_pop_top(emit_t *emit) {
820 vtype_kind_t vtype;
821 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
822 emit_post(emit);
823}
824
825static void emit_native_rot_two(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100826 vtype_kind_t vtype0, vtype1;
827 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
828 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +0100829}
830
831static void emit_native_rot_three(emit_t *emit) {
832 vtype_kind_t vtype0, vtype1, vtype2;
833 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
834 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
835}
836
837static void emit_native_jump(emit_t *emit, int label) {
838 emit_pre(emit);
839#if defined(N_X64)
840 asm_x64_jmp_label(emit->as, label);
841#elif defined(N_THUMB)
842 asm_thumb_b_label(emit->as, label);
843#endif
844 emit_post(emit);
845}
846
847static void emit_native_pop_jump_if_false(emit_t *emit, int label) {
848 vtype_kind_t vtype = peek_vtype(emit);
849 if (vtype == VTYPE_BOOL) {
850 emit_pre_pop_reg(emit, &vtype, REG_RET);
851 } else if (vtype == VTYPE_PYOBJ) {
852 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
853 emit_call(emit, RT_F_IS_TRUE, rt_is_true);
854 } else {
855 printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
856 assert(0);
857 }
858#if defined(N_X64)
859 asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
860 asm_x64_jcc_label(emit->as, JCC_JZ, label);
861#elif defined(N_THUMB)
862 asm_thumb_cmp_reg_bz_label(emit->as, REG_RET, label);
863#endif
864 emit_post(emit);
865}
866
867static void emit_native_pop_jump_if_true(emit_t *emit, int label) {
868 assert(0);
869}
870static void emit_native_jump_if_true_or_pop(emit_t *emit, int label) {
871 assert(0);
872}
873static void emit_native_jump_if_false_or_pop(emit_t *emit, int label) {
874 assert(0);
875}
876
877static void emit_native_setup_loop(emit_t *emit, int label) {
878 emit_pre(emit);
879 emit_post(emit);
880}
881
882static void emit_native_break_loop(emit_t *emit, int label) {
883 assert(0);
884}
885static void emit_native_continue_loop(emit_t *emit, int label) {
886 assert(0);
887}
888static void emit_native_setup_with(emit_t *emit, int label) {
889 // not supported, or could be with runtime call
890 assert(0);
891}
892static void emit_native_with_cleanup(emit_t *emit) {
893 assert(0);
894}
895static void emit_native_setup_except(emit_t *emit, int label) {
896 assert(0);
897}
898static void emit_native_setup_finally(emit_t *emit, int label) {
899 assert(0);
900}
901static void emit_native_end_finally(emit_t *emit) {
902 assert(0);
903}
904static void emit_native_get_iter(emit_t *emit) {
905 // perhaps the difficult one, as we want to rewrite for loops using native code
906 // in cases where we iterate over a Python object, can we use normal runtime calls?
907 assert(0);
908} // tos = getiter(tos)
909static void emit_native_for_iter(emit_t *emit, int label) {
910 assert(0);
911}
912static void emit_native_for_iter_end(emit_t *emit) {
913 assert(0);
914}
915
916static void emit_native_pop_block(emit_t *emit) {
917 emit_pre(emit);
918 emit_post(emit);
919}
920
921static void emit_native_pop_except(emit_t *emit) {
922 assert(0);
923}
924
925static void emit_native_unary_op(emit_t *emit, rt_unary_op_t op) {
926 vtype_kind_t vtype;
927 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
928 assert(vtype == VTYPE_PYOBJ);
929 emit_call_with_imm_arg(emit, RT_F_UNARY_OP, rt_unary_op, op, REG_ARG_1);
930 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
931}
932
933static void emit_native_binary_op(emit_t *emit, rt_binary_op_t op) {
934 vtype_kind_t vtype_lhs, vtype_rhs;
935 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
936 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
937 assert(op == RT_BINARY_OP_ADD);
938#if defined(N_X64)
939 asm_x64_add_r64_to_r64(emit->as, REG_ARG_3, REG_ARG_2);
940#elif defined(N_THUMB)
941 asm_thumb_add_reg_reg_reg(emit->as, REG_ARG_2, REG_ARG_2, REG_ARG_3);
942#endif
943 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
944 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
945 emit_call_with_imm_arg(emit, RT_F_BINARY_OP, rt_binary_op, op, REG_ARG_1);
946 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
947 } else {
948 printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs);
949 assert(0);
950 }
951}
952
953static void emit_native_compare_op(emit_t *emit, rt_compare_op_t op) {
954 vtype_kind_t vtype_lhs, vtype_rhs;
955 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
956 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
957 assert(op == RT_COMPARE_OP_LESS);
958#if defined(N_X64)
959 asm_x64_xor_r64_to_r64(emit->as, REG_RET, REG_RET);
960 asm_x64_cmp_r64_with_r64(emit->as, REG_ARG_3, REG_ARG_2);
961 asm_x64_setcc_r8(emit->as, JCC_JL, REG_RET);
962#elif defined(N_THUMB)
963 asm_thumb_cmp_reg_reg(emit->as, REG_ARG_2, REG_ARG_3);
964 asm_thumb_ite_ge(emit->as);
965 asm_thumb_movs_rlo_i8(emit->as, REG_RET, 0); // if r0 >= r1
966 asm_thumb_movs_rlo_i8(emit->as, REG_RET, 1); // if r0 < r1
967#endif
968 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
969 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
970 emit_call_with_imm_arg(emit, RT_F_COMPARE_OP, rt_compare_op, op, REG_ARG_1);
971 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
972 } else {
973 printf("ViperTypeError: can't do comparison between types %d and %d\n", vtype_lhs, vtype_rhs);
974 assert(0);
975 }
976}
977
978static void emit_native_build_tuple(emit_t *emit, int n_args) {
979 // call runtime, with types of args
980 // if wrapped in byte_array, or something, allocates memory and fills it
981 assert(0);
982}
983
984static void emit_native_build_list(emit_t *emit, int n_args) {
985 emit_pre(emit);
986 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items in reverse order
987 emit_call_with_imm_arg(emit, RT_F_BUILD_LIST, rt_build_list, n_args, REG_ARG_1);
988 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
989}
990
991static void emit_native_list_append(emit_t *emit, int list_index) {
992 // only used in list comprehension, so call runtime
993 assert(0);
994}
995
996static void emit_native_build_map(emit_t *emit, int n_args) {
997 emit_pre(emit);
998 emit_call_with_imm_arg(emit, RT_F_BUILD_MAP, rt_build_map, n_args, REG_ARG_1);
999 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
1000}
1001
1002static void emit_native_store_map(emit_t *emit) {
1003 vtype_kind_t vtype_key, vtype_value, vtype_map;
1004 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
1005 assert(vtype_key == VTYPE_PYOBJ);
1006 assert(vtype_value == VTYPE_PYOBJ);
1007 assert(vtype_map == VTYPE_PYOBJ);
1008 emit_call(emit, RT_F_STORE_MAP, rt_store_map);
1009 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
1010}
1011
1012static void emit_native_map_add(emit_t *emit, int map_index) {
1013 assert(0);
1014}
1015
1016static void emit_native_build_set(emit_t *emit, int n_args) {
1017 emit_pre(emit);
1018 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items in reverse order
1019 emit_call_with_imm_arg(emit, RT_F_BUILD_SET, rt_build_set, n_args, REG_ARG_1);
1020 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
1021}
1022
1023static void emit_native_set_add(emit_t *emit, int set_index) {
1024 assert(0);
1025}
1026static void emit_native_build_slice(emit_t *emit, int n_args) {
1027 assert(0);
1028}
1029static void emit_native_unpack_sequence(emit_t *emit, int n_args) {
1030 // call runtime, needs type decl
1031 assert(0);
1032}
1033static void emit_native_unpack_ex(emit_t *emit, int n_left, int n_right) {
1034 assert(0);
1035}
1036
1037static void emit_native_make_function(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
1038 // call runtime, with type info for args, or don't support dict/default params, or only support Python objects for them
1039 assert(n_default_params == 0 && n_dict_params == 0);
1040 emit_pre(emit);
1041 emit_call_with_imm_arg(emit, RT_F_MAKE_FUNCTION_FROM_ID, rt_make_function_from_id, scope->unique_code_id, REG_ARG_1);
1042 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1043}
1044
1045static void emit_native_make_closure(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
1046 assert(0);
1047}
1048
1049static void emit_native_call_function(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
1050 // call special viper runtime routine with type info for args, and wanted type info for return
1051 assert(n_keyword == 0 && !have_star_arg && !have_dbl_star_arg);
1052 if (n_positional == 0) {
1053 vtype_kind_t vtype_fun;
1054 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
1055 assert(vtype_fun == VTYPE_PYOBJ);
1056 emit_call(emit, RT_F_CALL_FUNCTION_0, rt_call_function_0);
1057 } else if (n_positional == 1) {
1058 vtype_kind_t vtype_fun, vtype_arg1;
1059 emit_pre_pop_reg_reg(emit, &vtype_arg1, REG_ARG_2, &vtype_fun, REG_ARG_1); // the single argument, the function
1060 assert(vtype_fun == VTYPE_PYOBJ);
1061 assert(vtype_arg1 == VTYPE_PYOBJ);
1062 emit_call(emit, RT_F_CALL_FUNCTION_1, rt_call_function_1);
1063 } else if (n_positional == 2) {
1064 vtype_kind_t vtype_fun, vtype_arg1, vtype_arg2;
1065 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
1066 assert(vtype_fun == VTYPE_PYOBJ);
1067 assert(vtype_arg1 == VTYPE_PYOBJ);
1068 assert(vtype_arg2 == VTYPE_PYOBJ);
1069 emit_call(emit, RT_F_CALL_FUNCTION_2, rt_call_function_2);
1070 } else {
1071 assert(0);
1072 }
1073 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1074}
1075
1076static void emit_native_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
1077 assert(n_keyword == 0 && !have_star_arg && !have_dbl_star_arg);
1078 if (n_positional == 0) {
1079 vtype_kind_t vtype_meth, vtype_self;
1080 emit_pre_pop_reg_reg(emit, &vtype_self, REG_ARG_2, &vtype_meth, REG_ARG_1); // the self object (or NULL), the method
1081 assert(vtype_meth == VTYPE_PYOBJ);
1082 assert(vtype_self == VTYPE_PYOBJ);
1083 emit_call(emit, RT_F_CALL_METHOD_1, rt_call_method_1);
1084 } else if (n_positional == 1) {
1085 vtype_kind_t vtype_meth, vtype_self, vtype_arg1;
1086 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
1087 assert(vtype_meth == VTYPE_PYOBJ);
1088 assert(vtype_self == VTYPE_PYOBJ);
1089 assert(vtype_arg1 == VTYPE_PYOBJ);
1090 emit_call(emit, RT_F_CALL_METHOD_2, rt_call_method_2);
1091 } else {
Damien7f5dacf2013-10-10 11:24:39 +01001092 emit_pre(emit);
1093 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
1094 emit_call_with_imm_arg(emit, RT_F_CALL_METHOD_N, rt_call_method_n, n_positional, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001095 }
1096 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1097}
1098
1099static void emit_native_return_value(emit_t *emit) {
1100 // easy. since we don't know who we return to, just return the raw value.
1101 // runtime needs then to know our type signature, but I think that's possible.
1102 vtype_kind_t vtype;
1103 emit_pre_pop_reg(emit, &vtype, REG_RET);
1104 if (emit->do_viper_types) {
1105 assert(vtype == VTYPE_PTR_NONE);
1106 } else {
1107 assert(vtype == VTYPE_PYOBJ);
1108 }
1109 emit->last_emit_was_return_value = true;
1110#if defined(N_X64)
1111 //asm_x64_call_ind(emit->as, 0, REG_RAX); to seg fault for debugging with gdb
1112 asm_x64_exit(emit->as);
1113#elif defined(N_THUMB)
1114 //asm_thumb_call_ind(emit->as, 0, REG_R0); to seg fault for debugging with gdb
1115 asm_thumb_exit(emit->as);
1116#endif
1117}
1118
1119static void emit_native_raise_varargs(emit_t *emit, int n_args) {
1120 // call runtime
1121 assert(0);
1122}
1123static void emit_native_yield_value(emit_t *emit) {
1124 // not supported (for now)
1125 assert(0);
1126}
1127static void emit_native_yield_from(emit_t *emit) {
1128 // not supported (for now)
1129 assert(0);
1130}
1131
1132const emit_method_table_t EXPORT_FUN(method_table) = {
1133 emit_native_set_viper_types,
1134 emit_native_start_pass,
1135 emit_native_end_pass,
1136 emit_native_last_emit_was_return_value,
1137 emit_native_get_stack_size,
1138 emit_native_set_stack_size,
1139
1140 emit_native_load_id,
1141 emit_native_store_id,
1142 emit_native_delete_id,
1143
1144 emit_native_label_assign,
1145 emit_native_import_name,
1146 emit_native_import_from,
1147 emit_native_import_star,
1148 emit_native_load_const_tok,
1149 emit_native_load_const_small_int,
1150 emit_native_load_const_int,
1151 emit_native_load_const_dec,
1152 emit_native_load_const_id,
1153 emit_native_load_const_str,
1154 emit_native_load_const_verbatim_start,
1155 emit_native_load_const_verbatim_int,
1156 emit_native_load_const_verbatim_str,
1157 emit_native_load_const_verbatim_strn,
1158 emit_native_load_const_verbatim_quoted_str,
1159 emit_native_load_const_verbatim_end,
1160 emit_native_load_fast,
1161 emit_native_load_name,
1162 emit_native_load_global,
1163 emit_native_load_deref,
1164 emit_native_load_closure,
1165 emit_native_load_attr,
1166 emit_native_load_method,
1167 emit_native_load_build_class,
1168 emit_native_store_fast,
1169 emit_native_store_name,
1170 emit_native_store_global,
1171 emit_native_store_deref,
1172 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01001173 emit_native_store_subscr,
Damiena3977762013-10-09 23:10:10 +01001174 emit_native_store_locals,
Damien13ed3a62013-10-08 09:05:10 +01001175 emit_native_delete_fast,
1176 emit_native_delete_name,
1177 emit_native_delete_global,
1178 emit_native_delete_deref,
1179 emit_native_delete_attr,
1180 emit_native_delete_subscr,
1181 emit_native_dup_top,
1182 emit_native_dup_top_two,
1183 emit_native_pop_top,
1184 emit_native_rot_two,
1185 emit_native_rot_three,
1186 emit_native_jump,
1187 emit_native_pop_jump_if_true,
1188 emit_native_pop_jump_if_false,
1189 emit_native_jump_if_true_or_pop,
1190 emit_native_jump_if_false_or_pop,
1191 emit_native_setup_loop,
1192 emit_native_break_loop,
1193 emit_native_continue_loop,
1194 emit_native_setup_with,
1195 emit_native_with_cleanup,
1196 emit_native_setup_except,
1197 emit_native_setup_finally,
1198 emit_native_end_finally,
1199 emit_native_get_iter,
1200 emit_native_for_iter,
1201 emit_native_for_iter_end,
1202 emit_native_pop_block,
1203 emit_native_pop_except,
1204 emit_native_unary_op,
1205 emit_native_binary_op,
1206 emit_native_compare_op,
1207 emit_native_build_tuple,
1208 emit_native_build_list,
1209 emit_native_list_append,
1210 emit_native_build_map,
1211 emit_native_store_map,
1212 emit_native_map_add,
1213 emit_native_build_set,
1214 emit_native_set_add,
1215 emit_native_build_slice,
1216 emit_native_unpack_sequence,
1217 emit_native_unpack_ex,
1218 emit_native_make_function,
1219 emit_native_make_closure,
1220 emit_native_call_function,
1221 emit_native_call_method,
1222 emit_native_return_value,
1223 emit_native_raise_varargs,
1224 emit_native_yield_value,
1225 emit_native_yield_from,
1226};
1227
1228#endif // defined(N_X64) || defined(N_THUMB)