Browse code

More fixes for global vars in the interpreter.

Török Edvin authored on 2010/03/22 07:01:28
Showing 3 changed files
... ...
@@ -1461,10 +1461,16 @@ void cli_bytecode_destroy(struct cli_bc *bc)
1461 1461
     }\
1462 1462
     val = map[o]; } while (0)
1463 1463
 
1464
+static inline int64_t ptr_compose(int32_t id, uint32_t offset)
1465
+{
1466
+    uint64_t i = id;
1467
+    return (i << 32) | offset;
1468
+}
1469
+
1464 1470
 static int cli_bytecode_prepare_interpreter(struct cli_bc *bc)
1465 1471
 {
1466 1472
     unsigned i, j, k;
1467
-    unsigned *gmap;
1473
+    uint64_t *gmap;
1468 1474
     bc->numGlobalBytes = 0;
1469 1475
     gmap = cli_malloc(bc->num_globals*sizeof(*gmap));
1470 1476
     if (!gmap)
... ...
@@ -1480,6 +1486,24 @@ static int cli_bytecode_prepare_interpreter(struct cli_bc *bc)
1480 1480
     bc->globalBytes = cli_calloc(1, bc->numGlobalBytes);
1481 1481
     if (!bc->globalBytes)
1482 1482
 	return CL_EMEM;
1483
+    for (j=0;j<bc->num_globals;j++) {
1484
+	struct cli_bc_type *ty;
1485
+	if (bc->globaltys[j] < 65)
1486
+	    continue;
1487
+	ty = &bc->types[bc->globaltys[j]-65];
1488
+	switch (ty->kind) {
1489
+	    case DPointerType:
1490
+		*(uint64_t*)&bc->globalBytes[gmap[j]] =
1491
+		    ptr_compose(bc->globals[j][1] - _FIRST_GLOBAL + 1,
1492
+				bc->globals[j][0]);
1493
+		break;
1494
+	    default:
1495
+		/*TODO*/
1496
+		if (!bc->globals[j][1])
1497
+		    continue; /* null */
1498
+		break;
1499
+	}
1500
+    }
1483 1501
 
1484 1502
     for (i=0;i<bc->num_func;i++) {
1485 1503
 	struct cli_bc_func *bcfunc = &bc->funcs[i];
... ...
@@ -272,7 +272,7 @@ static always_inline struct stack_entry *pop_stack(struct stack *stack,
272 272
  do {\
273 273
      if (p&0x80000000) {\
274 274
 	 uint32_t pg = p&0x7fffffff;\
275
-	 READNfrom(bc->numGlobalBytes, bc->globals, x, n, pg);\
275
+	 READNfrom(bc->numGlobalBytes, bc->globalBytes, x, n, pg);\
276 276
      } else {\
277 277
 	 READNfrom(func->numBytes, values, x, n, p);\
278 278
      }\
... ...
@@ -287,7 +287,7 @@ static always_inline struct stack_entry *pop_stack(struct stack *stack,
287 287
 
288 288
 #define PSIZE sizeof(int64_t)
289 289
 #define READP(x, p, asize) { int64_t iptr__;\
290
-    READN(iptr__, 8, p);\
290
+    READN(iptr__, 64, p);\
291 291
     x = ptr_torealptr(&ptrinfos, iptr__, (asize));\
292 292
     if (!x) {\
293 293
 	stop = CL_EBYTECODE;\
... ...
@@ -479,22 +479,29 @@ static inline int32_t ptr_register_stack(struct ptr_infos *infos,
479 479
     return ptr_compose(-(n-1), 0);
480 480
 }
481 481
 
482
-static inline int64_t ptr_register_glob(struct ptr_infos *infos,
483
-					void *values, uint32_t size)
482
+static inline int64_t ptr_register_glob_fixedid(struct ptr_infos *infos,
483
+						void *values, uint32_t size, unsigned n)
484 484
 {
485
-    unsigned n = infos->nstacks + 1;
486
-    struct ptr_info *sinfos = cli_realloc(infos->stack_infos,
487
-					  sizeof(*sinfos)*n);
488
-    if (!sinfos)
489
-	return 0;
490
-    infos->stack_infos = sinfos;
491
-    infos->nstacks = n;
492
-    sinfos = &sinfos[n-1];
485
+    struct ptr_info *sinfos;
486
+    if (n > infos->nglobs) {
487
+	sinfos = cli_realloc(infos->glob_infos, sizeof(*sinfos)*n);
488
+	if (!sinfos)
489
+	    return 0;
490
+	infos->glob_infos = sinfos;
491
+	infos->nglobs = n;
492
+    }
493
+    sinfos = &infos->glob_infos[n-1];
493 494
     sinfos->base = values;
494 495
     sinfos->size = size;
495 496
     return ptr_compose(n, 0);
496 497
 }
497 498
 
499
+static inline int64_t ptr_register_glob(struct ptr_infos *infos,
500
+					void *values, uint32_t size)
501
+{
502
+    return ptr_register_glob_fixedid(infos, values, size, infos->nglobs+1);
503
+}
504
+
498 505
 static inline int64_t ptr_index(int64_t ptr, uint32_t off)
499 506
 {
500 507
     int32_t ptrid = ptr >> 32;
... ...
@@ -544,6 +551,29 @@ static always_inline int check_sdivops(int64_t op0, int64_t op1)
544 544
     return op1 == 0 || (op0 == -1 && op1 ==  (-9223372036854775807LL-1LL));
545 545
 }
546 546
 
547
+static unsigned globaltypesize(uint16_t id)
548
+{
549
+    const struct cli_bc_type *ty;
550
+    if (id <= 64)
551
+	return (id + 7)/8;
552
+    if (id <= 69)
553
+	return 8; /* ptr */
554
+    ty = &cli_apicall_types[id - 69];
555
+    switch (ty->kind) {
556
+	case DArrayType:
557
+	    return ty->numElements*globaltypesize(ty->containedTypes[0]);
558
+	case DStructType:
559
+	case DPackedStructType:
560
+	    {
561
+		unsigned i, s = 0;
562
+		for (i=0;i<ty->numElements;i++)
563
+		    s += globaltypesize(ty->containedTypes[i]);
564
+		return s;
565
+	    }
566
+    }
567
+    return 0;
568
+}
569
+
547 570
 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)
548 571
 {
549 572
     unsigned i, j, stack_depth=0, bb_inst=0, stop=0, pc=0;
... ...
@@ -557,6 +587,13 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
557 557
 
558 558
     memset(&ptrinfos, 0, sizeof(ptrinfos));
559 559
     memset(&stack, 0, sizeof(stack));
560
+    for (i=0;i < cli_apicall_maxglobal - _FIRST_GLOBAL; i++) {
561
+	const struct cli_apiglobal *g = &cli_globals[i];
562
+	void *apiglobal = (void*)(((char*)&ctx->hooks) + g->offset);
563
+	uint32_t size = globaltypesize(g->type);
564
+	ptr_register_glob_fixedid(&ptrinfos, apiglobal, size, g->globalid - _FIRST_GLOBAL+1);
565
+    }
566
+
560 567
     do {
561 568
 	pc++;
562 569
 	switch (inst->interp_op) {
... ...
@@ -484,7 +484,6 @@ private:
484 484
     Constant *buildConstant(const Type *Ty, uint64_t *components, unsigned &c)
485 485
     {
486 486
         if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
487
-
488 487
           Value *idxs[1] = {
489 488
 	      ConstantInt::get(Type::getInt64Ty(Context), components[c++])
490 489
 	  };