~mpu/qbe

10 4

Various questions

Details
Message ID
<CAFvxmQbA1EyA7to-Ok4_GmGYm+9QfdSg71AaEc1xkLr1J=YUog@mail.gmail.com>
DKIM signature
pass
Download raw message
Hello,

I am trying to make a compiler from scratch of my own language for
learning purposes. I write it in C++ with as few dependencies as
possible, I don't rely on C/C++ runtime and standard libraries,...

For the moment I have a frontend that is able to generate an AST and a
backend able to generate an exe in PE format (Windows executables)
with a hardcoded hello world code. Now I struggle with the conversion
of my AST into machine code.
My concerns are about variables tracking, registers allocations,
tracking addresses in code,...

So I found QBE by doing some research and by its code size it seems
pretty interesting for me, but I still have a few questions before
investigating time on learning it.
Is there a C API instead of the IL? Because I make a compiler that
builds all sources in one step unlike C/C++ compilers with
intermediate obj files.
Is QBE structured in a way that ease the integration of additional
backends, because as far I understand QBE only targets linux which
have different executable formats and code organization (calling
conventions,...).


Best regards
Details
Message ID
<6ca0ee00-44f0-66e8-67a8-435aed54ad82@dreamquest.io>
In-Reply-To
<CAFvxmQbA1EyA7to-Ok4_GmGYm+9QfdSg71AaEc1xkLr1J=YUog@mail.gmail.com> (view parent)
DKIM signature
missing
Download raw message
Not an expert but maybe I can answer some questions...

 > Is there a C API instead of the IL?

No, to make it so that there is only one source of truth for input.  
 From what I've seen though, it shouldn't be too difficult to hack it up 
a little to read its IL from an in-memory buffer, if you really want.

 > Is QBE structured in a way that ease the integration of additional 
backends

Tentative yes?  There's an OSX target spec in amd64/targ.c , but I don't 
know how well it works.

Hope this helps,
Simon


On 1/19/23 15:40, Xavier Bigand wrote:
> Hello,
>
> I am trying to make a compiler from scratch of my own language for
> learning purposes. I write it in C++ with as few dependencies as
> possible, I don't rely on C/C++ runtime and standard libraries,...
>
> For the moment I have a frontend that is able to generate an AST and a
> backend able to generate an exe in PE format (Windows executables)
> with a hardcoded hello world code. Now I struggle with the conversion
> of my AST into machine code.
> My concerns are about variables tracking, registers allocations,
> tracking addresses in code,...
>
> So I found QBE by doing some research and by its code size it seems
> pretty interesting for me, but I still have a few questions before
> investigating time on learning it.
> Is there a C API instead of the IL? Because I make a compiler that
> builds all sources in one step unlike C/C++ compilers with
> intermediate obj files.
> Is QBE structured in a way that ease the integration of additional
> backends, because as far I understand QBE only targets linux which
> have different executable formats and code organization (calling
> conventions,...).
>
>
> Best regards
Details
Message ID
<CAFvxmQZMbYhhq748VQxZd6O2Z9XULbcZ1k55MRzGq29rUiAbWQ@mail.gmail.com>
In-Reply-To
<CAFvxmQbA1EyA7to-Ok4_GmGYm+9QfdSg71AaEc1xkLr1J=YUog@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
Thx Simon for your answer,

I think that I'll have to dig a little in the QBE code to find a way
to avoid QBE to also do lexing, parsing,... Even if these steps can be
pretty fast it seems to me unnecessary when you do a compiler for
another language, and it adds
complexity. I can think of potential floating numbers issues due to
the divergence of rules between my language and QBE IL,...

Maybe the API I am looking for is the internal QBE AST or equivalent.


I will check the code and see how I can use it, maybe with my
constraints I'll be only able to use it as a learning and inspiration
resource.



PS: Sorry if this message doesn't appear correctly in the mailing
list. I have no mail client other than gmail.

Best regards
Details
Message ID
<ee4ff5e8-62a2-405d-b37b-5ad2bc5022bb@app.fastmail.com>
In-Reply-To
<6ca0ee00-44f0-66e8-67a8-435aed54ad82@dreamquest.io> (view parent)
DKIM signature
pass
Download raw message
On Sat, Jan 21, 2023, at 18:52, Simon Heath wrote:
>  > Is QBE structured in a way that ease the integration of additional 
> backends
>
> Tentative yes?  There's an OSX target spec in amd64/targ.c , but I don't 
> know how well it works.

