diff --git a/src/ptlang_ast/ptlang_ast.c b/src/ptlang_ast/ptlang_ast.c index 7212f80..b086cb4 100644 --- a/src/ptlang_ast/ptlang_ast.c +++ b/src/ptlang_ast/ptlang_ast.c @@ -1226,6 +1226,7 @@ ptlang_utils_str ptlang_ast_exp_to_string(ptlang_ast_exp exp) case PTLANG_AST_EXP_BINARY: abort(); } + return ALLOCATED_STR(str); } static char *ptlang_ast_exp_type_get_symbol(ptlang_ast_exp exp) @@ -1287,4 +1288,4 @@ static char *ptlang_ast_exp_type_get_symbol(ptlang_ast_exp exp) default: abort(); } -} \ No newline at end of file +} diff --git a/src/ptlang_ast/ptlang_ast.h b/src/ptlang_ast/ptlang_ast.h index ffe6833..a910894 100644 --- a/src/ptlang_ast/ptlang_ast.h +++ b/src/ptlang_ast/ptlang_ast.h @@ -1,49 +1,13 @@ #ifndef PTLANG_AST_H #define PTLANG_AST_H +#include "ptlang_ast_nodes.h" #include "ptlang_utils.h" #include #include #include #include -typedef struct ptlang_ast_code_position_s -{ - uint64_t from_line; - uint64_t from_column; - uint64_t to_line; - uint64_t to_column; -} ptlang_ast_code_position_s; - -typedef struct ptlang_ast_code_position_s *ptlang_ast_code_position; - -typedef struct ptlang_ast_ident_s -{ - char *name; - ptlang_ast_code_position pos; -} ptlang_ast_ident; - -typedef struct ptlang_ast_type_s *ptlang_ast_type; -typedef struct ptlang_ast_stmt_s *ptlang_ast_stmt; -typedef struct ptlang_ast_module_s *ptlang_ast_module; -typedef struct ptlang_ast_func_s *ptlang_ast_func; -typedef struct ptlang_ast_exp_s *ptlang_ast_exp; -typedef struct ptlang_ast_decl_s *ptlang_ast_decl; -typedef struct ptlang_ast_struct_def_s *ptlang_ast_struct_def; -typedef struct ptlang_ast_struct_member_s *ptlang_ast_struct_member_list; -// typedef struct ptlang_ast_decl_list_s *ptlang_ast_decl_list; -// typedef struct ptlang_ast_type_list_s *ptlang_ast_type_list; -// typedef struct ptlang_ast_exp_list_s *ptlang_ast_exp_list; -// typedef struct ptlang_ast_str_exp_list_s *ptlang_ast_str_exp_list; - -enum ptlang_ast_type_float_size -{ - PTLANG_AST_TYPE_FLOAT_16 = 16, - PTLANG_AST_TYPE_FLOAT_32 = 32, - PTLANG_AST_TYPE_FLOAT_64 = 64, - PTLANG_AST_TYPE_FLOAT_128 = 128, -}; - ptlang_ast_struct_def ptlang_ast_struct_def_new(ptlang_ast_ident name, ptlang_ast_decl *members, ptlang_ast_code_position pos); diff --git a/src/ptlang_ast/ptlang_ast_impl.h b/src/ptlang_ast/ptlang_ast_impl.h index 0024fa3..b4c657f 100644 --- a/src/ptlang_ast/ptlang_ast_impl.h +++ b/src/ptlang_ast/ptlang_ast_impl.h @@ -11,295 +11,6 @@ #include #include -struct ptlang_ast_struct_def_s -{ - ptlang_ast_ident name; - ptlang_ast_decl *members; - ptlang_ast_code_position pos; -}; - -struct ptlang_ast_module_type_alias_s -{ - ptlang_ast_ident name; - ptlang_ast_type type; - ptlang_ast_code_position pos; -}; - -struct ptlang_ast_module_s -{ - ptlang_ast_func *functions; - ptlang_ast_decl *declarations; - ptlang_ast_struct_def *struct_defs; - struct ptlang_ast_module_type_alias_s *type_aliases; -}; - -struct ptlang_ast_decl_s -{ - ptlang_ast_type type; - ptlang_ast_exp init; - ptlang_ast_ident name; - bool writable; - bool export; - ptlang_ast_code_position pos; -}; - -// struct ptlang_ast_decl_list_s -// { -// uint64_t count; -// ptlang_ast_decl *decls; -// }; - -// struct ptlang_ast_type_list_s -// { -// uint64_t count; -// ptlang_ast_type *types; -// }; - -// struct ptlang_ast_exp_list_s -// { -// uint64_t count; -// ptlang_ast_exp *exps; -// } -// ; - -struct ptlang_ast_struct_member_s -{ - ptlang_ast_ident str; - ptlang_ast_exp exp; - ptlang_ast_code_position pos; -}; - -// struct ptlang_ast_str_exp_list_s -// { -// uint64_t count; -// struct ptlang_ast_str_exp_s *str_exps; -// }; - -struct ptlang_ast_stmt_block_s -{ - ptlang_ast_stmt *stmts; -}; - -struct ptlang_ast_stmt_control_flow_s -{ - ptlang_ast_exp condition; - ptlang_ast_stmt stmt; -}; - -struct ptlang_ast_stmt_control_flow2_s -{ - ptlang_ast_exp condition; - ptlang_ast_stmt stmt[2]; -}; - -struct ptlang_ast_stmt_s -{ - enum - { - PTLANG_AST_STMT_BLOCK, - PTLANG_AST_STMT_EXP, - PTLANG_AST_STMT_DECL, - PTLANG_AST_STMT_IF, - PTLANG_AST_STMT_IF_ELSE, - PTLANG_AST_STMT_WHILE, - PTLANG_AST_STMT_RETURN, - PTLANG_AST_STMT_RET_VAL, - PTLANG_AST_STMT_BREAK, - PTLANG_AST_STMT_CONTINUE, - } type; - union - { - struct ptlang_ast_stmt_block_s block; - ptlang_ast_exp exp; - ptlang_ast_decl decl; - struct ptlang_ast_stmt_control_flow_s control_flow; - struct ptlang_ast_stmt_control_flow2_s control_flow2; - uint64_t nesting_level; - } content; - ptlang_ast_code_position pos; -}; - -struct ptlang_ast_exp_binary_operator_s -{ - ptlang_ast_exp left_value; - ptlang_ast_exp right_value; -}; - -struct ptlang_ast_exp_function_call_s -{ - ptlang_ast_exp function; - ptlang_ast_exp *parameters; -}; - -struct ptlang_ast_exp_ternary_operator_s -{ - ptlang_ast_exp condition; - ptlang_ast_exp if_value; - ptlang_ast_exp else_value; -}; - -struct ptlang_ast_exp_cast_s -{ - ptlang_ast_type type; - ptlang_ast_exp value; -}; - -struct ptlang_ast_exp_struct_s -{ - ptlang_ast_ident type; - ptlang_ast_struct_member_list members; -}; - -struct ptlang_ast_exp_array_s -{ - ptlang_ast_type type; - ptlang_ast_exp *values; -}; - -struct ptlang_ast_exp_struct_member_s -{ - ptlang_ast_exp struct_; - ptlang_ast_ident member_name; -}; - -struct ptlang_ast_exp_array_element_s -{ - ptlang_ast_exp array; - ptlang_ast_exp index; -}; - -struct ptlang_ast_exp_reference_s -{ - bool writable; - ptlang_ast_exp value; -}; - -struct ptlang_ast_exp_s -{ - enum - { - PTLANG_AST_EXP_ASSIGNMENT, - PTLANG_AST_EXP_ADDITION, - PTLANG_AST_EXP_SUBTRACTION, - PTLANG_AST_EXP_NEGATION, - PTLANG_AST_EXP_MULTIPLICATION, - PTLANG_AST_EXP_DIVISION, - PTLANG_AST_EXP_MODULO, - PTLANG_AST_EXP_REMAINDER, - PTLANG_AST_EXP_EQUAL, - PTLANG_AST_EXP_NOT_EQUAL, - PTLANG_AST_EXP_GREATER, - PTLANG_AST_EXP_GREATER_EQUAL, - PTLANG_AST_EXP_LESS, - PTLANG_AST_EXP_LESS_EQUAL, - PTLANG_AST_EXP_LEFT_SHIFT, - PTLANG_AST_EXP_RIGHT_SHIFT, - PTLANG_AST_EXP_AND, - PTLANG_AST_EXP_OR, - PTLANG_AST_EXP_NOT, - PTLANG_AST_EXP_BITWISE_AND, - PTLANG_AST_EXP_BITWISE_OR, - PTLANG_AST_EXP_BITWISE_XOR, - PTLANG_AST_EXP_BITWISE_INVERSE, // xor -1 - PTLANG_AST_EXP_LENGTH, - PTLANG_AST_EXP_FUNCTION_CALL, - PTLANG_AST_EXP_VARIABLE, - PTLANG_AST_EXP_INTEGER, - PTLANG_AST_EXP_FLOAT, - PTLANG_AST_EXP_STRUCT, - PTLANG_AST_EXP_ARRAY, - PTLANG_AST_EXP_TERNARY, - PTLANG_AST_EXP_CAST, - PTLANG_AST_EXP_STRUCT_MEMBER, - PTLANG_AST_EXP_ARRAY_ELEMENT, - PTLANG_AST_EXP_REFERENCE, - PTLANG_AST_EXP_DEREFERENCE, - PTLANG_AST_EXP_BINARY, - } type; - union - { - struct ptlang_ast_exp_binary_operator_s binary_operator; - ptlang_ast_exp unary_operator; - struct ptlang_ast_exp_function_call_s function_call; - struct ptlang_ast_exp_ternary_operator_s ternary_operator; - char *str_prepresentation; - struct ptlang_ast_exp_struct_s struct_; - struct ptlang_ast_exp_array_s array; - struct ptlang_ast_exp_cast_s cast; - struct ptlang_ast_exp_struct_member_s struct_member; - struct ptlang_ast_exp_array_element_s array_element; - struct ptlang_ast_exp_reference_s reference; - uint8_t *binary; - } content; - ptlang_ast_code_position pos; - ptlang_ast_type ast_type; // use only after verify -}; - -struct ptlang_ast_type_integer_s -{ - bool is_signed; - uint32_t size; -}; - -struct ptlang_ast_type_function_s -{ - ptlang_ast_type return_type; - ptlang_ast_type *parameters; -}; - -struct ptlang_ast_type_heap_array_s -{ - ptlang_ast_type type; -}; - -struct ptlang_ast_type_array_s -{ - ptlang_ast_type type; - uint64_t len; -}; - -struct ptlang_ast_type_reference_s -{ - ptlang_ast_type type; - bool writable; -}; - -struct ptlang_ast_type_s -{ - enum - { - PTLANG_AST_TYPE_VOID, - PTLANG_AST_TYPE_INTEGER, - PTLANG_AST_TYPE_FLOAT, - PTLANG_AST_TYPE_FUNCTION, - PTLANG_AST_TYPE_HEAP_ARRAY, - PTLANG_AST_TYPE_ARRAY, - PTLANG_AST_TYPE_REFERENCE, - PTLANG_AST_TYPE_NAMED, - } type; - union - { - struct ptlang_ast_type_integer_s integer; - enum ptlang_ast_type_float_size float_size; - struct ptlang_ast_type_function_s function; - struct ptlang_ast_type_heap_array_s heap_array; - struct ptlang_ast_type_array_s array; - struct ptlang_ast_type_reference_s reference; - char *name; - } content; - ptlang_ast_code_position pos; -}; - -struct ptlang_ast_func_s -{ - ptlang_ast_ident name; - ptlang_ast_type return_type; - ptlang_ast_decl *parameters; - ptlang_ast_stmt stmt; - bool export; - ptlang_ast_code_position pos; -}; - static char *ptlang_ast_exp_type_get_symbol(ptlang_ast_exp exp); #endif diff --git a/src/ptlang_ast/ptlang_ast_nodes.h b/src/ptlang_ast/ptlang_ast_nodes.h new file mode 100644 index 0000000..6f51dde --- /dev/null +++ b/src/ptlang_ast/ptlang_ast_nodes.h @@ -0,0 +1,306 @@ +#ifndef PTLANG_AST_NODES_H +#define PTLANG_AST_NODES_H + +#include +#include + + +typedef struct ptlang_ast_type_s *ptlang_ast_type; +typedef struct ptlang_ast_stmt_s *ptlang_ast_stmt; +typedef struct ptlang_ast_module_s *ptlang_ast_module; +typedef struct ptlang_ast_func_s *ptlang_ast_func; +typedef struct ptlang_ast_exp_s *ptlang_ast_exp; +typedef struct ptlang_ast_decl_s *ptlang_ast_decl; +typedef struct ptlang_ast_struct_def_s *ptlang_ast_struct_def; +typedef struct ptlang_ast_struct_member_s *ptlang_ast_struct_member_list; + + +typedef struct ptlang_ast_code_position_s +{ + uint64_t from_line; + uint64_t from_column; + uint64_t to_line; + uint64_t to_column; +} ptlang_ast_code_position_s; + +typedef struct ptlang_ast_code_position_s *ptlang_ast_code_position; + +typedef struct ptlang_ast_ident_s +{ + char *name; + ptlang_ast_code_position pos; +} ptlang_ast_ident; + +enum ptlang_ast_type_float_size +{ + PTLANG_AST_TYPE_FLOAT_16 = 16, + PTLANG_AST_TYPE_FLOAT_32 = 32, + PTLANG_AST_TYPE_FLOAT_64 = 64, + PTLANG_AST_TYPE_FLOAT_128 = 128, +}; + +struct ptlang_ast_struct_def_s +{ + ptlang_ast_ident name; + ptlang_ast_decl *members; + ptlang_ast_code_position pos; +}; + +struct ptlang_ast_module_type_alias_s +{ + ptlang_ast_ident name; + ptlang_ast_type type; + ptlang_ast_code_position pos; +}; + +struct ptlang_ast_module_s +{ + ptlang_ast_func *functions; + ptlang_ast_decl *declarations; + ptlang_ast_struct_def *struct_defs; + struct ptlang_ast_module_type_alias_s *type_aliases; +}; + +struct ptlang_ast_decl_s +{ + ptlang_ast_type type; + ptlang_ast_exp init; + ptlang_ast_ident name; + bool writable; + bool export; + ptlang_ast_code_position pos; +}; + + +struct ptlang_ast_struct_member_s +{ + ptlang_ast_ident str; + ptlang_ast_exp exp; + ptlang_ast_code_position pos; +}; +struct ptlang_ast_stmt_block_s +{ + ptlang_ast_stmt *stmts; +}; + +struct ptlang_ast_stmt_control_flow_s +{ + ptlang_ast_exp condition; + ptlang_ast_stmt stmt; +}; + +struct ptlang_ast_stmt_control_flow2_s +{ + ptlang_ast_exp condition; + ptlang_ast_stmt stmt[2]; +}; + +struct ptlang_ast_stmt_s +{ + enum + { + PTLANG_AST_STMT_BLOCK, + PTLANG_AST_STMT_EXP, + PTLANG_AST_STMT_DECL, + PTLANG_AST_STMT_IF, + PTLANG_AST_STMT_IF_ELSE, + PTLANG_AST_STMT_WHILE, + PTLANG_AST_STMT_RETURN, + PTLANG_AST_STMT_RET_VAL, + PTLANG_AST_STMT_BREAK, + PTLANG_AST_STMT_CONTINUE, + } type; + union + { + struct ptlang_ast_stmt_block_s block; + ptlang_ast_exp exp; + ptlang_ast_decl decl; + struct ptlang_ast_stmt_control_flow_s control_flow; + struct ptlang_ast_stmt_control_flow2_s control_flow2; + uint64_t nesting_level; + } content; + ptlang_ast_code_position pos; +}; + +struct ptlang_ast_exp_binary_operator_s +{ + ptlang_ast_exp left_value; + ptlang_ast_exp right_value; +}; + +struct ptlang_ast_exp_function_call_s +{ + ptlang_ast_exp function; + ptlang_ast_exp *parameters; +}; + +struct ptlang_ast_exp_ternary_operator_s +{ + ptlang_ast_exp condition; + ptlang_ast_exp if_value; + ptlang_ast_exp else_value; +}; + +struct ptlang_ast_exp_cast_s +{ + ptlang_ast_type type; + ptlang_ast_exp value; +}; + +struct ptlang_ast_exp_struct_s +{ + ptlang_ast_ident type; + ptlang_ast_struct_member_list members; +}; + +struct ptlang_ast_exp_array_s +{ + ptlang_ast_type type; + ptlang_ast_exp *values; +}; + +struct ptlang_ast_exp_struct_member_s +{ + ptlang_ast_exp struct_; + ptlang_ast_ident member_name; +}; + +struct ptlang_ast_exp_array_element_s +{ + ptlang_ast_exp array; + ptlang_ast_exp index; +}; + +struct ptlang_ast_exp_reference_s +{ + bool writable; + ptlang_ast_exp value; +}; + +struct ptlang_ast_exp_s +{ + enum + { + PTLANG_AST_EXP_ASSIGNMENT, + PTLANG_AST_EXP_ADDITION, + PTLANG_AST_EXP_SUBTRACTION, + PTLANG_AST_EXP_NEGATION, + PTLANG_AST_EXP_MULTIPLICATION, + PTLANG_AST_EXP_DIVISION, + PTLANG_AST_EXP_MODULO, + PTLANG_AST_EXP_REMAINDER, + PTLANG_AST_EXP_EQUAL, + PTLANG_AST_EXP_NOT_EQUAL, + PTLANG_AST_EXP_GREATER, + PTLANG_AST_EXP_GREATER_EQUAL, + PTLANG_AST_EXP_LESS, + PTLANG_AST_EXP_LESS_EQUAL, + PTLANG_AST_EXP_LEFT_SHIFT, + PTLANG_AST_EXP_RIGHT_SHIFT, + PTLANG_AST_EXP_AND, + PTLANG_AST_EXP_OR, + PTLANG_AST_EXP_NOT, + PTLANG_AST_EXP_BITWISE_AND, + PTLANG_AST_EXP_BITWISE_OR, + PTLANG_AST_EXP_BITWISE_XOR, + PTLANG_AST_EXP_BITWISE_INVERSE, // xor -1 + PTLANG_AST_EXP_LENGTH, + PTLANG_AST_EXP_FUNCTION_CALL, + PTLANG_AST_EXP_VARIABLE, + PTLANG_AST_EXP_INTEGER, + PTLANG_AST_EXP_FLOAT, + PTLANG_AST_EXP_STRUCT, + PTLANG_AST_EXP_ARRAY, + PTLANG_AST_EXP_TERNARY, + PTLANG_AST_EXP_CAST, + PTLANG_AST_EXP_STRUCT_MEMBER, + PTLANG_AST_EXP_ARRAY_ELEMENT, + PTLANG_AST_EXP_REFERENCE, + PTLANG_AST_EXP_DEREFERENCE, + PTLANG_AST_EXP_BINARY, + } type; + union + { + struct ptlang_ast_exp_binary_operator_s binary_operator; + ptlang_ast_exp unary_operator; + struct ptlang_ast_exp_function_call_s function_call; + struct ptlang_ast_exp_ternary_operator_s ternary_operator; + char *str_prepresentation; + struct ptlang_ast_exp_struct_s struct_; + struct ptlang_ast_exp_array_s array; + struct ptlang_ast_exp_cast_s cast; + struct ptlang_ast_exp_struct_member_s struct_member; + struct ptlang_ast_exp_array_element_s array_element; + struct ptlang_ast_exp_reference_s reference; + uint8_t *binary; + } content; + ptlang_ast_code_position pos; + ptlang_ast_type ast_type; // use only after verify +}; + +struct ptlang_ast_type_integer_s +{ + bool is_signed; + uint32_t size; +}; + +struct ptlang_ast_type_function_s +{ + ptlang_ast_type return_type; + ptlang_ast_type *parameters; +}; + +struct ptlang_ast_type_heap_array_s +{ + ptlang_ast_type type; +}; + +struct ptlang_ast_type_array_s +{ + ptlang_ast_type type; + uint64_t len; +}; + +struct ptlang_ast_type_reference_s +{ + ptlang_ast_type type; + bool writable; +}; + +struct ptlang_ast_type_s +{ + enum + { + PTLANG_AST_TYPE_VOID, + PTLANG_AST_TYPE_INTEGER, + PTLANG_AST_TYPE_FLOAT, + PTLANG_AST_TYPE_FUNCTION, + PTLANG_AST_TYPE_HEAP_ARRAY, + PTLANG_AST_TYPE_ARRAY, + PTLANG_AST_TYPE_REFERENCE, + PTLANG_AST_TYPE_NAMED, + } type; + union + { + struct ptlang_ast_type_integer_s integer; + enum ptlang_ast_type_float_size float_size; + struct ptlang_ast_type_function_s function; + struct ptlang_ast_type_heap_array_s heap_array; + struct ptlang_ast_type_array_s array; + struct ptlang_ast_type_reference_s reference; + char *name; + } content; + ptlang_ast_code_position pos; +}; + +struct ptlang_ast_func_s +{ + ptlang_ast_ident name; + ptlang_ast_type return_type; + ptlang_ast_decl *parameters; + ptlang_ast_stmt stmt; + bool export; + ptlang_ast_code_position pos; +}; + +#endif diff --git a/src/ptlang_eval/ptlang_eval.h b/src/ptlang_eval/ptlang_eval.h index 5ba4115..7206fdd 100644 --- a/src/ptlang_eval/ptlang_eval.h +++ b/src/ptlang_eval/ptlang_eval.h @@ -1,6 +1,6 @@ #pragma once -#include "ptlang_ast_impl.h" +#include "ptlang_ast_nodes.h" #include // typedef ptlang_eval_value; diff --git a/src/ptlang_ir_builder/ptlang_ir_builder_impl.h b/src/ptlang_ir_builder/ptlang_ir_builder_impl.h index 67c4d40..8e18c61 100644 --- a/src/ptlang_ir_builder/ptlang_ir_builder_impl.h +++ b/src/ptlang_ir_builder/ptlang_ir_builder_impl.h @@ -4,7 +4,7 @@ #include #include -#include "ptlang_ast_impl.h" +#include "ptlang_ast_nodes.h" #include "ptlang_utils.h" // #include "" diff --git a/src/ptlang_main/main.c b/src/ptlang_main/main.c index e01ae79..e3aadfd 100644 --- a/src/ptlang_main/main.c +++ b/src/ptlang_main/main.c @@ -7,6 +7,7 @@ #include #include #include +#include void print_error(ptlang_error error) { @@ -62,61 +63,61 @@ int main(void) ptlang_context_destory(&ctx); - LLVMModuleRef llvmmod = ptlang_ir_builder_module(mod, target_data_layout); - LLVMDumpModule(llvmmod); - LLVMDisposeTargetData(target_data_layout); - char *error = NULL; - LLVMVerifyModule(llvmmod, LLVMAbortProcessAction, &error); - LLVMDisposeMessage(error); + // LLVMModuleRef llvmmod = ptlang_ir_builder_module(mod, target_data_layout); + // LLVMDumpModule(llvmmod); + // LLVMDisposeTargetData(target_data_layout); + // char *error = NULL; + // LLVMVerifyModule(llvmmod, LLVMAbortProcessAction, &error); + // LLVMDisposeMessage(error); - LLVMSetTarget(llvmmod, triple); + // LLVMSetTarget(llvmmod, triple); LLVMDisposeMessage(triple); - printf("\n ============= unopt =============\n\n"); + // printf("\n ============= unopt =============\n\n"); - LLVMDumpModule(llvmmod); + // LLVMDumpModule(llvmmod); - // #ifndef NOPT - LLVMPassBuilderOptionsRef pbo = LLVMCreatePassBuilderOptions(); + // // #ifndef NOPT + // LLVMPassBuilderOptionsRef pbo = LLVMCreatePassBuilderOptions(); - LLVMErrorRef err = LLVMRunPasses(llvmmod, "default", machine, pbo); - // err = LLVMRunPasses(llvmmod, "default", machine, pbo); + // LLVMErrorRef err = LLVMRunPasses(llvmmod, "default", machine, pbo); + // // err = LLVMRunPasses(llvmmod, "default", machine, pbo); - LLVMDisposePassBuilderOptions(pbo); + // LLVMDisposePassBuilderOptions(pbo); - if (err != LLVMErrorSuccess) - { - fprintf(stderr, "ERROR lll.c Just search this string asdfghjkl\n"); - fprintf(stderr, "%s\n", LLVMGetErrorMessage(err)); - } + // if (err != LLVMErrorSuccess) + // { + // fprintf(stderr, "ERROR lll.c Just search this string asdfghjkl\n"); + // fprintf(stderr, "%s\n", LLVMGetErrorMessage(err)); + // } - // #endif + // // #endif - printf("\n ============== opt ==============\n\n"); + // printf("\n ============== opt ==============\n\n"); - // printf("%p", err); + // // printf("%p", err); - LLVMDumpModule(llvmmod); + // LLVMDumpModule(llvmmod); - printf("\n ============== end ==============\n\n"); + // printf("\n ============== end ==============\n\n"); -#ifdef WIN32 -# define ASM_FILE "t.asm" -# define OBJ_FILE "t.obj" -#else -# define ASM_FILE "t.S" -# define OBJ_FILE "t.o" -#endif +// #ifdef WIN32 +// # define ASM_FILE "t.asm" +// # define OBJ_FILE "t.obj" +// #else +// # define ASM_FILE "t.S" +// # define OBJ_FILE "t.o" +// #endif - LLVMInitializeNativeAsmPrinter(); + // LLVMInitializeNativeAsmPrinter(); - LLVMTargetMachineEmitToFile(machine, llvmmod, ASM_FILE, LLVMAssemblyFile, &error); - LLVMTargetMachineEmitToFile(machine, llvmmod, OBJ_FILE, LLVMObjectFile, &error); + // LLVMTargetMachineEmitToFile(machine, llvmmod, ASM_FILE, LLVMAssemblyFile, &error); + // LLVMTargetMachineEmitToFile(machine, llvmmod, OBJ_FILE, LLVMObjectFile, &error); LLVMDisposeTargetMachine(machine); ptlang_ast_module_destroy(mod); - LLVMDisposeModule(llvmmod); + // LLVMDisposeModule(llvmmod); } diff --git a/src/ptlang_main/ptlang_context.c b/src/ptlang_main/ptlang_context.c index 803697b..4a765b6 100644 --- a/src/ptlang_main/ptlang_context.c +++ b/src/ptlang_main/ptlang_context.c @@ -1,6 +1,10 @@ -#include "ptlang_context.h" +#include "ptlang_context_impl.h" -void ptlang_context_destory(ptlang_context *ctx) { shfree(ctx->type_scope); } +void ptlang_context_destory(ptlang_context *ctx) +{ + arrfree(ctx->scope); + shfree(ctx->type_scope); +} ptlang_ast_type ptlang_context_unname_type(ptlang_ast_type type, ptlang_context_type_scope *type_scope) { diff --git a/src/ptlang_main/ptlang_context.h b/src/ptlang_main/ptlang_context.h index e83a7ac..f499007 100644 --- a/src/ptlang_main/ptlang_context.h +++ b/src/ptlang_main/ptlang_context.h @@ -1,7 +1,7 @@ #ifndef PTLANG_CONTEXT_H #define PTLANG_CONTEXT_H -#include "ptlang_ast_impl.h" +#include "ptlang_ast.h" #include typedef struct ptlang_context_type_scope_entry_s diff --git a/src/ptlang_main/ptlang_context_impl.h b/src/ptlang_main/ptlang_context_impl.h new file mode 100644 index 0000000..0ac770c --- /dev/null +++ b/src/ptlang_main/ptlang_context_impl.h @@ -0,0 +1,8 @@ +#ifndef PTLANG_CONTEXT_IMPL_H +#define PTLANG_CONTEXT_IMPL_H + +#include "ptlang_context.h" +#include +#include + +#endif diff --git a/src/ptlang_parser/ptlang_parser.c b/src/ptlang_parser/ptlang_parser.c index cb877f6..c3f168f 100644 --- a/src/ptlang_parser/ptlang_parser.c +++ b/src/ptlang_parser/ptlang_parser.c @@ -3,7 +3,7 @@ // ptlang_ast_module *ptlang_parser_module_out; // ptlang_error *syntax_errors; -void ptlang_yyerror(const PTLANG_YYLTYPE *yylloc, ptlang_ast_module *out, ptlang_error **syntax_errors, +void ptlang_yyerror(const PTLANG_YYLTYPE *yylloc, ptlang_ast_module out, ptlang_error **syntax_errors, char const *message) { // fprintf(stderr, "error from %d:%d to %d:%d : %s\n", yylloc->first_line, yylloc->first_column, diff --git a/src/ptlang_parser/ptlang_parser_impl.h b/src/ptlang_parser/ptlang_parser_impl.h index 9a86232..b71234d 100644 --- a/src/ptlang_parser/ptlang_parser_impl.h +++ b/src/ptlang_parser/ptlang_parser_impl.h @@ -24,7 +24,7 @@ # include "lexer.h" #endif -void ptlang_yyerror(const PTLANG_YYLTYPE *yylloc, ptlang_ast_module *out, ptlang_error **syntax_errors, +void ptlang_yyerror(const PTLANG_YYLTYPE *yylloc, ptlang_ast_module out, ptlang_error **syntax_errors, char const *message); // str: [sSuU][1-9][0-9]{0,6} diff --git a/src/ptlang_utils/ptlang_utils.h b/src/ptlang_utils/ptlang_utils.h index 22d9cd6..46e09dc 100644 --- a/src/ptlang_utils/ptlang_utils.h +++ b/src/ptlang_utils/ptlang_utils.h @@ -78,4 +78,6 @@ void ptlang_utils_graph_free(ptlang_utils_graph_node *graph); ptlang_utils_str ptlang_utils_sprintf_alloc(const char *fmt, ...); +char *ptlang_utils_build_str_from_str_arr(ptlang_utils_str *strings); + #endif diff --git a/src/ptlang_verify/ptlang_verify.c b/src/ptlang_verify/ptlang_verify.c index 2f6be64..b59053c 100644 --- a/src/ptlang_verify/ptlang_verify.c +++ b/src/ptlang_verify/ptlang_verify.c @@ -25,6 +25,8 @@ ptlang_error *ptlang_verify_module(ptlang_ast_module module, ptlang_context *ctx for (size_t i = 0; i < arrlenu(module->declarations); i++) { ptlang_verify_decl(module->declarations[i], 0, ctx, &errors); + if (module->declarations[i]->init != NULL) + ptlang_verify_exp_check_const(module->declarations[i]->init, ctx, &errors); } ptlang_verify_eval_globals(module, ctx, &errors); ptlang_verify_functions(module->functions, ctx, &errors); @@ -258,7 +260,6 @@ static void ptlang_verify_decl_header(ptlang_ast_decl decl, size_t scope_offset, if (correct) { - arrpush(ctx->scope, decl); } } @@ -1763,7 +1764,7 @@ static ptlang_ast_exp ptlang_verify_eval(ptlang_ast_exp exp, bool eval_fully, pt ptlang_utils_graph_node *nodes, ptlang_verify_node_table node_table, ptlang_ast_module module, ptlang_context *ctx, ptlang_error **errors) { - ptlang_ast_exp substituted = ptlang_malloc(sizeof(struct ptlang_ast_exp_s)); + ptlang_ast_exp substituted = NULL; ptlang_ast_exp evaluated = NULL; switch (exp->type) { @@ -1791,6 +1792,7 @@ static ptlang_ast_exp ptlang_verify_eval(ptlang_ast_exp exp, bool eval_fully, pt nodes, node_table, module, ctx, errors); ptlang_ast_exp right_value = ptlang_verify_eval(exp->content.binary_operator.right_value, true, node, nodes, node_table, module, ctx, errors); + substituted = ptlang_malloc(sizeof(struct ptlang_ast_exp_s)); *substituted = (struct ptlang_ast_exp_s){ .type = exp->type, .content.binary_operator = @@ -1828,11 +1830,12 @@ static ptlang_ast_exp ptlang_verify_eval(ptlang_ast_exp exp, bool eval_fully, pt { ptlang_ast_exp operand = ptlang_verify_eval(exp->content.unary_operator, true, node, nodes, node_table, module, ctx, errors); + substituted = ptlang_malloc(sizeof(struct ptlang_ast_exp_s)); *substituted = (struct ptlang_ast_exp_s){ .type = exp->type, .content.unary_operator = operand, - .pos = exp->pos, - .ast_type = exp->ast_type, + .pos = ptlang_ast_code_position_copy(exp->pos), + .ast_type = ptlang_ast_type_copy(exp->ast_type), }; break; } @@ -1948,12 +1951,13 @@ static ptlang_ast_exp ptlang_verify_eval(ptlang_ast_exp exp, bool eval_fully, pt { ptlang_ast_exp value = ptlang_verify_eval(exp->content.cast.value, eval_fully, node, nodes, node_table, module, ctx, errors); + substituted = ptlang_malloc(sizeof(struct ptlang_ast_exp_s)); *substituted = (struct ptlang_ast_exp_s){ .type = exp->type, - .content.cast.type = exp->content.cast.type, + .content.cast.type = ptlang_ast_type_copy(exp->content.cast.type), .content.cast.value = value, - .pos = exp->pos, - .ast_type = exp->ast_type, + .pos = ptlang_ast_code_position_copy(exp->pos), + .ast_type = ptlang_ast_type_copy(exp->ast_type), }; break; } @@ -2046,12 +2050,15 @@ static ptlang_ast_exp ptlang_verify_eval(ptlang_ast_exp exp, bool eval_fully, pt if (evaluated == NULL) evaluated = ptlang_eval_const_exp(substituted); - ptlang_free(substituted); + if (substituted != NULL) + ptlang_ast_exp_destroy(substituted); return evaluated; } ptlang_ast_exp ptlang_verify_get_default_value(ptlang_ast_type type, ptlang_context *ctx) { + if (type == NULL) + return NULL; switch (type->type) { case PTLANG_AST_TYPE_VOID: @@ -2145,6 +2152,8 @@ ptlang_ast_exp ptlang_verify_get_default_value(ptlang_ast_type type, ptlang_cont static size_t ptlang_verify_calc_node_count(ptlang_ast_type type, ptlang_context_type_scope *type_scope) { type = ptlang_context_unname_type(type, type_scope); + if (type == NULL) + return 1; // the node of a array / struct itself refers to the the reference to this array / struct switch (type->type) { @@ -2174,6 +2183,10 @@ static void ptlang_verify_label_nodes(ptlang_ast_exp path_exp, ptlang_utils_grap { node->data = path_exp; node++; + + if (path_exp->ast_type == NULL) + return; + switch (path_exp->ast_type->type) { case PTLANG_AST_TYPE_ARRAY: @@ -2185,6 +2198,7 @@ static void ptlang_verify_label_nodes(ptlang_ast_exp path_exp, ptlang_utils_grap ptlang_ast_exp index = ptlang_ast_exp_integer_new(index_str_repr, NULL); ptlang_ast_exp array_element = ptlang_ast_exp_array_element_new( ptlang_ast_exp_copy(path_exp), index, ptlang_ast_code_position_copy(path_exp->pos)); + array_element->ast_type = ptlang_ast_type_copy(path_exp->ast_type->content.array.type); ptlang_verify_label_nodes(array_element, node, type_scope); } break; @@ -2199,6 +2213,7 @@ static void ptlang_verify_label_nodes(ptlang_ast_exp path_exp, ptlang_utils_grap ptlang_ast_exp struct_member = ptlang_ast_exp_struct_member_new( ptlang_ast_exp_copy(path_exp), ptlang_ast_ident_copy(struct_def->members[i]->name), ptlang_ast_code_position_copy(path_exp->pos)); + struct_member->ast_type = ptlang_ast_type_copy(struct_def->members[i]->type); ptlang_verify_label_nodes(struct_member, node, type_scope); } break; @@ -2234,6 +2249,7 @@ static void ptlang_verify_eval_globals(ptlang_ast_module module, ptlang_context memcpy(var_name, module->declarations[i]->name.name, var_name_size); ptlang_ast_exp global_var = ptlang_ast_exp_variable_new( var_name, ptlang_ast_code_position_copy(module->declarations[i]->name.pos)); + global_var->ast_type = ptlang_ast_type_copy(module->declarations[i]->type); ptlang_verify_label_nodes(global_var, node, ctx->type_scope); if (module->declarations[i]->init != NULL) ptlang_verify_build_graph(node, module->declarations[i]->init, false, node_table, ctx); @@ -2244,9 +2260,20 @@ static void ptlang_verify_eval_globals(ptlang_ast_module module, ptlang_context { ptlang_utils_graph_node *node = shget(node_table, module->declarations[i]->name.name); if (module->declarations[i]->init != NULL) - module->declarations[i]->init = ptlang_verify_eval(module->declarations[i]->init, true, node, - nodes, node_table, module, ctx, errors); + { + ptlang_ast_exp unevaled = module->declarations[i]->init; + module->declarations[i]->init = + ptlang_verify_eval(unevaled, true, node, nodes, node_table, module, ctx, errors); + ptlang_ast_exp_destroy(unevaled); + } + } + shfree(node_table); + for (size_t i = 0; i < arrlenu(nodes); i++) + { + if (nodes[i].data != NULL) + ptlang_ast_exp_destroy(nodes[i].data); } + ptlang_utils_graph_free(nodes); } static void ptlang_verify_check_cycles_in_global_defs(ptlang_utils_graph_node *nodes, @@ -2266,8 +2293,12 @@ static void ptlang_verify_check_cycles_in_global_defs(ptlang_utils_graph_node *n { ptlang_utils_graph_node *node = shget(node_table, module->declarations[i]->name.name); if (module->declarations[i]->init == NULL || node->in_cycle) + { + if (module->declarations[i]->init != NULL) + ptlang_ast_exp_destroy(module->declarations[i]->init); module->declarations[i]->init = ptlang_verify_get_default_value(module->declarations[i]->type, ctx); + } } for (size_t i = 0, lowlink = 0; i < arrlenu(cycles);) @@ -2293,7 +2324,7 @@ static void ptlang_verify_check_cycles_in_global_defs(ptlang_utils_graph_node *n arrpush(message_components, CONST_STR(" form a circular definition.")); } - char *message = ptlang_utils_build_str_from_char_arr(message_components); + char *message = ptlang_utils_build_str_from_str_arr(message_components); size_t message_size = strlen(message) + 1; arrfree(message_components); @@ -2312,6 +2343,8 @@ static void ptlang_verify_check_cycles_in_global_defs(ptlang_utils_graph_node *n } } } + + arrfree(cycles); } /** @@ -2354,8 +2387,8 @@ static bool ptlang_verify_build_graph(ptlang_utils_graph_node *node, ptlang_ast_ case PTLANG_AST_EXP_LENGTH: break; case PTLANG_AST_EXP_VARIABLE: - ptlang_verify_add_dependency(node, ptlang_verify_get_node(exp, node_table, ctx), exp, node_table, - ctx); + ptlang_verify_add_dependency(node, ptlang_verify_get_node(exp, node_table, ctx), exp->ast_type, + node_table, ctx); break; case PTLANG_AST_EXP_INTEGER: case PTLANG_AST_EXP_FLOAT: @@ -2429,14 +2462,54 @@ static bool ptlang_verify_build_graph(ptlang_utils_graph_node *node, ptlang_ast_ } static void ptlang_verify_add_dependency(ptlang_utils_graph_node *from, ptlang_utils_graph_node *to, - ptlang_ast_exp exp, ptlang_verify_node_table node_table, + ptlang_ast_type type, ptlang_verify_node_table node_table, ptlang_context *ctx) { - size_t node_count = ptlang_verify_calc_node_count(exp->ast_type, ctx->type_scope); - for (size_t i = 0; i < node_count; i++) + type = ptlang_context_unname_type(type, ctx->type_scope); + ptlang_ast_type other = ((ptlang_ast_exp)to->data)->ast_type; + other = ptlang_context_unname_type(other, ctx->type_scope); + + arrpush(from->edges_to, to); + from++; + to++; + if (type != NULL && other != NULL && type->type == other->type) { - arrpush(from[i].edges_to, &to[i]); + switch (type->type) + { + case PTLANG_AST_TYPE_ARRAY: + { + if (type->content.array.len != other->content.array.len) + break; + for (size_t i = 0; i < type->content.array.len; i++) + { + ptlang_verify_add_dependency(from, to, type->content.array.type, node_table, ctx); + from++; + to++; + } + break; + } + case PTLANG_AST_TYPE_NAMED: + { + if (strcmp(type->content.name, other->content.name) != 0) + break; + ptlang_ast_struct_def struct_def = + ptlang_context_get_struct_def(type->content.name, ctx->type_scope); + for (size_t i = 0; i < arrlenu(struct_def->members); i++) + { + ptlang_verify_add_dependency(from, to, struct_def->members[i]->type, node_table, ctx); + from++; + to++; + } + break; + } + default: + break; + } } + // for (size_t i = 0; i < node_count; i++) + // { + // arrpush(from[i].edges_to, &to[i]); + // } } static ptlang_utils_graph_node * diff --git a/src/ptlang_verify/ptlang_verify_impl.h b/src/ptlang_verify/ptlang_verify_impl.h index 7c1a096..df9500f 100644 --- a/src/ptlang_verify/ptlang_verify_impl.h +++ b/src/ptlang_verify/ptlang_verify_impl.h @@ -1,7 +1,7 @@ #ifndef PTLANG_VERIFY_IMPL_H #define PTLANG_VERIFY_IMPL_H -#include "ptlang_ast_impl.h" +#include "ptlang_ast_nodes.h" #include "ptlang_eval.h" #include "ptlang_utils.h" #include "ptlang_verify.h" @@ -128,7 +128,7 @@ static ptlang_utils_graph_node * ptlang_verify_get_node(ptlang_ast_exp exp, ptlang_verify_node_table node_table, ptlang_context *ctx); static void ptlang_verify_add_dependency(ptlang_utils_graph_node *from, ptlang_utils_graph_node *to, - ptlang_ast_exp exp, ptlang_verify_node_table node_table, + ptlang_ast_type type, ptlang_verify_node_table node_table, ptlang_context *ctx); static bool ptlang_verify_build_graph(ptlang_utils_graph_node *node, ptlang_ast_exp exp, bool depends_on_ref,