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