New compiler

Started by Patrice Terrier, November 25, 2026, 09:49:36 AM

Previous topic - Next topic

Patrice Terrier

Hello Paul

José sent me a message about your new compiler. I was able to compile existing FB code with tiko, and it all went fine!

The only thing I noticed is the size of the resulting binary. Can we use any linker optimizations?

For comparison, my SHCplayer:
- tiko: 52,736 bytes
- fbc64 + ld (-nodeflibs, --gc-sections, -s): 29,184 bytes
- C version (VS2022): 24,576 bytes

Thanks for your work!
Patrice

Paul Squires

Thanks Patrice!

It's a lot of fun working with José and building a language with everything that we've always wanted.

I have made changes to the way the binaries are created by allowing COMDAT sections to be eliminated if not referenced. There is also a change to the runtime libraries to eliminate linked C runtime functions that are never referenced.

I have both of these changes currently working but I need to run them through hundreds of automated tests on Windows, Linux and MacOS, so I think the change won't arrive until the next version release.

One thing that I decided not to build in by default (I left it as a switch) is folding identical functions (-Wl /OPT:ICF).  It would shrink binaries further, but two such procedures would then share an address, so procedure pointers to them would compare equal. That's a little dangerous and better left to the programmer to decide whether to link with it or not. On large programs maybe it would be worth it, but if we're trying to squeeze out every last byte from small programs then maybe we'll save like 512 bytes (The exe changes in 512-byte steps, the file alignment, so a program saves either 0 or 512 bytes depending on whether it crosses a boundary).

The results are promising.

Sizes on windows-x86_64:

program              before    after
hello                26,112    15,872
MessageBox GUI       16,384    6,656
small WndProc GUI    25,600    14,848 (fbc64: 27,648)
Paul Squires
PlanetSquires Software

Patrice Terrier

#2
Paul,

I have done a lot of byte hunting on my own libraries, and I was able to reduce their size by almost 50% while adding features. For example, WinLIFT64.dll is now 226 KB, with complete WV2B (WebView2) encapsulation and full grid support.

Sticking to pure procedural code and plain structures, rather than classes, was the key, at least for me.

AI in good hands can help produce better code. But the artist is not the paintbrush, it is the hand holding it.

José Roca

> AI in good hands can help produce better code. But the artist is not the paintbrush, it is the hand holding it.

Yes, you need to have a plan, not just ask to the AI: I need a new compiler for tomorrow.

But just look at the enormous amount of documentation. Writing all that without any help would have taken a whole year.

Paul Squires

