aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-03-03 15:04:36 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-03-03 15:04:36 -0500
commitea7fcd85b40dd943e593d823827be7cdf4a972b0 (patch)
treeca2b6b8157b9d02971610515ddbee99aa1bd3866
parent5641d0c837985ba7f89d5efdc5e9c1873c403a57 (diff)
Make load method be "use$name"
-rw-r--r--Makefile5
-rw-r--r--compile.c14
-rw-r--r--compile.h1
-rw-r--r--enums.c9
-rw-r--r--structs.c5
-rw-r--r--tomo.c2
6 files changed, 27 insertions, 9 deletions
diff --git a/Makefile b/Makefile
index 2b8d2e18..351f81be 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,8 @@
PREFIX=/usr/local
VERSION=0.12.1
-CCONFIG=-std=c11 -Werror -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -fPIC -fsanitize=signed-integer-overflow -fno-sanitize-recover -fvisibility=hidden \
- -flto=auto -fno-fat-lto-objects -Wl,-flto -fdollars-in-identifiers
+CCONFIG=-std=c11 -Werror -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -fPIC \
+ -fsanitize=signed-integer-overflow -fno-sanitize-recover -fvisibility=hidden -fdollars-in-identifiers
+LTO=-flto=auto -fno-fat-lto-objects -Wl,-flto
LDFLAGS=-Wl,-rpath '-Wl,$$ORIGIN'
# MAKEFLAGS := --jobs=$(shell nproc) --output-sync=target
CWARN=-Wall -Wextra -Wno-format
diff --git a/compile.c b/compile.c
index 6794ffe8..4ca5cbb0 100644
--- a/compile.c
+++ b/compile.c
@@ -1083,21 +1083,31 @@ module_code_t compile_file(ast_t *ast)
CORD_appendf(&env->code->main, "%r\n", code);
}
+ const char *name = strrchr(ast->file->filename, '/');
+ name = name ? name : ast->file->filename;
+ size_t name_len = 0;
+ while (name[name_len] && (isalnum(name[name_len]) || name[name_len] == '_'))
+ ++name_len;
+ const char *module_name = heap_strn(name, name_len);
+
return (module_code_t){
+ .module_name=module_name,
.header=CORD_all(
"#pragma once\n",
// CORD_asprintf("#line 0 %r\n", Str__quoted(ast->file->filename, false)),
env->code->imports, "\n",
env->code->typedefs, "\n",
env->code->typecode, "\n",
- env->code->fndefs, "\n"),
+ env->code->fndefs, "\n",
+ "void use$", module_name, "(void);\n"
+ ),
.c_file=CORD_all(
// CORD_asprintf("#line 0 %r\n", Str__quoted(ast->file->filename, false)),
env->code->staticdefs, "\n",
env->code->funcs, "\n",
env->code->typeinfos, "\n",
"\n"
- "static void $load(void) {\n",
+ "void use$", module_name, "(void) {\n",
env->code->main,
"}\n"
),
diff --git a/compile.h b/compile.h
index a599c883..e3c2f368 100644
--- a/compile.h
+++ b/compile.h
@@ -8,6 +8,7 @@
#include "environment.h"
typedef struct {
+ const char *module_name;
CORD header, c_file;
} module_code_t;
diff --git a/enums.c b/enums.c
index 1e3d0773..c356fbd1 100644
--- a/enums.c
+++ b/enums.c
@@ -35,7 +35,7 @@ static CORD compile_str_method(env_t *env, ast_t *ast)
}
str_func = CORD_cat(str_func, ", \")\");\n");
}
- str_func = CORD_cat(str_func, "\t}\n}\n");
+ str_func = CORD_cat(str_func, "\tdefault: return CORD_EMPTY;\n\t}\n}\n");
return str_func;
}
@@ -44,6 +44,7 @@ static CORD compile_compare_method(env_t *env, ast_t *ast)
auto def = Match(ast, EnumDef);
CORD cmp_func = CORD_all("static int ", def->name, "__compare(const ", def->name, "_t *x, const ", def->name,
"_t *y, const TypeInfo *info) {\n"
+ "(void)info;\n"
"int diff = x->$tag - y->$tag;\n"
"if (diff) return diff;\n"
"switch (x->$tag) {\n");
@@ -52,7 +53,7 @@ static CORD compile_compare_method(env_t *env, ast_t *ast)
cmp_func = CORD_all(cmp_func, "\tcase $tag$", def->name, "$", tag->name, ": "
"return generic_compare(&x->", tag->name, ", &y->", tag->name, ", ", compile_type_info(env, tag_type), ");\n");
}
- cmp_func = CORD_all(cmp_func, "}\n}\n");
+ cmp_func = CORD_all(cmp_func, "default: return 0;\n}\n}\n");
return cmp_func;
}
@@ -61,6 +62,7 @@ static CORD compile_equals_method(env_t *env, ast_t *ast)
auto def = Match(ast, EnumDef);
CORD eq_func = CORD_all("static bool ", def->name, "__equal(const ", def->name, "_t *x, const ", def->name,
"_t *y, const TypeInfo *info) {\n"
+ "(void)info;\n"
"if (x->$tag != y->$tag) return no;\n"
"switch (x->$tag) {\n");
for (tag_ast_t *tag = def->tags; tag; tag = tag->next) {
@@ -68,7 +70,7 @@ static CORD compile_equals_method(env_t *env, ast_t *ast)
eq_func = CORD_all(eq_func, "\tcase $tag$", def->name, "$", tag->name, ": "
"return generic_equal(&x->", tag->name, ", &y->", tag->name, ", ", compile_type_info(env, tag_type), ");\n");
}
- eq_func = CORD_all(eq_func, "}\n}\n");
+ eq_func = CORD_all(eq_func, "default: return 0;\n}\n}\n");
return eq_func;
}
@@ -76,6 +78,7 @@ static CORD compile_hash_method(env_t *env, ast_t *ast)
{
auto def = Match(ast, EnumDef);
CORD hash_func = CORD_all("static uint32_t ", def->name, "__hash(const ", def->name, "_t *obj, const TypeInfo *info) {\n"
+ "(void)info;\n"
"uint32_t hashes[2] = {(uint32_t)obj->$tag};\n"
"switch (obj->$tag) {\n");
for (tag_ast_t *tag = def->tags; tag; tag = tag->next) {
diff --git a/structs.c b/structs.c
index 5e297927..d13e6db7 100644
--- a/structs.c
+++ b/structs.c
@@ -61,6 +61,7 @@ static CORD compile_compare_method(env_t *env, ast_t *ast)
auto def = Match(ast, StructDef);
CORD cmp_func = CORD_all("static int ", def->name, "__compare(const ", def->name, "_t *x, const ", def->name,
"_t *y, const TypeInfo *info) {\n"
+ "(void)info;\n",
"int diff;\n");
for (arg_ast_t *field = def->fields; field; field = field->next) {
type_t *field_type = get_arg_ast_type(env, field);
@@ -86,7 +87,8 @@ 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");
+ "_t *y, const TypeInfo *info) {\n"
+ "(void)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) {
@@ -110,6 +112,7 @@ 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"
+ "(void)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);
diff --git a/tomo.c b/tomo.c
index 5f540bf2..faf83f58 100644
--- a/tomo.c
+++ b/tomo.c
@@ -104,7 +104,7 @@ int main(int argc, char *argv[])
"(void)argv;\n"
"GC_INIT();\n"
"detect_color();\n"
- "$load();\n"
+ "use$", module.module_name, "();\n"
"return 0;\n"
"}\n"
);