4#include <llvm-c/Analysis.h>
5#include <llvm-c/BitWriter.h>
6#include <llvm-c/Core.h>
7#include <llvm-c/ExecutionEngine.h>
8#include <llvm-c/Target.h>
9#include <llvm-c/TargetMachine.h>
10#include <llvm-c/DebugInfo.h>
13#include <llvm-c/Types.h>
21#include "../ast/ast.h"
22#include "../c_libs/memory/memory.h"
24#define SYMBOL_HASH_SIZE 1024
25#define MAX_LINK_LIBS 64
108 LLVMModuleRef
module;
160 const char *module_name);
164 const char *module_name);
181 const char *field_name);
186void cache_symbol(
const char *module_name,
const char *symbol_name,
189 const char *symbol_name);
223 const char *filename);
252 LLVMValueRef value, LLVMTypeRef type,
257 const char *module_name);
261 const char *output_dir);
265 const char *output_path,
bool is_debug);
269 LLVMTypeRef type,
bool is_function);
290 LLVMValueRef value,
const char *name);
292 LLVMValueRef ptr,
unsigned index,
293 LLVMTypeRef element_type,
const char *name);
295 LLVMValueRef ptr,
unsigned index, LLVMValueRef value);
297 LLVMValueRef array_ptr, LLVMValueRef index,
307 const char *symbol_name);
323 const char *field_name);
326 const char *method_name,
bool is_public,
331 const char *struct_name,
332 LLVMValueRef *field_values,
345 const char *struct_name);
382 LLVMTypeRef from_type, LLVMTypeRef to_type);
388 LLVMTypeRef dest_type, LLVMValueRef src_array,
389 LLVMTypeRef src_type);
393 LLVMValueRef value, LLVMTypeRef type,
394 LLVMTypeRef element_type,
bool is_function);
398 LLVMTypeRef type, LLVMTypeRef element_type,
bool is_function);
474 LLVMValueRef range_struct);
476 LLVMValueRef range_struct);
482 LLVMTypeRef element_type);
const char * get_module_name_from_access(AstNode *node)
Get module name/alias from a compile-time member access node.
Definition member_access.c:214
LLVMValueRef codegen_expr_struct_assignment(CodeGenContext *ctx, AstNode *node)
Definition struct.c:506
void print_struct_info(CodeGenContext *ctx, const char *struct_name)
Definition struct_helpers.c:94
void debug_struct_layout(CodeGenContext *ctx, const char *struct_name)
Definition struct_helpers.c:144
void add_symbol(CodeGenContext *ctx, const char *name, LLVMValueRef value, LLVMTypeRef type, bool is_function)
Definition llvm.c:390
LLVMValueRef codegen_stmt_program(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:9
LLVMLinkage get_function_linkage(AstNode *node)
Definition llvm.c:572
LLVMValueRef range_length(CodeGenContext *ctx, LLVMValueRef range_struct)
Definition expr.c:62
LLVMValueRef codegen_expr_sizeof(CodeGenContext *ctx, AstNode *node)
Definition expr.c:1900
LLVMValueRef codegen_expr_free(CodeGenContext *ctx, AstNode *node)
Definition expr.c:1949
LLVMValueRef codegen_stmt_print(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:840
void add_symbol_to_module_with_element_type(ModuleCompilationUnit *module, const char *name, LLVMValueRef value, LLVMTypeRef type, LLVMTypeRef element_type, bool is_function)
Definition stmt.c:26
LLVMValueRef create_struct_zero_initializer(CodeGenContext *ctx, const char *struct_name)
Definition struct_helpers.c:37
void debug_object_files(const char *output_dir)
Definition module_handles.c:694
LLVMValueRef codegen_stmt_function(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:166
void add_symbol_with_element_type(CodeGenContext *ctx, const char *name, LLVMValueRef value, LLVMTypeRef type, LLVMTypeRef element_type, bool is_function)
Definition stmt.c:17
LLVMValueRef codegen_while_loop(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:1020
void branch_if_no_terminator(CodeGenContext *ctx, LLVMBasicBlockRef target)
Definition helpers.c:43
bool is_main_module(ModuleCompilationUnit *unit)
Definition module_handles.c:669
void init_symbol_cache(void)
Definition module_handles.c:19
bool compile_modules_to_objects(CodeGenContext *ctx, const char *output_dir)
Definition llvm.c:224
LLVMValueRef codegen_expr_identifier(CodeGenContext *ctx, AstNode *node)
Definition expr.c:131
LLVMTypeRef codegen_type_struct(CodeGenContext *ctx, const char *struct_name)
Definition struct.c:639
LLVMValueRef codegen_expr_addr(CodeGenContext *ctx, AstNode *node)
Definition expr.c:2077
void execute_deferred_statements_inline(CodeGenContext *ctx, DeferredStatement *defers)
Definition defer.c:21
LLVMValueRef codegen_case_value(CodeGenContext *ctx, AstNode *case_value)
Definition stmt.c:1269
void init_defer_stack(CodeGenContext *ctx)
Definition defer.c:3
LLVMValueRef codegen_stmt_switch(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:1162
int get_field_index(StructInfo *struct_info, const char *field_name)
Definition struct.c:43
LLVMValueRef codegen_expr_literal(CodeGenContext *ctx, AstNode *node)
Definition expr.c:72
void cache_struct(const char *name, StructInfo *info)
Definition module_handles.c:79
bool generate_object_file(CodeGenContext *ctx, const char *object_filename)
Definition llvm.c:536
LLVMValueRef codegen_stmt_defer(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:956
void import_variable_symbol(CodeGenContext *ctx, LLVM_Symbol *source_symbol, ModuleCompilationUnit *source_module, const char *alias)
Definition module_handles.c:573
LLVMValueRef codegen_expr_system(CodeGenContext *ctx, AstNode *node)
Definition expr.c:1502
LLVMValueRef create_range_struct(CodeGenContext *ctx, LLVMValueRef start, LLVMValueRef end)
Definition expr.c:19
void set_module_as_main(ModuleCompilationUnit *unit)
Definition module_handles.c:673
LLVMValueRef codegen_expr_struct_literal(CodeGenContext *ctx, AstNode *node)
Definition struct_expr.c:32
LLVMValueRef codegen_expr_binary(CodeGenContext *ctx, AstNode *node)
Definition binary_ops.c:20
LLVMValueRef codegen_stmt_link(CodeGenContext *ctx, AstNode *node)
Definition module_handles.c:462
bool block_has_terminator(LLVMBuilderRef builder)
Definition helpers.c:37
StructInfo * find_struct_type(CodeGenContext *ctx, const char *name)
Definition struct.c:22
ModuleCompilationUnit * find_module(CodeGenContext *ctx, const char *module_name)
Definition llvm.c:323
LLVMValueRef codegen_expr_array(CodeGenContext *ctx, AstNode *node)
Definition expr.c:711
ModuleCompilationUnit * create_module_unit(CodeGenContext *ctx, const char *module_name)
Definition llvm.c:305
bool is_struct_type(CodeGenContext *ctx, LLVMTypeRef type)
Definition struct_helpers.c:8
LLVMValueRef codegen_stmt_default(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:1409
LLVMTypeRef get_float_type(CodeGenContext *ctx, bool is_double)
Definition type_cache.c:47
bool generate_module_object_file(ModuleCompilationUnit *module, const char *output_path, bool is_debug)
Definition llvm.c:166
LLVMValueRef codegen_stmt_break_continue(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:962
LLVMValueRef codegen_for_loop(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:1072
LLVMValueRef build_global_string(CodeGenContext *ctx, const char *str, const char *name)
Definition helpers.c:54
LLVMValueRef codegen_expr_deref(CodeGenContext *ctx, AstNode *node)
Definition expr.c:1984
char * print_llvm_ir(CodeGenContext *ctx)
Definition llvm.c:529
void init_type_cache(CodeGenContext *ctx)
Definition type_cache.c:4
void cache_symbol(const char *module_name, const char *symbol_name, LLVM_Symbol *symbol)
Definition module_handles.c:25
LLVMValueRef convert_value_to_type(CodeGenContext *ctx, LLVMValueRef value, LLVMTypeRef from_type, LLVMTypeRef to_type)
Definition arrays.c:4
StructInfo * find_struct_type_fast(CodeGenContext *ctx, const char *name)
Definition module_handles.c:116
LLVMValueRef codegen_struct_literal(CodeGenContext *ctx, const char *struct_name, LLVMValueRef *field_values, size_t field_count)
Definition struct.c:649
LLVMValueRef codegen_stmt(CodeGenContext *ctx, AstNode *node)
Definition lookup.c:63
LLVMValueRef codegen_stmt_enum(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:506
LLVMValueRef get_const_int(CodeGenContext *ctx, unsigned bits, uint64_t value)
Definition type_cache.c:52
LLVMValueRef codegen_expr_cast(CodeGenContext *ctx, AstNode *node)
Definition expr.c:1246
void setup_module_debug_info(CodeGenContext *ctx, ModuleCompilationUnit *unit, const char *filename)
Definition llvm.c:645
CodeGenContext * init_codegen_context(ArenaAllocator *arena)
Definition llvm.c:449
LLVMValueRef alloca_and_store(CodeGenContext *ctx, LLVMTypeRef type, LLVMValueRef value, const char *name)
Definition helpers.c:4
LLVM_Symbol * find_symbol_global(CodeGenContext *ctx, const char *name, const char *module_name)
Definition llvm.c:359
bool is_range_type(LLVMTypeRef type)
Definition stmt.c:779
bool types_are_equal(LLVMTypeRef a, LLVMTypeRef b)
Definition type_cache.c:81
void add_symbol_to_module(ModuleCompilationUnit *module, const char *name, LLVMValueRef value, LLVMTypeRef type, bool is_function)
Definition llvm.c:337
LLVMValueRef codegen_stmt_block(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:623
LLVMTypeRef get_int_type(CodeGenContext *ctx, unsigned bits)
Definition type_cache.c:30
LLVMValueRef codegen_stmt_return(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:549
LLVMValueRef codegen_struct_method(CodeGenContext *ctx, AstNode *func_node, StructInfo *struct_info, const char *method_name, bool is_public, bool is_static)
Definition struct.c:330
void generate_external_declarations(CodeGenContext *ctx, ModuleCompilationUnit *target_module)
Definition llvm.c:401
bool pointer_type(LLVMTypeRef type)
Definition type_cache.c:77
void clear_defer_stack(CodeGenContext *ctx)
Definition defer.c:140
LLVMValueRef codegen_expr(CodeGenContext *ctx, AstNode *node)
Definition lookup.c:3
char * process_escape_sequences(const char *input)
Definition llvm.c:585
#define MAX_LINK_LIBS
Definition llvm.h:25
void cleanup_codegen_context(CodeGenContext *ctx)
Definition llvm.c:485
LLVMValueRef array_gep(CodeGenContext *ctx, LLVMTypeRef array_type, LLVMValueRef array_ptr, LLVMValueRef index, const char *name)
Definition helpers.c:29
LLVM_Symbol * find_symbol(CodeGenContext *ctx, const char *name)
Definition llvm.c:397
void struct_gep_store(CodeGenContext *ctx, LLVMTypeRef struct_type, LLVMValueRef ptr, unsigned index, LLVMValueRef value)
Definition helpers.c:21
LLVMTypeRef codegen_type_function(CodeGenContext *ctx, AstNode *node)
Definition type.c:91
LLVMValueRef get_default_value(LLVMTypeRef type)
Definition type_cache.c:83
void cleanup_module_caches(void)
Definition module_handles.c:142
LLVMValueRef range_contains(CodeGenContext *ctx, LLVMValueRef range_struct, LLVMValueRef value)
Definition expr.c:48
void set_debug_location(CodeGenContext *ctx, unsigned line, unsigned column)
Definition llvm.c:684
LLVMValueRef codegen_expr_assignment(CodeGenContext *ctx, AstNode *node)
Definition expr.c:489
LLVMValueRef codegen_expr_unary(CodeGenContext *ctx, AstNode *node)
Definition expr.c:152
void finalize_all_debug_info(CodeGenContext *ctx)
Definition llvm.c:678
LLVMTypeRef codegen_type_array(CodeGenContext *ctx, AstNode *node)
Definition type.c:76
LLVMValueRef codegen_stmt_case(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:1394
void import_function_symbol(CodeGenContext *ctx, LLVM_Symbol *source_symbol, ModuleCompilationUnit *source_module, const char *alias)
Definition module_handles.c:518
LLVMValueRef codegen_stmt_use(CodeGenContext *ctx, AstNode *node)
Definition module_handles.c:373
bool generate_program_modules(CodeGenContext *ctx, AstNode *ast_root, const char *output_dir)
Definition llvm.c:516
LLVMValueRef codegen_stmt_field(CodeGenContext *ctx, AstNode *node)
Definition struct.c:490
LLVMTypeRef codegen_type_pointer(CodeGenContext *ctx, AstNode *node)
Definition type.c:68
bool is_field_access_allowed(CodeGenContext *ctx, StructInfo *struct_info, int field_index)
Definition struct.c:53
void copy_array_elements(CodeGenContext *ctx, LLVMValueRef dest_array, LLVMTypeRef dest_type, LLVMValueRef src_array, LLVMTypeRef src_type)
Definition arrays.c:157
LLVMValueRef codegen_stmt_program_multi_module(CodeGenContext *ctx, AstNode *node)
Definition module_handles.c:280
LLVMValueRef codegen_expr_syscall(CodeGenContext *ctx, AstNode *node)
Generate LLVM IR for syscall expression.
Definition expr.c:1561
void set_current_module(CodeGenContext *ctx, ModuleCompilationUnit *module)
Definition llvm.c:333
LLVMValueRef get_range_end_value(CodeGenContext *ctx, LLVMValueRef range_struct)
Definition stmt.c:819
LLVMTypeRef codegen_type_basic(CodeGenContext *ctx, AstNode *node)
Definition type.c:37
StructInfo * lookup_cached_struct(const char *name)
Definition module_handles.c:100
LLVMValueRef codegen_loop(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:1151
void push_defer_statement(CodeGenContext *ctx, AstNode *statement)
Definition defer.c:9
LLVMValueRef codegen_expr_input(CodeGenContext *ctx, AstNode *node)
Definition expr.c:1331
void add_struct_type(CodeGenContext *ctx, StructInfo *struct_info)
Definition struct.c:37
bool is_enum_constant(LLVM_Symbol *sym)
Definition stmt.c:479
LLVMValueRef codegen_expr_index(CodeGenContext *ctx, AstNode *node)
Definition expr.c:827
bool validate_module_access(CodeGenContext *ctx, const char *prefix, const char *symbol_name)
Validate that a module access is permitted.
Definition member_access.c:239
LLVMValueRef codegen_stmt_if(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:662
LLVMTypeRef get_range_struct_type(CodeGenContext *ctx, LLVMTypeRef element_type)
Definition expr.c:10
bool is_module_identifier(CodeGenContext *ctx, const char *name)
Check if an identifier might be a module/alias.
Definition member_access.c:198
int64_t get_enum_constant_value(LLVM_Symbol *sym)
Definition stmt.c:493
void preprocess_all_modules(CodeGenContext *ctx)
Definition module_handles.c:128
LLVMValueRef struct_gep_load(CodeGenContext *ctx, LLVMTypeRef struct_type, LLVMValueRef ptr, unsigned index, LLVMTypeRef element_type, const char *name)
Definition helpers.c:12
void finalize_module_debug_info(ModuleCompilationUnit *unit)
Definition llvm.c:670
bool needs_conversion(LLVMTypeRef from, LLVMTypeRef to)
Definition type_cache.c:104
LLVMValueRef get_range_start_value(CodeGenContext *ctx, LLVMValueRef range_struct)
Definition stmt.c:799
LLVM_Symbol * find_symbol_with_module_support(CodeGenContext *ctx, const char *name)
Definition module_handles.c:643
void init_struct_cache(void)
Definition module_handles.c:73
LLVMValueRef create_struct_copy(CodeGenContext *ctx, LLVMValueRef src_struct, StructInfo *struct_info)
Definition struct_helpers.c:53
LLVMValueRef codegen_enum_member_case(CodeGenContext *ctx, AstNode *member_expr)
Definition stmt.c:1296
void print_module_info(CodeGenContext *ctx)
Definition module_handles.c:679
LLVMValueRef codegen_multidim_array_access(CodeGenContext *ctx, AstNode *base_expr, AstNode **indices, size_t index_count)
Definition arrays.c:89
LLVMValueRef codegen_expr_struct_access(CodeGenContext *ctx, AstNode *node)
Definition struct_access.c:31
void import_module_symbols(CodeGenContext *ctx, ModuleCompilationUnit *source_module, const char *alias)
Definition module_handles.c:482
void generate_cleanup_blocks(CodeGenContext *ctx)
Definition defer.c:65
LLVMValueRef codegen_expr_call(CodeGenContext *ctx, AstNode *node)
Definition expr.c:254
LLVMTypeRef extract_element_type_from_ast(CodeGenContext *ctx, AstNode *type_node)
Definition stmt.c:39
LLVMValueRef codegen_stmt_module(CodeGenContext *ctx, AstNode *node)
Definition module_handles.c:355
bool is_int_type(LLVMTypeRef type)
Definition type_cache.c:68
#define SYMBOL_HASH_SIZE
Definition llvm.h:24
unsigned int hash_string(const char *str)
Definition module_handles.c:8
LLVMValueRef codegen_stmt_os(CodeGenContext *ctx, AstNode *node)
Definition module_handles.c:402
const char * get_struct_name_from_type(CodeGenContext *ctx, LLVMTypeRef type)
Definition struct_helpers.c:23
LLVMTypeRef codegen_type(CodeGenContext *ctx, AstNode *node)
Definition lookup.c:124
bool generate_assembly_file(CodeGenContext *ctx, const char *asm_filename, bool is_debug)
Definition llvm.c:544
LLVM_Symbol * find_symbol_in_module(ModuleCompilationUnit *module, const char *name)
Definition llvm.c:349
LLVMValueRef codegen_module_access(CodeGenContext *ctx, AstNode *node)
Handle compile-time member access (::)
Definition member_access.c:23
LLVMValueRef codegen_stmt_struct(CodeGenContext *ctx, AstNode *node)
Definition struct.c:75
LLVMValueRef codegen_expr_alloc(CodeGenContext *ctx, AstNode *node)
Definition expr.c:1918
bool is_float_type(LLVMTypeRef type)
Definition type_cache.c:72
LLVMValueRef codegen_infinite_loop(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:980
LLVMValueRef codegen_stmt_expression(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:13
LLVMValueRef codegen_stmt_var_decl(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:68
LLVM_Symbol * lookup_cached_symbol(const char *module_name, const char *symbol_name)
Definition module_handles.c:52
StructInfo * find_concrete_struct_for_base(CodeGenContext *ctx, StructInfo *base_info, const char *field_name)
Definition struct.c:6
Type * array_type(Parser *parser)
Definition type.c:22
Arena allocator structure.
Definition memory.h:101
bool is_debug
Definition llvm.h:126
const char * target_os
Definition llvm.h:123
LLVMBasicBlockRef loop_break_block
Definition llvm.h:118
LLVMBuilderRef builder
Definition llvm.h:101
LLVMValueRef current_function
Definition llvm.h:116
LLVMContextRef context
Definition llvm.h:100
ArenaAllocator * arena
Definition llvm.h:130
CommonTypes common_types
Definition llvm.h:120
LLVMMetadataRef current_func_di
Definition llvm.h:127
LLVMModuleRef struct DeferredStatement * deferred_statements
Definition llvm.h:111
size_t deferred_capacity
Definition llvm.h:113
StructInfo * struct_types
Definition llvm.h:121
LLVMBasicBlockRef loop_continue_block
Definition llvm.h:117
size_t deferred_count
Definition llvm.h:112
ModuleCompilationUnit * modules
Definition llvm.h:104
ModuleCompilationUnit * current_module
Definition llvm.h:105
LLVMTypeRef void_type
Definition llvm.h:89
LLVMTypeRef i64
Definition llvm.h:83
LLVMTypeRef i32
Definition llvm.h:83
LLVMTypeRef f32
Definition llvm.h:86
LLVMValueRef const_i64_1
Definition llvm.h:94
LLVMTypeRef i8_ptr
Definition llvm.h:90
LLVMTypeRef i8
Definition llvm.h:83
LLVMValueRef const_i32_0
Definition llvm.h:93
LLVMTypeRef f64
Definition llvm.h:86
LLVMValueRef const_i64_0
Definition llvm.h:94
LLVMValueRef const_i32_1
Definition llvm.h:93
LLVMTypeRef i1
Definition llvm.h:83
LLVMTypeRef i16
Definition llvm.h:83
struct DeferredStatement * next
Definition llvm.h:66
AstNode * statement
Definition llvm.h:64
LLVMBasicBlockRef cleanup_block
Definition llvm.h:65
LLVMTypeRef type
Definition llvm.h:34
bool is_function
Definition llvm.h:36
char * name
Definition llvm.h:32
LLVMValueRef value
Definition llvm.h:33
LLVMTypeRef element_type
Definition llvm.h:35
struct LLVM_Symbol * next
Definition llvm.h:37
bool is_main_module
Definition llvm.h:45
size_t link_lib_count
Definition llvm.h:49
LLVMDIBuilderRef dibuilder
Definition llvm.h:51
LLVMMetadataRef compile_unit
Definition llvm.h:52
const char * link_libs[MAX_LINK_LIBS]
Definition llvm.h:48
struct ModuleCompilationUnit * next
Definition llvm.h:46
char * module_name
Definition llvm.h:42
LLVMModuleRef LLVM_Symbol * symbols
Definition llvm.h:44
LLVMMetadataRef file_metadata
Definition llvm.h:53
char ** dependencies
Definition llvm.h:58
const char * module_name
Definition llvm.h:57
bool processed
Definition llvm.h:60
size_t dep_count
Definition llvm.h:59
StructInfo * info
Definition llvm.h:146
struct StructHashEntry * next
Definition llvm.h:147
const char * name
Definition llvm.h:145
StructHashEntry * buckets[SYMBOL_HASH_SIZE]
Definition llvm.h:151
bool * field_is_public
Definition llvm.h:75
bool is_public
Definition llvm.h:77
char * name
Definition llvm.h:70
LLVMTypeRef * field_types
Definition llvm.h:73
LLVMTypeRef * field_element_types
Definition llvm.h:74
size_t field_count
Definition llvm.h:76
struct StructInfo * next
Definition llvm.h:78
char ** field_names
Definition llvm.h:72
LLVMTypeRef llvm_type
Definition llvm.h:71
const char * key
Definition llvm.h:134
struct SymbolHashEntry * next
Definition llvm.h:136
LLVM_Symbol * symbol
Definition llvm.h:135
SymbolHashEntry * buckets[SYMBOL_HASH_SIZE]
Definition llvm.h:140