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