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.
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.
function result = justSum(a,b)
result = a + b
end
; Simple sum function
define double @justSum(double %a, double %b) #0 {
%result = fadd double %a, %b
ret double %result
}
; 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
}
lli justSum.ll
4+4=8.000000
llvm-as justSum.ll -o justSum.bc
After our program is converted to LLVM IR we can do a lot of stuff like:
llc -march=x86-64 justSum.ll -o justSum.S
clang -o justSum justSum.S
llc -march=aarch64 justSum.ll -o justSum.S
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
git clone https://github.com/leonardoaraujosantos/Matlab_LLVM_Frontend.git
cd Matlab_LLVM_Frontend
mkdir build
cd build
cmake ..
make
mat2llvm simpleFunc.m -o simpleFunc.ll
We're going to work on the part related to Scanning, Parsing, and emiting Intermediate code.
Bellow we can find some references used by me during the project.