shithub: sl

Download patch

ref: 3065329a32e17d2ba7089746b2ed5286e132aa8d
parent: ca3cb2a832876265ceb857216a4faedc59eec9b8
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Mon Apr 7 12:38:50 EDT 2025

add "ptr" integer type

--- a/src/cvalues.c
+++ b/src/cvalues.c
@@ -233,6 +233,7 @@
 num_init(u32int, u32, T_U32)
 num_init(s64int, s64, T_S64)
 num_init(u64int, u64, T_U64)
+num_init(uintptr, ptr, T_PTR)
 num_init(float, double, T_FLOAT)
 num_init(double, double, T_DOUBLE)
 
@@ -325,9 +326,10 @@
 num_ctor_unboxed(u32, u32int, T_U32)
 num_ctor_unboxed(s64, s64int, T_S64)
 num_ctor_unboxed(u64, u64int, T_U64)
-num_ctor_init(utf8, u8int, T_U8)
+num_ctor_unboxed(ptr, uintptr, T_PTR)
 num_ctor(float, float, T_FLOAT)
 num_ctor(double, double, T_DOUBLE)
+num_ctor_init(utf8, u8int, T_U8)
 
 static void
 cvalue_mp_init(sl_type *type, sl_v a, void *dest)
@@ -833,6 +835,7 @@
 		case T_U32: return mk_u32(((u32int*)data)[index]);
 		case T_S64: return mk_s64(((s64int*)data)[index]);
 		case T_U64: return mk_u64(((u64int*)data)[index]);
+		case T_PTR: return mk_ptr(((uintptr*)data)[index]);
 		default: break;
 		}
 	}
@@ -991,6 +994,9 @@
 			}
 			i64 = -(s64int)ui64;
 			goto i64neg;
+		case T_PTR:
+			// a pointer is not exactly a number
+			break;
 		case T_MP:
 			mp = mpcopy(*(mpint**)a);
 			mp->sign = -mp->sign;
@@ -1085,6 +1091,11 @@
 		type_error("num", a);
 	if(!num_to_ptr(b, &bi, &tb, &bptr))
 		type_error("num", b);
+	// pointers are not really numbers
+	if(ta == T_PTR)
+		type_error("num", a);
+	if(tb == T_PTR)
+		type_error("num", b);
 
 	da = conv_to_double(a, aptr, ta);
 	db = conv_to_double(b, bptr, tb);
@@ -1112,6 +1123,11 @@
 		type_error("num", a);
 	if(!num_to_ptr(b, &bi, &tb, &bptr))
 		type_error("num", b);
+	// pointers are not really numbers
+	if(ta == T_PTR)
+		type_error("num", a);
+	if(tb == T_PTR)
+		type_error("num", b);
 
 	if(ta == T_MP){
 		if(tb == T_MP){
@@ -1201,6 +1217,7 @@
 		case T_U32: return mk_u32(*(u32int*)aptr & (u32int)b64);
 		case T_S64: return mk_s64(*(s64int*)aptr & (s64int)b64);
 		case T_U64: return mk_u64(*(u64int*)aptr & (u64int)b64);
+		case T_PTR: return mk_ptr(*(uintptr*)aptr & (uintptr)b64);
 		case T_MP:  mpand(*(mpint**)aptr, bmp, resmp); return mk_mp(resmp);
 		case T_FLOAT:
 		case T_DOUBLE: break;
@@ -1216,6 +1233,7 @@
 		case T_U32: return mk_u32(*(u32int*)aptr | (u32int)b64);
 		case T_S64: return mk_s64(*(s64int*)aptr | (s64int)b64);
 		case T_U64: return mk_u64(*(u64int*)aptr | (u64int)b64);
+		case T_PTR: return mk_ptr(*(uintptr*)aptr & (uintptr)b64);
 		case T_MP:  mpor(*(mpint**)aptr, bmp, resmp); return mk_mp(resmp);
 		case T_FLOAT:
 		case T_DOUBLE: break;
@@ -1231,6 +1249,7 @@
 		case T_U32: return mk_u32(*(u32int*)aptr ^ (u32int)b64);
 		case T_S64: return mk_s64(*(s64int*)aptr ^ (s64int)b64);
 		case T_U64: return mk_u64(*(u64int*)aptr ^ (u64int)b64);
+		case T_PTR: return mk_ptr(*(uintptr*)aptr & (uintptr)b64);
 		case T_MP:  mpxor(*(mpint**)aptr, bmp, resmp); return mk_mp(resmp);
 		case T_FLOAT:
 		case T_DOUBLE: break;
@@ -1306,6 +1325,7 @@
 		case T_U32: return mk_u32(~*(u32int*)aptr);
 		case T_S64: return mk_s64(~*(s64int*)aptr);
 		case T_U64: return mk_u64(~*(u64int*)aptr);
+		case T_PTR: return mk_ptr(~*(uintptr*)aptr);
 		case T_MP:; mpint *m = mpnew(0); mpnot(*(mpint**)aptr, m); return mk_mp(m);
 		default: abort();
 		}
@@ -1357,10 +1377,13 @@
 			case T_U32: return mk_u32((*(u32int*)aptr) >> n);
 			case T_S64: return mk_s64((*(s64int*)aptr) >> n);
 			case T_U64: return mk_u64((*(u64int*)aptr) >> n);
+			case T_PTR: return mk_ptr((*(uintptr*)aptr) >> n);
 			default: abort();
 			}
 		}else if(ta == T_U64)
 			return return_from_u64((*(u64int*)aptr)<<n);
