SchismC
A true assembly-based Windows port of HolyC, the programming language from TempleOS.
Overview
SchismC is a faithful reimplementation of HolyC that compiles directly to x86-64 assembly and generates native Windows executables. Unlike transpilers that generate C code, SchismC maintains the original HolyC philosophy of direct assembly integration and ahead-of-time compilation.
π MAJOR UPDATE: Full Compilation Pipeline Working!
β SchismC now successfully compiles HolyC programs to working Windows executables!
The compiler has been completely overhauled and now features:
- Working MASM Toolchain Integration - Direct compilation to Windows PE executables
- Fixed Memory Management - Resolved all buffer overflows and memory corruption issues
- **Windowsβ¦
SchismC
A true assembly-based Windows port of HolyC, the programming language from TempleOS.
Overview
SchismC is a faithful reimplementation of HolyC that compiles directly to x86-64 assembly and generates native Windows executables. Unlike transpilers that generate C code, SchismC maintains the original HolyC philosophy of direct assembly integration and ahead-of-time compilation.
π MAJOR UPDATE: Full Compilation Pipeline Working!
β SchismC now successfully compiles HolyC programs to working Windows executables!
The compiler has been completely overhauled and now features:
- Working MASM Toolchain Integration - Direct compilation to Windows PE executables
- Fixed Memory Management - Resolved all buffer overflows and memory corruption issues
- Windows Header Compatibility - Fixed all conflicts with Windows system headers
- Complete Compilation Pipeline - From HolyC source to working executable
- Hello World Success - Successfully compiles and runs βHello, World!β programs
Features
- β True Assembly-Based Compilation: Generates x86-64 machine code directly
- β HolyC Language Compatibility: Supports HolyC syntax and features
- β Working Windows Executables: Generates native Windows PE executables that actually run
- β MASM Toolchain Integration: Uses Microsoft Macro Assembler and Linker
- β Function Support: Complete function declarations, calls, and return statements
- β x64 Calling Convention: Proper Windows x64 calling convention implementation
- β MASM Output Generation: Generates Microsoft Macro Assembler (MASM) files
- β Inline Assembly Support: Seamless mixing of HolyC and assembly code
- β Ahead-of-Time Compilation: Generates native Windows PE executables
- β Optimization Passes: Multiple optimization levels for performance
- β Windows Integration: Native Windows API access and system calls
- β Console Output: Working string literal output using Windows API
Architecture
SchismC follows a traditional compiler architecture with three main phases:
Frontend
- Lexer: Tokenizes HolyC source code with Windows header compatibility
- Parser: Builds Abstract Syntax Tree (AST) with circular dependency resolution
Middle-End
- Intermediate Code Generation: Converts AST to intermediate representation
- Optimization: Multiple optimization passes for performance
Backend
- Assembly Generation: Converts intermediate code to x86-64 assembly
- MASM Output: Generates Microsoft Macro Assembler files for linking
- MASM Toolchain: Direct integration with Microsoft Macro Assembler and Linker
- x64 Calling Convention: Implements proper Windows x64 function calling
- Register Allocation: Efficient register management with X86_REG_* naming
- AOT Compilation: Generates native Windows executables
Project Structure
SchismC/
βββ src/
β βββ frontend/
β β βββ lexer/ # Tokenization with Windows compatibility
β β βββ parser/ # AST generation with circular dependency fixes
β βββ middleend/
β β βββ intermediate/ # IC generation
β β βββ optimization/ # Optimization passes
β βββ backend/
β β βββ aot/ # Ahead-of-time compilation (legacy)
β β βββ assembly/ # Assembly generation and MASM output
β β βββ codegen/ # Code generation
β β βββ registers/ # Register allocation
β βββ runtime/ # Runtime library with Windows compatibility
βββ include/ # Header files with Windows header fixes
βββ tests/ # Test programs
β βββ hello_world.hc # Working Hello World program
β βββ simple_console_demo.hc # Function demonstration
β βββ test_functions.hc # Function test cases
β βββ *.hc # Various test programs
βββ tools/ # Build tools
β βββ create_working_pe.c # PE generation tool
β βββ test_toolchain.bat # Toolchain testing
βββ docs/ # Documentation
βββ bin/ # Compiled executables
β βββ schismc.exe # Main compiler executable
βββ obj/ # Object files
βββ build_masm.bat # MASM build script
βββ run_demo.bat # Console demo runner
βββ demo_showcase.bat # Feature demonstration
βββ function_demo.asm # Console application demo
βββ output.asm # Generated MASM output
βββ hello_world.exe # Working Hello World executable
βββ test_masm_output.exe # MASM toolchain output
Building
Prerequisites
- Windows 10/11
- GCC (MinGW-w64 recommended)
- Make utility
- Microsoft Macro Assembler (MASM) -
ml64.exe
- Microsoft Linker (LINK) -
link.exe
- Windows SDK - for
kernel32.lib
Quick Build
make all
Manual Build
make all
Debug Build
make debug
Release Build
make release
Usage
Basic Compilation (Working!)
schismc.exe tests\hello_world.hc
This generates a working Windows executable that displays βHello, World!β
Manual MASM Compilation
# Generate MASM assembly
schismc.exe tests\hello_world.hc
# Assemble with MASM
& "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\bin\Hostx64\x64\ml64.exe" /c /Cp /Cx /W3 /nologo output.asm
# Link with Microsoft Linker
& "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\bin\Hostx64\x64\link.exe" /SUBSYSTEM:CONSOLE /ENTRY:main output.obj "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.26100.0\um\x64\kernel32.lib" /OUT:hello_world.exe
# Run the executable
.\hello_world.exe
π― Working Example: Hello World
Input (tests/hello_world.hc
):
I64 main() {
"Hello, World!";
return 0;
}
Generated MASM Assembly (output.asm
):
; Generated by SchismC - MASM Assembly Output
; Target: Windows x64
extrn GetStdHandle:PROC
extrn WriteConsoleA:PROC
extrn ExitProcess:PROC
.data
str_literal_0 DB "Hello, World!", 0
.code
; Main function
main PROC
push rbp ; Save caller's frame pointer
mov rbp, rsp ; Set up new frame pointer
sub rsp, 32h ; Allocate local space
; Get stdout handle
mov rcx, -11 ; STD_OUTPUT_HANDLE
call GetStdHandle
mov rdi, rax ; Save handle
; Write string to console
mov rcx, rdi ; hConsoleOutput
lea rdx, [str_literal_0] ; lpBuffer
mov r8, 13 ; nNumberOfCharsToWrite
mov r9, 0 ; lpNumberOfCharsWritten (NULL)
push 0 ; lpReserved (NULL)
sub rsp, 32 ; Shadow space
call WriteConsoleA
add rsp, 40 ; Clean up stack
mov rax, 0 ; Integer literal
mov rsp, rbp ; Restore stack pointer
pop rbp ; Restore caller's frame pointer
ret ; Return to caller
main ENDP
END
Output:
Hello, World!
HolyC Language Features
Built-in Types
I64
: 64-bit signed integerF64
: 64-bit floating pointString
: String typeBool
: Boolean typeU8
,U16
,U32
,U64
: Unsigned integersI8
,I16
,I32
: Signed integers
Expression Support
- β
Arithmetic Operators:
+
,-
,*
,/
,%
- β
Relational Operators:
<
,>
,<=
,>=
,==
,!=
- β
Logical Operators:
&&
,||
,!
- β
Bitwise Operators:
&
,|
,^
,~
,<<
,>>
- β
Unary Operators:
+
,-
,!
,~
,++
,--
- β
Assignment Operators:
=
,+=
,-=
,*=
,/=
,%=
,&=
,|=
,^=
,<<=
,>>=
- β
Ternary Operator:
condition ? true_expr : false_expr
- β Operator Precedence: Proper parsing hierarchy
- β
Parenthesized Expressions:
(expression)
- β
Function Calls in Expressions:
func(arg1, arg2)
Variable Support
- β
Variable Declarations:
I64 x;
- β
Variable Initialization:
I64 x = 10;
- β
Variable Assignment:
x = 20;
- β Variable Scope Management: Local and global variables
- β Type Checking: Comprehensive type validation and coercion
Built-in Functions
Print()
: Output function with format specifiersMAlloc()
: Memory allocationFree()
: Memory deallocationStrNew()
: String creationStrPrint()
: String formatting
Function Support
- Function Declarations: Full function signature parsing with parameters
- Function Calls: x64 calling convention with proper argument passing
- Return Statements: Support for simple values and complex expressions
- Parameter Handling: Proper parameter scope and type management
- x64 Calling Convention: RCX, RDX, R8, R9 for first 4 arguments, stack for additional
Control Structures
- β
if/else
statements - β
while
loops - β
for
loops - β
do-while
loops - β
switch
statements with case labels and range expressions - β
goto
statements and labels (including exported and local labels) - β
return
statements - β
break
andcontinue
statements
Array Support
- β
Array declarations with size:
I64 arr[5];
- β
Array initialization:
I64 arr[5] = {1, 2, 3, 4, 5};
- β
Array access with
[]
operator:arr[0]
,arr[i]
- β
Dynamic arrays (declared without size):
I64 arr[];
- β Multi-dimensional array support (planned)
Pointer Support
- β
Pointer declarations:
I64 *ptr;
,U8 *str;
- β
Address-of operator:
&variable
- β
Dereference operator:
*ptr
- β
Pointer assignment:
ptr = &variable;
,*ptr = value;
- β
Pointer arithmetic:
ptr + 1
,ptr - 1
,ptr++
,ptr--
- β
Pointer to array elements:
I64 *arr_ptr = &arr[0];
Struct/Class Support
- β
Class definitions:
class ClassName { ... };
- β
Union definitions:
union UnionName { ... };
- β
Member declarations:
I32 x;
,U8 *name;
- β
Member access:
object.member
,object->member
- β
Public classes:
public class ClassName { ... };
- β
Inheritance:
class Dog : Animal { ... };
- β Stack and heap allocation support
Assembly Integration
I64 add_numbers(I64 a, I64 b) {
I64 result;
asm {
mov rax, a
add rax, b
mov result, rax
}
return result;
}
Example Programs
Hello World (Working!)
I64 main() {
"Hello, World!";
return 0;
}
Function Demo
I64 get_hello_value() {
return 42;
}
I64 get_world_value() {
return 24;
}
I64 calculate_result() {
return 66;
}
Simple Calculator with Function Calls
I64 add(I64 a, I64 b) {
return a + b;
}
I64 multiply(I64 x, I64 y) {
return x * y;
}
I64 main() {
I64 a = 10;
I64 b = 3;
I64 sum = add(a, b);
I64 product = multiply(a, b);
return sum + product;
}
π§ Major Fixes and Improvements
Memory Management Fixes
- β
Buffer Overflow Prevention: Added bounds checking in
aot_append_binary
- β
Uninitialized Memory: Fixed uninitialized
AOTContext
andAssemblyContext
structures - β
Memory Corruption: Resolved binary size corruption in
aot_write_binary_windows
- β
Undefined Functions: Replaced undefined
aot_new()
andaot_free()
with proper implementations
Windows Compatibility Fixes
- β
Header Conflicts: Renamed
TokenType
toSchismTokenType
to avoid Windows header conflicts - β
Register Enum Conflicts: Renamed
REG_*
toX86_REG_*
to avoid Windows header conflicts - β
Bool Type Conflicts: Resolved conflicts between local and system
Bool
definitions - β
strdup Implementation: Added Windows-compatible
strdup
implementation for MinGW
Compilation Fixes
- β
Format Specifiers: Fixed
%lld
to%I64d
for MinGW compatibility - β Switch Statements: Added default cases to prevent compilation warnings
- β
Circular Dependencies: Fixed circular dependency in
parse_range_comparison
- β Missing Directories: Created missing object file directories
- β Multiple Definitions: Resolved duplicate function definitions
MASM Toolchain Integration
- β MASM Assembly Generation: Proper 64-bit MASM assembly output
- β
Windows API Integration: Direct calls to
GetStdHandle
andWriteConsoleA
- β
Microsoft Linker Integration: Proper linking with
kernel32.lib
- β PE Executable Generation: Working Windows PE executables
Testing
Test the Working Hello World
# Compile and run Hello World
schismc.exe tests\hello_world.hc
.\hello_world.exe
Run the test suite
make test
Test function support
.\bin\schismc.exe tests\simple_console_demo.hc
Development Status
This project has achieved a major milestone with a working compilation pipeline!
β Completed Phases
- Phase 1: Project structure and foundation
- Phase 2: Frontend (Lexer + Parser) with Windows compatibility
- Phase 3: Middle-end (Intermediate code + Optimization)
- Phase 4: Backend (Assembly generation + MASM output)
- Phase 5: Function Support (Declarations, calls, returns)
- Phase 6: x64 Calling Convention Implementation
- Phase 7: MASM Output Generation
- Phase 8: Console Application Demo
- Phase 9: Variable support and expressions
- Phase 10: Basic control flow structures (if/else, while)
- Phase 11: Assembly resolution and AOT compilation
- Phase 12: Advanced control flow (for, do-while, break/continue)
- Phase 13: Switch statements with case labels and range expressions
- Phase 14: Goto statements and labels
- Phase 15: Array support (declarations, access, initialization)
- Phase 16: Pointer support and arithmetic
- Phase 17: Struct/class support
- Phase 18: Working MASM Toolchain Integration π
- Phase 19: Memory Management and Windows Compatibility π
- Phase 20: Hello World Success π
π Recently Completed Major Features
- β Working Compilation Pipeline: HolyC β MASM β Executable
- β Memory Management Overhaul: Fixed all buffer overflows and corruption
- β Windows Header Compatibility: Resolved all system header conflicts
- β MASM Toolchain Integration: Direct Microsoft toolchain integration
- β Console Output: Working string literal output using Windows API
- β PE Executable Generation: Native Windows executables that actually run
- β Function Declarations: Complete parsing of function signatures with parameters
- β Function Calls: x64 calling convention with proper argument passing
- β Return Statements: Support for simple values and complex expressions
- β MASM Output: Generates Microsoft Macro Assembler files
- β Console Applications: Working console demos showing function support
- β x64 Calling Convention: Proper Windows x64 function calling implementation
- β Variable Support: Complete variable declarations, assignments, and scope management
- β Expression System: Full expression parsing with operator precedence
- β Type System: Comprehensive type checking and validation
- β Bitwise Operators: Complete bitwise operation support
- β Control Flow: if/else, while, for, do-while, switch, and goto statements
- β Loop Control: break and continue statements
- β Switch Statements: Case labels, range expressions (case 5...10), and default cases
- β Goto and Labels: Unconditional jumps with exported (label::) and local (@@label:) labels
- β Array Support: Array declarations, initialization, and access with [] operator
- β Pointer Support: Pointer declarations, dereferencing, address-of, and pointer arithmetic
- β Struct/Class Support: Class definitions, member access, inheritance, and object-oriented features
- β Ternary Operator: Conditional expressions
- β Compound Assignment: All compound assignment operators
- β AOT Compilation: Native Windows PE executable generation
π Next Development Priorities
- PowerShell Command Execution: Fix the command execution in the compiler for automated builds
- More Complex Programs: Test and support more advanced HolyC features
- Error Handling: Improve error messages and debugging
- Performance Optimization: Optimize the generated assembly code
- Standard Library: Implement more HolyC built-in functions
- Documentation: Expand documentation and examples
Contributing
Contributions are welcome! The compiler now has a solid foundation with working compilation. Please see the development guidelines in the docs/
directory.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Terry A. Davis for creating TempleOS and HolyC
- The TempleOS community for preserving and documenting the original system
- The open source community for tools and inspiration
- Microsoft for providing the MASM and Linker tools
References
βIn the beginning was the Word, and the Word was with God, and the Word was God.β - John 1:1