RPA Toolkit
dev
[rpatk.git] / tests / opmap-test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "rvmcpu.h"
4 #include "rvmoperator.h"
5 #include "rvmoperatoradd.h"
6
7
8 typedef struct rvm_testctx_s {
9         rvm_opmap_t *opmap;
10 } rvm_testctx_t;
11
12
13 static void test_swi_print_r(rvm_cpu_t *cpu, rvm_asmins_t *ins)
14 {
15         if (rvm_reg_gettype(RVM_REG_PTR(cpu, ins->op2)) == RVM_DTYPE_LONG)
16                 fprintf(stdout, "R%d = %ld\n", ins->op2, RVM_GET_REGL(cpu, ins->op2));
17         else if (rvm_reg_gettype(RVM_REG_PTR(cpu, ins->op2)) == RVM_DTYPE_DOUBLE)
18                 fprintf(stdout, "R%d = %5.2f\n", ins->op2, RVM_GET_REGD(cpu, ins->op2));
19         else
20                 fprintf(stdout, "Unknown type\n");
21 }
22
23
24 static void test_swi_add(rvm_cpu_t *cpu, rvm_asmins_t *ins)
25 {
26         rvm_testctx_t *ctx = (rvm_testctx_t *)cpu->userdata;
27         rvm_opmap_invoke_binary_handler(ctx->opmap, RVM_OPID_ADD, cpu, RVM_REG_PTR(cpu, R0), RVM_REG_PTR(cpu, ins->op2), RVM_REG_PTR(cpu, ins->op3));
28 }
29
30
31 static void test_swi_sub(rvm_cpu_t *cpu, rvm_asmins_t *ins)
32 {
33         rvm_testctx_t *ctx = (rvm_testctx_t *)cpu->userdata;
34         rvm_opmap_invoke_binary_handler(ctx->opmap, RVM_OPID_SUB, cpu, RVM_REG_PTR(cpu, R0), RVM_REG_PTR(cpu, ins->op2), RVM_REG_PTR(cpu, ins->op3));
35 }
36
37
38 static void test_swi_mul(rvm_cpu_t *cpu, rvm_asmins_t *ins)
39 {
40         rvm_testctx_t *ctx = (rvm_testctx_t *)cpu->userdata;
41         rvm_opmap_invoke_binary_handler(ctx->opmap, RVM_OPID_MUL, cpu, RVM_REG_PTR(cpu, R0), RVM_REG_PTR(cpu, ins->op2), RVM_REG_PTR(cpu, ins->op3));
42 }
43
44
45 static void test_swi_div(rvm_cpu_t *cpu, rvm_asmins_t *ins)
46 {
47         rvm_testctx_t *ctx = (rvm_testctx_t *)cpu->userdata;
48         rvm_opmap_invoke_binary_handler(ctx->opmap, RVM_OPID_DIV, cpu, RVM_REG_PTR(cpu, R0), RVM_REG_PTR(cpu, ins->op2), RVM_REG_PTR(cpu, ins->op3));
49 }
50
51
52 static rvm_switable_t switable[] = {
53                 {"add", test_swi_add},
54                 {"sub", test_swi_sub},
55                 {"mul", test_swi_mul},
56                 {"div", test_swi_div},
57                 {"print", test_swi_print_r},
58                 {NULL, NULL},
59 };
60
61
62 int main(int argc, char *argv[])
63 {
64         rvm_testctx_t ctx;
65         rvm_cpu_t *cpu;
66         rvm_opmap_t *opmap;
67         rvm_asmins_t code[1024];
68         ruint off = 0;
69         ruint ntable;
70
71         ctx.opmap = opmap = rvm_opmap_create();
72         cpu = rvm_cpu_create();
73         cpu->userdata = &ctx;
74
75         rvm_opmap_add_binary_operator(opmap, RVM_OPID_ADD);
76         rvm_opmap_set_binary_handler(opmap, RVM_OPID_ADD, rvm_op_add_double_double, RVM_DTYPE_DOUBLE, RVM_DTYPE_DOUBLE);
77         rvm_opmap_set_binary_handler(opmap, RVM_OPID_ADD, rvm_op_add_long_double, RVM_DTYPE_LONG, RVM_DTYPE_DOUBLE);
78         rvm_opmap_set_binary_handler(opmap, RVM_OPID_ADD, rvm_op_add_double_long, RVM_DTYPE_DOUBLE, RVM_DTYPE_LONG);
79         rvm_opmap_set_binary_handler(opmap, RVM_OPID_ADD, rvm_op_add_long_long, RVM_DTYPE_LONG, RVM_DTYPE_LONG);
80
81
82         ntable = rvm_cpu_switable_add(cpu, switable);
83         code[off++] = rvm_asmd(RVM_MOV, R1, DA, XX, 1);
84         code[off++] = rvm_asml(RVM_MOV, R2, DA, XX, 3.2);
85 //      code[off++] = rvm_asm(RVM_SWI, DA, XX, XX, RVM_SWI_ID(ntable, 1));                      // mul
86         code[off++] = rvm_asm(RVM_MOV, R0, DA, XX, rvm_cpu_getswi(cpu, "add"));         // add
87         code[off++] = rvm_asm(RVM_SWI, R0, R1, R2, 0);
88         code[off++] = rvm_asm(RVM_SWI, DA, R1, XX, rvm_cpu_getswi(cpu, "print"));       // print
89         code[off++] = rvm_asm(RVM_SWI, DA, R2, XX, rvm_cpu_getswi(cpu, "print"));       // print
90         code[off++] = rvm_asm(RVM_SWI, DA, R0, XX, rvm_cpu_getswi(cpu, "print"));       // print
91
92         code[off++] = rvm_asm(RVM_EXT, XX, XX, XX, 0);
93
94
95
96         rvm_cpu_exec_debug(cpu, code, 0);
97         rvm_cpu_destroy(cpu);
98         rvm_opmap_destroy(opmap);
99
100
101         fprintf(stdout, "It works!\n");
102         return 0;
103 }