Patchwork libgo patch committed: Use intgo

login
register
mail settings
Submitter Ian Taylor
Date Oct. 31, 2012, 8:50 p.m.
Message ID <mcra9v2nz7p.fsf@google.com>
Download mbox | patch
Permalink /patch/195995/
State New
Headers show

Comments

Ian Taylor - Oct. 31, 2012, 8:50 p.m.
This patch to libgo changes the goc2c program to change the Go type
"int" to the C type "intgo".  This is in preparation for changing the Go
type "int" to be 64 bits on x86_64.  The goc2c program used to be used
by the gc compiler, but it no longer is, so while I was there I removed
the gc support.  Bootstrapped and ran Go testsuite on
x86_64-unknown-linux-gnu.  Committed to mainline.

Ian

Patch

diff -r 47a142d6f93e libgo/Makefile.am
--- a/libgo/Makefile.am	Tue Oct 30 12:27:12 2012 -0700
+++ b/libgo/Makefile.am	Wed Oct 31 13:46:56 2012 -0700
@@ -536,35 +536,35 @@ 
 	$(CC_FOR_BUILD) $(CFLAGS_FOR_BUILD) $(LDFLAGS_FOR_BUILD) -o $@ $<
 
 malloc.c: $(srcdir)/runtime/malloc.goc goc2c
-	./goc2c --gcc $< > $@.tmp
+	./goc2c $< > $@.tmp
 	mv -f $@.tmp $@
 
 mprof.c: $(srcdir)/runtime/mprof.goc goc2c
-	./goc2c --gcc $< > $@.tmp
+	./goc2c $< > $@.tmp
 	mv -f $@.tmp $@
 
 reflect.c: $(srcdir)/runtime/reflect.goc goc2c
-	./goc2c --gcc $< > $@.tmp
+	./goc2c $< > $@.tmp
 	mv -f $@.tmp $@
 
 runtime1.c: $(srcdir)/runtime/runtime1.goc goc2c
-	./goc2c --gcc $< > $@.tmp
+	./goc2c $< > $@.tmp
 	mv -f $@.tmp $@
 
 sema.c: $(srcdir)/runtime/sema.goc goc2c
-	./goc2c --gcc $< > $@.tmp
+	./goc2c $< > $@.tmp
 	mv -f $@.tmp $@
 
 sigqueue.c: $(srcdir)/runtime/sigqueue.goc goc2c
-	./goc2c --gcc --go-pkgpath os_signal $< > $@.tmp
+	./goc2c --go-pkgpath os_signal $< > $@.tmp
 	mv -f $@.tmp $@
 
 time.c: $(srcdir)/runtime/time.goc goc2c
-	./goc2c --gcc $< > $@.tmp
+	./goc2c $< > $@.tmp
 	mv -f $@.tmp $@
 
 %.c: $(srcdir)/runtime/%.goc goc2c
-	./goc2c --gcc $< > $@.tmp
+	./goc2c $< > $@.tmp
 	mv -f $@.tmp $@
 
 go_bufio_files = \
diff -r 47a142d6f93e libgo/runtime/goc2c.c
--- a/libgo/runtime/goc2c.c	Tue Oct 30 12:27:12 2012 -0700
+++ b/libgo/runtime/goc2c.c	Wed Oct 31 13:46:56 2012 -0700
@@ -30,13 +30,10 @@ 
 #include <string.h>
 #include <errno.h>
 
-/* Whether we're emitting for gcc */
-static int gcc;
-
-/* Package path to use; only meaningful for gcc */
+/* Package path to use.  */
 static const char *pkgpath;
 
-/* Package prefix to use; only meaningful for gcc */
+/* Package prefix to use.  */
 static const char *prefix;
 
 /* File and line number */
@@ -50,53 +47,6 @@ 
 	char *type;
 };
 
-/* index into type_table */
-enum {
-	Bool,
-	Float,
-	Int,
-	Uint,
-	Uintptr,
-	String,
-	Slice,
-	Eface,
-};
-
-static struct {
-	char *name;
-	int size;
-} type_table[] = {
-	/* variable sized first, for easy replacement */
-	/* order matches enum above */
-	/* default is 32-bit architecture sizes */
-	"bool",		1,
-	"float",	4,
-	"int",		4,
-	"uint",		4,
-	"uintptr",	4,
-	"String",	8,
-	"Slice",	12,
-	"Eface",	8,
-
-	/* fixed size */
-	"float32",	4,
-	"float64",	8,
-	"byte",		1,
-	"int8",		1,
-	"uint8",	1,
-	"int16",	2,
-	"uint16",	2,
-	"int32",	4,
-	"uint32",	4,
-	"int64",	8,
-	"uint64",	8,
-
-	NULL,
-};
-
-/* Fixed structure alignment (non-gcc only) */
-int structround = 4;
-
 char *argv0;
 
 static void
@@ -147,6 +97,15 @@ 
 	return ret;
 }
 
+/* Copy a string into memory without fail.  */
+static char *
+xstrdup(const char *p)
+{
+	char *ret = xmalloc(strlen(p) + 1);
+	strcpy(ret, p);
+	return ret;
+}
+
 /* Free a list of parameters.  */
 static void
 free_params(struct params *p)
@@ -351,14 +310,29 @@ 
 	unsigned int len;
 
 	p = read_token_no_eof();
-	if (*p != '*')
+	if (*p != '*') {
+		/* Convert the Go type "int" to the C type "intgo",
+		   and similarly for "uint".  */
+		if (strcmp(p, "int") == 0)
+			return xstrdup("intgo");
+		else if (strcmp(p, "uint") == 0)
+			return xstrdup("uintgo");
 		return p;
+	}
 	op = p;
 	pointer_count = 0;
 	while (*p == '*') {
 		++pointer_count;
 		++p;
 	}
+
+	/* Convert the Go type "int" to the C type "intgo", and
+	   similarly for "uint".  */
+	if (strcmp(p, "int") == 0)
+	  p = (char *) "intgo";
+	else if (strcmp(p, "uint") == 0)
+	  p = (char *) "uintgo";
+
 	len = strlen(p);
 	q = xmalloc(len + pointer_count + 1);
 	memcpy(q, p, len);
@@ -372,39 +346,19 @@ 
 	return q;
 }
 
-/* Return the size of the given type. */
-static int
-type_size(char *p)
-{
-	int i;
-
-	if(p[strlen(p)-1] == '*')
-		return type_table[Uintptr].size;
-
-	for(i=0; type_table[i].name; i++)
-		if(strcmp(type_table[i].name, p) == 0)
-			return type_table[i].size;
-	if(!gcc) {
-		sysfatal("%s:%ud: unknown type %s\n", file, lineno, p);
-	}
-	return 1;
-}
-
 /*
  * Read a list of parameters.  Each parameter is a name and a type.
  * The list ends with a ')'.  We have already read the '('.
  */
 static struct params *
-read_params(int *poffset)
+read_params()
 {
 	char *token;
 	struct params *ret, **pp, *p;
-	int offset, size, rnd;
 
 	ret = NULL;
 	pp = &ret;
 	token = read_token_no_eof();
-	offset = 0;
 	if (strcmp(token, ")") != 0) {
 		while (1) {
 			p = xmalloc(sizeof(struct params));
@@ -414,14 +368,6 @@ 
 			*pp = p;
 			pp = &p->next;
 
-			size = type_size(p->type);
-			rnd = size;
-			if(rnd > structround)
-				rnd = structround;
-			if(offset%rnd)
-				offset += rnd - offset%rnd;
-			offset += size;
-
 			token = read_token_no_eof();
 			if (strcmp(token, ",") != 0)
 				break;
@@ -432,8 +378,6 @@ 
 		sysfatal("%s:%ud: expected '('\n",
 			file, lineno);
 	}
-	if (poffset != NULL)
-		*poffset = offset;
 	return ret;
 }
 
@@ -442,7 +386,7 @@ 
  * '{' character.  Returns 1 if it read a header, 0 at EOF.
  */
 static int
-read_func_header(char **name, struct params **params, int *paramwid, struct params **rets)
+read_func_header(char **name, struct params **params, struct params **rets)
 {
 	int lastline;
 	char *token;
@@ -474,13 +418,13 @@ 
 		sysfatal("%s:%ud: expected \"(\"\n",
 			file, lineno);
 	}
-	*params = read_params(paramwid);
+	*params = read_params();
 
 	token = read_token();
 	if (token == NULL || strcmp(token, "(") != 0)
 		*rets = NULL;
 	else {
-		*rets = read_params(NULL);
+		*rets = read_params();
 		token = read_token();
 	}
 	if (token == NULL || strcmp(token, "{") != 0) {
@@ -505,43 +449,6 @@ 
 	}
 }
 
