shithub: mc

Download patch

ref: 41a66f79ac8284bbffb9820deb8655b6bdda1c9b
parent: ac965251351332f405c9e5390e00d436fa467ff4
author: Ori Bernstein <ori@eigenstate.org>
date: Sun Jun 7 05:36:04 EDT 2015

Add vabytes() function.

    This gets an arbitrary blob with the type data.

--- a/libstd/fmt2.myr
+++ b/libstd/fmt2.myr
@@ -272,11 +272,14 @@
 		sbputs(sb, "array")
 	/* aggregate types */
 	| `Tytuple typecursor:
+		vabytes(ap)
 		sbputs(sb, "tuple")
 	| `Tystruct namecursor:
+		vabytes(ap)
 		sbputs(sb, "struct")
 	| `Tyunion namecursor:
-		sbputs(sb, "struct")
+		vabytes(ap)
+		sbputs(sb, "union")
 	| `Tyname (name, desc):
 		fallbackfmt(sb, params, desc, ap)
 	;;
--- a/libstd/varargs.myr
+++ b/libstd/varargs.myr
@@ -8,7 +8,7 @@
 
 	const vastart	: (args : ...# -> valist)
 	const vatype	: (ap : valist# -> byte[:])
-	const vaskip	: (ap : valist# -> byte[:])
+	const vabytes	: (ap : valist# -> byte[:])
 	generic vanext	: (ap : valist# -> @a)
 ;;
 
@@ -55,24 +55,38 @@
 	-> tcpeek(&ap.tc)
 }
 
+const vabytes = {ap
+	var sl
+	var ti, align, sz
+	var p
+
+	ti = typeinfo(tcpeek(&ap.tc))
+
+	/* apply the alignment to the arg pointer */
+	align = ti.align castto(intptr)
+	p = ap.args castto(intptr)
+	p = (p + align - 1) & ~(align - 1)
+	ap.args = p castto(byte#)
+
+	sl = ap.args[:ti.size]
+	tcnext(&ap.tc)
+
+	sz = ti.size castto(intptr)
+	ap.args = ((p castto(intptr)) + sz) castto(byte#)
+
+	-> sl
+}
+
 generic vanext = {ap -> @a
 	var v : @a
+	var ti
 	var align
 	var p
 
-	/*
-	 Assumptions about the ABI:
-	 * all types smaller than a word are
-	 * aligned to their own size. Larger
-	 * types are aligned to word size.
-	 */
-	if sizeof(@a) > 8
-		align = 8
-	else
-		align = sizeof(@a)
-	;;
+	ti = typeinfo(tcpeek(&ap.tc))
 
 	/* apply the alignment to the arg pointer */
+	align = ti.align castto(intptr)
 	p = ap.args castto(intptr)
 	p = (p + align - 1) & ~(align - 1)
 	ap.args = p castto(byte#)