Skip to content

Commit 7f5dacf

Browse files
committed
Implement basic class/object in native code.
1 parent a397776 commit 7f5dacf

4 files changed

Lines changed: 57 additions & 21 deletions

File tree

py/emitbc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,9 @@ static void emit_bc_store_subscr(emit_t *emit) {
354354
}
355355

356356
static void emit_bc_store_locals(emit_t *emit) {
357-
// not needed for byte code
357+
// not needed
358358
emit_pre(emit, -1);
359+
emit_write_byte_1(emit, PYBC_POP_TOP);
359360
}
360361

361362
static void emit_bc_delete_fast(emit_t *emit, qstr qstr, int local_num) {

py/emitnative.c

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,22 @@ static void need_reg_single(emit_t *emit, int reg_needed) {
344344
}
345345
}
346346

347-
static void need_reg_all(emit_t *emit) {
347+
static void need_reg_all(emit_t *emit, int num_stack_top_that_must_be_value) {
348348
for (int i = 0; i < emit->stack_size; i++) {
349349
stack_info_t *si = &emit->stack_info[i];
350350
if (si->kind == STACK_REG) {
351351
si->kind = STACK_VALUE;
352352
ASM_MOV_REG_TO_LOCAL(si->u_reg, emit->stack_start + i);
353353
}
354354
}
355+
// must do this after making all registers available because ASM_MOV_IMM_TO_LOCAL uses a temporary register
356+
for (int i = 0; i < num_stack_top_that_must_be_value; i++) {
357+
stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
358+
if (si->kind == STACK_IMM) {
359+
si->kind = STACK_VALUE;
360+
ASM_MOV_IMM_TO_LOCAL(si->u_imm, emit->stack_start + emit->stack_size - 1 - i);
361+
}
362+
}
355363
}
356364

357365
static void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
@@ -427,18 +435,20 @@ static void emit_post_push_reg_reg_reg_reg(emit_t *emit, vtype_kind_t vtypea, in
427435

428436
// vtype of all n_pop objects is VTYPE_PYOBJ
429437
static void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, int reg_dest, int n_pop) {
430-
need_reg_all(emit);
438+
need_reg_all(emit, n_pop);
431439
for (int i = 0; i < n_pop; i++) {
432-
assert(emit->stack_info[emit->stack_size + i].vtype == VTYPE_PYOBJ);
440+
assert(emit->stack_info[emit->stack_size - 1 - i].kind == STACK_VALUE);
441+
assert(emit->stack_info[emit->stack_size - 1 - i].vtype == VTYPE_PYOBJ);
433442
}
434443
ASM_MOV_LOCAL_ADDR_TO_REG(emit->stack_start + emit->stack_size - 1, reg_dest);
435444
adjust_stack(emit, -n_pop);
436445
}
437446

438447
// vtype of all n_push objects is VTYPE_PYOBJ
439448
static void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, int reg_dest, int n_push) {
440-
need_reg_all(emit);
449+
need_reg_all(emit, 0);
441450
for (int i = 0; i < n_push; i++) {
451+
emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
442452
emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
443453
}
444454
ASM_MOV_LOCAL_ADDR_TO_REG(emit->stack_start + emit->stack_size + n_push - 1, reg_dest);
@@ -454,7 +464,7 @@ static void emit_call(emit_t *emit, rt_fun_kind_t fun_kind, void *fun) {
454464
}
455465

456466
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) {
457-
need_reg_all(emit);
467+
need_reg_all(emit, 0);
458468
ASM_MOV_IMM_TO_REG(arg_val, arg_reg);
459469
emit_call(emit, fun_kind, fun);
460470
}
@@ -549,8 +559,13 @@ static void emit_native_load_const_dec(emit_t *emit, qstr qstr) {
549559
}
550560

551561
static void emit_native_load_const_id(emit_t *emit, qstr qstr) {
552-
// not supported for viper?
553-
assert(0);
562+
emit_pre(emit);
563+
if (emit->do_viper_types) {
564+
assert(0);
565+
} else {
566+
emit_call_with_imm_arg(emit, RT_F_LOAD_CONST_STR, rt_load_const_str, qstr, REG_ARG_1); // TODO
567+
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
568+
}
554569
}
555570

