RPA Toolkit
merge the work on extended ops
[rpatk.git] / rvm / rvmreg.c
1 #include "rmem.h"
2 #include "rvmreg.h"
3 #include "rrefreg.h"
4 #include "rref.h"
5
6
7 static void r_array_oncopy_rvmreg(rarray_t *array)
8 {
9         ruint index;
10         rvmreg_t *r;
11
12         for (index = 0; index < array->len; index++) {
13                 r = (rvmreg_t *)r_array_slot(array, index);
14                 rvm_reg_copy(r, r);
15         }
16 }
17
18
19 static void r_array_ondestroy_rvmreg(rarray_t *array)
20 {
21         ruint index;
22         rvmreg_t *r;
23
24         for (index = 0; index < array->len; index++) {
25                 r = (rvmreg_t *)r_array_slot(array, index);
26                 rvm_reg_cleanup(r);
27         }
28 }
29
30
31 rarray_t *r_array_create_rvmreg()
32 {
33         rarray_t *array = r_array_create(sizeof(rvmreg_t));
34         if (array) {
35                 array->oncopy = r_array_oncopy_rvmreg;
36                 array->ondestroy = r_array_ondestroy_rvmreg;
37         }
38         return array;
39 }
40
41
42 rharray_t *r_harray_create_rvmreg()
43 {
44         rharray_t *harray = r_harray_create(sizeof(rvmreg_t));
45         if (harray) {
46                 harray->members->oncopy = r_array_oncopy_rvmreg;
47                 harray->members->ondestroy = r_array_ondestroy_rvmreg;
48         }
49         return harray;
50 }
51
52
53 rvmreg_t rvm_reg_create_string_ansi(const rchar *s)
54 {
55         rvmreg_t r;
56         r_memset(&r, 0, sizeof(r));
57         RVM_REG_SETP(&r, r_string_create_from_ansistr(s));
58         RVM_REG_SETTYPE(&r, RVM_DTYPE_STRING);
59         RVM_REG_SETFLAG(&r, RVM_INFOBIT_ROBJECT);
60         return r;
61 }
62
63
64 rvmreg_t rvm_reg_create_string(const rstr_t *s)
65 {
66         rvmreg_t r;
67         r_memset(&r, 0, sizeof(r));
68         RVM_REG_SETP(&r, r_string_create_from_rstr(s));
69         RVM_REG_SETTYPE(&r, RVM_DTYPE_STRING);
70         RVM_REG_SETFLAG(&r, RVM_INFOBIT_ROBJECT);
71         return r;
72 }
73
74 rvmreg_t rvm_reg_create_refreg()
75 {
76         rvmreg_t r;
77         r_memset(&r, 0, sizeof(r));
78         RVM_REG_SETP(&r, r_refreg_create());
79         RVM_REG_SETTYPE(&r, RVM_DTYPE_REFREG);
80         RVM_REG_SETFLAG(&r, RVM_INFOBIT_ROBJECT);
81         return r;
82 }
83
84
85 rvmreg_t rvm_reg_create_array()
86 {
87         rvmreg_t r;
88         r_memset(&r, 0, sizeof(r));
89         rvm_reg_setarray(&r, r_array_create_rvmreg());
90         return r;
91 }
92
93
94 rvmreg_t rvm_reg_create_harray()
95 {
96         rvmreg_t r;
97         r_memset(&r, 0, sizeof(r));
98         rvm_reg_setharray(&r, r_harray_create_rvmreg());
99         return r;
100 }
101
102 void rvm_reg_setstring(rvmreg_t *r, rstring_t *ptr)
103 {
104         RVM_REG_SETP(r, ptr);
105         RVM_REG_SETTYPE(r, RVM_DTYPE_STRING);
106         RVM_REG_SETFLAG(r, RVM_INFOBIT_ROBJECT);
107 }
108
109
110 void rvm_reg_setarray(rvmreg_t *r, rarray_t *ptr)
111 {
112         RVM_REG_SETP(r, ptr);
113         RVM_REG_SETTYPE(r, RVM_DTYPE_ARRAY);
114         RVM_REG_SETFLAG(r, RVM_INFOBIT_ROBJECT);
115 }
116
117
118 void rvm_reg_setharray(rvmreg_t *r, rharray_t *ptr)
119 {
120         RVM_REG_SETP(r, ptr);
121         RVM_REG_SETTYPE(r, RVM_DTYPE_HARRAY);
122         RVM_REG_SETFLAG(r, RVM_INFOBIT_ROBJECT);
123 }
124
125
126 rvmreg_t rvm_reg_create_double(rdouble d)
127 {
128         rvmreg_t r;
129         r_memset(&r, 0, sizeof(r));
130         RVM_REG_SETD(&r, d);
131         RVM_REG_SETTYPE(&r, RVM_DTYPE_DOUBLE);
132         return r;
133 }
134
135
136 rvmreg_t rvm_reg_create_long(rlong l)
137 {
138         rvmreg_t r;
139         r_memset(&r, 0, sizeof(r));
140         rvm_reg_setlong(&r, l);
141         return r;
142 }
143
144
145 void rvm_reg_cleanup(rvmreg_t *reg)
146 {
147         if (rvm_reg_gettype(reg) == RVM_DTYPE_REFREG)
148                 r_ref_dec((rref_t*)RVM_REG_GETP(reg));
149         else if (rvm_reg_tstflag(reg, RVM_INFOBIT_ROBJECT)) {
150                 r_object_destroy((robject_t*)RVM_REG_GETP(reg));
151         }
152         RVM_REG_CLEAR(reg);
153 }
154
155
156 rvmreg_t *rvm_reg_copy(rvmreg_t *dst, const rvmreg_t *src)
157 {
158         if (dst != src)
159                 *dst = *src;
160         if (rvm_reg_tstflag(dst, RVM_INFOBIT_ROBJECT))
161                 dst->v.p = r_object_copy(dst->v.p);
162         return dst;
163 }
164
165
166 rvmreg_t *rvm_reg_refer(rvmreg_t *dst, const rvmreg_t *src)
167 {
168         if (rvm_reg_gettype(dst) == RVM_DTYPE_REFREG) {
169                 if (dst != src)
170                         *dst = *src;
171                 r_ref_inc((rref_t*)RVM_REG_GETP(dst));
172                 return dst;
173         }
174         return NULL;
175 }
176
177
178 void rvm_reg_settype(rvmreg_t *r, ruint type)
179 {
180         RVM_REG_SETTYPE(r, type);
181 }
182
183
184 ruint rvm_reg_gettype(const rvmreg_t *r)
185 {
186         return RVM_REG_GETTYPE(r);
187 }
188
189
190 rboolean rvm_reg_tstflag(const rvmreg_t *r, ruint16 flag)
191 {
192         return RVM_REG_TSTFLAG(r, flag);
193 }
194
195
196 void rvm_reg_setflag(rvmreg_t *r, ruint16 flag)
197 {
198         RVM_REG_SETFLAG(r, flag);
199 }
200
201
202 void rvm_reg_clrflag(rvmreg_t *r, ruint16 flag)
203 {
204         RVM_REG_CLRFLAG(r, flag);
205 }
206
207
208 void rvm_reg_setlong(rvmreg_t *r, rlong l)
209 {
210         RVM_REG_SETL(r, l);
211         RVM_REG_SETTYPE(r, RVM_DTYPE_LONG);
212         RVM_REG_CLRFLAG(r, RVM_INFOBIT_ROBJECT);
213 }
214
215
216 void rvm_reg_setdouble(rvmreg_t *r, rdouble d)
217 {
218         RVM_REG_SETD(r, d);
219         RVM_REG_SETTYPE(r, RVM_DTYPE_DOUBLE);
220         RVM_REG_CLRFLAG(r, RVM_INFOBIT_ROBJECT);
221
222 }
223
224
225 void rvm_reg_setrefreg(rvmreg_t *r, struct rrefreg_s *ptr)
226 {
227         RVM_REG_SETP(r, ptr);
228         RVM_REG_SETTYPE(r, RVM_DTYPE_REFREG);
229         RVM_REG_SETFLAG(r, RVM_INFOBIT_ROBJECT);
230 }
231
232
233 void rvm_reg_convert_to_refreg(rvmreg_t *reg)
234 {
235         rrefreg_t * refreg = NULL;
236
237         if (rvm_reg_gettype(reg) == RVM_DTYPE_REFREG)
238                 return;
239         refreg = r_refreg_create();
240         *REFREG2REGPTR(refreg) = *reg;
241         RVM_REG_CLEAR(reg);
242         rvm_reg_setrefreg(reg, refreg);
243 }
244
245
246 rvmreg_t *rvm_reg_unshadow(rvmreg_t *reg)
247 {
248         if (rvm_reg_gettype(reg) != RVM_DTYPE_REFREG)
249                 return reg;
250         return REFREG2REGPTR(RVM_REG_GETP(reg));
251 }