Advice on porting language to support Haiku

After joining the Haiku community 1.5 month ago, I started learning some more about C/ C++ fundamental, playing and contributing with few linux utility written in C++ to get myself more familiar with the C/C++ and unix system call.

I am currently researching and preparing for gsoc 2023 right now and want to do some research on this project idea - Porting the Go Programming Language to Haiku.
GSoC project ideas | Haiku Project (haiku-os.org)

Beside studying this course about language and compilers CS143: Compilers (stanford.edu) to understand how a programming language and compliers works. Is there any course or tutorial that I could look at to learn the fundamental related to this project.

Currently I have some idea on what a operating system is but I have not much idea on how a programming language and system and hardware interact with each other. I got some idea on how hardware work from a C perspective, e.g. how data stored in memory. But dont know "what is the interaction between C, software, OS, hardware behind a malloc " e.g. I have no idea what to do, how to do, to port a language to support Haiku

4 Likes

Hello!

I think there is much talk on porting Golang to Haiku, one thread I am most familiar with is Help with porting golang

There are some people who are working on porting it and has different progress status so check out that, I think Korli’s work is ahead so you can see if you could help with that.

From my experience exploring source code is a good idea to learn how things work, and see if you can make some simple contributions there.

That said even I am interested to learn the theoretical part of OS and how porting works so let’s see if some other developer could list out some resources.

3 Likes

Hello and welvome :slight_smile:

Just a word of warning, GSoC 2023 has not been announced yet, and we don’t know if Google will do it and if they will again accept Haiku as a mentoring organization. It is likely that will be the case, but not certain. That being said, it’s a good idea to start early so that you can make most of the coding period when it happens.

Maybe surprisingly for this project, you may not need to know that much about how a compiler works, besides the basic idea: it takes sourcecode, parses it, and convert it into executable code.

The executable code depends largely on the processor, and that does not change if you replace the operating system. So that part does not need a lot of changes.

What needs work is making sure the Go programs can call functions from the operating system. If I remember correctly from the previous porting attempts, the idea then was that Go for Linux would call directly into the kernel using “system calls”. However, in Haiku, the interface between applications and the kernel is not stable, and we do not recommend using it directly in this way. Instead we expect applicaitons (including those written in Go) will only call into the C standard library (functions you probably already know if you written a C program: open, read, write, malloc, etc).

So that’s where a part of the work is: investigating if that is possible for the Go compiler, how it works on other systems currently (do they still call the kernel directly on Linux? or did they change that? what about their support for other operating systems?)

Also you may have to learn about cross compilation and if it’s needed to setup Go for that. We already have a very old port of Go. At the time, the Go compiler itself was written in C. Now it’s written in Go. So it may be a problem to build it starting from “nothing”. You have basically two options:

  • Start from an older version or a kind of limited mini-compiler, and build newer/larger versions until you have the whole Go compiler able to build itself,
  • Start the work by making a cross-compiler, that runs on Linux but creates Haiku executables

Are these two solutions supported by the current Go compiler? Or only one of them? Which route did the existing porting efforts (in the thread linked by MashiJams) try to take, and how far they went?

3 Likes

@PulkoMandy Thank you for your reply, may I try to re-organise your reply to confirm did I understand it correctly?

A complier mission is to transfer source code to processor dependent executable code. which the original go complier will already handle processor dependent executable code for us already. The only problem and reason that need someone contribute to this project is,

  1. Go complier might use linux system call (uncertain), I need to look at source code on how go complier work on other system

  2. Cross compiler - CodeDocs , it state an example

For example, a compiler that runs on a Windows 7 but generates code that runs on Android smartphone is a cross compiler.

So if I Start the work by making a cross-compiler, that runs on Linux but creates Haiku executables , I am starting the project by developing and running a cross-complier on Linux and transfer golang source code to Haiku executable on Linux, then later on bring the complier on Haiku when it is fully functioning.

The reason that I need to develop and run on Linux is that because if it is using Linux system call, we want to compare which part of the code is working on Haiku vs Linux, therefore I need to start it on Linux instead on Haiku itself?

Yes.

Also, because the Go compiler itself is written in Go, you first need a way to compile the compiler. In compiler world, this is called “bootstrapping” (from the english expression “pulling yourself out of quicksand by pulling on your boot straps” if I’m not mistaken).

You have two options for this “bootstrapping”: use a cross-compiler to compile a native compiler, or start by a version of the Go compiler that is written in another language, already available on Haiku.

The second approach was used for the old Go port we already have. So one option is starting from that, and building newer and newer versions of Go until we get to the latest one. But it has been a few years and it may require fixing things again and again for each version of Go.

So, starting from a modern Go on Linux is maybe easier? Not having tried this myself, I’m not sure what the challenges would be in that case.

It may be a good idea to also contact maintainers of the Go compiler and ask them how they would do it. They probably know better what the problems could be in each approach or maybe they have other ways to offer that I didn’t think of.

2 Likes

Yes, that should be the way forward for the Go compiler port and cross-compiling it with Korli’s changes in Help with porting golang - #12 by korli

The idea is to be able to run the latest version (or a recent version) of the Go compiler on Haiku and then upstream the changes to join the other supported OSes that can use Go.

2 Likes
2 Likes

The Go developers already have a porting policy PortingPolicy · golang/go Wiki · GitHub for other operating systems to follow and asking them to dedicate their time to actually do all the porting work is unlikely going to happen.

At least for Golang (and unlike Google’s other projects), they accept patches for adding support for other OSes. In our case, it will most likely come from new or existing HaikuPorters or interested folks rather than them.

There are many good projects to post too!

https://discuss.haiku-os.org/t/open-source-nvidia-drivers-with-mit-license/

https://discuss.haiku-os.org/t/code-nvmm-to-dragonfly-its-more-os-independence/

2 Likes

Yes I think. For Component Pascal (Oberon dialect) I compiled binaries from Windows first (GitHub - X547/BlackBox-Haiku: BlackBox Component Builder port to Haiku operating system).

Distributing Go compiler and runtime in binary form will be probably required.

could you compile the latest version of Go again using cross-compilation in Linux or MacOS ?