added test for typedef

This commit is contained in:
Sven Vogel 2024-05-21 00:41:03 +02:00
parent 17e2cd7110
commit cc1dc790e1
6 changed files with 180 additions and 4 deletions

View File

@ -68,7 +68,7 @@ enum Scale_t collapse_scale_list(const AST_NODE_PTR list, double base) {
enum Sign_t string_to_sign(const char* keyword) {
if (strcmp(keyword, "signed") == 0) {
return Signed;
} else if (strcmp(keyword, "signed") == 0) {
} else if (strcmp(keyword, "unsigned") == 0) {
return Unsigned;
}
@ -139,7 +139,7 @@ struct CompositeType_t ast_type_to_composite(const TypeScopeRef scope, AST_NODE_
}
} else if (count == 3) {
const char* typename = AST_get_node(type, 3)->value;
const char* typename = AST_get_node(type, 2)->value;
GemstoneTypedefRef known_type = type_scope_get_type_from_name(scope, typename);
if (known_type == NULL) {
@ -156,7 +156,6 @@ struct CompositeType_t ast_type_to_composite(const TypeScopeRef scope, AST_NODE_
// sign, scale and type
composite.sign = string_to_sign(AST_get_node(type, 0)->value);
composite.scale = collapse_scale_list(AST_get_node(type, 1), (double) composite.scale);
composite.prim = resolve_primitive(AST_get_node(type, 2)->value);
}
return composite;

View File

@ -10,3 +10,4 @@ add_subdirectory(logging)
add_subdirectory(input_file)
add_subdirectory(ast)
add_subdirectory(glib)
add_subdirectory(llvm)

View File

@ -0,0 +1,5 @@
include(CTest)
# Provide test to run here or include another CMakeLists.txt
add_subdirectory(typedef)

View File

@ -0,0 +1,51 @@
include(CTest)
# ------------------------------------------------ #
# Setup Glib 2.0 #
# ------------------------------------------------ #
find_package(PkgConfig REQUIRED)
pkg_search_module(GLIB REQUIRED IMPORTED_TARGET glib-2.0)
# ------------------------------------------------ #
# LLVM backend #
# ------------------------------------------------ #
# Fetch LLVM link configuration
execute_process(COMMAND llvm-config --libs all
OUTPUT_VARIABLE LLVM_LIBS)
# Strip whitespace from output
string(STRIP "${LLVM_LIBS}" LLVM_LIBS)
# Link all targets to LLVM
link_libraries(${LLVM_LIBS})
# ------------------------------------------------ #
# Source #
# ------------------------------------------------ #
include_directories(${PROJECT_SOURCE_DIR}/src)
include_directories(PRIVATE ${GLIB_INCLUDE_DIRS})
file(GLOB_RECURSE SOURCE_FILES ${PROJECT_SOURCE_DIR}/src/*.c)
list(REMOVE_ITEM SOURCE_FILES ${PROJECT_SOURCE_DIR}/src/main.c)
set(LEX_GENERATED_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/lex/lexer.ll.c)
set(YACC_GENERATED_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/yacc/parser.tab.c)
# ------------------------------------------------------- #
# CTEST 1
# test typedef
add_executable(typedef
${SOURCE_FILES}
${LEX_GENERATED_SOURCE_FILE}
${YACC_GENERATED_SOURCE_FILE}
typedef.c)
set_target_properties(typedef
PROPERTIES
OUTPUT_NAME "typedef"
RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/llvm)
target_link_libraries(typedef PkgConfig::GLIB)
add_test(NAME typedef
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/tests/llvm/typedef
COMMAND ${GEMSTONE_BINARY_DIR}/tests/llvm/typedef test.txt)

View File

@ -0,0 +1,6 @@
type unsigned half half int: u8
type unsigned half int: u16
type unsigned int: u32
type unsigned double int: u64
type unsigned double double int: u128

View File

@ -0,0 +1,114 @@
#include "llvm/types/scope.h"
#include <ast/ast.h>
#include <stdlib.h>
#include <sys/log.h>
#include <yacc/parser.tab.h>
#include <sys/col.h>
#include <lex/util.h>
#include <llvm/backend.h>
#include <codegen/backend.h>
#include <llvm/types/type.h>
#define LOG_LEVEL LOG_LEVEL_DEBUG
extern FILE *yyin;
AST_NODE_PTR root;
/**
* @brief Log a debug message to inform about beginning exit procedures
*
*/
void notify_exit(void) { DEBUG("Exiting gemstone..."); }
/**
* @brief Closes File after compiling.
*
*/
void close_file(void) {
if (NULL != yyin) {
fclose(yyin);
}
}
/**
* @brief Run compiler setup here
*
*/
void setup(void) {
// setup preample
log_init();
DEBUG("starting gemstone...");
#if LOG_LEVEL <= LOG_LEVEL_DEBUG
atexit(&notify_exit);
#endif
// actual setup
AST_init();
col_init();
lex_init();
DEBUG("finished starting up gemstone...");
}
void run_backend_codegen() {
llvm_backend_init();
BackendError err;
err = init_backend();
if (err.kind != Success) {
return;
}
void* code = NULL;
err = generate_code(root, &code);
if (err.kind != Success) {
return;
}
err = deinit_backend();
}
void check_typedef(const AST_NODE_PTR node) {
TypeScopeRef scope = type_scope_new();
GemstoneTypedefRef typdef = get_type_def_from_ast(scope, node);
type_scope_delete(scope);
}
int main(int argc, char *argv[]) {
setup();
atexit(close_file);
// Check for file input as argument
if (2 != argc) {
INFO("Usage: %s <filename>\n", argv[0]);
PANIC("No File could be found");
}
// filename as first argument
char *filename = argv[1];
FILE *file = fopen(filename, "r");
if (NULL == file) {
PANIC("File couldn't be opened!");
}
yyin = file;
root = AST_new_node(AST_Module, NULL);
yyparse();
for (size_t i = 0; i < root->child_count; i++) {
check_typedef(root->children[i]);
}
AST_delete_node(root);
return 0;
}