|
...
|
...
|
@@ -1475,10 +1475,15 @@ void cli_bytecode_destroy(struct cli_bc *bc)
|
|
1475
|
1475
|
#define MAP(val) do { operand_t o = val; \
|
|
1476
|
1476
|
if (o & 0x80000000) {\
|
|
1477
|
1477
|
o &= 0x7fffffff;\
|
|
1478
|
|
- o = bcfunc->numValues + bcfunc->numConstants + o;\
|
|
|
1478
|
+ if (o > bc->num_globals) {\
|
|
|
1479
|
+ cli_errmsg("bytecode: global out of range: %u > %u, for instruction %u in function %u\n",\
|
|
|
1480
|
+ o, bc->num_globals, j, i);\
|
|
|
1481
|
+ return CL_EBYTECODE;\
|
|
|
1482
|
+ }\
|
|
|
1483
|
+ val = 0x80000000 | gmap[o];\
|
|
|
1484
|
+ break;\
|
|
1479
|
1485
|
}\
|
|
1480
|
1486
|
if (o > totValues) {\
|
|
1481
|
|
- printf("%d\n", _FIRST_GLOBAL);\
|
|
1482
|
1487
|
cli_errmsg("bytecode: operand out of range: %u > %u, for instruction %u in function %u\n", o, totValues, j, i);\
|
|
1483
|
1488
|
return CL_EBYTECODE;\
|
|
1484
|
1489
|
}\
|
|
...
|
...
|
@@ -1487,11 +1492,26 @@ void cli_bytecode_destroy(struct cli_bc *bc)
|
|
1487
|
1487
|
static int cli_bytecode_prepare_interpreter(struct cli_bc *bc)
|
|
1488
|
1488
|
{
|
|
1489
|
1489
|
unsigned i, j, k;
|
|
|
1490
|
+ unsigned *gmap;
|
|
|
1491
|
+ bc->numGlobalBytes = 0;
|
|
|
1492
|
+ gmap = cli_malloc(bc->num_globals*sizeof(*gmap));
|
|
|
1493
|
+ if (!gmap)
|
|
|
1494
|
+ return CL_EMEM;
|
|
|
1495
|
+ for (j=0;j<bc->num_globals;j++) {
|
|
|
1496
|
+ uint16_t ty = bc->globaltys[j];
|
|
|
1497
|
+ unsigned align = typealign(bc, ty);
|
|
|
1498
|
+ assert(align);
|
|
|
1499
|
+ bc->numGlobalBytes = (bc->numGlobalBytes + align-1)&(~(align-1));
|
|
|
1500
|
+ gmap[j] = bc->numGlobalBytes;
|
|
|
1501
|
+ bc->numGlobalBytes += typesize(bc, ty);
|
|
|
1502
|
+ }
|
|
1490
|
1503
|
|
|
1491
|
1504
|
for (i=0;i<bc->num_func;i++) {
|
|
1492
|
1505
|
struct cli_bc_func *bcfunc = &bc->funcs[i];
|
|
1493
|
1506
|
unsigned totValues = bcfunc->numValues + bcfunc->numConstants + bc->num_globals;
|
|
1494
|
1507
|
unsigned *map = cli_malloc(sizeof(*map)*totValues);
|
|
|
1508
|
+ if (!map)
|
|
|
1509
|
+ return CL_EMEM;
|
|
1495
|
1510
|
bcfunc->numBytes = 0;
|
|
1496
|
1511
|
for (j=0;j<bcfunc->numValues;j++) {
|
|
1497
|
1512
|
uint16_t ty = bcfunc->types[j];
|
|
...
|
...
|
@@ -1507,14 +1527,6 @@ static int cli_bytecode_prepare_interpreter(struct cli_bc *bc)
|
|
1507
|
1507
|
map[bcfunc->numValues+j] = bcfunc->numBytes;
|
|
1508
|
1508
|
bcfunc->numBytes += 8;
|
|
1509
|
1509
|
}
|
|
1510
|
|
- for (j=0;j<bc->num_globals;j++) {
|
|
1511
|
|
- uint16_t ty = bc->globaltys[j];
|
|
1512
|
|
- unsigned align = typealign(bc, ty);
|
|
1513
|
|
- assert(align);
|
|
1514
|
|
- bcfunc->numBytes = (bcfunc->numBytes + align-1)&(~(align-1));
|
|
1515
|
|
- map[bcfunc->numValues+bcfunc->numConstants+j] = bcfunc->numBytes;
|
|
1516
|
|
- bcfunc->numBytes += typesize(bc, ty);
|
|
1517
|
|
- }
|
|
1518
|
1510
|
for (j=0;j<bcfunc->numInsts;j++) {
|
|
1519
|
1511
|
struct cli_bc_inst *inst = &bcfunc->allinsts[j];
|
|
1520
|
1512
|
inst->dest = map[inst->dest];
|
|
...
|
...
|
@@ -1646,6 +1658,7 @@ static int cli_bytecode_prepare_interpreter(struct cli_bc *bc)
|
|
1646
|
1646
|
}
|
|
1647
|
1647
|
free(map);
|
|
1648
|
1648
|
}
|
|
|
1649
|
+ free(gmap);
|
|
1649
|
1650
|
bc->state = bc_interp;
|
|
1650
|
1651
|
return CL_SUCCESS;
|
|
1651
|
1652
|
}
|