Compile for foreign systems.

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Compile for foreign systems.

Post by netmaestro »

I will definitely be avoiding bloat. This code compiled with GCC produces an 8k exe; PureBasic's equivalent, just the window and button, comes in at 18k:

Code: Select all

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <commctrl.h>

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
INITCOMMONCONTROLSEX icc;

char szClassName[ ] = "Windows App";

int WINAPI WinMain ( HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    InitCommonControlsEx(&icc);
    HWND button;
    HWND hwnd;
    MSG messages;
    WNDCLASSEX mywinclass;
    INT font;

    int windowx = GetSystemMetrics(SM_CXSCREEN)/2 - (640/2);
    int windowy = GetSystemMetrics(SM_CYSCREEN)/2 - (480/2);

    mywinclass.hInstance     = hThisInstance;
    mywinclass.lpszClassName = szClassName;
    mywinclass.lpfnWndProc   = WindowProcedure;
    mywinclass.style         = CS_DBLCLKS;
    mywinclass.cbSize        = sizeof (WNDCLASSEX);

    mywinclass.hIcon        = LoadIcon (NULL, IDI_APPLICATION);
    mywinclass.hIconSm      = LoadIcon (NULL, IDI_APPLICATION);
    mywinclass.hCursor      = LoadCursor (NULL, IDC_ARROW);
    mywinclass.lpszMenuName = NULL;
    mywinclass.cbClsExtra   = 0;
    mywinclass.cbWndExtra   = 0;
    mywinclass.hbrBackground = (HBRUSH) COLOR_WINDOW;

    if (!RegisterClassEx (&mywinclass))
        return 0;

    hwnd = CreateWindowEx (
           0,
           szClassName,
           "Windows App",
           WS_OVERLAPPEDWINDOW,
           windowx,
           windowy,
           640,
           480,
           HWND_DESKTOP,
           NULL,
           hThisInstance,
           NULL
           );

   button = CreateWindowEx (
           0,
           "BUTTON",
           "I can be pushed around",
           WS_CHILD | WS_VISIBLE,
           20,
           20,
           200,
           20,
           hwnd,
           NULL,
           hThisInstance,
           NULL
           );

    font = (INT)GetStockObject(DEFAULT_GUI_FONT);
    SendMessage(button,WM_SETFONT, font, 0);

    ShowWindow (hwnd, nCmdShow);

    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }

    return messages.wParam;
}


LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_DESTROY:
            PostQuitMessage (0);
            break;
        default:
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
BERESHEIT
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Compile for foreign systems.

Post by fsw »

netmaestro wrote:I'm currently working on a compiler of my own, its output is ANSI C code which gets passed to GCC and theoretically it should be able to target Windows and Linux. But to get where I want to go with it will take years at the rate I'm going. I think the idea is sound enough though.
This is really funny... and I tell you why.

In this thread are (at least) 3 posters that work on compilers - just for fun.

Keep on coding :mrgreen:
Last edited by fsw on Sat Jul 30, 2011 3:17 pm, edited 1 time in total.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Compile for foreign systems.

Post by Tenaja »

I've seen some gcc output, and it can be rather sloppy. GCC is put together to be a do-all compiler, so it does not put out very efficient code. Well, at least not the target I was looking at. The target was a 32-bit mcu that contained operations for handling 64-bit vars, but gcc output code as if it could only handle 32 bits variables. It was pretty disappointing.
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Compile for foreign systems.

Post by fsw »

Tenaja wrote:I've seen some gcc output, and it can be rather sloppy. GCC is put together to be a do-all compiler, so it does not put out very efficient code. Well, at least not the target I was looking at. The target was a 32-bit mcu that contained operations for handling 64-bit vars, but gcc output code as if it could only handle 32 bits variables. It was pretty disappointing.
Did you look at TCC :?:
It has i32/x64/arm capabilities, don't know if it even optimizes though...
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Compile for foreign systems.

Post by fsw »

Suppose this thread will pretty soon be moved to offtopic :mrgreen:
User avatar
idle
Always Here
Always Here
Posts: 5839
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Compile for foreign systems.

Post by idle »

fsw wrote:
Tenaja wrote:I've seen some gcc output, and it can be rather sloppy. GCC is put together to be a do-all compiler, so it does not put out very efficient code. Well, at least not the target I was looking at. The target was a 32-bit mcu that contained operations for handling 64-bit vars, but gcc output code as if it could only handle 32 bits variables. It was pretty disappointing.
Did you look at TCC :?:
It has i32/x64/arm capabilities, don't know if it even optimizes though...
thats what llvm is for!
You just spit out butt ugly SSA asm, it optimizes it and you use the assembler and linker of your choice!
Windows 11, Manjaro, Raspberry Pi OS
Image
Post Reply