Compiler in a Day Challenge

Started by Theo Gottwald, Today at 02:45:03 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Theo Gottwald

I did some tests today and to make the result in short, a compiler in a day is possible, (i made ~8 today in my tests - one is outside that folder and one you can download below).
The bad news is you can not use Powerbasic as Base-Language. While AI can do that it makes much more mistakes then in C.

2026-04-01 14_39_44-Skeleton und 5 weitere Registerkarten – Explorer.png

Therefore to get a "Compiler in one day" you need to switch over to C, i used the free MingW64 C (Download instructions below).

Especially KimiK2.5 is a Runner with C. Much faster then with Powerbasic.
When the Linker with Powerbasic needed hours, it can do it in C in much less time.
Key to fast success is here to know the components.

2026-04-01 14_34_00-Eigenschaften von backend, ....png

Last month i thought "I use not enough KI".
Thats solved now. Now this month i think "I do not yet use it properly".
More and faster is possible.

Yet, i can say that one of the Keys is using Multiple Named Desktops helps a lot.
The needed Tool is part of the Smart-Package-Robot Distribution.

Having too many Windows from multiple Project on the same desktop, has the consequence that I tell the wrong AI to do something that makes no sense in the context of that other project.

So having multiple Named Desktops helps to always know which AI is woking on which project.

2026-04-01 14_36_37-SPR-Desktops-Manager  (c) 2026 Theo Gottwald.png

Picture 1: For Fun Reasons i added (ATARI 400-)PILOT Language to the multi-Language Inputs.

2026-04-01 14_18_39-Administrator_  Kimi-Code CLI.png

Picture 2. Another Test-Project.

2026-04-01 14_24_41-PBXB64 – Datei-Explorer.png

2026-04-01 14_49_58-Smart Package Robot (SPR) - Downloads — Originalprofil — Mozilla Firefox.png



For my valued Forum Members
i give you the Source-Code of the compiler PBXA (because I dropped its development).
SO you can see yourself how far you can get in 1 day. Using AI Properly.

This is a NATIVE COMPILER - it does NOT Need a C- or other compiler to produce an executable.
It may already be able to produce a "Hello World" (it should, i did not test manually).
Just give it a try.

The AI says about it:
• PBXA64 is a minimal, end-to-end working C compiler that targets Windows x64 and emits native PE executables. It lives in PBXA64 inside the main project tree and is built with MinGW-W64 GCC (-std=c11 -Wall -Wextra -Werror -O2).

  What it compiles

  PBXA64 compiles a tiny custom language called MGC (not C or BASIC). The grammar is intentionally small so the entire pipeline fits in a few thousand lines of code:

  • Functions with typed parameters and return types
    fn add(a: i32, b: i32) -> i32 { ... }
  • Variable declarations with let
    let x: i64 = 42;
  • Expressions: integer literals, names, binary operators (+ - * / % == <, etc.), unary operators (- ! ~), calls, casts
    and assignments
  • Statements: blocks, return, expression statements
  • Modules as the top-level container

  No structs, no pointers, no preprocessor, no floating-point codegen, and no optimizer — just a straight path from sour
  ce → tokens → AST → IR → x64 machine code → COFF object → PE .exe.

  Architecture

  Stage                        Status      Key files
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  Lexer                        ✅ Working  src/frontend/lexer.c
  Parser                      ✅ Working  src/frontend/parser.c
  Semantic Analysis            ✅ Working  src/frontend/semantic.c, symbol.c
  IR Generation                ✅ Working  src/middle/ir_builder.c
  Code Generation (isel)      ✅ Working  src/backend/isel.c
  x64 Encoding                ✅ Working  src/backend/x64_encoding.c
  Assembler / Object builder  ✅ Working  src/assembler/assembler.c
  Linker / PE Writer          ✅ Working  src/linker/linker.c, pe_writer.c
  Runtime (CRT stub)          ✅ Working  src/runtime/crt.c

  Current state

  PBXA64 is the a runnable compiler right now. It successfully builds and executes programs such as:

  fn add(a: i32, b: i32) -> i32 {
      return a + b;
  }

  fn main() -> i32 {
      return add(10, 32);  // exits with 42
  }

  The executable (build/pbxa64.exe) is about 340 KB and links without external dependencies beyond the MinGW toolchain u
  sed to build it.

  Strengths

  • Actually emits working PE files. Every stage from lexer to linker is functional enough to produce a runnable Windows executable.
  • Tiny surface area. Because the language is minimal, you can modify the backend (e.g., add a new x64 instruction, change the calling convention, or experiment with register allocation) and see results in minutes.
  • Clean separation. The pipeline stages (driver/pipeline.c) are well separated, making it easy to isolate bugs.

  Limitations

  • Toy language. MGC is deliberately small; it will never compile real C or PowerBASIC without a massive frontend rewrite   
  • No optimizer. There is no SSA, no constant folding, no inlining, and no peephole pass.
  • Stubbed advanced types. Floats, strings, chars, and structs are parsed but lowered to safe stubs (usually 0) in the builder to avoid backend crashes.
  • No debug info. No PDB, no line tables, no unwind info.

  When to use PBXA64

  Use PBXA64 as a fast testbed for compiler backend experiments (instruction selection, encoding, linker relocations, PE layout) when you need a working end-to-end pipeline without the complexity of a 45 kLOC C/BASIC frontend. If your goal is a production C or BASIC compiler, PBXB64 or PBX_Hybrid are the better, but PBXA64 is small and can produce a working .exe now.




TO compile it YOU WILL NEED MinGW-C. Here is how you get it:

Here is a **concise, actionable guide** to get **MinGW-w64** on Windows 👇

---

## 📥 Recommended Method (Easiest): MSYS2

### 1. Download

Go to the official site:
👉 MSYS2
Download the installer (`msys2-x86_64.exe`)

---

### 2. Install

* Run the installer
* Install to default path: `C:\msys64`

---

### 3. Update system (IMPORTANT)

Open **MSYS2 terminal** and run:

```bash
pacman -Syu
```

Restart terminal, then run again:

```bash
pacman -Su
```

---

### 4. Install MinGW-w64 toolchain

For 64-bit compiler:

```bash
pacman -S mingw-w64-x86_64-gcc
```

---

### 5. Add to PATH

Add this to your Windows PATH:

```
C:\msys64\mingw64\bin
```

---

### 6. Verify

Open CMD or PowerShell:

```bash
gcc --version
```

---

## ⚙️ Alternative (Less Recommended): Standalone Build

Download from:
👉 MinGW-w64

But:

* manual setup
* inconsistent builds
* no package manager

👉 MSYS2 is significantly more robust for development.

---

## 🧠 Practical Note

For your compiler project:

* You **need MSYS2**, not plain MinGW
* It gives you:

  * `gcc`, `ld`, `as`
  * headers + CRT
  * linker + import libs


I do not take any copyright on it.
You can take it and make your own 64-bit compiler out of it if you have fun.
The core is complete, you just need to extend the compiler to understand whatever you want.
The architecture is very easy to understand (due to the fact that i did not touch it), its definitely university - like.
If you make a 3-year study in compiler-science, they will ask you to do something like this for your doctor.
So its complete - and it can be extended. TO BE ANYTHING you ever dreamed of.