Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1 | // 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" |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 27 | #include "mpyconfig.h" |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 28 | #include "lexer.h" |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 29 | #include "parse.h" |
| 30 | #include "scope.h" |
| 31 | #include "runtime.h" |
| 32 | #include "emit.h" |
| 33 | |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 34 | // wrapper around everything in this file |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 35 | #if N_X64 || N_THUMB |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 36 | |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 37 | #if N_X64 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 38 | |
| 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)) |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 53 | #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) |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 54 | #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 | |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 58 | #elif N_THUMB |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 59 | |
| 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)) |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 76 | #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) |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 77 | #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 | |
| 83 | typedef enum { |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 84 | STACK_VALUE, |
| 85 | STACK_REG, |
| 86 | STACK_IMM, |
| 87 | } stack_info_kind_t; |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 88 | |
| 89 | typedef 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 | |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 99 | typedef 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 | |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 108 | struct _emit_t { |
| 109 | int pass; |
| 110 | |
| 111 | bool do_viper_types; |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 112 | |
| 113 | int local_vtype_alloc; |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 114 | vtype_kind_t *local_vtype; |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 115 | |
| 116 | int stack_info_alloc; |
| 117 | stack_info_t *stack_info; |
| 118 | |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 119 | int stack_start; |
| 120 | int stack_size; |
| 121 | |
| 122 | bool last_emit_was_return_value; |
| 123 | |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 124 | scope_t *scope; |
| 125 | |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 126 | #if N_X64 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 127 | asm_x64_t *as; |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 128 | #elif N_THUMB |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 129 | asm_thumb_t *as; |
| 130 | #endif |
| 131 | }; |
| 132 | |
| 133 | emit_t *EXPORT_FUN(new)(uint max_num_labels) { |
| 134 | emit_t *emit = m_new(emit_t, 1); |
| 135 | emit->do_viper_types = false; |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 136 | emit->local_vtype = NULL; |
| 137 | emit->stack_info = NULL; |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 138 | #if N_X64 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 139 | emit->as = asm_x64_new(max_num_labels); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 140 | #elif N_THUMB |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 141 | emit->as = asm_thumb_new(max_num_labels); |
| 142 | #endif |
| 143 | return emit; |
| 144 | } |
| 145 | |
| 146 | static void emit_native_set_viper_types(emit_t *emit, bool do_viper_types) { |
| 147 | emit->do_viper_types = do_viper_types; |
| 148 | } |
| 149 | |
| 150 | static 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; |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 155 | emit->scope = scope; |
| 156 | |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 157 | 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); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | if (emit->do_viper_types) { |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 167 | // TODO set types of arguments based on type signature |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 168 | 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 | } |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 175 | } else { |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 176 | 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; |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 185 | #if N_X64 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 186 | asm_x64_start_pass(emit->as, pass); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 187 | #elif N_THUMB |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 188 | 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 | } |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 201 | #if N_X64 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 202 | asm_x64_entry(emit->as, num_locals); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 203 | #elif N_THUMB |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 204 | asm_thumb_entry(emit->as, num_locals); |
| 205 | #endif |
| 206 | |
| 207 | // initialise locals from parameters |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 208 | #if N_X64 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 209 | 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 | } |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 221 | #elif N_THUMB |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 222 | 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 | |
| 241 | static void emit_native_end_pass(emit_t *emit) { |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 242 | #if N_X64 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 243 | if (!emit->last_emit_was_return_value) { |
| 244 | asm_x64_exit(emit->as); |
| 245 | } |
| 246 | asm_x64_end_pass(emit->as); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 247 | #elif N_THUMB |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 248 | 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) { |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 260 | #if N_X64 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 261 | 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); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 263 | #elif N_THUMB |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 264 | 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 | |
| 270 | static bool emit_native_last_emit_was_return_value(emit_t *emit) { |
| 271 | return emit->last_emit_was_return_value; |
| 272 | } |
| 273 | |
| 274 | static int emit_native_get_stack_size(emit_t *emit) { |
| 275 | return emit->stack_size; |
| 276 | } |
| 277 | |
| 278 | static void emit_native_set_stack_size(emit_t *emit, int size) { |
| 279 | emit->stack_size = size; |
| 280 | } |
| 281 | |
| 282 | static 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 | |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 290 | /* |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 291 | static 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 | } |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 295 | */ |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 296 | |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 297 | // this must be called at start of emit functions |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 298 | static void emit_pre(emit_t *emit) { |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 299 | 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 | */ |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | static vtype_kind_t peek_vtype(emit_t *emit) { |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 325 | return emit->stack_info[emit->stack_size - 1].vtype; |
| 326 | } |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 327 | |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 328 | // pos=1 is TOS, pos=2 is next, etc |
| 329 | // use pos=0 for no skipping |
| 330 | static void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) { |
| 331 | skip_stack_pos = emit->stack_size - skip_stack_pos; |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 332 | for (int i = 0; i < emit->stack_size; i++) { |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 333 | 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 | } |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | } |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 342 | |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 343 | static void need_reg_all(emit_t *emit) { |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 344 | 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 | } |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 353 | static 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 |
| 370 | static 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]; |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 373 | *vtype = si->vtype; |
| 374 | switch (si->kind) { |
| 375 | case STACK_VALUE: |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 376 | ASM_MOV_LOCAL_TO_REG(emit->stack_start + emit->stack_size - pos, reg_dest); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 377 | break; |
| 378 | |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 379 | case STACK_REG: |
| 380 | if (si->u_reg != reg_dest) { |
| 381 | ASM_MOV_REG_TO_REG(si->u_reg, reg_dest); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 382 | } |
| 383 | break; |
| 384 | |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 385 | case STACK_IMM: |
| 386 | ASM_MOV_IMM_TO_REG(si->u_imm, reg_dest); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 387 | break; |
| 388 | } |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 389 | } |
| 390 | |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 391 | static 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 | |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 397 | static 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); |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 399 | emit_pre_pop_reg(emit, vtypeb, regb); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static 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); |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 404 | emit_pre_pop_reg(emit, vtypeb, regb); |
| 405 | emit_pre_pop_reg(emit, vtypec, regc); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static void emit_post(emit_t *emit) { |
| 409 | } |
| 410 | |
| 411 | static void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) { |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 412 | stack_info_t *si = &emit->stack_info[emit->stack_size]; |
| 413 | si->vtype = vtype; |
| 414 | si->kind = STACK_REG; |
| 415 | si->u_reg = reg; |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 416 | adjust_stack(emit, 1); |
| 417 | } |
| 418 | |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 419 | static 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 | |
| 427 | static 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 | |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 432 | static 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) { |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 433 | emit_post_push_reg(emit, vtypea, rega); |
| 434 | emit_post_push_reg(emit, vtypeb, regb); |
| 435 | emit_post_push_reg(emit, vtypec, regc); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | static 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) { |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 439 | 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); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // vtype of all n_pop objects is VTYPE_PYOBJ |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 446 | // does not use any temporary registers (but may use reg_dest before loading it with stack pointer) |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 447 | static void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, int reg_dest, int n_pop) { |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 448 | need_reg_all(emit); |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 449 | for (int i = 0; i < n_pop; i++) { |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 450 | 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); |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 458 | } |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 459 | 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 |
| 464 | static void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, int reg_dest, int n_push) { |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 465 | need_reg_all(emit); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 466 | for (int i = 0; i < n_push; i++) { |
Damien | 7f5dacf | 2013-10-10 11:24:39 +0100 | [diff] [blame] | 467 | emit->stack_info[emit->stack_size + i].kind = STACK_VALUE; |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 468 | emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ; |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 469 | } |
| 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 | |
| 474 | static void emit_call(emit_t *emit, rt_fun_kind_t fun_kind, void *fun) { |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 475 | need_reg_all(emit); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 476 | #if N_X64 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 477 | asm_x64_call_ind(emit->as, fun, REG_RAX); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 478 | #elif N_THUMB |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 479 | asm_thumb_bl_ind(emit->as, rt_fun_table[fun_kind], fun_kind, REG_R3); |
| 480 | #endif |
| 481 | } |
| 482 | |
| 483 | static 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) { |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 484 | need_reg_all(emit); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 485 | ASM_MOV_IMM_TO_REG(arg_val, arg_reg); |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 486 | #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 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static void emit_native_load_id(emit_t *emit, qstr qstr) { |
| 494 | // check for built-ins |
| 495 | if (strcmp(qstr_str(qstr), "v_int") == 0) { |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 496 | assert(0); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 497 | 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 | |
| 506 | static 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 | |
| 511 | static 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 | |
| 516 | static void emit_native_label_assign(emit_t *emit, int l) { |
Damien | 6ba1314 | 2013-11-02 20:34:54 +0000 | [diff] [blame] | 517 | emit_pre(emit); |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 518 | // need to commit stack because we can jump here from elsewhere |
| 519 | need_stack_settled(emit); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 520 | #if N_X64 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 521 | asm_x64_label_assign(emit->as, l); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 522 | #elif N_THUMB |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 523 | asm_thumb_label_assign(emit->as, l); |
| 524 | #endif |
Damien | 6ba1314 | 2013-11-02 20:34:54 +0000 | [diff] [blame] | 525 | emit_post(emit); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | static void emit_native_import_name(emit_t *emit, qstr qstr) { |
| 529 | // not implemented |
| 530 | assert(0); |
| 531 | } |
| 532 | |
| 533 | static void emit_native_import_from(emit_t *emit, qstr qstr) { |
| 534 | // not implemented |
| 535 | assert(0); |
| 536 | } |
| 537 | |
| 538 | static void emit_native_import_star(emit_t *emit) { |
| 539 | // not implemented |
| 540 | assert(0); |
| 541 | } |
| 542 | |
| 543 | static 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 | |
| 566 | static 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 | |
| 575 | static 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 | |
| 581 | static void emit_native_load_const_dec(emit_t *emit, qstr qstr) { |
Damien | 6ba1314 | 2013-11-02 20:34:54 +0000 | [diff] [blame] | 582 | // 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); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | static void emit_native_load_const_id(emit_t *emit, qstr qstr) { |
Damien | 7f5dacf | 2013-10-10 11:24:39 +0100 | [diff] [blame] | 589 | 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 | } |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | static 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 | |
| 611 | static void emit_native_load_const_verbatim_start(emit_t *emit) { |
| 612 | // not supported/needed for viper |
| 613 | assert(0); |
| 614 | } |
| 615 | |
| 616 | static void emit_native_load_const_verbatim_int(emit_t *emit, int val) { |
| 617 | // not supported/needed for viper |
| 618 | assert(0); |
| 619 | } |
| 620 | |
| 621 | static void emit_native_load_const_verbatim_str(emit_t *emit, const char *str) { |
| 622 | // not supported/needed for viper |
| 623 | assert(0); |
| 624 | } |
| 625 | |
| 626 | static 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 | |
| 631 | static 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 | |
| 636 | static void emit_native_load_const_verbatim_end(emit_t *emit) { |
| 637 | // not supported/needed for viper |
| 638 | assert(0); |
| 639 | } |
| 640 | |
| 641 | static 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); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 647 | #if N_X64 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 648 | if (local_num == 0) { |
| 649 | emit_post_push_reg(emit, vtype, REG_LOCAL_1); |
| 650 | } else { |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 651 | need_reg_single(emit, REG_RAX, 0); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 652 | asm_x64_mov_local_to_r64(emit->as, local_num - 1, REG_RAX); |
| 653 | emit_post_push_reg(emit, vtype, REG_RAX); |
| 654 | } |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 655 | #elif N_THUMB |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 656 | 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 { |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 663 | need_reg_single(emit, REG_R0, 0); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 664 | 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 | |
| 670 | static 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 | |
| 676 | static 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 | |
Damien | 27fb45e | 2013-10-20 15:07:49 +0100 | [diff] [blame] | 682 | static void emit_native_load_deref(emit_t *emit, qstr qstr, int local_num) { |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 683 | // 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 | |
Damien | 27fb45e | 2013-10-20 15:07:49 +0100 | [diff] [blame] | 688 | static void emit_native_load_closure(emit_t *emit, qstr qstr, int local_num) { |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 689 | // not implemented |
| 690 | assert(0); |
| 691 | } |
| 692 | |
| 693 | static 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 | |
| 705 | static 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 | |
| 713 | static void emit_native_load_build_class(emit_t *emit) { |
Damien | 7f5dacf | 2013-10-10 11:24:39 +0100 | [diff] [blame] | 714 | 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); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | static void emit_native_store_fast(emit_t *emit, qstr qstr, int local_num) { |
| 720 | vtype_kind_t vtype; |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 721 | #if N_X64 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 722 | 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 | } |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 728 | #elif N_THUMB |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 729 | 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 | |
| 753 | static 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 | |
| 762 | static void emit_native_store_global(emit_t *emit, qstr qstr) { |
| 763 | // not implemented |
| 764 | assert(0); |
| 765 | } |
| 766 | |
Damien | 27fb45e | 2013-10-20 15:07:49 +0100 | [diff] [blame] | 767 | static void emit_native_store_deref(emit_t *emit, qstr qstr, int local_num) { |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 768 | // not implemented |
| 769 | assert(0); |
| 770 | } |
| 771 | |
| 772 | static void emit_native_store_attr(emit_t *emit, qstr qstr) { |
Damien | 7f5dacf | 2013-10-10 11:24:39 +0100 | [diff] [blame] | 773 | 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); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 779 | } |
| 780 | |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 781 | static 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 | |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 794 | static 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 | |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 801 | static 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 | |
| 807 | static void emit_native_delete_name(emit_t *emit, qstr qstr) { |
| 808 | // not implemented |
| 809 | // use rt_delete_name |
| 810 | assert(0); |
| 811 | } |
| 812 | |
| 813 | static void emit_native_delete_global(emit_t *emit, qstr qstr) { |
| 814 | // not implemented |
| 815 | // use rt_delete_global |
| 816 | assert(0); |
| 817 | } |
| 818 | |
Damien | 27fb45e | 2013-10-20 15:07:49 +0100 | [diff] [blame] | 819 | static void emit_native_delete_deref(emit_t *emit, qstr qstr, int local_num) { |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 820 | // not supported |
| 821 | assert(0); |
| 822 | } |
| 823 | |
| 824 | static void emit_native_delete_attr(emit_t *emit, qstr qstr) { |
| 825 | // not supported |
| 826 | assert(0); |
| 827 | } |
| 828 | |
| 829 | static void emit_native_delete_subscr(emit_t *emit) { |
| 830 | // not supported |
| 831 | assert(0); |
| 832 | } |
| 833 | |
| 834 | static 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 | |
| 840 | static 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 | |
| 846 | static 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 | |
| 852 | static void emit_native_rot_two(emit_t *emit) { |
Damien | ff8ed77 | 2013-10-08 22:18:32 +0100 | [diff] [blame] | 853 | 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); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | static 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 | |
| 864 | static void emit_native_jump(emit_t *emit, int label) { |
| 865 | emit_pre(emit); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 866 | #if N_X64 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 867 | asm_x64_jmp_label(emit->as, label); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 868 | #elif N_THUMB |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 869 | asm_thumb_b_label(emit->as, label); |
| 870 | #endif |
| 871 | emit_post(emit); |
| 872 | } |
| 873 | |
Damien | 1a6633a | 2013-11-03 13:58:19 +0000 | [diff] [blame] | 874 | static void emit_native_pop_jump_pre_helper(emit_t *emit, int label) { |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 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 | } |
Damien | 1a6633a | 2013-11-03 13:58:19 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | static void emit_native_pop_jump_if_false(emit_t *emit, int label) { |
| 888 | emit_native_pop_jump_pre_helper(emit, label); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 889 | #if N_X64 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 890 | asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET); |
| 891 | asm_x64_jcc_label(emit->as, JCC_JZ, label); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 892 | #elif N_THUMB |
Damien | 1a6633a | 2013-11-03 13:58:19 +0000 | [diff] [blame] | 893 | asm_thumb_cmp_rlo_i8(emit->as, REG_RET, 0); |
| 894 | asm_thumb_bcc_label(emit->as, THUMB_CC_EQ, label); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 895 | #endif |
| 896 | emit_post(emit); |
| 897 | } |
| 898 | |
| 899 | static void emit_native_pop_jump_if_true(emit_t *emit, int label) { |
Damien | 1a6633a | 2013-11-03 13:58:19 +0000 | [diff] [blame] | 900 | emit_native_pop_jump_pre_helper(emit, label); |
| 901 | #if N_X64 |
| 902 | asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET); |
| 903 | asm_x64_jcc_label(emit->as, JCC_JNZ, label); |
| 904 | #elif N_THUMB |
| 905 | asm_thumb_cmp_rlo_i8(emit->as, REG_RET, 0); |
| 906 | asm_thumb_bcc_label(emit->as, THUMB_CC_NE, label); |
| 907 | #endif |
| 908 | emit_post(emit); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 909 | } |
Damien | 1a6633a | 2013-11-03 13:58:19 +0000 | [diff] [blame] | 910 | |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 911 | static void emit_native_jump_if_true_or_pop(emit_t *emit, int label) { |
| 912 | assert(0); |
| 913 | } |
| 914 | static void emit_native_jump_if_false_or_pop(emit_t *emit, int label) { |
| 915 | assert(0); |
| 916 | } |
| 917 | |
| 918 | static void emit_native_setup_loop(emit_t *emit, int label) { |
| 919 | emit_pre(emit); |
| 920 | emit_post(emit); |
| 921 | } |
| 922 | |
| 923 | static void emit_native_break_loop(emit_t *emit, int label) { |
Damien | 6ba1314 | 2013-11-02 20:34:54 +0000 | [diff] [blame] | 924 | emit_native_jump(emit, label); // TODO properly |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 925 | } |
| 926 | static void emit_native_continue_loop(emit_t *emit, int label) { |
| 927 | assert(0); |
| 928 | } |
| 929 | static void emit_native_setup_with(emit_t *emit, int label) { |
| 930 | // not supported, or could be with runtime call |
| 931 | assert(0); |
| 932 | } |
| 933 | static void emit_native_with_cleanup(emit_t *emit) { |
| 934 | assert(0); |
| 935 | } |
| 936 | static void emit_native_setup_except(emit_t *emit, int label) { |
| 937 | assert(0); |
| 938 | } |
| 939 | static void emit_native_setup_finally(emit_t *emit, int label) { |
| 940 | assert(0); |
| 941 | } |
| 942 | static void emit_native_end_finally(emit_t *emit) { |
| 943 | assert(0); |
| 944 | } |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 945 | |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 946 | static void emit_native_get_iter(emit_t *emit) { |
| 947 | // perhaps the difficult one, as we want to rewrite for loops using native code |
| 948 | // in cases where we iterate over a Python object, can we use normal runtime calls? |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 949 | |
| 950 | vtype_kind_t vtype; |
| 951 | emit_pre_pop_reg(emit, &vtype, REG_ARG_1); |
| 952 | assert(vtype == VTYPE_PYOBJ); |
| 953 | emit_call(emit, RT_F_GETITER, rt_getiter); |
| 954 | emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 955 | } |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 956 | |
| 957 | static void emit_native_for_iter(emit_t *emit, int label) { |
| 958 | emit_pre(emit); |
| 959 | vtype_kind_t vtype; |
| 960 | emit_access_stack(emit, 1, &vtype, REG_ARG_1); |
| 961 | assert(vtype == VTYPE_PYOBJ); |
| 962 | emit_call(emit, RT_F_ITERNEXT, rt_iternext); |
| 963 | ASM_MOV_IMM_TO_REG((machine_uint_t)py_const_stop_iteration, REG_TEMP1); |
| 964 | #if N_X64 |
| 965 | asm_x64_cmp_r64_with_r64(emit->as, REG_RET, REG_TEMP1); |
| 966 | asm_x64_jcc_label(emit->as, JCC_JE, label); |
| 967 | #elif N_THUMB |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 968 | asm_thumb_cmp_reg_reg(emit->as, REG_RET, REG_TEMP1); |
Damien | 9b9e996 | 2013-11-03 14:25:43 +0000 | [diff] [blame^] | 969 | asm_thumb_bcc_label(emit->as, THUMB_CC_EQ, label); |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 970 | #endif |
| 971 | emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); |
| 972 | } |
| 973 | |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 974 | static void emit_native_for_iter_end(emit_t *emit) { |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 975 | // adjust stack counter (we get here from for_iter ending, which popped the value for us) |
| 976 | emit_pre(emit); |
| 977 | adjust_stack(emit, -1); |
| 978 | emit_post(emit); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | static void emit_native_pop_block(emit_t *emit) { |
| 982 | emit_pre(emit); |
| 983 | emit_post(emit); |
| 984 | } |
| 985 | |
| 986 | static void emit_native_pop_except(emit_t *emit) { |
| 987 | assert(0); |
| 988 | } |
| 989 | |
| 990 | static void emit_native_unary_op(emit_t *emit, rt_unary_op_t op) { |
| 991 | vtype_kind_t vtype; |
| 992 | emit_pre_pop_reg(emit, &vtype, REG_ARG_2); |
| 993 | assert(vtype == VTYPE_PYOBJ); |
| 994 | emit_call_with_imm_arg(emit, RT_F_UNARY_OP, rt_unary_op, op, REG_ARG_1); |
| 995 | emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); |
| 996 | } |
| 997 | |
| 998 | static void emit_native_binary_op(emit_t *emit, rt_binary_op_t op) { |
| 999 | vtype_kind_t vtype_lhs, vtype_rhs; |
| 1000 | emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2); |
| 1001 | if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) { |
| 1002 | assert(op == RT_BINARY_OP_ADD); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 1003 | #if N_X64 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1004 | asm_x64_add_r64_to_r64(emit->as, REG_ARG_3, REG_ARG_2); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 1005 | #elif N_THUMB |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1006 | asm_thumb_add_reg_reg_reg(emit->as, REG_ARG_2, REG_ARG_2, REG_ARG_3); |
| 1007 | #endif |
| 1008 | emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2); |
| 1009 | } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) { |
| 1010 | emit_call_with_imm_arg(emit, RT_F_BINARY_OP, rt_binary_op, op, REG_ARG_1); |
| 1011 | emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); |
| 1012 | } else { |
| 1013 | printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs); |
| 1014 | assert(0); |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | static void emit_native_compare_op(emit_t *emit, rt_compare_op_t op) { |
| 1019 | vtype_kind_t vtype_lhs, vtype_rhs; |
| 1020 | emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2); |
| 1021 | if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) { |
| 1022 | assert(op == RT_COMPARE_OP_LESS); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 1023 | #if N_X64 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1024 | asm_x64_xor_r64_to_r64(emit->as, REG_RET, REG_RET); |
| 1025 | asm_x64_cmp_r64_with_r64(emit->as, REG_ARG_3, REG_ARG_2); |
| 1026 | asm_x64_setcc_r8(emit->as, JCC_JL, REG_RET); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 1027 | #elif N_THUMB |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1028 | asm_thumb_cmp_reg_reg(emit->as, REG_ARG_2, REG_ARG_3); |
| 1029 | asm_thumb_ite_ge(emit->as); |
| 1030 | asm_thumb_movs_rlo_i8(emit->as, REG_RET, 0); // if r0 >= r1 |
| 1031 | asm_thumb_movs_rlo_i8(emit->as, REG_RET, 1); // if r0 < r1 |
| 1032 | #endif |
| 1033 | emit_post_push_reg(emit, VTYPE_BOOL, REG_RET); |
| 1034 | } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) { |
| 1035 | emit_call_with_imm_arg(emit, RT_F_COMPARE_OP, rt_compare_op, op, REG_ARG_1); |
| 1036 | emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); |
| 1037 | } else { |
| 1038 | printf("ViperTypeError: can't do comparison between types %d and %d\n", vtype_lhs, vtype_rhs); |
| 1039 | assert(0); |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | static void emit_native_build_tuple(emit_t *emit, int n_args) { |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 1044 | // for viper: call runtime, with types of args |
| 1045 | // if wrapped in byte_array, or something, allocates memory and fills it |
| 1046 | emit_pre(emit); |
| 1047 | emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items in reverse order |
| 1048 | emit_call_with_imm_arg(emit, RT_F_BUILD_TUPLE, rt_build_tuple, n_args, REG_ARG_1); |
| 1049 | emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | static void emit_native_build_list(emit_t *emit, int n_args) { |
| 1053 | emit_pre(emit); |
| 1054 | emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items in reverse order |
| 1055 | emit_call_with_imm_arg(emit, RT_F_BUILD_LIST, rt_build_list, n_args, REG_ARG_1); |
| 1056 | emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list |
| 1057 | } |
| 1058 | |
| 1059 | static void emit_native_list_append(emit_t *emit, int list_index) { |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 1060 | // only used in list comprehension |
| 1061 | vtype_kind_t vtype_list, vtype_item; |
| 1062 | emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2); |
| 1063 | emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1); |
| 1064 | assert(vtype_list == VTYPE_PYOBJ); |
| 1065 | assert(vtype_item == VTYPE_PYOBJ); |
| 1066 | emit_call(emit, RT_F_LIST_APPEND, rt_list_append); |
| 1067 | emit_post(emit); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | static void emit_native_build_map(emit_t *emit, int n_args) { |
| 1071 | emit_pre(emit); |
| 1072 | emit_call_with_imm_arg(emit, RT_F_BUILD_MAP, rt_build_map, n_args, REG_ARG_1); |
| 1073 | emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map |
| 1074 | } |
| 1075 | |
| 1076 | static void emit_native_store_map(emit_t *emit) { |
| 1077 | vtype_kind_t vtype_key, vtype_value, vtype_map; |
| 1078 | emit_pre_pop_reg_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3, &vtype_map, REG_ARG_1); // key, value, map |
| 1079 | assert(vtype_key == VTYPE_PYOBJ); |
| 1080 | assert(vtype_value == VTYPE_PYOBJ); |
| 1081 | assert(vtype_map == VTYPE_PYOBJ); |
| 1082 | emit_call(emit, RT_F_STORE_MAP, rt_store_map); |
| 1083 | emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map |
| 1084 | } |
| 1085 | |
| 1086 | static void emit_native_map_add(emit_t *emit, int map_index) { |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 1087 | // only used in list comprehension |
| 1088 | vtype_kind_t vtype_map, vtype_key, vtype_value; |
| 1089 | emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3); |
| 1090 | emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1); |
| 1091 | assert(vtype_map == VTYPE_PYOBJ); |
| 1092 | assert(vtype_key == VTYPE_PYOBJ); |
| 1093 | assert(vtype_value == VTYPE_PYOBJ); |
| 1094 | emit_call(emit, RT_F_STORE_MAP, rt_store_map); |
| 1095 | emit_post(emit); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | static void emit_native_build_set(emit_t *emit, int n_args) { |
| 1099 | emit_pre(emit); |
| 1100 | emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items in reverse order |
| 1101 | emit_call_with_imm_arg(emit, RT_F_BUILD_SET, rt_build_set, n_args, REG_ARG_1); |
| 1102 | emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set |
| 1103 | } |
| 1104 | |
| 1105 | static void emit_native_set_add(emit_t *emit, int set_index) { |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 1106 | // only used in set comprehension |
| 1107 | vtype_kind_t vtype_set, vtype_item; |
| 1108 | emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2); |
| 1109 | emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1); |
| 1110 | assert(vtype_set == VTYPE_PYOBJ); |
| 1111 | assert(vtype_item == VTYPE_PYOBJ); |
| 1112 | emit_call(emit, RT_F_STORE_SET, rt_store_set); |
| 1113 | emit_post(emit); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1114 | } |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 1115 | |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1116 | static void emit_native_build_slice(emit_t *emit, int n_args) { |
| 1117 | assert(0); |
| 1118 | } |
| 1119 | static void emit_native_unpack_sequence(emit_t *emit, int n_args) { |
| 1120 | // call runtime, needs type decl |
| 1121 | assert(0); |
| 1122 | } |
| 1123 | static void emit_native_unpack_ex(emit_t *emit, int n_left, int n_right) { |
| 1124 | assert(0); |
| 1125 | } |
| 1126 | |
| 1127 | static void emit_native_make_function(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) { |
| 1128 | // call runtime, with type info for args, or don't support dict/default params, or only support Python objects for them |
| 1129 | assert(n_default_params == 0 && n_dict_params == 0); |
| 1130 | emit_pre(emit); |
| 1131 | emit_call_with_imm_arg(emit, RT_F_MAKE_FUNCTION_FROM_ID, rt_make_function_from_id, scope->unique_code_id, REG_ARG_1); |
| 1132 | emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); |
| 1133 | } |
| 1134 | |
| 1135 | static void emit_native_make_closure(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) { |
| 1136 | assert(0); |
| 1137 | } |
| 1138 | |
| 1139 | static void emit_native_call_function(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) { |
| 1140 | // call special viper runtime routine with type info for args, and wanted type info for return |
| 1141 | assert(n_keyword == 0 && !have_star_arg && !have_dbl_star_arg); |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1142 | /* |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1143 | if (n_positional == 0) { |
| 1144 | vtype_kind_t vtype_fun; |
| 1145 | emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function |
| 1146 | assert(vtype_fun == VTYPE_PYOBJ); |
| 1147 | emit_call(emit, RT_F_CALL_FUNCTION_0, rt_call_function_0); |
| 1148 | } else if (n_positional == 1) { |
| 1149 | vtype_kind_t vtype_fun, vtype_arg1; |
| 1150 | emit_pre_pop_reg_reg(emit, &vtype_arg1, REG_ARG_2, &vtype_fun, REG_ARG_1); // the single argument, the function |
| 1151 | assert(vtype_fun == VTYPE_PYOBJ); |
| 1152 | assert(vtype_arg1 == VTYPE_PYOBJ); |
| 1153 | emit_call(emit, RT_F_CALL_FUNCTION_1, rt_call_function_1); |
| 1154 | } else if (n_positional == 2) { |
| 1155 | vtype_kind_t vtype_fun, vtype_arg1, vtype_arg2; |
| 1156 | emit_pre_pop_reg_reg_reg(emit, &vtype_arg2, REG_ARG_3, &vtype_arg1, REG_ARG_2, &vtype_fun, REG_ARG_1); // the second argument, the first argument, the function |
| 1157 | assert(vtype_fun == VTYPE_PYOBJ); |
| 1158 | assert(vtype_arg1 == VTYPE_PYOBJ); |
| 1159 | assert(vtype_arg2 == VTYPE_PYOBJ); |
| 1160 | emit_call(emit, RT_F_CALL_FUNCTION_2, rt_call_function_2); |
| 1161 | } else { |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1162 | */ |
| 1163 | emit_pre(emit); |
| 1164 | if (n_positional != 0) { |
| 1165 | emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional); // pointer to args in reverse order |
| 1166 | } |
| 1167 | vtype_kind_t vtype_fun; |
| 1168 | emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function |
| 1169 | assert(vtype_fun == VTYPE_PYOBJ); |
| 1170 | emit_call_with_imm_arg(emit, RT_F_CALL_FUNCTION_N, rt_call_function_n, n_positional, REG_ARG_2); |
| 1171 | //} |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1172 | emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); |
| 1173 | } |
| 1174 | |
| 1175 | static void emit_native_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) { |
| 1176 | assert(n_keyword == 0 && !have_star_arg && !have_dbl_star_arg); |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1177 | /* |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1178 | if (n_positional == 0) { |
| 1179 | vtype_kind_t vtype_meth, vtype_self; |
| 1180 | emit_pre_pop_reg_reg(emit, &vtype_self, REG_ARG_2, &vtype_meth, REG_ARG_1); // the self object (or NULL), the method |
| 1181 | assert(vtype_meth == VTYPE_PYOBJ); |
| 1182 | assert(vtype_self == VTYPE_PYOBJ); |
| 1183 | emit_call(emit, RT_F_CALL_METHOD_1, rt_call_method_1); |
| 1184 | } else if (n_positional == 1) { |
| 1185 | vtype_kind_t vtype_meth, vtype_self, vtype_arg1; |
| 1186 | emit_pre_pop_reg_reg_reg(emit, &vtype_arg1, REG_ARG_3, &vtype_self, REG_ARG_2, &vtype_meth, REG_ARG_1); // the first argument, the self object (or NULL), the method |
| 1187 | assert(vtype_meth == VTYPE_PYOBJ); |
| 1188 | assert(vtype_self == VTYPE_PYOBJ); |
| 1189 | assert(vtype_arg1 == VTYPE_PYOBJ); |
| 1190 | emit_call(emit, RT_F_CALL_METHOD_2, rt_call_method_2); |
| 1191 | } else { |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1192 | */ |
Damien | 7f5dacf | 2013-10-10 11:24:39 +0100 | [diff] [blame] | 1193 | emit_pre(emit); |
| 1194 | emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_positional + 2); // pointer to items in reverse order, including meth and self |
| 1195 | emit_call_with_imm_arg(emit, RT_F_CALL_METHOD_N, rt_call_method_n, n_positional, REG_ARG_1); |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1196 | //} |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1197 | emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); |
| 1198 | } |
| 1199 | |
| 1200 | static void emit_native_return_value(emit_t *emit) { |
| 1201 | // easy. since we don't know who we return to, just return the raw value. |
| 1202 | // runtime needs then to know our type signature, but I think that's possible. |
| 1203 | vtype_kind_t vtype; |
| 1204 | emit_pre_pop_reg(emit, &vtype, REG_RET); |
| 1205 | if (emit->do_viper_types) { |
| 1206 | assert(vtype == VTYPE_PTR_NONE); |
| 1207 | } else { |
| 1208 | assert(vtype == VTYPE_PYOBJ); |
| 1209 | } |
| 1210 | emit->last_emit_was_return_value = true; |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 1211 | #if N_X64 |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1212 | //asm_x64_call_ind(emit->as, 0, REG_RAX); to seg fault for debugging with gdb |
| 1213 | asm_x64_exit(emit->as); |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 1214 | #elif N_THUMB |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1215 | //asm_thumb_call_ind(emit->as, 0, REG_R0); to seg fault for debugging with gdb |
| 1216 | asm_thumb_exit(emit->as); |
| 1217 | #endif |
| 1218 | } |
| 1219 | |
| 1220 | static void emit_native_raise_varargs(emit_t *emit, int n_args) { |
| 1221 | // call runtime |
| 1222 | assert(0); |
| 1223 | } |
| 1224 | static void emit_native_yield_value(emit_t *emit) { |
| 1225 | // not supported (for now) |
| 1226 | assert(0); |
| 1227 | } |
| 1228 | static void emit_native_yield_from(emit_t *emit) { |
| 1229 | // not supported (for now) |
| 1230 | assert(0); |
| 1231 | } |
| 1232 | |
| 1233 | const emit_method_table_t EXPORT_FUN(method_table) = { |
| 1234 | emit_native_set_viper_types, |
| 1235 | emit_native_start_pass, |
| 1236 | emit_native_end_pass, |
| 1237 | emit_native_last_emit_was_return_value, |
| 1238 | emit_native_get_stack_size, |
| 1239 | emit_native_set_stack_size, |
| 1240 | |
| 1241 | emit_native_load_id, |
| 1242 | emit_native_store_id, |
| 1243 | emit_native_delete_id, |
| 1244 | |
| 1245 | emit_native_label_assign, |
| 1246 | emit_native_import_name, |
| 1247 | emit_native_import_from, |
| 1248 | emit_native_import_star, |
| 1249 | emit_native_load_const_tok, |
| 1250 | emit_native_load_const_small_int, |
| 1251 | emit_native_load_const_int, |
| 1252 | emit_native_load_const_dec, |
| 1253 | emit_native_load_const_id, |
| 1254 | emit_native_load_const_str, |
| 1255 | emit_native_load_const_verbatim_start, |
| 1256 | emit_native_load_const_verbatim_int, |
| 1257 | emit_native_load_const_verbatim_str, |
| 1258 | emit_native_load_const_verbatim_strn, |
| 1259 | emit_native_load_const_verbatim_quoted_str, |
| 1260 | emit_native_load_const_verbatim_end, |
| 1261 | emit_native_load_fast, |
| 1262 | emit_native_load_name, |
| 1263 | emit_native_load_global, |
| 1264 | emit_native_load_deref, |
| 1265 | emit_native_load_closure, |
| 1266 | emit_native_load_attr, |
| 1267 | emit_native_load_method, |
| 1268 | emit_native_load_build_class, |
| 1269 | emit_native_store_fast, |
| 1270 | emit_native_store_name, |
| 1271 | emit_native_store_global, |
| 1272 | emit_native_store_deref, |
| 1273 | emit_native_store_attr, |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1274 | emit_native_store_subscr, |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 1275 | emit_native_store_locals, |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 1276 | emit_native_delete_fast, |
| 1277 | emit_native_delete_name, |
| 1278 | emit_native_delete_global, |
| 1279 | emit_native_delete_deref, |
| 1280 | emit_native_delete_attr, |
| 1281 | emit_native_delete_subscr, |
| 1282 | emit_native_dup_top, |
| 1283 | emit_native_dup_top_two, |
| 1284 | emit_native_pop_top, |
| 1285 | emit_native_rot_two, |
| 1286 | emit_native_rot_three, |
| 1287 | emit_native_jump, |
| 1288 | emit_native_pop_jump_if_true, |
| 1289 | emit_native_pop_jump_if_false, |
| 1290 | emit_native_jump_if_true_or_pop, |
| 1291 | emit_native_jump_if_false_or_pop, |
| 1292 | emit_native_setup_loop, |
| 1293 | emit_native_break_loop, |
| 1294 | emit_native_continue_loop, |
| 1295 | emit_native_setup_with, |
| 1296 | emit_native_with_cleanup, |
| 1297 | emit_native_setup_except, |
| 1298 | emit_native_setup_finally, |
| 1299 | emit_native_end_finally, |
| 1300 | emit_native_get_iter, |
| 1301 | emit_native_for_iter, |
| 1302 | emit_native_for_iter_end, |
| 1303 | emit_native_pop_block, |
| 1304 | emit_native_pop_except, |
| 1305 | emit_native_unary_op, |
| 1306 | emit_native_binary_op, |
| 1307 | emit_native_compare_op, |
| 1308 | emit_native_build_tuple, |
| 1309 | emit_native_build_list, |
| 1310 | emit_native_list_append, |
| 1311 | emit_native_build_map, |
| 1312 | emit_native_store_map, |
| 1313 | emit_native_map_add, |
| 1314 | emit_native_build_set, |
| 1315 | emit_native_set_add, |
| 1316 | emit_native_build_slice, |
| 1317 | emit_native_unpack_sequence, |
| 1318 | emit_native_unpack_ex, |
| 1319 | emit_native_make_function, |
| 1320 | emit_native_make_closure, |
| 1321 | emit_native_call_function, |
| 1322 | emit_native_call_method, |
| 1323 | emit_native_return_value, |
| 1324 | emit_native_raise_varargs, |
| 1325 | emit_native_yield_value, |
| 1326 | emit_native_yield_from, |
| 1327 | }; |
| 1328 | |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 1329 | #endif // N_X64 || N_THUMB |