+		else if(ta == T_PTR)
+			return return_from_u64((*(uintptr*)aptr)<<n);
 		else if(ta < T_FLOAT)
 			return return_from_s64(conv_to_s64(a, aptr, ta)<<n);
 	}
@@ -1399,9 +1422,10 @@
 	ctor_cv_intern(u32, T_U32, u32int);
 	ctor_cv_intern(s64, T_S64, s64int);
 	ctor_cv_intern(u64, T_U64, u64int);
-	ctor_cv_intern(utf8, T_U8, u8int);
+	ctor_cv_intern(ptr, T_PTR, uintptr);
 	ctor_cv_intern(float, T_FLOAT, float);
 	ctor_cv_intern(double, T_DOUBLE, double);
+	ctor_cv_intern(utf8, T_U8, u8int);
 
 	ctor_cv_intern(arr, NONNUMERIC, int);
 
@@ -1413,9 +1437,10 @@
 	mk_primtype(u32, u32int);
 	mk_primtype(s64, s64int);
 	mk_primtype(u64, u64int);
-	mk_primtype(utf8, u8int);
+	mk_primtype(ptr, uintptr);
 	mk_primtype(float, float);
 	mk_primtype(double, double);
+	mk_primtype(utf8, u8int);
 
 	ctor_cv_intern(bignum, T_MP, mpint*);
 	sl_mptype = get_type(sl_bignumsym);
@@ -1430,6 +1455,7 @@
 	unboxedtypes[T_U32] = sl_u32type;
 	unboxedtypes[T_S64] = sl_s64type;
 	unboxedtypes[T_U64] = sl_u64type;
+	unboxedtypes[T_PTR] = sl_ptrtype;
 	unboxedtypesyms[T_S8] = sl_s8sym;
 	unboxedtypesyms[T_U8] = sl_u8sym;
 	unboxedtypesyms[T_S16] = sl_s16sym;
@@ -1438,6 +1464,7 @@
 	unboxedtypesyms[T_U32] = sl_u32sym;
 	unboxedtypesyms[T_S64] = sl_s64sym;
 	unboxedtypesyms[T_U64] = sl_u64sym;
+	unboxedtypesyms[T_PTR] = sl_ptrsym;
 
 	sl_strtype = get_type(mk_list2(sl_arrsym, sl_utf8sym));
 	sl_emptystr = cvalue_from_ref(sl_strtype, (char*)"", 0);
--- a/src/cvalues.h
+++ b/src/cvalues.h
@@ -44,8 +44,6 @@
 sl_v sl_idiv2(sl_v a, sl_v b);
 void cvalues_init(void);
 
-sl_v mk_double(double n);
-sl_v mk_float(float n);
 sl_v mk_s8(s8int n);
 sl_v mk_u8(u8int n);
 sl_v mk_s16(s16int n);
@@ -54,6 +52,9 @@
 sl_v mk_u32(u32int n);
 sl_v mk_s64(s64int n);
 sl_v mk_u64(u64int n);
