shithub: scc

Download patch

ref: cbb848825ea18d45601a09baafaf7000eef12526
parent: be4a2c8631c6401c37a7caf59d5bd476d2a5a125
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sat Feb 4 12:26:27 EST 2017

[cc1] Add prtree()

This functon is intended for debugging process and print the
tree passed as parameter to stderr.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -410,6 +410,7 @@
 #define accept(t) ((yytoken == (t)) ? next() : 0)
 
 /* code.c */
+extern void prtree(Node *np);
 extern void emit(unsigned, void *);
 extern Node *node(unsigned op, Type *tp, Node *left, Node *rigth);
 extern Node *varnode(Symbol *sym);
--- a/cc1/code.c
+++ b/cc1/code.c
@@ -137,6 +137,8 @@
 	[OTYP] = emittype,
 };
 
+static FILE *outfp = stdout;
+
 void
 freetree(Node *np)
 {
@@ -155,6 +157,16 @@
 }
 
 void
+prtree(Node *np)
+{
+	outfp = stderr;
+	fputs("DBG prtree", outfp);
+	emitnode(np);
+	putc('\n', outfp);
+	outfp = stdout;
+}
+
+void
 emit(unsigned op, void *arg)
 {
 	extern int failure;
@@ -184,7 +196,7 @@
 		c = 'X';
 	else
 		c = 'A';
-	printf("%c%u", c, sym->id);
+	fprintf(outfp, "%c%u", c, sym->id);
 }
 
 static void
@@ -199,9 +211,10 @@
 	case INT:
 	case ENUM:
 		u = (tp->prop & TSIGNED) ? (TUINT) sym->u.i : sym->u.u;
-		printf("#%c%llX",
-		       np->type->letter,
-		       (long long) sym->u.i & ones(tp->size));
+		fprintf(outfp,
+		        "#%c%llX",
+		        np->type->letter,
+		        (long long) sym->u.i & ones(tp->size));
 		break;
 	default:
 		abort();
@@ -220,7 +233,7 @@
 		 * and it means that we will have two '\t'
 		 * for the first element
 		 */
-		putchar('\t');
+		putc('\t', outfp);
 	}
 	(np->flags & NCONST) ? emitconst(np) : emitvar(np->sym);
 }
@@ -228,12 +241,12 @@
 static void
 emitletter(Type *tp)
 {
-	putchar(tp->letter);
+	putc(tp->letter, outfp);
 	switch (tp->op) {
 	case ARY:
 	case STRUCT:
 	case UNION:
-		printf("%u", tp->id);
+		fprintf(outfp, "%u", tp->id);
 	}
 }
 
@@ -251,16 +264,18 @@
 	switch (tp->op) {
 	case ARY:
 		emitletter(tp);
-		putchar('\t');
+		putc('\t', outfp);
 		emitletter(tp->type);
-		printf("\t#%c%llX\n",
-		       sizettype->letter, (long long) tp->n.elem);
+		fprintf(outfp,
+		        "\t#%c%llX\n",
+		        sizettype->letter, (long long) tp->n.elem);
 		return;
 	case UNION:
 	case STRUCT:
 		emitletter(tp);
 		tag = tp->tag->name;
-		printf("\t\"%s\t#%c%lX\t#%c%X\n",
+		fprintf(outfp,
+		       "\t\"%s\t#%c%lX\t#%c%X\n",
 		       (tag) ? tag : "",
 		       sizettype->letter,
 		       tp->size,
@@ -292,14 +307,15 @@
 		while (isprint(*bp) && bp < lim)
 			++bp;
 		if ((n = bp - s) > 1)
-			printf("\t#\"%.*s\n", n, s);
+			fprintf(outfp, "\t#\"%.*s\n", n, s);
 		else
 			bp = s;
 		if (bp == lim)
 			break;
 		do {
-			printf("\t#%c%02X\n",
-			       chartype->letter, (*bp++) & 0xFF);
+			fprintf(outfp,
+			        "\t#%c%02X\n",
+			        chartype->letter, (*bp++) & 0xFF);
 		} while (!isprint(*bp) && bp < lim);
 	}
 }
@@ -362,9 +378,9 @@
 {
 	Node *np = arg;
 
-	puts("\t(");
+	fputs("\t(\n", outfp);
 	emitdesig(np, np->type);
-	puts(")");
+	fputs(")\n", outfp);
 }
 
 static void
@@ -375,18 +391,18 @@
 	if (sym->flags & SEMITTED)
 		return;
 	emitvar(sym);
-	putchar('\t');
+	putc('\t', outfp);
 	if (sym->type->op == FTN) {
 		emitletter(sym->type->type);
-		putchar('\t');
+		putc('\t', outfp);
 	}
 	emitletter(sym->type);
-	printf("\t\"%s", (sym->name) ? sym->name : "");
+	fprintf(outfp, "\t\"%s", (sym->name) ? sym->name : "");
 	if (sym->flags & SFIELD)
-		printf("\t#%c%llX", sizettype->letter, sym->u.i);
+		fprintf(outfp, "\t#%c%llX", sizettype->letter, sym->u.i);
 	sym->flags |= SEMITTED;
 	if ((sym->flags & SHASINIT) == 0)
-		putchar('\n');
+		putc('\n', outfp);
 }
 
 static void
@@ -396,7 +412,7 @@
 
 	emitnode(lp);
 	if (np->type != voidtype)
-		printf("\tg%c", np->type->letter);
+		fprintf(outfp, "\tg%c", np->type->letter);
 }
 
 static void
@@ -408,7 +424,7 @@
 	emitnode(np->left);
 	emitnode(np->right);
 	if ((s = optxt[op]) != NULL)  {      /* do not print in OCOLON case */
-		printf("\t%s", optxt[op]);
+		fprintf(outfp, "\t%s", optxt[op]);
 		emitletter(np->type);
 	}
 }
@@ -419,7 +435,7 @@
 	Node *np = arg;
 
 	emitnode(np);
-	putchar('\n');
+	putc('\n', outfp);
 	freetree(np);
 }
 
@@ -429,11 +445,11 @@
 	Symbol *sym = arg, **sp;
 
 	emitdcl(op, arg);
-	puts("{");
+	fputs("{\n", outfp);
 
 	for (sp = sym->u.pars; sp && *sp; ++sp)
 		emit(ODECL, *sp);
-	puts("\\");
+	fputs("\\\n", outfp);
 	free(sym->u.pars);
 	sym->u.pars = NULL;
 }
@@ -441,7 +457,7 @@
 static void
 emittext(unsigned op, void *arg)
 {
-	fputs(optxt[op], stdout);
+	fputs(optxt[op], outfp);
 }
 
 static void
@@ -448,7 +464,7 @@
 emitsymid(unsigned op, void *arg)
 {
 	Symbol *sym = arg;
-	printf(optxt[op], sym->id);
+	fprintf(outfp, optxt[op], sym->id);
 }
 
 Node *