shithub: scc

Download patch

ref: 1fa83f138d13af191704b2bf3504091c6c92a57a
parent: 57ca51bc90319ba7b58eefac227b3dfa8c671032
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed May 6 15:28:36 EDT 2015

Convert emitswitch into private of code.c

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -28,6 +28,8 @@
 
 typedef struct ctype Type;
 typedef struct symbol Symbol;
+typedef struct caselist Caselist;
+typedef struct node Node;
 
 struct ctype {
 	uint8_t op;           /* type builder operator */
@@ -79,8 +81,18 @@
 
 extern void pushctx(void), popctx(void);
 
-typedef struct caselist Caselist;
+struct scase {
+	Symbol *label;
+	Node *expr;
+	struct scase *next;
+};
 
+struct caselist {
+	short nr;
+	Symbol *deflabel;
+	struct scase *head;
+};
+
 extern void compound(Symbol *lbreak, Symbol *lcont, Caselist *lswitch);
 
 extern Type *typename(void);
@@ -136,7 +148,7 @@
 extern uint8_t next(void);
 extern void expect(uint8_t tok);
 
-typedef struct node {
+struct node {
 	uint8_t op;
 	Type *type;
 	Symbol *sym;
@@ -144,7 +156,7 @@
 	bool symbol: 1;
 	bool constant : 1;
 	struct node *left, *right;
-} Node;
+};
 
 enum {
 	OPTR = 1, OADD, OSIZE, OMUL, OSUB,
@@ -155,7 +167,7 @@
 	OCOMMA, OCAST, OSYM, OASK, OFIELD, OTYP,
 	OLABEL, ODEFAULT, OCASE, OSTRUCT, OJUMP, OBRANCH,
 	OEXPR, OEFUN, OESTRUCT, OELOOP, OBLOOP, OPRINT,
-	OFUN, ORET, ODECL,
+	OFUN, ORET, ODECL, OSWITCH,
 	/* TODO: This order is important, but must be changed */
 	OAND, OOR,
 	/*
@@ -165,11 +177,7 @@
 	OEQ = 0x40, ONE, OLT, OGE, OLE, OGT
 };
 
-/*TODO: clean these declarations */
-extern void
-	emit(uint8_t, void *),
-	emitswitch(short);
-
+extern void emit(uint8_t, void *);
 extern Node *node(uint8_t op, Type *tp, Node *left, Node *rigth);
 extern Node *symbol(Symbol *sym);
 extern void freetree(Node *np);
--- a/cc1/code.c
+++ b/cc1/code.c
@@ -13,7 +13,8 @@
             emitsizeof(uint8_t, void *), emitexp(uint8_t, void *),
             emitsymid(uint8_t, void *), emittext(uint8_t, void *),
             emitprint(uint8_t, void *), emitfun(uint8_t, void *),
-            emitret(uint8_t, void *), emitdcl(uint8_t, void *);
+            emitret(uint8_t, void *), emitdcl(uint8_t, void *),
+            emitswitch(uint8_t, void *);
 
 char *optxt[] = {
 	[OADD] = "+",
@@ -121,7 +122,8 @@
 	[OPRINT] = emitprint,
 	[OFUN] = emitfun,
 	[ORET] = emitret,
-	[ODECL] = emitdcl
+	[ODECL] = emitdcl,
+	[OSWITCH] = emitswitch
 };
 
 void
@@ -303,10 +305,12 @@
 	printf(optxt[op], sym->id);
 }
 
-void
-emitswitch(short nr)
+static void
+emitswitch(uint8_t op, void *arg)
 {
-	printf("\teI\t#%0x", nr);
+	Caselist *lcase = arg;
+
+	printf("\teI\t#%0x", lcase->nr);
 }
 
 void
--- a/cc1/stmt.c
+++ b/cc1/stmt.c
@@ -7,18 +7,6 @@
 #include "../inc/cc.h"
 #include "cc1.h"
 
-struct scase {
-	Symbol *label;
-	Node *expr;
-	struct scase *next;
-};
-
-struct caselist {
-	short nr;
-	Symbol *deflabel;
-	struct scase *head;
-};
-
 Symbol *curfun;
 
 extern Node *convert(Node *np, Type *tp1, char iscast);
@@ -249,7 +237,7 @@
 	emit(OJUMP, lcond);
 	stmt(lbreak, lcont, &lcase);
 	emit(OLABEL, lcond);
-	emitswitch(lcase.nr);
+	emit(OSWITCH, &lcase);
 	emit(OEXPR, cond);
 	for (p = lcase.head; p; p = next) {
 		emit(OCASE, p->label);