Skip to content

Commit

Permalink
Add generating of for loop to kaleidoscope
Browse files Browse the repository at this point in the history
  • Loading branch information
garazdawi committed Jun 20, 2011
1 parent 9c59b1e commit f3905d9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
6 changes: 5 additions & 1 deletion tests/kaleidoscope_SUITE.erl
Expand Up @@ -25,7 +25,7 @@ end_per_testcase(_Tc, _Config) ->
ok.

all() ->
[adder, complex, extern].
[adder, complex, extern, condition].

adder(_Config) ->
compile(["def adding(test1,test2) test1+test2;"]).
Expand All @@ -41,6 +41,10 @@ extern(_Config) ->
condition(_Config) ->
compile(["def fib(x) if x < 3.0 then 1.0 else fib(x-1.0)+fib(x-2.0);"]).

for(_Config) ->
compile(["extern putchard(char);",
"def printstar(n) for i = 1.0, i < n, 1.0 in putchard(42.0);",
"def printstar2(n) for i = 1.0, i < n in putchard(42.0);"]).

compile(Strs) ->
Ctx = llevm:'LLVMGetGlobalContext'(),
Expand Down
36 changes: 35 additions & 1 deletion tests/kaleidoscope_SUITE_data/kal_gen.erl
Expand Up @@ -76,7 +76,41 @@ gen_body(M, B, Params, {'if', Cond, Then, Else}) ->
llevm:'LLVMPositionBuilderAtEnd'(B, ContBB),
PhiNode = llevm:'LLVMBuildPhi'(B, llevm:'LLVMDoubleType'(), "iftmp"),
llevm:'LLVMAddIncoming'(PhiNode, {ThenV,ElseV},{ThenBB,ElseBB},2),
PhiNode.
PhiNode;
gen_body(M, B, Params, {'for', {variable,VarName}, Value, Cond, Incr, Body}) ->
StartValRef = gen_body(M, B, Params, Value),

PreBB = llevm:'LLVMGetInsertBlock'(B),
FunRef = llevm:'LLVMGetBasicBlockParent'(PreBB),
LoopBB = llevm:'LLVMAppendBasicBlock'(FunRef, "loop"),
AfterBB = llevm:'LLVMAppendBasicBlock'(FunRef, "after_loop"),

llevm:'LLVMBuildBr'(B, LoopBB),

llevm:'LLVMPositionBuilderAtEnd'(B, LoopBB),
PhiVal = llevm:'LLVMBuildPhi'(B, llevm:'LLVMDoubleType'(), VarName),
NewParams = [{VarName,PhiVal}|Params],
gen_body(M, B, NewParams, Body),

StepVal = case Incr of
undefined ->
gen_body(M, B, NewParams, {const, "1.0"});
_Else ->
gen_body(M, B, NewParams, Incr)
end,
NextVal = llevm:'LLVMBuildFAdd'(B, StepVal, PhiVal, "nextvar"),

llevm:'LLVMAddIncoming'(PhiVal, {StartValRef,NextVal},{PreBB,LoopBB},2),

CondRef = gen_body(M, B, NewParams, Cond),
CmpRef = llevm:'LLVMBuildFCmp'(B, {'LLVMRealPredicate',7}, CondRef,
gen_body(M, B, NewParams, {const,"0.0"}),
"ifcond"),

llevm:'LLVMBuildCondBr'(B, CmpRef, LoopBB, AfterBB),

llevm:'LLVMPositionBuilderAtEnd'(B, AfterBB),
llevm:'LLVMConstNull'(llevm:'LLVMDoubleType'()).


get_params(FunRef, [{variable, Name}|Rest], I) ->
Expand Down
4 changes: 3 additions & 1 deletion tests/kaleidoscope_SUITE_data/kal_parse.yrl
@@ -1,5 +1,5 @@
Nonterminals module variable expressions toplevelexpr expression variables params external function prototype.
Terminals def extern num ident '(' ')' ';' ',' '+' '-' '<' '*' 'if' 'then' 'else'.
Terminals def extern num ident '(' ')' ';' ',' '+' '-' '<' '*' '=' 'if' 'then' 'else' 'for' 'in'.

Rootsymbol module.
Left 100 '+'.
Expand Down Expand Up @@ -31,6 +31,8 @@ expression -> expression '-' expression : {'-', '$1', '$3'}.
expression -> expression '*' expression : {'*', '$1', '$3'}.
expression -> expression '<' expression : {'<', '$1', '$3'}.
expression -> 'if' expression 'then' expression 'else' expression : {'if', '$2','$4','$6'}.
expression -> 'for' variable '=' expression ',' expression 'in' expression : {for, '$2', '$4', '$6', undefined, '$8'}.
expression -> 'for' variable '=' expression ',' expression ',' expression 'in' expression : {for, '$2', '$4', '$6', '$8', '$10'}.
expression -> ident '(' params ')' : {call, value_of('$1'),'$3'}.

params -> expression : ['$1'].
Expand Down
6 changes: 5 additions & 1 deletion tests/kaleidoscope_SUITE_data/kal_scan.erl
Expand Up @@ -21,6 +21,10 @@ scan("then"++Rest, none, Acc) ->
scan(Rest, none, [{then,num()}|Acc]);
scan("else"++Rest, none, Acc) ->
scan(Rest, none, [{else,num()}|Acc]);
scan("for"++Rest, none, Acc) ->
scan(Rest, none, [{for,num()}|Acc]);
scan("in"++Rest, none, Acc) ->
scan(Rest, none, [{in,num()}|Acc]);
scan(" "++Rest, none, Acc) ->
scan(Rest, none, Acc);
scan("\n"++Rest, none, Acc) ->
Expand All @@ -30,7 +34,7 @@ scan("\n"++Rest, none, Acc) ->
scan([Tok|Rest], none, Acc)
when Tok == $(; Tok == $); Tok == $;; Tok == $,;
Tok == $+; Tok == $-; Tok == $/; Tok == $*;
Tok == $< ->
Tok == $<; Tok == $= ->
scan(Rest, none, [{list_to_atom([Tok]),num()}|Acc]);

%% Number scan
Expand Down

0 comments on commit f3905d9

Please sign in to comment.