Commit b546f507 authored by Lucio De Re's avatar Lucio De Re Committed by Russ Cox

runtime: make goc2c build on Plan 9

pkg/runtime/Makefile:
. Adjusted so "goc2c.c" is built using the Plan 9 libraries.

pkg/runtime/goc2c.c:
. Added/subtracted #include headers to correspond to Plan 9
  toolkit.
. Changed fprintf(stderr,...)/exit() combinations to
  sysfatal() calls, adjusted the "%u" format to "%ud".
. Added exits(0) at the end of main().
. Made main() a void-returning function and removed the
  "return 0" at the end of it.

Tested on UBUNTU and Plan 9 only.

R=r, rsc
CC=golang-dev
https://golang.org/cl/4626093
parent 025abd53
...@@ -120,7 +120,7 @@ $(GOARCH)/asm.h: mkasmh.sh runtime.acid.$(GOARCH) ...@@ -120,7 +120,7 @@ $(GOARCH)/asm.h: mkasmh.sh runtime.acid.$(GOARCH)
mv -f $@.x $@ mv -f $@.x $@
goc2c: goc2c.c goc2c: goc2c.c
quietgcc -o $@ $< quietgcc -o $@ -I "$(GOROOT)/include" $< "$(GOROOT)/lib/lib9.a"
mkversion: mkversion.c mkversion: mkversion.c
quietgcc -o $@ -I "$(GOROOT)/include" $< "$(GOROOT)/lib/lib9.a" quietgcc -o $@ -I "$(GOROOT)/include" $< "$(GOROOT)/lib/lib9.a"
......
...@@ -2,26 +2,27 @@ ...@@ -2,26 +2,27 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
/* Translate a .goc file into a .c file. A .goc file is a combination /*
of a limited form of Go with C. */ * Translate a .goc file into a .c file. A .goc file is a combination
* of a limited form of Go with C.
*/
/* /*
package PACKAGENAME package PACKAGENAME
{# line} {# line}
func NAME([NAME TYPE { , NAME TYPE }]) [(NAME TYPE { , NAME TYPE })] \{ func NAME([NAME TYPE { , NAME TYPE }]) [(NAME TYPE { , NAME TYPE })] \{
C code with proper brace nesting C code with proper brace nesting
\} \}
*/ */
/* We generate C code which implements the function such that it can /*
be called from Go and executes the C code. */ * We generate C code which implements the function such that it can
* be called from Go and executes the C code.
*/
#include <assert.h> #include <u.h>
#include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <libc.h>
#include <string.h>
#include <errno.h>
/* Whether we're emitting for gcc */ /* Whether we're emitting for gcc */
static int gcc; static int gcc;
...@@ -88,16 +89,14 @@ int structround = 4; ...@@ -88,16 +89,14 @@ int structround = 4;
static void static void
bad_eof(void) bad_eof(void)
{ {
fprintf(stderr, "%s:%u: unexpected EOF\n", file, lineno); sysfatal("%s:%ud: unexpected EOF\n", file, lineno);
exit(1);
} }
/* Out of memory. */ /* Out of memory. */
static void static void
bad_mem(void) bad_mem(void)
{ {
fprintf(stderr, "%s:%u: out of memory\n", file, lineno); sysfatal("%s:%ud: out of memory\n", file, lineno);
exit(1);
} }
/* Allocate memory without fail. */ /* Allocate memory without fail. */
...@@ -196,8 +195,10 @@ getchar_skipping_comments(void) ...@@ -196,8 +195,10 @@ getchar_skipping_comments(void)
} }
} }
/* Read and return a token. Tokens are delimited by whitespace or by /*
[(),{}]. The latter are all returned as single characters. */ * Read and return a token. Tokens are delimited by whitespace or by
* [(),{}]. The latter are all returned as single characters.
*/
static char * static char *
read_token(void) read_token(void)
{ {
...@@ -259,11 +260,11 @@ read_package(void) ...@@ -259,11 +260,11 @@ read_package(void)
char *token; char *token;
token = read_token_no_eof(); token = read_token_no_eof();
if (token == nil)
sysfatal("%s:%ud: no token\n", file, lineno);
if (strcmp(token, "package") != 0) { if (strcmp(token, "package") != 0) {
fprintf(stderr, sysfatal("%s:%ud: expected \"package\", got \"%s\"\n",
"%s:%u: expected \"package\", got \"%s\"\n",
file, lineno, token); file, lineno, token);
exit(1);
} }
return read_token_no_eof(); return read_token_no_eof();
} }
...@@ -290,8 +291,10 @@ read_preprocessor_lines(void) ...@@ -290,8 +291,10 @@ read_preprocessor_lines(void)
} }
} }
/* Read a type in Go syntax and return a type in C syntax. We only /*
permit basic types and pointers. */ * Read a type in Go syntax and return a type in C syntax. We only
* permit basic types and pointers.
*/
static char * static char *
read_type(void) read_type(void)
{ {
...@@ -333,13 +336,14 @@ type_size(char *p) ...@@ -333,13 +336,14 @@ type_size(char *p)
for(i=0; type_table[i].name; i++) for(i=0; type_table[i].name; i++)
if(strcmp(type_table[i].name, p) == 0) if(strcmp(type_table[i].name, p) == 0)
return type_table[i].size; return type_table[i].size;
fprintf(stderr, "%s:%u: unknown type %s\n", file, lineno, p); sysfatal("%s:%ud: unknown type %s\n", file, lineno, p);
exit(1);
return 0; return 0;
} }
/* Read a list of parameters. Each parameter is a name and a type. /*
The list ends with a ')'. We have already read the '('. */ * 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 * static struct params *
read_params(int *poffset) read_params(int *poffset)
{ {
...@@ -375,17 +379,18 @@ read_params(int *poffset) ...@@ -375,17 +379,18 @@ read_params(int *poffset)
} }
} }
if (strcmp(token, ")") != 0) { if (strcmp(token, ")") != 0) {
fprintf(stderr, "%s:%u: expected '('\n", sysfatal("%s:%ud: expected '('\n",
file, lineno); file, lineno);
exit(1);
} }
if (poffset != NULL) if (poffset != NULL)
*poffset = offset; *poffset = offset;
return ret; return ret;
} }
/* Read a function header. This reads up to and including the initial /*
'{' character. Returns 1 if it read a header, 0 at EOF. */ * Read a function header. This reads up to and including the initial
* '{' character. Returns 1 if it read a header, 0 at EOF.
*/
static int static int
read_func_header(char **name, struct params **params, int *paramwid, struct params **rets) read_func_header(char **name, struct params **params, int *paramwid, struct params **rets)
{ {
...@@ -416,9 +421,8 @@ read_func_header(char **name, struct params **params, int *paramwid, struct para ...@@ -416,9 +421,8 @@ read_func_header(char **name, struct params **params, int *paramwid, struct para
token = read_token(); token = read_token();
if (token == NULL || strcmp(token, "(") != 0) { if (token == NULL || strcmp(token, "(") != 0) {
fprintf(stderr, "%s:%u: expected \"(\"\n", sysfatal("%s:%ud: expected \"(\"\n",
file, lineno); file, lineno);
exit(1);
} }
*params = read_params(paramwid); *params = read_params(paramwid);
...@@ -430,9 +434,8 @@ read_func_header(char **name, struct params **params, int *paramwid, struct para ...@@ -430,9 +434,8 @@ read_func_header(char **name, struct params **params, int *paramwid, struct para
token = read_token(); token = read_token();
} }
if (token == NULL || strcmp(token, "{") != 0) { if (token == NULL || strcmp(token, "{") != 0) {
fprintf(stderr, "%s:%u: expected \"{\"\n", sysfatal("%s:%ud: expected \"{\"\n",
file, lineno); file, lineno);
exit(1);
} }
return 1; return 1;
} }
...@@ -581,8 +584,10 @@ write_func_trailer(char *package, char *name, ...@@ -581,8 +584,10 @@ write_func_trailer(char *package, char *name,
write_6g_func_trailer(rets); write_6g_func_trailer(rets);
} }
/* Read and write the body of the function, ending in an unnested } /*
(which is read but not written). */ * Read and write the body of the function, ending in an unnested }
* (which is read but not written).
*/
static void static void
copy_body(void) copy_body(void)
{ {
...@@ -669,15 +674,15 @@ process_file(void) ...@@ -669,15 +674,15 @@ process_file(void)
static void static void
usage(void) usage(void)
{ {
fprintf(stderr, "Usage: goc2c [--6g | --gc] [file]\n"); sysfatal("Usage: goc2c [--6g | --gc] [file]\n");
exit(1);
} }
int void
main(int argc, char **argv) main(int argc, char **argv)
{ {
char *goarch; char *goarch;
argv0 = argv[0];
while(argc > 1 && argv[1][0] == '-') { while(argc > 1 && argv[1][0] == '-') {
if(strcmp(argv[1], "-") == 0) if(strcmp(argv[1], "-") == 0)
break; break;
...@@ -694,7 +699,7 @@ main(int argc, char **argv) ...@@ -694,7 +699,7 @@ main(int argc, char **argv)
if(argc <= 1 || strcmp(argv[1], "-") == 0) { if(argc <= 1 || strcmp(argv[1], "-") == 0) {
file = "<stdin>"; file = "<stdin>";
process_file(); process_file();
return 0; exits(0);
} }
if(argc > 2) if(argc > 2)
...@@ -702,8 +707,7 @@ main(int argc, char **argv) ...@@ -702,8 +707,7 @@ main(int argc, char **argv)
file = argv[1]; file = argv[1];
if(freopen(file, "r", stdin) == 0) { if(freopen(file, "r", stdin) == 0) {
fprintf(stderr, "open %s: %s\n", file, strerror(errno)); sysfatal("open %s: %r\n", file);
exit(1);
} }
if(!gcc) { if(!gcc) {
...@@ -719,5 +723,5 @@ main(int argc, char **argv) ...@@ -719,5 +723,5 @@ main(int argc, char **argv)
} }
process_file(); process_file();
return 0; exits(0);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment