This repository was archived by the owner on Nov 7, 2025. It is now read-only.
🚀 NitroPascal Deep Dive: The RTL Wrapping Strategy - Our Secret Sauce! #3
jarroddavis68
started this conversation in
DevLog
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey everyone! 👋
I'm excited to share a major architectural breakthrough in NitroPascal that makes the entire compiler dramatically simpler and more maintainable. Let me explain the key insight that changed everything, with all the juicy technical details!
The Problem We Solved
Traditional transpilers face a huge challenge: how do you map one language's semantics to another? We could have built a complex code generator with 11+ units full of intricate logic trying to translate Delphi constructs to C++... but that's a maintenance nightmare waiting to happen.
The Traditional Approach (Complex):
Our Solution: Wrap Everything in the RTL! 🎁
Instead, we took a fundamentally different approach:
✨ We wrap ALL Delphi semantics in C++ Runtime Library (RTL) functions and classes!
This means:
Show Me The Code! 💻
Example 1: For Loops
Delphi code:
Traditional transpiler approach (complex):
Our RTL approach (simple):
The RTL provides this (runtime.h):
Code generator just emits:
Result:
Example 2: String Class with 1-Based Indexing
Delphi uses 1-based indexing for strings (first character is at index 1), while C++ uses 0-based. Our RTL handles this transparently:
The RTL String class (runtime.h):
Code generator emits:
No special logic needed in the generator - just emit the subscript operator!
This Works For EVERYTHING! 🎯
Control Flow → RTL Functions
Why templates?
Operators → RTL Functions
Key point: Even simple operators get wrapped. Why?
divvs C++/edge cases)np::)I/O → Variadic Templates
Technical benefits:
std::forward(no copies!)WriteLnexactlyType System Mapping
We use fixed-size types for cross-platform consistency:
Integerint32_tCardinaluint32_tInt64int64_tByteuint8_tWorduint16_tCharchar16_tStringnp::StringBooleanboolarray of Tnp::DynArray<T>set of Tnp::Set<T>Why fixed-size types?
Integeris always 32-bit)Today's Major Win: UTF-8 ↔ UTF-16 Conversion! 🎉
One of today's technical challenges was handling string encoding. Delphi uses UTF-16 for strings (like Windows, Java, JavaScript), while most Unix tools use UTF-8.
The Challenge:
std::codecvt_utf8_utf16andstd::wstring_convert) are deprecated and removed in C++20Our Solution:
We implemented manual UTF-8 ↔ UTF-16 conversion in pure C++20 with full Unicode support:
Technical highlights:
Memory Management
Why wrap these?
Why This Architecture Is Brilliant 🧠
1. Simple Code Generator
That's 12 lines. A traditional generator would be 200+ lines handling all the edge cases!
2. Correctness Guarantees
3. Performance
Example: Our
ForLooptemplate compiles to the same assembly as a raw C++ for-loop. Zero overhead!4. Maintainability
5. Extensibility
Want to add a new Delphi feature?
Traditional: Rewrite parts of the generator (risky!)
Our approach:
Example - adding
Inc(x):C++20 Features We're Using
int32_t,char16_t, etc.The Complete Architecture
Current RTL Status 📊
Implemented (as of today!):
Write,WriteLn,ReadLn(variadic templates)ForLoop,ForLoopDownto,WhileLoop,RepeatUntilDiv,Mod,Shl,Shr,InLength,Copy,Pos,IntToStr,StrToInt, etc.New,DisposeComing Soon:
TArray<T>)TList<T>,TDictionary<K,V>)try...except...finally)Real-World Example
This Delphi program:
Generates this C++:
Compiles cleanly. Runs perfectly. Native speed. ✨
Performance Notes
Q: Doesn't all this function calling add overhead?
A: No! Modern C++ compilers are amazing:
Proof: Compile with
-O3and look at the assembly. OurForLoopbecomes a raw loop with no function call overhead!Why This Matters
This architecture means we can:
The Philosophy
This is the NitroPascal way - elegant simplicity through careful architecture.
Technical Resources
Want to dive deeper? Check out:
Everything is on GitHub (coming soon!) and the design doc is meticulously documented.
Questions? Comments? Technical discussions welcome!
Have you worked with:
Let's discuss! Drop your thoughts below! 💬👇
TL;DR for the skimmers:
🚀 NitroPascal: Real Pascal. Real Performance. Real Simple (under the hood). 🚀
Beta Was this translation helpful? Give feedback.
All reactions