Skip to content

Backend & Runtime

Backend & Runtime

LLVM Code Emission

The emitter (src/codegen/native/emitter.tg) converts Core-IR into LLVM IR text (.ll files).

Emitter Entry Point

func emit_program_core(core: CoreProgram, module_name: String) -> String

If the Core-IR has errors (error_count > 0), the emitter returns an empty string. Otherwise, it normalizes the source and delegates to the source-level emitter.

Emission Sub-Modules

Located in src/codegen/native/emit/:

ModulePurpose
source.tgTop-level program emission
expr.tgExpression emission (arithmetic, calls, field access)
exprcond.tgConditional expression emission
exprcore.tgCore expression patterns
stmt.tgStatement emission
stmtcore.tgCore statement patterns
stmtfn.tgFunction statement emission
flow.tgControl flow emission (if/else, while, return)
string.tgString literal and concatenation emission
text.tgText processing utilities
helpers.tgShared helper functions

LLVM IR Output

The emitter produces standard LLVM IR text. For a simple hello world:

; ModuleID = 'hello'
declare i32 @puts(i8*)
@.str.0 = private unnamed_addr constant [24 x i8] c"Hello Self-Hosted World!\00"
define i32 @main() {
entry:
call i32 @puts(i8* getelementptr ([24 x i8], [24 x i8]* @.str.0, i32 0, i32 0))
ret i32 0
}

LLVM API Bindings

The compiler has bindings to LLVM’s C API in src/codegen/llvm/:

FilePurpose
entry.tgLLVM module creation entry point
api/core.tgCore LLVM operations (modules, functions, basic blocks)
api/target.tgTarget machine configuration

Runtime ABI

Compiled Thagore programs link against a runtime ABI library that provides essential functions.

Runtime Functions

FunctionPurpose
print / putsConsole output
__thg_str_substrString substring extraction
__thg_str_addString concatenation
__thg_str_containsString search
__thg_str_starts_withPrefix check
__thg_str_ends_withSuffix check
__thg_str_trimWhitespace trimming
__thg_str_replaceString replacement
__thg_str_lower / upperCase conversion
__string_from_codepointCreate string from Unicode codepoint
__thg_ptr_nullNull pointer
__thg_ptr_set / getPointer array operations
__thg_arg_count / getCommand-line arguments
__thg_fs_read_textFile reading
__thg_fs_write_textFile writing
__fs_exists / mkdir / list_dirFile system operations
__process_runShell command execution
__process_exec_argvProcess spawning with args
__io_read_lineConsole input
__thg_input_i32Integer input
__time_now_msCurrent time in milliseconds
__time_sleepThread sleep
__http_get / postHTTP client
__thg_py_*Python bridge functions

Runtime Artifacts

PlatformFileSize
Windows x64thag_runtime.lib~3.5 MB
Linux x64libthag_runtime.a~2.8 MB
macOS ARM64libthag_runtime.a~2.8 MB

Linking Process

After LLVM IR emission, the compiler invokes Clang to:

  1. Compile .ll to object file (.obj / .o)
  2. Link with the runtime ABI library
  3. Link with system libraries (libc, etc.)
  4. Produce the final native executable
Terminal window
# What the compiler does internally:
clang -O2 program.ll -o program.exe -L. -lthag_runtime

Interpreter Mode

The compiler also includes an interpreter (src/codegen/interpreter/eval.tg) for evaluating expressions at compile time (used by the comptime feature).