9front - general discussion about 9front
 help / color / mirror / Atom feed
From: Jacob Moody <moody@posixcafe.org>
To: 9front@9front.org
Subject: [9front] [PATCH] first attempt at libmach tests
Date: Wed, 26 Jul 2023 17:43:36 -0500	[thread overview]
Message-ID: <a06bb27d-b02e-8f44-d9e4-f491c6729ea1@posixcafe.org> (raw)

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

             reply	other threads:[~2023-07-26 22:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-26 22:43 Jacob Moody [this message]
2023-07-26 23:59 ` Jacob Moody

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a06bb27d-b02e-8f44-d9e4-f491c6729ea1@posixcafe.org \
    --to=moody@posixcafe.org \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).