RPA Toolkit
merge the work on extended ops
[rpatk.git] / rvm / rvmoperatorcast.c
1 #include "rvmoperatorcast.h"
2 #include "rvmreg.h"
3
4
5 void rvm_op_cast_static_static(rvmcpu_t *cpu, rvmreg_t *res, const rvmreg_t *arg1, const rvmreg_t *arg2)
6 {
7         *res = *arg1;
8 }
9
10
11 void rvm_op_cast_double_unsigned(rvmcpu_t *cpu, rvmreg_t *res, const rvmreg_t *arg1, const rvmreg_t *arg2)
12 {
13         rword r = (rword)RVM_REG_GETD(arg1);
14
15         RVM_REG_CLEAR(res);
16         RVM_REG_SETTYPE(res, RVM_DTYPE_UNSIGNED);
17         RVM_REG_SETL(res, r);
18 }
19
20 void rvm_op_cast_double_long(rvmcpu_t *cpu, rvmreg_t *res, const rvmreg_t *arg1, const rvmreg_t *arg2)
21 {
22         rlong r = (rlong)RVM_REG_GETD(arg1);
23
24         RVM_REG_CLEAR(res);
25         RVM_REG_SETTYPE(res, RVM_DTYPE_LONG);
26         RVM_REG_SETL(res, r);
27 }
28
29
30 void rvm_op_cast_long_unsigned(rvmcpu_t *cpu, rvmreg_t *res, const rvmreg_t *arg1, const rvmreg_t *arg2)
31 {
32         rword r = (rword)RVM_REG_GETL(arg1);
33
34         RVM_REG_CLEAR(res);
35         RVM_REG_SETTYPE(res, RVM_DTYPE_UNSIGNED);
36         RVM_REG_SETU(res, r);
37 }
38
39
40 void rvm_op_cast_long_double(rvmcpu_t *cpu, rvmreg_t *res, const rvmreg_t *arg1, const rvmreg_t *arg2)
41 {
42         rdouble r = (rdouble)RVM_REG_GETL(arg1);
43
44         RVM_REG_CLEAR(res);
45         RVM_REG_SETTYPE(res, RVM_DTYPE_DOUBLE);
46         RVM_REG_SETL(res, r);
47 }
48
49
50 void rvm_op_cast_unsigned_long(rvmcpu_t *cpu, rvmreg_t *res, const rvmreg_t *arg1, const rvmreg_t *arg2)
51 {
52         rlong r = (rlong)RVM_REG_GETU(arg1);
53
54         RVM_REG_CLEAR(res);
55         RVM_REG_SETTYPE(res, RVM_DTYPE_LONG);
56         RVM_REG_SETL(res, r);
57 }
58
59
60 void rvm_op_cast_unsigned_double(rvmcpu_t *cpu, rvmreg_t *res, const rvmreg_t *arg1, const rvmreg_t *arg2)
61 {
62         rdouble r = (rdouble)RVM_REG_GETU(arg1);
63
64         RVM_REG_CLEAR(res);
65         RVM_REG_SETTYPE(res, RVM_DTYPE_DOUBLE);
66         RVM_REG_SETD(res, r);
67 }
68
69
70 void rvm_op_cast_string_string(rvmcpu_t *cpu, rvmreg_t *res, const rvmreg_t *arg1, const rvmreg_t *arg2)
71 {
72         if (res == arg1)
73                 RVM_ABORT(cpu, RVM_E_ILLEGALDST);
74         rvm_reg_setstring(res, r_string_copy(RVM_REG_GETP(arg1)));
75 }
76
77
78 void rvm_op_cast_refreg_string(rvmcpu_t *cpu, rvmreg_t *res, const rvmreg_t *arg1, const rvmreg_t *arg2)
79 {
80         if (res == arg1)
81                 RVM_ABORT(cpu, RVM_E_ILLEGALDST);
82         rvm_reg_refer(res, arg1);
83 }
84
85
86 void rvm_op_cast_init(rvm_opmap_t *opmap)
87 {
88         rvm_opmap_add_binary_operator(opmap, RVM_OPID_CAST);
89         rvm_opmap_set_binary_handler(opmap, RVM_OPID_CAST, rvm_op_cast_static_static, RVM_DTYPE_DOUBLE, RVM_DTYPE_DOUBLE);
90         rvm_opmap_set_binary_handler(opmap, RVM_OPID_CAST, rvm_op_cast_static_static, RVM_DTYPE_LONG, RVM_DTYPE_LONG);
91         rvm_opmap_set_binary_handler(opmap, RVM_OPID_CAST, rvm_op_cast_long_double, RVM_DTYPE_LONG, RVM_DTYPE_DOUBLE);
92         rvm_opmap_set_binary_handler(opmap, RVM_OPID_CAST, rvm_op_cast_long_unsigned, RVM_DTYPE_LONG, RVM_DTYPE_UNSIGNED);
93         rvm_opmap_set_binary_handler(opmap, RVM_OPID_CAST, rvm_op_cast_double_long, RVM_DTYPE_DOUBLE, RVM_DTYPE_LONG);
94         rvm_opmap_set_binary_handler(opmap, RVM_OPID_CAST, rvm_op_cast_double_unsigned, RVM_DTYPE_DOUBLE, RVM_DTYPE_UNSIGNED);
95         rvm_opmap_set_binary_handler(opmap, RVM_OPID_CAST, rvm_op_cast_unsigned_long, RVM_DTYPE_UNSIGNED, RVM_DTYPE_LONG);
96         rvm_opmap_set_binary_handler(opmap, RVM_OPID_CAST, rvm_op_cast_unsigned_double, RVM_DTYPE_UNSIGNED, RVM_DTYPE_DOUBLE);
97         rvm_opmap_set_binary_handler(opmap, RVM_OPID_CAST, rvm_op_cast_refreg_string, RVM_DTYPE_REFREG, RVM_DTYPE_STRING);
98         rvm_opmap_set_binary_handler(opmap, RVM_OPID_CAST, rvm_op_cast_string_string, RVM_DTYPE_STRING, RVM_DTYPE_STRING);
99 }
100