Giter VIP home page Giter VIP logo

ts-llvm's Introduction

Notice: This project is not being actively worked on. If you're interested to pick it up, let me know. See #12.

ts-llvm Build Status

ts-llvm is a compiler for TypeScript that generates LLVM code, which can then be optimized using all existing LLVM optimizations, and converted further into e.g. native code, WebAssembly, or JavaScript (using emscripten).

It uses the TypeScript Compiler API to parse and typecheck the input TypeScript code. The resulting TypeScript AST is transformed into LLVM IR using the llvm-node bindings. ts-llvm also provides a runtime library written in C++ that implements native replacements for built-in TypeScript APIs such as console.log.

ts-llvm is still in very early stages of development. For example, it cannot yet compile most TypeScript programs and it doesn't yet do any garbage collection. If you're willing to contribute, any help will be greatly appreciated. For questions you can create a GitHub issue or ask on the ts-llvm Slack.

Check out the language features roadmap and the standard library roadmap to see what has been implemented so far, and what still needs to be implemented for the v1.0 release.

Goals

  • Compile a strictly-typed subset of TypeScript to LLVM IR.
  • Use the TypeScript compiler API to parse and type-check the input code.
  • Implement the JavaScript standard library (such as the array and string types) in C++/C/Rust to allow TypeScript code compiled using ts-llvm to use it.
  • Experiment with ideas such as Node Addons written in TS compiled to native code using ts-llvm, with autogenerated JS/TS bindings.
  • Don't add new features to TypeScript, such as integer types or manual memory management.

Related projects

  • StaticScript, another TypeScript to LLVM compiler.
  • AssemblyScript, a TypeScript to WebAssembly compiler.
  • Speedy.js, a JavaScript subset to WebAssembly compiler. Uses the TypeScript compiler API.

Building from source

  1. You need to have LLVM installed on your system.
  2. Run npm install to install other dependencies. If llvm-config is not on your PATH, you may need to tell llvm-node where to find LLVM on your system, e.g. by running npm config set cmake_LLVM_DIR $(path-to-llvm/bin/llvm-config --cmakedir) before npm install.
  3. If you want ts-llvm to generate native code, you need to have llc and g++ (any C++ compiler) on your PATH. llc will not be required once llvm-node gains support for the LLVM legacy PassManager and addPassesToEmitFile APIs. g++ will not be required once ts-llvm learns to detect the system C++ compiler on its own.

Once everything above is set up, you can use the following commands:

  • npm run build will build the ts-llvm compiler in the build directory.
  • npm start will run the ts-llvm compiler. You can pass arguments to the compiler after two consecutive hyphens: e.g. npm start -- --help will print the compiler's usage information.
  • npm test will run the test suite. ts-llvm uses snapshot testing to test the output LLVM IR code. Use npm run updateSnapshots to update the snapshot files.

License

ts-llvm is licensed under the MIT license.

ts-llvm's People

Contributors

emlai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ts-llvm's Issues

question/comment about using ts-llvm and emscripten to replace webpack

Hi, I started looking for a JS frontend for the llvm today because I found webpack to be very difficult to use, and I was hoping I could use some combo of ts-llvm and emscripten to "compile" my javascript (and typescript) for the browser. I think this project would be suitable, but it doesn't seem to be feature complete enough yet.

I'm not sure if you considered a use case like this, I'm not even sure it would be very practical. But I thought it might be worth mentioning.

There didn't seem to be a js to llvm compiler out there, will this project work to compile plain js to llvm?

happy hacking,

Language features for v1.0

This issue lists language features to be implemented for the v1.0 release. For the standard library roadmap, see #7.

Note that ts-llvm will not attempt to implement all JavaScript/TypeScript language features, only those that are used the most and are compatible with strict typing. Language features not in this list will be implemented later.

See e.g. this JavaScript reference for a full list of JS language features:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference

For a full list of TypeScript-specific language features, see the TypeScript handbook:
https://www.typescriptlang.org/docs/handbook/basic-types.html


Data types:

  • Booleans
  • Numbers
  • Strings
  • Array of numbers/booleans/strings
  • Array of objects
  • Objects
  • Null or undefined
  • Functions as parameters and return values

Statements:

  • Top-level statements
  • if...else
  • while
  • for
  • for...of
  • break
  • continue

Declarations:

  • Global functions
  • Local functions
  • Variable declarations
  • Classes
  • Class constructors
  • Class methods

Expressions:

  • Number literals
  • Boolean literals
  • String literals
  • Object literals
    • Shorthand property names
  • Array literals
  • this (for classes)

Operators:

  • =
  • Comparison operators ===, !==, <, >, <=, >=
    • for numbers
    • for booleans
    • for strings
    • for objects
  • Arithmetic operators for numbers +, -, *, /, %
  • + for strings
  • Bitwise operators &, |, ^, ~, <<, >>, >>>
  • Logical operators !, &&, ||
  • Assignment operators +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, >>>=
  • [] (for arrays)

