JIT (WIP): first files
[scilab.git] / scilab / modules / ast / src / cpp / jit / JITForExp.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2015 - Scilab Enterprises - Calixte DENIZET
4  *
5  *  This file must be used under the terms of the CeCILL.
6  *  This source file is licensed as described in the file COPYING, which
7  *  you should have received as part of this distribution.  The terms
8  *  are also available at
9  *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 #include "JITScalars.hxx"
14 #include "JITArrayofs.hxx"
15 #include "JITVisitor.hxx"
16
17 namespace jit
18 {
19     void JITVisitor::visit(const ast::VarDec & e)
20     {
21
22     }
23
24     void JITVisitor::visit(const ast::ForExp & e)
25     {
26         const ast::VarDec & vd = static_cast<const ast::VarDec &>(e.getVardec());
27         if (vd.getInit().isListExp())
28         {
29             const symbol::Symbol & symIterator = vd.getSymbol();
30             const ast::ListExp & le = static_cast<const ast::ListExp &>(vd.getInit());
31             if (le.getDecorator().getResult().getRange().isValid())
32             {
33                 // for i = start:step:end...
34                 // is equivalent to for (int64_t i = start; i < end + step; i += step)...
35                 
36                 le.getStart().accept(*this);
37                 llvm::Value * start = Cast::cast<int64_t>(getResult()->loadData(*this), false, *this);
38                 le.getStep().accept(*this);
39                 llvm::Value * step = Cast::cast<int64_t>(getResult()->loadData(*this), false, *this);
40                 le.getEnd().accept(*this);
41                 llvm::Value * end = Cast::cast<int64_t>(getResult()->loadData(*this), false, *this);
42                 end = builder.CreateAdd(end, step);
43
44                 llvm::BasicBlock * cur_block = builder.GetInsertBlock();
45                 llvm::BasicBlock * condBlock = llvm::BasicBlock::Create(context, "for_cond", function);
46                 llvm::BasicBlock * loopBlock = llvm::BasicBlock::Create(context, "for_loop", function);
47                 llvm::BasicBlock * afterBlock = llvm::BasicBlock::Create(context, "for_after", function);
48
49                 blocks.emplace(condBlock, afterBlock);
50                 
51                 llvm::Value * cmp_i1 = builder.CreateICmpSLT(start, end);
52                 builder.CreateCondBr(cmp_i1, loopBlock, afterBlock);
53
54                 
55                 builder.SetInsertPoint(loopBlock);
56                 llvm::PHINode * i = builder.CreatePHI(getTy<int64_t>(), 2);
57                 i->addIncoming(start, cur_block);
58                 JITScilabPtr & it = variables.find(symIterator)->second;
59                 it->storeData(*this, i);
60
61                 e.getBody().accept(*this);
62                 builder.CreateBr(condBlock);
63
64                 builder.SetInsertPoint(condBlock);
65                 llvm::Value * ipstp_i64 = builder.CreateAdd(i, step);
66                 i->addIncoming(ipstp_i64, condBlock);
67                 cmp_i1 = builder.CreateICmpSLT(ipstp_i64, end);
68                 builder.CreateCondBr(cmp_i1, loopBlock, afterBlock);
69
70                 builder.SetInsertPoint(afterBlock);
71 /*
72                 llvm::Value * cmp_i1 = builder.CreateICmpSLT(start, end);
73                 builder.CreateCondBr(cmp_i1, BBBody, BBAfter);
74
75                 builder.SetInsertPoint(BBBody);
76                 llvm::PHINode * i = builder.CreatePHI(getTy<int64_t>(), 2);
77                 i->addIncoming(start, cur_block);
78                 JITScilabPtr & it = variables.find(symIterator)->second;
79                 it->storeData(*this, i);
80
81                 e.getBody().accept(*this);
82
83                 if (builder.GetInsertBlock() != BBAfter)
84                 {
85                     BBAfter->moveAfter(builder.GetInsertBlock());
86                 }
87                 
88                 llvm::Value * ipstp_i64 = builder.CreateAdd(i, step);
89                 i->addIncoming(ipstp_i64, builder.GetInsertBlock());
90                 cmp_i1 = builder.CreateICmpSLT(ipstp_i64, end);
91                 builder.CreateCondBr(cmp_i1, BBBody, BBAfter);
92                 builder.SetInsertPoint(BBAfter);
93 */
94             }
95             
96         }
97
98         
99         // e.getBody().accept(*this);
100     }
101
102 }