556571
static void emit_native_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
@@ -669,8 +684,9 @@ static void emit_native_load_method(emit_t *emit, qstr qstr) {
669684
}
670685

671686
static void emit_native_load_build_class(emit_t *emit) {
672-
// not supported
673-
assert(0);
687+
emit_pre(emit);
688+
emit_call(emit, RT_F_LOAD_BUILD_CLASS, rt_load_build_class);
689+
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
674690
}
675691

676692
static void emit_native_store_fast(emit_t *emit, qstr qstr, int local_num) {
@@ -727,8 +743,12 @@ static void emit_native_store_deref(emit_t *emit, qstr qstr) {
727743
}
728744

729745
static void emit_native_store_attr(emit_t *emit, qstr qstr) {
730-
// not implemented
731-
assert(0);
746+
vtype_kind_t vtype_base, vtype_val;
747+
emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
748+
assert(vtype_base == VTYPE_PYOBJ);
749+
assert(vtype_val == VTYPE_PYOBJ);
750+
emit_call_with_imm_arg(emit, RT_F_STORE_ATTR, rt_store_attr, qstr, REG_ARG_2); // arg2 = attribute name
751+
emit_post(emit);
732752
}
733753

734754
static void emit_native_store_subscr(emit_t *emit) {
@@ -1069,7 +1089,9 @@ static void emit_native_call_method(emit_t *emit, int n_positional, int n_keywor
10691089
assert(vtype_arg1 == VTYPE_PYOBJ);
10701090
emit_call(emit, RT_F_CALL_METHOD_2, rt_call_method_2);
10711091
} else {
1072-
assert(0);
1092+
emit_pre(emit);
1093+
emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_positional + 2); // pointer to items in reverse order, including meth and self
1094+
emit_call_with_imm_arg(emit, RT_F_CALL_METHOD_N, rt_call_method_n, n_positional, REG_ARG_1);
10731095
}
10741096
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
10751097
}

py/runtime.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
#include "runtime.h"
1010
#include "vm.h"
1111

12+
#if 0 // print debugging info
13+
#define DEBUG_printf(args...) printf(args)
14+
#define DEBUG_OP_printf(args...) printf(args)
15+
#else // don't print debugging info
1216
#define DEBUG_printf(args...) (void)0
13-
//#define DEBUG_printf(args...) printf(args)
14-
1517
#define DEBUG_OP_printf(args...) (void)0
16-
//#define DEBUG_OP_printf(args...) printf(args)
18+
#endif
1719

