shithub: mc

Download patch

ref: ce931f082769f332e545cc477bc85c2bbca71ac2
parent: 47faa09b2507bdfb2e3dd983f9aeb1ab54b2f2ef
parent: 05e04ed1df3c315aa69b2b5c16f16e83922d5aed
author: S. Gilles <sgilles@math.umd.edu>
date: Sun May 13 14:07:35 EDT 2018

Merge remote-tracking branch 'ori/master' into libmath

--- a/6/gengas.c
+++ b/6/gengas.c
@@ -350,7 +350,8 @@
 	char cwd[1024];
 
 	resetregs();
-	getcwd(cwd, sizeof cwd);
+	if (!getcwd(cwd, sizeof cwd))
+		die("getcwd failed: %s\n", cwd);
 	is.reglocs = mkht(varhash, vareq);
 	is.name = fn->name;
 	is.stkoff = fn->stkoff;
--- /dev/null
+++ b/default.nix
@@ -1,0 +1,25 @@
+{ pkgs ? (import <nixpkgs> {})
+, stdenv ? pkgs.stdenv
+, bison ? pkgs.bison
+, binutils ? pkgs.binutils
+, fetchurl ? pkgs.fetchurl
+}:
+
+stdenv.mkDerivation rec {
+  name = "myrddin";
+
+  src = ./.;
+
+  buildInputs = [ bison binutils ];
+
+  preBuild = ''
+    make bootstrap
+  '';
+
+  postPatch = ''
+    substituteInPlace "mbld/opts.myr" --replace '"ld"' '"${binutils}/bin/ld"'
+    substituteInPlace "configure"     --replace '"ld"' '"${binutils}/bin/ld"'
+    substituteInPlace "mbld/opts.myr" --replace '"as"' '"${binutils}/bin/as"'
+    substituteInPlace "configure"     --replace '"as"' '"${binutils}/bin/as"'
+  '';
+}
--- a/lib/crypto/aesgcm.myr
+++ b/lib/crypto/aesgcm.myr
@@ -81,7 +81,23 @@
 }
 
 const aesgcmdecrypt = {c, buf, aad, tag
-	-> false
+	var tmp : byte[16]
+	var L, Y
+
+	ghash(c, aad, Y)
+	ghash(c, buf, Y)
+	L[0] = buf.len << 3
+	L[1] = buf.len >> 29
+	L[2] = aad.len << 3
+	L[3] = aad.len >> 29
+	ghash1(c, L, Y)
+	store128(Y, tmp[:])
+	aesctr(c, tmp[:])
+	if bufeq(tag, tmp[:])
+		-> false
+	;;
+	aesctr(c, buf)
+	-> true
 }
 
 const ghash = {c, buf, Y
--- a/lib/crypto/ct.myr
+++ b/lib/crypto/ct.myr
@@ -12,6 +12,7 @@
 	generic mux	: (x : @t, a : @t, b : @t ->@t)	:: integral,numeric @t
 	generic min	: (a : @t, b : @t -> @t)	:: integral,numeric @t
 	generic max	: (a : @t, b : @t -> @t)	:: integral,numeric @t
+	const bufeq	: (a : byte[:], b : byte[:] -> bool)
 ;;
 
 generic not = {a : @t :: integral,numeric @t
@@ -72,4 +73,15 @@
 
 	x = lt(a, b)
 	-> mux(x, b, a)
+}
+
+const bufeq = {a, b
+	var r, n
+
+	r = 1
+	n = min(a.len, b.len)
+	for var i = 0; i < n; i++
+		r = mux(r, eq(a[i], b[i]), r)
+	;;
+	-> (r : bool)
 }
--- a/lib/math/bld.sub
+++ b/lib/math/bld.sub
@@ -34,6 +34,9 @@
 
 	# util
 	util.myr
+	ftrap.myr
+	ftrap-impl+plan9-x64.s
+	ftrap-impl+posixy-x64.s
 
 	lib ../std:std
 ;;
--- /dev/null
+++ b/lib/math/ftrap-impl+plan9-x64.s
@@ -1,0 +1,14 @@
+TEXT	math$fptrap(SB),$0
+	SUBQ	$4,SP
+	WAIT
+	STMXCSR	(SP)
+	MOVL	(SP),AX
+	ANDL	$~0x1f80,AX
+	TESTB	DI,DI
+	JNZ	.apply
+	ORL	$0x1f80,AX
+.apply:
+	MOVL	AX,(SP)
+	LDMXCSR	(SP)
+	ADDQ	$4,SP
+	RET
--- /dev/null
+++ b/lib/math/ftrap-impl+posixy-x64.s
@@ -1,0 +1,17 @@
+.globl _math$fptrap
+.globl math$fptrap
+_math$fptrap:
+math$fptrap:
+	subq	$4,%rsp
+	wait
+	stmxcsr	(%rsp)
+	movl	(%rsp),%eax
+	andl	$~0x1f80,%eax
+	testb	%dil,%dil
+	jnz	.apply
+	orl	$0x1f80,%eax
+.apply:
+	movl	%eax,(%rsp)
+	ldmxcsr	(%rsp)
+	addq	$4,%rsp
+	ret
--- /dev/null
+++ b/lib/math/ftrap.myr
@@ -1,0 +1,3 @@
+pkg math =
+	extern const fptrap	: (f : bool -> void)
+;;
--- a/lib/math/test/exp-impl.myr
+++ b/lib/math/test/exp-impl.myr
@@ -7,6 +7,7 @@
    are tested extensively in expm101 and expm102.
  */
 const main = {
+	math.fptrap(false)
 	testr.run([
 		[.name="exp-01", .fn = exp01],
 		[.name="exp-02", .fn = exp02],
--- a/lib/math/test/fma-impl.myr
+++ b/lib/math/test/fma-impl.myr
@@ -3,6 +3,7 @@
 use testr
 
 const main = {
+	math.fptrap(false)
 	testr.run([
 		[.name="fma-01", .fn = fma01],
 		[.name="fma-02", .fn = fma02],
--- a/lib/math/test/sqrt-impl.myr
+++ b/lib/math/test/sqrt-impl.myr
@@ -3,6 +3,7 @@
 use testr
 
 const main = {
+	math.fptrap(false)
 	testr.run([
 		[.name="sqrt-01", .fn = sqrt01],
 		[.name="sqrt-02", .fn = sqrt02],
--- a/lib/math/test/trunc-impl.myr
+++ b/lib/math/test/trunc-impl.myr
@@ -3,6 +3,7 @@
 use testr
 
 const main = {
+	math.fptrap(false)
 	testr.run([
 		[.name = "trunc-01",    .fn = trunc01],
 		[.name = "trunc-02",    .fn = trunc02],
--- a/lib/std/blat.myr
+++ b/lib/std/blat.myr
@@ -8,8 +8,12 @@
 
 const blat = {path, buf, perm
 	match openmode(path, Ocreat|Owrite, perm)
-	| `Ok fd:	-> fblat(fd, buf)
-	| `Err e:	-> false
+	| `Ok fd:
+		var r = fblat(fd, buf)
+		close(fd)
+		-> r
+	| `Err e:
+		-> false
 	;;
 }
 
--- a/lib/std/bld.sub
+++ b/lib/std/bld.sub
@@ -1,4 +1,7 @@
-testdeps = ../testr:testr ;;
+testdeps = 
+	../testr:testr
+	../math:math
+;;
 
 lib std =
 	lib ../sys:sys
--- a/lib/std/putint.myr
+++ b/lib/std/putint.myr
@@ -39,7 +39,6 @@
 const putbe = {buf, val, n
 	var k
 	
-	iassert(buf.len >= n, "buffer too small")
 	for var i = 0; i < n; i++
 		k = val >> (8*(n-i-1))
 		buf[i] = (k & 0xff: byte)
@@ -48,7 +47,6 @@
 }
 
 const putle = {buf, val, n
-	iassert(buf.len >= n, "buffer too small")
 	for var i = 0; i < n; i++
 		buf[i] = (val & 0xff : byte)
 		val >>= 8
--- a/lib/std/test/fltbits.myr
+++ b/lib/std/test/fltbits.myr
@@ -1,18 +1,9 @@
 use std
 use testr
+use math
 
-var testnan
-
 const main = {
-	var si
-
-	/* Floating point mode traps on 9front. */
-	std.getsysinfo(&si)
-	match si.system
-	| "Plan9":	testnan = false
-	| _:		testnan = true
-	;;
-
+	math.fptrap(false)
 	testr.run([
 		[.name = "isnan", .fn = isnan01],
 		[.name = "bits-roundtrip-32", .fn = bitsround32],
@@ -25,26 +16,24 @@
 }
 
 const isnan01 = {c
-	if testnan
-		testr.check(c, std.isnan(std.flt64nan()), "std.flt64nan() should give a NaN")
-		testr.check(c, std.isnan(std.flt32nan()), "std.flt32nan() should give a NaN")
-	
-		/*
-		   a NaN should be {any sign bit}, then {8 or 11 exponent
-		   bits, all 1}, then {any non-zero sequence of 23 or 52
-		   bits}
-		 */
-		testr.check(c, std.isnan(std.flt64frombits(0xfff0000500000000ul)), "0xfff0000500000000 should be a NaN")
-		testr.check(c, std.isnan(std.flt64frombits(0x7ff0000500000000ul)), "0x7ff0000500000000 should be a NaN")
-		testr.check(c, std.isnan(std.flt32frombits(0xff800090)), "0xff800090 should be a NaN")
-		testr.check(c, std.isnan(std.flt32frombits(0x7f800090)), "0x7f800090 should be a NaN")
-	
-		/* if the significand bits are all 0, it's an infinity instead */
-		testr.check(c, !std.isnan(std.flt64frombits(0x7ff0000000000000ul)), "Infinities[1] should not be NaNs")
-		testr.check(c, !std.isnan(std.flt64frombits(0xfff0000000000000ul)), "Infinities[2] should not be NaNs")
-		testr.check(c, !std.isnan(std.flt32frombits(0xff800000)), "Infinities[3] should not be NaNs")
-		testr.check(c, !std.isnan(std.flt32frombits(0x7f800000)), "Infinities[4] should not be NaNs")
-	;;
+	testr.check(c, std.isnan(std.flt64nan()), "std.flt64nan() should give a NaN")
+	testr.check(c, std.isnan(std.flt32nan()), "std.flt32nan() should give a NaN")
+
+	/*
+	   a NaN should be {any sign bit}, then {8 or 11 exponent
+	   bits, all 1}, then {any non-zero sequence of 23 or 52
+	   bits}
+	 */
+	testr.check(c, std.isnan(std.flt64frombits(0xfff0000500000000ul)), "0xfff0000500000000 should be a NaN")
+	testr.check(c, std.isnan(std.flt64frombits(0x7ff0000500000000ul)), "0x7ff0000500000000 should be a NaN")
+	testr.check(c, std.isnan(std.flt32frombits(0xff800090)), "0xff800090 should be a NaN")
+	testr.check(c, std.isnan(std.flt32frombits(0x7f800090)), "0x7f800090 should be a NaN")
+
+	/* if the significand bits are all 0, it's an infinity instead */
+	testr.check(c, !std.isnan(std.flt64frombits(0x7ff0000000000000ul)), "Infinities[1] should not be NaNs")
+	testr.check(c, !std.isnan(std.flt64frombits(0xfff0000000000000ul)), "Infinities[2] should not be NaNs")
+	testr.check(c, !std.isnan(std.flt32frombits(0xff800000)), "Infinities[3] should not be NaNs")
+	testr.check(c, !std.isnan(std.flt32frombits(0x7f800000)), "Infinities[4] should not be NaNs")
 }
 
 const bitsround32 = {c
@@ -58,11 +47,9 @@
 		testr.check(c, u == v, "bits -> flt -> bits non-identity: {} != {}", u, v)
 	;;
 
-	if testnan
-		var nan_f = std.flt32frombits(0xff800090)
-		var nan_g = std.flt32frombits(std.flt32bits(nan_f))
-		testr.check(c, nan_f == nan_g, "flt -> bits -> flt non-identity for nan")
-	;;
+	var nan_f = std.flt32frombits(0xff800090)
+	var nan_g = std.flt32frombits(std.flt32bits(nan_f))
+	testr.check(c, nan_f == nan_g, "flt -> bits -> flt non-identity for nan")
 
 	var inf_f = std.flt32frombits(0x7f800000)
 	var inf_g = std.flt32frombits(std.flt32bits(inf_f))
@@ -80,11 +67,9 @@
 		testr.check(c, u == v, "bits -> flt -> bits non-identity: {} != {}", u, v)
 	;;
 
-	if testnan
-		var nan_f = std.flt64frombits(0x7ff000000000a000ul)
-		var nan_g = std.flt64frombits(std.flt64bits(nan_f))
-		testr.check(c, nan_f == nan_g, "flt -> bits -> flt non-identity for nan")
-	;;
+	var nan_f = std.flt64frombits(0x7ff000000000a000ul)
+	var nan_g = std.flt64frombits(std.flt64bits(nan_f))
+	testr.check(c, nan_f == nan_g, "flt -> bits -> flt non-identity for nan")
 
 	var inf_f = std.flt64frombits(0xfff0000000000000ul)
 	var inf_g = std.flt64frombits(std.flt64bits(inf_f))
@@ -117,7 +102,10 @@
 }
 
 const exploderound32 = {c
-	for f : [1.0, 0.00001, 123.45, 1111111111111111.2, -1.9, -0.0001, 0.000000000000000000000000000000000000006054601, std.flt32nan()][:]
+	var vals
+	vals = [1.0, 0.00001, 123.45, 1111111111111111.2, -1.9, -0.0001, 0.000000000000000000000000000000000000006054601, std.flt32nan()][:]
+
+	for f : vals
 		var n, e, s
 		(n, e, s) = std.flt32explode(f)
 		var g = std.flt32assem(n, e, s)
--- a/mbld/build.myr
+++ b/mbld/build.myr
@@ -126,6 +126,7 @@
 	var staletime
 
 	staletime = n.deptime
+	staletime = std.max(staletime, b.tctime)
 	for d : n.ndep
 		staletime = std.max(staletime, d.mtime)
 	;;
--- a/mbld/deps.myr
+++ b/mbld/deps.myr
@@ -651,3 +651,4 @@
 	std.slpush(&g.nodes, nod)
 	-> nod
 }
+
--- a/mbld/main.myr
+++ b/mbld/main.myr
@@ -152,9 +152,42 @@
 		.leaves = [][:],
 		.nodes = [][:],
 	])
+	b.tctime = tctime()
 	bld.addsysattrs(b, tags)
 	-> b
 }
+
+const tctime = {
+	var path : byte[1024]
+	var tm
+
+	tm = 0
+	for bin : [bld.opt_muse, bld.opt_mc][:]
+		match which(path[:], bin)
+		| `std.None:
+		| `std.Some p:
+			match std.fmtime(p)
+			| `std.Ok t:	tm = std.max(tm, t)
+			| `std.Err e:	tm = std.now()
+			;;
+		;;
+	;;
+	-> tm
+}
+
+const which = {buf, name
+	var path, f
+
+	path = std.getenvv("PATH", "/bin")
+	for p : std.bysplit(path, ":")
+		f = std.bfmt(buf, "{}/{}", p, name)
+		if std.fexists(f)
+			-> `std.Some f
+		;;
+	;;
+	-> `std.None
+}
+
 
 const findproj = {b
 	var dir
--- a/mbld/types.myr
+++ b/mbld/types.myr
@@ -7,6 +7,7 @@
 		tags	: std.htab(byte[:], (int, int, int))#
 		libs	: std.htab(byte[:], libdep#)#
 		deps	: depgraph#
+		tctime	: std.time	/* time the toolchain was updated */
 
 		/* in flight builds */
 		queue	: node#[:]
--- a/mk/c.mk
+++ b/mk/c.mk
@@ -29,7 +29,6 @@
 $(BIN): $(OBJ) $(EXTRADEP) $(DEPS)
 	$(CC) -o $@ $(OBJ) $(_LIBSRCHPATHS) $(_LIBPATHS) $(LDFLAGS)
 
-.PHONY: $(DEPS)
 $(DEPS):
 	@cd $(dir $@) && $(MAKE)
 
--- /dev/null
+++ b/support/pkg/default.nix
@@ -1,0 +1,25 @@
+{ pkgs ? (import <nixpkgs> {})
+, stdenv ? pkgs.stdenv
+, bison ? pkgs.bison
+, binutils ? pkgs.binutils
+, fetchurl ? pkgs.fetchurl
+}:
+
+stdenv.mkDerivation rec {
+  name = "myrddin";
+
+  src = ../..;
+
+  buildInputs = [ bison binutils ];
+
+  preBuild = ''
+    make bootstrap
+  '';
+
+  postPatch = ''
+    substituteInPlace "mbld/opts.myr" --replace '"ld"' '"${binutils}/bin/ld"'
+    substituteInPlace "configure"     --replace '"ld"' '"${binutils}/bin/ld"'
+    substituteInPlace "mbld/opts.myr" --replace '"as"' '"${binutils}/bin/as"'
+    substituteInPlace "configure"     --replace '"as"' '"${binutils}/bin/as"'
+  '';
+}