leonardoaraujosantos / Matlab_LLVM_Frontend

Generate LLVM IR from matlab source code.
14 stars 2 forks source link

Matlab LLVM Frontend

Scanning and Parsing

Roughly speaking, scanning divides the input into meaningful chunks, called tokens, and parsing figures out how the tokens relate to each other. For example, consider this snippet of C code:

alpha = beta + gamma ;

A scanner divides this into the tokens alpha, equal sign, beta, plus sign, gamma, and semicolon. Then the parser determines that beta + gamma is an expression, and that the expression is assigned to alpha.

Emiting code

After the parser outputs an AST tree, we just emit LLVM code for each tree node. The result is LLVM working version of a matlab program.

Example Matlab Input

function result = justSum(a,b)
  result = a + b
end

Result LLVM IR

; Simple sum function
define double @justSum(double %a, double %b) #0 {
  %result = fadd double %a, %b
  ret double %result
}

Chaning LLVM IR to output something .... (printf is part of LLVM)

; Declare 8-byte vector ['4','+','4','=','%','f','\0A','\00']
@.str = private unnamed_addr constant [8 x i8] c"4+4=%f\0A\00", align 1

; Declare prototype for printf
declare i32 @printf(i8*, ...)

; Same function
define double @justSum(double %a, double %b) #0 {
  %result = fadd double %a, %b 
  ret double %result
}

; main function 
define i32 @main() #0 {
  %1 = call double @justSum(double 4.000000e+00, double 4.000000e+00)
  %2 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([8 x i8]* @.str, i32 0, i32 0), double %1)
  ret i32 0
}

Interpretting IR

Interpret LLVM IR

lli justSum.ll

4+4=8.000000

Convert LLVM IR to byte code

llvm-as justSum.ll -o justSum.bc

Motivation

After our program is converted to LLVM IR we can do a lot of stuff like:

Generate x86-64

llc -march=x86-64 justSum.ll -o justSum.S

Compile generated x86-64 Assembly code

clang -o justSum justSum.S

Generate ARM 64-bit

llc -march=aarch64 justSum.ll -o justSum.S

Generate Javascript (requires emscripten)

emcc justSum.ll -o justSum.js
function _justSum($a,$b){
 var label=0;

 var $result=($a)+($b);
 return $result;
}

Creating a webpage with results

emcc justSum.ll -o justSum.html

Idea

Installation

git clone https://github.com/leonardoaraujosantos/Matlab_LLVM_Frontend.git
cd Matlab_LLVM_Frontend
mkdir build
cd build
cmake ..
make

Usage

mat2llvm simpleFunc.m -o simpleFunc.ll

Where are we?

We're going to work on the part related to Scanning, Parsing, and emiting Intermediate code.

alt tag

Reference Tutorials

Bellow we can find some references used by me during the project.

Compilers

LLVM

Flex Bison

Antlr (Version 3 output C/C++ code)

Other Matlab frontend