RPA Toolkit
adding rvm_compiler_t to the rpagen test
[rpatk.git] / rvm / rvmscope.c
1 #include "rvmscope.h"
2 #include "rstring.h"
3 #include "rmem.h"
4
5 rvm_scope_t *rvm_scope_create()
6 {
7         rvm_scope_t *scope;
8
9         scope = (rvm_scope_t*)r_malloc(sizeof(*scope));
10         if (!scope)
11                 return NULL;
12         r_memset(scope, 0, sizeof(*scope));
13         scope->names = r_array_create(sizeof(rstr_t*));
14         scope->nameshash = r_hash_create(5, r_hash_rstrequal, r_hash_rstrhash);
15         scope->varstack = r_array_create(sizeof(rvm_varmap_t));
16         scope->scopestack = r_array_create(sizeof(scope->varstack->len));
17         return scope;
18 }
19
20
21 void rvm_scope_destroy(rvm_scope_t *scope)
22 {
23         int i;
24         int len = scope->names->len;
25
26         for (i = 0; i < len; i++)
27                 r_free(r_array_index(scope->names, i, rchar*));
28         r_array_destroy(scope->names);
29         r_array_destroy(scope->varstack);
30         r_array_destroy(scope->scopestack);
31         r_hash_destroy(scope->nameshash);
32         r_free(scope);
33 }
34
35
36 rchar *rvm_scope_addname(rvm_scope_t *scope, const rchar* name, ruint namesize)
37 {
38         rstr_t namestr = {(rchar*)name, namesize};
39         rstr_t *dupname = r_hash_lookup(scope->nameshash, &namestr);
40
41         if (!dupname) {
42                 dupname = r_rstrdup(name, namesize);
43                 r_array_add(scope->names, (rconstpointer)&dupname);
44                 r_hash_insert(scope->nameshash, dupname, dupname);
45         }
46         return dupname->str;
47 }
48
49
50 rchar *rvm_scope_addstrname(rvm_scope_t *scope, const rchar *name)
51 {
52         return rvm_scope_addname(scope, name, r_strlen(name));
53 }
54
55
56 void rvm_scope_push(rvm_scope_t* scope)
57 {
58         ruint scopesize = scope->varstack->len;
59         r_array_add(scope->scopestack, &scopesize);
60 }
61
62
63 void rvm_scope_pop(rvm_scope_t* scope)
64 {
65         ruint scopesize = r_array_index(scope->scopestack, scope->scopestack->len - 1, ruint);
66         r_array_setsize(scope->scopestack, scope->scopestack->len - 1);
67         r_array_setsize(scope->varstack, scopesize);
68 }
69
70
71 void rvm_scope_addoffset(rvm_scope_t *scope, const rchar *name, ruint namesize, ruint32 off)
72 {
73         rvm_varmap_t vmap;
74
75         vmap.name = rvm_scope_addname(scope, name, namesize);
76         vmap.data.offset = off;
77         vmap.datatype = VARMAP_DATATYPE_OFFSET;
78         r_array_add(scope->varstack, &vmap);
79 }
80
81
82 ruint rvm_scope_numentries(rvm_scope_t *scope)
83 {
84         return r_array_size(scope->varstack);
85 }
86
87
88 void rvm_scope_addpointer(rvm_scope_t *scope, const rchar *name, ruint namesize, rpointer ptr)
89 {
90         rvm_varmap_t vmap;
91
92         vmap.name = rvm_scope_addname(scope, name, namesize);
93         vmap.data.ptr = ptr;
94         vmap.datatype = VARMAP_DATATYPE_PTR;
95         r_array_add(scope->varstack, &vmap);
96 }
97
98
99 rvm_varmap_t *rvm_scope_lookup(rvm_scope_t *scope, const rchar *name, ruint namesize)
100 {
101         ruint scopesize = scope->varstack->len;
102         rvm_varmap_t *varmap;
103         rint i;
104
105         for (i = scopesize - 1; i >= 0; i--) {
106                 varmap = (rvm_varmap_t*)r_array_slot(scope->varstack, i);
107                 if (r_strncmp(varmap->name, name, namesize) == 0)
108                         return varmap;
109         }
110         return NULL;
111 }
112
113
114 rvm_varmap_t *rvm_scope_lookup_tip(rvm_scope_t *scope, const rchar *name, ruint namesize)
115 {
116         ruint scopesize = scope->varstack->len;
117         ruint tipstart = r_array_empty(scope->scopestack) ? 0 : r_array_last(scope->scopestack, ruint);
118         rvm_varmap_t *varmap;
119         rint i;
120
121         for (i = scopesize - 1; i >= tipstart; i--) {
122                 varmap = (rvm_varmap_t*)r_array_slot(scope->varstack, i);
123                 if (r_strncmp(varmap->name, name, namesize) == 0)
124                         return varmap;
125         }
126         return NULL;
127 }
128
129
130 rvm_varmap_t *rvm_scope_lookup_s(rvm_scope_t *scope, const rchar *name)
131 {
132         return rvm_scope_lookup(scope, name, r_strlen(name));
133 }
134
135
136 rvm_varmap_t *rvm_scope_lookuptip_s(rvm_scope_t *scope, const rchar *name)
137 {
138         return rvm_scope_lookup_tip(scope, name, r_strlen(name));
139 }