+sl_v mk_ptr(uintptr n);
 sl_v mk_mp(mpint *n);
+sl_v mk_float(float n);
+sl_v mk_double(double n);
 
 usize llength(sl_v v) sl_purefn;
--- a/src/operators.c
+++ b/src/operators.c
@@ -13,6 +13,7 @@
 	case T_U32: return uitomp(*(u32int*)data, nil);
 	case T_S64: return vtomp(*(s64int*)data, nil);
 	case T_U64: return uvtomp(*(u64int*)data, nil);
+	case T_PTR: return uvtomp(*(uintptr*)data, nil);
 	case T_MP:  return mpcopy(*(mpint**)data);
 	case T_FLOAT:  return dtomp(*(float*)data, nil);
 	case T_DOUBLE: return dtomp(*(double*)data, nil);
@@ -38,6 +39,7 @@
 			d = -d;
 		return d;
 	case T_U64: return *(u64int*)data;
+	case T_PTR: return *(uintptr*)data;
 	case T_MP:  return mptod(*(mpint**)data);
 	case T_FLOAT:  return *(float*)data;
 	case T_DOUBLE: return *(double*)data;
@@ -60,6 +62,7 @@
 	case T_U32: return (ctype)*(u32int*)data; \
 	case T_S64: return (ctype)*(s64int*)data; \
 	case T_U64: return (ctype)*(u64int*)data; \
+	case T_PTR: return (ctype)*(uintptr*)data; \
 	case T_MP:  return (ctype)mptov(*(mpint**)data); \
 	case T_FLOAT:  return (ctype)*(float*)data; \
 	case T_DOUBLE: return (ctype)*(double*)data; \
@@ -88,6 +91,7 @@
 	case T_U32: return *(u32int*)data; break;
 	case T_S64: return *(s64int*)data; break;
 	case T_U64: return *(u64int*)data; break;
+	case T_PTR: return *(uintptr*)data; break;
 	case T_MP:  return mptouv(*(mpint**)data); break;
 	case T_FLOAT:
 		if(*(float*)data >= 0)
@@ -116,6 +120,7 @@
 	case T_U32: return *(u32int*)a < *(u32int*)b;
 	case T_S64: return *(s64int*)a < *(s64int*)b;
 	case T_U64: return *(u64int*)a < *(u64int*)b;
+	case T_PTR: return *(uintptr*)a < *(uintptr*)b;
 	case T_MP:  return mpcmp(*(mpint**)a, *(mpint**)b) < 0;
 	case T_FLOAT:  return *(float*)a < *(float*)b;
 	case T_DOUBLE: return *(double*)a < *(double*)b;
@@ -136,6 +141,7 @@
 	case T_U32: return *(u32int*)a == *(u32int*)b;
 	case T_S64: return *(s64int*)a == *(s64int*)b;
 	case T_U64: return *(u64int*)a == *(u64int*)b;
+	case T_PTR: return *(uintptr*)a == *(uintptr*)b;
 	case T_MP:  return mpcmp(*(mpint**)a, *(mpint**)b) == 0;
 	case T_FLOAT:  return *(float*)a == *(float*)b && !isnan(*(float*)a);
 	case T_DOUBLE: return *(double*)a == *(double*)b && !isnan(*(double*)b);
--- a/src/operators.h
+++ b/src/operators.h
@@ -1,11 +1,17 @@
 #pragma once
 
-mpint *conv_to_mp(sl_v v, void *data, sl_numtype tag);
-double conv_to_double(sl_v v, void *data, sl_numtype tag);
-s64int conv_to_s64(sl_v v, void *data, sl_numtype tag);
-u64int conv_to_u64(sl_v v, void *data, sl_numtype tag);
 s32int conv_to_s32(sl_v v, void *data, sl_numtype tag);
 u32int conv_to_u32(sl_v v, void *data, sl_numtype tag);
+s64int conv_to_s64(sl_v v, void *data, sl_numtype tag);
+u64int conv_to_u64(sl_v v, void *data, sl_numtype tag);
+mpint *conv_to_mp(sl_v v, void *data, sl_numtype tag);
+double conv_to_double(sl_v v, void *data, sl_numtype tag);
+
+#if defined(BITS64)
+#define conv_to_ptr conv_to_u64
+#else
+#define conv_to_ptr conv_to_u32
+#endif
 
 bool cmp_same_lt(void *a, void *b, sl_numtype tag);
 bool cmp_same_eq(void *a, void *b, sl_numtype tag);