1820
// enable/disable float support with this definition
1921
#define PY_FLOAT (1)
@@ -914,7 +916,7 @@ py_obj_t rt_call_function_1(py_obj_t fun, py_obj_t arg) {
914916
py_obj_t rt_call_function_2(py_obj_t fun, py_obj_t arg1, py_obj_t arg2) {
915917
if (IS_O(fun, O_FUN_2)) {
916918
py_obj_base_t *o = fun;
917-
DEBUG_OP_printf("calling native %p with 2 args\n", o->u_fun.fun);
919+
DEBUG_OP_printf("calling native %p(%p, %p)\n", o->u_fun.fun, arg1, arg2);
918920
return ((py_fun_2_t)o->u_fun.fun)(arg1, arg2);
919921
} else if (IS_O(fun, O_FUN_BC)) {
920922
py_obj_base_t *o = fun;
@@ -937,7 +939,12 @@ py_obj_t rt_call_function_2(py_obj_t fun, py_obj_t arg1, py_obj_t arg2) {
937939

938940
// args are in reverse order in the array
939941
py_obj_t rt_call_function_n(py_obj_t fun, int n_args, const py_obj_t *args) {
940-
if (IS_O(fun, O_FUN_BC)) {
942+
if (IS_O(fun, O_FUN_2)) {
943+
assert(n_args == 2);
944+
py_obj_base_t *o = fun;
945+
DEBUG_OP_printf("calling native %p(%p, %p)\n", o->u_fun.fun, args[1], args[0]);
946+
return ((py_fun_2_t)o->u_fun.fun)(args[1], args[0]);
947+
} else if (IS_O(fun, O_FUN_BC)) {
941948
py_obj_base_t *o = fun;
942949
assert(o->u_fun_bc.n_args == n_args);
943950
DEBUG_OP_printf("calling byte code %p with %d args\n", o->u_fun_bc.code, n_args);
@@ -949,7 +956,7 @@ py_obj_t rt_call_function_n(py_obj_t fun, int n_args, const py_obj_t *args) {
949956
}
950957

951958
py_obj_t rt_call_method_1(py_obj_t fun, py_obj_t self) {
952-
DEBUG_OP_printf("call method %p %p\n", fun, self);
959+
DEBUG_OP_printf("call method %p(self=%p)\n", fun, self);
953960
if (self == NULL) {
954961
return rt_call_function_0(fun);
955962
} else {
@@ -958,7 +965,7 @@ py_obj_t rt_call_method_1(py_obj_t fun, py_obj_t self) {
958965
}
959966

960967
py_obj_t rt_call_method_2(py_obj_t fun, py_obj_t self, py_obj_t arg) {
961-
DEBUG_OP_printf("call method %p %p %p\n", fun, self, arg);
968+
DEBUG_OP_printf("call method %p(self=%p, %p)\n", fun, self, arg);
962969
if (self == NULL) {
963970
return rt_call_function_1(fun, arg);
964971
} else {
@@ -969,7 +976,7 @@ py_obj_t rt_call_method_2(py_obj_t fun, py_obj_t self, py_obj_t arg) {
969976
// args contains: arg(n_args-1) arg(n_args-2) ... arg(0) self/NULL fun
970977
// if n_args==0 then there are only self/NULL and fun
971978
py_obj_t rt_call_method_n(int n_args, const py_obj_t *args) {
972-
DEBUG_OP_printf("call method %p %p %d args\n", args[n_args + 1], args[n_args] , n_args);
979+
DEBUG_OP_printf("call method %p(self=%p, n_args=%d)\n", args[n_args + 1], args[n_args], n_args);
973980
return rt_call_function_n(args[n_args + 1], n_args + ((args[n_args] == NULL) ? 0 : 1), args);
974981
}
975982

@@ -1186,9 +1193,11 @@ void *rt_fun_table[RT_F_NUMBER_OF] = {
11861193
rt_load_const_str,
11871194
rt_load_name,
11881195
rt_load_global,
1196+
rt_load_build_class,
11891197
rt_load_attr,
11901198
rt_load_method,
11911199
rt_store_name,
1200+
rt_store_attr,
11921201
rt_store_subscr,
11931202
rt_is_true,
11941203
rt_unary_op,
@@ -1202,6 +1211,7 @@ void *rt_fun_table[RT_F_NUMBER_OF] = {
12021211
rt_call_function_2,
12031212
rt_call_method_1,
12041213
rt_call_method_2,
1214+
rt_call_method_n,
12051215
rt_binary_op,
12061216
rt_compare_op,
12071217
};

py/runtime.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ typedef enum {
5151
RT_F_LOAD_CONST_STR = 0,
5252
RT_F_LOAD_NAME,
5353
RT_F_LOAD_GLOBAL,
54+
RT_F_LOAD_BUILD_CLASS,
5455
RT_F_LOAD_ATTR,
5556
RT_F_LOAD_METHOD,
5657
RT_F_STORE_NAME,
58+
RT_F_STORE_ATTR,
5759
RT_F_STORE_SUBSCR,
5860
RT_F_IS_TRUE,
5961
RT_F_UNARY_OP,
@@ -67,6 +69,7 @@ typedef enum {
6769
RT_F_CALL_FUNCTION_2,
6870
RT_F_CALL_METHOD_1,
6971
RT_F_CALL_METHOD_2,
72+
RT_F_CALL_METHOD_N,
7073
RT_F_BINARY_OP,
7174
RT_F_COMPARE_OP,
7275
RT_F_NUMBER_OF,

0 commit comments

Comments
 (0)