TS features:

  • Generic classes
  • Generic functions
  • Enums
  • Interfaces
  • Union types with at least null or undefined. See #5 for more discussion.
  • Function types

Miscellaneous:

  • Garbage collection (#1)

About Add Union Types

Hi, i want to add union type. However, i rarely use js, so some concepts which i understand may be error.
I will describe my thoughts about union type and if you approve it, then i will commit pr when done.

Example
ts code

function foo(): string | number | undefined;

converted to llvm-ir

%string = type { i8*, i32 }
%string.number.box = type { i32, %string, double}

so, the first i32 means the real return type's index, and then 0 means undefined.

Thus, we can use type { i32, [ 0 x i8 ] } to indicate undefined and use i32* null to indicate null in ts.

When union type is used on the parameter, i only get a certain type from parameter because function emitted when called, the type of parameter passed in at this time is ok. In this way, function will be emitted for each combination of parameter types. This can cause serious code bloat problem. Must do something to merge these function.

Will this project be continued?

I have to say this is exactly the wasm solution for typescript that I'm looking for, @emlai great job! ๐Ÿ‘ But I also notice this project has been stopped for 3 months, would this project be continued?

Standard library features for v1.0

This issue lists standard library features to be implemented for the v1.0 release.

Note that ts-llvm will not attempt to implement all of the JavaScript standard library, only those features that are used the most and are compatible with strict typing. Language features not in this list will be implemented later.

See e.g. this JavaScript reference for a full list of JS standard library features:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects

Standard library APIs implemented currently in ts-llvm can be seen in: https://github.com/ts-llvm/ts-llvm/blob/master/lib/lib.ts-llvm.d.ts. Their implementations are in https://github.com/ts-llvm/ts-llvm/tree/master/lib/runtime.


Array:

  • constructor from array literal
  • length (get only)
  • push (single parameter only)
  • element access via subscript
  • includes
  • join
  • indexOf
  • lastIndexOf
  • slice
  • toString
  • every
  • filter
  • find
  • findIndex
  • map
  • reduce
  • some

String:

  • constructor from string literal
  • length
  • concat (single parameter only)
  • includes
  • endsWith
  • startsWith
  • indexOf
  • lastIndexOf
  • replace (no regex)
  • split
  • slice
  • substring
  • toLowerCase
  • toUpperCase
  • trim
  • toString

Map:

  • constructor
  • size
  • clear
  • delete
  • get
  • has
  • set
  • keys
  • values

Set:

  • constructor
  • size
  • add
  • clear
  • delete
  • has
  • keys
  • values

console:

  • log

Global functions:

  • parseFloat
  • parseInt

Cannot run npm test

When I run npm test, I got an error:

Error: Command failed: llc -O3 -filetype=obj array.bc -o array.o
llc: array.bc: error: Invalid type for value

    at checkExecSyncError (child_process.js:600:11)
    at Object.execFileSync (child_process.js:618:13)
    at writeExecutableToFile (/Users/z/Projects/Node/ts-llvm/src/main.ts:106:5)
    at main (/Users/z/Projects/Node/ts-llvm/src/main.ts:72:5)
    at Object.<anonymous> (/Users/z/Projects/Node/ts-llvm/src/main.ts:22:3)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Module.m._compile (/Users/z/Projects/Node/ts-llvm/node_modules/ts-node/src/index.ts:439:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/Users/z/Projects/Node/ts-llvm/node_modules/ts-node/src/index.ts:442:12)
    at Module.load (internal/modules/cjs/loader.js:599:32)

or

$ npm test

> [email protected] test /Users/z/Projects/Node/ts-llvm
> ts-node test/test-runner.ts

Error: Command failed: llc -O3 -filetype=obj boolean.bc -o boolean.o
llc: boolean.bc: error: Invalid type for value

    at checkExecSyncError (child_process.js:600:11)
    at Object.execFileSync (child_process.js:618:13)
    at writeExecutableToFile (/Users/z/Projects/Node/ts-llvm/src/main.ts:106:5)
    at main (/Users/z/Projects/Node/ts-llvm/src/main.ts:72:5)
    at Object.<anonymous> (/Users/z/Projects/Node/ts-llvm/src/main.ts:22:3)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Module.m._compile (/Users/z/Projects/Node/ts-llvm/node_modules/ts-node/src/index.ts:439:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/Users/z/Projects/Node/ts-llvm/node_modules/ts-node/src/index.ts:442:12)
    at Module.load (internal/modules/cjs/loader.js:599:32)

I tried with [email protected] and [email protected] (macos, installed via brew), the npm install finished without any error message, did I miss something?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.