Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make P6{int,num} complain about weird bit sizes, and teach P6int sub-…
…bytes.
  • Loading branch information
arnsholt committed Feb 1, 2013
1 parent e50b1c2 commit 65c6ac0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
43 changes: 34 additions & 9 deletions src/6model/reprs/P6int.c
Expand Up @@ -13,6 +13,11 @@ static REPROps *this_repr;
static void set_int(PARROT_INTERP, STable *st, void *data, INTVAL value);
static INTVAL get_int(PARROT_INTERP, STable *st, void *data);

static void die_bad_bits(PARROT_INTERP) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6int can only handle 1, 2, 4, 8, 16, 32 or 64 bit wide ints.");
}

/* Creates a new type object of this representation, and associates it with
* the given HOW. */
static PMC * type_object_for(PARROT_INTERP, PMC *HOW) {
Expand Down Expand Up @@ -51,11 +56,16 @@ static void compose(PARROT_INTERP, STable *st, PMC *repr_info) {
if(!PMC_IS_NULL(integer)) {
/* TODO: Handle possible unsigned key. How to handle it though, since
* Parrot's INTVAL is inherently signed? */
/* XXX: Make sure bits is a value we actually support. */
/* XXX: I should probably handle the case where no "bits" key is set
* as well, which would set it to 0 (I think?). */
repr_data->bits = VTABLE_get_integer_keyed_str(interp, integer,
Parrot_str_new_constant(interp, "bits"));

if (repr_data->bits != 1 && repr_data->bits != 2 && repr_data->bits != 4
&& repr_data->bits != 8 && repr_data->bits != 16 && repr_data->bits != 32
&& repr_data->bits != 64) {
die_bad_bits(interp);
}
}
}

Expand All @@ -81,8 +91,16 @@ static void copy_to(PARROT_INTERP, STable *st, void *src, void *dest) {
static void set_int(PARROT_INTERP, STable *st, void *data, INTVAL value) {
P6intREPRData *repr_data = (P6intREPRData *) st->REPR_data;

/* XXX: This won't work with int{1,2,4} */
switch (repr_data->bits) {
case 1:
*(Parrot_Int1 *)data = value & 0x1;
break;
case 2:
*(Parrot_Int1 *)data = value & 0x3;
break;
case 4:
*(Parrot_Int1 *)data = value & 0xf;
break;
case 8:
*(Parrot_Int1 *)data = value;
break;
Expand All @@ -96,8 +114,7 @@ static void set_int(PARROT_INTERP, STable *st, void *data, INTVAL value) {
*(Parrot_Int8 *)data = value;
break;
default:
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6int can only handle 8, 16, 32 or 64 bit ints.");
die_bad_bits(interp);
}
}

Expand All @@ -106,8 +123,13 @@ static void set_int(PARROT_INTERP, STable *st, void *data, INTVAL value) {
static INTVAL get_int(PARROT_INTERP, STable *st, void *data) {
P6intREPRData *repr_data = (P6intREPRData *) st->REPR_data;

/* XXX: This won't work with int{1,2,4} */
switch (repr_data->bits) {
case 1:
return (*(Parrot_Int1 *)data) & 0x1;
case 2:
return (*(Parrot_Int1 *)data) & 0x3;
case 4:
return (*(Parrot_Int1 *)data) & 0xf;
case 8:
return *(Parrot_Int1 *)data;
case 16:
Expand All @@ -117,8 +139,7 @@ static INTVAL get_int(PARROT_INTERP, STable *st, void *data) {
case 64:
return *(Parrot_Int8 *)data;
default:
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6int can only handle 8, 16, 32 or 64 bit ints.");
die_bad_bits(interp);
}
}

Expand Down Expand Up @@ -180,6 +201,11 @@ static storage_spec get_storage_spec(PARROT_INTERP, STable *st) {
}

switch (spec.bits) {
/* XXX: Setting align to that of a byte for widths less than 1 byte might
* not be entirely the best choice. */
case 1:
case 2:
case 4:
case 8:
spec.align = ALIGNOF1(Parrot_Int1);
break;
Expand All @@ -193,8 +219,7 @@ static storage_spec get_storage_spec(PARROT_INTERP, STable *st) {
spec.align = ALIGNOF1(Parrot_Int8);
break;
default:
/* TODO: Throw exception for unknown sizes. */
break;
die_bad_bits(interp);
}

return spec;
Expand Down
20 changes: 12 additions & 8 deletions src/6model/reprs/P6num.c
Expand Up @@ -13,6 +13,11 @@ static REPROps *this_repr;
static void set_num(PARROT_INTERP, STable *st, void *data, FLOATVAL value);
static FLOATVAL get_num(PARROT_INTERP, STable *st, void *data);

static void die_bad_bits(PARROT_INTERP) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6num can only handle 32 or 64 bit wide floats.");
}

/* Creates a new type object of this representation, and associates it with
* the given HOW. */
static PMC * type_object_for(PARROT_INTERP, PMC *HOW) {
Expand Down Expand Up @@ -49,11 +54,14 @@ static void compose(PARROT_INTERP, STable *st, PMC *repr_info) {

repr_data->bits = sizeof(FLOATVAL)*8;
if(!PMC_IS_NULL(info)) {
/* XXX: Make sure bits is a value we actually support. */
/* XXX: I should probably handle the case where no "bits" key is set
* as well, which would set it to 0 (I think?). */
repr_data->bits = VTABLE_get_integer_keyed_str(interp, info,
Parrot_str_new_constant(interp, "bits"));

if(repr_data->bits != 32 && repr_data->bits != 64) {
die_bad_bits(interp);
}
}
}

Expand Down Expand Up @@ -114,8 +122,7 @@ static void set_num(PARROT_INTERP, STable *st, void *data, FLOATVAL value) {
*(Parrot_Float8 *)data = value;
break;
default:
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6num can only handle 32 or 64 bit floats.");
die_bad_bits(interp);
}
}

Expand All @@ -130,8 +137,7 @@ static FLOATVAL get_num(PARROT_INTERP, STable *st, void *data) {
case 64:
return *(Parrot_Float8 *)data;
default:
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6num can only handle 32 or 64 bit floats.");
die_bad_bits(interp);
}
}

Expand Down Expand Up @@ -171,8 +177,6 @@ static storage_spec get_storage_spec(PARROT_INTERP, STable *st) {
spec.boxed_primitive = STORAGE_SPEC_BP_NUM;
spec.can_box = STORAGE_SPEC_CAN_BOX_NUM;

/*spec.bits = sizeof(FLOATVAL) * 8;
spec.align = ALIGNOF1(void *);*/
if (repr_data && repr_data->bits) {
spec.bits = repr_data->bits;
}
Expand All @@ -188,7 +192,7 @@ static storage_spec get_storage_spec(PARROT_INTERP, STable *st) {
spec.align = ALIGNOF1(Parrot_Float8);
break;
default:
/* TODO: Throw exception for unknown sizes. */
die_bad_bits(interp);
break;
}

Expand Down

0 comments on commit 65c6ac0

Please sign in to comment.