diff --git a/rts/idris_bitstring.c b/rts/idris_bitstring.c index 8f11aa3e67..c3ae52d7ce 100644 --- a/rts/idris_bitstring.c +++ b/rts/idris_bitstring.c @@ -2,131 +2,94 @@ #include "idris_rts.h" -VAL idris_b8(VM *vm, VAL a) { - uint8_t A = GETINT(a); - VAL cl = allocate(sizeof(Closure), 0); +VAL idris_b8const(VM *vm, uint8_t a) { + Bits8 * cl = iallocate(vm, sizeof(*cl), 0); SETTY(cl, CT_BITS8); - cl->info.bits8 = (uint8_t) A; - return cl; + cl->bits8 = a; + return (VAL)cl; } -VAL idris_b16(VM *vm, VAL a) { - uint16_t A = GETINT(a); - VAL cl = allocate(sizeof(Closure), 0); +VAL idris_b16const(VM *vm, uint16_t a) { + Bits16 * cl = iallocate(vm, sizeof(*cl), 0); SETTY(cl, CT_BITS16); - cl->info.bits16 = (uint16_t) A; - return cl; + cl->bits16 = a; + return (VAL)cl; } -VAL idris_b32(VM *vm, VAL a) { - uint32_t A = GETINT(a); - VAL cl = allocate(sizeof(Closure), 0); +VAL idris_b32const(VM *vm, uint32_t a) { + Bits32 * cl = iallocate(vm, sizeof(*cl), 0); SETTY(cl, CT_BITS32); - cl->info.bits32 = (uint32_t) A; - return cl; + cl->bits32 = a; + return (VAL)cl; } -VAL idris_b64(VM *vm, VAL a) { - uint64_t A = GETINT(a); - VAL cl = allocate(sizeof(Closure), 0); +VAL idris_b64const(VM *vm, uint64_t a) { + Bits64 * cl = iallocate(vm, sizeof(*cl), 0); SETTY(cl, CT_BITS64); - cl->info.bits64 = (uint64_t) A; - return cl; + cl->bits64 = a; + return (VAL)cl; } -VAL idris_castB32Int(VM *vm, VAL a) { - return MKINT(GETBITS32(a)); +VAL idris_b8(VM *vm, VAL a) { + return idris_b8const(vm, GETINT(a)); } -VAL idris_b8const(VM *vm, uint8_t a) { - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = a; - return cl; +VAL idris_b16(VM *vm, VAL a) { + return idris_b16const(vm, GETINT(a)); } -VAL idris_b16const(VM *vm, uint16_t a) { - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = a; - return cl; +VAL idris_b32(VM *vm, VAL a) { + return idris_b32const(vm, GETINT(a)); } -VAL idris_b32const(VM *vm, uint32_t a) { - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = a; - return cl; +VAL idris_b64(VM *vm, VAL a) { + return idris_b64const(vm, GETINT(a)); } -VAL idris_b64const(VM *vm, uint64_t a) { - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = a; - return cl; +VAL idris_castB32Int(VM *vm, VAL a) { + return MKINT(GETBITS32(a)); } VAL idris_b8Plus(VM *vm, VAL a, VAL b) { uint8_t A = GETBITS8(a); uint8_t B = GETBITS8(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = A + B; - return cl; + return idris_b8const(vm, A + B); } VAL idris_b8Minus(VM *vm, VAL a, VAL b) { uint8_t A = GETBITS8(a); uint8_t B = GETBITS8(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = A - B; - return cl; + return idris_b8const(vm, A - B); } VAL idris_b8Times(VM *vm, VAL a, VAL b) { uint8_t A = GETBITS8(a); uint8_t B = GETBITS8(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = A * B; - return cl; + return idris_b8const(vm, A * B); } VAL idris_b8UDiv(VM *vm, VAL a, VAL b) { uint8_t A = GETBITS8(a); uint8_t B = GETBITS8(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = A / B; - return cl; + return idris_b8const(vm, A / B); } VAL idris_b8SDiv(VM *vm, VAL a, VAL b) { - uint8_t A = GETBITS8(a); - uint8_t B = GETBITS8(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = (uint8_t) (((int8_t) A) / ((int8_t) B)); - return cl; + int8_t A = GETBITS8(a); + int8_t B = GETBITS8(b); + return idris_b8const(vm, A / B); } VAL idris_b8URem(VM *vm, VAL a, VAL b) { uint8_t A = GETBITS8(a); uint8_t B = GETBITS8(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = A % B; - return cl; + return idris_b8const(vm, A % B); } VAL idris_b8SRem(VM *vm, VAL a, VAL b) { - uint8_t A = GETBITS8(a); - uint8_t B = GETBITS8(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = (uint8_t) (((int8_t) A) % ((int8_t) B)); - return cl; + int8_t A = GETBITS8(a); + int8_t B = GETBITS8(b); + return idris_b8const(vm, A % B); } VAL idris_b8Lt(VM *vm, VAL a, VAL b) { @@ -151,127 +114,85 @@ VAL idris_b8Gte(VM *vm, VAL a, VAL b) { VAL idris_b8Compl(VM *vm, VAL a) { uint8_t A = GETBITS8(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = ~ A; - return cl; + return idris_b8const(vm, ~ A); } VAL idris_b8And(VM *vm, VAL a, VAL b) { uint8_t A = GETBITS8(a); uint8_t B = GETBITS8(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = A & B; - return cl; + return idris_b8const(vm, A & B); } VAL idris_b8Or(VM *vm, VAL a, VAL b) { uint8_t A = GETBITS8(a); uint8_t B = GETBITS8(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = A | B; - return cl; + return idris_b8const(vm, A | B); } VAL idris_b8Xor(VM *vm, VAL a, VAL b) { uint8_t A = GETBITS8(a); uint8_t B = GETBITS8(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = A ^ B; - return cl; + return idris_b8const(vm, A ^ B); } VAL idris_b8Shl(VM *vm, VAL a, VAL b) { uint8_t A = GETBITS8(a); uint8_t B = GETBITS8(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = A << B; - return cl; + return idris_b8const(vm, A << B); } VAL idris_b8LShr(VM *vm, VAL a, VAL b) { uint8_t A = GETBITS8(a); uint8_t B = GETBITS8(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = A >> B; - return cl; + return idris_b8const(vm, A >> B); } VAL idris_b8AShr(VM *vm, VAL a, VAL b) { - uint8_t A = GETBITS8(a); - uint8_t B = GETBITS8(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = (uint8_t) (((int8_t) A) >> ((int8_t) B)); - return cl; + int8_t A = GETBITS8(a); + int8_t B = GETBITS8(b); + return idris_b8const(vm, A >> B); } VAL idris_b16Plus(VM *vm, VAL a, VAL b) { uint16_t A = GETBITS16(a); uint16_t B = GETBITS16(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = A + B; - return cl; + return idris_b16const(vm, A + B); } VAL idris_b16Minus(VM *vm, VAL a, VAL b) { uint16_t A = GETBITS16(a); uint16_t B = GETBITS16(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = A - B; - return cl; + return idris_b16const(vm, A - B); } VAL idris_b16Times(VM *vm, VAL a, VAL b) { uint16_t A = GETBITS16(a); uint16_t B = GETBITS16(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = A * B; - return cl; + return idris_b16const(vm, A * B); } VAL idris_b16UDiv(VM *vm, VAL a, VAL b) { uint16_t A = GETBITS16(a); uint16_t B = GETBITS16(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = A / B; - return cl; + return idris_b16const(vm, A / B); } VAL idris_b16SDiv(VM *vm, VAL a, VAL b) { - uint16_t A = GETBITS16(a); - uint16_t B = GETBITS16(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = (uint16_t) (((int16_t) A) / ((int16_t) B)); - return cl; + int16_t A = GETBITS16(a); + int16_t B = GETBITS16(b); + return idris_b16const(vm, A / B); } VAL idris_b16URem(VM *vm, VAL a, VAL b) { uint16_t A = GETBITS16(a); uint16_t B = GETBITS16(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = A % B; - return cl; + return idris_b16const(vm, A % B); } VAL idris_b16SRem(VM *vm, VAL a, VAL b) { - uint16_t A = GETBITS16(a); - uint16_t B = GETBITS16(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = (uint16_t) (((int16_t) A) % ((int16_t) B)); - return cl; + int16_t A = GETBITS16(a); + int16_t B = GETBITS16(b); + return idris_b16const(vm, A % B); } VAL idris_b16Lt(VM *vm, VAL a, VAL b) { @@ -296,127 +217,85 @@ VAL idris_b16Gte(VM *vm, VAL a, VAL b) { VAL idris_b16Compl(VM *vm, VAL a) { uint16_t A = GETBITS16(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = ~ A; - return cl; + return idris_b16const(vm, ~ A); } VAL idris_b16And(VM *vm, VAL a, VAL b) { uint16_t A = GETBITS16(a); uint16_t B = GETBITS16(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = A & B; - return cl; + return idris_b16const(vm, A & B); } VAL idris_b16Or(VM *vm, VAL a, VAL b) { uint16_t A = GETBITS16(a); uint16_t B = GETBITS16(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = A | B; - return cl; + return idris_b16const(vm, A | B); } VAL idris_b16Xor(VM *vm, VAL a, VAL b) { uint16_t A = GETBITS16(a); uint16_t B = GETBITS16(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = A ^ B; - return cl; + return idris_b16const(vm, A ^ B); } VAL idris_b16Shl(VM *vm, VAL a, VAL b) { uint16_t A = GETBITS16(a); uint16_t B = GETBITS16(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = A << B; - return cl; + return idris_b16const(vm, A << B); } VAL idris_b16LShr(VM *vm, VAL a, VAL b) { uint16_t A = GETBITS16(a); uint16_t B = GETBITS16(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = A >> B; - return cl; + return idris_b16const(vm, A >> B); } VAL idris_b16AShr(VM *vm, VAL a, VAL b) { - uint16_t A = GETBITS16(a); - uint16_t B = GETBITS16(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = (uint16_t) (((int16_t) A) >> ((int16_t) B)); - return cl; + int16_t A = GETBITS16(a); + int16_t B = GETBITS16(b); + return idris_b16const(vm, A >> B); } VAL idris_b32Plus(VM *vm, VAL a, VAL b) { uint32_t A = GETBITS32(a); uint32_t B = GETBITS32(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = A + B; - return cl; + return idris_b32const(vm, A + B); } VAL idris_b32Minus(VM *vm, VAL a, VAL b) { uint32_t A = GETBITS32(a); uint32_t B = GETBITS32(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = A - B; - return cl; + return idris_b32const(vm, A - B); } VAL idris_b32Times(VM *vm, VAL a, VAL b) { uint32_t A = GETBITS32(a); uint32_t B = GETBITS32(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = A * B; - return cl; + return idris_b32const(vm, A * B); } VAL idris_b32UDiv(VM *vm, VAL a, VAL b) { uint32_t A = GETBITS32(a); uint32_t B = GETBITS32(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = A / B; - return cl; + return idris_b32const(vm, A / B); } VAL idris_b32SDiv(VM *vm, VAL a, VAL b) { - uint32_t A = GETBITS32(a); - uint32_t B = GETBITS32(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = (uint32_t) (((int32_t) A) / ((int32_t) B)); - return cl; + int32_t A = GETBITS32(a); + int32_t B = GETBITS32(b); + return idris_b32const(vm, A / B); } VAL idris_b32URem(VM *vm, VAL a, VAL b) { uint32_t A = GETBITS32(a); uint32_t B = GETBITS32(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = A % B; - return cl; + return idris_b32const(vm, A % B); } VAL idris_b32SRem(VM *vm, VAL a, VAL b) { - uint32_t A = GETBITS32(a); - uint32_t B = GETBITS32(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = (uint32_t) (((int32_t) A) % ((int32_t) B)); - return cl; + int32_t A = GETBITS32(a); + int32_t B = GETBITS32(b); + return idris_b32const(vm, A % B); } VAL idris_b32Lt(VM *vm, VAL a, VAL b) { @@ -441,353 +320,246 @@ VAL idris_b32Gte(VM *vm, VAL a, VAL b) { VAL idris_b32Compl(VM *vm, VAL a) { uint32_t A = GETBITS32(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = ~ A; - return cl; + return idris_b32const(vm, ~ A); } VAL idris_b32And(VM *vm, VAL a, VAL b) { uint32_t A = GETBITS32(a); uint32_t B = GETBITS32(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = A & B; - return cl; + return idris_b32const(vm, A & B); } VAL idris_b32Or(VM *vm, VAL a, VAL b) { uint32_t A = GETBITS32(a); uint32_t B = GETBITS32(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = A | B; - return cl; + return idris_b32const(vm, A | B); } VAL idris_b32Xor(VM *vm, VAL a, VAL b) { uint32_t A = GETBITS32(a); uint32_t B = GETBITS32(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = A ^ B; - return cl; + return idris_b32const(vm, A ^ B); } VAL idris_b32Shl(VM *vm, VAL a, VAL b) { uint32_t A = GETBITS32(a); uint32_t B = GETBITS32(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = A << B; - return cl; + return idris_b32const(vm, A << B); } VAL idris_b32LShr(VM *vm, VAL a, VAL b) { uint32_t A = GETBITS32(a); uint32_t B = GETBITS32(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = A >> B; - return cl; + return idris_b32const(vm, A >> B); } VAL idris_b32AShr(VM *vm, VAL a, VAL b) { - uint32_t A = GETBITS32(a); - uint32_t B = GETBITS32(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = (uint32_t) (((int32_t)A) >> ((int32_t)B)); - return cl; + int32_t A = GETBITS32(a); + int32_t B = GETBITS32(b); + return idris_b32const(vm, A >> B); } VAL idris_b64Plus(VM *vm, VAL a, VAL b) { uint64_t A = GETBITS64(a); uint64_t B = GETBITS64(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = A + B; - return cl; + return idris_b64const(vm, A + B); } VAL idris_b64Minus(VM *vm, VAL a, VAL b) { uint64_t A = GETBITS64(a); uint64_t B = GETBITS64(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = A - B; - return cl; + return idris_b64const(vm, A - B); } VAL idris_b64Times(VM *vm, VAL a, VAL b) { uint64_t A = GETBITS64(a); uint64_t B = GETBITS64(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = A * B; - return cl; + return idris_b64const(vm, A * B); } VAL idris_b64UDiv(VM *vm, VAL a, VAL b) { uint64_t A = GETBITS64(a); uint64_t B = GETBITS64(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = A / B; - return cl; + return idris_b64const(vm, A / B); } VAL idris_b64SDiv(VM *vm, VAL a, VAL b) { - uint64_t A = GETBITS64(a); - uint64_t B = GETBITS64(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = (uint64_t) (((int64_t) A) / ((int64_t) B)); - return cl; + int64_t A = GETBITS64(a); + int64_t B = GETBITS64(b); + return idris_b64const(vm, A / B); } VAL idris_b64URem(VM *vm, VAL a, VAL b) { uint64_t A = GETBITS64(a); uint64_t B = GETBITS64(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = A % B; - return cl; + return idris_b64const(vm, A % B); } VAL idris_b64SRem(VM *vm, VAL a, VAL b) { - uint64_t A = GETBITS64(a); - uint64_t B = GETBITS64(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = (uint64_t) (((int64_t) A) % ((int64_t) B)); - return cl; + int64_t A = GETBITS64(a); + int64_t B = GETBITS64(b); + return idris_b64const(vm, A % B); } VAL idris_b64Lt(VM *vm, VAL a, VAL b) { - return MKINT((i_int) (a->info.bits64 < b->info.bits64)); + uint64_t A = GETBITS64(a); + uint64_t B = GETBITS64(b); + return MKINT((i_int) (A < B)); } VAL idris_b64Gt(VM *vm, VAL a, VAL b) { - return MKINT((i_int) (a->info.bits64 > b->info.bits64)); + uint64_t A = GETBITS64(a); + uint64_t B = GETBITS64(b); + return MKINT((i_int) (A > B)); } VAL idris_b64Eq(VM *vm, VAL a, VAL b) { - return MKINT((i_int) (a->info.bits64 == b->info.bits64)); + uint64_t A = GETBITS64(a); + uint64_t B = GETBITS64(b); + return MKINT((i_int) (A == B)); } VAL idris_b64Lte(VM *vm, VAL a, VAL b) { - return MKINT((i_int) (a->info.bits64 <= b->info.bits64)); + uint64_t A = GETBITS64(a); + uint64_t B = GETBITS64(b); + return MKINT((i_int) (A <= B)); } VAL idris_b64Gte(VM *vm, VAL a, VAL b) { - return MKINT((i_int) (a->info.bits64 >= b->info.bits64)); + uint64_t A = GETBITS64(a); + uint64_t B = GETBITS64(b); + return MKINT((i_int) (A >= B)); } VAL idris_b64Compl(VM *vm, VAL a) { uint64_t A = GETBITS64(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = ~ A; - return cl; + return idris_b64const(vm, ~ A); } VAL idris_b64And(VM *vm, VAL a, VAL b) { uint64_t A = GETBITS64(a); uint64_t B = GETBITS64(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = A & B; - return cl; + return idris_b64const(vm, A & B); } VAL idris_b64Or(VM *vm, VAL a, VAL b) { uint64_t A = GETBITS64(a); uint64_t B = GETBITS64(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = A | B; - return cl; + return idris_b64const(vm, A | B); } VAL idris_b64Xor(VM *vm, VAL a, VAL b) { uint64_t A = GETBITS64(a); uint64_t B = GETBITS64(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = A ^ B; - return cl; + return idris_b64const(vm, A ^ B); } VAL idris_b64Shl(VM *vm, VAL a, VAL b) { uint64_t A = GETBITS64(a); uint64_t B = GETBITS64(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = A << B; - return cl; + return idris_b64const(vm, A << B); } VAL idris_b64LShr(VM *vm, VAL a, VAL b) { uint64_t A = GETBITS64(a); uint64_t B = GETBITS64(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = A >> B; - return cl; + return idris_b64const(vm, A >> B); } VAL idris_b64AShr(VM *vm, VAL a, VAL b) { - uint64_t A = GETBITS64(a); - uint64_t B = GETBITS64(b); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = (uint64_t) (((int64_t) A) >> ((int64_t) B)); - return cl; + int64_t A = GETBITS64(a); + int64_t B = GETBITS64(b); + return idris_b64const(vm, A >> B); } VAL idris_b8Z16(VM *vm, VAL a) { uint8_t A = GETBITS8(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = (uint16_t) A; - return cl; + return idris_b16const(vm, A); } VAL idris_b8Z32(VM *vm, VAL a) { uint8_t A = GETBITS8(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = (uint32_t) A; - return cl; + return idris_b32const(vm, A); } VAL idris_b8Z64(VM *vm, VAL a) { uint8_t A = GETBITS8(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = (uint64_t) A; - return cl; + return idris_b64const(vm, A); } VAL idris_b8S16(VM *vm, VAL a) { - uint8_t A = GETBITS8(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = (uint16_t) (int16_t) (int8_t) A; - return cl; + int8_t A = GETBITS8(a); + return idris_b16const(vm, (int16_t) A); } VAL idris_b8S32(VM *vm, VAL a) { - uint8_t A = GETBITS8(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = (uint32_t) (int32_t) (int8_t) A; - return cl; + int8_t A = GETBITS8(a); + return idris_b32const(vm, (int32_t) A); } VAL idris_b8S64(VM *vm, VAL a) { - uint8_t A = GETBITS8(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = (uint64_t) (int64_t) (int8_t) A; - return cl; + int8_t A = GETBITS8(a); + return idris_b64const(vm, (int64_t) A); } VAL idris_b16Z32(VM *vm, VAL a) { uint16_t A = GETBITS16(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = (uint32_t) A; - return cl; + return idris_b32const(vm, (uint32_t) A); } VAL idris_b16Z64(VM *vm, VAL a) { uint16_t A = GETBITS16(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = (uint64_t) A; - return cl; + return idris_b64const(vm, (uint64_t) A); } VAL idris_b16S32(VM *vm, VAL a) { - uint16_t A = GETBITS16(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = (uint32_t) (int32_t) (int16_t) A; - return cl; + int16_t A = GETBITS16(a); + return idris_b32const(vm, (int32_t) A); } VAL idris_b16S64(VM *vm, VAL a) { - uint16_t A = GETBITS16(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = (uint64_t) (int64_t) (int16_t) A; - return cl; + int16_t A = GETBITS16(a); + return idris_b64const(vm, (int64_t) A); } VAL idris_b16T8(VM *vm, VAL a) { uint16_t A = GETBITS16(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = (uint8_t) A; - return cl; + return idris_b8const(vm, (uint8_t) A); } VAL idris_b32Z64(VM *vm, VAL a) { uint32_t A = GETBITS32(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = (uint64_t) A; - return cl; + return idris_b64const(vm, (uint64_t) A); } VAL idris_b32S64(VM *vm, VAL a) { - uint32_t A = GETBITS32(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS64); - cl->info.bits64 = (uint64_t) (int64_t) (int32_t) A; - return cl; + int32_t A = GETBITS32(a); + return idris_b64const(vm, (int64_t) A); } VAL idris_b32T8(VM *vm, VAL a) { uint32_t A = GETBITS32(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = (uint8_t) A; - return cl; + return idris_b8const(vm, (uint8_t) A); } VAL idris_b32T16(VM *vm, VAL a) { uint32_t A = GETBITS32(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = (uint16_t) A; - return cl; + return idris_b16const(vm, (uint16_t) A); } VAL idris_b64T8(VM *vm, VAL a) { uint64_t A = GETBITS64(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS8); - cl->info.bits8 = (uint8_t) A; - return cl; + return idris_b8const(vm, (uint8_t) A); } VAL idris_b64T16(VM *vm, VAL a) { uint64_t A = GETBITS64(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS16); - cl->info.bits16 = (uint16_t) A; - return cl; + return idris_b16const(vm, (uint16_t) A); } VAL idris_b64T32(VM *vm, VAL a) { uint64_t A = GETBITS64(a); - VAL cl = allocate(sizeof(Closure), 0); - SETTY(cl, CT_BITS32); - cl->info.bits32 = (uint32_t) A; - return cl; + return idris_b32const(vm, (uint32_t) A); } VAL idris_peekB8(VM* vm, VAL ptr, VAL offset) { diff --git a/rts/idris_buffer.c b/rts/idris_buffer.c index a37462e726..9541de1705 100644 --- a/rts/idris_buffer.c +++ b/rts/idris_buffer.c @@ -110,10 +110,9 @@ double idris_getBufferDouble(void* buffer, int loc) { VAL idris_getBufferString(void* buffer, int loc, int len) { Buffer* b = buffer; - char * s = (char*)(b->data + loc); size_t sz = loc >= 0 && loc+len <= b->size? len : 0; - return MKSTRlen(NULL, s, sz); + return MKSTRlen(get_vm(), s, sz); } int idris_readBuffer(FILE* h, void* buffer, int loc, int max) { diff --git a/rts/idris_gc.c b/rts/idris_gc.c index 0966cf7497..0e06393b96 100644 --- a/rts/idris_gc.c +++ b/rts/idris_gc.c @@ -5,8 +5,8 @@ #include VAL copy(VM* vm, VAL x) { - int ar, len; - Closure* cl = NULL; + int ar; + VAL cl; if (x==NULL || ISINT(x)) { return x; } @@ -18,7 +18,7 @@ VAL copy(VM* vm, VAL x) { c_heap_mark_item(GETCDATA(x)); break; case CT_BIGINT: - cl = MKBIGMc(vm, GETPTR(x)); + cl = MKBIGMc(vm, GETMPZ(x)); break; case CT_CON: ar = CARITY(x); @@ -39,47 +39,47 @@ VAL copy(VM* vm, VAL x) { case CT_BITS64: case CT_RAWDATA: { - size_t size = sizeof(Closure) + x->extrasz; - cl = allocate(size, 1); - memcpy(cl, x, size); + Hdr * s = (Hdr*)x; + cl = iallocate(vm, s->sz, 1); + memcpy(cl, x, s->sz); } break; default: + cl = NULL; break; } SETTY(x, CT_FWD); - x->info.ptr = cl; + ((Fwd*)x)->fwd = cl; return cl; } void cheney(VM *vm) { - size_t i, len; char* scan = aligned_heap_pointer(vm->heap.heap); while(scan < vm->heap.next) { VAL heap_item = (VAL)scan; // If it's a CT_CON, CT_REF or CT_STROFFSET, copy its arguments switch(GETTY(heap_item)) { - case CT_CON: - len = CARITY(heap_item); - for(i = 0; i < len; ++i) { - VAL newptr = copy(vm, heap_item->extra.cargs[i]); - heap_item->extra.cargs[i] = newptr; - } - break; - case CT_ARRAY: - len = CELEM(heap_item); - for(i = 0; i < len; ++i) { - VAL newptr = copy(vm, heap_item->extra.array[i]); - heap_item->extra.array[i] = newptr; - } - break; - case CT_REF: - heap_item->info.ptr = copy(vm, (VAL)(GETPTR(heap_item))); - break; - case CT_STROFFSET: - heap_item->extra.basestr[0] = copy(vm, heap_item->extra.basestr[0]); - break; + case CT_CON: { + Con * c = (Con*)heap_item; + size_t len = CARITY(c); + for(size_t i = 0; i < len; ++i) + c->args[i] = copy(vm, c->args[i]); + } break; + case CT_ARRAY: { + Array * a = (Array*)heap_item; + size_t len = CELEM(a); + for(size_t i = 0; i < len; ++i) + a->array[i] = copy(vm, a->array[i]); + } break; + case CT_REF: { + Ref * r = (Ref*)heap_item; + r->ref = copy(vm, r->ref); + } break; + case CT_STROFFSET: { + StrOffset * s = (StrOffset*)heap_item; + s->base = (String*)copy(vm, (VAL)s->base); + } break; default: // Nothing to copy break; } diff --git a/rts/idris_gmp.c b/rts/idris_gmp.c index 81ab79d382..61347433e4 100644 --- a/rts/idris_gmp.c +++ b/rts/idris_gmp.c @@ -22,17 +22,17 @@ VAL MKBIGI(int val) { return MKINT((i_int)val); } -static mpz_t * big(VAL v) { - return (mpz_t*)(v->info.ptr); +static mpz_t * big(BigInt * v) { + return (mpz_t*)(v->big); } -static VAL allocBig() { - idris_requireAlloc(IDRIS_MAXGMP); - VAL cl = allocate(sizeof(Closure) + sizeof(mpz_t), 0); - idris_doneAlloc(); +#define BIG(x) big((BigInt*)(x)) + +static BigInt * allocBig(VM * vm) { + idris_requireAlloc(vm, IDRIS_MAXGMP); + BigInt * cl = iallocate(vm, sizeof(*cl) + sizeof(mpz_t), 0); + idris_doneAlloc(vm); SETTY(cl, CT_BIGINT); - cl->info.ptr = (void*)(cl+1); - cl->extrasz = sizeof(mpz_t); mpz_init(*big(cl)); return cl; } @@ -42,117 +42,119 @@ VAL MKBIGC(VM* vm, char* val) { return MKBIGI(0); } else { - VAL cl = allocBig(); + BigInt * cl = allocBig(vm); mpz_set_str(*big(cl), val, 10); - return cl; + return (VAL)cl; } } VAL MKBIGM(VM* vm, void* ibig) { - VAL cl = allocBig(); + BigInt * cl = allocBig(vm); mpz_set(*big(cl), *((mpz_t*)ibig)); - return cl; + return (VAL)cl; } VAL MKBIGMc(VM* vm, void* ibig) { - VAL cl = allocBig(); + BigInt * cl = allocBig(vm); mpz_init_set(*big(cl), *((mpz_t*)ibig)); - return cl; + return (VAL)cl; } VAL MKBIGUI(VM* vm, unsigned long val) { - VAL cl = allocBig(); + BigInt * cl = allocBig(vm); mpz_init_set_ui(*big(cl), val); - return cl; + return (VAL)cl; } VAL MKBIGSI(VM* vm, signed long val) { - VAL cl = allocBig(); + BigInt * cl = allocBig(vm); mpz_init_set_si(*big(cl), val); - return cl; + return (VAL)cl; } -VAL GETBIG(VM * vm, VAL x) { +static BigInt * getbig(VM * vm, VAL x) { if (ISINT(x)) { - VAL cl = allocBig(); + BigInt * cl = allocBig(vm); mpz_set_si(*big(cl), GETINT(x)); return cl; } else { switch(GETTY(x)) { case CT_FWD: - return GETBIG(vm, x->info.ptr); + return getbig(vm, ((Fwd*)x)->fwd); default: - return x; + return (BigInt*)x; } } } +#define GETBIG (VAL)getbig + VAL bigAdd(VM* vm, VAL x, VAL y) { - VAL cl = allocBig(); - mpz_add(*big(cl), GETMPZ(GETBIG(vm,x)), GETMPZ(GETBIG(vm,y))); - return cl; + BigInt * cl = allocBig(vm); + mpz_add(*big(cl), *big(getbig(vm,x)), *big(getbig(vm,y))); + return (VAL)cl; } VAL bigSub(VM* vm, VAL x, VAL y) { - VAL cl = allocBig(); - mpz_sub(*big(cl), GETMPZ(GETBIG(vm,x)), GETMPZ(GETBIG(vm,y))); - return cl; + BigInt * cl = allocBig(vm); + mpz_sub(*big(cl), *big(getbig(vm,x)), *big(getbig(vm,y))); + return (VAL)cl; } VAL bigMul(VM* vm, VAL x, VAL y) { - VAL cl = allocBig(); - mpz_mul(*big(cl), GETMPZ(GETBIG(vm,x)), GETMPZ(GETBIG(vm,y))); - return cl; + BigInt * cl = allocBig(vm); + mpz_mul(*big(cl), *big(getbig(vm,x)), *big(getbig(vm,y))); + return (VAL)cl; } VAL bigDiv(VM* vm, VAL x, VAL y) { - VAL cl = allocBig(); - mpz_tdiv_q(*big(cl), GETMPZ(GETBIG(vm,x)), GETMPZ(GETBIG(vm,y))); - return cl; + BigInt * cl = allocBig(vm); + mpz_tdiv_q(*big(cl), *big(getbig(vm,x)), *big(getbig(vm,y))); + return (VAL)cl; } VAL bigMod(VM* vm, VAL x, VAL y) { - VAL cl = allocBig(); - mpz_tdiv_r(*big(cl), GETMPZ(GETBIG(vm,x)), GETMPZ(GETBIG(vm,y))); - return cl; + BigInt * cl = allocBig(vm); + mpz_tdiv_r(*big(cl), *big(getbig(vm,x)), *big(getbig(vm,y))); + return (VAL)cl; } VAL bigAnd(VM* vm, VAL x, VAL y) { - VAL cl = allocBig(); - mpz_and(*big(cl), GETMPZ(GETBIG(vm,x)), GETMPZ(GETBIG(vm,y))); - return cl; + BigInt * cl = allocBig(vm); + mpz_and(*big(cl), *big(getbig(vm,x)), *big(getbig(vm,y))); + return (VAL)cl; } VAL bigOr(VM* vm, VAL x, VAL y) { - VAL cl = allocBig(); - mpz_ior(*big(cl), GETMPZ(GETBIG(vm,x)), GETMPZ(GETBIG(vm,y))); - return cl; + BigInt * cl = allocBig(vm); + mpz_ior(*big(cl), *big(getbig(vm,x)), *big(getbig(vm,y))); + return (VAL)cl; } VAL bigShiftLeft(VM* vm, VAL x, VAL y) { - VAL cl = allocBig(); - mpz_mul_2exp(*big(cl), GETMPZ(GETBIG(vm,x)), GETINT(y)); - return cl; + BigInt * cl = allocBig(vm); + mpz_mul_2exp(*big(cl), *big(getbig(vm,x)), GETINT(y)); + return (VAL)cl; } VAL bigLShiftRight(VM* vm, VAL x, VAL y) { - VAL cl = allocBig(); - mpz_fdiv_q_2exp(*big(cl), GETMPZ(GETBIG(vm,x)), GETINT(y)); - return cl; + BigInt * cl = allocBig(vm); + mpz_fdiv_q_2exp(*big(cl), *big(getbig(vm,x)), GETINT(y)); + return (VAL)cl; } VAL bigAShiftRight(VM* vm, VAL x, VAL y) { - VAL cl = allocBig(); - mpz_fdiv_q_2exp(*big(cl), GETMPZ(GETBIG(vm,x)), GETINT(y)); - return cl; + BigInt * cl = allocBig(vm); + mpz_fdiv_q_2exp(*big(cl), *big(getbig(vm,x)), GETINT(y)); + return (VAL)cl; } VAL idris_bigAnd(VM* vm, VAL x, VAL y) { if (ISINT(x) && ISINT(y)) { return INTOP(&, x, y); } else { - return bigAnd(vm, GETBIG(vm, x), GETBIG(vm, y)); + return bigAnd(vm, (VAL)GETBIG(vm, x), (VAL)GETBIG(vm, y)); } } @@ -160,7 +162,7 @@ VAL idris_bigOr(VM* vm, VAL x, VAL y) { if (ISINT(x) && ISINT(y)) { return INTOP(|, x, y); } else { - return bigOr(vm, GETBIG(vm, x), GETBIG(vm, y)); + return bigOr(vm, (VAL)GETBIG(vm, x), (VAL)GETBIG(vm, y)); } } @@ -173,12 +175,12 @@ VAL idris_bigPlus(VM* vm, VAL x, VAL y) { } i_int res = vx + vy; if (res >= 1<<30 || res <= -(1 << 30)) { - return bigAdd(vm, GETBIG(vm, x), GETBIG(vm, y)); + return bigAdd(vm, (VAL)GETBIG(vm, x), (VAL)GETBIG(vm, y)); } else { return MKINT(res); } } else { - return bigAdd(vm, GETBIG(vm, x), GETBIG(vm, y)); + return bigAdd(vm, (VAL)GETBIG(vm, x), (VAL)GETBIG(vm, y)); } } @@ -264,29 +266,29 @@ VAL idris_bigMod(VM* vm, VAL x, VAL y) { int bigEqConst(VAL x, int c) { if (ISINT(x)) { return (GETINT(x) == c); } else { - int rv = mpz_cmp_si(GETMPZ(x), c); + int rv = mpz_cmp_si(*BIG(x), c); return (rv == 0); } } VAL bigEq(VM* vm, VAL x, VAL y) { - return MKINT((i_int)(mpz_cmp(GETMPZ(x), GETMPZ(y)) == 0)); + return MKINT((i_int)(mpz_cmp(*BIG(x), *BIG(y)) == 0)); } VAL bigLt(VM* vm, VAL x, VAL y) { - return MKINT((i_int)(mpz_cmp(GETMPZ(x), GETMPZ(y)) < 0)); + return MKINT((i_int)(mpz_cmp(*BIG(x), *BIG(y)) < 0)); } VAL bigGt(VM* vm, VAL x, VAL y) { - return MKINT((i_int)(mpz_cmp(GETMPZ(x), GETMPZ(y)) > 0)); + return MKINT((i_int)(mpz_cmp(*BIG(x), *BIG(y)) > 0)); } VAL bigLe(VM* vm, VAL x, VAL y) { - return MKINT((i_int)(mpz_cmp(GETMPZ(x), GETMPZ(y)) <= 0)); + return MKINT((i_int)(mpz_cmp(*BIG(x), *BIG(y)) <= 0)); } VAL bigGe(VM* vm, VAL x, VAL y) { - return MKINT((i_int)(mpz_cmp(GETMPZ(x), GETMPZ(y)) >= 0)); + return MKINT((i_int)(mpz_cmp(*BIG(x), *BIG(y)) >= 0)); } VAL idris_bigEq(VM* vm, VAL x, VAL y) { @@ -338,7 +340,7 @@ VAL idris_castBigInt(VM* vm, VAL i) { if (ISINT(i)) { return i; } else { - return MKINT((i_int)(mpz_get_ui(GETMPZ(i)))); + return MKINT((i_int)(mpz_get_ui(*BIG(i)))); } } @@ -346,15 +348,15 @@ VAL idris_castBigFloat(VM* vm, VAL i) { if (ISINT(i)) { return MKFLOAT(vm, GETINT(i)); } else { - return MKFLOAT(vm, mpz_get_d(GETMPZ(i))); + return MKFLOAT(vm, mpz_get_d(*BIG(i))); } } VAL idris_castFloatBig(VM* vm, VAL f) { double val = GETFLOAT(f); - VAL cl = allocBig(); + BigInt * cl = allocBig(vm); mpz_init_set_d(*big(cl), val); - return cl; + return (VAL)cl; } VAL idris_castStrBig(VM* vm, VAL i) { @@ -362,7 +364,7 @@ VAL idris_castStrBig(VM* vm, VAL i) { } VAL idris_castBigStr(VM* vm, VAL i) { - char* str = mpz_get_str(NULL, 10, GETMPZ(GETBIG(vm, i))); + char* str = mpz_get_str(NULL, 10, *big(getbig(vm, i))); return MKSTR(vm, str); } diff --git a/rts/idris_gmp.h b/rts/idris_gmp.h index 9048fe6d5e..ec82e325f2 100644 --- a/rts/idris_gmp.h +++ b/rts/idris_gmp.h @@ -48,6 +48,6 @@ VAL idris_bigLShiftRight(VM* vm, VAL x, VAL y); uint64_t idris_truncBigB64(const mpz_t bi); -#define GETMPZ(x) *((mpz_t*)((x)->info.ptr)) +#define GETMPZ(x) *((mpz_t*)(((BigInt*)(x))->big)) #endif diff --git a/rts/idris_heap.c b/rts/idris_heap.c index 02967ad6ef..4dd5220c11 100644 --- a/rts/idris_heap.c +++ b/rts/idris_heap.c @@ -192,10 +192,11 @@ void heap_check_pointers(Heap * heap) { switch(GETTY(heap_item)) { case CT_CON: { - int ar = CARITY(heap_item); - int i = 0; + Con * c = (Con*)heap_item; + size_t ar = CARITY(c); + size_t i; for(i = 0; i < ar; ++i) { - VAL ptr = heap_item->extra.cargs[i]; + VAL ptr = c->args[i]; if (is_valid_ref(ptr)) { // Check for closure. diff --git a/rts/idris_rts.c b/rts/idris_rts.c index 25580f5c34..f1efbb8be5 100644 --- a/rts/idris_rts.c +++ b/rts/idris_rts.c @@ -7,10 +7,10 @@ #include "idris_bitstring.h" #include "getline.h" -#define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(COND)?1:-1] - +#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__DragonFly__) +#include +#endif -STATIC_ASSERT(sizeof(Closure) == 16, sizeofClosure); #ifdef HAS_PTHREAD static pthread_key_t vm_key; @@ -18,7 +18,7 @@ static pthread_key_t vm_key; static VM* global_vm; #endif -void free_key(VM *vm) { +void free_key(void *vvm) { // nothing to free, we just used the VM pointer which is freed elsewhere } @@ -88,7 +88,6 @@ VM* idris_vm(void) { VM* get_vm(void) { #ifdef HAS_PTHREAD - init_threadkeys(); return pthread_getspecific(vm_key); #else return global_vm; @@ -101,7 +100,7 @@ void close_vm(VM* vm) { #ifdef HAS_PTHREAD void create_key(void) { - pthread_key_create(&vm_key, (void*)free_key); + pthread_key_create(&vm_key, free_key); } #endif @@ -160,13 +159,7 @@ CData cdata_manage(void * data, size_t size, CDataFinalizer finalizer) return c_heap_create_item(data, size, finalizer); } -void idris_requireAlloc(size_t size) { -#ifdef HAS_PTHREAD - VM* vm = pthread_getspecific(vm_key); -#else - VM* vm = global_vm; -#endif - +void idris_requireAlloc(VM * vm, size_t size) { if (!(vm->heap.next + size < vm->heap.end)) { idris_gc(vm); } @@ -178,9 +171,8 @@ void idris_requireAlloc(size_t size) { #endif } -void idris_doneAlloc(void) { +void idris_doneAlloc(VM * vm) { #ifdef HAS_PTHREAD - VM* vm = pthread_getspecific(vm_key); int lock = vm->processes > 0; if (lock) { // We only need to lock if we're in concurrent mode pthread_mutex_unlock(&vm->alloc_lock); @@ -193,10 +185,9 @@ int space(VM* vm, size_t size) { } void* idris_alloc(size_t size) { - Closure* cl = (Closure*) allocate(sizeof(Closure)+size, 0); + RawData * cl = (RawData*) allocate(sizeof(*cl)+size, 0); SETTY(cl, CT_RAWDATA); - cl->extrasz = size; - return (void*)(cl+1); + return (void*)cl->raw; } void* idris_realloc(void* old, size_t old_size, size_t size) { @@ -208,19 +199,20 @@ void* idris_realloc(void* old, size_t old_size, size_t size) { void idris_free(void* ptr, size_t size) { } -void* allocate(size_t isize, int outerlock) { +void * allocate(size_t sz, int lock) { + return iallocate(get_vm(), sz, lock); +} + +void* iallocate(VM * vm, size_t isize, int outerlock) { // return malloc(size); size_t size = aligned(isize); #ifdef HAS_PTHREAD - VM* vm = pthread_getspecific(vm_key); int lock = vm->processes > 0 && !outerlock; if (lock) { // not message passing pthread_mutex_lock(&vm->alloc_lock); } -#else - VM* vm = global_vm; #endif if (vm->heap.next + size < vm->heap.end) { @@ -236,6 +228,7 @@ void* allocate(size_t isize, int outerlock) { pthread_mutex_unlock(&vm->alloc_lock); } #endif + ((Hdr*)ptr)->sz = size; return (void*)ptr; } else { // If we're trying to allocate something bigger than the heap, @@ -249,104 +242,97 @@ void* allocate(size_t isize, int outerlock) { pthread_mutex_unlock(&vm->alloc_lock); } #endif - return allocate(size, 0); + return iallocate(vm, size, outerlock); } } -static Closure * allocStr(size_t len, int outer) { - Closure * cl = allocate(sizeof(Closure) + len + 1, outer); +static String * allocStr(VM * vm, size_t len, int outer) { + String * cl = iallocate(vm, sizeof(*cl) + len + 1, outer); SETTY(cl, CT_STRING); - cl->extrasz = len + 1; - cl->info.slen = len; + cl->slen = len; return cl; } -/* Now a macro -void* allocCon(VM* vm, int arity, int outer) { - Closure* cl = allocate(vm, sizeof(Closure) + sizeof(VAL)*arity, - outer); - SETTY(cl, CT_CON); - - cl -> info.c.arity = arity; -// cl -> info.c.tag = 42424242; -// printf("%p\n", cl); - return (void*)cl; -} -*/ - VAL MKFLOAT(VM* vm, double val) { - Closure* cl = allocate(sizeof(Closure), 0); + Float * cl = iallocate(vm, sizeof(*cl), 0); SETTY(cl, CT_FLOAT); - cl -> info.f = val; - return cl; + cl->f = val; + return (VAL)cl; } VAL MKSTRlen(VM* vm, const char * str, size_t len) { - Closure* cl = allocStr(len, 0); - memcpy(cl->extra.str, str, len); + String * cl = allocStr(vm, len, 0); + memcpy(cl->str, str, len); if (str == NULL) - cl->info.slen = ~0; - return cl; + cl->slen = ~0; + return (VAL)cl; } VAL MKSTR(VM* vm, const char* str) { return MKSTRlen(vm, str, str? strlen(str) : 0); } +static char * getstroff(StrOffset * stroff) { + return stroff->base->str + stroff->offset; +} + char* GETSTROFF(VAL stroff) { // Assume STROFF - return stroff->extra.basestr[0]->extra.str + stroff->info.soffset; + return getstroff((StrOffset*)stroff); +} + +static size_t getstrofflen(StrOffset * stroff) { + return stroff->base->slen - stroff->offset; } size_t GETSTROFFLEN(VAL stroff) { // Assume STROFF // we're working in char* here so no worries about utf8 char length - return stroff->extra.basestr[0]->info.slen - stroff->info.soffset; + return getstrofflen((StrOffset*)stroff); } VAL MKCDATA(VM* vm, CHeapItem * item) { c_heap_insert_if_needed(vm, &vm->c_heap, item); - Closure* cl = allocate(sizeof(Closure), 0); + CDataC * cl = iallocate(vm, sizeof(*cl), 0); SETTY(cl, CT_CDATA); - cl->info.c_heap_item = item; - return cl; + cl->item = item; + return (VAL)cl; } VAL MKCDATAc(VM* vm, CHeapItem * item) { c_heap_insert_if_needed(vm, &vm->c_heap, item); - Closure* cl = allocate(sizeof(Closure), 1); + CDataC * cl = iallocate(vm, sizeof(*cl), 1); SETTY(cl, CT_CDATA); - cl->info.c_heap_item = item; - return cl; + cl->item = item; + return (VAL)cl; } VAL MKPTR(VM* vm, void* ptr) { - Closure* cl = allocate(sizeof(Closure), 0); + Ptr * cl = iallocate(vm, sizeof(*cl), 0); SETTY(cl, CT_PTR); - cl -> info.ptr = ptr; - return cl; + cl->ptr = ptr; + return (VAL)cl; } VAL MKMPTR(VM* vm, void* ptr, size_t size) { - Closure* cl = allocate(sizeof(Closure) + size, 0); + ManagedPtr * cl = iallocate(vm, sizeof(*cl) + size, 0); SETTY(cl, CT_MANAGEDPTR); - memcpy(cl->extra.mptr, ptr, size); - cl->extrasz = size; - return cl; + memcpy(cl->mptr, ptr, size); + return (VAL)cl; } VAL MKFLOATc(VM* vm, double val) { - Closure* cl = allocate(sizeof(Closure), 1); + Float * cl = iallocate(vm, sizeof(*cl), 1); SETTY(cl, CT_FLOAT); - cl -> info.f = val; - return cl; + cl->f = val; + return (VAL)cl; } VAL MKSTRclen(VM* vm, char* str, size_t len) { - Closure* cl = allocStr(len, 1); - memcpy(cl->extra.str, str, len); - return cl; + String * cl = allocStr(vm, len, 1); + memcpy(cl->str, str, len); + return (VAL)cl; } VAL MKSTRc(VM* vm, char* str) { @@ -354,46 +340,45 @@ VAL MKSTRc(VM* vm, char* str) { } VAL MKPTRc(VM* vm, void* ptr) { - Closure* cl = allocate(sizeof(Closure), 1); + Ptr * cl = iallocate(vm, sizeof(*cl), 1); SETTY(cl, CT_PTR); - cl -> info.ptr = ptr; - return cl; + cl->ptr = ptr; + return (VAL)cl; } VAL MKMPTRc(VM* vm, void* ptr, size_t size) { - Closure* cl = allocate(sizeof(Closure) + size, 1); + ManagedPtr * cl = iallocate(vm, sizeof(*cl) + size, 1); SETTY(cl, CT_MANAGEDPTR); - memcpy(cl->extra.mptr, ptr, size); - cl->extrasz = size; - return cl; + memcpy(cl->mptr, ptr, size); + return (VAL)cl; } VAL MKB8(VM* vm, uint8_t bits8) { - Closure* cl = allocate(sizeof(Closure), 1); + Bits8 * cl = iallocate(vm, sizeof(*cl), 1); SETTY(cl, CT_BITS8); - cl -> info.bits8 = bits8; - return cl; + cl->bits8 = bits8; + return (VAL)cl; } VAL MKB16(VM* vm, uint16_t bits16) { - Closure* cl = allocate(sizeof(Closure), 1); + Bits16 * cl = iallocate(vm, sizeof(*cl), 1); SETTY(cl, CT_BITS16); - cl -> info.bits16 = bits16; - return cl; + cl->bits16 = bits16; + return (VAL)cl; } VAL MKB32(VM* vm, uint32_t bits32) { - Closure* cl = allocate(sizeof(Closure), 1); + Bits32 * cl = iallocate(vm, sizeof(*cl), 1); SETTY(cl, CT_BITS32); - cl -> info.bits32 = bits32; - return cl; + cl->bits32 = bits32; + return (VAL)cl; } VAL MKB64(VM* vm, uint64_t bits64) { - Closure* cl = allocate(sizeof(Closure), 1); + Bits64 * cl = iallocate(vm, sizeof(*cl), 1); SETTY(cl, CT_BITS64); - cl -> info.bits64 = bits64; - return cl; + cl->bits64 = bits64; + return (VAL)cl; } void idris_trace(VM* vm, const char* func, int line) { @@ -427,25 +412,29 @@ void dumpVal(VAL v) { return; } switch(GETTY(v)) { - case CT_CON: - printf("%d[", (int)TAG(v)); - for(i = 0; i < CARITY(v); ++i) { - dumpVal(v->extra.cargs[i]); - } - printf("] "); - break; - case CT_STRING: - printf("STR[%s]", v->extra.str); - break; - case CT_STROFFSET: + case CT_CON: { + Con * cl = (Con*)v; + printf("%d[", (int)TAG(cl)); + for(i = 0; i < CARITY(cl); ++i) { + dumpVal(cl->args[i]); + } + printf("] "); + } break; + case CT_STRING: { + String * cl = (String*)v; + printf("STR[%s]", cl->str); + } break; + case CT_STROFFSET: { + StrOffset * cl = (StrOffset*)v; printf("OFFSET["); - dumpVal((VAL)(v->extra.basestr[0])); + dumpVal((VAL)cl->base); printf("]"); - break; - case CT_FWD: + } break; + case CT_FWD: { + Fwd * cl = (Fwd*)v; printf("CT_FWD "); - dumpVal((VAL)(v->info.ptr)); - break; + dumpVal((VAL)cl->fwd); + } break; default: printf("val"); } @@ -466,7 +455,7 @@ void idris_poke(void* ptr, i_int offset, uint8_t data) { VAL idris_peekPtr(VM* vm, VAL ptr, VAL offset) { - void** addr = (void **)((char *)GETPTR(ptr) + GETINT(offset)); + void** addr = (void **)(((char *)GETPTR(ptr)) + GETINT(offset)); return MKPTR(vm, *addr); } @@ -500,41 +489,41 @@ void idris_memmove(void* dest, void* src, i_int dest_offset, i_int src_offset, i VAL idris_castIntStr(VM* vm, VAL i) { int x = (int) GETINT(i); - Closure* cl = allocStr(16, 0); - cl->info.slen = sprintf(cl->extra.str, "%d", x); - return cl; + String * cl = allocStr(vm, 16, 0); + cl->slen = sprintf(cl->str, "%d", x); + return (VAL)cl; } VAL idris_castBitsStr(VM* vm, VAL i) { - Closure* cl; + String * cl; ClosureType ty = GETTY(i); switch (ty) { case CT_BITS8: // max length 8 bit unsigned int str 3 chars (256) - cl = allocStr(4, 0); - cl->info.slen = sprintf(cl->extra.str, "%" PRIu8, GETBITS8(i)); + cl = allocStr(vm, 4, 0); + cl->slen = sprintf(cl->str, "%" PRIu8, GETBITS8(i)); break; case CT_BITS16: // max length 16 bit unsigned int str 5 chars (65,535) - cl = allocStr(6, 0); - cl->info.slen = sprintf(cl->extra.str, "%" PRIu16, GETBITS16(i)); + cl = allocStr(vm, 6, 0); + cl->slen = sprintf(cl->str, "%" PRIu16, GETBITS16(i)); break; case CT_BITS32: // max length 32 bit unsigned int str 10 chars (4,294,967,295) - cl = allocStr(11, 0); - cl->info.slen = sprintf(cl->extra.str, "%" PRIu32, GETBITS32(i)); + cl = allocStr(vm, 11, 0); + cl->slen = sprintf(cl->str, "%" PRIu32, GETBITS32(i)); break; case CT_BITS64: // max length 64 bit unsigned int str 20 chars (18,446,744,073,709,551,615) - cl = allocStr(21, 0); - cl->info.slen = sprintf(cl->extra.str, "%" PRIu64, GETBITS64(i)); + cl = allocStr(vm, 21, 0); + cl->slen = sprintf(cl->str, "%" PRIu64, GETBITS64(i)); break; default: fprintf(stderr, "Fatal Error: ClosureType %d, not an integer type", ty); exit(EXIT_FAILURE); } - return cl; + return (VAL)cl; } VAL idris_castStrInt(VM* vm, VAL i) { @@ -547,9 +536,9 @@ VAL idris_castStrInt(VM* vm, VAL i) { } VAL idris_castFloatStr(VM* vm, VAL i) { - Closure* cl = allocStr(32, 0); - cl->info.slen = snprintf(cl->extra.str, 32, "%.16g", GETFLOAT(i)); - return cl; + String * cl = allocStr(vm, 32, 0); + cl->slen = snprintf(cl->str, 32, "%.16g", GETFLOAT(i)); + return (VAL)cl; } VAL idris_castStrFloat(VM* vm, VAL i) { @@ -562,10 +551,10 @@ VAL idris_concat(VM* vm, VAL l, VAL r) { size_t llen = GETSTRLEN(l); size_t rlen = GETSTRLEN(r); - Closure* cl = allocStr(llen + rlen, 0); - memcpy(cl->extra.str, ls, llen); - memcpy(cl->extra.str + llen, rs, rlen); - return cl; + String * cl = allocStr(vm, llen + rlen, 0); + memcpy(cl->str, ls, llen); + memcpy(cl->str + llen, rs, rlen); + return (VAL)cl; } VAL idris_strlt(VM* vm, VAL l, VAL r) { @@ -627,32 +616,31 @@ VAL idris_strHead(VM* vm, VAL str) { } VAL MKSTROFFc(VM* vm, VAL basestr) { - Closure* cl = allocate(sizeof(Closure) + sizeof(VAL), 1); + StrOffset * cl = iallocate(vm, sizeof(*cl), 1); SETTY(cl, CT_STROFFSET); - cl->extrasz = sizeof(VAL); - cl->extra.basestr[0] = basestr; - return cl; + cl->base = (String*)basestr; + return (VAL)cl; } VAL idris_strShift(VM* vm, VAL str, int num) { + size_t sz = sizeof(StrOffset); // If there's no room, just copy the string, or we'll have a problem after // gc moves str - if (space(vm, sizeof(Closure))) { + if (space(vm, sz)) { int offset = 0; - VAL root = str; - Closure* cl = allocate(sizeof(Closure) + sizeof(VAL), 0); + StrOffset * root = (StrOffset*)str; + StrOffset * cl = iallocate(vm, sz, 0); SETTY(cl, CT_STROFFSET); - cl->extrasz = sizeof(VAL); while(root!=NULL && !ISSTR(root)) { // find the root, carry on. // In theory, at most one step here! - offset += root->info.soffset; - root = root->extra.basestr[0]; + offset += root->offset; + root = (StrOffset*)root->base; } - cl->extra.basestr[0] = root; - cl->info.soffset = offset+idris_utf8_findOffset(GETSTR(str), num); - return cl; + cl->base = (String*)root; + cl->offset = offset+idris_utf8_findOffset(GETSTR(str), num); + return (VAL)cl; } else { char* nstr = GETSTR(str); return MKSTR(vm, nstr+idris_utf8_charlen(nstr)); @@ -667,22 +655,22 @@ VAL idris_strCons(VM* vm, VAL x, VAL xs) { char *xstr = GETSTR(xs); int xval = GETINT(x); size_t xlen = GETSTRLEN(xs); + String * cl; if (xval < 0x80) { // ASCII char - Closure* cl = allocStr(xlen + 1, 0); - cl->extra.str[0] = (char)(GETINT(x)); - memcpy(cl->extra.str+1, xstr, xlen); - return cl; + cl = allocStr(vm, xlen + 1, 0); + cl->str[0] = (char)(GETINT(x)); + memcpy(cl->str+1, xstr, xlen); } else { char *init = idris_utf8_fromChar(xval); size_t ilen = strlen(init); int newlen = ilen + xlen; - Closure* cl = allocStr(newlen, 0); - memcpy(cl->extra.str, init, ilen); - memcpy(cl->extra.str + ilen, xstr, xlen); + cl = allocStr(vm, newlen, 0); + memcpy(cl->str, init, ilen); + memcpy(cl->str + ilen, xstr, xlen); free(init); - return cl; } + return (VAL)cl; } VAL idris_strIndex(VM* vm, VAL str, VAL i) { @@ -703,10 +691,10 @@ VAL idris_substr(VM* vm, VAL offset, VAL length, VAL str) { char *start = idris_utf8_advance(str_val, offset_val); char *end = idris_utf8_advance(start, length_val); size_t sz = end - start; - Closure* newstr = allocStr(sz, 0); - memcpy(newstr->extra.str, start, sz); - newstr->extra.str[sz] = '\0'; - return newstr; + String * newstr = allocStr(vm, sz, 0); + memcpy(newstr->str, start, sz); + newstr->str[sz] = '\0'; + return (VAL)newstr; } } @@ -714,16 +702,16 @@ VAL idris_strRev(VM* vm, VAL str) { char *xstr = GETSTR(str); size_t xlen = GETSTRLEN(str); - Closure* cl = allocStr(xlen, 0); - idris_utf8_rev(xstr, cl->extra.str); - return cl; + String * cl = allocStr(vm, xlen, 0); + idris_utf8_rev(xstr, cl->str); + return (VAL)cl; } VAL idris_newRefLock(VAL x, int outerlock) { - Closure* cl = allocate(sizeof(Closure), outerlock); + Ref * cl = allocate(sizeof(*cl), outerlock); SETTY(cl, CT_REF); - cl->info.ptr = (void*)x; - return cl; + cl->ref = x; + return (VAL)cl; } VAL idris_newRef(VAL x) { @@ -731,30 +719,34 @@ VAL idris_newRef(VAL x) { } void idris_writeRef(VAL ref, VAL x) { - ref->info.ptr = (void*)x; + Ref * r = (Ref*)ref; + r->ref = x; SETTY(ref, CT_REF); } VAL idris_readRef(VAL ref) { - return (VAL)(ref->info.ptr); + Ref * r = (Ref*)ref; + return r->ref; } VAL idris_newArray(VM* vm, int size, VAL def) { - Closure* cl; + Array * cl; int i; - allocArray(cl, vm, size, 0); + cl = allocArrayF(vm, size, 0); for(i=0; iextra.array[i] = def; + cl->array[i] = def; } - return cl; + return (VAL)cl; } void idris_arraySet(VAL arr, int index, VAL newval) { - arr->extra.array[index] = newval; + Array * cl = (Array*)arr; + cl->array[index] = newval; } VAL idris_arrayGet(VAL arr, int index) { - return arr->extra.array[index]; + Array * cl = (Array*)arr; + return cl->array[index]; } VAL idris_systemInfo(VM* vm, VAL index) { @@ -847,8 +839,8 @@ static void copyArray(VM* vm, VAL * dst, VAL * src, size_t len) { // VM is assumed to be a different vm from the one x lives on static VAL doCopyTo(VM* vm, VAL x) { - int ar, len; - Closure* cl; + int ar; + VAL cl; if (x==NULL || ISINT(x)) { return x; } @@ -857,22 +849,24 @@ static VAL doCopyTo(VM* vm, VAL x) { cl = MKCDATAc(vm, GETCDATA(x)); break; case CT_BIGINT: - cl = MKBIGMc(vm, GETPTR(x)); + cl = MKBIGMc(vm, GETMPZ(x)); break; case CT_CON: ar = CARITY(x); if (ar == 0 && CTAG(x) < 256) { // globally allocated cl = x; } else { - allocCon(cl, vm, CTAG(x), ar, 1); - copyArray(vm, cl->extra.cargs, x->extra.cargs, ar); + Con * c = allocConF(vm, CTAG(x), ar, 1); + copyArray(vm, c->args, ((Con*)x)->args, ar); + cl = (VAL)c; } break; - case CT_ARRAY: - len = CELEM(x); - allocArray(cl, vm, len, 1); - copyArray(vm, cl->extra.array, x->extra.array, len); - break; + case CT_ARRAY: { + size_t len = CELEM(x); + Array * a = allocArrayF(vm, len, 1); + copyArray(vm, a->array, ((Array*)x)->array, len); + cl = (VAL)a; + } break; case CT_STRING: case CT_FLOAT: case CT_PTR: @@ -883,9 +877,8 @@ static VAL doCopyTo(VM* vm, VAL x) { case CT_BITS64: case CT_RAWDATA: { - size_t size = sizeof(Closure) + x->extrasz; - cl = allocate(size, 0); - memcpy(cl, x, size); + cl = iallocate(vm, x->hdr.sz, 0); + memcpy(cl, x, x->hdr.sz); } break; default: @@ -895,10 +888,7 @@ static VAL doCopyTo(VM* vm, VAL x) { } VAL copyTo(VM* vm, VAL x) { - VM* current = pthread_getspecific(vm_key); - pthread_setspecific(vm_key, vm); VAL ret = doCopyTo(vm, x); - pthread_setspecific(vm_key, current); return ret; } @@ -1116,13 +1106,12 @@ VAL* nullary_cons; void init_nullaries(void) { int i; - VAL cl; nullary_cons = malloc(256 * sizeof(VAL)); for(i = 0; i < 256; ++i) { - cl = calloc(1, sizeof(Closure)); + Con * cl = calloc(1, sizeof(*cl)); SETTY(cl, CT_CON); - cl->info.tag = i; - nullary_cons[i] = cl; + cl->tag = i; + nullary_cons[i] = (VAL)cl; } } diff --git a/rts/idris_rts.h b/rts/idris_rts.h index 82b5cd4a0e..66c71f342b 100644 --- a/rts/idris_rts.h +++ b/rts/idris_rts.h @@ -3,19 +3,17 @@ #include #include +#include +#ifdef HAS_PTHREAD #include #include -#ifdef HAS_PTHREAD #include #endif -#include -#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__DragonFly__) -#include -#endif #include "idris_heap.h" #include "idris_stats.h" + #ifndef EXIT_SUCCESS #define EXIT_SUCCESS 0 #endif @@ -26,36 +24,111 @@ // Closures typedef enum { CT_CON, CT_ARRAY, CT_INT, CT_BIGINT, CT_FLOAT, CT_STRING, CT_STROFFSET, - CT_BITS8, CT_BITS16, CT_BITS32, CT_BITS64, CT_UNIT, CT_PTR, CT_REF, + CT_BITS8, CT_BITS16, CT_BITS32, CT_BITS64, CT_PTR, CT_REF, CT_FWD, CT_MANAGEDPTR, CT_RAWDATA, CT_CDATA } ClosureType; -typedef struct Closure *VAL; -typedef struct Closure { +typedef struct Hdr { uint64_t ty:8; - uint64_t extrasz:56; - union { - uint32_t tag; - size_t slen; - size_t soffset; - int i; - double f; - void * ptr; - uint8_t bits8; - uint16_t bits16; - uint32_t bits32; - uint64_t bits64; - CHeapItem* c_heap_item; - } info; - union { - VAL array[0]; - VAL cargs[0]; - VAL basestr[0]; - char str[0]; - char mptr[0]; // A foreign pointer, managed by the idris GC - } extra; -} Closure; + uint64_t sz:56; +} Hdr; + + +typedef struct Val { + Hdr hdr; +} Val; + +typedef struct Val * VAL; + +typedef struct Con { + Hdr hdr; + uint32_t tag; + uint32_t arity; + VAL args[0]; +} Con; + +typedef struct Array { + Hdr hdr; + VAL array[0]; +} Array; + +typedef struct Int { + Hdr hdr; + int i; +} Int; + +typedef struct BigInt { + Hdr hdr; + char big[0]; +} BigInt; + +typedef struct Float { + Hdr hdr; + double f; +} Float; + +typedef struct String { + Hdr hdr; + size_t slen; + char str[0]; +} String; + +typedef struct StrOffset { + Hdr hdr; + String * base; + size_t offset; +} StrOffset; + +typedef struct Bits8 { + Hdr hdr; + uint8_t bits8; +} Bits8; + +typedef struct Bits16 { + Hdr hdr; + uint16_t bits16; +} Bits16; + +typedef struct Bits32 { + Hdr hdr; + uint32_t bits32; +} Bits32; + +typedef struct Bits64 { + Hdr hdr; + uint64_t bits64; +} Bits64; + +typedef struct Ptr { + Hdr hdr; + void * ptr; +} Ptr; + +typedef struct Ref { + Hdr hdr; + VAL ref; +} Ref; + +typedef struct Fwd { + Hdr hdr; + VAL fwd; +} Fwd; + +typedef struct ManagedPtr { + Hdr hdr; + char mptr[0]; +} ManagedPtr; + +typedef struct RawData { + Hdr hdr; + char raw[0]; +} RawData; + +typedef struct CDataC { + Hdr hdr; + CHeapItem * item; +} CDataC; struct VM; @@ -164,37 +237,37 @@ typedef void(*func)(VM*, VAL*); #define REG1 (vm->reg1) // Retrieving values -static inline char * getstr(VAL x) { - return x->info.slen == ~0? NULL : x->extra.str; +static inline char * getstr(String * x) { + return x->slen == ~0? NULL : x->str; } -static inline size_t getstrlen(VAL x) { - return x->info.slen == ~0? 0 : x->info.slen; +static inline size_t getstrlen(String * x) { + return x->slen == ~0? 0 : x->slen; } -#define GETSTR(x) (ISSTR(x) ? getstr((VAL)(x)) : GETSTROFF(x)) -#define GETSTRLEN(x) (ISSTR(x) ? getstrlen((VAL)(x)) : GETSTROFFLEN(x)) -#define GETPTR(x) (((VAL)(x))->info.ptr) -#define GETMPTR(x) (((VAL)(x))->extra.mptr) -#define GETFLOAT(x) (((VAL)(x))->info.f) -#define GETCDATA(x) (((VAL)(x))->info.c_heap_item) +#define GETSTR(x) (ISSTR(x) ? getstr((String*)(x)) : GETSTROFF(x)) +#define GETSTRLEN(x) (ISSTR(x) ? getstrlen((String*)(x)) : GETSTROFFLEN(x)) +#define GETPTR(x) (((Ptr*)(x))->ptr) +#define GETMPTR(x) (((ManagedPtr*)(x))->mptr) +#define GETFLOAT(x) (((Float*)(x))->f) +#define GETCDATA(x) (((CDataC*)(x))->item) -#define GETBITS8(x) (((VAL)(x))->info.bits8) -#define GETBITS16(x) (((VAL)(x))->info.bits16) -#define GETBITS32(x) (((VAL)(x))->info.bits32) -#define GETBITS64(x) (((VAL)(x))->info.bits64) +#define GETBITS8(x) (((Bits8*)(x))->bits8) +#define GETBITS16(x) (((Bits16*)(x))->bits16) +#define GETBITS32(x) (((Bits32*)(x))->bits32) +#define GETBITS64(x) (((Bits64*)(x))->bits64) // Already checked it's a CT_CON -#define CTAG(x) ((x)->info.tag) -#define CARITY(x) (((x)->extrasz) / sizeof(VAL)) +#define CTAG(x) (((Con*)(x))->tag) +#define CARITY(x) (((Con*)(x))->arity) -#define TAG(x) (ISINT(x) || x == NULL ? (-1) : ( GETTY(x) == CT_CON ? CTAG(x) : (-1)) ) -#define ARITY(x) (ISINT(x) || x == NULL ? (-1) : ( GETTY(x) == CT_CON ? CARITY(x) : (-1)) ) +#define TAG(x) (ISINT(x) || x == NULL ? (-1) : ( GETTY(x) == CT_CON ? CTAG((Con*)x) : (-1)) ) +#define ARITY(x) (ISINT(x) || x == NULL ? (-1) : ( GETTY(x) == CT_CON ? CARITY((Con*)x) : (-1)) ) -#define CELEM(x) (((x)->extrasz) / sizeof(VAL)) +#define CELEM(x) (((x)->hdr.sz - sizeof(Array)) / sizeof(VAL)) -#define GETTY(x) ((x)->ty) -#define SETTY(x,t) ((x)->ty = t) +#define GETTY(x) ((ClosureType)((x)->hdr.ty)) +#define SETTY(x,t) ((x)->hdr.ty = t) // Integers, floats and operators @@ -261,14 +334,16 @@ VAL MKCDATAc(VM* vm, CHeapItem * item); char* GETSTROFF(VAL stroff); size_t GETSTROFFLEN(VAL stroff); -#define SETARG(x, i, a) ((x)->extra.cargs)[i] = ((VAL)(a)) -#define GETARG(x, i) ((x)->extra.cargs[i]) +#define SETARG(x, i, a) (((Con*)(x))->args)[i] = ((VAL)(a)) +#define GETARG(x, i) (((Con*)(x))->args[i]) #define PROJECT(vm,r,loc,num) \ - memcpy(&(LOC(loc)), (r)->extra.cargs, sizeof(VAL)*num) + memcpy(&(LOC(loc)), ((Con*)(r))->args, sizeof(VAL)*num) #define SLIDE(vm, args) \ memcpy(&(LOC(0)), &(TOP(0)), sizeof(VAL)*args) +void* iallocate(VM *, size_t, int); + void* allocate(size_t size, int outerlock); // void* allocCon(VM* vm, int arity, int outerlock); @@ -278,8 +353,8 @@ void* allocate(size_t size, int outerlock); // idris_doneAlloc *must* be called when allocation from C is done (as it // may take a lock if other threads are running). -void idris_requireAlloc(size_t size); -void idris_doneAlloc(void); +void idris_requireAlloc(VM *, size_t size); +void idris_doneAlloc(VM *); // public interface to allocation (note that this may move other pointers // if allocating beyond the limits given by idris_requireAlloc!) @@ -288,30 +363,35 @@ void* idris_alloc(size_t size); void* idris_realloc(void* old, size_t old_size, size_t size); void idris_free(void* ptr, size_t size); -#define allocCon(cl, vm, t, a, o) do { \ - size_t sz = sizeof(VAL)*a; \ - cl = allocate(sizeof(Closure) + sz, o); \ - SETTY(cl, CT_CON); \ - cl->info.tag = t; \ - cl->extrasz = sz; \ - } while (0) +static inline void updateConF(Con * cl, unsigned tag, unsigned arity) { + SETTY(cl, CT_CON); + cl->tag = tag; + cl->arity = arity; + cl->hdr.sz = sizeof(*cl) + sizeof(VAL) * arity; +} -#define updateCon(cl, old, t, a) do { \ - cl = old; \ - SETTY(cl, CT_CON); \ - cl->info.tag = t; \ - cl->extrasz = sizeof(VAL)*a; \ - } while (0) +static inline Con * allocConF(VM * vm, unsigned tag, unsigned arity, int outer) { + size_t sz = sizeof(VAL) * arity; + Con * cl = iallocate(vm, sizeof(*cl) + sz, outer); + updateConF(cl, tag, arity); + return cl; +} +static inline Array * allocArrayF(VM * vm, size_t len, int outer) { + size_t sz = sizeof(VAL) * len; + Array * cl = iallocate(vm, sizeof(*cl) + sz, outer); + SETTY(cl, CT_ARRAY); + return cl; +} + + +#define allocCon(cl, vm, t, a, o) (cl) = (VAL)allocConF(vm, t, a, o) + +#define updateCon(cl, old, tag, arity) (cl) = (old); updateConF(cl, tag, arity) #define NULL_CON(x) nullary_cons[x] -#define allocArray(cl, vm, len, o) do { \ - size_t sz = sizeof(VAL)*len; \ - cl = allocate(sizeof(Closure) + sz, o); \ - SETTY(cl, CT_ARRAY); \ - cl->extrasz = sz; \ - } while (0) +#define allocArray(cl, vm, len, o) (cl) = (VAL)allocArrayF(vm, len, o) int idris_errno(void); char* idris_showerror(int err); @@ -449,11 +529,13 @@ void stackOverflow(void); #include "idris_gmp.h" static inline size_t valSize(VAL v) { - return sizeof(Closure) + v->extrasz; + return v->hdr.sz; } static inline size_t aligned(size_t sz) { return (sz + 7) & ~7; } +VM* get_vm(void); + #endif diff --git a/rts/idris_stdfgn.c b/rts/idris_stdfgn.c index 0fe9a1c106..421cc52ad5 100644 --- a/rts/idris_stdfgn.c +++ b/rts/idris_stdfgn.c @@ -6,7 +6,6 @@ #include #include #include -#include #include #include @@ -79,7 +78,7 @@ void* idris_dirOpen(char* dname) { void idris_dirClose(void* h) { DirInfo* di = (DirInfo*)h; - + closedir(di->dirptr); free(di); } @@ -219,4 +218,3 @@ VAL idris_getString(VM* vm, void* buffer) { free(sb); return str; } -