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