9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] [PATCH] first attempt at libmach tests
@ 2023-07-26 22:43 Jacob Moody
  2023-07-26 23:59 ` Jacob Moody
  0 siblings, 1 reply; 2+ messages in thread
From: Jacob Moody @ 2023-07-26 22:43 UTC (permalink / raw)
  To: 9front

Before attempting to add thumb support I think it would be nice
to have some basic tests for the current architectures that we
ship compilers for.

If there is other things that people feel should be tested
please let me know.

Thank you,
Moody

diff 53b322fc31bd0086336f6ca529338dd5125a9dcb uncommitted
--- /dev/null
+++ b/sys/src/libmach/test/bss.c
@@ -1,0 +1,1 @@
+char bsssymbol[32];
--- /dev/null
+++ b/sys/src/libmach/test/executable.c
@@ -1,0 +1,126 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include <mach.h>
+
+static struct {
+	char *name;
+	char c;
+	char *asm[3];
+} exectab[] = {
+	{ "386",  '8', {
+		"SUBL	$48,SP",
+		"MOVW	$9,AX",
+		"JMP	_main+7(SB)",
+	}},
+
+	{ "amd64", '6', {
+		"SUBQ	$50,SP",
+		"MOVW	$9,AX",
+		"JMP	_main+8(SB)",
+	}},
+
+	{ "arm", '5', {
+		"MOVW.W	R14,#-4c(R13)",
+		"MOVW	$#9,R2",
+		"B	_main+8",
+	}},
+
+	{ "arm64", '7', {
+		"SUB	$96,SP,SP",
+		"MOVZW	$9,R2",
+		"B	_main+8(SB)",
+	}},
+
+	{ "mips", 'v', {
+		"ADD	$-4c,R29",
+		"MOVW	$9,R2",
+		"JMP	_main+8(SB)",
+	}},
+
+	{ "spim", '0', {
+		"ADD	$-4c,R29",
+		"MOVW	$9,R2",
+		"JMP	_main+8(SB)",
+	}},
+
+	{ "sparc", 'k', {
+		"SUB	$50, R1",
+		"MOVW	$9, R2",
+		"BA	_main+8(SB)",
+	}},
+
+	{ "power", 'q', {
+		"SUB	$80,R1",
+		"MOVW	$9,R2",
+		"B	_main+8",
+	}},
+};
+
+void
+main(void)
+{
+	int i, fd;
+	int j, n;
+	Fhdr f;
+	Symbol sy, sy2;
+	char buf[128];
+	char buf2[128];
+	Map *m;
+
+	for(i = 0; i < nelem(exectab); i++){
+		snprint(buf, sizeof buf, "./%c.out", exectab[i].c);
+		fd = open(buf, OREAD);
+		if(fd < 0)
+			sysfatal("open: %r");
+		if(crackhdr(fd, &f) != 1)
+			sysfatal("crackhdr: %r");
+		machbytype(f.type);
+		m = newmap(nil, 0);
+		if(m == nil)
+			sysfatal("newmap: %r");
+		m = loadmap(m, fd, &f);
+		if(m == nil)
+			sysfatal("loadmap: %r");
+		if(syminit(fd, &f) < 0)
+			sysfatal("syminit: %r");
+
+		snprint(buf2, sizeof buf2, "/sys/src/libmach/test/%c.s", exectab[i].c);
+		filesym(0, buf, sizeof buf);
+		if(strcmp(buf, buf2) != 0)
+			sysfatal("invalid source symbols");
+
+		if(textsym(&sy, 0) != 1 || strcmp(sy.name, "_main") != 0)
+			sysfatal("no _main symbol");
+		if(textsym(&sy2, 1) != 1 || strcmp(sy2.name, "etext") != 0)
+			sysfatal("no etext symbol");
+		if(textsym(&sy2, 2) != 0)
+			sysfatal("extra text symbol: %s", sy2.name);
+
+		if(sy.value != f.entry)
+			sysfatal("_main is not the entrypoint");
+
+		n = 0;
+		for(j = 0; j < nelem(exectab[i].asm); j++){
+			n += machdata->das(m, sy.value+n, 0, buf, sizeof buf);
+			if(n < 0)
+				sysfatal("das: %r");
+			if(strcmp(exectab[i].asm[j], buf) != 0)
+				sysfatal("das mismatch %s vs %s", exectab[i].asm[j], buf);
+		}
+
+		n = 0;
+		for(j = 0; ; j++){
+			if(globalsym(&sy2, j) != 1)
+				break;
+			if(strcmp(sy2.name, "datsymbol") == 0)
+				n |= 0x1;
+			if(strcmp(sy2.name, "bsssymbol") == 0)
+				n |= 0x2;
+		}
+		if(n != 3)
+			sysfatal("missing datsymbol or bsssymbol: %d", n);
+		close(fd);
+	}
+	exits(nil);
+}
--- /dev/null
+++ b/sys/src/libmach/test/genasm.rc
@@ -1,0 +1,14 @@
+#!/bin/rc
+
+word=$1
+reg=$2
+br=$3
+echo '
+#define NPRIVATES 16
+GLOBL datsymbol(SB), 1, $'^$word^'
+
+TEXT _main(SB), 1, $(2*'^$word^'+NPRIVATES*4)
+	MOVW $9, '^$reg^'
+loop:
+	'^$br^' loop
+'
--- /dev/null
+++ b/sys/src/libmach/test/mkfile
@@ -1,0 +1,43 @@
+</$objtype/mkfile
+
+OS=8 6 5 7 k q v 0
+BINS=${OS:%=%.out}
+CLEANFILES=${OS:%=%.s}
+
+TEST=\
+	executable\
+
+
+</sys/src/cmd/mktest
+
+$O.executable: executable.$O $BINS
+	$LD $LDFLAGS -o $target executable.$O
+
+%.out: genasm.rc %.s bss.c
+	$stem'a' $AFLAGS $stem'.s'
+	$stem'c' $CFLAGS bss.c
+	$stem'l' $LDFLAGS -o $target $stem'.'^$stem bss.$stem
+
+6.s:
+	genasm.rc 8 AX JMP > $target
+
+8.s:
+	genasm.rc 4 AX JMP > $target
+
+5.s:
+	genasm.rc 4 R2 B > $target
+
+7.s:
+	genasm.rc 8 R2 B > $target
+
+k.s:
+	genasm.rc 4 R2 JMP > $target
+
+q.s:
+	genasm.rc 4 R2 BR > $target
+
+v.s:
+	genasm.rc 4 R2 JMP > $target
+
+0.s:
+	genasm.rc 4 R2 JMP > $target

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [9front] [PATCH] first attempt at libmach tests
  2023-07-26 22:43 [9front] [PATCH] first attempt at libmach tests Jacob Moody
@ 2023-07-26 23:59 ` Jacob Moody
  0 siblings, 0 replies; 2+ messages in thread
From: Jacob Moody @ 2023-07-26 23:59 UTC (permalink / raw)
  To: 9front

retracting after discussion with cinap on irc


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-07-27  0:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-26 22:43 [9front] [PATCH] first attempt at libmach tests Jacob Moody
2023-07-26 23:59 ` Jacob Moody

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).