-/* Write a 6g function header.  */
-static void
-write_6g_func_header(char *package, char *name, struct params *params,
-		     int paramwid, struct params *rets)
-{
-	int first, n;
-
-	printf("void\n%s·%s(", package, name);
-	first = 1;
-	write_params(params, &first);
-
-	/* insert padding to align output struct */
-	if(rets != NULL && paramwid%structround != 0) {
-		n = structround - paramwid%structround;
-		if(n & 1)
-			printf(", uint8");
-		if(n & 2)
-			printf(", uint16");
-		if(n & 4)
-			printf(", uint32");
-	}
-
-	write_params(rets, &first);
-	printf(")\n{\n");
-}
-
-/* Write a 6g function trailer.  */
-static void
-write_6g_func_trailer(struct params *rets)
-{
-	struct params *p;
-
-	for (p = rets; p != NULL; p = p->next)
-		printf("\tFLUSH(&%s);\n", p->name);
-	printf("}\n");
-}
-
 /* Define the gcc function return type if necessary.  */
 static void
 define_gcc_return_type(char *package, char *name, struct params *rets)
@@ -619,14 +526,10 @@ 
 
 /* Write out a function header.  */
 static void
-write_func_header(char *package, char *name,
-		  struct params *params, int paramwid,
+write_func_header(char *package, char *name, struct params *params, 
 		  struct params *rets)
 {
-	if (gcc)
-		write_gcc_func_header(package, name, params, rets);
-	else
-		write_6g_func_header(package, name, params, paramwid, rets);
+	write_gcc_func_header(package, name, params, rets);
 	printf("#line %d \"%s\"\n", lineno, file);
 }
 
@@ -635,10 +538,7 @@ 
 write_func_trailer(char *package, char *name,
 		   struct params *rets)
 {
-	if (gcc)
-		write_gcc_func_trailer(package, name, rets);
-	else
-		write_6g_func_trailer(rets);
+	write_gcc_func_trailer(package, name, rets);
 }
 
 /*
@@ -713,12 +613,11 @@ 
 {
 	char *package, *name;
 	struct params *params, *rets;
-	int paramwid;
 
 	package = read_package();
 	read_preprocessor_lines();
-	while (read_func_header(&name, &params, &paramwid, &rets)) {
-		write_func_header(package, name, params, paramwid, rets);
+	while (read_func_header(&name, &params, &rets)) {
+		write_func_header(package, name, params, rets);
 		copy_body();
 		write_func_trailer(package, name, rets);
 		free(name);
@@ -731,7 +630,7 @@ 
 static void
 usage(void)
 {
-	sysfatal("Usage: goc2c [--6g | --gc] [--go-pkgpath PKGPATH] [--go-prefix PREFIX] [file]\n");
+	sysfatal("Usage: goc2c [--go-pkgpath PKGPATH] [--go-prefix PREFIX] [file]\n");
 }
 
 void
@@ -743,11 +642,7 @@ 
 	while(argc > 1 && argv[1][0] == '-') {
 		if(strcmp(argv[1], "-") == 0)
 			break;
-		if(strcmp(argv[1], "--6g") == 0)
-			gcc = 0;
-		else if(strcmp(argv[1], "--gcc") == 0)
-			gcc = 1;
-		else if (strcmp(argv[1], "--go-pkgpath") == 0 && argc > 2) {
+		if (strcmp(argv[1], "--go-pkgpath") == 0 && argc > 2) {
 			pkgpath = argv[2];
 			argc--;
 			argv++;
@@ -775,18 +670,6 @@ 
 		sysfatal("open %s: %r\n", file);
 	}
 
-	if(!gcc) {
-		// 6g etc; update size table
-		goarch = getenv("GOARCH");
-		if(goarch != NULL && strcmp(goarch, "amd64") == 0) {
-			type_table[Uintptr].size = 8;
-			type_table[String].size = 16;
-			type_table[Slice].size = 8+4+4;
-			type_table[Eface].size = 8+8;
-			structround = 8;
-		}
-	}
-
 	printf("// AUTO-GENERATED by autogen.sh; DO NOT EDIT\n\n");
 	process_file();
 	exit(0);