blob: db375181af89d1758de2a737d7817a77cd4eac37 [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))
Damieneb19efb2013-10-10 22:06:54 +010062#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 +010063#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))
Damieneb19efb2013-10-10 22:06:54 +010085#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 +010086#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
Damieneb19efb2013-10-10 22:06:54 +0100347static void need_reg_all(emit_t *emit) {
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 }
355}
356
357static void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damienff8ed772013-10-08 22:18:32 +0100358 emit->last_emit_was_return_value = false;
359 adjust_stack(emit, -1);
360 need_reg_single(emit, reg_dest);
361 stack_info_t *si = &emit->stack_info[emit->stack_size];
362 *vtype = si->vtype;
363 switch (si->kind) {
364 case STACK_VALUE:
365 ASM_MOV_LOCAL_TO_REG(emit->stack_start + emit->stack_size, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100366 break;
367
Damienff8ed772013-10-08 22:18:32 +0100368 case STACK_REG:
369 if (si->u_reg != reg_dest) {
370 ASM_MOV_REG_TO_REG(si->u_reg, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100371 }
372 break;
373
Damienff8ed772013-10-08 22:18:32 +0100374 case STACK_IMM:
375 ASM_MOV_IMM_TO_REG(si->u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100376 break;
377 }
Damien13ed3a62013-10-08 09:05:10 +0100378}
379
380static void emit_pre_pop_reg_reg(emit_t *emit, vtype_kind_t *vtypea, int rega, vtype_kind_t *vtypeb, int regb) {
381 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100382 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +0100383}
384
385static 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) {
386 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100387 emit_pre_pop_reg(emit, vtypeb, regb);
388 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100389}
390
391static void emit_post(emit_t *emit) {
392}
393
394static void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +0100395 stack_info_t *si = &emit->stack_info[emit->stack_size];
396 si->vtype = vtype;
397 si->kind = STACK_REG;
398 si->u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +0100399 adjust_stack(emit, 1);
400}
401
Damienff8ed772013-10-08 22:18:32 +0100402static void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, machine_int_t imm) {
403 stack_info_t *si = &emit->stack_info[emit->stack_size];
404 si->vtype = vtype;
405 si->kind = STACK_IMM;
406 si->u_imm = imm;
407 adjust_stack(emit, 1);
408}
409
410static void emit_post_push_reg_reg(emit_t *emit, vtype_kind_t vtypea, int rega, vtype_kind_t vtypeb, int regb) {
411 emit_post_push_reg(emit, vtypea, rega);
412 emit_post_push_reg(emit, vtypeb, regb);
413}
414
Damien13ed3a62013-10-08 09:05:10 +0100415static 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 +0100416 emit_post_push_reg(emit, vtypea, rega);
417 emit_post_push_reg(emit, vtypeb, regb);
418 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100419}
420
421static 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 +0100422 emit_post_push_reg(emit, vtypea, rega);
423 emit_post_push_reg(emit, vtypeb, regb);
424 emit_post_push_reg(emit, vtypec, regc);
425 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +0100426}
427
428// vtype of all n_pop objects is VTYPE_PYOBJ
Damieneb19efb2013-10-10 22:06:54 +0100429// does not use any temporary registers (but may use reg_dest before loading it with stack pointer)
Damien13ed3a62013-10-08 09:05:10 +0100430static void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, int reg_dest, int n_pop) {
Damieneb19efb2013-10-10 22:06:54 +0100431 need_reg_all(emit);
Damienff8ed772013-10-08 22:18:32 +0100432 for (int i = 0; i < n_pop; i++) {
Damieneb19efb2013-10-10 22:06:54 +0100433 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
434 // must push any imm's to stack
435 if (si->kind == STACK_IMM) {
436 si->kind = STACK_VALUE;
437 ASM_MOV_IMM_TO_LOCAL_USING(si->u_imm, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
438 }
439 assert(si->kind == STACK_VALUE);
440 assert(si->vtype == VTYPE_PYOBJ);
Damienff8ed772013-10-08 22:18:32 +0100441 }
Damien13ed3a62013-10-08 09:05:10 +0100442 ASM_MOV_LOCAL_ADDR_TO_REG(emit->stack_start + emit->stack_size - 1, reg_dest);
443 adjust_stack(emit, -n_pop);
444}
445
446// vtype of all n_push objects is VTYPE_PYOBJ
447static void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, int reg_dest, int n_push) {
Damieneb19efb2013-10-10 22:06:54 +0100448 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +0100449 for (int i = 0; i < n_push; i++) {
Damien7f5dacf2013-10-10 11:24:39 +0100450 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
Damienff8ed772013-10-08 22:18:32 +0100451 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
Damien13ed3a62013-10-08 09:05:10 +0100452 }
453 ASM_MOV_LOCAL_ADDR_TO_REG(emit->stack_start + emit->stack_size + n_push - 1, reg_dest);
454 adjust_stack(emit, n_push);
455}
456
457static void emit_call(emit_t *emit, rt_fun_kind_t fun_kind, void *fun) {
458#if defined(N_X64)
459 asm_x64_call_ind(emit->as, fun, REG_RAX);
460#elif defined(N_THUMB)
461 asm_thumb_bl_ind(emit->as, rt_fun_table[fun_kind], fun_kind, REG_R3);
462#endif
463}
464
465static 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 +0100466 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +0100467 ASM_MOV_IMM_TO_REG(arg_val, arg_reg);
468 emit_call(emit, fun_kind, fun);
469}
470
471static void emit_native_load_id(emit_t *emit, qstr qstr) {
472 // check for built-ins
473 if (strcmp(qstr_str(qstr), "v_int") == 0) {
Damienff8ed772013-10-08 22:18:32 +0100474 assert(0);
Damien13ed3a62013-10-08 09:05:10 +0100475 emit_pre(emit);
476 //emit_post_push_blank(emit, VTYPE_BUILTIN_V_INT);
477
478 // not a built-in, so do usual thing
479 } else {
480 emit_common_load_id(emit, &EXPORT_FUN(method_table), emit->scope, qstr);
481 }
482}
483
484static void emit_native_store_id(emit_t *emit, qstr qstr) {
485 // TODO check for built-ins and disallow
486 emit_common_store_id(emit, &EXPORT_FUN(method_table), emit->scope, qstr);
487}
488
489static void emit_native_delete_id(emit_t *emit, qstr qstr) {
490 // TODO check for built-ins and disallow
491 emit_common_delete_id(emit, &EXPORT_FUN(method_table), emit->scope, qstr);
492}
493
494static void emit_native_label_assign(emit_t *emit, int l) {
495#if defined(N_X64)
496 asm_x64_label_assign(emit->as, l);
497#elif defined(N_THUMB)
498 asm_thumb_label_assign(emit->as, l);
499#endif
500}
501
502static void emit_native_import_name(emit_t *emit, qstr qstr) {
503 // not implemented
504 assert(0);
505}
506
507static void emit_native_import_from(emit_t *emit, qstr qstr) {
508 // not implemented
509 assert(0);
510}
511
512static void emit_native_import_star(emit_t *emit) {
513 // not implemented
514 assert(0);
515}
516
517static void emit_native_load_const_tok(emit_t *emit, py_token_kind_t tok) {
518 emit_pre(emit);
519 int vtype;
520 machine_uint_t val;
521 if (emit->do_viper_types) {
522 switch (tok) {
523 case PY_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
524 case PY_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
525 case PY_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
526 default: assert(0); vtype = 0; val = 0; // shouldn't happen
527 }
528 } else {
529 vtype = VTYPE_PYOBJ;
530 switch (tok) {
531 case PY_TOKEN_KW_NONE: val = (machine_uint_t)py_const_none; break;
532 case PY_TOKEN_KW_FALSE: val = (machine_uint_t)py_const_false; break;
533 case PY_TOKEN_KW_TRUE: val = (machine_uint_t)py_const_true; break;
534 default: assert(0); vtype = 0; val = 0; // shouldn't happen
535 }
536 }
537 emit_post_push_imm(emit, vtype, val);
538}
539
540static void emit_native_load_const_small_int(emit_t *emit, int arg) {
541 emit_pre(emit);
542 if (emit->do_viper_types) {
543 emit_post_push_imm(emit, VTYPE_INT, arg);
544 } else {
545 emit_post_push_imm(emit, VTYPE_PYOBJ, (arg << 1) | 1);
546 }
547}
548
549static void emit_native_load_const_int(emit_t *emit, qstr qstr) {
550 // not implemented
551 // load integer, check fits in 32 bits
552 assert(0);
553}
554
555static void emit_native_load_const_dec(emit_t *emit, qstr qstr) {
556 // not supported for viper (although, could support floats in future)
557 assert(0);
558}
559
560static void emit_native_load_const_id(emit_t *emit, qstr qstr) {
Damien7f5dacf2013-10-10 11:24:39 +0100561 emit_pre(emit);
562 if (emit->do_viper_types) {
563 assert(0);
564 } else {
565 emit_call_with_imm_arg(emit, RT_F_LOAD_CONST_STR, rt_load_const_str, qstr, REG_ARG_1); // TODO
566 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
567 }
Damien13ed3a62013-10-08 09:05:10 +0100568}
569
570static void emit_native_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
571 emit_pre(emit);
572 if (emit->do_viper_types) {
573 // not implemented properly
574 // load a pointer to the asciiz string?
575 assert(0);
576 emit_post_push_imm(emit, VTYPE_PTR, (machine_uint_t)qstr_str(qstr));
577 } else {
578 emit_call_with_imm_arg(emit, RT_F_LOAD_CONST_STR, rt_load_const_str, qstr, REG_ARG_1);
579 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
580 }
581}
582
583static void emit_native_load_const_verbatim_start(emit_t *emit) {
584 // not supported/needed for viper
585 assert(0);
586}
587
588static void emit_native_load_const_verbatim_int(emit_t *emit, int val) {
589 // not supported/needed for viper
590 assert(0);
591}
592
593static void emit_native_load_const_verbatim_str(emit_t *emit, const char *str) {
594 // not supported/needed for viper
595 assert(0);
596}
597
598static void emit_native_load_const_verbatim_strn(emit_t *emit, const char *str, int len) {
599 // not supported/needed for viper
600 assert(0);
601}
602
603static void emit_native_load_const_verbatim_quoted_str(emit_t *emit, qstr qstr, bool bytes) {
604 // not supported/needed for viper
605 assert(0);
606}
607
608static void emit_native_load_const_verbatim_end(emit_t *emit) {
609 // not supported/needed for viper
610 assert(0);
611}
612
613static void emit_native_load_fast(emit_t *emit, qstr qstr, int local_num) {
614 vtype_kind_t vtype = emit->local_vtype[local_num];
615 if (vtype == VTYPE_UNBOUND) {
616 printf("ViperTypeError: local %s used before type known\n", qstr_str(qstr));
617 }
618 emit_pre(emit);
619#if defined(N_X64)
620 if (local_num == 0) {
621 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
622 } else {
Damienff8ed772013-10-08 22:18:32 +0100623 need_reg_single(emit, REG_RAX);
Damien13ed3a62013-10-08 09:05:10 +0100624 asm_x64_mov_local_to_r64(emit->as, local_num - 1, REG_RAX);
625 emit_post_push_reg(emit, vtype, REG_RAX);
626 }
627#elif defined(N_THUMB)
628 if (local_num == 0) {
629 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
630 } else if (local_num == 1) {
631 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
632 } else if (local_num == 2) {
633 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
634 } else {
Damienff8ed772013-10-08 22:18:32 +0100635 need_reg_single(emit, REG_R0);
Damien13ed3a62013-10-08 09:05:10 +0100636 asm_thumb_mov_reg_local(emit->as, REG_R0, local_num - 1);
637 emit_post_push_reg(emit, vtype, REG_R0);
638 }
639#endif
640}
641
642static void emit_native_load_name(emit_t *emit, qstr qstr) {
643 emit_pre(emit);
644 emit_call_with_imm_arg(emit, RT_F_LOAD_NAME, rt_load_name, qstr, REG_ARG_1);
645 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
646}
647
648static void emit_native_load_global(emit_t *emit, qstr qstr) {
649 emit_pre(emit);
650 emit_call_with_imm_arg(emit, RT_F_LOAD_GLOBAL, rt_load_global, qstr, REG_ARG_1);
651 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
652}
653
654static void emit_native_load_deref(emit_t *emit, qstr qstr) {
655 // not implemented
656 // in principle could support this quite easily (ldr r0, [r0, #0]) and then get closed over variables!
657 assert(0);
658}
659
660static void emit_native_load_closure(emit_t *emit, qstr qstr) {
661 // not implemented
662 assert(0);
663}
664
665static void emit_native_load_attr(emit_t *emit, qstr qstr) {
666 // depends on type of subject:
667 // - integer, function, pointer to integers: error
668 // - pointer to structure: get member, quite easy
669 // - Python object: call rt_load_attr, and needs to be typed to convert result
670 vtype_kind_t vtype_base;
671 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
672 assert(vtype_base == VTYPE_PYOBJ);
673 emit_call_with_imm_arg(emit, RT_F_LOAD_ATTR, rt_load_attr, qstr, REG_ARG_2); // arg2 = attribute name
674 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
675}
676
677static void emit_native_load_method(emit_t *emit, qstr qstr) {
678 vtype_kind_t vtype_base;
679 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
680 assert(vtype_base == VTYPE_PYOBJ);
681 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
682 emit_call_with_imm_arg(emit, RT_F_LOAD_METHOD, rt_load_method, qstr, REG_ARG_2); // arg2 = method name
683}
684
685static void emit_native_load_build_class(emit_t *emit) {
Damien7f5dacf2013-10-10 11:24:39 +0100686 emit_pre(emit);
687 emit_call(emit, RT_F_LOAD_BUILD_CLASS, rt_load_build_class);
688 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +0100689}
690
691static void emit_native_store_fast(emit_t *emit, qstr qstr, int local_num) {
692 vtype_kind_t vtype;
693#if defined(N_X64)
694 if (local_num == 0) {
695 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
696 } else {
697 emit_pre_pop_reg(emit, &vtype, REG_RAX);
698 asm_x64_mov_r64_to_local(emit->as, REG_RAX, local_num - 1);
699 }
700#elif defined(N_THUMB)
701 if (local_num == 0) {
702 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
703 } else if (local_num == 1) {
704 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
705 } else if (local_num == 2) {
706 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
707 } else {
708 emit_pre_pop_reg(emit, &vtype, REG_R0);
709 asm_thumb_mov_local_reg(emit->as, local_num - 1, REG_R0);
710 }
711#endif
712
713 emit_post(emit);
714
715 // check types
716 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
717 // first time this local is assigned, so give it a type of the object stored in it
718 emit->local_vtype[local_num] = vtype;
719 } else if (emit->local_vtype[local_num] != vtype) {
720 // type of local is not the same as object stored in it
721 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);
722 }
723}
724
725static void emit_native_store_name(emit_t *emit, qstr qstr) {
726 // rt_store_name, but needs conversion of object (maybe have rt_viper_store_name(obj, type))
727 vtype_kind_t vtype;
728 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
729 assert(vtype == VTYPE_PYOBJ);
730 emit_call_with_imm_arg(emit, RT_F_STORE_NAME, rt_store_name, qstr, REG_ARG_1); // arg1 = name
731 emit_post(emit);
732}
733
734static void emit_native_store_global(emit_t *emit, qstr qstr) {
735 // not implemented
736 assert(0);
737}
738
739static void emit_native_store_deref(emit_t *emit, qstr qstr) {
740 // not implemented
741 assert(0);
742}
743
744static void emit_native_store_attr(emit_t *emit, qstr qstr) {
Damien7f5dacf2013-10-10 11:24:39 +0100745 vtype_kind_t vtype_base, vtype_val;
746 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
747 assert(vtype_base == VTYPE_PYOBJ);
748 assert(vtype_val == VTYPE_PYOBJ);
749 emit_call_with_imm_arg(emit, RT_F_STORE_ATTR, rt_store_attr, qstr, REG_ARG_2); // arg2 = attribute name
750 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +0100751}
752
Damien13ed3a62013-10-08 09:05:10 +0100753static void emit_native_store_subscr(emit_t *emit) {
754 // depends on type of subject:
755 // - integer, function, pointer to structure: error
756 // - pointer to integers: store as per array
757 // - Python object: call runtime with converted object or type info
758 vtype_kind_t vtype_index, vtype_base, vtype_value;
759 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
760 assert(vtype_index == VTYPE_PYOBJ);
761 assert(vtype_base == VTYPE_PYOBJ);
762 assert(vtype_value == VTYPE_PYOBJ);
763 emit_call(emit, RT_F_STORE_SUBSCR, rt_store_subscr);
764}
765
Damiena3977762013-10-09 23:10:10 +0100766static void emit_native_store_locals(emit_t *emit) {
767 // not needed
768 vtype_kind_t vtype;
769 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
770 emit_post(emit);
771}
772
Damien13ed3a62013-10-08 09:05:10 +0100773static void emit_native_delete_fast(emit_t *emit, qstr qstr, int local_num) {
774 // not implemented
775 // could support for Python types, just set to None (so GC can reclaim it)
776 assert(0);
777}
778
779static void emit_native_delete_name(emit_t *emit, qstr qstr) {
780 // not implemented
781 // use rt_delete_name
782 assert(0);
783}
784
785static void emit_native_delete_global(emit_t *emit, qstr qstr) {
786 // not implemented
787 // use rt_delete_global
788 assert(0);
789}
790
791static void emit_native_delete_deref(emit_t *emit, qstr qstr) {
792 // not supported
793 assert(0);
794}
795
796static void emit_native_delete_attr(emit_t *emit, qstr qstr) {
797 // not supported
798 assert(0);
799}
800
801static void emit_native_delete_subscr(emit_t *emit) {
802 // not supported
803 assert(0);
804}
805
806static void emit_native_dup_top(emit_t *emit) {
807 vtype_kind_t vtype;
808 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
809 emit_post_push_reg_reg(emit, vtype, REG_TEMP0, vtype, REG_TEMP0);
810}
811
812static void emit_native_dup_top_two(emit_t *emit) {
813 vtype_kind_t vtype0, vtype1;
814 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
815 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
816}
817
818static void emit_native_pop_top(emit_t *emit) {
819 vtype_kind_t vtype;
820 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
821 emit_post(emit);
822}
823
824static void emit_native_rot_two(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100825 vtype_kind_t vtype0, vtype1;
826 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
827 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +0100828}
829
830static void emit_native_rot_three(emit_t *emit) {
831 vtype_kind_t vtype0, vtype1, vtype2;
832 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
833 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
834}
835
836static void emit_native_jump(emit_t *emit, int label) {
837 emit_pre(emit);
838#if defined(N_X64)
839 asm_x64_jmp_label(emit->as, label);
840#elif defined(N_THUMB)
841 asm_thumb_b_label(emit->as, label);
842#endif
843 emit_post(emit);
844}
845
846static void emit_native_pop_jump_if_false(emit_t *emit, int label) {
847 vtype_kind_t vtype = peek_vtype(emit);
848 if (vtype == VTYPE_BOOL) {
849 emit_pre_pop_reg(emit, &vtype, REG_RET);
850 } else if (vtype == VTYPE_PYOBJ) {
851 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
852 emit_call(emit, RT_F_IS_TRUE, rt_is_true);
853 } else {
854 printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
855 assert(0);
856 }
857#if defined(N_X64)
858 asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
859 asm_x64_jcc_label(emit->as, JCC_JZ, label);
860#elif defined(N_THUMB)
861 asm_thumb_cmp_reg_bz_label(emit->as, REG_RET, label);
862#endif
863 emit_post(emit);
864}
865
866static void emit_native_pop_jump_if_true(emit_t *emit, int label) {
867 assert(0);
868}
869static void emit_native_jump_if_true_or_pop(emit_t *emit, int label) {
870 assert(0);
871}
872static void emit_native_jump_if_false_or_pop(emit_t *emit, int label) {
873 assert(0);
874}
875
876static void emit_native_setup_loop(emit_t *emit, int label) {
877 emit_pre(emit);
878 emit_post(emit);
879}
880
881static void emit_native_break_loop(emit_t *emit, int label) {
882 assert(0);
883}
884static void emit_native_continue_loop(emit_t *emit, int label) {
885 assert(0);
886}
887static void emit_native_setup_with(emit_t *emit, int label) {
888 // not supported, or could be with runtime call
889 assert(0);
890}
891static void emit_native_with_cleanup(emit_t *emit) {
892 assert(0);
893}
894static void emit_native_setup_except(emit_t *emit, int label) {
895 assert(0);
896}
897static void emit_native_setup_finally(emit_t *emit, int label) {
898 assert(0);
899}
900static void emit_native_end_finally(emit_t *emit) {
901 assert(0);
902}
903static void emit_native_get_iter(emit_t *emit) {
904 // perhaps the difficult one, as we want to rewrite for loops using native code
905 // in cases where we iterate over a Python object, can we use normal runtime calls?
906 assert(0);
907} // tos = getiter(tos)
908static void emit_native_for_iter(emit_t *emit, int label) {
909 assert(0);
910}
911static void emit_native_for_iter_end(emit_t *emit) {
912 assert(0);
913}
914
915static void emit_native_pop_block(emit_t *emit) {
916 emit_pre(emit);
917 emit_post(emit);
918}
919
920static void emit_native_pop_except(emit_t *emit) {
921 assert(0);
922}
923
924static void emit_native_unary_op(emit_t *emit, rt_unary_op_t op) {
925 vtype_kind_t vtype;
926 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
927 assert(vtype == VTYPE_PYOBJ);
928 emit_call_with_imm_arg(emit, RT_F_UNARY_OP, rt_unary_op, op, REG_ARG_1);
929 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
930}
931
932static void emit_native_binary_op(emit_t *emit, rt_binary_op_t op) {
933 vtype_kind_t vtype_lhs, vtype_rhs;
934 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
935 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
936 assert(op == RT_BINARY_OP_ADD);
937#if defined(N_X64)
938 asm_x64_add_r64_to_r64(emit->as, REG_ARG_3, REG_ARG_2);
939#elif defined(N_THUMB)
940 asm_thumb_add_reg_reg_reg(emit->as, REG_ARG_2, REG_ARG_2, REG_ARG_3);
941#endif
942 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
943 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
944 emit_call_with_imm_arg(emit, RT_F_BINARY_OP, rt_binary_op, op, REG_ARG_1);
945 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
946 } else {
947 printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs);
948 assert(0);
949 }
950}
951
952static void emit_native_compare_op(emit_t *emit, rt_compare_op_t op) {
953 vtype_kind_t vtype_lhs, vtype_rhs;
954 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
955 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
956 assert(op == RT_COMPARE_OP_LESS);
957#if defined(N_X64)
958 asm_x64_xor_r64_to_r64(emit->as, REG_RET, REG_RET);
959 asm_x64_cmp_r64_with_r64(emit->as, REG_ARG_3, REG_ARG_2);
960 asm_x64_setcc_r8(emit->as, JCC_JL, REG_RET);
961#elif defined(N_THUMB)
962 asm_thumb_cmp_reg_reg(emit->as, REG_ARG_2, REG_ARG_3);
963 asm_thumb_ite_ge(emit->as);
964 asm_thumb_movs_rlo_i8(emit->as, REG_RET, 0); // if r0 >= r1
965 asm_thumb_movs_rlo_i8(emit->as, REG_RET, 1); // if r0 < r1
966#endif
967 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
968 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
969 emit_call_with_imm_arg(emit, RT_F_COMPARE_OP, rt_compare_op, op, REG_ARG_1);
970 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
971 } else {
972 printf("ViperTypeError: can't do comparison between types %d and %d\n", vtype_lhs, vtype_rhs);
973 assert(0);
974 }
975}
976
977static void emit_native_build_tuple(emit_t *emit, int n_args) {
978 // call runtime, with types of args
979 // if wrapped in byte_array, or something, allocates memory and fills it
980 assert(0);
981}
982
983static void emit_native_build_list(emit_t *emit, int n_args) {
984 emit_pre(emit);
985 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items in reverse order
986 emit_call_with_imm_arg(emit, RT_F_BUILD_LIST, rt_build_list, n_args, REG_ARG_1);
987 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
988}
989
990static void emit_native_list_append(emit_t *emit, int list_index) {
991 // only used in list comprehension, so call runtime
992 assert(0);
993}
994
995static void emit_native_build_map(emit_t *emit, int n_args) {
996 emit_pre(emit);
997 emit_call_with_imm_arg(emit, RT_F_BUILD_MAP, rt_build_map, n_args, REG_ARG_1);
998 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
999}
1000
1001static void emit_native_store_map(emit_t *emit) {
1002 vtype_kind_t vtype_key, vtype_value, vtype_map;
1003 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
1004 assert(vtype_key == VTYPE_PYOBJ);
1005 assert(vtype_value == VTYPE_PYOBJ);
1006 assert(vtype_map == VTYPE_PYOBJ);
1007 emit_call(emit, RT_F_STORE_MAP, rt_store_map);
1008 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
1009}
1010
1011static void emit_native_map_add(emit_t *emit, int map_index) {
1012 assert(0);
1013}
1014
1015static void emit_native_build_set(emit_t *emit, int n_args) {
1016 emit_pre(emit);
1017 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items in reverse order
1018 emit_call_with_imm_arg(emit, RT_F_BUILD_SET, rt_build_set, n_args, REG_ARG_1);
1019 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
1020}
1021
1022static void emit_native_set_add(emit_t *emit, int set_index) {
1023 assert(0);
1024}
1025static void emit_native_build_slice(emit_t *emit, int n_args) {
1026 assert(0);
1027}
1028static void emit_native_unpack_sequence(emit_t *emit, int n_args) {
1029 // call runtime, needs type decl
1030 assert(0);
1031}
1032static void emit_native_unpack_ex(emit_t *emit, int n_left, int n_right) {
1033 assert(0);
1034}
1035
1036static void emit_native_make_function(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
1037 // call runtime, with type info for args, or don't support dict/default params, or only support Python objects for them
1038 assert(n_default_params == 0 && n_dict_params == 0);
1039 emit_pre(emit);
1040 emit_call_with_imm_arg(emit, RT_F_MAKE_FUNCTION_FROM_ID, rt_make_function_from_id, scope->unique_code_id, REG_ARG_1);
1041 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1042}
1043
1044static void emit_native_make_closure(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
1045 assert(0);
1046}
1047
1048static void emit_native_call_function(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
1049 // call special viper runtime routine with type info for args, and wanted type info for return
1050 assert(n_keyword == 0 && !have_star_arg && !have_dbl_star_arg);
Damieneb19efb2013-10-10 22:06:54 +01001051 /*
Damien13ed3a62013-10-08 09:05:10 +01001052 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 {
Damieneb19efb2013-10-10 22:06:54 +01001071 */
1072 emit_pre(emit);
1073 if (n_positional != 0) {
1074 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional); // pointer to args in reverse order
1075 }
1076 vtype_kind_t vtype_fun;
1077 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
1078 assert(vtype_fun == VTYPE_PYOBJ);
1079 emit_call_with_imm_arg(emit, RT_F_CALL_FUNCTION_N, rt_call_function_n, n_positional, REG_ARG_2);
1080 //}
Damien13ed3a62013-10-08 09:05:10 +01001081 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1082}
1083
1084static void emit_native_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
1085 assert(n_keyword == 0 && !have_star_arg && !have_dbl_star_arg);
Damieneb19efb2013-10-10 22:06:54 +01001086 /*
Damien13ed3a62013-10-08 09:05:10 +01001087 if (n_positional == 0) {
1088 vtype_kind_t vtype_meth, vtype_self;
1089 emit_pre_pop_reg_reg(emit, &vtype_self, REG_ARG_2, &vtype_meth, REG_ARG_1); // the self object (or NULL), the method
1090 assert(vtype_meth == VTYPE_PYOBJ);
1091 assert(vtype_self == VTYPE_PYOBJ);
1092 emit_call(emit, RT_F_CALL_METHOD_1, rt_call_method_1);
1093 } else if (n_positional == 1) {
1094 vtype_kind_t vtype_meth, vtype_self, vtype_arg1;
1095 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
1096 assert(vtype_meth == VTYPE_PYOBJ);
1097 assert(vtype_self == VTYPE_PYOBJ);
1098 assert(vtype_arg1 == VTYPE_PYOBJ);
1099 emit_call(emit, RT_F_CALL_METHOD_2, rt_call_method_2);
1100 } else {
Damieneb19efb2013-10-10 22:06:54 +01001101 */
Damien7f5dacf2013-10-10 11:24:39 +01001102 emit_pre(emit);
1103 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
1104 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 +01001105 //}
Damien13ed3a62013-10-08 09:05:10 +01001106 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1107}
1108
1109static void emit_native_return_value(emit_t *emit) {
1110 // easy. since we don't know who we return to, just return the raw value.
1111 // runtime needs then to know our type signature, but I think that's possible.
1112 vtype_kind_t vtype;
1113 emit_pre_pop_reg(emit, &vtype, REG_RET);
1114 if (emit->do_viper_types) {
1115 assert(vtype == VTYPE_PTR_NONE);
1116 } else {
1117 assert(vtype == VTYPE_PYOBJ);
1118 }
1119 emit->last_emit_was_return_value = true;
1120#if defined(N_X64)
1121 //asm_x64_call_ind(emit->as, 0, REG_RAX); to seg fault for debugging with gdb
1122 asm_x64_exit(emit->as);
1123#elif defined(N_THUMB)
1124 //asm_thumb_call_ind(emit->as, 0, REG_R0); to seg fault for debugging with gdb
1125 asm_thumb_exit(emit->as);
1126#endif
1127}
1128
1129static void emit_native_raise_varargs(emit_t *emit, int n_args) {
1130 // call runtime
1131 assert(0);
1132}
1133static void emit_native_yield_value(emit_t *emit) {
1134 // not supported (for now)
1135 assert(0);
1136}
1137static void emit_native_yield_from(emit_t *emit) {
1138 // not supported (for now)
1139 assert(0);
1140}
1141
1142const emit_method_table_t EXPORT_FUN(method_table) = {
1143 emit_native_set_viper_types,
1144 emit_native_start_pass,
1145 emit_native_end_pass,
1146 emit_native_last_emit_was_return_value,
1147 emit_native_get_stack_size,
1148 emit_native_set_stack_size,
1149
1150 emit_native_load_id,
1151 emit_native_store_id,
1152 emit_native_delete_id,
1153
1154 emit_native_label_assign,
1155 emit_native_import_name,
1156 emit_native_import_from,
1157 emit_native_import_star,
1158 emit_native_load_const_tok,
1159 emit_native_load_const_small_int,
1160 emit_native_load_const_int,
1161 emit_native_load_const_dec,
1162 emit_native_load_const_id,
1163 emit_native_load_const_str,
1164 emit_native_load_const_verbatim_start,
1165 emit_native_load_const_verbatim_int,
1166 emit_native_load_const_verbatim_str,
1167 emit_native_load_const_verbatim_strn,
1168 emit_native_load_const_verbatim_quoted_str,
1169 emit_native_load_const_verbatim_end,
1170 emit_native_load_fast,
1171 emit_native_load_name,
1172 emit_native_load_global,
1173 emit_native_load_deref,
1174 emit_native_load_closure,
1175 emit_native_load_attr,
1176 emit_native_load_method,
1177 emit_native_load_build_class,
1178 emit_native_store_fast,
1179 emit_native_store_name,
1180 emit_native_store_global,
1181 emit_native_store_deref,
1182 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01001183 emit_native_store_subscr,
Damiena3977762013-10-09 23:10:10 +01001184 emit_native_store_locals,
Damien13ed3a62013-10-08 09:05:10 +01001185 emit_native_delete_fast,
1186 emit_native_delete_name,
1187 emit_native_delete_global,
1188 emit_native_delete_deref,
1189 emit_native_delete_attr,
1190 emit_native_delete_subscr,
1191 emit_native_dup_top,
1192 emit_native_dup_top_two,
1193 emit_native_pop_top,
1194 emit_native_rot_two,
1195 emit_native_rot_three,
1196 emit_native_jump,
1197 emit_native_pop_jump_if_true,
1198 emit_native_pop_jump_if_false,
1199 emit_native_jump_if_true_or_pop,
1200 emit_native_jump_if_false_or_pop,
1201 emit_native_setup_loop,
1202 emit_native_break_loop,
1203 emit_native_continue_loop,
1204 emit_native_setup_with,
1205 emit_native_with_cleanup,
1206 emit_native_setup_except,
1207 emit_native_setup_finally,
1208 emit_native_end_finally,
1209 emit_native_get_iter,
1210 emit_native_for_iter,
1211 emit_native_for_iter_end,
1212 emit_native_pop_block,
1213 emit_native_pop_except,
1214 emit_native_unary_op,
1215 emit_native_binary_op,
1216 emit_native_compare_op,
1217 emit_native_build_tuple,
1218 emit_native_build_list,
1219 emit_native_list_append,
1220 emit_native_build_map,
1221 emit_native_store_map,
1222 emit_native_map_add,
1223 emit_native_build_set,
1224 emit_native_set_add,
1225 emit_native_build_slice,
1226 emit_native_unpack_sequence,
1227 emit_native_unpack_ex,
1228 emit_native_make_function,
1229 emit_native_make_closure,
1230 emit_native_call_function,
1231 emit_native_call_method,
1232 emit_native_return_value,
1233 emit_native_raise_varargs,
1234 emit_native_yield_value,
1235 emit_native_yield_from,
1236};
1237
1238#endif // defined(N_X64) || defined(N_THUMB)