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

@ -226,14 +226,13 @@ void print_help(void) {
" --lld-fatal-warnings treat linker warnings as errors",
" --gsc-fatal-warnings treat parser warnings as errors",
"Options:",
" --verbose print logs with level information or higher",
" --debug print debug logs (if not disabled at compile time)",
" --version print the version",
" --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"
" --verbose print logs with level information or higher",
" --debug print debug logs (if not disabled at compile time)",
" --version print the version",
" --list-targets print a list of all available targets supported",
" --help print this help dialog",
" --color-always always colorize output",
" --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

@ -10,7 +10,7 @@
/**
* @brief Primitive types form the basis of all other types.
*
*
*/
typedef enum PrimitiveType_t {
// 4 byte signed integer in two's complement
@ -21,7 +21,7 @@ typedef enum PrimitiveType_t {
/**
* @brief Represents the sign of a composite type.
*
*
*/
typedef enum Sign_t {
// type has no sign bit
@ -34,13 +34,13 @@ typedef enum Sign_t {
* @brief Represents the scale of composite type which is multiplied
* with the base size in order to retrieve the the composites size.
* @attention Valid value are: { 1/8, 1/4, 1/2, 1, 2, 4, 8 }
*
*
*/
typedef double Scale;
/**
* @brief A composite type is an extended definition of a primitive type.
*
*
*/
typedef struct CompositeType_t {
// sign of composite
@ -52,7 +52,7 @@ typedef struct CompositeType_t {
/**
* @brief Specifies the specific type of the generic type struct.
*
*
*/
typedef enum TypeKind_t {
TypeKindPrimitive,
@ -66,7 +66,7 @@ typedef struct Type_t Type;
/**
* @brief Reference points to a type.
* @attention Can be nested. A reference can point to another reference: REF -> REF -> REF -> Primitive
*
*
*/
typedef Type* ReferenceType;
@ -86,11 +86,11 @@ typedef struct BoxMember_t {
/**
* @brief Essentially a g lorified struct
*
*
*/
typedef struct BoxType_t {
// hashtable of members.
// Associates the memebers name (const char*) with its type (BoxMember)
// Associates the memebers name (const char*) with its type (BoxMember)
GHashTable* member; //BoxMember Pointer
AST_NODE_PTR nodePtr;
} BoxType;
@ -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;
@ -130,7 +130,7 @@ typedef struct Typedefine_t {
/**
* @brief Reprents the value of type. Can be used to definitions, initialization and for expressions contants.
*
*
*/
typedef struct TypeValue_t {
// the type
@ -146,7 +146,7 @@ typedef struct TypeValue_t {
/**
* @brief Specifies a parameters I/O properties
*
*
*/
typedef enum IO_Qualifier_t {
// Can be read from but not written to.
@ -162,7 +162,7 @@ typedef enum IO_Qualifier_t {
/**
* @brief A functions parameter declaration.
*
*
*/
typedef struct ParameterDeclaration_t {
Type *type;
@ -172,7 +172,7 @@ typedef struct ParameterDeclaration_t {
/**
* @brief A functions parameter.
*
*
*/
typedef struct ParameterDefinition_t {
ParameterDeclaration declaration;
@ -189,11 +189,11 @@ typedef enum ParameterKind_t {
/**
* @brief A parameter can either be a declaration or a definition
*
*
*/
typedef struct Parameter_t {
const char* name;
ParameterKind kind;
union ParameterImplementation {
ParameterDeclaration declaration;
@ -254,9 +254,9 @@ typedef struct VariableDeclaration_t {
/**
* @brief Definition of a variable
*
*
* @attention NOTE: The types of the initializer and the declaration must be equal
*
*
*/
typedef struct VariableDefiniton_t {
VariableDeclaration declaration;
@ -281,17 +281,28 @@ 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 |
// '------------------------------------------------'
/**
* @brief Perform a type cast, converting a value to different type whilest preserving as much of the original
* values information.
* values information.
*
* @attention NOTE: Must check wether the given value's type can be parsed into
* the target type without loss.
* Lossy mean possibly loosing information such when casting a float into an int (no fraction anymore).
* Lossy mean possibly loosing information such when casting a float into an int (no fraction anymore).
*
*/
typedef struct TypeCast_t {
@ -305,7 +316,7 @@ typedef struct TypeCast_t {
*
* @attention NOTE: The given value's type must have the size in bytes as the target type.
* Transmuting a short int into a float should yield an error.
*
*
*/
typedef struct Transmute_t {
Type *targetType;
@ -319,7 +330,7 @@ typedef struct Transmute_t {
/**
* @brief Represents the arithmetic operator.
*
*
*/
typedef enum ArithmeticOperator_t {
Add,
@ -335,7 +346,7 @@ typedef enum ArithmeticOperator_t {
/**
* @brief Represents the relational operator.
*
*
*/
typedef enum RelationalOperator_t {
Equal,
@ -395,7 +406,7 @@ typedef struct Operation_t {
union OperationImplementation {
ArithmeticOperator arithmetic;
RelationalOperator relational;
BooleanOperator boolean;
BooleanOperator boolean;
LogicalOperator logical;
BitwiseOperator bitwise;
} impl;
@ -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);