Fixing up structs
This commit is contained in:
parent
a4c3faa525
commit
50fedc8f44
2
Makefile
2
Makefile
@ -43,7 +43,7 @@ test: nextlang
|
||||
for f in tests/*; do echo -e "\x1b[1;4m$$f\x1b[m"; VERBOSE=0 ./nextlang "$$f" || break; done
|
||||
|
||||
clean:
|
||||
rm -f nextlang *.o builtins/*.o libnext.so
|
||||
rm -f nextlang *.o SipHash/halfsiphash.o builtins/*.o libnext.so
|
||||
|
||||
%.1: %.1.md
|
||||
pandoc --lua-filter=.pandoc/bold-code.lua -s $< -t man -o $@
|
||||
|
@ -457,9 +457,9 @@ CORD compile(env_t *env, ast_t *ast)
|
||||
CORD code = "{\n";
|
||||
env = fresh_scope(env);
|
||||
for (ast_list_t *stmt = stmts; stmt; stmt = stmt->next) {
|
||||
bind_statement(env, stmt->ast);
|
||||
code = CORD_cat(code, compile_statement(env, stmt->ast));
|
||||
code = CORD_cat(code, "\n");
|
||||
bind_statement(env, stmt->ast);
|
||||
}
|
||||
return CORD_cat(code, "}");
|
||||
}
|
||||
@ -546,7 +546,7 @@ CORD compile(env_t *env, ast_t *ast)
|
||||
CORD name = compile(env, fndef->name);
|
||||
CORD_appendf(&env->code->staticdefs, "static %r %r_(", fndef->ret_type ? compile_type_ast(fndef->ret_type) : "void", name);
|
||||
for (arg_ast_t *arg = fndef->args; arg; arg = arg->next) {
|
||||
type_t *arg_type = arg->type ? parse_type_ast(env, arg->type) : get_type(env, arg->value);
|
||||
type_t *arg_type = get_arg_ast_type(env, arg);
|
||||
CORD_appendf(&env->code->staticdefs, "%r %s", compile_type(arg_type), arg->name);
|
||||
if (arg->next) env->code->staticdefs = CORD_cat(env->code->staticdefs, ", ");
|
||||
}
|
||||
@ -558,7 +558,7 @@ CORD compile(env_t *env, ast_t *ast)
|
||||
env_t *body_scope = fresh_scope(env);
|
||||
body_scope->locals->fallback = env->globals;
|
||||
for (arg_ast_t *arg = fndef->args; arg; arg = arg->next) {
|
||||
type_t *arg_type = arg->type ? parse_type_ast(env, arg->type) : get_type(env, arg->value);
|
||||
type_t *arg_type = get_arg_ast_type(env, arg);
|
||||
CORD arg_typecode = compile_type(arg_type);
|
||||
CORD_appendf(&env->code->funcs, "%r %s", arg_typecode, arg->name);
|
||||
if (arg->next) env->code->funcs = CORD_cat(env->code->funcs, ", ");
|
||||
@ -994,10 +994,10 @@ module_code_t compile_file(ast_t *ast)
|
||||
CORD_appendf(&env->code->imports, "#include \"nextlang.h\"\n");
|
||||
|
||||
for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next) {
|
||||
bind_statement(env, stmt->ast);
|
||||
CORD code = compile_statement(env, stmt->ast);
|
||||
if (code)
|
||||
CORD_appendf(&env->code->main, "%r\n", code);
|
||||
bind_statement(env, stmt->ast);
|
||||
}
|
||||
|
||||
return (module_code_t){
|
||||
|
@ -12,6 +12,7 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "SipHash/halfsiphash.h"
|
||||
#include "builtins/array.h"
|
||||
#include "builtins/bool.h"
|
||||
#include "builtins/color.h"
|
||||
|
112
structs.c
112
structs.c
@ -11,6 +11,30 @@
|
||||
#include "typecheck.h"
|
||||
#include "util.h"
|
||||
|
||||
static bool is_plain_data(env_t *env, type_t *t)
|
||||
{
|
||||
switch (t->tag) {
|
||||
case BoolType: case IntType: case NumType: case PointerType: case FunctionType:
|
||||
return true;
|
||||
case StructType: {
|
||||
for (arg_t *arg = Match(t, StructType)->fields; arg; arg = arg->next) {
|
||||
if (!is_plain_data(env, get_arg_type(env, arg)))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case EnumType: {
|
||||
for (tag_t *tag = Match(t, EnumType)->tags; tag; tag = tag->next) {
|
||||
if (!is_plain_data(env, tag->type))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static CORD compile_str_method(env_t *env, ast_t *ast)
|
||||
{
|
||||
auto def = Match(ast, StructDef);
|
||||
@ -22,8 +46,8 @@ static CORD compile_str_method(env_t *env, ast_t *ast)
|
||||
} else {
|
||||
CORD_appendf(&str_func, "\treturn CORD_all(use_color ? \"\\x1b[0;1m%s\\x1b[m(\" : \"%s(\"", def->name, def->name);
|
||||
for (arg_ast_t *field = def->fields; field; field = field->next) {
|
||||
type_t *field_t = parse_type_ast(env, field->type);
|
||||
CORD field_str = expr_as_string(env, CORD_cat("obj->", field->name), field_t, "use_color");
|
||||
type_t *field_type = get_arg_ast_type(env, field);
|
||||
CORD field_str = expr_as_string(env, CORD_cat("obj->", field->name), field_type, "use_color");
|
||||
CORD_appendf(&str_func, ", \"%s=\", %r", field->name, field_str);
|
||||
if (field->next) CORD_appendf(&str_func, ", \", \"");
|
||||
}
|
||||
@ -39,15 +63,65 @@ static CORD compile_compare_method(env_t *env, ast_t *ast)
|
||||
"_t *y, const TypeInfo *info) {\n"
|
||||
"int diff;\n");
|
||||
for (arg_ast_t *field = def->fields; field; field = field->next) {
|
||||
type_t *field_type = parse_type_ast(env, field->type);
|
||||
cmp_func = CORD_all(cmp_func, "diff = generic_compare(&x->", field->name, ", &y->", field->name, ", ",
|
||||
compile_type_info(env, field_type), ");\n"
|
||||
"if (diff != 0) return diff;\n");
|
||||
type_t *field_type = get_arg_ast_type(env, field);
|
||||
switch (field_type->tag) {
|
||||
case BoolType: case IntType: case NumType: case PointerType: case FunctionType:
|
||||
cmp_func = CORD_all(cmp_func, "diff = (x->", field->name, " > y->", field->name, ") - (x->", field->name, " < y->", field->name, ");");
|
||||
break;
|
||||
case StringType:
|
||||
cmp_func = CORD_all(cmp_func, "diff = CORD_cmp(x->", field->name, ", y->", field->name, ");");
|
||||
break;
|
||||
default:
|
||||
cmp_func = CORD_all(cmp_func, "diff = generic_compare(&x->", field->name, ", &y->", field->name, ", ",
|
||||
compile_type_info(env, field_type), ");\n");
|
||||
break;
|
||||
}
|
||||
cmp_func = CORD_all(cmp_func, "if (diff != 0) return diff;\n");
|
||||
}
|
||||
cmp_func = CORD_all(cmp_func, "return 0;\n}\n");
|
||||
return cmp_func;
|
||||
}
|
||||
|
||||
static CORD compile_equals_method(env_t *env, ast_t *ast)
|
||||
{
|
||||
auto def = Match(ast, StructDef);
|
||||
CORD eq_func = CORD_all("static bool ", def->name, "__equal(const ", def->name, "_t *x, const ", def->name,
|
||||
"_t *y, const TypeInfo *info) {\n");
|
||||
for (arg_ast_t *field = def->fields; field; field = field->next) {
|
||||
type_t *field_type = parse_type_ast(env, field->type);
|
||||
switch (field_type->tag) {
|
||||
case BoolType: case IntType: case NumType: case PointerType: case FunctionType:
|
||||
eq_func = CORD_all(eq_func, "if (x->", field->name, " != y->", field->name, ") return no;\n");
|
||||
break;
|
||||
case StringType:
|
||||
eq_func = CORD_all(eq_func, "if (CORD_cmp(x->", field->name, ", y->", field->name, ") != 0) return no;\n");
|
||||
break;
|
||||
default:
|
||||
eq_func = CORD_all(eq_func, "if (!generic_equal(&x->", field->name, ", &y->", field->name, ", ",
|
||||
compile_type_info(env, field_type), ")) return no;\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
eq_func = CORD_all(eq_func, "return yes;\n}\n");
|
||||
return eq_func;
|
||||
}
|
||||
|
||||
static CORD compile_hash_method(env_t *env, ast_t *ast)
|
||||
{
|
||||
auto def = Match(ast, StructDef);
|
||||
CORD hash_func = CORD_all("static uint32_t ", def->name, "__hash(const ", def->name, "_t *obj, const TypeInfo *info) {\n"
|
||||
"uint32_t field_hashes[] = {");
|
||||
for (arg_ast_t *field = def->fields; field; field = field->next) {
|
||||
type_t *field_type = get_arg_ast_type(env, field);
|
||||
hash_func = CORD_all(hash_func, "\ngeneric_hash(&obj->", field->name, ", ", compile_type_info(env, field_type), "),");
|
||||
}
|
||||
hash_func = CORD_all(hash_func, "};\n"
|
||||
"uint32_t hash;\n"
|
||||
"halfsiphash(&field_hashes, sizeof(field_hashes), SSS_HASH_VECTOR, (uint8_t*)&hash, sizeof(hash));\n"
|
||||
"return hash;\n}\n");
|
||||
return hash_func;
|
||||
}
|
||||
|
||||
void compile_struct_def(env_t *env, ast_t *ast)
|
||||
{
|
||||
auto def = Match(ast, StructDef);
|
||||
@ -66,13 +140,25 @@ void compile_struct_def(env_t *env, ast_t *ast)
|
||||
CORD_appendf(&env->code->typedefs, "typedef struct { TypeInfo type; } %s_namespace_t;\n", def->name);
|
||||
CORD_appendf(&env->code->typedefs, "extern %s_namespace_t %s;\n", def->name, def->name);
|
||||
|
||||
env->code->funcs = CORD_all(
|
||||
env->code->funcs, compile_compare_method(env, ast), compile_str_method(env, ast));
|
||||
env->code->typeinfos = CORD_all(
|
||||
env->code->typeinfos,
|
||||
"public ", def->name, "_namespace_t ", def->name, " = {{.tag=CustomInfo, .CustomInfo={"
|
||||
".as_str=(void*)", def->name, "__as_str, "
|
||||
".compare=(void*)", def->name, "__compare}}};\n");
|
||||
type_t *t = Table_str_get(env->types, def->name);
|
||||
CORD typeinfo = CORD_asprintf("public %s_namespace_t %s = {{%zu, %zu, {.tag=CustomInfo, .CustomInfo={",
|
||||
def->name, def->name, type_size(t), type_align(t));
|
||||
|
||||
typeinfo = CORD_all(typeinfo, ".as_str=(void*)", def->name, "__as_str, ");
|
||||
env->code->funcs = CORD_all(env->code->funcs, compile_str_method(env, ast));
|
||||
if (!t || !is_plain_data(env, t)) {
|
||||
env->code->funcs = CORD_all(
|
||||
env->code->funcs, compile_equals_method(env, ast), compile_compare_method(env, ast),
|
||||
compile_hash_method(env, ast));
|
||||
typeinfo = CORD_all(
|
||||
typeinfo,
|
||||
".as_str=(void*)", def->name, "__as_str, "
|
||||
".equal=(void*)", def->name, "__equal, "
|
||||
".hash=(void*)", def->name, "__hash, "
|
||||
".compare=(void*)", def->name, "__compare");
|
||||
}
|
||||
typeinfo = CORD_cat(typeinfo, "}}}};\n");
|
||||
env->code->typeinfos = CORD_all(env->code->typeinfos, typeinfo);
|
||||
}
|
||||
|
||||
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
|
||||
|
48
typecheck.c
48
typecheck.c
@ -685,39 +685,6 @@ type_t *get_namespace_type(env_t *env, ast_t *namespace_ast, type_t *type)
|
||||
return Type(StructType, .fields=ns_fields);
|
||||
}
|
||||
|
||||
// typedef struct {
|
||||
// file_t *file;
|
||||
// env_t *env;
|
||||
// ast_t *ast;
|
||||
// } parsed_file_info_t;
|
||||
|
||||
// static parsed_file_info_t *get_file_info(env_t *env, const char *path)
|
||||
// {
|
||||
// static table_t cache = {0};
|
||||
|
||||
// struct stat file_stat;
|
||||
// const char *sss_path = strlen(path) > 4 && streq(path + strlen(path) - 4, ".sss") ? path : heap_strf("%s.sss", path);
|
||||
// if (stat(sss_path, &file_stat) == -1)
|
||||
// compiler_err(NULL, NULL, NULL, "I can't find the file %s", sss_path);
|
||||
|
||||
// parsed_file_info_t *file_info = Table_str_get(&cache, path);
|
||||
// if (file_info) return file_info;
|
||||
|
||||
// file_t *f = load_file(sss_path);
|
||||
// file_info = new(parsed_file_info_t, .file=f);
|
||||
// Table_str_set(&cache, path, file_info);
|
||||
// file_info->env = new(env_t);
|
||||
// *file_info->env = *env;
|
||||
|
||||
// file_info->ast = parse_file(f, NULL);
|
||||
// table_t *type_bindings = new(table_t, .fallback=&env->global->types);
|
||||
// bind_types(env, type_bindings, file_info->ast);
|
||||
// populate_types(env, type_bindings, file_info->ast);
|
||||
// bind_variables(env, new(table_t, .fallback=&env->global->bindings), file_info->ast);
|
||||
// // type_t *ns_t = get_namespace_type(env, ast, NULL);
|
||||
// return file_info;
|
||||
// }
|
||||
|
||||
type_t *get_file_type(env_t *env, const char *path)
|
||||
{
|
||||
// auto info = get_file_info(env, path);
|
||||
@ -727,4 +694,19 @@ type_t *get_file_type(env_t *env, const char *path)
|
||||
return get_namespace_type(env, ast, NULL);
|
||||
}
|
||||
|
||||
type_t *get_arg_ast_type(env_t *env, arg_ast_t *arg)
|
||||
{
|
||||
assert(arg->type || arg->value);
|
||||
if (arg->type)
|
||||
return parse_type_ast(env, arg->type);
|
||||
return get_type(env, arg->value);
|
||||
}
|
||||
|
||||
type_t *get_arg_type(env_t *env, arg_t *arg)
|
||||
{
|
||||
assert(arg->type || arg->default_val);
|
||||
if (arg->type) return arg->type;
|
||||
return get_type(env, arg->default_val);
|
||||
}
|
||||
|
||||
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
|
||||
|
@ -15,5 +15,7 @@ bool is_discardable(env_t *env, ast_t *ast);
|
||||
type_t *get_namespace_type(env_t *env, ast_t *namespace_ast, type_t *type);
|
||||
type_t *get_file_type(env_t *env, const char *path);
|
||||
type_t *get_function_def_type(env_t *env, ast_t *ast);
|
||||
type_t *get_arg_type(env_t *env, arg_t *arg);
|
||||
type_t *get_arg_ast_type(env_t *env, arg_ast_t *arg);
|
||||
|
||||
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
|
||||
|
42
types.c
42
types.c
@ -435,10 +435,34 @@ size_t type_size(type_t *t)
|
||||
case ClosureType: return sizeof(struct {void *fn, *userdata;});
|
||||
case PointerType: return sizeof(void*);
|
||||
case StructType: {
|
||||
errx(1, "Not implemented");
|
||||
size_t size = 0;
|
||||
for (arg_t *field = Match(t, StructType)->fields; field; field = field->next) {
|
||||
type_t *field_type = field->type;
|
||||
if (field_type->tag == BoolType) {
|
||||
size += 1; // Bit packing
|
||||
} else {
|
||||
size_t align = type_align(field_type);
|
||||
if (align > 1 && size % align > 0)
|
||||
size += align - (size % align); // Padding
|
||||
size += type_size(field_type);
|
||||
}
|
||||
}
|
||||
return size;
|
||||
}
|
||||
case EnumType: {
|
||||
errx(1, "Not implemented");
|
||||
size_t max_align = 0;
|
||||
size_t max_size = 0;
|
||||
for (tag_t *tag = Match(t, EnumType)->tags; tag; tag = tag->next) {
|
||||
size_t align = type_align(tag->type);
|
||||
if (align > max_align) max_align = align;
|
||||
size_t size = type_size(tag->type);
|
||||
if (size > max_size) max_size = size;
|
||||
}
|
||||
size_t size = sizeof(UnknownType); // generic enum
|
||||
if (max_align > 1 && size % max_align > 0)
|
||||
size += max_align - (size % max_align);
|
||||
size += max_size;
|
||||
return size;
|
||||
}
|
||||
case TypeInfoType: return sizeof(TypeInfo);
|
||||
}
|
||||
@ -460,10 +484,20 @@ size_t type_align(type_t *t)
|
||||
case ClosureType: return __alignof__(void*);
|
||||
case PointerType: return __alignof__(void*);
|
||||
case StructType: {
|
||||
errx(1, "Not implemented");
|
||||
size_t align = 0;
|
||||
for (arg_t *field = Match(t, StructType)->fields; field; field = field->next) {
|
||||
size_t field_align = type_align(field->type);
|
||||
if (field_align > align) align = field_align;
|
||||
}
|
||||
return align;
|
||||
}
|
||||
case EnumType: {
|
||||
errx(1, "Not implemented");
|
||||
size_t align = __alignof__(UnknownType);
|
||||
for (tag_t *tag = Match(t, EnumType)->tags; tag; tag = tag->next) {
|
||||
size_t tag_align = type_align(tag->type);
|
||||
if (tag_align > align) align = tag_align;
|
||||
}
|
||||
return align;
|
||||
}
|
||||
case TypeInfoType: return __alignof__(TypeInfo);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user