... | ... |
@@ -660,12 +660,13 @@ void cli_bytecode_destroy(struct cli_bc *bc) |
660 | 660 |
struct cli_bc_bb *BB = &f->BB[j]; |
661 | 661 |
for(k=0;k<BB->numInsts;k++) { |
662 | 662 |
struct cli_bc_inst *ii = &BB->insts[k]; |
663 |
- if (operand_counts[ii->opcode] > 3) |
|
663 |
+ if (operand_counts[ii->opcode] > 3 || ii->opcode == OP_CALL_DIRECT) |
|
664 | 664 |
free(ii->u.ops.ops); |
665 | 665 |
} |
666 | 666 |
} |
667 | 667 |
free(f->BB); |
668 | 668 |
free(f->allinsts); |
669 |
+ free(f->constants); |
|
669 | 670 |
} |
670 | 671 |
free(bc->funcs); |
671 | 672 |
} |
... | ... |
@@ -93,7 +93,7 @@ static int jump(const struct cli_bc_func *func, uint16_t bbid, struct cli_bc_bb |
93 | 93 |
static struct cli_bc_value *allocate_stack(const struct cli_bc_func *func) |
94 | 94 |
{ |
95 | 95 |
unsigned i; |
96 |
- struct cli_bc_value *values = cli_calloc(func->numValues+func->numConstants, sizeof(*values)); |
|
96 |
+ struct cli_bc_value *values = cli_malloc((func->numValues+func->numConstants)*sizeof(*values)); |
|
97 | 97 |
if (!values) |
98 | 98 |
return NULL; |
99 | 99 |
for (i=func->numValues;i<func->numValues+func->numConstants;i++) |
... | ... |
@@ -103,7 +103,7 @@ static struct cli_bc_value *allocate_stack(const struct cli_bc_func *func) |
103 | 103 |
|
104 | 104 |
int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct cli_bc_func *func, const struct cli_bc_inst *inst) |
105 | 105 |
{ |
106 |
- unsigned i, stack_depth=0, bb_inst=0, stop=0; |
|
106 |
+ unsigned i, stack_depth=0, bb_inst=0, stop=0, stack_max_depth=0; |
|
107 | 107 |
struct cli_bc_func *func2; |
108 | 108 |
struct stack_entry *stack = NULL; |
109 | 109 |
struct cli_bc_bb *bb = NULL; |
... | ... |
@@ -200,17 +200,31 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct |
200 | 200 |
value = stack[stack_depth].ret; |
201 | 201 |
func = stack[stack_depth].func; |
202 | 202 |
value->v = values[inst->u.unaryop].v; |
203 |
- free(values); |
|
203 |
+ old_values = values; |
|
204 | 204 |
values = stack[stack_depth].values; |
205 |
+ stack[stack_depth].values = old_values; |
|
205 | 206 |
CHECK_GT(func->numValues+func->numConstants, value-values); |
206 | 207 |
CHECK_GT(value-values, -1); |
207 |
- if (!stack[stack_depth].bb) { |
|
208 |
- stop = CL_BREAK; |
|
209 |
- bb_inst--; |
|
210 |
- break; |
|
211 |
- } |
|
212 | 208 |
bb = stack[stack_depth].bb; |
213 | 209 |
bb_inst = stack[stack_depth].bb_inst; |
210 |
+ if ((stack_depth < stack_max_depth*3/4) || !stack_depth) { |
|
211 |
+ for (i=stack_depth;i<stack_max_depth;i++) { |
|
212 |
+ free(stack[i].values); |
|
213 |
+ } |
|
214 |
+ if (!stack_depth) { |
|
215 |
+ free(stack); |
|
216 |
+ stack = 0; |
|
217 |
+ } else { |
|
218 |
+ stack = cli_realloc2(stack, sizeof(*stack)*stack_depth); |
|
219 |
+ if (!stack) |
|
220 |
+ return CL_EMEM; |
|
221 |
+ } |
|
222 |
+ stack_max_depth = stack_depth; |
|
223 |
+ } |
|
224 |
+ if (!bb) { |
|
225 |
+ stop = CL_BREAK; |
|
226 |
+ continue; |
|
227 |
+ } |
|
214 | 228 |
inst = &bb->insts[bb_inst]; |
215 | 229 |
break; |
216 | 230 |
case OP_ICMP_EQ: |
... | ... |
@@ -251,20 +265,25 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct |
251 | 251 |
CHECK_FUNCID(inst->u.ops.funcid); |
252 | 252 |
func2 = &bc->funcs[inst->u.ops.funcid]; |
253 | 253 |
CHECK_EQ(func2->numArgs, inst->u.ops.numOps); |
254 |
- stack = cli_realloc2(stack, sizeof(*stack)*(stack_depth+1)); |
|
255 |
- if (!stack) |
|
256 |
- return CL_EMEM; |
|
254 |
+ old_values = values; |
|
255 |
+ if (stack_depth+1 > stack_max_depth) { |
|
256 |
+ stack = cli_realloc2(stack, sizeof(*stack)*(stack_depth+1)); |
|
257 |
+ if (!stack) |
|
258 |
+ return CL_EMEM; |
|
259 |
+ stack_max_depth = stack_depth+1; |
|
260 |
+ values = allocate_stack(func2); |
|
261 |
+ if (!values) |
|
262 |
+ return CL_EMEM; |
|
263 |
+ } else { |
|
264 |
+ values = stack[stack_depth].values; |
|
265 |
+ } |
|
257 | 266 |
stack[stack_depth].func = func; |
258 | 267 |
stack[stack_depth].ret = value; |
259 | 268 |
stack[stack_depth].bb = bb; |
260 | 269 |
stack[stack_depth].bb_inst = bb_inst; |
261 |
- stack[stack_depth].values = values; |
|
270 |
+ stack[stack_depth].values = old_values; |
|
262 | 271 |
stack_depth++; |
263 |
- cli_dbgmsg("Executing %d\n", inst->u.ops.funcid); |
|
264 |
- old_values = values; |
|
265 |
- values = allocate_stack(func2); |
|
266 |
- if (!values) |
|
267 |
- return CL_EMEM; |
|
272 |
+//cli_dbgmsg("Executing %d\n", inst->u.ops.funcid); |
|
268 | 273 |
for (i=0;i<func2->numArgs;i++) |
269 | 274 |
values[i] = old_values[inst->u.ops.ops[i]]; |
270 | 275 |
func = func2; |