chore: run clang format

This commit is contained in:
Sven Vogel 2024-10-10 22:59:45 +02:00
parent fb055a5225
commit d2d70edb06
14 changed files with 240 additions and 230 deletions

View File

@ -5,12 +5,15 @@
#include <stdio.h> #include <stdio.h>
#include <sys/log.h> #include <sys/log.h>
bool AST_annotation_array_contains_flag(AST_Annotation* haystack, const char* needle) { bool AST_annotation_array_contains_flag(AST_Annotation* haystack,
const char* needle) {
if (haystack->kind == AnnotationKindArray) { if (haystack->kind == AnnotationKindArray) {
for (guint i = 0; i < haystack->impl.array->len; i++) { for (guint i = 0; i < haystack->impl.array->len; i++) {
AST_Annotation child = g_array_index(haystack->impl.array, AST_Annotation, i); AST_Annotation child =
g_array_index(haystack->impl.array, AST_Annotation, i);
if (child.kind == AnnotationKindFlag && strcmp(child.impl.flag, needle) == 0) { if (child.kind == AnnotationKindFlag
&& strcmp(child.impl.flag, needle) == 0) {
return true; return true;
} }
} }
@ -232,8 +235,8 @@ void AST_delete_node(struct AST_Node_t* node) {
DEBUG("Deleting AST node: %p", node); DEBUG("Deleting AST node: %p", node);
if (node->parent != NULL) { if (node->parent != NULL) {
[[maybe_unused]] const struct AST_Node_t* child = [[maybe_unused]]
AST_detach_child(node->parent, node); const struct AST_Node_t* child = AST_detach_child(node->parent, node);
assert(child == node); assert(child == node);
} }
@ -284,8 +287,8 @@ static void AST_fprint_graphviz_node_definition(FILE* stream,
assert(node != NULL); assert(node != NULL);
char* module_path = module_ref_to_str(node->location.module_ref); char* module_path = module_ref_to_str(node->location.module_ref);
fprintf(stream, "\tnode%p [label=\"@%s\\n%s\"]\n", (void*) node, module_path, fprintf(stream, "\tnode%p [label=\"@%s\\n%s\"]\n", (void*) node,
AST_node_to_string(node)); module_path, AST_node_to_string(node));
if (node->children == NULL) { if (node->children == NULL) {
return; return;
@ -367,7 +370,8 @@ void AST_import_module(AST_NODE_PTR dst, size_t k, AST_NODE_PTR src) {
switch (node->kind) { switch (node->kind) {
case AST_FunDef: case AST_FunDef:
{ {
AST_NODE_PTR decl = AST_new_node(node->location, AST_FunDecl, NULL); AST_NODE_PTR decl =
AST_new_node(node->location, AST_FunDecl, NULL);
for (int u = 0; u < node->children->len - 1; u++) { for (int u = 0; u < node->children->len - 1; u++) {
AST_push_node(decl, AST_get_node(node, u)); AST_push_node(decl, AST_get_node(node, u));
@ -378,7 +382,8 @@ void AST_import_module(AST_NODE_PTR dst, size_t k, AST_NODE_PTR src) {
} }
case AST_ProcDef: case AST_ProcDef:
{ {
AST_NODE_PTR decl = AST_new_node(node->location, AST_ProcDecl, NULL); AST_NODE_PTR decl =
AST_new_node(node->location, AST_ProcDecl, NULL);
for (int u = 0; u < node->children->len - 1; u++) { for (int u = 0; u < node->children->len - 1; u++) {
AST_push_node(decl, AST_get_node(node, u)); AST_push_node(decl, AST_get_node(node, u));

View File

@ -14,7 +14,7 @@
static GHashTable* args = NULL; static GHashTable* args = NULL;
static Dependency *new_dependency(); static Dependency* new_dependency();
const char* ARCH_X86_64 = "x86_64"; const char* ARCH_X86_64 = "x86_64";
const char* ARCH_I386 = "i386"; const char* ARCH_I386 = "i386";
@ -44,43 +44,36 @@ const char* ENV_ANDROID = "android";
const char* ENV_MACHO = "macho"; const char* ENV_MACHO = "macho";
const char* ENV_ELF = "elf"; const char* ENV_ELF = "elf";
bool target_has_shared_dependency(TargetLinkConfig* config) bool target_has_shared_dependency(TargetLinkConfig* config) {
{
bool has_shared = false; bool has_shared = false;
const char* shared_files[] = { const char* shared_files[] = {".so", ".dll"};
".so",
".dll"
};
for (guint i = 0; i < config->object_file_names->len; i++) for (guint i = 0; i < config->object_file_names->len; i++) {
{
char* object_file = g_array_index(config->object_file_names, char*, i); char* object_file = g_array_index(config->object_file_names, char*, i);
for (int k = 0; k < sizeof(shared_files)/sizeof(char*); k++) for (int k = 0; k < sizeof(shared_files) / sizeof(char*); k++) {
{
has_shared = g_str_has_suffix(object_file, shared_files[k]); has_shared = g_str_has_suffix(object_file, shared_files[k]);
if (has_shared) if (has_shared) {
break; break;
} }
}
if (has_shared) if (has_shared) {
break; break;
} }
}
return has_shared; return has_shared;
} }
const char* find_string(const char* haystack, const char** options, size_t size) const char* find_string(const char* haystack, const char** options,
{ size_t size) {
const static char* found = NULL; const static char* found = NULL;
if (haystack != NULL) if (haystack != NULL) {
{ for (size_t i = 0; i < size / sizeof(const char*); i++) {
for (size_t i = 0; i < size/sizeof(const char*); i++) if (strstr(haystack, options[i])) {
{
if (strstr(haystack, options[i]))
{
found = options[i]; found = options[i];
break; break;
} }
@ -90,65 +83,36 @@ const char* find_string(const char* haystack, const char** options, size_t size)
return found; return found;
} }
const char* extract_arch_from_triple(const char* triple) const char* extract_arch_from_triple(const char* triple) {
{ const char* known_archs[] = {ARCH_X86_64, ARCH_I386, ARCH_ARM, ARCH_THUMB,
const char* known_archs[] = { ARCH_MIPS};
ARCH_X86_64,
ARCH_I386,
ARCH_ARM,
ARCH_THUMB,
ARCH_MIPS
};
return find_string(triple, known_archs, sizeof(known_archs)); return find_string(triple, known_archs, sizeof(known_archs));
} }
const char* extract_sub_from_triple(const char* triple) const char* extract_sub_from_triple(const char* triple) {
{ const char* known_subs[] = {SUB_V5, SUB_V6M, SUB_V7A, SUB_V7M};
const char* known_subs[] = {
SUB_V5,
SUB_V6M,
SUB_V7A,
SUB_V7M
};
return find_string(triple, known_subs, sizeof(known_subs)); return find_string(triple, known_subs, sizeof(known_subs));
} }
const char* extract_vendor_from_triple(const char* triple) const char* extract_vendor_from_triple(const char* triple) {
{ const char* known_subs[] = {VENDOR_PC, VENDOR_APPLE, VENDOR_NVIDIA,
const char* known_subs[] = { VENDOR_IBM};
VENDOR_PC,
VENDOR_APPLE,
VENDOR_NVIDIA,
VENDOR_IBM
};
return find_string(triple, known_subs, sizeof(known_subs)); return find_string(triple, known_subs, sizeof(known_subs));
} }
const char* extract_sys_from_triple(const char* triple) const char* extract_sys_from_triple(const char* triple) {
{ const char* known_sys[] = {SYS_NONE, SYS_LINUX, SYS_WIN32, SYS_DARWIN,
const char* known_sys[] = { SYS_CUDA};
SYS_NONE,
SYS_LINUX,
SYS_WIN32,
SYS_DARWIN,
SYS_CUDA
};
return find_string(triple, known_sys, sizeof(known_sys)); return find_string(triple, known_sys, sizeof(known_sys));
} }
const char* extract_env_from_triple(const char* triple) const char* extract_env_from_triple(const char* triple) {
{ const char* known_envs[] = {ENV_EABI, ENV_GNU, ENV_ANDROID, ENV_MACHO,
const char* known_envs[] = { ENV_ELF};
ENV_EABI,
ENV_GNU,
ENV_ANDROID,
ENV_MACHO,
ENV_ELF
};
return find_string(triple, known_envs, sizeof(known_envs)); return find_string(triple, known_envs, sizeof(known_envs));
} }
@ -603,7 +567,8 @@ static int get_mode_from_str(TargetCompilationMode* mode, const char* name) {
return PROJECT_SEMANTIC_ERR; return PROJECT_SEMANTIC_ERR;
} }
static int parse_dependency(Dependency* dependency, toml_table_t* table, char* name) { static int parse_dependency(Dependency* dependency, toml_table_t* table,
char* name) {
dependency->name = mem_strdup(MemoryNamespaceOpt, name); dependency->name = mem_strdup(MemoryNamespaceOpt, name);
bool is_project = false; bool is_project = false;
@ -676,7 +641,8 @@ static int parse_target(const ProjectConfig* config,
toml_table_t* dependencies = toml_table_in(target_table, "dependencies"); toml_table_t* dependencies = toml_table_in(target_table, "dependencies");
if (dependencies) { if (dependencies) {
target_config->dependencies = mem_new_g_hash_table(MemoryNamespaceOpt, g_str_hash, g_str_equal); target_config->dependencies =
mem_new_g_hash_table(MemoryNamespaceOpt, g_str_hash, g_str_equal);
for (int i = 0; i < toml_table_ntab(dependencies); i++) { for (int i = 0; i < toml_table_ntab(dependencies); i++) {
char* key = (char*) toml_key_in(dependencies, i); char* key = (char*) toml_key_in(dependencies, i);
@ -686,11 +652,14 @@ static int parse_target(const ProjectConfig* config,
toml_table_t* dependency_table = toml_table_in(dependencies, key); toml_table_t* dependency_table = toml_table_in(dependencies, key);
Dependency* dependency = new_dependency(); Dependency* dependency = new_dependency();
if (parse_dependency(dependency, dependency_table, key) == PROJECT_SEMANTIC_ERR) { if (parse_dependency(dependency, dependency_table, key)
== PROJECT_SEMANTIC_ERR) {
return PROJECT_SEMANTIC_ERR; return PROJECT_SEMANTIC_ERR;
} }
g_hash_table_insert(target_config->dependencies, mem_strdup(MemoryNamespaceOpt, key), dependency); g_hash_table_insert(target_config->dependencies,
mem_strdup(MemoryNamespaceOpt, key),
dependency);
} }
} }

View File

@ -34,13 +34,13 @@ ModuleRef* module_ref_clone(ModuleRef*);
typedef struct TargetLinkConfig_t { typedef struct TargetLinkConfig_t {
// name of object files to link // name of object files to link
GArray *object_file_names; GArray* object_file_names;
// treat warnings as errors // treat warnings as errors
gboolean fatal_warnings; gboolean fatal_warnings;
// colorize linker output // colorize linker output
bool colorize; bool colorize;
char *output_file; char* output_file;
char *driver; char* driver;
// entry point symbol name // entry point symbol name
char* entry; char* entry;
} TargetLinkConfig; } TargetLinkConfig;
@ -52,28 +52,25 @@ typedef enum TargetCompilationMode_t {
Library Library
} TargetCompilationMode; } TargetCompilationMode;
typedef enum DependencyKind_t { typedef enum DependencyKind_t { GemstoneProject, NativeLibrary } DependencyKind;
GemstoneProject,
NativeLibrary
} DependencyKind;
// Possible configuration modes: // Possible configuration modes:
// - build project (local/git) // - build project (local/git)
// - native library (static/shared) // - native library (static/shared)
typedef struct Dependency_t { typedef struct Dependency_t {
char *name; char* name;
DependencyKind kind; DependencyKind kind;
union { union {
struct { struct {
char *path; char* path;
char *target; char* target;
} project; } project;
struct { struct {
char *name; char* name;
bool shared; bool shared;
} library; } library;
} mode; } mode;
GArray *libraries; GArray* libraries;
} Dependency; } Dependency;
/** /**
@ -82,20 +79,20 @@ typedef struct Dependency_t {
* Intermediate representations can be printed as well. * Intermediate representations can be printed as well.
*/ */
typedef struct TargetConfig_t { typedef struct TargetConfig_t {
char *name; char* name;
bool print_ast; bool print_ast;
bool print_asm; bool print_asm;
bool print_ir; bool print_ir;
// root module file which imports all submodules // root module file which imports all submodules
// if this is NULL use the first commandline argument as root module // if this is NULL use the first commandline argument as root module
char *root_module; char* root_module;
// output directory for binaries // output directory for binaries
char *output_directory; char* output_directory;
// output directory for intermediate representations (LLVM-IR, Assembly, // output directory for intermediate representations (LLVM-IR, Assembly,
// ...) // ...)
char *archive_directory; char* archive_directory;
// binary driver for executable generation // binary driver for executable generation
char *driver; char* driver;
// system to compile code for // system to compile code for
// LLVM triple, see: https://clang.llvm.org/docs/CrossCompilation.html#target-triple // LLVM triple, see: https://clang.llvm.org/docs/CrossCompilation.html#target-triple
// in case this is empty this will be the native platform // in case this is empty this will be the native platform
@ -106,13 +103,13 @@ typedef struct TargetConfig_t {
int optimization_level; int optimization_level;
// path to look for object files // path to look for object files
// (can be extra library paths, auto included is output_directory) // (can be extra library paths, auto included is output_directory)
GArray *link_search_paths; GArray* link_search_paths;
// treat linker warnings as errors // treat linker warnings as errors
bool lld_fatal_warnings; bool lld_fatal_warnings;
// treat parser warnings as errors // treat parser warnings as errors
bool gsc_fatal_warnings; bool gsc_fatal_warnings;
GArray *import_paths; GArray* import_paths;
GHashTable *dependencies; GHashTable* dependencies;
} TargetConfig; } TargetConfig;
/** /**
@ -122,16 +119,16 @@ typedef struct TargetConfig_t {
*/ */
typedef struct ProjectConfig_t { typedef struct ProjectConfig_t {
// name of the project // name of the project
char *name; char* name;
// description // description
char *desc; char* desc;
// version // version
char *version; char* version;
// license // license
char *license; char* license;
// list of authors // list of authors
GArray *authors; GArray* authors;
GHashTable *targets; GHashTable* targets;
} ProjectConfig; } ProjectConfig;
/** /**
@ -141,9 +138,9 @@ typedef struct Option_t {
// index in which the option appeared in the argument array // index in which the option appeared in the argument array
int index; int index;
// identifier of the option // identifier of the option
const char *string; const char* string;
// option if format is equals to --option=value // option if format is equals to --option=value
const char *value; const char* value;
// whether or not this option has a value // whether or not this option has a value
bool is_opt; bool is_opt;
} Option; } Option;
@ -194,7 +191,7 @@ bool target_has_shared_dependency(TargetLinkConfig*);
*/ */
[[nodiscard("must be freed")]] [[nodiscard("must be freed")]]
TargetConfig *default_target_config(); TargetConfig* default_target_config();
/** /**
* @brief Create the default configuration for a project. * @brief Create the default configuration for a project.
@ -203,7 +200,7 @@ TargetConfig *default_target_config();
*/ */
[[nodiscard("must be freed")]] [[nodiscard("must be freed")]]
ProjectConfig *default_project_config(); ProjectConfig* default_project_config();
/** /**
* @brief Create a new default target configuration an write command line * @brief Create a new default target configuration an write command line
@ -212,7 +209,7 @@ ProjectConfig *default_project_config();
*/ */
[[nodiscard("must be freed")]] [[nodiscard("must be freed")]]
TargetConfig *default_target_config_from_args(); TargetConfig* default_target_config_from_args();
/** /**
* @brief Load a project configuration from a TOML file with the name * @brief Load a project configuration from a TOML file with the name
@ -222,7 +219,7 @@ TargetConfig *default_target_config_from_args();
*/ */
[[gnu::nonnull(1)]] [[gnu::nonnull(1)]]
int load_project_config(ProjectConfig *config); int load_project_config(ProjectConfig* config);
/** /**
* @brief Print a help dialog to stdout. * @brief Print a help dialog to stdout.
@ -235,14 +232,14 @@ void print_help(void);
*/ */
[[gnu::nonnull(1)]] [[gnu::nonnull(1)]]
void delete_project_config(ProjectConfig *config); void delete_project_config(ProjectConfig* config);
/** /**
* @brief Delete a target configuration by deallocation. * @brief Delete a target configuration by deallocation.
*/ */
[[gnu::nonnull(1)]] [[gnu::nonnull(1)]]
void delete_target_config(TargetConfig *); void delete_target_config(TargetConfig*);
/** /**
* @brief Parse the given command line arguments so that calls to * @brief Parse the given command line arguments so that calls to
@ -250,7 +247,7 @@ void delete_target_config(TargetConfig *);
* @param argc Number of arguments * @param argc Number of arguments
* @param argv Array of arguments * @param argv Array of arguments
*/ */
void parse_options(int argc, char *argv[]); void parse_options(int argc, char* argv[]);
/** /**
* @brief Tests whether an option was set as argument. * @brief Tests whether an option was set as argument.
@ -260,7 +257,7 @@ void parse_options(int argc, char *argv[]);
*/ */
[[gnu::nonnull(1)]] [[gnu::nonnull(1)]]
bool is_option_set(const char *option); bool is_option_set(const char* option);
/** /**
* @brief Returns the options information if present * @brief Returns the options information if present
@ -270,7 +267,7 @@ bool is_option_set(const char *option);
*/ */
[[gnu::nonnull(1)]] [[gnu::nonnull(1)]]
const Option *get_option(const char *option); const Option* get_option(const char* option);
/** /**
* @brief Put a copy of all options whos index is greather than the index * @brief Put a copy of all options whos index is greather than the index
@ -280,7 +277,7 @@ const Option *get_option(const char *option);
*/ */
[[gnu::nonnull(1)]] [[nodiscard("must be freed")]] [[gnu::nonnull(1)]] [[nodiscard("must be freed")]]
GArray *get_non_options_after(const char *command); GArray* get_non_options_after(const char* command);
void init_toml(); void init_toml();

View File

@ -10,6 +10,7 @@
#include <glib.h> #include <glib.h>
#include <io/files.h> #include <io/files.h>
#include <lex/util.h> #include <lex/util.h>
#include <link/lib.h>
#include <llvm/backend.h> #include <llvm/backend.h>
#include <mem/cache.h> #include <mem/cache.h>
#include <set/set.h> #include <set/set.h>
@ -17,7 +18,6 @@
#include <sys/log.h> #include <sys/log.h>
#include <unistd.h> #include <unistd.h>
#include <yacc/parser.tab.h> #include <yacc/parser.tab.h>
#include <link/lib.h>
#define GRAPHVIZ_FILE_EXTENSION "gv" #define GRAPHVIZ_FILE_EXTENSION "gv"
@ -239,7 +239,8 @@ static int compile_module_with_dependencies(ModuleFileStack* unit,
if (NULL == module_ref) { if (NULL == module_ref) {
module_ref = mem_alloc(MemoryNamespaceOpt, sizeof(ModuleRef)); module_ref = mem_alloc(MemoryNamespaceOpt, sizeof(ModuleRef));
module_ref->module_path = mem_new_g_array(MemoryNamespaceOpt, sizeof(char*)); module_ref->module_path =
mem_new_g_array(MemoryNamespaceOpt, sizeof(char*));
} else { } else {
module_ref_push(module_ref, module_from_basename(file->path)); module_ref_push(module_ref, module_from_basename(file->path));
} }
@ -262,37 +263,51 @@ static int compile_module_with_dependencies(ModuleFileStack* unit,
gchar* cwd = g_get_current_dir(); gchar* cwd = g_get_current_dir();
chdir(dependency->mode.project.path); chdir(dependency->mode.project.path);
ProjectConfig* new_config = default_project_config(); ProjectConfig* new_config =
default_project_config();
if (load_project_config(new_config)) { if (load_project_config(new_config)) {
print_message(Error, "Failed to load project config: `%s`", print_message(
Error, "Failed to load project config: `%s`",
child->value); child->value);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
TargetConfig* dep_target = g_hash_table_lookup(new_config->targets, dependency->mode.project.target); TargetConfig* dep_target = g_hash_table_lookup(
new_config->targets,
dependency->mode.project.target);
if (build_target(unit, dep_target)) { if (build_target(unit, dep_target)) {
print_message(Error, "Failed to build project config: `%s`", print_message(
Error, "Failed to build project config: `%s`",
child->value); child->value);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
GPathBuf* buf = g_path_buf_new_from_path(dependency->mode.project.path); GPathBuf* buf = g_path_buf_new_from_path(
TargetConfig* dep_conf = g_hash_table_lookup(new_config->targets, dependency->mode.project.target); dependency->mode.project.path);
TargetConfig* dep_conf = g_hash_table_lookup(
new_config->targets,
dependency->mode.project.target);
char* root_mod = dep_conf->root_module; char* root_mod = dep_conf->root_module;
g_path_buf_push(buf, root_mod); g_path_buf_push(buf, root_mod);
char* rel_path = g_path_buf_to_path(buf); char* rel_path = g_path_buf_to_path(buf);
GPathBuf* dep_bin = g_path_buf_new(); GPathBuf* dep_bin = g_path_buf_new();
g_path_buf_push(dep_bin, dependency->mode.project.path); g_path_buf_push(dep_bin,
g_path_buf_push(dep_bin, dep_conf->archive_directory); dependency->mode.project.path);
char* dep_obj_file = g_strjoin(".", dep_conf->name, "o", NULL); g_path_buf_push(dep_bin,
dep_conf->archive_directory);
char* dep_obj_file =
g_strjoin(".", dep_conf->name, "o", NULL);
g_path_buf_push(dep_bin, dep_obj_file); g_path_buf_push(dep_bin, dep_obj_file);
char* dep_bin_path = g_path_buf_free_to_path(dep_bin); char* dep_bin_path =
g_path_buf_free_to_path(dep_bin);
g_free(dep_obj_file); g_free(dep_obj_file);
char* cached_dep_bin_path = mem_strdup(MemoryNamespaceLld, dep_bin_path); char* cached_dep_bin_path =
mem_strdup(MemoryNamespaceLld, dep_bin_path);
g_free(dep_bin_path); g_free(dep_bin_path);
g_array_append_val(dependency->libraries, cached_dep_bin_path); g_array_append_val(dependency->libraries,
cached_dep_bin_path);
const char* path = const char* path =
get_absolute_import_path(target, rel_path); get_absolute_import_path(target, rel_path);
@ -303,12 +318,16 @@ static int compile_module_with_dependencies(ModuleFileStack* unit,
} }
ModuleFile* imported_file = push_file(unit, path); ModuleFile* imported_file = push_file(unit, path);
AST_NODE_PTR imported_module = AST_NODE_PTR imported_module = AST_new_node(
AST_new_node(empty_location(imported_file, module_ref), AST_Module, NULL); empty_location(imported_file, module_ref),
AST_Module, NULL);
if (compile_module_with_dependencies(unit, imported_file, dep_conf, imported_module) if (compile_module_with_dependencies(
unit, imported_file, dep_conf,
imported_module)
== EXIT_SUCCESS) { == EXIT_SUCCESS) {
AST_import_module(root_module, i + 1, imported_module); AST_import_module(root_module, i + 1,
imported_module);
} else { } else {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -320,41 +339,52 @@ static int compile_module_with_dependencies(ModuleFileStack* unit,
gchar* cached_directory = gchar* cached_directory =
mem_strdup(MemoryNamespaceLld, directory); mem_strdup(MemoryNamespaceLld, directory);
g_free(directory); g_free(directory);
g_array_append_val(target->import_paths, cached_directory); g_array_append_val(target->import_paths,
cached_directory);
chdir(cwd); chdir(cwd);
g_free(cwd); g_free(cwd);
GHashTableIter iter; GHashTableIter iter;
g_hash_table_iter_init(&iter, dep_target->dependencies); g_hash_table_iter_init(&iter,
dep_target->dependencies);
char* key; char* key;
Dependency* dep; Dependency* dep;
while (g_hash_table_iter_next(&iter, (gpointer) &key, (gpointer) &dep)) { while (g_hash_table_iter_next(
&iter, (gpointer) &key, (gpointer) &dep)) {
if (dep->kind == GemstoneProject) { if (dep->kind == GemstoneProject) {
for (guint i = 0; i < dep->libraries->len; i++) { for (guint i = 0; i < dep->libraries->len;
char* dep_lib = g_array_index(dep->libraries, char*, i); i++) {
g_array_append_val(dependency->libraries, dep_lib); char* dep_lib = g_array_index(
dep->libraries, char*, i);
g_array_append_val(
dependency->libraries, dep_lib);
} }
} else if (dep->kind == NativeLibrary) { } else if (dep->kind == NativeLibrary) {
char* library_name = build_platform_library_name(dep->mode.library.name, dep->mode.library.shared); char* library_name =
g_array_append_val(dependency->libraries, library_name); build_platform_library_name(
dep->mode.library.name,
dep->mode.library.shared);
g_array_append_val(dependency->libraries,
library_name);
} }
} }
break; break;
case NativeLibrary: case NativeLibrary:
char* library_name = build_platform_library_name(dependency->mode.library.name, dependency->mode.library.shared); char* library_name = build_platform_library_name(
g_array_append_val(dependency->libraries, library_name); dependency->mode.library.name,
dependency->mode.library.shared);
g_array_append_val(dependency->libraries,
library_name);
break; break;
default: default:
break; break;
} }
} else { } else {
print_message(Error, "Cannot resolve path for import: `%s`", print_message(Error, "Cannot resolve path for import: `%s`",
child->value); child->value);
@ -379,8 +409,8 @@ static int compile_module_with_dependencies(ModuleFileStack* unit,
module_ref_push(module_ref, module_from_basename(path)); module_ref_push(module_ref, module_from_basename(path));
ModuleFile* imported_file = push_file(unit, path); ModuleFile* imported_file = push_file(unit, path);
AST_NODE_PTR imported_module = AST_NODE_PTR imported_module = AST_new_node(
AST_new_node(empty_location(imported_file, module_ref), AST_Module, NULL); empty_location(imported_file, module_ref), AST_Module, NULL);
if (compile_file_to_ast(imported_module, imported_file) if (compile_file_to_ast(imported_module, imported_file)
== EXIT_SUCCESS) { == EXIT_SUCCESS) {

View File

@ -228,8 +228,8 @@ void print_diagnostic(TokenLocation* location, Message kind,
TokenLocation new_location(unsigned long int line_start, TokenLocation new_location(unsigned long int line_start,
unsigned long int col_start, unsigned long int col_start,
unsigned long int line_end, unsigned long int line_end,
unsigned long int col_end, unsigned long int col_end, ModuleFile* file,
ModuleFile* file, ModuleRef* ref) { ModuleRef* ref) {
TokenLocation location; TokenLocation location;
location.line_start = line_start; location.line_start = line_start;

View File

@ -78,8 +78,8 @@ void delete_files(ModuleFileStack* stack);
TokenLocation new_location(unsigned long int line_start, TokenLocation new_location(unsigned long int line_start,
unsigned long int col_start, unsigned long int col_start,
unsigned long int line_end, unsigned long int line_end,
unsigned long int col_end, unsigned long int col_end, ModuleFile* file,
ModuleFile* file, ModuleRef* ref); ModuleRef* ref);
/** /**
* @brief Create a new empty location with all of its contents set to zero * @brief Create a new empty location with all of its contents set to zero

View File

@ -11,7 +11,8 @@
#include <mem/cache.h> #include <mem/cache.h>
#include <sys/log.h> #include <sys/log.h>
static driver_init AVAILABLE_DRIVER[] = {clang_get_driver, gcc_get_driver, lldc_get_driver}; static driver_init AVAILABLE_DRIVER[] = {clang_get_driver, gcc_get_driver,
lldc_get_driver};
static GHashTable* binary_driver = NULL; static GHashTable* binary_driver = NULL;
@ -82,7 +83,8 @@ static const char* get_library_file_extension(bool shared) {
} }
char* build_platform_library_name(char* basename, bool shared) { char* build_platform_library_name(char* basename, bool shared) {
char* library_name = g_strjoin("", "lib", basename, ".", get_library_file_extension(shared), NULL); char* library_name = g_strjoin("", "lib", basename, ".",
get_library_file_extension(shared), NULL);
char* cached_library_name = mem_strdup(MemoryNamespaceLld, library_name); char* cached_library_name = mem_strdup(MemoryNamespaceLld, library_name);
g_free(library_name); g_free(library_name);
return cached_library_name; return cached_library_name;

View File

@ -8,15 +8,11 @@
#include <mem/cache.h> #include <mem/cache.h>
#include <stdio.h> #include <stdio.h>
extern int lld_main(int Argc, const char **Argv, const char **outstr); extern int lld_main(int Argc, const char** Argv, const char** outstr);
const char* FLAGS[] = { const char* FLAGS[] = {"--fatal-warnings", "--nostdlib"};
"--fatal-warnings",
"--nostdlib"
};
const char* get_optimization_level_string(TargetConfig* config) const char* get_optimization_level_string(TargetConfig* config) {
{
char* buffer = mem_alloc(MemoryNamespaceLld, 6); char* buffer = mem_alloc(MemoryNamespaceLld, 6);
sprintf(buffer, "-O%d", config->optimization_level); sprintf(buffer, "-O%d", config->optimization_level);
@ -37,17 +33,17 @@ bool lldc_link(TargetConfig* target_config, TargetLinkConfig* link_config) {
} }
if (link_config->entry != NULL) { if (link_config->entry != NULL) {
char* colored_diagnostics = mem_asprintf(MemoryNamespaceLld, "--entry=%s", link_config->entry); char* colored_diagnostics =
mem_asprintf(MemoryNamespaceLld, "--entry=%s", link_config->entry);
g_array_append_val(arguments, colored_diagnostics); g_array_append_val(arguments, colored_diagnostics);
} }
const char* optimization_level = get_optimization_level_string(target_config); const char* optimization_level =
get_optimization_level_string(target_config);
g_array_append_val(arguments, optimization_level); g_array_append_val(arguments, optimization_level);
if (extract_sys_from_triple(target_config->triple) == SYS_LINUX) if (extract_sys_from_triple(target_config->triple) == SYS_LINUX) {
{ if (target_has_shared_dependency(link_config)) {
if (target_has_shared_dependency(link_config))
{
// add dynamic linker // add dynamic linker
print_message(Info, "Enabling dynamic linker for build"); print_message(Info, "Enabling dynamic linker for build");
@ -57,13 +53,16 @@ bool lldc_link(TargetConfig* target_config, TargetLinkConfig* link_config) {
const char* default_dynamic_linker_path = "/usr/bin/ld.so"; const char* default_dynamic_linker_path = "/usr/bin/ld.so";
const char* dynamic_linker_option = "--dynamic-linker="; const char* dynamic_linker_option = "--dynamic-linker=";
char* buffer = mem_alloc(MemoryNamespaceLld, strlen(dynamic_linker_option) + strlen(default_dynamic_linker_path) + 1); char* buffer = mem_alloc(
sprintf(buffer, "%s%s", dynamic_linker_option, default_dynamic_linker_path); MemoryNamespaceLld, strlen(dynamic_linker_option)
+ strlen(default_dynamic_linker_path) + 1);
sprintf(buffer, "%s%s", dynamic_linker_option,
default_dynamic_linker_path);
g_array_append_val(arguments, buffer); g_array_append_val(arguments, buffer);
} }
} }
for (int i = 0; i < sizeof(FLAGS)/sizeof(char*); i++) { for (int i = 0; i < sizeof(FLAGS) / sizeof(char*); i++) {
char* flag = (char*) FLAGS[i]; char* flag = (char*) FLAGS[i];
g_array_append_val(arguments, flag); g_array_append_val(arguments, flag);
@ -86,12 +85,14 @@ bool lldc_link(TargetConfig* target_config, TargetLinkConfig* link_config) {
char* buffer = mem_alloc(MemoryNamespaceLld, chars + 1); char* buffer = mem_alloc(MemoryNamespaceLld, chars + 1);
size_t offset = 0; size_t offset = 0;
for (guint i = 0; i < arguments->len; i++) { for (guint i = 0; i < arguments->len; i++) {
offset += sprintf(buffer + offset, "%s ", g_array_index(arguments, char*, i)); offset +=
sprintf(buffer + offset, "%s ", g_array_index(arguments, char*, i));
} }
print_message(Info, buffer); print_message(Info, buffer);
const char* message = NULL; const char* message = NULL;
const bool code = lld_main(arguments->len, (const char**) arguments->data, &message); const bool code =
lld_main(arguments->len, (const char**) arguments->data, &message);
if (!code) { if (!code) {
print_message(Error, message); print_message(Error, message);
@ -113,4 +114,3 @@ BinaryDriver* lldc_get_driver() {
return driver; return driver;
} }

View File

@ -60,8 +60,7 @@ static char* create_target_output_name(const TargetConfig* config) {
return cached_name; return cached_name;
} }
Target create_target_from_triple(char* triple) Target create_target_from_triple(char* triple) {
{
Target target; Target target;
target.triple = mem_strdup(MemoryNamespaceLld, triple); target.triple = mem_strdup(MemoryNamespaceLld, triple);
@ -79,11 +78,9 @@ Target create_target_from_config(TargetConfig* config) {
DEBUG("Building target from configuration"); DEBUG("Building target from configuration");
Target target = create_native_target(); Target target = create_native_target();
if (config->triple != NULL) if (config->triple != NULL) {
{
target = create_target_from_triple(config->triple); target = create_target_from_triple(config->triple);
} else } else {
{
config->triple = target.triple; config->triple = target.triple;
} }
@ -91,8 +88,8 @@ Target create_target_from_config(TargetConfig* config) {
target.opt = llvm_opt_from_int(config->optimization_level); target.opt = llvm_opt_from_int(config->optimization_level);
INFO("Configured target: %s/%d: (%s) on %s { %s }", target.name, INFO("Configured target: %s/%d: (%s) on %s { %s }", target.name, target.opt,
target.opt, target.triple, target.cpu, target.features); target.triple, target.cpu, target.features);
return target; return target;
} }

View File

@ -99,8 +99,9 @@ TargetLinkConfig* lld_create_link_config(__attribute__((unused))
for (guint k = 0; k < dep->libraries->len; k++) { for (guint k = 0; k < dep->libraries->len; k++) {
char* lib = g_array_index(dep->libraries, char*, k); char* lib = g_array_index(dep->libraries, char*, k);
if (lib == NULL) if (lib == NULL) {
continue; continue;
}
// resolve path to object file // resolve path to object file
if (g_file_test(lib, G_FILE_TEST_EXISTS)) { if (g_file_test(lib, G_FILE_TEST_EXISTS)) {
@ -122,7 +123,8 @@ TargetLinkConfig* lld_create_link_config(__attribute__((unused))
return config; return config;
} }
BackendError lld_link_target(TargetConfig* target_config, TargetLinkConfig* config) { BackendError lld_link_target(TargetConfig* target_config,
TargetLinkConfig* config) {
if (link_run(target_config, config)) { if (link_run(target_config, config)) {
return SUCCESS; return SUCCESS;

View File

@ -13,7 +13,8 @@ TargetLinkConfig* lld_create_link_config(__attribute__((unused))
const TargetConfig* target_config, const TargetConfig* target_config,
const Module* module); const Module* module);
BackendError lld_link_target(TargetConfig* target_config, TargetLinkConfig* config); BackendError lld_link_target(TargetConfig* target_config,
TargetLinkConfig* config);
void lld_delete_link_config(TargetLinkConfig* config); void lld_delete_link_config(TargetLinkConfig* config);

View File

@ -124,8 +124,7 @@ BackendError export_object(LLVMBackendCompileUnit* unit, const Target* target,
LLVMInitializeAllAsmPrinters(); LLVMInitializeAllAsmPrinters();
DEBUG("creating target..."); DEBUG("creating target...");
if (LLVMGetTargetFromTriple(target->triple, &llvm_target, &error) if (LLVMGetTargetFromTriple(target->triple, &llvm_target, &error) != 0) {
!= 0) {
ERROR("failed to create target machine: %s", error); ERROR("failed to create target machine: %s", error);
err = new_backend_impl_error(Implementation, NULL, err = new_backend_impl_error(Implementation, NULL,
"unable to create target machine"); "unable to create target machine");
@ -135,8 +134,8 @@ BackendError export_object(LLVMBackendCompileUnit* unit, const Target* target,
DEBUG("Creating target machine..."); DEBUG("Creating target machine...");
LLVMTargetMachineRef target_machine = LLVMCreateTargetMachine( LLVMTargetMachineRef target_machine = LLVMCreateTargetMachine(
llvm_target, target->triple, target->cpu, target->features, llvm_target, target->triple, target->cpu, target->features, target->opt,
target->opt, target->reloc, target->model); target->reloc, target->model);
print_message(Info, "Generating code for: %s", target->triple); print_message(Info, "Generating code for: %s", target->triple);

View File

@ -1,4 +1,5 @@
#include "cfg/opt.h" #include "cfg/opt.h"
#include <assert.h> #include <assert.h>
#include <ast/ast.h> #include <ast/ast.h>
#include <glib.h> #include <glib.h>
@ -2649,12 +2650,15 @@ int createFunction(Function* function, AST_NODE_PTR currentNode) {
} }
if (!(currentNode->annotation.kind == AnnotationKindArray if (!(currentNode->annotation.kind == AnnotationKindArray
&& AST_annotation_array_contains_flag(&currentNode->annotation, "nomangle"))) { && AST_annotation_array_contains_flag(&currentNode->annotation,
"nomangle"))) {
// compose function name by appending parent modules // compose function name by appending parent modules
char* modules = module_ref_to_str(currentNode->location.module_ref); char* modules = module_ref_to_str(currentNode->location.module_ref);
char* composed_name = g_strjoin("", modules, "::", function->name, NULL); char* composed_name =
char* cached_composed_name = mem_strdup(MemoryNamespaceSet, composed_name); g_strjoin("", modules, "::", function->name, NULL);
char* cached_composed_name =
mem_strdup(MemoryNamespaceSet, composed_name);
g_free(composed_name); g_free(composed_name);
function->name = cached_composed_name; function->name = cached_composed_name;
@ -2930,10 +2934,13 @@ Module* create_set(AST_NODE_PTR currentNode) {
} }
if (function_node->annotation.kind == AnnotationKindArray) { if (function_node->annotation.kind == AnnotationKindArray) {
if (AST_annotation_array_contains_flag(&function_node->annotation, "entry")) { if (AST_annotation_array_contains_flag(
&function_node->annotation, "entry")) {
if (rootModule->entry != NULL) { if (rootModule->entry != NULL) {
print_diagnostic(&function_node->location, Error, "Multiple functions marked as entry points"); print_diagnostic(
&function_node->location, Error,
"Multiple functions marked as entry points");
return NULL; return NULL;
} }
@ -2941,7 +2948,8 @@ Module* create_set(AST_NODE_PTR currentNode) {
} }
} }
g_hash_table_insert(rootModule->functions, function->name, function); g_hash_table_insert(rootModule->functions, function->name,
function);
DEBUG("created function successfully"); DEBUG("created function successfully");
break; break;