TypeScript Native (AOT) Compiler

Implementation of the native Ahead-of-Time compiler for TypeScript

Abstract

A lot of time has passed since the appearance of the C and C ++ programming languages, during this time many new languages ​​have appeared such as Java, C #, F #, Rust, Erlang, Python, etc. All new languages ​​brought something new to the way of programming and solving common programming problems. With the proliferation of the Internet, languages ​​such as JavaScript and its successor TypeScript have emerged. The latter, in turn, has become very popular for Web development.

Introduction

C and C ++ remain the leaders among compilers for native code, but over time, many people have switched from C ++ to new languages ​​such as TypeScript and have already forgotten the practices used for programming in C ++. Let me give you an attempt to develop a compiler that uses TypeScript as a language to compile machine code as C or C ++ does.

TypeScript Native (AOT) Compiler (hereinafter tsc)

Let’s see what is the difference between C / C ++ code and the same code in TypeScript.

C / C ++ code

#include <cstdio>

int foo(int val)
{
  return val;
}

int main()
{
  int val = 1;
  printf("Value: %d", foo(val));
  return 0;
}	

and analogue in TypeScript

function foo(val = 0)
{
  return val;
}

function main() {
  const val = 1;
  print("Value:", foo(val));
}

As we can see, there is not much difference between the code, but for lovers of TypeScript, the C / C ++ syntax has a lot of unnecessary things. For example, why explicitly specify the type of a variable if the compiler can determine its type itself at compile time.

tsc allows you to compile native code or execute immediately without compilation (JIT). This is convenient for checking the code without “long” compilation and when the code is ready, generate an executable file (EXE). It is also possible to generate WASM and execute it in Chrome and all browsers that support WASM.

tsc It also allows you to compile code using GC (Garbage collection) or disable it and manage memory yourself.

Interaction with C / C ++

Because tsc compiles native code, then interaction with C / C ++ is already available “out-of-box”, it is enough to declare the function that you want to use, for example:

type int64 = 2147483648;
declare malloc(size: int64): any;

Conclusion

tsc is being developed using LLVM and is currently in active development. You can take part in the development or try the compiler yourself on Git ASDAlexander77 / TypeScriptCompiler: TypeScript Compiler (by LLVM) (github.com)

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *