Merge remote-tracking branch 'origin/90-implement-the-struct-tree-for-the-parser' into 85-implement-llvm-backend

# Conflicts:
#	src/cfg/opt.c
#	src/set/types.h
This commit is contained in:
Sven Vogel 2024-06-08 23:10:15 +02:00
commit 98ecd8a6ae
13 changed files with 2261 additions and 55 deletions

View File

@ -232,8 +232,7 @@ void print_help(void) {
" --list-targets print a list of all available targets supported",
" --help print this help dialog",
" --color-always always colorize output",
" --version print the version",
" --print-memory-stats print statistics of the garbage collector"
" --print-gc-stats print statistics of the garbage collector"
};
for (unsigned int i = 0; i < sizeof(lines) / sizeof(const char *); i++) {

View File

@ -14,6 +14,7 @@
#include <codegen/backend.h>
#include <llvm/backend.h>
#include <mem/cache.h>
#include <set/set.h>
#define GRAPHVIZ_FILE_EXTENSION "gv"
@ -178,6 +179,7 @@ static void build_target(ModuleFileStack *unit, const TargetConfig *target) {
if (setup_target_environment(target) == 0) {
print_ast_to_file(ast, target);
Module* test = create_set(ast);
// TODO: parse AST to semantic values
Module* module = NULL;
@ -190,6 +192,7 @@ static void build_target(ModuleFileStack *unit, const TargetConfig *target) {
mem_purge_namespace(MemoryNamespaceLex);
mem_purge_namespace(MemoryNamespaceAst);
mem_purge_namespace(MemoryNamespaceSet);
print_file_statistics(file);
}

View File

@ -3,6 +3,8 @@
//
#include <io/files.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/log.h>
#include <assert.h>
#include <sys/col.h>
@ -82,7 +84,7 @@ static void custom_fgets(char *buffer, size_t n, FILE *stream) {
}
}
void print_diagnostic(ModuleFile *file, TokenLocation *location, Message kind, const char *message) {
void print_diagnostic(ModuleFile *file, TokenLocation *location, Message kind, const char *message, ...) {
assert(file->handle != NULL);
assert(location != NULL);
assert(message != NULL);
@ -120,8 +122,16 @@ void print_diagnostic(ModuleFile *file, TokenLocation *location, Message kind, c
const char *absolute_path = get_absolute_path(file->path);
printf("%s%s:%ld:%s %s%s:%s %s\n", BOLD, absolute_path, location->line_start, RESET, accent_color, kind_text, RESET,
message);
printf("%s%s:%ld:%s %s%s:%s ", BOLD, absolute_path, location->line_start, RESET, accent_color, kind_text, RESET);
va_list args;
va_start(args, message);
vprintf(message, args);
va_end(args);
printf("\n");
mem_free((void *) absolute_path);

View File

@ -92,7 +92,7 @@ TokenLocation empty_location(void);
* @param message
*/
[[gnu::nonnull(1), gnu::nonnull(2)]]
void print_diagnostic(ModuleFile *file, TokenLocation *location, Message kind, const char *message);
void print_diagnostic(ModuleFile *file, TokenLocation *location, Message kind, const char *message, ...);
[[gnu::nonnull(2)]]
/**

View File

@ -101,7 +101,7 @@
DEBUG("\"%s\" tokenized with \'ValStr\'", yytext); yylval.string = mem_strdup(MemoryNamespaceLex, yytext); return(ValStr);};
\"\"\"[^\"]*\"\"\" {
yytext = yytext +3;
yytext[yyleng - 4] = 0;
yytext[yyleng - 6] = 0;
DEBUG("\"%s\" tokenized with \'ValMultistr\'", yytext); yylval.string = mem_strdup(MemoryNamespaceLex, yytext); return(ValMultistr);};
[ \r\t] { /* ignore whitespace */ };

View File

@ -66,7 +66,7 @@ int main(int argc, char *argv[]) {
run_compiler();
if (is_option_set("print-memory-stats")) {
if (is_option_set("print-gc-stats")) {
print_memory_statistics();
}

View File

@ -7,6 +7,7 @@
#include <glib.h>
#include <string.h>
#include <assert.h>
#include <cfg/opt.h>
static GHashTable* namespaces = NULL;
@ -20,6 +21,17 @@ typedef struct MemoryNamespaceStatistic_t {
size_t purged_free_count;
} MemoryNamespaceStatistic;
typedef enum MemoryBlockType_t {
GenericBlock,
GLIB_Array,
GLIB_HashTable
} MemoryBlockType;
typedef struct MemoryBlock_t {
void* block_ptr;
MemoryBlockType kind;
} MemoryBlock;
typedef struct MemoryNamespace_t {
MemoryNamespaceStatistic statistic;
GArray* blocks;
@ -44,9 +56,11 @@ static void* namespace_malloc(MemoryNamespaceRef memoryNamespace, size_t size) {
assert(memoryNamespace != NULL);
assert(size != 0);
void* block = malloc(size);
MemoryBlock block;
block.block_ptr = malloc(size);
block.kind = GenericBlock;
if (block == NULL) {
if (block.block_ptr == NULL) {
memoryNamespace->statistic.faulty_allocations ++;
} else {
g_array_append_val(memoryNamespace->blocks, block);
@ -55,20 +69,36 @@ static void* namespace_malloc(MemoryNamespaceRef memoryNamespace, size_t size) {
memoryNamespace->statistic.bytes_allocated += size;
}
return block;
return block.block_ptr;
}
static void namespace_free_block(MemoryBlock block) {
switch (block.kind) {
case GenericBlock:
free(block.block_ptr);
break;
case GLIB_Array:
g_array_free(block.block_ptr, TRUE);
break;
case GLIB_HashTable:
g_hash_table_destroy(block.block_ptr);
break;
}
}
static gboolean namespace_free(MemoryNamespaceRef memoryNamespace, void* block) {
for (guint i = 0; i < memoryNamespace->blocks->len; i++) {
void* current_block = g_array_index(memoryNamespace->blocks, void*, i);
MemoryBlock current_block = g_array_index(memoryNamespace->blocks, MemoryBlock, i);
if (current_block == block) {
if (current_block.block_ptr == block) {
assert(block != NULL);
free(block);
namespace_free_block(current_block);
g_array_remove_index(memoryNamespace->blocks, i);
memoryNamespace->statistic.manual_free_count++;
return TRUE;
}
}
@ -79,13 +109,13 @@ static void* namespace_realloc(MemoryNamespaceRef memoryNamespace, void* block,
void* reallocated_block = NULL;
for (guint i = 0; i < memoryNamespace->blocks->len; i++) {
void* current_block = g_array_index(memoryNamespace->blocks, void*, i);
MemoryBlock current_block = g_array_index(memoryNamespace->blocks, MemoryBlock, i);
if (current_block == block) {
reallocated_block = realloc(block, size);
if (current_block.block_ptr == block) {
reallocated_block = realloc(current_block.block_ptr, size);
if (reallocated_block != NULL) {
g_array_index(memoryNamespace->blocks, void*, i) = reallocated_block;
g_array_index(memoryNamespace->blocks, MemoryBlock, i).block_ptr = reallocated_block;
memoryNamespace->statistic.bytes_allocated += size;
memoryNamespace->statistic.reallocation_count ++;
} else {
@ -107,9 +137,9 @@ static void namespace_delete(MemoryNamespaceRef memoryNamespace) {
static void namespace_purge(MemoryNamespaceRef memoryNamespace) {
for (guint i = 0; i < memoryNamespace->blocks->len; i++) {
void* current_block = g_array_index(memoryNamespace->blocks, void*, i);
MemoryBlock current_block = g_array_index(memoryNamespace->blocks, MemoryBlock, i);
free(current_block);
namespace_free_block(current_block);
memoryNamespace->statistic.purged_free_count ++;
}
@ -120,7 +150,7 @@ static void namespace_purge(MemoryNamespaceRef memoryNamespace) {
static MemoryNamespaceRef namespace_new() {
MemoryNamespaceRef memoryNamespace = malloc(sizeof(MemoryNamespace));
memoryNamespace->blocks = g_array_new(FALSE, FALSE, sizeof(void*));
memoryNamespace->blocks = g_array_new(FALSE, FALSE, sizeof(MemoryBlock));
memoryNamespace->statistic.bytes_allocated = 0;
memoryNamespace->statistic.allocation_count = 0;
memoryNamespace->statistic.manual_free_count = 0;
@ -132,6 +162,30 @@ static MemoryNamespaceRef namespace_new() {
return memoryNamespace;
}
GArray *namespace_new_g_array(MemoryNamespaceRef namespace, guint size) {
MemoryBlock block;
block.block_ptr = g_array_new(FALSE, FALSE, size);
block.kind = GLIB_Array;
g_array_append_val(namespace->blocks, block);
namespace->statistic.bytes_allocated += sizeof(GArray*);
namespace->statistic.allocation_count ++;
return block.block_ptr;
}
GHashTable *namespace_new_g_hash_table(MemoryNamespaceRef namespace, GHashFunc hash_func, GEqualFunc key_equal_func) {
MemoryBlock block;
block.block_ptr = g_hash_table_new(hash_func, key_equal_func);
block.kind = GLIB_HashTable;
g_array_append_val(namespace->blocks, block);
namespace->statistic.bytes_allocated += sizeof(GHashTable*);
namespace->statistic.allocation_count ++;
return block.block_ptr;
}
static void cleanup() {
if (namespaces == NULL) {
printf("==> Memory cache was unused <==\n");
@ -221,7 +275,7 @@ void mem_purge_namespace(MemoryNamespaceName name) {
namespace_purge(cache);
} else {
PANIC("purging invalid namespace: %s", name);
WARN("purging invalid namespace: %s", name);
}
}
@ -267,5 +321,25 @@ void print_memory_statistics() {
namespace_statistics_print(&total, "summary");
printf("Note: untracked are memory allocations from external libraries.\n");
printf("Note: untracked are memory allocations from external libraries and non-gc managed components.\n");
}
GArray* mem_new_g_array(MemoryNamespaceName name, guint element_size) {
MemoryNamespaceRef cache = check_namespace(name);
if (cache == NULL) {
PANIC("memory namespace not created");
}
return namespace_new_g_array(cache, element_size);
}
GHashTable* mem_new_g_hash_table(MemoryNamespaceName name, GHashFunc hash_func, GEqualFunc key_equal_func) {
MemoryNamespaceRef cache = check_namespace(name);
if (cache == NULL) {
PANIC("memory namespace not created");
}
return namespace_new_g_hash_table(cache, hash_func, key_equal_func);
}

View File

@ -7,6 +7,7 @@
#include <mem/cache.h>
#include <stddef.h>
#include <glib.h>
typedef char* MemoryNamespaceName;
@ -87,4 +88,8 @@ void* mem_clone(MemoryNamespaceName name, void* data, size_t size);
void print_memory_statistics();
GArray* mem_new_g_array(MemoryNamespaceName name, guint element_size);
GHashTable* mem_new_g_hash_table(MemoryNamespaceName name, GHashFunc hash_func, GEqualFunc key_equal_func);
#endif //GEMSTONE_CACHE_H

2050
src/set/set.c Normal file

File diff suppressed because it is too large Load Diff

14
src/set/set.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef _SET_H_
#define _SET_H_
#include <ast/ast.h>
#include <set/types.h>
#define SEMANTIC_OK 0
#define SEMANTIC_ERROR 1
Module * create_set(AST_NODE_PTR rootNodePtr );
void delete_set(Module* module);
#endif

0
src/set/types.c Normal file
View File

View File

@ -114,7 +114,7 @@ typedef struct Type_t {
union TypeImplementation_t {
PrimitiveType primitive;
CompositeType composite;
BoxType box;
BoxType* box;
ReferenceType reference;
} impl;
AST_NODE_PTR nodePtr;
@ -281,6 +281,17 @@ typedef struct Variable_t {
AST_NODE_PTR nodePtr;
} Variable;
typedef struct Dereference_t {
Expression* index;
Expression* variable;
AST_NODE_PTR nodePtr;
}Dereference;
typedef struct AddressOf_t {
Expression* variable;
AST_NODE_PTR node_ptr;
}AddressOf;
// .------------------------------------------------.
// | Casts |
// '------------------------------------------------'
@ -412,7 +423,9 @@ typedef enum ExpressionKind_t {
ExpressionKindTypeCast,
ExpressionKindTransmute,
ExpressionKindConstant,
ExpressionKindVariable
ExpressionKindVariable,
ExpressionKindDereference,
ExpressionKindAddressOf,
} ExpressionKind;
typedef struct Expression_t {
@ -425,6 +438,8 @@ typedef struct Expression_t {
Transmute transmute;
TypeValue constant;
Variable* variable;
Dereference dereference;
AddressOf addressOf;
} impl;
AST_NODE_PTR nodePtr;
} Expression;
@ -543,4 +558,39 @@ typedef struct Module_t {
GArray* imports;
} Module;
// .------------------------------------------------.
// | Cleanup Code |
// '------------------------------------------------'
void delete_box_access(BoxAccess* access);
void delete_variable(Variable* variable);
void delete_type(Type* type);
void delete_box(BoxType* box);
void delete_declaration(VariableDeclaration* decl);
void delete_definition(VariableDefiniton* definition);
void delete_expression(Expression* expr);
void delete_operation(Operation* operation);
void delete_type_value(TypeValue* value);
void delete_transmute(Transmute* trans);
void delete_typecast(TypeCast* cast);
void delete_box_member(BoxMember* member);
void delete_box_type(BoxType *box_type);
void delete_composite([[maybe_unused]] CompositeType* composite);
void delete_module(Module* module);
#endif // SET_TYPES_H_

View File

@ -135,7 +135,8 @@
%left '(' ')' '[' ']'
%%
program: program programbody {AST_push_node(root, $2);}
program: program programbody {AST_push_node(root, $2);
}
| programbody {AST_push_node(root, $1);};
programbody: moduleimport {$$ = $1;}
@ -376,7 +377,7 @@ decl: type ':' identlist {AST_NODE_PTR decl = AST_new_node(new_loc(), AST_Decl,
AST_push_node(decl, $1);
AST_push_node(decl, $2);
AST_push_node(decl, $4);
$$ = decl;}
$$ = decl;};
definition: decl '=' expr { AST_NODE_PTR def = AST_new_node(new_loc(), AST_Def, NULL);