OSX is fully supported, both on amd64 and arm64 (M1) hardware.
See the output of 'qbe -h' for the backends available.
Details
Message ID
<35ab7ce2-eb5a-43e0-ab01-094033b5d43f@app.fastmail.com>
In-Reply-To
<CAFvxmQZMbYhhq748VQxZd6O2Z9XULbcZ1k55MRzGq29rUiAbWQ@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
On Sat, Jan 21, 2023, at 21:14, Xavier Bigand wrote:
> Maybe the API I am looking for is the internal QBE AST or equivalent.

I do recommend generating text at first. It will make
debugging your frontend easier, and it will also make it
simple for you to report qbe bugs and ask for help on this
mailing list.

You could structure your code emission as in the hare compiler
(https://git.sr.ht/~sircmpwn/harec/tree/master/item/src/qbe.c)
and have two implementations of this module, one text, and one
where you hook up directly in qbe's C code.
Details
Message ID
<CAFvxmQZZu4ERew8_YPJ8bq+5XdCE6pczHCtnG9Q6BaPH84ZDtg@mail.gmail.com>
In-Reply-To
<CAFvxmQZMbYhhq748VQxZd6O2Z9XULbcZ1k55MRzGq29rUiAbWQ@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
Thx Quentin for tips,

I think you're right, I'll certainly generate text at first and then
take a look at hare to jump the parsing of IL.

But first of all I need to be able to build qbe under Windows. And If
I am correct, QBE generates textual ASM, so I'll also have to change
the ASM output format and code generation (calling convention,...).

My final goal is to have a self contained compiler, without link
step,... I may also want to try to be able to JIT some part of my
language. I already took a look at how to translate ASM instructions
to machine code.

I also have some concerns on how QBE allows tracking of debug symbols,
in long term I may want to try to do my own debugging file format and
debugger.

I'll keep you updated about my progress even if it will take some time
as I don't have much free time.

Le sam. 21 janv. 2023 à 21:14, Xavier Bigand
<flamaros.xavier@gmail.com> a écrit :
>
> Thx Simon for your answer,
>
> I think that I'll have to dig a little in the QBE code to find a way
> to avoid QBE to also do lexing, parsing,... Even if these steps can be
> pretty fast it seems to me unnecessary when you do a compiler for
> another language, and it adds
> complexity. I can think of potential floating numbers issues due to
> the divergence of rules between my language and QBE IL,...
>
> Maybe the API I am looking for is the internal QBE AST or equivalent.
>
>
> I will check the code and see how I can use it, maybe with my
> constraints I'll be only able to use it as a learning and inspiration
> resource.
>
>
>
> PS: Sorry if this message doesn't appear correctly in the mailing
> list. I have no mail client other than gmail.
>
> Best regards



-- 
Xavier
Details
Message ID
<CQ17APAQXG42.2IKI6ZVC1Z7KB@taiga>
In-Reply-To
<CAFvxmQZZu4ERew8_YPJ8bq+5XdCE6pczHCtnG9Q6BaPH84ZDtg@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
For my part I'm not particularly interested in seeing qbe grow Windows
support upstream. You should be using better operating systems.
Details
Message ID
<CAFvxmQZDSr6ih954Ht_yH1uyW47w4JqhvRkwmjZgk--aN-Cd3A@mail.gmail.com>
In-Reply-To
<CQ17APAQXG42.2IKI6ZVC1Z7KB@taiga> (view parent)
DKIM signature
pass
Download raw message
Hello Drew,

I don't plan to do merge requests, as I don't target to do something
of a production quality and only want to do a compiler for just myself
as a learning experience.

I understand the QBE philosophy and it is why I choose to try to use it.

Le mer. 25 janv. 2023 à 11:31, Drew DeVault <sir@cmpwn.com> a écrit :
>
> For my part I'm not particularly interested in seeing qbe grow Windows
> support upstream. You should be using better operating systems.



-- 
Xavier
Details
Message ID
<CAFvxmQYR7YAuj3HasVoq6r+nSCEg2RNmKeZPH5rqNTuN76xBRw@mail.gmail.com>
In-Reply-To
<CAFvxmQZDSr6ih954Ht_yH1uyW47w4JqhvRkwmjZgk--aN-Cd3A@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
Hi,

I had time to make QBE build under Windows with Visual Studio, it was
easy to fix the code, major issues were the goto labels that have the
same names of variables and dependency on getopt.

Now I am trying to figure out how I can add a new amd64 format. There
are two things that I have to consider, the first one is the ABI which
is Microsoft x64 calling convention instead of System V AMD64; and the
second thing is the executable format which is PE (Portable
Executable) format instead of ELF.

I saw that the Asm enum for the executable format and the Target
struct to be able to configure both things. I think that handling the
Microsoft x64 ABI should be ok by adding specifics Target functions,
but I have more doubts for the generation of the ASM file at the
correct format.

For example, in NASM I have to specify externals functions I want to
use in the code (see a sample code at the end) and I found nothing the
IL that allow to add external symbols so if I am correct I will
certainly have to extend the IL or hack the code to add a fixed list
of symbols I have to specify as extern.

Also I searching to what gas (of gas.c) stand for?


; nasm -fwin32 hello_world.nasm
;
; From a native Visual Studio Tools Command prompt
; link /subsystem:console /nodefaultlib /entry:main hello_world.obj kernel32.lib


    global main             ; make main label available for linker
    extern  GetStdHandle
    extern  WriteFile
    extern  ExitProcess

    SECTION .data

message:
    db      'Hello World', 0    ; I put an addtionnal 0 but it can be omitted

    SECTION .text

main:
    sub     rsp, 0x20 ; reserve 32 bytes (0x20) of shadow space

    ; hStdOut = GetStdHandle(STD_OUTPUT_HANDLE)
    mov     rcx, -11 ; STD_OUTPUT_HANDLE((DWORD)-11)
    call    GetStdHandle

    ; WriteFile( hstdOut, message, length(message), &bytes, 0);
    add     rsp, 0x20
    mov     rcx, rax                                ; move previous
result (rax: hStdOut) to first parameter register (rcx)
    mov     rdx, message
    mov     r8, 11
    mov     r9, 0                                   ; [out, optional]
lpNumberOfBytesWritten
    push    0                                       ; [in, out,
optional] lpOverlapped
    call    WriteFile

    ; ExitProcess(0)
    add     rsp, 0x20
    mov     rcx, 0
    call    ExitProcess

    ; never here
    hlt
Details
Message ID
<df4447f7-df2e-446c-abba-aa6a4b1e9bdc@app.fastmail.com>
In-Reply-To
<CAFvxmQYR7YAuj3HasVoq6r+nSCEg2RNmKeZPH5rqNTuN76xBRw@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
On Thu, Feb 2, 2023, at 22:47, Xavier Bigand wrote:
> Now I am trying to figure out how I can add a new amd64 format. There
> are two things that I have to consider, the first one is the ABI which
> is Microsoft x64 calling convention instead of System V AMD64; and the
> second thing is the executable format which is PE (Portable
> Executable) format instead of ELF.

To start easy I would implement the new ABI and use mingw to
assemble qbe's output in gnu as syntax.
Details
Message ID
<CAFvxmQbuwL0MVSnQGhfcBrHvfJuOptjw=LxT+OBkwKfwoR6yxQ@mail.gmail.com>
In-Reply-To
<df4447f7-df2e-446c-abba-aa6a4b1e9bdc@app.fastmail.com> (view parent)
DKIM signature
pass
Download raw message
Le ven. 3 févr. 2023 à 09:14, Quentin Carbonneaux <quentin@c9x.me> a écrit :
> To start easy I would implement the new ABI and use mingw to
> assemble qbe's output in gnu as syntax.

Using mingw isn't really a thing I want to do, because my goal is to
get out all that complexity. I will not add it to myself even if it is
temporary.

I already started to modify the QBE code to change the ASM output
format, I use the same principle for architecture targets, so I don't
break the actual code. And for potential issues like "extern" I found
that nasm supports file inclusion as C does, so I will be able to
inject things that aren't supported by QBE IL.

It is pleasant to do C again, and for the moment QBE seems easy enough
to understand for my purpose, so thank you to contributors.
Reply to thread Export thread (mbox)