~lbnz/xr0

02d4acd404c5f8ce89cde623683158ca97efa545 — Amisi Kiarie 2 months ago 2fc4cd1 path/init
end-to-end
A 0.c => 0.c +37 -0
@@ 0,0 1,37 @@
#include <stdio.h>

int
main()
{
	int x = 0;
	int y = 0;
	int z = 0;
	printf("x: %d, y: %d, z: %d\n", x, y, z);

}

struct list { int x; struct list * next; };

void
foo()
{
	struct  { char c; char d; } cd = { 0, 0 };
	struct point { int x; int y; int z; } p = (struct point) { 0, 0, 0 };
	struct point p2 = (struct point) { 0, 0, 0 };
	struct man { char * name; int age; } man = (struct man) { NULL, 0 };
	struct man john = (struct man) { NULL, 0 };
	struct list l1 = (struct list) { 0, NULL };
	int i = 0;
	abc: if (1) {
		struct point p3 = (struct point) { 0, 0, 0 };
		struct man james = (struct man) { NULL, 0 };
	} else {
		struct list l2 = (struct list) { 0, NULL };
	};

	for (i = 0; i<20; i++) {
		struct point p4 = (struct point) { 0, 0, 0 };
		struct man roger = (struct man) { NULL, 0 };
	}
}


M include/ast.h => include/ast.h +3 -0
@@ 633,6 633,9 @@ ast_externdecl_as_function(struct ast_externdecl *);
struct ast_externdecl *
ast_decl_create(char *name, struct ast_type *);

struct ast_externdecl *
ast_include_create(char *filename);

void
ast_externdecl_install(struct ast_externdecl *decl, struct externals *ext);


M src/ast/ast.c => src/ast/ast.c +0 -1
@@ 50,7 50,6 @@ ast_initprint(struct ast *node, struct externals *ext)
	for (int i = 0; i < node->n; i++) {
		char *s = ast_externdecl_initprint(node->decl[i], ext);
		strbuilder_printf(b, "%s\n", s);
		printf("%s\n", s);
		free(s);
	}
	return strbuilder_build(b);

M src/ast/externdecl.c => src/ast/externdecl.c +35 -6
@@ 12,6 12,7 @@ struct ast_externdecl {
		EXTERN_VARIABLE,
		EXTERN_TYPEDEF,
		EXTERN_STRUCT,
		EXTERN_INCLUDE,
	} kind;
	union {
		struct ast_function *function;


@@ 21,6 22,7 @@ struct ast_externdecl {
			struct ast_type *type;
		} _typedef;
		struct ast_type *_struct;
		char *include;
	};
};



@@ 65,6 67,15 @@ ast_decl_create(char *name, struct ast_type *t)
	return decl;
}

struct ast_externdecl *
ast_include_create(char *filename)
{
	struct ast_externdecl *decl = malloc(sizeof(struct ast_externdecl));
	decl->kind = EXTERN_INCLUDE;
	decl->include = filename;
	return decl;
}

void
ast_externdecl_install(struct ast_externdecl *decl, struct externals *ext)
{


@@ 88,6 99,8 @@ ast_externdecl_install(struct ast_externdecl *decl, struct externals *ext)
	case EXTERN_STRUCT:
		externals_declarestruct(ext, decl->_struct);
		break;
	case EXTERN_INCLUDE:
		break;
	default:
		assert(false);
	}


@@ 110,6 123,9 @@ ast_externdecl_destroy(struct ast_externdecl *decl)
	case EXTERN_STRUCT:
		ast_type_destroy(decl->_struct);
		break;
	case EXTERN_INCLUDE:
		free(decl->include);
		break;
	default:
		assert(false);
	}


@@ 117,10 133,13 @@ ast_externdecl_destroy(struct ast_externdecl *decl)
}

static char *
ast_typedef_initprint(struct ast_externdecl *, struct externals *);
typedef_initprint(struct ast_externdecl *, struct externals *);

static char *
ast_struct_initprint(struct ast_externdecl *, struct externals *);
struct_initprint(struct ast_externdecl *, struct externals *);

static char *
include_initprint(char *);

char *
ast_externdecl_initprint(struct ast_externdecl *decl, struct externals *ext)


@@ 132,16 151,18 @@ ast_externdecl_initprint(struct ast_externdecl *decl, struct externals *ext)
		/* TODO: globals */
		assert(false);
	case EXTERN_TYPEDEF:
		return ast_typedef_initprint(decl, ext);
		return typedef_initprint(decl, ext);
	case EXTERN_STRUCT:
		return ast_struct_initprint(decl, ext);
		return struct_initprint(decl, ext);
	case EXTERN_INCLUDE:
		return include_initprint(decl->include);
	default:
		assert(false);
	}
}

