Browse code

Add support for type loader.

Török Edvin authored on 2009/07/31 21:28:36
Showing 7 changed files
... ...
@@ -310,6 +310,7 @@ static inline char *readString(const unsigned char *p, unsigned *off, unsigned l
310 310
     }
311 311
     return str;
312 312
 }
313
+
313 314
 static int parseHeader(struct cli_bc *bc, unsigned char *buffer)
314 315
 {
315 316
     uint64_t magic1;
... ...
@@ -339,6 +340,7 @@ static int parseHeader(struct cli_bc *bc, unsigned char *buffer)
339 339
     bc->metadata.maxMem = readNumber(buffer, &offset, len, &ok);
340 340
     bc->metadata.maxTime = readNumber(buffer, &offset, len, &ok);
341 341
     bc->metadata.targetExclude = readString(buffer, &offset, len, &ok);
342
+    bc->num_types = readNumber(buffer, &offset, len, &ok);
342 343
     bc->num_func = readNumber(buffer, &offset, len, &ok);
343 344
     bc->state = bc_loaded;
344 345
     if (!ok) {
... ...
@@ -364,6 +366,139 @@ static int parseHeader(struct cli_bc *bc, unsigned char *buffer)
364 364
 	cli_errmsg("Out of memory allocating %u functions\n", bc->num_func);
365 365
 	return CL_EMEM;
366 366
     }
367
+    bc->types = cli_calloc(bc->num_types, sizeof(*bc->types));
368
+    if (!bc->types) {
369
+	cli_errmsg("Out of memory allocating %u types\n", bc->num_types);
370
+	return CL_EMEM;
371
+    }
372
+    return CL_SUCCESS;
373
+}
374
+
375
+static uint16_t readTypeID(struct cli_bc *bc, unsigned char *buffer,
376
+			   unsigned *offset, unsigned len, char *ok)
377
+{
378
+    uint64_t t = readNumber(buffer, offset, len, ok);
379
+    if (!ok)
380
+	return ~0u;
381
+    if (t >= bc->num_types + bc->start_tid) {
382
+	cli_errmsg("Invalid type id: %u\n", t);
383
+	*ok = 0;
384
+	return ~0u;
385
+    }
386
+    return t;
387
+}
388
+
389
+static void parseType(struct cli_bc *bc, struct cli_bc_type *ty,
390
+		      unsigned char *buffer, unsigned *off, unsigned len,
391
+		      char *ok)
392
+{
393
+    unsigned j;
394
+
395
+    ty->numElements = readFixedNumber(buffer, off, len, ok, 1);
396
+    if (!ok) {
397
+	cli_errmsg("Error parsing type\n");
398
+	*ok = 0;
399
+	return;
400
+    }
401
+    ty->containedTypes = cli_malloc(sizeof(*ty->containedTypes)*ty->numElements);
402
+    if (!ty->containedTypes) {
403
+	cli_errmsg("Out of memory allocating %u types\n", ty->numElements);
404
+	*ok = 0;
405
+	return;
406
+    }
407
+    for (j=0;j<ty->numElements;j++) {
408
+	ty->containedTypes[j] = readTypeID(bc, buffer, off, len, ok);
409
+    }
410
+}
411
+
412
+static uint16_t containedTy[] = {8,16,32,64};
413
+
414
+static void add_static_types(struct cli_bc *bc)
415
+{
416
+    unsigned i;
417
+    for (i=0;i<4;i++) {
418
+	bc->types[i].kind = PointerType;
419
+	bc->types[i].numElements = 1;
420
+	bc->types[i].containedTypes = &containedTy[i];
421
+    }
422
+}
423
+
424
+static int parseTypes(struct cli_bc *bc, unsigned char *buffer)
425
+{
426
+    unsigned i, j, offset = 1, ok=1, len = strlen(buffer);
427
+    if (buffer[0] != 'T') {
428
+	cli_errmsg("Invalid function types header: %c\n", buffer[0]);
429
+	return CL_EMALFDB;
430
+    }
431
+    bc->start_tid = readFixedNumber(buffer, &offset, len, &ok, 2);
432
+    if (bc->start_tid != BC_START_TID) {
433
+	cli_warnmsg("Type start id mismatch: %u != %u\n", bc->start_tid,
434
+		    BC_START_TID);
435
+	return CL_BREAK;
436
+    }
437
+    add_static_types(bc);
438
+    for (i=(BC_START_TID - 64);i<bc->num_types;i++) {
439
+	struct cli_bc_type *ty = &bc->types[i];
440
+	uint8_t t = readFixedNumber(buffer, &offset, len, &ok, 1);
441
+	uint16_t tid;
442
+	if (!ok) {
443
+	    cli_errmsg("Error reading type kind\n");
444
+	    return CL_EMALFDB;
445
+	}
446
+	switch (t) {
447
+	    case 1:
448
+		ty->kind = FunctionType;
449
+		parseType(bc, ty, buffer, &offset, len, &ok);
450
+		if (!ok) {
451
+		    cli_errmsg("Error parsing type %u\n", i);
452
+		    return CL_EMALFDB;
453
+		}
454
+		break;
455
+	    case 2:
456
+	    case 3:
457
+		ty->kind = (t == 2) ? StructType : PackedStructType;
458
+		parseType(bc, ty, buffer, &offset, len, &ok);
459
+		if (!ok) {
460
+		    cli_errmsg("Error parsing type %u\n", i);
461
+		    return CL_EMALFDB;
462
+		}
463
+		break;
464
+	    case 4:
465
+		ty->kind = ArrayType;
466
+		/* number of elements of array, not subtypes! */
467
+		ty->numElements = readNumber(buffer, &offset, len, &ok);
468
+		if (!ok) {
469
+		    cli_errmsg("Error parsing type %u\n", i);
470
+		    return CL_EMALFDB;
471
+		}
472
+		/* fall-through */
473
+	    case 5:
474
+		if (t == 5) {
475
+		    ty->kind = PointerType;
476
+		    ty->numElements = 1;
477
+		}
478
+		ty->containedTypes = cli_malloc(sizeof(*ty->containedTypes));
479
+		if (!ty->containedTypes) {
480
+		    cli_errmsg("Out of memory allocating containedType\n");
481
+		    return CL_EMALFDB;
482
+		}
483
+		ty->containedTypes[0] = readTypeID(bc, buffer, &offset, len, &ok);
484
+		if (!ok) {
485
+		    cli_errmsg("Error parsing type %u\n", i);
486
+		    return CL_EMALFDB;
487
+		}
488
+		break;
489
+	    default:
490
+		cli_errmsg("Invalid type kind: %u\n", t);
491
+		return CL_EMALFDB;
492
+	}
493
+    }
494
+    return CL_SUCCESS;
495
+}
496
+
497
+static int parseApis(struct cli_bc *bc, unsigned char *buffer)
498
+{
499
+    //TODO
367 500
     return CL_SUCCESS;
368 501
 }