Quote from: José Roca on November 25, 2026, 12:45:42 PMBut just look at the enormous amount of documentation. Writing all that without any help would have taken a whole year.
Lol, the documentation (which I've always been a chronic procrastinator with) would have taken me three lifetimes to produce.
Paul Squires
PlanetSquires Software

Paul Squires

The following is about as far as tiko will go with exe size optimization (at least based on today's codebase).

Previously, the -Os switch did not reduce exe size because the tiko generated .ll intermediate files did not have the flag on it. It is now implemented along with special versions of the runtime library that were also generated with the size optimization flag. When you use the -Os flag, your program will get size reduction along with automatically linking the special size optimized runtime library. That optimized runtime is NOT the default because for runtime heavy work it will perform slightly slower.
program original   fbc64 now -O2 now -Os
hello    26,112     —    15,360 (−41%) 14,336 (−45%)
MessageBox GUI 16,384     —    6,144 (−63%) 5,632 (−66%)
WndProc GUI 25,600     27,648 13,824 (−46%) 11,776 (−54%)
Paul Squires
PlanetSquires Software

Patrice Terrier

Paul,

With VS, I always favor size over speed (/O1, /Os), because with modern processors I never see any real speed difference. Smaller code also fits better in the CPU cache, which often makes up for the optimizations I give up.

It would be nice if tiko offered a similar size-first option, something like -Os/-Oz for clang plus /OPT:REF and /OPT:ICF for lld-link.

Patrice Terrier

#7
Paul

Here is small piece of C code, that compiles to 5632 bytes

//+--------------------------------------------------------------------------+
//|                                                                          |
//|                                 MINI                                     |
//|                                                                          |
//|               Example to create very small binary window                 |
//|                    using only the core SDK flat API                      |
//|             and the Tiny C Runtime library from Matt Pietrek.            |
//|                                                                          |
//+--------------------------------------------------------------------------+
//|                                                                          |
//|                         Author Patrice TERRIER                           |
//|                            www.objreader.com                             |
//|                           objreader@gmail.com                            |
//|                                                                          |
//+--------------------------------------------------------------------------+
//|                  Project started on : 01-04-2001 (MM-DD-YYYY)            |
//|                        Last revised : 07-04-2024 (MM-DD-YYYY)            |
//+--------------------------------------------------------------------------+

#include <windows.h>

constexpr auto CLIENT_WIDTH   = 640;
constexpr auto CLIENT_HEIGHT  = 480;

struct PROP {
    HWND        hMain;
    long        minTrackSizeW;
    long        minTrackSizeH;
};

static PROP gP;

static long rWidth(IN RECT &r) {
    return r.right - r.left;
}

static long rHeight(IN RECT &r) {
    return r.bottom - r.top;
}

static LRESULT CALLBACK WndProc(IN HWND hWnd, IN UINT uMsg, IN WPARAM wParam, IN LPARAM lParam) {
    LRESULT nRet = -1;
    MINMAXINFO* pMM;
    switch (uMsg) {
    case WM_CREATE:
        break;

    case WM_GETMINMAXINFO:
        pMM = (MINMAXINFO*)lParam;
        pMM->ptMinTrackSize.x = gP.minTrackSizeW;
        pMM->ptMinTrackSize.y = gP.minTrackSizeH;
        break;

    case WM_SIZE:
        InvalidateRect(hWnd, NULL, TRUE);
        nRet = 0;
        break;

    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hDC = BeginPaint(hWnd, &ps);
            // Paint the window content here
            EndPaint(hWnd, &ps);
            nRet = 0;
        }
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        nRet = 0;
        break;
    }

    if (nRet == -1) nRet = DefWindowProc(hWnd, uMsg, wParam, lParam);
    return nRet;
}

int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) {
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    int nRet = 0;
    WCHAR szClass[] = L"MINI";

    // Detect process without using a Mutex
    HWND hFound = FindWindow(szClass, NULL);
    if (hFound) {
        if (IsIconic(hFound)) { ShowWindow(hFound, SW_RESTORE); }
        SetForegroundWindow(hFound);
        return nRet;
    }

    WNDCLASSEX wcx = { 0 };
    wcx.cbSize = sizeof(wcx);
    long IsInitialized = GetClassInfoEx(hInstance, szClass, &wcx);
    if (!IsInitialized) {
        wcx.style         = CS_HREDRAW | CS_VREDRAW;
        wcx.lpfnWndProc   = WndProc;
        wcx.cbClsExtra    = 0;
        wcx.cbWndExtra    = 0;
        wcx.hInstance     = hInstance;
        wcx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wcx.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wcx.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
        wcx.lpszMenuName  = 0;
        wcx.lpszClassName = szClass;
        wcx.hIconSm       = wcx.hIcon;
        if (RegisterClassEx(&wcx)) { IsInitialized = -1; }
    }

    if (IsInitialized) {
        DWORD dwExStyle = 0;
        DWORD dwStyle = WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
        RECT lpr = { 0 }; SetRect(&lpr, 0, 0, CLIENT_WIDTH, CLIENT_HEIGHT);
        AdjustWindowRectEx(&lpr, dwStyle, FALSE, dwExStyle);
        gP.minTrackSizeW = rWidth(lpr);
        gP.minTrackSizeH = rHeight(lpr);
        long x = max((GetSystemMetrics(SM_CXSCREEN) - gP.minTrackSizeW) / 2, 0);
        long y = max((GetSystemMetrics(SM_CYSCREEN) - gP.minTrackSizeH) / 2, 0);

        gP.hMain = CreateWindowEx(dwExStyle, szClass, szClass, dwStyle, x, y, gP.minTrackSizeW, gP.minTrackSizeH, 0, 0, hInstance, NULL);
        if (gP.hMain) {

            ShowWindow(gP.hMain, nCmdShow);
            SetForegroundWindow(gP.hMain); // Slightly Higher Priority

            MSG msg = { 0 };
            while (GetMessage(&msg, NULL, 0, 0)) {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
            nRet = (int) msg.wParam;
        }
        UnregisterClass(wcx.lpszClassName, hInstance);
    }
    return nRet;
}

the same asm code compile to 2560 bytes

Here is the FB version

'+--------------------------------------------------------------------------+
'|                                                                          |
'|                                 MINI                                     |
'|                                                                          |
'|               Example to create very small binary window                 |
'|                    using only the core SDK flat API                      |
'|                         (tiko / FB dialect port)                         |
'|                                                                          |
'+--------------------------------------------------------------------------+
'|                                                                          |
'|                         Author Patrice TERRIER                           |
'|                            www.objreader.com                             |
'|                           objreader@gmail.com                            |
'|                                                                          |
'+--------------------------------------------------------------------------+
'|                  Project started on : 01-04-2001 (MM-DD-YYYY)            |
'|                        Last revised : 11-25-2026 (MM-DD-YYYY)            |
'+--------------------------------------------------------------------------+

#define UNICODE
#lang "fb"

#include once "windows.bi"

const CLIENT_WIDTH  = 640
const CLIENT_HEIGHT = 480

type PROP
    hMain         as HWND
    minTrackSizeW as long
    minTrackSizeH as long
end type

dim shared gP as PROP

private function rWidth(byref r as RECT) as long
    return r.right - r.left
end function

private function rHeight(byref r as RECT) as long
    return r.bottom - r.top
end function

private function WndProc(byval hWnd as HWND, byval uMsg as UINT, byval wParam as WPARAM, byval lParam as LPARAM) as LRESULT
    dim nRet as LRESULT = -1
    dim pMM as MINMAXINFO ptr

    select case uMsg
    case WM_CREATE

    case WM_GETMINMAXINFO
        pMM = cast(MINMAXINFO ptr, lParam)
        pMM->ptMinTrackSize.x = gP.minTrackSizeW
        pMM->ptMinTrackSize.y = gP.minTrackSizeH

    case WM_SIZE
        InvalidateRect(hWnd, NULL, TRUE)
        nRet = 0

    case WM_PAINT
        dim ps as PAINTSTRUCT
        dim hPaintDC as HDC = BeginPaint(hWnd, @ps)
        ' Paint the window content here
        EndPaint(hWnd, @ps)
        nRet = 0

    case WM_DESTROY
        PostQuitMessage(0)
        nRet = 0
    end select

    if nRet = -1 then nRet = DefWindowProcW(hWnd, uMsg, wParam, lParam)
    return nRet
end function

private function AppMain(byval hInstance as HINSTANCE, byval nCmdShow as long) as long
    dim nRet as long = 0
    dim szClass as wstring * 8 = "MINI"
    dim pClass as LPCWSTR = cast(LPCWSTR, @szClass)

    ' Detect process without using a Mutex
    dim hFound as HWND = FindWindowW(pClass, NULL)
    if hFound then
        if IsIconic(hFound) then ShowWindow(hFound, SW_RESTORE)
        SetForegroundWindow(hFound)
        return nRet
    end if

    dim wcx as WNDCLASSEXW
    wcx.cbSize = sizeof(wcx)
    dim IsInitialized as long = GetClassInfoExW(hInstance, pClass, @wcx)
    if IsInitialized = 0 then
        wcx.style         = CS_HREDRAW or CS_VREDRAW
        wcx.lpfnWndProc   = @WndProc
        wcx.cbClsExtra    = 0
        wcx.cbWndExtra    = 0
        wcx.hInstance     = hInstance
        wcx.hIcon         = LoadIconW(NULL, IDI_APPLICATION)
        wcx.hCursor       = LoadCursorW(NULL, IDC_ARROW)
        wcx.hbrBackground = cast(HBRUSH, COLOR_BTNSHADOW)
        wcx.lpszMenuName  = 0
        wcx.lpszClassName = pClass
        wcx.hIconSm       = wcx.hIcon
        if RegisterClassExW(@wcx) then IsInitialized = -1
    end if

    if IsInitialized then
        dim dwExStyle as DWORD = 0
        dim dwStyle as DWORD = WS_OVERLAPPEDWINDOW or WS_CLIPSIBLINGS or WS_CLIPCHILDREN
        dim lpr as RECT : SetRect(@lpr, 0, 0, CLIENT_WIDTH, CLIENT_HEIGHT)
        AdjustWindowRectEx(@lpr, dwStyle, FALSE, dwExStyle)
        gP.minTrackSizeW = rWidth(lpr)
        gP.minTrackSizeH = rHeight(lpr)
        dim x as long = (GetSystemMetrics(SM_CXSCREEN) - gP.minTrackSizeW) \ 2 : if x < 0 then x = 0
        dim y as long = (GetSystemMetrics(SM_CYSCREEN) - gP.minTrackSizeH) \ 2 : if y < 0 then y = 0

        gP.hMain = CreateWindowExW(dwExStyle, pClass, pClass, dwStyle, x, y, gP.minTrackSizeW, gP.minTrackSizeH, 0, 0, hInstance, NULL)
        if gP.hMain then

            ShowWindow(gP.hMain, nCmdShow)
            SetForegroundWindow(gP.hMain) ' Slightly Higher Priority

            dim msg as MSG
            while GetMessageW(@msg, NULL, 0, 0)
                TranslateMessage(@msg)
                DispatchMessageW(@msg)
            wend
            nRet = clng(msg.wParam)
        end if
        UnregisterClassW(wcx.lpszClassName, hInstance)
    end if
    return nRet
end function

' tiko runs module-level code from its own startup (WinMainCRTStartup override is not called)
ExitProcess(AppMain(GetModuleHandleW(NULL), SW_SHOWDEFAULT))


Paul Squires

#8
Quote from: Patrice Terrier on November 25, 2026, 02:44:02 PMIt would be nice if tiko offered a similar size-first option, something like -Os/-Oz for clang plus /OPT:REF and /OPT:ICF for lld-link.
Hi Patrice,

You must have missed my earlier post about -Os being implemented today: https://www.planetsquires.com/protect/forum/index.php?msg=36913

/OPT:REF already in the build/link process.

I'm leaving /OPT:ICF switch to the user to add to the build via the tiko.toml build file for whatever build profile they wish. You would add it to the build's "options" key as "-Wl /OPT:ICF"

Paul Squires
PlanetSquires Software

Paul Squires

#9
Hi Patrice,

I ran your FB example with tiko's new size reductions implemented today. Here are the results:
Build    Bytes
-O2 -s gui                 8,192 bytes
-O2 -s gui -Wl /OPT:ICF    7,680 bytes
-Os -s gui                 7,168 bytes
-Os -s gui -Wl /OPT:ICF    7,168 bytes
ICF adds nothing at -Os: PE files grow in 512-byte steps, so whatever ICF folds at -Os isn't enough to save a step.

Other than being a cool exercise to see how small we can shrink exe size, and to take us back to our "smaller, faster" roots, it is not tiko's domain for that kind of functionality. I think those types of exercises are better left to C or asm.

I doubt that current FBC can compile that code smaller than 7,168 bytes. I tried a few gcc switches on the code using FBC and I could only get it to 23,552 bytes.
Paul Squires
PlanetSquires Software

Patrice Terrier

Paul,

7,168 bytes is already a very good result!

I was checking this statement on your front page:
"native binaries as fast and small as C"

That is why I made this test, with C as the challenger.
My VS C version of MINI is 5,632 bytes, so tiko is only three 512-byte steps behind (1.5 KB).
Close enough to C for me!

Thank you for implementing the size reductions so quickly!

BTW, my own DLLs (written in C: WinLIFT64, GDImage64, SHC) work like a charm with tiko. I only had to add an #inclib to SHC.bi, because my fbc batch passed SHC.lib on the ld command line.

Patrice

Patrice Terrier

Paul,

The new -Os works fine, I got exactly the same size than when calling directly GCC with it.

Paul Squires

Quote from: Patrice Terrier on November 27, 2026, 08:29:27 AMThe new -Os works fine, I got exactly the same size than when calling directly GCC with it.
Excellent! I am happy that it is working correctly.
Paul Squires
PlanetSquires Software

Patrice Terrier

BTW, I think -Os should be implicit (default value) for SDK coders  :)