comparison src/pt-jit.cc @ 14944:c0a5ab3b9278

jit_const no longer inherits from jit_instruction
author Max Brister <max@2bass.com>
date Fri, 08 Jun 2012 15:30:07 -0500
parents 8efcaf5aa233
children 591aeec5c520
comparison
equal deleted inserted replaced
14943:8efcaf5aa233 14944:c0a5ab3b9278
1224 iter != entry_block->end (); ++iter) 1224 iter != entry_block->end (); ++iter)
1225 if (jit_extract_argument *extract = dynamic_cast<jit_extract_argument *> (*iter)) 1225 if (jit_extract_argument *extract = dynamic_cast<jit_extract_argument *> (*iter))
1226 arguments.push_back (std::make_pair (extract->name (), true)); 1226 arguments.push_back (std::make_pair (extract->name (), true));
1227 1227
1228 convert_llvm to_llvm; 1228 convert_llvm to_llvm;
1229 function = to_llvm.convert (module, arguments, blocks); 1229 function = to_llvm.convert (module, arguments, blocks, constants);
1230 1230
1231 #ifdef OCTAVE_JIT_DEBUG 1231 #ifdef OCTAVE_JIT_DEBUG
1232 std::cout << "-------------------- llvm ir --------------------"; 1232 std::cout << "-------------------- llvm ir --------------------";
1233 llvm::raw_os_ostream llvm_cout (std::cout); 1233 llvm::raw_os_ostream llvm_cout (std::cout);
1234 function->print (llvm_cout); 1234 function->print (llvm_cout);
1287 1287
1288 void 1288 void
1289 jit_convert::visit_colon_expression (tree_colon_expression& expr) 1289 jit_convert::visit_colon_expression (tree_colon_expression& expr)
1290 { 1290 {
1291 // in the futher we need to add support for classes and deal with rvalues 1291 // in the futher we need to add support for classes and deal with rvalues
1292 jit_instruction *base = visit (expr.base ()); 1292 jit_value *base = visit (expr.base ());
1293 jit_instruction *limit = visit (expr.limit ()); 1293 jit_value *limit = visit (expr.limit ());
1294 jit_instruction *increment; 1294 jit_value *increment;
1295 tree_expression *tinc = expr.increment (); 1295 tree_expression *tinc = expr.increment ();
1296 1296
1297 if (tinc) 1297 if (tinc)
1298 increment = visit (tinc); 1298 increment = visit (tinc);
1299 else 1299 else
1300 { 1300 increment = create<jit_const_scalar> (1);
1301 increment = create<jit_const_scalar> (1);
1302 block->append (increment);
1303 }
1304 1301
1305 result = block->append (create<jit_call> (jit_typeinfo::make_range, base, 1302 result = block->append (create<jit_call> (jit_typeinfo::make_range, base,
1306 limit, increment)); 1303 limit, increment));
1307 1304
1308 } 1305 }
1409 block->append (create<jit_break> (check_block)); 1406 block->append (create<jit_break> (check_block));
1410 finish_breaks (check_block, continues); 1407 finish_breaks (check_block, continues);
1411 1408
1412 block = check_block; 1409 block = check_block;
1413 const jit_function& add_fn = jit_typeinfo::binary_op (octave_value::op_add); 1410 const jit_function& add_fn = jit_typeinfo::binary_op (octave_value::op_add);
1414 jit_instruction *one = create<jit_const_index> (1); 1411 jit_value *one = create<jit_const_index> (1);
1415 block->append (one);
1416
1417 jit_call *iter_inc = create<jit_call> (add_fn, iterator, one); 1412 jit_call *iter_inc = create<jit_call> (add_fn, iterator, one);
1418 block->append (iter_inc); 1413 block->append (iter_inc);
1419 block->append (create<jit_assign> (iterator, iter_inc)); 1414 block->append (create<jit_assign> (iterator, iter_inc));
1420 check = block->append (create<jit_call> (jit_typeinfo::for_check, control, 1415 check = block->append (create<jit_call> (jit_typeinfo::for_check, control,
1421 iterator)); 1416 iterator));
1526 blocks.push_back (entry_blocks[i]); 1521 blocks.push_back (entry_blocks[i]);
1527 1522
1528 if (! tic->is_else_clause ()) 1523 if (! tic->is_else_clause ())
1529 { 1524 {
1530 tree_expression *expr = tic->condition (); 1525 tree_expression *expr = tic->condition ();
1531 jit_instruction *cond = visit (expr); 1526 jit_value *cond = visit (expr);
1532 cond = create<jit_call> (&jit_typeinfo::logically_true, cond); 1527 jit_instruction *check = create<jit_call> (&jit_typeinfo::logically_true, cond);
1533 block->append (cond); 1528 block->append (check);
1534 1529
1535 jit_block *next = create<jit_block> (block->name () + "a"); 1530 jit_block *next = create<jit_block> (block->name () + "a");
1536 blocks.push_back (next); 1531 blocks.push_back (next);
1537 block->append (create<jit_check_error> (next, final_block)); 1532 block->append (create<jit_check_error> (next, final_block));
1538 block = next; 1533 block = next;
1539 1534
1540 jit_block *body = create<jit_block> (i == 0 ? "if_body" : "ifelse_body"); 1535 jit_block *body = create<jit_block> (i == 0 ? "if_body" : "ifelse_body");
1541 blocks.push_back (body); 1536 blocks.push_back (body);
1542 1537
1543 jit_instruction *br = create<jit_cond_break> (cond, body, 1538 jit_instruction *br = create<jit_cond_break> (check, body,
1544 entry_blocks[i + 1]); 1539 entry_blocks[i + 1]);
1545 block->append (br); 1540 block->append (br);
1546 block = body; 1541 block = body;
1547 } 1542 }
1548 1543
1613 Range rv = v.range_value (); 1608 Range rv = v.range_value ();
1614 result = create<jit_const_range> (rv); 1609 result = create<jit_const_range> (rv);
1615 } 1610 }
1616 else 1611 else
1617 fail ("Unknown constant"); 1612 fail ("Unknown constant");
1618
1619 block->append (result);
1620 } 1613 }
1621 1614
1622 void 1615 void
1623 jit_convert::visit_fcn_handle (tree_fcn_handle&) 1616 jit_convert::visit_fcn_handle (tree_fcn_handle&)
1624 { 1617 {
1658 void 1651 void
1659 jit_convert::visit_simple_assignment (tree_simple_assignment& tsa) 1652 jit_convert::visit_simple_assignment (tree_simple_assignment& tsa)
1660 { 1653 {
1661 // resolve rhs 1654 // resolve rhs
1662 tree_expression *rhs = tsa.right_hand_side (); 1655 tree_expression *rhs = tsa.right_hand_side ();
1663 jit_instruction *rhsv = visit (rhs); 1656 jit_value *rhsv = visit (rhs);
1664 1657
1665 // resolve lhs 1658 // resolve lhs
1666 tree_expression *lhs = tsa.left_hand_side (); 1659 tree_expression *lhs = tsa.left_hand_side ();
1667 if (! lhs->is_identifier ()) 1660 if (! lhs->is_identifier ())
1668 fail (); 1661 fail ();
1691 do_bind_ans = (! id->is_variable ()); 1684 do_bind_ans = (! id->is_variable ());
1692 } 1685 }
1693 else 1686 else
1694 do_bind_ans = (! expr->is_assignment_expression ()); 1687 do_bind_ans = (! expr->is_assignment_expression ());
1695 1688
1696 jit_instruction *expr_result = visit (expr); 1689 jit_value *expr_result = visit (expr);
1697 1690
1698 if (do_bind_ans) 1691 if (do_bind_ans)
1699 do_assign ("ans", expr_result, expr->print_result ()); 1692 do_assign ("ans", expr_result, expr->print_result ());
1700 else if (expr->is_identifier () && expr->print_result ()) 1693 else if (expr->is_identifier () && expr->print_result ())
1701 { 1694 {
1782 entry_block->prepend (extract); 1775 entry_block->prepend (extract);
1783 1776
1784 return vmap[vname] = var; 1777 return vmap[vname] = var;
1785 } 1778 }
1786 1779
1787 jit_instruction * 1780 jit_value *
1788 jit_convert::do_assign (const std::string& lhs, jit_instruction *rhs, 1781 jit_convert::do_assign (const std::string& lhs, jit_value *rhs,
1789 bool print) 1782 bool print)
1790 { 1783 {
1791 jit_variable *var = get_variable (lhs); 1784 jit_variable *var = get_variable (lhs);
1792 block->append (create<jit_assign> (var, rhs)); 1785 block->append (create<jit_assign> (var, rhs));
1793 1786
1796 const jit_function& print_fn = jit_typeinfo::print_value (); 1789 const jit_function& print_fn = jit_typeinfo::print_value ();
1797 jit_const_string *name = create<jit_const_string> (lhs); 1790 jit_const_string *name = create<jit_const_string> (lhs);
1798 block->append (create<jit_call> (print_fn, name, var)); 1791 block->append (create<jit_call> (print_fn, name, var));
1799 } 1792 }
1800 1793
1801 return rhs; 1794 return var;
1802 } 1795 }
1803 1796
1804 jit_instruction * 1797 jit_value *
1805 jit_convert::visit (tree& tee) 1798 jit_convert::visit (tree& tee)
1806 { 1799 {
1807 result = 0; 1800 result = 0;
1808 tee.accept (*this); 1801 tee.accept (*this);
1809 1802
1810 jit_instruction *ret = result; 1803 jit_value *ret = result;
1811 result = 0; 1804 result = 0;
1812 return ret; 1805 return ret;
1813 } 1806 }
1814 1807
1815 void 1808 void
1988 1981
1989 // -------------------- jit_convert::convert_llvm -------------------- 1982 // -------------------- jit_convert::convert_llvm --------------------
1990 llvm::Function * 1983 llvm::Function *
1991 jit_convert::convert_llvm::convert (llvm::Module *module, 1984 jit_convert::convert_llvm::convert (llvm::Module *module,
1992 const std::vector<std::pair< std::string, bool> >& args, 1985 const std::vector<std::pair< std::string, bool> >& args,
1993 const std::list<jit_block *>& blocks) 1986 const std::list<jit_block *>& blocks,
1987 const std::list<jit_value *>& constants)
1994 { 1988 {
1995 jit_type *any = jit_typeinfo::get_any (); 1989 jit_type *any = jit_typeinfo::get_any ();
1996 1990
1997 // argument is an array of octave_base_value*, or octave_base_value** 1991 // argument is an array of octave_base_value*, or octave_base_value**
1998 llvm::Type *arg_type = any->to_llvm (); // this is octave_base_value* 1992 llvm::Type *arg_type = any->to_llvm (); // this is octave_base_value*
2024 jblock->stash_llvm (block); 2018 jblock->stash_llvm (block);
2025 } 2019 }
2026 2020
2027 jit_block *first = *blocks.begin (); 2021 jit_block *first = *blocks.begin ();
2028 builder.CreateBr (first->to_llvm ()); 2022 builder.CreateBr (first->to_llvm ());
2023
2024 // constants aren't in the IR, we visit those first
2025 for (std::list<jit_value *>::const_iterator iter = constants.begin ();
2026 iter != constants.end (); ++iter)
2027 if (! isa<jit_instruction> (*iter))
2028 visit (*iter);
2029 2029
2030 // convert all instructions 2030 // convert all instructions
2031 for (biter = blocks.begin (); biter != blocks.end (); ++biter) 2031 for (biter = blocks.begin (); biter != blocks.end (); ++biter)
2032 visit (*biter); 2032 visit (*biter);
2033 2033