--- a/src/plan9/platform.h
+++ b/src/plan9/platform.h
@@ -39,10 +39,12 @@
 #define BITS64
 #define PRIdPTR PRId64
 #define PRIuPTR PRIu64
+#define PRIxPTR PRIx64
 typedef long long ssize;
 #else
 #define PRIdPTR "ld"
 #define PRIuPTR "lud"
+#define PRIxPTR "llux"
 typedef long ssize;
 #endif
 
--- a/src/print.c
+++ b/src/print.c
@@ -766,6 +766,14 @@
 		if(n < 1)
 			goto err;
 		sl.hpos += n;
+	}else if(type == sl_ptrsym){
+		uintptr p = *(uintptr*)data;
+		n = (weak || sl.print_princ)
+			? ios_printf(f, "0x%"PRIxPTR, p)
+			: ios_printf(f, "#%s(0x%"PRIxPTR")", sym_name(type), p);
+		if(n < 1)
+			goto err;
+		sl.hpos += n;
 	}else if(type == sl_bignumsym){
 		mpint *i = *(mpint**)data;
 		char *s = mptoa(i, 10, nil, 0);
--- a/src/sl.c
+++ b/src/sl.c
@@ -27,7 +27,7 @@
 sl_v sl_tablesym, sl_arrsym;
 sl_v sl_iosym, sl_rdsym, sl_wrsym, sl_apsym, sl_crsym, sl_truncsym;
 sl_v sl_s8sym, sl_u8sym, sl_s16sym, sl_u16sym, sl_s32sym, sl_u32sym;
-sl_v sl_s64sym, sl_u64sym, sl_bignumsym;
+sl_v sl_s64sym, sl_u64sym, sl_ptrsym, sl_bignumsym;
 sl_v sl_utf8sym, sl_runesym, sl_floatsym, sl_doublesym;
 
 sl_type *sl_mptype, *sl_builtintype;
@@ -34,7 +34,7 @@
 sl_type *sl_s8type, *sl_u8type;
 sl_type *sl_s16type, *sl_u16type;
 sl_type *sl_s32type, *sl_u32type;
-sl_type *sl_s64type, *sl_u64type;
+sl_type *sl_s64type, *sl_u64type, *sl_ptrtype;
 sl_type *sl_floattype, *sl_doubletype;
 sl_type *sl_utf8type, *sl_runetype;
 sl_type *sl_strtype;
--- a/src/sl.h
+++ b/src/sl.h
@@ -37,6 +37,7 @@
 	T_S16, T_U16,
 	T_S32, T_U32,
 	T_S64, T_U64,
+	T_PTR,
 	T_UNBOXED_NUM,
 	T_MP = T_UNBOXED_NUM,
 	T_FLOAT, T_DOUBLE,
@@ -441,7 +442,7 @@
 extern sl_v sl_arrsym;
 extern sl_v sl_iosym, sl_rdsym, sl_wrsym, sl_apsym, sl_crsym, sl_truncsym;
 extern sl_v sl_s8sym, sl_u8sym, sl_s16sym, sl_u16sym, sl_s32sym, sl_u32sym;
-extern sl_v sl_s64sym, sl_u64sym, sl_bignumsym;
+extern sl_v sl_s64sym, sl_u64sym, sl_ptrsym, sl_bignumsym;
 extern sl_v sl_utf8sym, sl_runesym, sl_floatsym, sl_doublesym;
 
 extern sl_type *sl_mptype, *sl_builtintype;
@@ -448,7 +449,7 @@
 extern sl_type *sl_s8type, *sl_u8type;
 extern sl_type *sl_s16type, *sl_u16type;
 extern sl_type *sl_s32type, *sl_u32type;
-extern sl_type *sl_s64type, *sl_u64type;
+extern sl_type *sl_s64type, *sl_u64type, *sl_ptrtype;
 extern sl_type *sl_floattype, *sl_doubletype;
 extern sl_type *sl_utf8type, *sl_runetype;
 extern sl_type *sl_strtype, *sl_runestrtype;