shithub: mc

Download patch

ref: 6361a241f462769284053e07d51cd0638d3e94af
parent: 631e14604ba34ad97514cf302de75c3c3102d316
author: Ori Bernstein <[email protected]>
date: Wed Jan 20 18:06:03 EST 2016

Add a distinction between label values and names.

	This allows us to have labels work across scopes in assembly
	source.

--- a/mi/cfg.c
+++ b/mi/cfg.c
@@ -214,7 +214,7 @@
 		}
 	}
 	post = mkbb(cfg);
-	bprintf(buf, sizeof buf, ".R%d", nextret++);
+	bprintf(buf, sizeof buf, ".Lret.%d", nextret++);
 	label(cfg, mklbl(fn->loc, buf), post);
 
 	cfg->start = pre;
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -580,7 +580,10 @@
 	| Tendln {$$ = NULL;}
 	;
 
-goto    : Tgoto Tident {$$ = mkexpr($1->loc, Ojmp, mklbl($2->loc, $2->id), NULL);}
+goto    : Tgoto Tident {
+		$$ = mkexpr($1->loc, Ojmp, mklbl($2->loc, ""), NULL);
+		$$->expr.args[0]->lit.lblname = strdup($2->id);
+	}
 	;
 
 retexpr : Tret expr {$$ = mkexpr($1->loc, Oret, $2, NULL);}
@@ -971,7 +974,12 @@
 	}
 	;
 
-label   : Tcolon Tident {$$ = mklbl($2->loc, $2->id);}
+label   : Tcolon Tident {
+		char buf[512];
+		genlblstr(buf, sizeof buf, $2->id);
+		$$ = mklbl($2->loc, buf);
+		$$->expr.args[0]->lit.lblname = strdup($2->id);
+	}
 	;
 
 %%
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -1530,7 +1530,7 @@
 		break;
 	case Ojmp: /* goto void* -> void */
 		if (args[0]->type == Nlit && args[0]->lit.littype == Llbl)
-			args[0] = getlbl(curstab(), args[0]->loc, args[0]->lit.lblval);
+			args[0] = getlbl(curstab(), args[0]->loc, args[0]->lit.lblname);
 		infersub(st, n, ret, sawret, &isconst);
 		settype(st, n, mktype(Zloc, Tyvoid));
 		break;
@@ -1864,7 +1864,7 @@
 		break;
 	case Nlit:
 		if (n->lit.littype == Llbl)
-			putlbl(curstab(), n->lit.lblval, n);
+			putlbl(curstab(), n->lit.lblname, n);
 		break;
 	case Nname:
 	case Nuse:
--- a/parse/node.c
+++ b/parse/node.c
@@ -231,6 +231,7 @@
 	assert(lbl != NULL);
 	n = mknode(loc, Nlit);
 	n->lit.littype = Llbl;
+	n->lit.lblname = NULL;
 	n->lit.lblval = strdup(lbl);
 	return mkexpr(loc, Olit, n, NULL);
 }
@@ -238,7 +239,10 @@
 char *genlblstr(char *buf, size_t sz, char *suffix)
 {
 	static int nextlbl;
-	bprintf(buf, 128, ".L%d%s", nextlbl++, suffix);
+	size_t len;
+
+	len = snprintf(buf, 128, ".L%d.%s", nextlbl++, suffix);
+	assert(len <= sz);
 	return buf;
 }
 
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -264,7 +264,10 @@
 				double fltval;
 				uint32_t chrval;
 				Str strval;
-				char *lblval;
+				struct {
+					char *lblval;
+					char *lblname;
+				};
 				int boolval;
 				Node *fnval;
 			};