static char *
ast_typedef_initprint(struct ast_externdecl *decl, struct externals *ext)
typedef_initprint(struct ast_externdecl *decl, struct externals *ext)
{
	assert(decl->kind == EXTERN_TYPEDEF);
	struct strbuilder *b = strbuilder_create();


@@ 152,7 173,7 @@ ast_typedef_initprint(struct ast_externdecl *decl, struct externals *ext)
}

static char *
ast_struct_initprint(struct ast_externdecl *decl, struct externals *ext)
struct_initprint(struct ast_externdecl *decl, struct externals *ext)
{
	struct strbuilder *b = strbuilder_create();
	char *s = ast_type_str(decl->_struct);


@@ 160,3 181,11 @@ ast_struct_initprint(struct ast_externdecl *decl, struct externals *ext)
	free(s);
	return strbuilder_build(b);
}

static char *
include_initprint(char *s)
{
	struct strbuilder *b = strbuilder_create();
	strbuilder_printf(b, "#include %s\n", s);
	return strbuilder_build(b);
}

M src/ast/gram.y => src/ast/gram.y +7 -1
@@ 90,6 90,7 @@ variable_array_create(struct ast_variable *v)
%token AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
%token SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
%token XOR_ASSIGN OR_ASSIGN TYPE_NAME
%token PP_INCLUDE PP_LITERAL

%token TYPEDEF EXTERN STATIC AUTO REGISTER
%token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID


@@ 170,7 171,7 @@ variable_array_create(struct ast_variable *v)

%type <expr_array> argument_expression_list

%type <externdecl> external_declaration
%type <externdecl> external_declaration include
%type <function> function_definition
%type <function_declarator> function_declarator
%type <integer> pointer


@@ 796,6 797,11 @@ translation_unit
external_declaration
	: function_definition	{ $$ = ast_functiondecl_create($1); }
	| declaration		{ $$ = ast_decl_create($1.name, $1.t); }
	| include
	;

include
	: PP_INCLUDE PP_LITERAL	{ $$ = ast_include_create(dynamic_str(yytext)); }
	;

block_statement

M src/ast/lex.l => src/ast/lex.l +8 -2
@@ 28,7 28,7 @@ check_type();
%}

%%
"#"			{ preproc(); }
"#include"		{ preproc(); return(PP_INCLUDE); }
"/*"			{ comment(); }
".malloc"		{ count(); return(TK_ALLOC); }
".free"			{ count(); return(TK_DEALLOC); }


@@ 83,6 83,7 @@ L?'(\\.|[^\\'])+'	{ count(); return(CHAR_LITERAL); }
{D}+"."{D}*({E})?{FS}?	{ count(); return(CONSTANT); }

L?\"(\\.|[^\\"])*\"	{ count(); return(STRING_LITERAL); }
\<(\\.|[^\\>])*\>	{ count(); return(PP_LITERAL); }

"..."			{ count(); return(ELLIPSIS); }
">>="			{ count(); return(RIGHT_ASSIGN); }


@@ 260,7 261,12 @@ preproc()
	if (marker.filename) {
		free(marker.filename);
	}
	marker = process_linemarker();
	marker = (struct lexememarker) {
		.linenum = 1,
		.column = 0,
		.filename = dynamic_str("file"),
		.flags = LM_FLAG_RESUME_FILE,
	};
}

void

M src/main.c => src/main.c +9 -3
@@ 18,6 18,7 @@
/* XXX */
#define INCLUDE_ENVVAR		"XR0_INCLUDES"
#define OUTPUT_PATH		"0.c"
#define INIT_OUTPUT_PATH	"a.c"
#define PREPROC_CMD_TEMPLATE	"cc %s -nostdinc -E -xc %s"
#define PREPROC_CMD_BASE_LEN 	(strlen(PREPROC_CMD_TEMPLATE) - 4)



@@ 484,13 485,12 @@ skipvblock(FILE *f)
	}
}


static int
init(struct config *c)
{
	/* preprocess */
	extern FILE *yyin;
	yyin = preprocess(c->infile, c->includedirs);
	yyin = fopen(c->infile, "rb");

	/* lex and parse */
	lex_begin();


@@ 502,6 502,12 @@ init(struct config *c)
	pass0(root, ext);

	char *s = ast_initprint(root, ext);
	assert(false && s);

	FILE *out = fopen(c->outfile, "w");
	fprintf(out, "%s", s);
	fclose(out);

	free(s);

	return 0;
}

M tests/99-program/200-init/init.c => tests/99-program/200-init/init.c +2 -2
@@ 3,8 3,8 @@
int
main()
{
	int x;
	printf("x: %d\n", x);
	int x; int y; int z;
	printf("x: %d, y: %d, z: %d\n", x, y, z);
}

struct list {