How to implement new Haxe target?

Hi all,

Are there any resources that could help me make a start with implementing a new Haxe target?

I don’t know OCaml (yet), and I understand I should probably learn it first before anything else, but once I will have a basic understanding of OCaml, where do I go from there?

Is there any documentation on writing Haxe compiler targets?

Regards,

Me

3 Likes

Usually it’s just a file called gensomething.ml that receives all the types in the compilation and traverses their structure to write the target language in a file(s). Some targets also need extra “filters” run before the generation to modify the typed tree so it’s more conforming to the target language semantics.

After getting a hang of OCaml, I would just find the place in main.ml where -js argument is handled and follow the process from there. It’ll lead you to all the places you need to modify to create a new target. (basically, you need a new platform variant for your target and then OCaml will point at the all places where it’s handled, one of which will be the GenYourTarget.generate call).

5 Likes

Also, depending on the nature of the target in question you’ll have to develop run-time and standard library stuff, like GC, closures, anonymous structures, reflection, etc.

3 Likes

In his 2016 WWX talk about the Lua target, @jdonaldson mentioned how he somewhat extensively documented the process of implementing a new target via his commit descriptions. Timestamped link.

5 Likes

Some other targets, like gencpp (and I think genhl) actually build their own intermediate AST from haxe’s one, because it’s easier to express and work with target specifics that way (e.g. pointers/references in C++). They then write the output from that intermediate structures rather than from Haxe’s typed tree.

3 Likes

There’s also some (most probably outdated, but still helpful) info on the old haxe website: Development of a compilation target for Haxe - Haxe

2 Likes

Hi @nadako & @Gama11,

Many thanks for the info, very useful :grinning: :sunglasses: :smiley:

Out of pure curiosity, what are your secret plans?

1 Like

Try looking at haxe/genjson.ml at development · HaxeFoundation/haxe · GitHub

There are only two things you need to know in order to understand OCaml:

  1. In general, you can only access stuff that’s above you. Thus, always start reading OCaml files from the bottom.
  2. f a b is basically a function call f(a, b).

Start in the generate function, see how it runs generate_module_type for each known type and then see how that branches off into generate_class, generate_enum, generate_typedef and generate_abstract. All you have to do is replicate that in your generator and make it output some (byte)code instead of JSON. Easy!

2 Likes

(hush, don’t tell anybody, but) https://freepascal.org, a Sys target that can be used on a multitude of processors and operating systems.
As an added bonus, there’s FreePascal’s sister project http://www.lazarus-ide.org, which makes creating GUI’ed applications ridiculously easy.
Yes, yes, I know, Pascal is ancient, but the FreePascal and Lazarus projects are very actively maintained, new features are added on a regular basis, and there’s a tonne of 3rd party libraries available to achieve a gazillion things.
I’ve been a Delphi and Pascal programmer since I was tiny (i.e. decades ago), so I’ve got the “Deep knowledge of the target platform” covered (as mentioned on http://old.haxe.org/doc/advanced/code_generators/needed).
Now all I need to do is get:

  • Deeper knowledge of Haxe language (of course!)
  • Learn Ocaml
  • Understand Haxe compiler sources

:open_mouth::sweat::astonished::grimacing:

3 Likes

Pascal was also the first language I programmed in (after trying C and failing at the time), I must have been 12-13 years old and I used Delphi 4 at the time.

So many fond memories and nostalgia. I had so much fun coming up with form apps and CLI apps, and trying DelphiX (DirectX headers). It’s nice how it also brings back other memories at the time, specially of DOS and Win95/98 games I used to play.

I don’t know, must be the psychology behind nostalgia, but I always look back at those old times as “simpler” and “warmer”, even though we all know computing improved a lot since then, including software engineering/programming tools, although Delphi was a revolution at the time with its RAD (Rapid App Dev) philosophy.

I love remembering those while drinking a cup of coffee :slight_smile:

It’d be awesome to have a FreePascal target!

2 Likes

1 Like

Heh, Pascal was also my first language. I used for quite a few years (about 5). The last year, I did Borland Delphi. But then Microsoft grabbed me with Visual Basic. That was kinda’ meh, but then I went into Visual C++ and C# and never looked back. It’s all curly braces for me now :smiley:

I think it’s AWESOME that you’re considering adding such a venerable target! It’s one of the biggest languages out there. Good luck!

(If my long terms plan work out, I’m going to look into adding some machine learning-friendly targets and platforms for use with Haxe. OpenCL anyone? Not sure if Haxe would be of help to that space yet :smiley: )

2 Likes
begin
  if braces[btCurly].kill(klNow) then begin
    writeln('Bwahahaha!')
  end
end;

Venerable indeed :blush::hugs::slightly_smiling_face:

Oh, that would be awesome!
Ever since I came across Haxe (about 10 years ago), I’ve had the wish for it to become more mainstream, more well-known, and used for things other than mainly games.
Haxe can do ANYTHING.
It’s a perfectly viable tool to create business apps, libraries of any kind, command line tools, heck, targeting e.g. C++ one could even create operating systems and device drivers!

Haxe is unique, @ncannasse is a genius with a super-human output, and everyone contributing is a hero :slightly_smiling_face::slightly_smiling_face::slightly_smiling_face:

Opening another parenthesis here: A Ruby target would also be awesome!

The Ruby eco-system is vast and high-quality (overall) - gems solving pretty much every problem you can think of - it’d be great to tap into it from Haxe.

EDIT: As a matter of fact, there is already an unofficial Ruby target, but it’s incomplete and based off a very old Haxe version, check here: support for ruby target · Issue #2394 · HaxeFoundation/haxe · GitHub. It was a quite venerable effort from Paul Fitz, but unfortunately it didn’t go forward.

2 Likes

Unfortunately already running into various issues…
The instructions (see below) seem to be… out of date, maybe?
It doesn’t help that I’m not actually sure what I’m doing.
Anyway, I’m tired, I’m going to try again tomorrow.

Trying on Windows 10, by the way.

awesome! another think hl to c can done same target hl to pascal

Hi Matt,

an object pascal target would be awesome. From time to time I also played with the idea to start one. If you want we can join forces. Like you I am a long time pascal programmer (started with UCSD pascal on Apple II). Currently I use Haxe a lot inside my Delphi programms via neko, but targeting pascal directly would open great possibilities.

cheers Adrian.

1 Like

What issues did you encounter?
If you have problems with mingw/ocaml you can try GitHub - HaxeFoundation/ocamhaxe: OCaml distribution for Haxe compilation.

1 Like