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 <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) {
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;
}
}
@ -232,8 +235,8 @@ void AST_delete_node(struct AST_Node_t* node) {
DEBUG("Deleting AST node: %p", node);
if (node->parent != NULL) {
[[maybe_unused]] const struct AST_Node_t* child =
AST_detach_child(node->parent, node);
[[maybe_unused]]
const struct AST_Node_t* child = AST_detach_child(node->parent, node);
assert(child == node);
}
@ -284,8 +287,8 @@ static void AST_fprint_graphviz_node_definition(FILE* stream,
assert(node != NULL);
char* module_path = module_ref_to_str(node->location.module_ref);
fprintf(stream, "\tnode%p [label=\"@%s\\n%s\"]\n", (void*) node, module_path,
AST_node_to_string(node));
fprintf(stream, "\tnode%p [label=\"@%s\\n%s\"]\n", (void*) node,
module_path, AST_node_to_string(node));
if (node->children == NULL) {
return;
@ -366,27 +369,29 @@ void AST_import_module(AST_NODE_PTR dst, size_t k, AST_NODE_PTR src) {
// TODO: resolve by public symbols
switch (node->kind) {
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++) {
AST_push_node(decl, AST_get_node(node, u));
for (int u = 0; u < node->children->len - 1; u++) {
AST_push_node(decl, AST_get_node(node, u));
}
node = decl;
break;
}
node = decl;
break;
}
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++) {
AST_push_node(decl, AST_get_node(node, u));
for (int u = 0; u < node->children->len - 1; u++) {
AST_push_node(decl, AST_get_node(node, u));
}
node = decl;
break;
}
node = decl;
break;
}
case AST_Typedef:
case AST_Def:
break;
@ -394,7 +399,7 @@ void AST_import_module(AST_NODE_PTR dst, size_t k, AST_NODE_PTR src) {
node = NULL;
}
if (node != NULL) {
if (node != NULL) {
AST_insert_node(dst, k + d, node);
d++;
}

View File

@ -14,21 +14,21 @@
static GHashTable* args = NULL;
static Dependency *new_dependency();
static Dependency* new_dependency();
const char* ARCH_X86_64 = "x86_64";
const char* ARCH_I386 = "i386";
const char* ARCH_ARM = "arm";
const char* ARCH_THUMB = "thumb";
const char* ARCH_MIPS = "mips";
const char* ARCH_I386 = "i386";
const char* ARCH_ARM = "arm";
const char* ARCH_THUMB = "thumb";
const char* ARCH_MIPS = "mips";
const char* SUB_V5 = "v5";
const char* SUB_V5 = "v5";
const char* SUB_V6M = "v6m";
const char* SUB_V7A = "v7a";
const char* SUB_V7M = "v7m";
const char* VENDOR_PC = "pc";
const char* VENDOR_APPLE = "apple";
const char* VENDOR_PC = "pc";
const char* VENDOR_APPLE = "apple";
const char* VENDOR_NVIDIA = "nvidia";
const char* VENDOR_IBM = "ibm";
@ -38,49 +38,42 @@ const char* SYS_WIN32 = "win32";
const char* SYS_DARWIN = "darwin";
const char* SYS_CUDA = "cuda";
const char* ENV_EABI = "eabi";
const char* ENV_EABI = "eabi";
const char* ENV_GNU = "gnu";
const char* ENV_ANDROID = "android";
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;
const char* shared_files[] = {
".so",
".dll"
};
const char* shared_files[] = {".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);
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]);
if (has_shared)
if (has_shared) {
break;
}
}
if (has_shared)
if (has_shared) {
break;
}
}
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;
if (haystack != NULL)
{
for (size_t i = 0; i < size/sizeof(const char*); i++)
{
if (strstr(haystack, options[i]))
{
if (haystack != NULL) {
for (size_t i = 0; i < size / sizeof(const char*); i++) {
if (strstr(haystack, options[i])) {
found = options[i];
break;
}
@ -90,65 +83,36 @@ const char* find_string(const char* haystack, const char** options, size_t size)
return found;
}
const char* extract_arch_from_triple(const char* triple)
{
const char* known_archs[] = {
ARCH_X86_64,
ARCH_I386,
ARCH_ARM,
ARCH_THUMB,
ARCH_MIPS
};
const char* extract_arch_from_triple(const char* triple) {
const char* known_archs[] = {ARCH_X86_64, ARCH_I386, ARCH_ARM, ARCH_THUMB,
ARCH_MIPS};
return find_string(triple, known_archs, sizeof(known_archs));
}
const char* extract_sub_from_triple(const char* triple)
{
const char* known_subs[] = {
SUB_V5,
SUB_V6M,
SUB_V7A,
SUB_V7M
};
const char* extract_sub_from_triple(const char* triple) {
const char* known_subs[] = {SUB_V5, SUB_V6M, SUB_V7A, SUB_V7M};
return find_string(triple, known_subs, sizeof(known_subs));
}
const char* extract_vendor_from_triple(const char* triple)
{
const char* known_subs[] = {
VENDOR_PC,
VENDOR_APPLE,
VENDOR_NVIDIA,
VENDOR_IBM
};
const char* extract_vendor_from_triple(const char* triple) {
const char* known_subs[] = {VENDOR_PC, VENDOR_APPLE, VENDOR_NVIDIA,
VENDOR_IBM};
return find_string(triple, known_subs, sizeof(known_subs));
}
const char* extract_sys_from_triple(const char* triple)
{
const char* known_sys[] = {
SYS_NONE,
SYS_LINUX,
SYS_WIN32,
SYS_DARWIN,
SYS_CUDA
};
const char* extract_sys_from_triple(const char* triple) {
const char* known_sys[] = {SYS_NONE, SYS_LINUX, SYS_WIN32, SYS_DARWIN,
SYS_CUDA};
return find_string(triple, known_sys, sizeof(known_sys));
}
const char* extract_env_from_triple(const char* triple)
{
const char* known_envs[] = {
ENV_EABI,
ENV_GNU,
ENV_ANDROID,
ENV_MACHO,
ENV_ELF
};
const char* extract_env_from_triple(const char* triple) {
const char* known_envs[] = {ENV_EABI, ENV_GNU, ENV_ANDROID, ENV_MACHO,
ENV_ELF};
return find_string(triple, known_envs, sizeof(known_envs));
}
@ -305,7 +269,7 @@ TargetConfig* default_target_config() {
config->lld_fatal_warnings = FALSE;
config->gsc_fatal_warnings = FALSE;
config->import_paths = mem_new_g_array(MemoryNamespaceOpt, sizeof(char*));
config->triple = NULL;
config->triple = NULL;
return config;
}
@ -603,7 +567,8 @@ static int get_mode_from_str(TargetCompilationMode* mode, const char* name) {
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);
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");
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++) {
char* key = (char*) toml_key_in(dependencies, i);
@ -685,12 +651,15 @@ static int parse_target(const ProjectConfig* config,
}
toml_table_t* dependency_table = toml_table_in(dependencies, key);
Dependency* dependency = new_dependency();
if (parse_dependency(dependency, dependency_table, key) == PROJECT_SEMANTIC_ERR) {
Dependency* dependency = new_dependency();
if (parse_dependency(dependency, dependency_table, key)
== 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 {
// name of object files to link
GArray *object_file_names;
GArray* object_file_names;
// treat warnings as errors
gboolean fatal_warnings;
// colorize linker output
bool colorize;
char *output_file;
char *driver;
char* output_file;
char* driver;
// entry point symbol name
char* entry;
} TargetLinkConfig;
@ -52,28 +52,25 @@ typedef enum TargetCompilationMode_t {
Library
} TargetCompilationMode;
typedef enum DependencyKind_t {
GemstoneProject,
NativeLibrary
} DependencyKind;
typedef enum DependencyKind_t { GemstoneProject, NativeLibrary } DependencyKind;
// Possible configuration modes:
// - build project (local/git)
// - native library (static/shared)
typedef struct Dependency_t {
char *name;
char* name;
DependencyKind kind;
union {
struct {
char *path;
char *target;
char* path;
char* target;
} project;
struct {
char *name;
char* name;
bool shared;
} library;
} mode;
GArray *libraries;
GArray* libraries;
} Dependency;
/**
@ -82,20 +79,20 @@ typedef struct Dependency_t {
* Intermediate representations can be printed as well.
*/
typedef struct TargetConfig_t {
char *name;
char* name;
bool print_ast;
bool print_asm;
bool print_ir;
// root module file which imports all submodules
// if this is NULL use the first commandline argument as root module
char *root_module;
char* root_module;
// output directory for binaries
char *output_directory;
char* output_directory;
// output directory for intermediate representations (LLVM-IR, Assembly,
// ...)
char *archive_directory;
char* archive_directory;
// binary driver for executable generation
char *driver;
char* driver;
// system to compile code for
// LLVM triple, see: https://clang.llvm.org/docs/CrossCompilation.html#target-triple
// in case this is empty this will be the native platform
@ -106,13 +103,13 @@ typedef struct TargetConfig_t {
int optimization_level;
// path to look for object files
// (can be extra library paths, auto included is output_directory)
GArray *link_search_paths;
GArray* link_search_paths;
// treat linker warnings as errors
bool lld_fatal_warnings;
// treat parser warnings as errors
bool gsc_fatal_warnings;
GArray *import_paths;
GHashTable *dependencies;
GArray* import_paths;
GHashTable* dependencies;
} TargetConfig;
/**
@ -122,16 +119,16 @@ typedef struct TargetConfig_t {
*/
typedef struct ProjectConfig_t {
// name of the project
char *name;
char* name;
// description
char *desc;
char* desc;
// version
char *version;
char* version;
// license
char *license;
char* license;
// list of authors
GArray *authors;
GHashTable *targets;
GArray* authors;
GHashTable* targets;
} ProjectConfig;
/**
@ -141,9 +138,9 @@ typedef struct Option_t {
// index in which the option appeared in the argument array
int index;
// identifier of the option
const char *string;
const char* string;
// option if format is equals to --option=value
const char *value;
const char* value;
// whether or not this option has a value
bool is_opt;
} Option;
@ -194,7 +191,7 @@ bool target_has_shared_dependency(TargetLinkConfig*);
*/
[[nodiscard("must be freed")]]
TargetConfig *default_target_config();
TargetConfig* default_target_config();
/**
* @brief Create the default configuration for a project.
@ -203,7 +200,7 @@ TargetConfig *default_target_config();
*/
[[nodiscard("must be freed")]]
ProjectConfig *default_project_config();
ProjectConfig* default_project_config();
/**
* @brief Create a new default target configuration an write command line
@ -212,7 +209,7 @@ ProjectConfig *default_project_config();
*/
[[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
@ -222,7 +219,7 @@ TargetConfig *default_target_config_from_args();
*/
[[gnu::nonnull(1)]]
int load_project_config(ProjectConfig *config);
int load_project_config(ProjectConfig* config);
/**
* @brief Print a help dialog to stdout.
@ -235,14 +232,14 @@ void print_help(void);
*/
[[gnu::nonnull(1)]]
void delete_project_config(ProjectConfig *config);
void delete_project_config(ProjectConfig* config);
/**
* @brief Delete a target configuration by deallocation.
*/
[[gnu::nonnull(1)]]
void delete_target_config(TargetConfig *);
void delete_target_config(TargetConfig*);
/**
* @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 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.
@ -260,7 +257,7 @@ void parse_options(int argc, char *argv[]);
*/
[[gnu::nonnull(1)]]
bool is_option_set(const char *option);
bool is_option_set(const char* option);
/**
* @brief Returns the options information if present
@ -270,7 +267,7 @@ bool is_option_set(const char *option);
*/
[[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
@ -280,7 +277,7 @@ const Option *get_option(const char *option);
*/
[[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();

View File

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

View File

@ -60,7 +60,7 @@ ModuleFile* push_file(ModuleFileStack* stack, const char* path) {
char* module_from_basename(char* path) {
char* basename = g_path_get_basename(path);
char* dot = strrchr(basename, '.');
char* dot = strrchr(basename, '.');
if (dot != NULL) {
*dot = '\0';
@ -228,8 +228,8 @@ void print_diagnostic(TokenLocation* location, Message kind,
TokenLocation new_location(unsigned long int line_start,
unsigned long int col_start,
unsigned long int line_end,
unsigned long int col_end,
ModuleFile* file, ModuleRef* ref) {
unsigned long int col_end, ModuleFile* file,
ModuleRef* ref) {
TokenLocation location;
location.line_start = line_start;

View File

@ -78,8 +78,8 @@ void delete_files(ModuleFileStack* stack);
TokenLocation new_location(unsigned long int line_start,
unsigned long int col_start,
unsigned long int line_end,
unsigned long int col_end,
ModuleFile* file, ModuleRef* ref);
unsigned long int col_end, ModuleFile* file,
ModuleRef* ref);
/**
* @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 <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;
@ -82,7 +83,8 @@ static const char* get_library_file_extension(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);
g_free(library_name);
return cached_library_name;

View File

@ -8,15 +8,11 @@
#include <mem/cache.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[] = {
"--fatal-warnings",
"--nostdlib"
};
const char* FLAGS[] = {"--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);
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) {
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);
}
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);
if (extract_sys_from_triple(target_config->triple) == SYS_LINUX)
{
if (target_has_shared_dependency(link_config))
{
if (extract_sys_from_triple(target_config->triple) == SYS_LINUX) {
if (target_has_shared_dependency(link_config)) {
// add dynamic linker
print_message(Info, "Enabling dynamic linker for build");
@ -55,15 +51,18 @@ bool lldc_link(TargetConfig* target_config, TargetLinkConfig* link_config) {
g_array_append_val(arguments, enable_dynamic_linker);
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);
sprintf(buffer, "%s%s", dynamic_linker_option, default_dynamic_linker_path);
char* buffer = mem_alloc(
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);
}
}
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];
g_array_append_val(arguments, flag);
@ -83,15 +82,17 @@ bool lldc_link(TargetConfig* target_config, TargetLinkConfig* link_config) {
chars += strlen(g_array_index(arguments, char*, i)) + 1;
}
char* buffer = mem_alloc(MemoryNamespaceLld, chars + 1);
char* buffer = mem_alloc(MemoryNamespaceLld, chars + 1);
size_t offset = 0;
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);
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) {
print_message(Error, message);
@ -113,4 +114,3 @@ BinaryDriver* lldc_get_driver() {
return driver;
}

View File

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

View File

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

View File

@ -13,7 +13,8 @@ TargetLinkConfig* lld_create_link_config(__attribute__((unused))
const TargetConfig* target_config,
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);

View File

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

View File

@ -375,7 +375,7 @@ char* mem_asprintf(MemoryNamespaceName name, const char* format, ...) {
va_start(args, fmt);
char* buffer = NULL;
int chars = vasprintf(&buffer, format, args);
int chars = vasprintf(&buffer, format, args);
va_end(args);

View File

@ -1,4 +1,5 @@
#include "cfg/opt.h"
#include <assert.h>
#include <ast/ast.h>
#include <glib.h>
@ -2092,7 +2093,7 @@ char* module_ref_to_string(AST_NODE_PTR node) {
char* submodule = module_ref_to_string(AST_get_last_node(node));
char* curmodule = module_ref_to_string(AST_get_node(node, 0));
char* composed = g_strjoin("::", curmodule, submodule, NULL);
char* composed = g_strjoin("::", curmodule, submodule, NULL);
char* cached_composed = mem_strdup(MemoryNamespaceSet, composed);
g_free(composed);
mem_free(curmodule);
@ -2649,12 +2650,15 @@ int createFunction(Function* function, AST_NODE_PTR currentNode) {
}
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
char* modules = module_ref_to_str(currentNode->location.module_ref);
char* composed_name = g_strjoin("", modules, "::", function->name, NULL);
char* cached_composed_name = mem_strdup(MemoryNamespaceSet, composed_name);
char* composed_name =
g_strjoin("", modules, "::", function->name, NULL);
char* cached_composed_name =
mem_strdup(MemoryNamespaceSet, composed_name);
g_free(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 (AST_annotation_array_contains_flag(&function_node->annotation, "entry")) {
if (AST_annotation_array_contains_flag(
&function_node->annotation, "entry")) {
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;
}
@ -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");
break;