diff options
Diffstat (limited to 'src/pkg/runtime/goc2c.c')
-rw-r--r-- | src/pkg/runtime/goc2c.c | 96 |
1 files changed, 50 insertions, 46 deletions
diff --git a/src/pkg/runtime/goc2c.c b/src/pkg/runtime/goc2c.c index 826ceff3a..61236e226 100644 --- a/src/pkg/runtime/goc2c.c +++ b/src/pkg/runtime/goc2c.c @@ -2,26 +2,27 @@ // Use of this source code is governed by a BSD-style // 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 - {# line} - func NAME([NAME TYPE { , NAME TYPE }]) [(NAME TYPE { , NAME TYPE })] \{ - C code with proper brace nesting - \} + package PACKAGENAME + {# line} + func NAME([NAME TYPE { , NAME TYPE }]) [(NAME TYPE { , NAME TYPE })] \{ + 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 <ctype.h> +#include <u.h> #include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <errno.h> +#include <libc.h> /* Whether we're emitting for gcc */ static int gcc; @@ -88,16 +89,14 @@ int structround = 4; static void bad_eof(void) { - fprintf(stderr, "%s:%u: unexpected EOF\n", file, lineno); - exit(1); + sysfatal("%s:%ud: unexpected EOF\n", file, lineno); } /* Out of memory. */ static void bad_mem(void) { - fprintf(stderr, "%s:%u: out of memory\n", file, lineno); - exit(1); + sysfatal("%s:%ud: out of memory\n", file, lineno); } /* Allocate memory without fail. */ @@ -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 * read_token(void) { @@ -259,11 +260,11 @@ read_package(void) char *token; token = read_token_no_eof(); + if (token == nil) + sysfatal("%s:%ud: no token\n", file, lineno); if (strcmp(token, "package") != 0) { - fprintf(stderr, - "%s:%u: expected \"package\", got \"%s\"\n", + sysfatal("%s:%ud: expected \"package\", got \"%s\"\n", file, lineno, token); - exit(1); } return read_token_no_eof(); } @@ -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 * read_type(void) { @@ -333,13 +336,14 @@ type_size(char *p) for(i=0; type_table[i].name; i++) if(strcmp(type_table[i].name, p) == 0) return type_table[i].size; - fprintf(stderr, "%s:%u: unknown type %s\n", file, lineno, p); - exit(1); + sysfatal("%s:%ud: unknown type %s\n", file, lineno, p); 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 * read_params(int *poffset) { @@ -375,17 +379,18 @@ read_params(int *poffset) } } if (strcmp(token, ")") != 0) { - fprintf(stderr, "%s:%u: expected '('\n", + sysfatal("%s:%ud: expected '('\n", file, lineno); - exit(1); } if (poffset != NULL) *poffset = offset; 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 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 token = read_token(); if (token == NULL || strcmp(token, "(") != 0) { - fprintf(stderr, "%s:%u: expected \"(\"\n", + sysfatal("%s:%ud: expected \"(\"\n", file, lineno); - exit(1); } *params = read_params(paramwid); @@ -430,9 +434,8 @@ read_func_header(char **name, struct params **params, int *paramwid, struct para token = read_token(); } if (token == NULL || strcmp(token, "{") != 0) { - fprintf(stderr, "%s:%u: expected \"{\"\n", + sysfatal("%s:%ud: expected \"{\"\n", file, lineno); - exit(1); } return 1; } @@ -581,8 +584,10 @@ write_func_trailer(char *package, char *name, 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 copy_body(void) { @@ -669,15 +674,15 @@ process_file(void) static void usage(void) { - fprintf(stderr, "Usage: goc2c [--6g | --gc] [file]\n"); - exit(1); + sysfatal("Usage: goc2c [--6g | --gc] [file]\n"); } -int +void main(int argc, char **argv) { char *goarch; + argv0 = argv[0]; while(argc > 1 && argv[1][0] == '-') { if(strcmp(argv[1], "-") == 0) break; @@ -694,7 +699,7 @@ main(int argc, char **argv) if(argc <= 1 || strcmp(argv[1], "-") == 0) { file = "<stdin>"; process_file(); - return 0; + exits(0); } if(argc > 2) @@ -702,8 +707,7 @@ main(int argc, char **argv) file = argv[1]; if(freopen(file, "r", stdin) == 0) { - fprintf(stderr, "open %s: %s\n", file, strerror(errno)); - exit(1); + sysfatal("open %s: %r\n", file); } if(!gcc) { @@ -719,5 +723,5 @@ main(int argc, char **argv) } process_file(); - return 0; + exits(0); } |