# How to implement new Haxe target?

**URL:** https://community.haxe.org/t/how-to-implement-new-haxe-target/1599
**Category:** Haxe
**Created:** [March 13, 2019, 12:55pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599 "2019-03-13T12:55:57Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Matt](https://avatars.discourse-cdn.com/v2/letter/m/a6a055/32.png) [@Matt](https://community.haxe.org/u/Matt)
#### Post date: [March 13, 2019, 12:55pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/1 "2019-03-13T12:55:57Z")

</div>

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

---

<div class="post-metadata">

### Author: ![nadako](https://community.haxe.org/user_avatar/community.haxe.org/nadako/32/5_2.png) [@nadako](https://community.haxe.org/u/nadako)
#### Post date: [March 13, 2019, 1:04pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/2 "2019-03-13T13:04:30Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![nadako](https://community.haxe.org/user_avatar/community.haxe.org/nadako/32/5_2.png) [@nadako](https://community.haxe.org/u/nadako)
#### Post date: [March 13, 2019, 1:06pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/3 "2019-03-13T13:06:51Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Gama11](https://community.haxe.org/user_avatar/community.haxe.org/gama11/32/7_2.png) [@Gama11](https://community.haxe.org/u/Gama11)
#### Post date: [March 13, 2019, 1:07pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/4 "2019-03-13T13:07:32Z")

</div>

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](https://youtu.be/cGf_-VK0KcM?t=1045).

 ![](https://community.haxe.org/uploads/default/original/1X/a6c2324777846ea1571da267fdef27e15d02b2d4.jpeg)

---

<div class="post-metadata">

### Author: ![nadako](https://community.haxe.org/user_avatar/community.haxe.org/nadako/32/5_2.png) [@nadako](https://community.haxe.org/u/nadako)
#### Post date: [March 13, 2019, 1:10pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/5 "2019-03-13T13:10:55Z")

</div>

> [@nadako](#):
>
> 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.

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.

---

<div class="post-metadata">

### Author: ![nadako](https://community.haxe.org/user_avatar/community.haxe.org/nadako/32/5_2.png) [@nadako](https://community.haxe.org/u/nadako)
#### Post date: [March 13, 2019, 1:13pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/6 "2019-03-13T13:13:50Z")

</div>

There’s also some (most probably outdated, but still helpful) info on the **old** haxe website: [Development of a compilation target for Haxe - Haxe](http://old.haxe.org/doc/advanced/code_generators)

---

<div class="post-metadata">

### Author: ![Matt](https://avatars.discourse-cdn.com/v2/letter/m/a6a055/32.png) [@Matt](https://community.haxe.org/u/Matt)
#### Post date: [March 13, 2019, 1:17pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/7 "2019-03-13T13:17:31Z")

</div>

Hi @nadako & @Gama11,

Many thanks for the info, very useful 😀 🕶 😃

---

<div class="post-metadata">

### Author: ![mark.knol](https://community.haxe.org/user_avatar/community.haxe.org/mark.knol/32/50_2.png) [@mark.knol](https://community.haxe.org/u/mark.knol)
#### Post date: [March 13, 2019, 1:18pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/8 "2019-03-13T13:18:27Z")

</div>

Out of pure curiosity, what are your secret plans?

---

<div class="post-metadata">

### Author: ![Simn](https://community.haxe.org/user_avatar/community.haxe.org/simn/32/388_2.png) [@Simn](https://community.haxe.org/u/Simn)
#### Post date: [March 13, 2019, 3:49pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/9 "2019-03-13T15:49:19Z")

</div>

Try looking at [haxe/genjson.ml at development · HaxeFoundation/haxe · GitHub](https://github.com/HaxeFoundation/haxe/blob/development/src/core/json/genjson.ml)

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!

---

<div class="post-metadata">

### Author: ![Matt](https://avatars.discourse-cdn.com/v2/letter/m/a6a055/32.png) [@Matt](https://community.haxe.org/u/Matt)
#### Post date: [March 13, 2019, 4:56pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/10 "2019-03-13T16:56:56Z")

</div>

> [@mark.knol](#):
>
> Out of pure curiosity, what are your secret plans?

(hush, don’t tell anybody, but) [https://freepascal.org](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](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](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

…

😮😓😲😬

---

<div class="post-metadata">

### Author: ![fullofcaffeine](https://community.haxe.org/user_avatar/community.haxe.org/fullofcaffeine/32/28_2.png) [@fullofcaffeine](https://community.haxe.org/u/fullofcaffeine)
#### Post date: [March 13, 2019, 6:24pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/11 "2019-03-13T18:24:00Z")

</div>

> [@Matt](#):
>
> I’ve been a Delphi and Pascal programmer since I was tiny (i.e. decades ago),

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 🙂

It’d be awesome to have a FreePascal target!

---

<div class="post-metadata">

### Author: ![Matt](https://avatars.discourse-cdn.com/v2/letter/m/a6a055/32.png) [@Matt](https://community.haxe.org/u/Matt)
#### Post date: [March 13, 2019, 7:35pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/12 "2019-03-13T19:35:49Z")

</div>

![Capture](https://community.haxe.org/uploads/default/original/1X/6767c474fc14795a96070d14055aa28fc65d4dd5.png)

---

<div class="post-metadata">

### Author: ![Kyliathy](https://community.haxe.org/user_avatar/community.haxe.org/kyliathy/32/452_2.png) [@Kyliathy](https://community.haxe.org/u/Kyliathy)
#### Post date: [March 13, 2019, 8:14pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/13 "2019-03-13T20:14:31Z")

</div>

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 😃

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 😃 )

---

<div class="post-metadata">

### Author: ![Matt](https://avatars.discourse-cdn.com/v2/letter/m/a6a055/32.png) [@Matt](https://community.haxe.org/u/Matt)
#### Post date: [March 13, 2019, 8:33pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/14 "2019-03-13T20:33:17Z")

</div>

> [@Kyliathy](#):
>
> It’s all curly braces for me now 😃

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

```

> [@Kyliathy](#):
>
> 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!

Venerable indeed 😊🤗🙂

> [@Kyliathy](#):
>
> (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 😃 )

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 🙂🙂🙂

---

<div class="post-metadata">

### Author: ![fullofcaffeine](https://community.haxe.org/user_avatar/community.haxe.org/fullofcaffeine/32/28_2.png) [@fullofcaffeine](https://community.haxe.org/u/fullofcaffeine)
#### Post date: [March 13, 2019, 8:34pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/15 "2019-03-13T20:34:34Z")

</div>

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](https://github.com/HaxeFoundation/haxe/issues/2394). It was a quite venerable effort from Paul Fitz, but unfortunately it didn’t go forward.

---

<div class="post-metadata">

### Author: ![Matt](https://avatars.discourse-cdn.com/v2/letter/m/a6a055/32.png) [@Matt](https://community.haxe.org/u/Matt)
#### Post date: [March 13, 2019, 10:51pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/16 "2019-03-13T22:51:13Z")

</div>

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.

> <https://github.com/HaxeFoundation/haxe/blob/development/extra/BUILDING.md>

---

<div class="post-metadata">

### Author: ![Matt](https://avatars.discourse-cdn.com/v2/letter/m/a6a055/32.png) [@Matt](https://community.haxe.org/u/Matt)
#### Post date: [March 13, 2019, 10:51pm UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/17 "2019-03-13T22:51:47Z")

</div>

Trying on Windows 10, by the way.

---

<div class="post-metadata">

### Author: ![longde123](https://community.haxe.org/user_avatar/community.haxe.org/longde123/32/590_2.png) [@longde123](https://community.haxe.org/u/longde123)
#### Post date: [March 14, 2019, 1:49am UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/18 "2019-03-14T01:49:07Z")

</div>

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

---

<div class="post-metadata">

### Author: ![AdrianV](https://community.haxe.org/user_avatar/community.haxe.org/adrianv/32/152_2.png) [@AdrianV](https://community.haxe.org/u/AdrianV)
#### Post date: [March 14, 2019, 8:20am UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/19 "2019-03-14T08:20:49Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ibilon](https://community.haxe.org/user_avatar/community.haxe.org/ibilon/32/13_2.png) [@ibilon](https://community.haxe.org/u/ibilon)
#### Post date: [March 14, 2019, 10:22am UTC](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599/20 "2019-03-14T10:22:12Z")

</div>

> [@Matt](#):
>
> Unfortunately already running into various issues…

What issues did you encounter?  
If you have problems with mingw/ocaml you can try [GitHub - HaxeFoundation/ocamhaxe: OCaml distribution for Haxe compilation](https://github.com/HaxeFoundation/ocamhaxe).

[Next page](https://community.haxe.org/t/how-to-implement-new-haxe-target/1599.md?page=2)
