Luma 0.1.0
A low-level compiled alternative to C, C++, and more!
Loading...
Searching...
No Matches
llvm.h
Go to the documentation of this file.
1// Enhanced llvm.h - Complete module system declarations
2#pragma once
3// LLVM C API Headers
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>
11
12// Standard Library Headers
13#include <llvm-c/Types.h>
14#include <stdalign.h>
15#include <stdbool.h>
16#include <stdint.h>
17#include <stdio.h>
18#include <string.h>
19
20// Project Headers
21#include "../ast/ast.h"
22#include "../c_libs/memory/memory.h"
23
24#define SYMBOL_HASH_SIZE 1024
25#define MAX_LINK_LIBS 64
26
27typedef struct LLVM_Symbol LLVM_Symbol;
30
32 char *name;
33 LLVMValueRef value;
34 LLVMTypeRef type; // The type of the symbol itself
35 LLVMTypeRef element_type; // For pointer types, what it points to
38};
39
40// Individual module compilation unit
43 LLVMModuleRef module;
47
50
51 // Source context for error reporting
54 const char *file_path;
55
56 LLVMDIBuilderRef dibuilder;
57 LLVMMetadataRef compile_unit;
58 LLVMMetadataRef file_metadata;
59};
60
61typedef struct ModuleDependencyInfo {
62 const char *module_name;
63 char **dependencies; // Array of module names this depends on
64 size_t dep_count;
67
73
74typedef struct StructInfo {
75 char *name;
76 const char *module_name;
77 LLVMTypeRef llvm_type;
79 LLVMTypeRef *field_types;
80 LLVMTypeRef *field_element_types;
86
87typedef struct CommonTypes {
88 // Integer types
89 LLVMTypeRef i1, i8, i16, i32, i64;
90
91 // Float types
92 LLVMTypeRef f32, f64;
93
94 // Special types
95 LLVMTypeRef void_type;
96 LLVMTypeRef i8_ptr;
97
98 // Common constants (cached for performance)
99 LLVMValueRef const_i32_0, const_i32_1;
102
103// Code generation context
105 // LLVM Core Components
106 LLVMContextRef context;
107 LLVMBuilderRef builder;
108
109 // Module Management (New System)
112
113 // Legacy Support (for backward compatibility)
114 LLVMModuleRef module;
115
116 // Defer statement tracking
120
121 // Code Generation State
122 LLVMValueRef current_function;
123 LLVMBasicBlockRef loop_continue_block;
124 LLVMBasicBlockRef loop_break_block;
125
128
129 const char *target_os;
130
131 // Debug Info
133 LLVMMetadataRef current_func_di;
134
135 // Memory Management
137};
138
139typedef struct SymbolHashEntry {
140 const char *key; // "module_name:symbol_name"
144
148
149// Hash table for struct types
155
159
160// =============================================================================
161// MODULE MANAGEMENT FUNCTIONS
162// =============================================================================
163
164// Create a new module compilation unit
166 const char *module_name);
167
168// Find module by name
170 const char *module_name);
171
172// Set current module for code generation
174
175// Compile all modules to separate object files
176bool compile_modules_to_objects(CodeGenContext *ctx, const char *output_dir);
177
178// Generate external function declarations for cross-module calls
180 ModuleCompilationUnit *target_module);
182StructInfo *find_struct_type_fast(CodeGenContext *ctx, const char *name);
183void cleanup_module_caches(void);
184void debug_object_files(const char *output_dir);
186 StructInfo *base_info,
187 const char *field_name);
188
189unsigned int hash_string(const char *str);
190
191void init_symbol_cache(void);
192void cache_symbol(const char *module_name, const char *symbol_name,
193 LLVM_Symbol *symbol);
194LLVM_Symbol *lookup_cached_symbol(const char *module_name,
195 const char *symbol_name);
196
197void init_struct_cache(void);
198void cache_struct(const char *name, StructInfo *info);
199StructInfo *lookup_cached_struct(const char *name);
200StructInfo *find_struct_type_fast(CodeGenContext *ctx, const char *name);
202 AstNode *node);
203LLVMValueRef codegen_member_address(CodeGenContext *ctx, AstNode *node,
204 LLVMTypeRef *value_type_out,
205 LLVMTypeRef *element_type_out);
206
207// =============================================================================
208// SYMBOL IMPORT AND MODULE INTEROP
209// =============================================================================
210
211// Import symbols from one module to another
213 ModuleCompilationUnit *source_module,
214 const char *alias);
215
216// Import specific function symbol
217void import_function_symbol(CodeGenContext *ctx, LLVM_Symbol *source_symbol,
218 ModuleCompilationUnit *source_module,
219 const char *alias);
220
221// Import specific variable symbol
222void import_variable_symbol(CodeGenContext *ctx, LLVM_Symbol *source_symbol,
223 ModuleCompilationUnit *source_module,
224 const char *alias);
225
226// Enhanced symbol lookup with module support
228 const char *name);
229
230// Debug info support
232 const char *filename);
235void set_debug_location(CodeGenContext *ctx, unsigned line, unsigned column);
236
237// =============================================================================
238// MODULE UTILITY FUNCTIONS
239// =============================================================================
240
241// Report a codegen error through the shared error system. If a node is
242// provided, the source location is taken from it; otherwise line 0 is used.
243void cg_error(CodeGenContext *ctx, AstNode *node, const char *error_type,
244 const char *format, ...);
245void cg_error_help(CodeGenContext *ctx, AstNode *node, const char *error_type,
246 const char *help, const char *format, ...);
247
248// Check if module is the main module
250
251// Set a module as the main module
253
254// Print module information for debugging
256void debug_object_files(const char *output_dir);
257
258// =============================================================================
259// ENHANCED CORE API FUNCTIONS
260// =============================================================================
261
262// Context Management
265
266// Enhanced Symbol Table Operations (now module-aware)
267void add_symbol_to_module(ModuleCompilationUnit *module, const char *name,
268 LLVMValueRef value, LLVMTypeRef type,
269 bool is_function);
271 const char *name);
272LLVM_Symbol *find_symbol_global(CodeGenContext *ctx, const char *name,
273 const char *module_name);
274
275// Main Code Generation
277 const char *output_dir);
278
279// Object File Generation (per module)
281 const char *output_path, bool is_debug);
282
283// Existing API (preserved for compatibility)
284void add_symbol(CodeGenContext *ctx, const char *name, LLVMValueRef value,
285 LLVMTypeRef type, bool is_function);
286LLVM_Symbol *find_symbol(CodeGenContext *ctx, const char *name);
287char *print_llvm_ir(CodeGenContext *ctx);
288bool generate_object_file(CodeGenContext *ctx, const char *object_filename);
289bool generate_assembly_file(CodeGenContext *ctx, const char *asm_filename,
290 bool is_debug);
291char *process_escape_sequences(const char *input);
292LLVMLinkage get_function_linkage(AstNode *node);
293
295LLVMTypeRef get_int_type(CodeGenContext *ctx, unsigned bits);
296LLVMTypeRef get_float_type(CodeGenContext *ctx, bool is_double);
297LLVMValueRef get_const_int(CodeGenContext *ctx, unsigned bits, uint64_t value);
298bool is_int_type(LLVMTypeRef type);
299bool is_float_type(LLVMTypeRef type);
300bool pointer_type(LLVMTypeRef type);
301bool types_are_equal(LLVMTypeRef a, LLVMTypeRef b);
302bool needs_conversion(LLVMTypeRef from, LLVMTypeRef to);
303LLVMValueRef get_default_value(LLVMTypeRef type);
304
305LLVMValueRef alloca_and_store(CodeGenContext *ctx, LLVMTypeRef type,
306 LLVMValueRef value, const char *name);
307LLVMValueRef struct_gep_load(CodeGenContext *ctx, LLVMTypeRef struct_type,
308 LLVMValueRef ptr, unsigned index,
309 LLVMTypeRef element_type, const char *name);
310void struct_gep_store(CodeGenContext *ctx, LLVMTypeRef struct_type,
311 LLVMValueRef ptr, unsigned index, LLVMValueRef value);
312LLVMValueRef array_gep(CodeGenContext *ctx, LLVMTypeRef array_type,
313 LLVMValueRef array_ptr, LLVMValueRef index,
314 const char *name);
315bool block_has_terminator(LLVMBuilderRef builder);
316void branch_if_no_terminator(CodeGenContext *ctx, LLVMBasicBlockRef target);
317LLVMValueRef build_global_string(CodeGenContext *ctx, const char *str,
318 const char *name);
319
320LLVMValueRef codegen_module_access(CodeGenContext *ctx, AstNode *node);
321bool is_module_identifier(CodeGenContext *ctx, const char *name);
322bool validate_module_access(CodeGenContext *ctx, const char *prefix,
323 const char *symbol_name);
324const char *get_module_name_from_access(AstNode *node);
325
326// =============================================================================
327// ENHANCED STRUCT SUPPORT FUNCTIONS
328// =============================================================================
329
330// Core struct management
331StructInfo *find_struct_type(CodeGenContext *ctx, const char *name);
332void add_struct_type(CodeGenContext *ctx, StructInfo *struct_info);
333int get_field_index(StructInfo *struct_info, const char *field_name);
335 int field_index);
337 CodeGenContext *ctx,
338 StructInfo *base_info,
339 const char *field_name);
340LLVMValueRef codegen_struct_method(CodeGenContext *ctx, AstNode *func_node,
341 StructInfo *struct_info,
342 const char *method_name, bool is_public,
343 bool is_static);
344
345// Struct literal/initializer support
346LLVMValueRef codegen_struct_literal(CodeGenContext *ctx,
347 const char *struct_name,
348 LLVMValueRef *field_values,
349 size_t field_count);
350
351// =============================================================================
352// STRUCT TYPE SYSTEM INTEGRATION
353// =============================================================================
354
355// Enhanced type checking for structs
356bool is_struct_type(CodeGenContext *ctx, LLVMTypeRef type);
357const char *get_struct_name_from_type(CodeGenContext *ctx, LLVMTypeRef type);
358
359// Struct construction helpers
361 const char *struct_name);
362LLVMValueRef create_struct_copy(CodeGenContext *ctx, LLVMValueRef src_struct,
363 StructInfo *struct_info);
364
365// Struct debugging and introspection
366void print_struct_info(CodeGenContext *ctx, const char *struct_name);
367void debug_struct_layout(CodeGenContext *ctx, const char *struct_name);
368
369StructInfo *find_struct_type(CodeGenContext *ctx, const char *name);
370void add_struct_type(CodeGenContext *ctx, StructInfo *struct_info);
371LLVMValueRef codegen_stmt_struct(CodeGenContext *ctx, AstNode *node);
372LLVMValueRef codegen_stmt_field(CodeGenContext *ctx, AstNode *node);
373LLVMValueRef codegen_expr_struct_access(CodeGenContext *ctx, AstNode *node);
374LLVMTypeRef codegen_type_struct(CodeGenContext *ctx, const char *struct_name);
375
376// =============================================================================
377// AST NODE HANDLERS - PUBLIC INTERFACE
378// =============================================================================
379
380// Primary Dispatch Functions
381LLVMValueRef codegen_expr(CodeGenContext *ctx, AstNode *node);
382LLVMValueRef codegen_stmt(CodeGenContext *ctx, AstNode *node);
383LLVMTypeRef codegen_type(CodeGenContext *ctx, AstNode *node);
384
385// Enhanced handlers for module system
387 AstNode *node);
388LLVMValueRef codegen_stmt_module(CodeGenContext *ctx, AstNode *node);
389LLVMValueRef codegen_stmt_use(CodeGenContext *ctx, AstNode *node);
390LLVMValueRef codegen_stmt_os(CodeGenContext *ctx, AstNode *node);
391LLVMValueRef codegen_stmt_link(CodeGenContext *ctx, AstNode *node);
392
393// =============================================================================
394// AST NODE HANDLERS - EXPRESSION TYPES
395// =============================================================================
396
397LLVMValueRef convert_value_to_type(CodeGenContext *ctx, LLVMValueRef value,
398 LLVMTypeRef from_type, LLVMTypeRef to_type);
400 AstNode *base_expr,
401 AstNode **indices,
402 size_t index_count);
403void copy_array_elements(CodeGenContext *ctx, LLVMValueRef dest_array,
404 LLVMTypeRef dest_type, LLVMValueRef src_array,
405 LLVMTypeRef src_type);
406
407// Enhanced symbol management with element type support
408void add_symbol_with_element_type(CodeGenContext *ctx, const char *name,
409 LLVMValueRef value, LLVMTypeRef type,
410 LLVMTypeRef element_type, bool is_function);
411
413 ModuleCompilationUnit *module, const char *name, LLVMValueRef value,
414 LLVMTypeRef type, LLVMTypeRef element_type, bool is_function);
415
416// Helper function to extract element type from AST
418 AstNode *type_node);
419
420LLVMValueRef codegen_expr_literal(CodeGenContext *ctx, AstNode *node);
421LLVMValueRef codegen_expr_identifier(CodeGenContext *ctx, AstNode *node);
422LLVMValueRef codegen_expr_binary(CodeGenContext *ctx, AstNode *node);
423LLVMValueRef codegen_expr_unary(CodeGenContext *ctx, AstNode *node);
424LLVMValueRef codegen_expr_call(CodeGenContext *ctx, AstNode *node);
425LLVMValueRef codegen_expr_assignment(CodeGenContext *ctx, AstNode *node);
426LLVMValueRef codegen_expr_array(CodeGenContext *ctx, AstNode *node);
427LLVMValueRef codegen_expr_index(CodeGenContext *ctx, AstNode *node);
428LLVMValueRef codegen_expr_cast(CodeGenContext *ctx, AstNode *node);
429LLVMValueRef codegen_expr_input(CodeGenContext *ctx, AstNode *node);
430LLVMValueRef codegen_expr_system(CodeGenContext *ctx, AstNode *node);
431LLVMValueRef codegen_expr_syscall(CodeGenContext *ctx, AstNode *node);
432LLVMValueRef codegen_expr_sizeof(CodeGenContext *ctx, AstNode *node);
433LLVMValueRef codegen_expr_alloc(CodeGenContext *ctx, AstNode *node);
434LLVMValueRef codegen_expr_free(CodeGenContext *ctx, AstNode *node);
435LLVMValueRef codegen_expr_deref(CodeGenContext *ctx, AstNode *node);
436LLVMValueRef codegen_expr_addr(CodeGenContext *ctx, AstNode *node);
437LLVMValueRef codegen_expr_struct_literal(CodeGenContext *ctx, AstNode *node);
438
439// =============================================================================
440// AST NODE HANDLERS - STATEMENT TYPES
441// =============================================================================
442
443LLVMValueRef codegen_stmt_program(CodeGenContext *ctx, AstNode *node);
444LLVMValueRef codegen_stmt_expression(CodeGenContext *ctx, AstNode *node);
445LLVMValueRef codegen_stmt_var_decl(CodeGenContext *ctx, AstNode *node);
446LLVMValueRef codegen_stmt_function(CodeGenContext *ctx, AstNode *node);
447
450LLVMValueRef codegen_stmt_enum(CodeGenContext *ctx, AstNode *node);
451
452LLVMValueRef codegen_stmt_return(CodeGenContext *ctx, AstNode *node);
453LLVMValueRef codegen_stmt_block(CodeGenContext *ctx, AstNode *node);
454LLVMValueRef codegen_stmt_if(CodeGenContext *ctx, AstNode *node);
455LLVMValueRef codegen_stmt_print(CodeGenContext *ctx, AstNode *node);
456LLVMValueRef codegen_stmt_defer(CodeGenContext *ctx, AstNode *node);
457
458LLVMValueRef codegen_infinite_loop(CodeGenContext *ctx, AstNode *node);
459LLVMValueRef codegen_while_loop(CodeGenContext *ctx, AstNode *node);
460LLVMValueRef codegen_for_loop(CodeGenContext *ctx, AstNode *node);
461LLVMValueRef codegen_loop(CodeGenContext *ctx, AstNode *node);
462
463LLVMValueRef codegen_stmt_break_continue(CodeGenContext *ctx, AstNode *node);
464
465LLVMValueRef codegen_stmt_switch(CodeGenContext *ctx, AstNode *node);
466LLVMValueRef codegen_stmt_case(CodeGenContext *ctx, AstNode *node);
467LLVMValueRef codegen_stmt_default(CodeGenContext *ctx, AstNode *node);
468LLVMValueRef codegen_case_value(CodeGenContext *ctx, AstNode *case_value);
470 AstNode *member_expr);
471
472// =============================================================================
473// DEFER MANAGEMENT FUNCTIONS
474// =============================================================================
475
477void push_defer_statement(CodeGenContext *ctx, AstNode *statement);
479 DeferredStatement *defers);
482
483// =============================================================================
484// RANGE SUPPORT FUNCTIONS - Add this new section
485// =============================================================================
486
487// Range type checking and manipulation
488bool is_range_type(LLVMTypeRef type);
489LLVMValueRef get_range_start_value(CodeGenContext *ctx,
490 LLVMValueRef range_struct);
491LLVMValueRef get_range_end_value(CodeGenContext *ctx,
492 LLVMValueRef range_struct);
493
494// Range creation helper (optional - for consistent range creation)
495LLVMValueRef create_range_struct(CodeGenContext *ctx, LLVMValueRef start,
496 LLVMValueRef end);
498 LLVMTypeRef element_type);
499
500// Range operations (add these if you need them later)
501LLVMValueRef range_contains(CodeGenContext *ctx, LLVMValueRef range_struct,
502 LLVMValueRef value);
503LLVMValueRef range_length(CodeGenContext *ctx, LLVMValueRef range_struct);
504
505// =============================================================================
506// AST NODE HANDLERS - TYPE SYSTEM
507// =============================================================================
508
509LLVMTypeRef codegen_type_basic(CodeGenContext *ctx, AstNode *node);
510LLVMTypeRef codegen_type_pointer(CodeGenContext *ctx, AstNode *node);
511LLVMTypeRef codegen_type_array(CodeGenContext *ctx, AstNode *node);
512LLVMTypeRef codegen_type_function(CodeGenContext *ctx, AstNode *node);
const char * get_module_name_from_access(AstNode *node)
Get module name/alias from a compile-time member access node.
Definition member_access.c:263
LLVMValueRef codegen_expr_struct_assignment(CodeGenContext *ctx, AstNode *node)
Definition struct.c:523
void print_struct_info(CodeGenContext *ctx, const char *struct_name)
Definition struct_helpers.c:96
void debug_struct_layout(CodeGenContext *ctx, const char *struct_name)
Definition struct_helpers.c:146
void add_symbol(CodeGenContext *ctx, const char *name, LLVMValueRef value, LLVMTypeRef type, bool is_function)
Definition llvm.c:402
LLVMValueRef codegen_stmt_program(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:9
LLVMLinkage get_function_linkage(AstNode *node)
Definition llvm.c:585
LLVMValueRef range_length(CodeGenContext *ctx, LLVMValueRef range_struct)
Definition expr.c:62
LLVMValueRef codegen_expr_sizeof(CodeGenContext *ctx, AstNode *node)
Definition expr.c:1914
LLVMValueRef codegen_expr_free(CodeGenContext *ctx, AstNode *node)
Definition expr.c:1963
LLVMValueRef codegen_stmt_print(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:860
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:698
LLVMValueRef codegen_member_address(CodeGenContext *ctx, AstNode *node, LLVMTypeRef *value_type_out, LLVMTypeRef *element_type_out)
Definition struct_access.c:535
LLVMValueRef codegen_stmt_function(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:176
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:1041
void branch_if_no_terminator(CodeGenContext *ctx, LLVMBasicBlockRef target)
Definition helpers.c:46
bool is_main_module(ModuleCompilationUnit *unit)
Definition module_handles.c:673
void init_symbol_cache(void)
Definition module_handles.c:19
bool compile_modules_to_objects(CodeGenContext *ctx, const char *output_dir)
Definition llvm.c:235
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:691
LLVMValueRef codegen_expr_addr(CodeGenContext *ctx, AstNode *node)
Definition expr.c:2092
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:1293
void init_defer_stack(CodeGenContext *ctx)
Definition defer.c:3
LLVMValueRef codegen_stmt_switch(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:1185
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:548
LLVMValueRef codegen_stmt_defer(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:977
void import_variable_symbol(CodeGenContext *ctx, LLVM_Symbol *source_symbol, ModuleCompilationUnit *source_module, const char *alias)
Definition module_handles.c:576
LLVMValueRef codegen_expr_system(CodeGenContext *ctx, AstNode *node)
Definition expr.c:1515
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:677
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:465
bool block_has_terminator(LLVMBuilderRef builder)
Definition helpers.c:40
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:335
LLVMValueRef codegen_expr_array(CodeGenContext *ctx, AstNode *node)
Definition expr.c:721
void cg_error_help(CodeGenContext *ctx, AstNode *node, const char *error_type, const char *help, const char *format,...)
Definition helpers.c:138
ModuleCompilationUnit * create_module_unit(CodeGenContext *ctx, const char *module_name)
Definition llvm.c:317
bool is_struct_type(CodeGenContext *ctx, LLVMTypeRef type)
Definition struct_helpers.c:8
LLVMValueRef codegen_stmt_default(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:1438
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:171
LLVMValueRef codegen_stmt_break_continue(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:983
LLVMValueRef codegen_for_loop(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:1094
LLVMValueRef build_global_string(CodeGenContext *ctx, const char *str, const char *name)
Definition helpers.c:57
LLVMValueRef codegen_expr_deref(CodeGenContext *ctx, AstNode *node)
Definition expr.c:1998
char * print_llvm_ir(CodeGenContext *ctx)
Definition llvm.c:541
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:702
LLVMValueRef codegen_stmt(CodeGenContext *ctx, AstNode *node)
Definition lookup.c:64
LLVMValueRef codegen_stmt_enum(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:513
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:1257
void setup_module_debug_info(CodeGenContext *ctx, ModuleCompilationUnit *unit, const char *filename)
Definition llvm.c:658
CodeGenContext * init_codegen_context(ArenaAllocator *arena)
Definition llvm.c:461
LLVMValueRef alloca_and_store(CodeGenContext *ctx, LLVMTypeRef type, LLVMValueRef value, const char *name)
Definition helpers.c:7
LLVM_Symbol * find_symbol_global(CodeGenContext *ctx, const char *name, const char *module_name)
Definition llvm.c:371
bool is_range_type(LLVMTypeRef type)
Definition stmt.c:799
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:349
LLVMValueRef codegen_stmt_block(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:643
LLVMTypeRef get_int_type(CodeGenContext *ctx, unsigned bits)
Definition type_cache.c:30
LLVMValueRef codegen_stmt_return(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:556
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:345
void generate_external_declarations(CodeGenContext *ctx, ModuleCompilationUnit *target_module)
Definition llvm.c:413
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:598
#define MAX_LINK_LIBS
Definition llvm.h:25
void cleanup_codegen_context(CodeGenContext *ctx)
Definition llvm.c:497
LLVMValueRef array_gep(CodeGenContext *ctx, LLVMTypeRef array_type, LLVMValueRef array_ptr, LLVMValueRef index, const char *name)
Definition helpers.c:32
LLVM_Symbol * find_symbol(CodeGenContext *ctx, const char *name)
Definition llvm.c:409
void struct_gep_store(CodeGenContext *ctx, LLVMTypeRef struct_type, LLVMValueRef ptr, unsigned index, LLVMValueRef value)
Definition helpers.c:24
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:697
LLVMValueRef codegen_expr_assignment(CodeGenContext *ctx, AstNode *node)
Definition expr.c:491
LLVMValueRef codegen_expr_unary(CodeGenContext *ctx, AstNode *node)
Definition expr.c:152
void finalize_all_debug_info(CodeGenContext *ctx)
Definition llvm.c:691
LLVMTypeRef codegen_type_array(CodeGenContext *ctx, AstNode *node)
Definition type.c:76
LLVMValueRef codegen_stmt_case(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:1423
void import_function_symbol(CodeGenContext *ctx, LLVM_Symbol *source_symbol, ModuleCompilationUnit *source_module, const char *alias)
Definition module_handles.c:521
LLVMValueRef codegen_stmt_use(CodeGenContext *ctx, AstNode *node)
Definition module_handles.c:378
bool generate_program_modules(CodeGenContext *ctx, AstNode *ast_root, const char *output_dir)
Definition llvm.c:528
LLVMValueRef codegen_stmt_field(CodeGenContext *ctx, AstNode *node)
Definition struct.c:506
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:281
LLVMValueRef codegen_expr_syscall(CodeGenContext *ctx, AstNode *node)
Generate LLVM IR for syscall expression.
Definition expr.c:1575
void set_current_module(CodeGenContext *ctx, ModuleCompilationUnit *module)
Definition llvm.c:345
LLVMValueRef get_range_end_value(CodeGenContext *ctx, LLVMValueRef range_struct)
Definition stmt.c:839
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:1174
void push_defer_statement(CodeGenContext *ctx, AstNode *statement)
Definition defer.c:9
LLVMValueRef codegen_expr_input(CodeGenContext *ctx, AstNode *node)
Definition expr.c:1342
void add_struct_type(CodeGenContext *ctx, StructInfo *struct_info)
Definition struct.c:37
bool is_enum_constant(LLVM_Symbol *sym)
Definition stmt.c:486
LLVMValueRef codegen_expr_index(CodeGenContext *ctx, AstNode *node)
Definition expr.c:838
bool validate_module_access(CodeGenContext *ctx, const char *prefix, const char *symbol_name)
Validate that a module access is permitted.
Definition member_access.c:288
LLVMValueRef codegen_stmt_if(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:682
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:247
int64_t get_enum_constant_value(LLVM_Symbol *sym)
Definition stmt.c:500
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:15
void finalize_module_debug_info(ModuleCompilationUnit *unit)
Definition llvm.c:683
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:819
LLVM_Symbol * find_symbol_with_module_support(CodeGenContext *ctx, const char *name)
Definition module_handles.c:647
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:55
LLVMValueRef codegen_enum_member_case(CodeGenContext *ctx, AstNode *member_expr)
Definition stmt.c:1322
void cg_error(CodeGenContext *ctx, AstNode *node, const char *error_type, const char *format,...)
Definition helpers.c:130
void print_module_info(CodeGenContext *ctx)
Definition module_handles.c:683
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:30
void import_module_symbols(CodeGenContext *ctx, ModuleCompilationUnit *source_module, const char *alias)
Definition module_handles.c:485
void generate_cleanup_blocks(CodeGenContext *ctx)
Definition defer.c:65
LLVMValueRef codegen_expr_call(CodeGenContext *ctx, AstNode *node)
Definition expr.c:255
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:360
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:406
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:126
bool generate_assembly_file(CodeGenContext *ctx, const char *asm_filename, bool is_debug)
Definition llvm.c:556
LLVM_Symbol * find_symbol_in_module(ModuleCompilationUnit *module, const char *name)
Definition llvm.c:361
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:81
LLVMValueRef codegen_expr_alloc(CodeGenContext *ctx, AstNode *node)
Definition expr.c:1932
bool is_float_type(LLVMTypeRef type)
Definition type_cache.c:72
LLVMValueRef codegen_infinite_loop(CodeGenContext *ctx, AstNode *node)
Definition stmt.c:1001
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
Definition ast.h:142
Definition llvm.h:104
bool is_debug
Definition llvm.h:132
const char * target_os
Definition llvm.h:129
LLVMBasicBlockRef loop_break_block
Definition llvm.h:124
LLVMBuilderRef builder
Definition llvm.h:107
LLVMValueRef current_function
Definition llvm.h:122
LLVMContextRef context
Definition llvm.h:106
ArenaAllocator * arena
Definition llvm.h:136
CommonTypes common_types
Definition llvm.h:126
LLVMMetadataRef current_func_di
Definition llvm.h:133
LLVMModuleRef struct DeferredStatement * deferred_statements
Definition llvm.h:117
size_t deferred_capacity
Definition llvm.h:119
StructInfo * struct_types
Definition llvm.h:127
LLVMBasicBlockRef loop_continue_block
Definition llvm.h:123
size_t deferred_count
Definition llvm.h:118
ModuleCompilationUnit * modules
Definition llvm.h:110
ModuleCompilationUnit * current_module
Definition llvm.h:111
Definition llvm.h:87
LLVMTypeRef void_type
Definition llvm.h:95
LLVMTypeRef i64
Definition llvm.h:89
LLVMTypeRef i32
Definition llvm.h:89
LLVMTypeRef f32
Definition llvm.h:92
LLVMValueRef const_i64_1
Definition llvm.h:100
LLVMTypeRef i8_ptr
Definition llvm.h:96
LLVMTypeRef i8
Definition llvm.h:89
LLVMValueRef const_i32_0
Definition llvm.h:99
LLVMTypeRef f64
Definition llvm.h:92
LLVMValueRef const_i64_0
Definition llvm.h:100
LLVMValueRef const_i32_1
Definition llvm.h:99
LLVMTypeRef i1
Definition llvm.h:89
LLVMTypeRef i16
Definition llvm.h:89
Definition llvm.h:68
struct DeferredStatement * next
Definition llvm.h:71
AstNode * statement
Definition llvm.h:69
LLVMBasicBlockRef cleanup_block
Definition llvm.h:70
Definition llvm.h:31
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
Definition llvm.h:41
size_t token_count
Definition llvm.h:53
bool is_main_module
Definition llvm.h:45
size_t link_lib_count
Definition llvm.h:49
LLVMDIBuilderRef dibuilder
Definition llvm.h:56
LLVMMetadataRef compile_unit
Definition llvm.h:57
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
const char * file_path
Definition llvm.h:54
Token * tokens
Definition llvm.h:52
LLVMModuleRef LLVM_Symbol * symbols
Definition llvm.h:44
LLVMMetadataRef file_metadata
Definition llvm.h:58
Definition llvm.h:61
char ** dependencies
Definition llvm.h:63
const char * module_name
Definition llvm.h:62
bool processed
Definition llvm.h:65
size_t dep_count
Definition llvm.h:64
Definition llvm.h:150
StructInfo * info
Definition llvm.h:152
struct StructHashEntry * next
Definition llvm.h:153
const char * name
Definition llvm.h:151
Definition llvm.h:156
StructHashEntry * buckets[SYMBOL_HASH_SIZE]
Definition llvm.h:157
Definition llvm.h:74
bool * field_is_public
Definition llvm.h:81
bool is_public
Definition llvm.h:83
char * name
Definition llvm.h:75
LLVMTypeRef * field_types
Definition llvm.h:79
LLVMTypeRef * field_element_types
Definition llvm.h:80
size_t field_count
Definition llvm.h:82
const char * module_name
Definition llvm.h:76
struct StructInfo * next
Definition llvm.h:84
char ** field_names
Definition llvm.h:78
LLVMTypeRef llvm_type
Definition llvm.h:77
Definition llvm.h:139
const char * key
Definition llvm.h:140
struct SymbolHashEntry * next
Definition llvm.h:142
LLVM_Symbol * symbol
Definition llvm.h:141
Definition llvm.h:145
SymbolHashEntry * buckets[SYMBOL_HASH_SIZE]
Definition llvm.h:146
Represents a single token extracted by the lexer.
Definition lexer.h:151