369 502
 
... ...
@@ -629,6 +764,8 @@ static int parseBB(struct cli_bc *bc, unsigned func, unsigned bb, unsigned char
629 629
 
630 630
 enum parse_state {
631 631
     PARSE_BC_HEADER=0,
632
+    PARSE_BC_TYPES,
633
+    PARSE_BC_APIS,
632 634
     PARSE_FUNC_HEADER,
633 635
     PARSE_BB
634 636
 };
... ...
@@ -656,6 +793,24 @@ int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio)
656 656
 		    cli_errmsg("Error at bytecode line %u\n", row);
657 657
 		    return rc;
658 658
 		}
659
+		state = PARSE_BC_TYPES;
660
+		break;
661
+	    case PARSE_BC_TYPES:
662
+		rc = parseTypes(bc, (unsigned char*)buffer);
663
+		if (rc != CL_SUCCESS) {
664
+		    cli_errmsg("Error at bytecode line %u\n", row);
665
+		    return rc;
666
+		}
667
+		state = PARSE_BC_APIS;
668
+		break;
669
+	    case PARSE_BC_APIS:
670
+		rc = parseApis(bc, (unsigned char*)buffer);
671
+		if (rc == CL_BREAK) /* skip */
672
+		    return CL_SUCCESS;
673
+		if (rc != CL_SUCCESS) {
674
+		    cli_errmsg("Error at bytecode line %u\n", row);
675
+		    return rc;
676
+		}
659 677
 		state = PARSE_FUNC_HEADER;
660 678
 		break;
661 679
 	    case PARSE_FUNC_HEADER:
... ...
@@ -29,6 +29,7 @@ struct cli_bc_ctx;
29 29
 struct cli_bc_func;
30 30
 struct cli_bc_value;
31 31
 struct cli_bc_inst;
32
+struct cli_bc_type;
32 33
 
