Code Monkey home page Code Monkey logo

irradiant's Introduction

Irradiant

A Clang-based C to Lua source-to-source compiler.

Implementation details

Irradiant's currently in the "hacky prototype" stage. It uses a visitor to traverse the Clang AST and directly output Lua code as it goes along - there is no creation of a Lua AST. This approach allows for a quick proof of concept (as C-without-types has a superficial resemblance to Lua), but breaks down when more complicated analysis is required (as is the case for use of the heap, structs, and more.)

This also means that it has to be conservative in the code that it outputs - as it doesn't know the context of each individual expression, it has to produce Lua that works in any context. For example, a++ is lowered to (function() local _ = a; a = a + 1; return _; end)() - even if the result of the increment is otherwise unused. An example of where this approach is excessive:

int i = 0;
while (i < 10)
	i++;

will become

local i = 0
while i < 10 do
    (function() local _ = i; i = i + 1; return _ end)()
end

There is considerable room for improvement here. A more advanced optimizer might be able to lift the increment out of the closure, and then remove the closure (as it's dead code).

Why not LLVM IR?

Emscripten, the LLVM IR to JS compiler, has proven that using LLVM IR is a viable approach. However, this means the semantics of the original language are lost, and the generated code is not particularly human readable. I wanted to build a C source-to-source compiler in which the original structure of the code was still fundamentally present.

What works

More or less everything in the test folder (which includes real C code!) Basic features:

  • Functions
  • Arrays
  • Binary operators (including pre/post increment/decrement operators)
  • Some unary operators
  • Ternary operators
  • If/else statements (including else if)
  • While loops
  • Do-while loops
  • Variable mutation in conditionals
  • Switch statements

What doesn't work

Everything unimplemented, but the big ones:

  • Pointers
  • Implicit fallthrough in switch statements
  • Structs
  • The heap
  • Basically anything complicated

irradiant's People

Contributors

philpax avatar

Watchers

 avatar

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.