~pixelherodev/c3lib

3a1f0d6788f8a8ae8864404b8eea0bf1cdc57362 — Noam Preil a month ago 4b180f5
fix function names
2 files changed, 15 insertions(+), 63 deletions(-)

M codegen/c.c
M sema/expr.c
M codegen/c.c => codegen/c.c +6 -5
@@ 149,7 149,7 @@ cg_call(uint32_t index)
			return 0;
		ctx.counter += 1;
	}
	buf = h_c3str(ctx.c3, c3nodechild(ctx.c3, c3nodechild(ctx.c3, node, 2), 0));
	buf = h_c3str(ctx.c3, stbds_hmget(ctx.c3.analysis.names, index));
	fprintf(ctx.out, "%s(", buf);
	if(!cg_params(index))
		return 0;


@@ 435,18 435,20 @@ static int
cg_fn(uint32_t index)
{
	uint32_t i;
	uint32_t fntype = c3nodechild(ctx.c3, c3nodechild(ctx.c3, index, 1), 0);
	uint32_t fntype = stbds_hmget(ctx.c3.analysis.types, index);
	uint32_t rettype = c3nodechild(ctx.c3, fntype, 0);
	uint32_t params = c3nodechild(ctx.c3, fntype, 1);
	uint16_t param_count = c3nodekids(ctx.c3, params);
	int anykids = 0;
	uint32_t param;
	char *buf;
	int anykids = 0;
	if(rettype == -1)
		FATAL("oops", 0);
	ctx.blocks = NULL;
	ctx.counter = 0;
	ctx.regalloc.node = NULL;
	ctx.regalloc.tmp = NULL;
	ctx.current.name = h_c3str(ctx.c3, c3nodechild(ctx.c3, c3nodechild(ctx.c3, c3nodechild(ctx.c3, index, 1), 2), 0));
	ctx.current.name = h_c3str(ctx.c3, stbds_hmget(ctx.c3.analysis.names, index));
	if(!cg_type(rettype))
		return 0;
	fprintf(ctx.out, "\n%s(", ctx.current.name);


@@ 495,7 497,6 @@ codegen_c(c3ctx c3, FILE *file)
	fprintf(ctx.out, "#include <stdio.h>\n#include <stdbool.h>\n#define zyg_noreturn void\n\n");
	for(i = 0; i < nodes; i += 1){
		index = c3.analysis.fns[i];
		index = c3nodechild(ctx.c3, index, 0);
		tag = c3nodetag(ctx.c3, index);
		switch(tag){
		case C3_VALUE_FN:

M sema/expr.c => sema/expr.c +9 -58
@@ 23,31 23,12 @@ static struct{
		// e.g. inlining can generate extra blocks, which also get IDs.
		uint32_t id;
	}block;
	// Function node references - given as indices containing indices of C3_VALUE_FNS - which need to be updated to point to generated C3_IR_FNs
	// In addition to everything in here, all entries in the call graph need to be updated
	uint32_t *fn_refs;
	struct{
		uint32_t *values;
		uint32_t *irs;
	} fn_map;
	c3ctx *c3;
	uint32_t next_block_id;
	// during normal analysis, this is set to zero. When inlining a function, this is set to 1. l_block, when inline, sets this to the return value.
	// TODO: add IR for breaking with a value, and use that instead of this hack.
	uint32_t cur_fn;
	uint32_t *inlined_args;
} ctx;

static uint32_t
s_map(uint32_t index)
{
	uint32_t i;
	for(i = 0; i < stb_sb_count(ctx.fn_map.values); i += 1)
		if(ctx.fn_map.values[i] == index)
			return ctx.fn_map.irs[i];
	return -1;
}

static uint32_t l_block(uint32_t index, int type);
static uint32_t s_expr(uint32_t index, uint32_t **list);
static uint32_t s_decl(uint32_t index);


@@ 156,6 137,8 @@ s_call(uint32_t index)
		ERROR("ICE: expected VALUE_FN for EXPR_CALL, found '%s'", c3tagstr(tag));
		return -1;
	}
	if(!stb_sb_push_u32(&kids, stbds_hmget(ctx.c3->analysis.names, index)))
		OOM();
	param_list = c3nodechild(*ctx.c3, index, 0);
	conv = c3nodetag(*ctx.c3, c3nodechild(*ctx.c3, param_list, 2));
	param_list = c3nodechild(*ctx.c3, param_list, 1);


@@ 185,8 168,6 @@ s_call(uint32_t index)
			OOM();
	}
	tmp = c3appendarr(ctx.c3, C3_IR_CALL, stb_sb_count(kids), kids);
	if(tmp == -1 || !stb_sb_push_u32(&ctx.fn_refs, tmp + 1))
		OOM();
	return tmp;
}



@@ 636,20 617,20 @@ s_decl(uint32_t index)
uint32_t
l_fn(uint32_t index)
{
	uint32_t tmp;
	uint32_t tmp, tmp2;
	ctx.next_block_id = 0;
	ctx.cur_fn = index;
	if(!stb_sb_push_u32(&ctx.fn_map.values, index))
		OOM();
	tmp = l_block(c3nodechild(*ctx.c3, index, 1), 1);
	if(tmp == -1){
		ERROR("Failed to lower function block", 0);
		return -1;
	}
	index = c3append(ctx.c3, C3_IR_FN, 2, tmp, index);
	if(!stb_sb_push_u32(&ctx.fn_map.irs, index))
		OOM();
	return index;
	tmp = c3append(ctx.c3, C3_IR_FN, 1, tmp);
	tmp2 = stbds_hmget(ctx.c3->analysis.names, index);
	stbds_hmput(ctx.c3->analysis.names, tmp, tmp2);
	tmp2 = stbds_hmget(ctx.c3->analysis.types, index);
	stbds_hmput(ctx.c3->analysis.types, tmp, tmp2);
	return tmp;
}

int


@@ 687,9 668,6 @@ expr_gen_test(void)
static void
ctx_init(c3ctx *c3)
{
	ctx.fn_refs = NULL;
	ctx.fn_map.irs = NULL;
	ctx.fn_map.values = NULL;
	ctx.c3 = c3;
	ctx.inlined_args = NULL;
}


@@ 704,32 682,5 @@ expr_init(c3ctx *c3)
int
expr_finish(void)
{
	uint32_t i;
	uint32_t index, rindex;
	uint16_t kids;
	uint32_t *fns = ctx.c3->analysis.fns;
	kids = stb_sb_count(fns);
	for(i = 0; i < kids; i += 1){
		index = fns[i];
		rindex = s_map(c3nodechild(*ctx.c3, index, 0));
		if(rindex == -1){
			ERROR("ICE: unable to map function from CFG to IR; index %u: %u", index, c3nodechild(*ctx.c3, index, 0));
			c3nodedump(*ctx.c3, rindex, 0, 1);
			return 0;
		}
		ctx.c3->ast.nodes[index + 1].node_index = rindex;
	}
	for(i = 0; i < stb_sb_count(ctx.fn_refs); i += 1){
		rindex = ctx.fn_refs[i];
		index = ctx.c3->ast.nodes[rindex].node_index;
		ctx.c3->ast.nodes[index].node_index = s_map(index);
		if(ctx.c3->ast.nodes[index].node_index == -1){
			ERROR("ICE: unable to map function from child index %d to IR", ctx.c3->ast.nodes[index]);
			return 0;
		}
	}
	stb_sb_free(ctx.fn_refs);
	stb_sb_free(ctx.fn_map.irs);
	stb_sb_free(ctx.fn_map.values);
	return 1;
}