33 34
 enum bc_state {
34 35
     bc_loaded,
... ...
@@ -41,9 +42,12 @@ struct cli_bc {
41 41
   char *sigmaker;
42 42
   unsigned id;
43 43
   struct bytecode_metadata metadata;
44
+  unsigned num_types;
44 45
   unsigned num_func;
45 46
   struct cli_bc_func *funcs;
47
+  struct cli_bc_type *types;
46 48
   enum bc_state state;
49
+  uint16_t start_tid;
47 50
 };
48 51
 
49 52
 struct cli_bc_ctx *cli_bytecode_context_alloc(void);
... ...
@@ -22,6 +22,8 @@
22 22
 
23 23
 #ifndef BYTECODE_PRIV_H
24 24
 #define BYTECODE_PRIV_H
25
+
26
+#include "type_desc.h"
25 27
 typedef uint32_t operand_t;
26 28
 typedef uint16_t bbid_t;
27 29
 typedef uint16_t funcid_t;
... ...
@@ -66,6 +66,7 @@ enum bc_opcode {
66 66
   OP_ICMP_SLT,
67 67
   OP_SELECT,
68 68
   OP_CALL_DIRECT,
69
+  OP_CALL_API,
69 70
   OP_COPY,
70 71
   OP_GEP1,
71 72
   OP_GEP2,
... ...
@@ -88,10 +89,13 @@ static const unsigned char operand_counts[] = {
88 88
   /* SELECT */
89 89
   3,
90 90
   /* CALLs have variable number of operands */
91
-  0,
91
+  0, 0,
92 92
   /* OP_COPY */
93 93
   2,
94 94
   /* OP_GEP1, OP_GEP2, OP_GEPN, OP_STORE, OP_LOAD*/
95 95
   2, 3, 0, 2, 1
96 96
 };
97
+
98
+#define BC_START_TID 69
99
+
97 100
 #endif
98 101
new file mode 100644
... ...
@@ -0,0 +1,29 @@
0
+enum derived_t {
1
+  FunctionType,
2
+  PointerType,
3
+  StructType,
4
+  PackedStructType,
5
+  ArrayType
6
+};
7
+
8
+struct cli_bc_type {
9
+    enum derived_t kind;
10
+    uint16_t *containedTypes;
11
+    unsigned numElements;
12
+};
13
+
14
+typedef int32_t (*cli_apicall_int2)(int32_t, int32_t);
15
+typedef int32_t (*cli_apicall_pointer)(void*, uint32_t);
16
+
17
+struct cli_apicall {
18
+    const char *name;
19
+    const struct cli_bc_type *type;
20
+    uint8_t kind;
21
+};
22
+
23
+extern const struct cli_bc_type cli_apicall_types[];
24
+
25
+extern const struct cli_apicall cli_apicalls[];
26
+extern const cli_apicall_int2 cli_apicalls0[];
27
+extern const cli_apicall_pointer cli_apicalls1[];
28
+extern const unsigned cli_apicall_maxapi;
... ...
@@ -1,4 +1,6 @@
1
-ClamBCaa`|`````|`bbep`clamcoincidencejb
1
+ClamBCaa`|`````|`aebbep`clamcoincidencejb
2
+Ted
3
+E``
2 4
 A`Lbabb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bFbbbaa
3 5
 Bb`b`oa`abb`baaoa`acb`baboa`adb`bacoa`aeb`badoa`afb`baeoa`agb`bafoa`ahb`bagoa`aib`bahoa`ajb`baioa`akb`bajoa`alb`bakoa`amb`baloa`anb`bamoa`aob`banoa`b`ab`baooa`baab`bb`aoa`bbab`bbaaa`aa`b`bbbaa`baaabb`bbcaa`bbaacb`bbdaa`bcaadb`bbeaa`bdaaeb`bbfaa`beaafb`bbgaa`bfaagb`bbhaa`bgaahb`bbiaa`bhaaib`bbjaa`biaajb`bbkaa`bjaakb`bblaa`bkaalb`bbmaa`blaamb`bbnaa`bmaanb`bboaa`bnaaob`bb`ba`boab`aTcab`bb`bE
4 6
 A`Lb`cahaab`bahaab`bahaab`bahaab`bb`aaab`bb`aaab`bb`aaab`bb`aaab`bb`baab`bb`baab`bb`baab`bb`baab`bb`daab`bb`daab`bb`daab`bb`daab`bFbaebab
... ...
@@ -1,3 +1,5 @@
1
-ClamBCaa`|`````|`aap`clamcoincidencejb
1
+ClamBCaa`|`````|`aeaap`clamcoincidencejb
2
+Ted
3
+E``
2 4
 A`L`Faaaa
3 5
 BTcaHm``odcbab`bE