Browse code

bytecode: prepare for implementing gep.

Török Edvin authored on 2009/08/24 19:43:08
Showing 3 changed files
... ...
@@ -69,13 +69,16 @@ static unsigned typesize(const struct cli_bc *bc, uint16_t type)
69 69
 	return 4;
70 70
     if (type <= 64)
71 71
 	return 8;
72
-    return 0;
72
+    return bc->types[type-65].size;
73 73
 }
74 74
 
75 75
 static unsigned typealign(const struct cli_bc *bc, uint16_t type)
76 76
 {
77
-    unsigned size = typesize(bc, type);
78
-    return size ? size : 1;
77
+    if (type <= 64) {
78
+	unsigned size = typesize(bc, type);
79
+	return size ? size : 1;
80
+    }
81
+    return bc->types[type-65].align;
79 82
 }
80 83
 
81 84
 int cli_bytecode_context_setfuncid(struct cli_bc_ctx *ctx, const struct cli_bc *bc, unsigned funcid)
... ...
@@ -456,6 +459,7 @@ static void add_static_types(struct cli_bc *bc)
456 456
 	bc->types[i].kind = PointerType;
457 457
 	bc->types[i].numElements = 1;
458 458
 	bc->types[i].containedTypes = &containedTy[i];
459
+	bc->types[i].size = bc->types[i].align = sizeof(void*);
459 460
     }
460 461
 }
461 462
 
... ...
@@ -485,6 +489,7 @@ static int parseTypes(struct cli_bc *bc, unsigned char *buffer)
485 485
 	switch (t) {
486 486
 	    case 1:
487 487
 		ty->kind = FunctionType;
488
+		ty->size = ty->align = sizeof(void*);
488 489
 		parseType(bc, ty, buffer, &offset, len, &ok);
489 490
 		if (!ok) {
490 491
 		    cli_errmsg("Error parsing type %u\n", i);
... ...
@@ -494,6 +499,7 @@ static int parseTypes(struct cli_bc *bc, unsigned char *buffer)
494 494
 	    case 2:
495 495
 	    case 3:
496 496
 		ty->kind = (t == 2) ? StructType : PackedStructType;
497
+		ty->size = ty->align = 0;/* TODO:calculate size/align of structs */
497 498
 		parseType(bc, ty, buffer, &offset, len, &ok);
498 499
 		if (!ok) {
499 500
 		    cli_errmsg("Error parsing type %u\n", i);
... ...
@@ -524,6 +530,12 @@ static int parseTypes(struct cli_bc *bc, unsigned char *buffer)
524 524
 		    cli_errmsg("Error parsing type %u\n", i);
525 525
 		    return CL_EMALFDB;
526 526
 		}
527
+		if (t == 5) {
528
+		    ty->size = ty->align = sizeof(void);
529
+		} else {
530
+		    ty->size = ty->numElements*typesize(bc, ty->containedTypes[0]);
531
+		    ty->align = typealign(bc, ty->containedTypes[0]);
532
+		}
527 533
 		break;
528 534
 	    default:
529 535
 		cli_errmsg("Invalid type kind: %u\n", t);
... ...
@@ -1074,10 +1086,6 @@ static int cli_bytecode_prepare_interpreter(struct cli_bc *bc)
1074 1074
 	for (j=0;j<bcfunc->numValues;j++) {
1075 1075
 	    uint16_t ty = bcfunc->types[j];
1076 1076
 	    unsigned align;
1077
-	    if (ty > 64) {
1078
-		cli_errmsg("Bytecode: non-integer types not yet implemented\n");
1079
-		return CL_EMALFDB;
1080
-	    }
1081 1077
 	    align = typealign(bc, ty);
1082 1078
 	    bcfunc->numBytes  = (bcfunc->numBytes + align-1)&(~(align-1));
1083 1079
 	    map[j] = bcfunc->numBytes;
... ...
@@ -1179,6 +1187,15 @@ static int cli_bytecode_prepare_interpreter(struct cli_bc *bc)
1179 1179
 		case OP_LOAD:
1180 1180
 		    MAP(inst->u.unaryop);
1181 1181
 		    break;
1182
+		case OP_GEP1:
1183
+		    MAP(inst->u.binop[0]);
1184
+		    MAP(inst->u.binop[1]);
1185
+		    break;
1186
+		case OP_GEP2:
1187
+		    MAP(inst->u.three[0]);
1188
+		    MAP(inst->u.three[1]);
1189
+		    MAP(inst->u.three[2]);
1190
+		    break;
1182 1191
 		default:
1183 1192
 		    cli_dbgmsg("Unhandled opcode: %d\n", inst->opcode);
1184 1193
 		    return CL_EBYTECODE;
... ...
@@ -601,6 +601,10 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
601 601
 		old_values = values;
602 602
 		stack_entry = allocate_stack(&stack, stack_entry, func2, func, inst->dest,
603 603
 					     bb, bb_inst);
604
+		if (!stack_entry) {
605
+		    stop = CL_EMEM;
606
+		    break;
607
+		}
604 608
 		values = stack_entry->values;
605 609
 		TRACE_EXEC(inst->u.ops.funcid, inst->dest, inst->type, stack_depth);
606 610
 		if (stack_depth > 10000) {
... ...
@@ -764,7 +768,7 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
764 764
 		ptr->una_u64 = v;
765 765
 		break;
766 766
 	    }
767
-
767
+	    /* TODO: implement OP_GEP1, OP_GEP2, OP_GEPN */
768 768
 	    default:
769 769
 		cli_errmsg("Opcode %u of type %u is not implemented yet!\n",
770 770
 			   inst->interp_op/5, inst->interp_op%5);
... ...
@@ -773,7 +777,8 @@ int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct
773 773
 	}
774 774
 	bb_inst++;
775 775
 	inst++;
776
-	CHECK_GT(bb->numInsts, bb_inst);
776
+	if (bb)
777
+	    CHECK_GT(bb->numInsts, bb_inst);
777 778
     } while (stop == CL_SUCCESS);
778 779
 
779 780
     cli_stack_destroy(&stack);
... ...
@@ -33,7 +33,9 @@ enum derived_t {
33 33
 struct cli_bc_type {
34 34
     enum derived_t kind;
35 35
     uint16_t *containedTypes;
36
+    uint32_t size;
36 37
     unsigned numElements;
38
+    unsigned align;
37 39
 };
38 40
 
39 41
 typedef int32_t (*cli_apicall_int2)(int32_t, int32_t);