shithub: mc

Download patch

ref: 6c574d4fc09d60095b4d74e865a67a8b1d2b2270
parent: 5cf18de659c44f4287c103dd81298c5f6b7e0951
author: Ori Bernstein <ori@eigenstate.org>
date: Sun Mar 19 20:38:35 EDT 2017

Step by line in gdb.

	I guess it's a good idea.

--- a/6/asm.h
+++ b/6/asm.h
@@ -131,39 +131,41 @@
 };
 
 struct Func {
-	char *name;   /* function name */
-	Type *type;   /* type of function */
+	char *name;	/* function name */
+	Srcloc loc;	/* location of definition */
+	Type *type;	/* type of function */
 
-	Node **args;  /* argument list */
-	size_t nargs; /* number of args, including hidden ones */
-	Htab *stkoff; /* Loc* -> int stkoff map */
-	Htab *envoff; /* Loc* -> int envoff map */
-	size_t stksz; /* stack size */
-	Node *ret;    /* return value */
+	Node **args;	/* argument list */
+	size_t nargs;	/* number of args, including hidden ones */
+	Htab *stkoff;	/* Loc* -> int stkoff map */
+	Htab *envoff;	/* Loc* -> int envoff map */
+	size_t stksz;	/* stack size */
+	Node *ret;	/* return value */
 
-	Cfg  *cfg;     /* flow graph */
-	char isexport; /* is this exported from the asm? */
-	char hasenv;   /* do we have an environment? */
+	Cfg  *cfg;	/* flow graph */
+	char isexport;	/* is this exported from the asm? */
+	char hasenv;	/* do we have an environment? */
 };
 
 struct Asmbb {
-	int id;       /* unique identifier */
-	char **lbls;  /* list of BB labels */
-	size_t nlbls; /* number of labels */
-	Insn **il;    /* instructions */
-	size_t ni;    /* number of instructions */
+	int id;		/* unique identifier */
+	char **lbls;	/* list of BB labels */
+	size_t nlbls;	/* number of labels */
+	Insn **il;	/* instructions */
+	size_t ni;	/* number of instructions */
 
-	Bitset *pred; /* set of predecessor BB ids */
-	Bitset *succ; /* set of successor BB ids */
-	Bitset *use;  /* registers used by this BB */
-	Bitset *def;  /* registers defined by this BB */
-	Bitset *livein; /* variables live on entrance to BB */
-	Bitset *liveout;  /* variables live on exit from BB */
+	Bitset *pred;	/* set of predecessor BB ids */
+	Bitset *succ;	/* set of successor BB ids */
+	Bitset *use;	/* registers used by this BB */
+	Bitset *def;	/* registers defined by this BB */
+	Bitset *livein;	/* variables live on entrance to BB */
+	Bitset *liveout;	/* variables live on exit from BB */
 };
 
 /* instruction selection state */
 struct Isel {
 	char *name;
+	char *cwd;
 
 	Cfg  *cfg;          /* cfg built with nodes */
 
@@ -255,8 +257,8 @@
 void simpglobl(Node *dcl, Htab *globls, Func ***fn, size_t *nfn, Node ***blob, size_t *nblob);
 void selfunc(Isel *is, Func *fn, Htab *globls, Htab *strtab);
 void gen(Node *file, char *out);
-void gengas(Node *file, char *out);
-void genp9(Node *file, char *out);
+void gengas(Node *file, FILE *fd);
+void genp9(Node *file, FILE *fd);
 
 /* blob stuff */
 Blob *mkblobpad(size_t sz);
--- a/6/gen.c
+++ b/6/gen.c
@@ -176,12 +176,20 @@
 
 void gen(Node *file, char *out)
 {
+	FILE *fd;
+
+	fd = fopen(out, "w");
+	if (!fd)
+		die("Couldn't open fd %s", out);
+
 	switch (asmsyntax) {
 	case Plan9:
-		genp9(file, out);	break;
+		genp9(file, fd);
+		break;
 	case Gnugaself:
 	case Gnugasmacho:
-		gengas(file, out);	break;
+		gengas(file, fd);
+		break;
 	default:
 		die("unknown target");  break;
 	}
--- a/6/gengas.c
+++ b/6/gengas.c
@@ -322,8 +322,10 @@
 static void genfunc(FILE *fd, Func *fn, Htab *globls, Htab *strtab)
 {
 	Isel is = {0,};
+	char cwd[1024];
 
 	resetregs();
+	getcwd(cwd, sizeof cwd);
 	is.reglocs = mkht(varhash, vareq);
 	is.name = fn->name;
 	is.stkoff = fn->stkoff;
@@ -331,6 +333,7 @@
 	is.globls = globls;
 	is.ret = fn->ret;
 	is.cfg = fn->cfg;
+	is.cwd = strdup(cwd);
 
 	if (fn->hasenv)
 		is.envp = locreg(ModeQ);
@@ -397,14 +400,14 @@
 	}
 }
 
-void gengas(Node *file, char *out)
+void gengas(Node *file, FILE *fd)
 {
 	Htab *globls, *strtab;
 	Node *n, **blob;
 	Func **fn;
+	char dir[1024], *path;
 	size_t nfn, nblob;
 	size_t i;
-	FILE *fd;
 
 	/* ensure that all physical registers have a loc created before any
 	 * other locs, so that locmap[Physreg] maps to the Loc for the physreg
@@ -440,9 +443,11 @@
 	}
 	popstab();
 
-	fd = fopen(out, "w");
-	if (!fd)
-		die("Couldn't open fd %s", out);
+	getcwd(dir, sizeof dir);
+	for (i = 0; i < file->file.nfiles; i++) {
+		path = file->file.files[i];
+		fprintf(fd, ".file %zd \"%s/%s\"\n", i + 1, dir, path);
+	}
 
 	strtab = mkht(strlithash, strliteq);
 	fprintf(fd, ".data\n");
--- a/6/genp9.c
+++ b/6/genp9.c
@@ -500,7 +500,7 @@
 	writeblob(fd, b, 0, lbl);
 }
 
-void genp9(Node *file, char *out)
+void genp9(Node *file, FILE *fd)
 {
 	Htab *globls, *strtab;
 	Node *n, **blob;
@@ -507,7 +507,6 @@
 	Func **fn;
 	size_t nfn, nblob;
 	size_t i;
-	FILE *fd;
 
 	/* ensure that all physical registers have a loc created before any
 	 * other locs, so that locmap[Physreg] maps to the Loc for the physreg
@@ -542,10 +541,6 @@
 		}
 	}
 	popstab();
-
-	fd = fopen(out, "w");
-	if (!fd)
-		die("Couldn't open fd %s", out);
 
 	strtab = mkht(strlithash, strliteq);
 	for (i = 0; i < nblob; i++)
--- a/6/insns.def
+++ b/6/insns.def
@@ -375,6 +375,18 @@
     Use(None),
     Def(None))
 
+Insn(Ifile,
+    "\t.file %v \"%v\"\n",
+    "\t//file: %V \"%V\"\n",
+    Use(None),
+    Def(None))
+
+Insn(Iloc,
+    "\t.loc %v\n",
+    "",
+    Use(None),
+    Def(None))
+
 Insn(Icomment,
     "\t#%v\n",
     "\t//%V:\n",
--- a/6/isel.c
+++ b/6/isel.c
@@ -1009,10 +1009,12 @@
 
 void selfunc(Isel *is, Func *fn, Htab *globls, Htab *strtab)
 {
+	int fileid, lastline;
 	Node *n;
 	Bb *bb;
 	size_t i, j;
 	char buf[128];
+	char *path;
 
 
 	for (i = 0; i < fn->cfg->nbb; i++)
@@ -1019,7 +1021,18 @@
 		lappend(&is->bb, &is->nbb, mkasmbb(fn->cfg->bb[i]));
 
 	is->curbb = is->bb[0];
+
+	fileid = fn->loc.file;
+	if (fileid >= 0 && fn->loc.line > 0) {
+		path = file->file.files[fileid];
+		bprintf(buf, sizeof buf, "%s/%s:%d", is->cwd, path, fn->loc.line);
+		g(is, Icomment, locstrlbl(buf), NULL);
+		bprintf(buf, sizeof buf, "%zd %d", fileid + 1, fn->loc.line);
+		g(is, Iloc, locstrlbl(buf), NULL);
+	}
+
 	prologue(is, fn, fn->stksz);
+	lastline = -1;
 	for (j = 0; j < fn->cfg->nbb - 1; j++) {
 		is->curbb = is->bb[j];
 		if (!is->bb[j])
@@ -1028,9 +1041,15 @@
 		for (i = 0; i < bb->nnl; i++) {
 			/* put in a comment that says where this line comes from */
 			n = bb->nl[i];
-			bprintf(buf, sizeof buf, "bb = %ld, bbidx = %ld, %s:%d",
-					j, i, file->file.files[n->loc.file], n->loc.line);
-			g(is, Icomment, locstrlbl(buf), NULL);
+			fileid = n->loc.file;
+			if (n->loc.file >= 0 && n->loc.line != -1 && n->loc.line != lastline) {
+				lastline = n->loc.line;
+				path = file->file.files[fileid];
+				bprintf(buf, sizeof buf, "%s/%s:%d", is->cwd, path, n->loc.line);
+				g(is, Icomment, locstrlbl(buf), NULL);
+				bprintf(buf, sizeof buf, "%zd %d", fileid + 1, n->loc.line);
+				g(is, Iloc, locstrlbl(buf), NULL);
+			}
 			isel(is, fn->cfg->bb[j]->nl[i]);
 		}
 	}
--- a/6/main.c
+++ b/6/main.c
@@ -98,6 +98,12 @@
 		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
 			die("Couldn't run assembler");
 	}
+	/* 
+	We don't want to keep the asm source around, but it's useful
+	for deubgging without mapping to line numbers.
+	*/
+	if (asmsyntax != Plan9)
+		unlink(asmsrc);
 }
 
 static char *dirname(char *path)
--- a/6/simp.c
+++ b/6/simp.c
@@ -1387,6 +1387,7 @@
 
 	fn = zalloc(sizeof(Func));
 	fn->name = strdup(name);
+	fn->loc = dcl->loc;
 	fn->type = dcl->decl.type;
 	fn->isexport = isexport(dcl);
 	fn->stksz = align(s->stksz, 8);