-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrb_vm.var.stack.c
324 lines (259 loc) · 8.2 KB
/
rb_vm.var.stack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* -MODULE----------------------------------------------------------------------
RapidBATCH
Copyright (C) 2008, 2009 by Phorward Software Technologies, Jan Max Meyer
http://www.phorward-software.com ++ [email protected]
File: rb_vm.stack.c
Author: Jan Max Meyer
Usage: Virtual machine variable access and manipulation via the
virtual machine stack
----------------------------------------------------------------------------- */
/*
* Includes
*/
/* #define __WITH_TRACE */
#include "rb_global.h"
/*
* Global variables
*/
/*
* Functions
*/
/* -FUNCTION--------------------------------------------------------------------
Function: rb_vm_stack_get_var_base()
Author: Jan Max Meyer
Usage: Retrieves a variable base pointer from a stack address.
If the item is not a variable, then it will be transformed
into a variable base item!
Parameters: vm_stack* stack Stack to be used
Returns: vm_var* Pointer to the variable
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
----------------------------------------------------------------------------- */
vm_var* rb_vm_stack_get_var_base( vm_stack* stack )
{
vm_stackitem value;
vm_stackitem* var_addr;
vm_stackitem* ptr;
vm_var* var;
PROC( "rb_vm_stack_get_var_base" );
PARMS( "stack", "%p", stack );
var_addr = rb_vm_stack_access( stack, stack->stack_top - 1 );
ptr = rb_vm_stack_access( stack, ITEM_VAL_GET_ADDR( var_addr ) );
VARS( "ptr->type", "%d", ptr->type );
switch( ptr->type )
{
case ITEM_VAR:
var = &( ptr->attribute.var );
break;
case ITEM_PTR:
var = ptr->attribute.var_ptr;
break;
default:
MSG( "Transforming stack item into a variable!" );
memcpy( &value, ptr, sizeof( vm_stackitem ) );
memset( ptr, 0, sizeof( vm_stackitem ) );
ptr->type = ITEM_VAR;
var = &( ptr->attribute.var );
rb_vm_var_set( var, ITEM_VAL_STRUCT( &value ),
DONT_COPY_STRING );
}
RETURN( var );
}
/* -FUNCTION--------------------------------------------------------------------
Function: rb_vm_stack_var_resolve()
Author: Jan Max Meyer
Usage: Retrieves the vm_var-pointer to a variable, according
to the stacked index and index depth information.
Parameters: vm_stack* stack Stack to be used
pboolean allocate TRUE: Memory will be (re-)allo-
cated to the desired index, if
required
FALSE: Memory will not be re-
allocated, if memory does not
exists, the function immedia-
telly returns (vm_var*)NULL.
pboolean drop_index TRUE: Drops all index variables
FALSE: Doesn't modify the stack
Returns: vm_var* Pointer to the variable
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
21.06.2009 Jan Max Meyer Added feature of getting array sizes by
accessing element zero.
----------------------------------------------------------------------------- */
vm_var* rb_vm_stack_var_resolve( vm_stack* stack,
pboolean allocate, pboolean* get_size )
{
int i;
vm_var* var;
vm_var* prev_var;
vm_stackitem* ptr;
vm_stackitem* count;
pboolean set_size = FALSE;
PROC( "rb_vm_stack_var_resolve" );
PARMS( "stack", "%p", stack );
PARMS( "allocate", "%d", allocate );
PARMS( "get_size", "%p", get_size );
/* Get variable base pointer */
prev_var = var = rb_vm_stack_get_var_base( stack );
/* Get depth counter */
count = rb_vm_stack_access( stack, stack->stack_top - 2 );
/* Get pointer to variable by resolving all indexes */
for( i = ITEM_VAL_GET_LONG( count ); i > 0; i-- )
{
ptr = rb_vm_stack_access( stack, stack->stack_top - 2 - i );
if( !( var = rb_vm_var_get_idx( prev_var,
ITEM_VAL_STRUCT( ptr ), allocate ) ) )
{
MSG( "Can't get variable" );
break;
}
/*
VARS( "get_size", "%p", get_size );
VARS( "*get_size", "%d", get_size ? *get_size : 0 );
VARS( "prev_var", "%p", prev_var );
VARS( "var", "%p", var );
*/
/* If the function returned the same variable as the
previous, a dimension size request was made */
if( prev_var == var )
{
if( get_size && *get_size )
{
MSG( "Size request - stopping variable resolvement loop" );
set_size = TRUE;
break;
/*
if( rb_vm_optimal_value( ITEM_VAL_STRUCT( ptr ) )
{
if( ITEM_VAL_GET_ADDR( ptr ) == 0 )
{
set_size = TRUE;
break;
}
}
*/
}
else
{
rb_vm_error( "array_index_of_null" );
}
}
prev_var = var;
}
/* Drop used items off the stack and free memory */
MSG( "Dropping stack elements" );
rb_vm_stack_drop( stack, ITEM_VAL_GET_LONG( count ) + 2 );
/* Set size flag? */
if( get_size )
*get_size = set_size;
RETURN( var );
}
/* -FUNCTION--------------------------------------------------------------------
Function: rb_vm_stack_init_var()
Author: Jan Max Meyer
Usage: Initializes a variable structure to a fixed dimension depth.
Parameters: vm_stack* stack Stack to be used.
vm_var* var Pointer to variable; Only used
by rb_vm_stack_init_var() it-
self in recursive call.
Should be (vm_var*)NULL for
the initial call.
int max Maximum depth of the index
values that resist on the
stack. This must be -1 for the
initial call.
int depth The current recursion depth;
only for the recursion.
Returns: pboolean TRUE: On success,
FALSE: else
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
----------------------------------------------------------------------------- */
pboolean rb_vm_stack_init_var( vm_stack* stack, vm_var* var, int max, int depth )
{
vm_addr i = TRUE;
vm_stackitem* index;
PROC( "rb_vm_stack_init_var" );
PARMS( "stack", "%p", stack );
PARMS( "var", "%p", var );
PARMS( "max", "%d", max );
PARMS( "depth", "%d", depth );
if( max < 0 )
{
MSG( "INIT" );
/* Get variable base pointer */
var = rb_vm_stack_get_var_base( stack );
/* Get depth counter */
index = rb_vm_stack_access( stack, stack->stack_top - 2 );
/* Begin of recursion! */
i = rb_vm_stack_init_var( stack, var,
(int)ITEM_VAL_GET_LONG( index ), 0 );
/* Drop used items off the stack and free memory */
rb_vm_stack_drop( stack, ITEM_VAL_GET_LONG( index ) + 2 );
}
else if( depth < max )
{
index = rb_vm_stack_access( stack,
stack->stack_top - 2 - max + depth );
if( !rb_vm_var_get_idx( var, &( index->attribute.val ), TRUE ) )
{
MSG( "Allocation error?" );
RETURN( FALSE );
}
VARS( "depth", "%d", depth );
VARS( "max", "%d", max );
MSG( "Allocate yet another dimension!" );
VARS( "var->begin_cnt", "%ld", var->begin_cnt );
for( i = 0; i < var->begin_cnt; i++ )
{
VARS( "i", "%ld", i );
if( !rb_vm_stack_init_var( stack,
var->begin + i, max, depth + 1 ) )
{
MSG( "Abort, has an error occured?" );
RETURN( FALSE );
}
}
}
RETURN( i );
}
/* -FUNCTION--------------------------------------------------------------------
Function: rb_vm_stack_var_push()
Author: Jan Max Meyer
Usage: Pushes a variable.
Parameters: vm_stack* stack Stack to be used
vm_var* var Variable value to be pushed
Returns: status ERR_OK on success,
else one of the error defines.
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
----------------------------------------------------------------------------- */
status rb_vm_stack_var_push( vm_stack* stack, vm_var* var )
{
vm_stackitem val;
PROC( "rb_vm_stack_var_push" );
PARMS( "stack", "%p", stack );
PARMS( "var", "%p", var );
if( !( stack ) )
RETURN( -1 );
memset( &val, 0, sizeof( vm_stackitem ) );
val.type = ITEM_VAL;
if( var )
{
MSG( "Var exists, reading content" );
if( rb_vm_var_get( ITEM_VAL_STRUCT( &val ),
var, FALSE ) == 0 )
{
if( rb_vm_stack_push( stack, &val ) )
RETURN( 0 );
}
}
else
{
MSG( "Variable does not exist, pushing empty string" );
ITEM_VAL_SET_CSTR( &val, (char*)NULL );
if( rb_vm_stack_push( stack, &val ) )
RETURN( 0 );
}
RETURN( -1 );
}