Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that main works correctly #123

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions src/api/cst-dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ static void yf_dump_vardecl(struct yfcs_vardecl * node, FILE * out) {
static void yf_dump_funcdecl(struct yfcs_funcdecl * node, FILE * out) {
yf_print_line(out, "funcdecl");
indent();
yf_print_line(out, "name: %s", node->name.name);
if (node->extc)
yf_print_line(out, "[extc]");
yf_print_line(out, "params");
Expand Down
2 changes: 1 addition & 1 deletion src/api/sym.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct yfsn_param {
struct yfs_fn {

char * name;
struct yfs_type * rtype; /* "return type" */
struct yfs_type rtype; /* "return type" */
struct yf_list params; /* list of param */

};
Expand Down
2 changes: 1 addition & 1 deletion src/driver/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ static int yfc_validate_compile(
if (yf_ensure_entry_point(pdata)) {
return 1;
}
retval = yf_backend_generate_code(adata);
retval = yf_backend_generate_code(pdata, adata);
}

return retval;
Expand Down
9 changes: 5 additions & 4 deletions src/driver/compiler-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ static int create_output_file_name(
* Generate C code
*/
static int yf_gen_c(
struct yf_compilation_data * data,
struct yf_compile_analyse_job * fdata, struct yf_gen_info * info) {
return yfg_gen(fdata, info);
return yfg_gen(data, fdata, info);
}

/**
Expand Down Expand Up @@ -289,14 +290,14 @@ int yf_ensure_entry_point(
}

if (total_entries != 1)
YF_PRINT_ERROR("total 'main' functions found: %d\n", total_entries);
YF_PRINT_ERROR("total 'main' functions found: %d", total_entries);

return total_entries != 1;

}

int yf_backend_generate_code(
struct yf_compile_analyse_job * data
struct yf_compilation_data * cdata, struct yf_compile_analyse_job * data
) {
struct yf_gen_info ginfo = {
.yf_prefix = data->unit_info->file_prefix,
Expand All @@ -305,5 +306,5 @@ int yf_backend_generate_code(
create_formatted_prefix(
ginfo.yf_prefix, ginfo.gen_prefix, 256
);
return yf_gen_c(data, &ginfo);
return yf_gen_c(cdata, data, &ginfo);
}
2 changes: 1 addition & 1 deletion src/driver/compiler-backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int yf_backend_add_link_job(
);

int yf_backend_generate_code(
struct yf_compile_analyse_job *
struct yf_compilation_data*, struct yf_compile_analyse_job *
);

/**
Expand Down
17 changes: 14 additions & 3 deletions src/gen/gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <api/abstract-tree.h>
#include <api/operator.h>
#include <gen/symgen.h>
#include <gen/typegen.h>
#include <util/yfc-out.h>

Expand Down Expand Up @@ -102,7 +103,7 @@ static void yf_gen_funcdecl(
struct yf_ast_node * child;
int argct = 0;
char typebuf[256];
yfg_ctype(256, typebuf, node->name->fn.rtype);
yfg_ctype(256, typebuf, &node->name->fn.rtype);

/* Hacky fix -- but it should work. */
/* We need to check if the function is called "main" because the C compiler
Expand All @@ -119,12 +120,17 @@ static void yf_gen_funcdecl(
fprintf(
out, "%s /* %s */ %s$$%s",
typebuf,
node->name->fn.rtype->name,
node->name->fn.rtype.name,
i->gen_prefix,
node->name->fn.name
);
}
} else {
if (node->extc) {
YF_PRINT_ERROR("'main' function may not be declared extc");
/* TODO check this in semantic analysis */
/* return 1; */
}
fprintf(out, "int main");
}

Expand Down Expand Up @@ -261,7 +267,10 @@ static int create_all_parent_dirs(char * path) {
return 0;
}

int yfg_gen(struct yf_compile_analyse_job * data, struct yf_gen_info * info) {
int yfg_gen(
struct yf_compilation_data * cdata,
struct yf_compile_analyse_job * data, struct yf_gen_info * info
) {

create_all_parent_dirs(data->unit_info->output_file);

Expand All @@ -275,6 +284,8 @@ int yfg_gen(struct yf_compile_analyse_job * data, struct yf_gen_info * info) {
fprintf(out, "/* Generated by yfc. */\n\n");
fprintf(out, "#include <stdint.h>\n\n");

yfg_output_syms(cdata, data, out);

yf_gen_node(&data->ast_tree, out, info);

fclose(out);
Expand Down
5 changes: 4 additions & 1 deletion src/gen/gen.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <api/compilation-data.h>
#include <api/generation.h>

int yfg_gen(struct yf_compile_analyse_job * data, struct yf_gen_info * info);
int yfg_gen(
struct yf_compilation_data * cdata,
struct yf_compile_analyse_job * data, struct yf_gen_info * info
);

#endif /* GEN_GEN_H */
43 changes: 43 additions & 0 deletions src/gen/symgen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "symgen.h"

#include <util/yfc-out.h>

static int dump_symbol(
FILE * out,
struct yf_sym * sym
) {
/* TODO */
return 0;
}

int yfg_output_syms(
struct yf_compilation_data * cdata,
struct yf_compile_analyse_job * data,
FILE * out
) {

struct yfh_cursor cursor;
struct yfs_symtab * fsymtab;

/* We iterate through all symtabs and dump all decls into the file */
for (yfh_cursor_init(&cursor, &cdata->symtables); !yfh_cursor_next(&cursor); ) {

yfh_cursor_get(&cursor, NULL, (void **)&fsymtab);

struct yfh_cursor inner_cursor;
struct yf_sym * sym;
int fail;

for (yfh_cursor_init(&inner_cursor, &fsymtab->table); !yfh_cursor_next(&cursor); ) {
yfh_cursor_get(&inner_cursor, NULL, (void **)&sym);
if ((fail = dump_symbol(out, sym))) {
YF_PRINT_ERROR("gen: panic: error in dumping symbol");
return fail;
}
}

}

return 0;

}
19 changes: 19 additions & 0 deletions src/gen/symgen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* For projects -- dumping all symbols from other files as forward declarations
* in the current file.
*/

#ifndef GEN_SYMGEN_H
#define GEN_SYMGEN_H

#include <stdio.h> /* FILE */

#include <api/compilation-data.h>

int yfg_output_syms(
struct yf_compilation_data * cdata,
struct yf_compile_analyse_job * data,
FILE * out
);

#endif /* GEN_SYMGEN_H */
29 changes: 23 additions & 6 deletions src/semantics/symtab.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#include "symtab.h"

#include <string.h>

#include <api/sym.h>
#include <semantics/types.h>
#include <util/allocator.h>
#include <util/yfc-out.h>

static int yfs_add_var(struct yf_hashmap * symtab, struct yf_parse_node *);
static int yfs_add_fn(struct yf_hashmap * symtab, struct yf_parse_node *);
static int yfs_add_var(struct yf_compile_analyse_job *,
struct yf_hashmap * symtab, struct yf_parse_node *);
static int yfs_add_fn(struct yf_compile_analyse_job *,
struct yf_hashmap * symtab, struct yf_parse_node *);

int yfs_build_symtab(struct yf_compile_analyse_job * data) {

Expand All @@ -18,16 +23,19 @@ int yfs_build_symtab(struct yf_compile_analyse_job * data) {
YF_PRINT_ERROR("symtab: failed to allocate table");
return 3; /* Memory error */
}

yfh_init(&data->types.table);
yfv_add_builtin_types(data);

ret = 0;
YF_LIST_FOREACH(data->parse_tree.program.decls, node) {
switch (node->type) {
case YFCS_VARDECL:
if (yfs_add_var(&data->symtab.table, node))
if (yfs_add_var(data, &data->symtab.table, node))
ret = 1;
break;
case YFCS_FUNCDECL:
if (yfs_add_fn(&data->symtab.table, node))
if (yfs_add_fn(data, &data->symtab.table, node))
ret = 1;
break;
default:
Expand All @@ -40,7 +48,10 @@ int yfs_build_symtab(struct yf_compile_analyse_job * data) {

}

static int yfs_add_var(struct yf_hashmap * symtab, struct yf_parse_node * n) {
static int yfs_add_var(
struct yf_compile_analyse_job * data,
struct yf_hashmap * symtab, struct yf_parse_node * n
) {

struct yfcs_vardecl * v = &n->vardecl;
struct yf_sym * vsym, * dupl;
Expand Down Expand Up @@ -68,7 +79,10 @@ static int yfs_add_var(struct yf_hashmap * symtab, struct yf_parse_node * n) {

}

static int yfs_add_fn(struct yf_hashmap * symtab, struct yf_parse_node * f) {
static int yfs_add_fn(
struct yf_compile_analyse_job * udata, struct yf_hashmap * symtab,
struct yf_parse_node * f
) {

struct yfcs_funcdecl * fn = &f->funcdecl;

Expand Down Expand Up @@ -102,6 +116,9 @@ static int yfs_add_fn(struct yf_hashmap * symtab, struct yf_parse_node * f) {

}

/* TODO -- actually look up custom types once those exist. */
fsym->fn.rtype = *yfv_get_type_s(udata, fn->ret.databuf);

yfh_set(symtab, fsym->fn.name, fsym);

return 0;
Expand Down
42 changes: 41 additions & 1 deletion src/semantics/types.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
#include "types.h"

void yft_add_type(
struct yf_compile_analyse_job * udata,
char * name, int size, enum yfpt_format fmt
) {

struct yfs_type * type = yf_malloc(sizeof (struct yfs_type));
type->primitive.size = size;
type->kind = YFS_T_PRIMITIVE;
type->primitive.type = fmt;
type->name = name;
yfv_add_type(udata, type);

}

void yfv_add_builtin_types(struct yf_compile_analyse_job * udata) {

/* All types are signed for now - unsigned types are not yet supported. */

/* "standard" types. */
yft_add_type(udata, "char", 8, YFS_F_INT );
yft_add_type(udata, "short", 16, YFS_F_INT );
yft_add_type(udata, "int", 32, YFS_F_INT );
yft_add_type(udata, "long", 64, YFS_F_INT );
yft_add_type(udata, "void", 0, YFS_F_NONE );
yft_add_type(udata, "float", 32, YFS_F_FLOAT);
yft_add_type(udata, "double", 64, YFS_F_FLOAT);

/* Convenience types. */
yft_add_type(udata, "i16", 16, YFS_F_INT );
yft_add_type(udata, "i32", 32, YFS_F_INT );
yft_add_type(udata, "i64", 64, YFS_F_INT );
yft_add_type(udata, "f16", 16, YFS_F_FLOAT);
yft_add_type(udata, "f32", 32, YFS_F_FLOAT);
yft_add_type(udata, "f64", 64, YFS_F_FLOAT);

/* We're considering bool to be one bit for conversion purposes. */
yft_add_type(udata, "bool", 1, YFS_F_INT );

}

enum yfs_conversion_allowedness yfs_is_safe_conversion(
struct yfs_type * from, struct yfs_type * to
) {
Expand Down Expand Up @@ -93,7 +133,7 @@ struct yfs_type * yfse_get_expr_type(
}
break;
case YFA_E_FUNCCALL:
return expr->as.call.name->fn.rtype;
return &expr->as.call.name->fn.rtype;
}

}
Expand Down
7 changes: 7 additions & 0 deletions src/semantics/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
#include <api/loc.h>
#include <semantics/validate/validate-internal.h>

void yft_add_type(
struct yf_compile_analyse_job * udata,
char * name, int size, enum yfpt_format fmt
);

void yfv_add_builtin_types(struct yf_compile_analyse_job * udata);

enum yfs_conversion_allowedness {
YFS_CONVERSION_OK,
YFS_CONVERSION_LOSSY, /* like i64 -> bool */
Expand Down
6 changes: 5 additions & 1 deletion src/semantics/validate/validate-func.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ int validate_funcdecl(
return 2;
}

if ((a->name->fn.rtype = yfv_get_type_t(
struct yfs_type * ty;

if ((ty = yfv_get_type_t(
validator->udata, c->ret
)) == NULL) {
YF_PRINT_ERROR(
Expand All @@ -36,6 +38,8 @@ int validate_funcdecl(
return 2;
}

a->name->fn.rtype = *ty;

/* Now, validate the argument list. */
/* Also, open a new scope for arguments. */
enter_scope(validator, &a->param_scope);
Expand Down
Loading