RPA Toolkit
e1996204ef9b288e3adbd586fcae0ba9170ef3af
[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_r0(rvm_cpu_t *cpu, rvm_asmins_t *ins)
14 {
15         if (rvm_reg_gettype(&cpu->r[R0]) == RVM_DTYPE_LONG)
16                 fprintf(stdout, "R0 = %ld\n", RVM_GET_REGL(cpu, R0));
17         else if (rvm_reg_gettype(&cpu->r[R0]) == RVM_DTYPE_DOUBLE)
18                 fprintf(stdout, "R0 = %5.2f\n", RVM_GET_REGD(cpu, R0));
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, &cpu->r[R0], &cpu->r[R1], &cpu->r[R2]);
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, &cpu->r[R0], &cpu->r[R1], &cpu->r[R2]);
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, &cpu->r[R0], &cpu->r[R1], &cpu->r[R2]);
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, &cpu->r[R0], &cpu->r[R1], &cpu->r[R2]);
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_r0},
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
87         code[off++] = rvm_asm(RVM_SWI, DA, XX, XX, rvm_cpu_getswi(cpu, "add"));         // add
88         code[off++] = rvm_asm(RVM_SWI, DA, XX, XX, rvm_cpu_getswi(cpu, "print"));       // print
89
90         code[off++] = rvm_asm(RVM_EXT, XX, XX, XX, 0);
91
92
93
94         rvm_cpu_exec_debug(cpu, code, 0);
95         rvm_cpu_destroy(cpu);
96         rvm_opmap_destroy(opmap);
97
98
99         fprintf(stdout, "It works!\n");
100         return 0;
101 }