GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
jit-typeinfo.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2012-2013 Max Brister
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 // Author: Max Brister <max@2bass.com>
24 
25 // defines required by llvm
26 #define __STDC_LIMIT_MACROS
27 #define __STDC_CONSTANT_MACROS
28 
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32 
33 #ifdef HAVE_LLVM
34 
35 #include "jit-typeinfo.h"
36 
37 #include <llvm/Analysis/Verifier.h>
38 #include <llvm/ExecutionEngine/ExecutionEngine.h>
39 
40 #ifdef HAVE_LLVM_IR_FUNCTION_H
41 #include <llvm/IR/GlobalVariable.h>
42 #include <llvm/IR/LLVMContext.h>
43 #include <llvm/IR/Function.h>
44 #include <llvm/IR/Instructions.h>
45 #include <llvm/IR/Intrinsics.h>
46 #else
47 #include <llvm/GlobalVariable.h>
48 #include <llvm/LLVMContext.h>
49 #include <llvm/Function.h>
50 #include <llvm/Instructions.h>
51 #include <llvm/Intrinsics.h>
52 #endif
53 
54 #ifdef HAVE_LLVM_SUPPORT_IRBUILDER_H
55 #include <llvm/Support/IRBuilder.h>
56 #elif defined(HAVE_LLVM_IR_IRBUILDER_H)
57 #include <llvm/IR/IRBuilder.h>
58 #else
59 #include <llvm/IRBuilder.h>
60 #endif
61 
62 #include <llvm/Support/raw_os_ostream.h>
63 
64 #include "jit-ir.h"
65 #include "ov.h"
66 #include "ov-builtin.h"
67 #include "ov-complex.h"
68 #include "ov-scalar.h"
69 #include "pager.h"
70 
71 static llvm::LLVMContext& context = llvm::getGlobalContext ();
72 
74 
75 std::ostream& jit_print (std::ostream& os, jit_type *atype)
76 {
77  if (! atype)
78  return os << "null";
79  return os << atype->name ();
80 }
81 
82 // function that jit code calls
83 extern "C" void
84 octave_jit_print_any (const char *name, octave_base_value *obv)
85 {
86  obv->print_with_name (octave_stdout, name, true);
87 }
88 
89 extern "C" void
90 octave_jit_print_scalar (const char *name, double value)
91 {
92  // FIXME: We should avoid allocating a new octave_scalar each time
93  octave_value ov (value);
94  ov.print_with_name (octave_stdout, name);
95 }
96 
97 extern "C" octave_base_value*
99  octave_base_value *rhs)
100 {
101  octave_value olhs (lhs, true);
102  octave_value orhs (rhs, true);
103  octave_value result = do_binary_op (op, olhs, orhs);
104  octave_base_value *rep = result.internal_rep ();
105  rep->grab ();
106  return rep;
107 }
108 
109 extern "C" octave_idx_type
110 octave_jit_compute_nelem (double base, double limit, double inc)
111 {
112  Range rng = Range (base, limit, inc);
113  return rng.nelem ();
114 }
115 
116 extern "C" void
118 {
119  obv->release ();
120 }
121 
122 extern "C" void
124 {
125  delete m->array;
126 }
127 
128 extern "C" octave_base_value *
130 {
131  obv->grab ();
132  return obv;
133 }
134 
135 extern "C" jit_matrix
137 {
138  return *m->array;
139 }
140 
141 extern "C" octave_base_value *
143 {
144  octave_value ret (*m->array);
145  octave_base_value *rep = ret.internal_rep ();
146  rep->grab ();
147  delete m->array;
148 
149  return rep;
150 }
151 
152 extern "C" jit_matrix
154 {
155  NDArray m = obv->array_value ();
156  obv->release ();
157  return m;
158 }
159 
160 extern "C" octave_base_value *
162 {
163  Range temp (*rng);
164  octave_value ret (temp);
165  octave_base_value *rep = ret.internal_rep ();
166  rep->grab ();
167 
168  return rep;
169 }
170 extern "C" jit_range
172 {
173 
174  jit_range r (obv->range_value ());
175  obv->release ();
176  return r;
177 }
178 
179 extern "C" double
181 {
182  double ret = obv->double_value ();
183  obv->release ();
184  return ret;
185 }
186 
187 extern "C" octave_base_value *
189 {
190  return new octave_scalar (value);
191 }
192 
193 extern "C" Complex
195 {
196  Complex ret = obv->complex_value ();
197  obv->release ();
198  return ret;
199 }
200 
201 extern "C" octave_base_value *
203 {
204  if (c.imag () == 0)
205  return new octave_scalar (c.real ());
206  else
207  return new octave_complex (c);
208 }
209 
210 extern "C" void
212 {
213  try
214  {
216  }
217  catch (const octave_execution_exception&)
218  {
220  }
221 }
222 
223 extern "C" void
225 {
226  try
227  {
229  }
230  catch (const octave_execution_exception&)
231  {
233  }
234 }
235 
236 extern "C" void
238  octave_idx_type ext)
239 {
240  try
241  {
242  gripe_index_out_of_range (nd, dim, iext, ext);
243  }
244  catch (const octave_execution_exception&)
245  {
247  }
248 }
249 
250 extern "C" jit_matrix
252  double value)
253 {
254  NDArray *array = mat->array;
255  if (array->nelem () < index)
256  array->resize1 (index);
257 
258  double *data = array->fortran_vec ();
259  data[index - 1] = value;
260 
261  mat->update ();
262  return *mat;
263 }
264 
265 static void
266 make_indices (double *indices, octave_idx_type idx_count,
267  Array<idx_vector>& result)
268 {
269  result.resize (dim_vector (1, idx_count));
270  for (octave_idx_type i = 0; i < idx_count; ++i)
271  result(i) = idx_vector (indices[i]);
272 }
273 
274 extern "C" double
275 octave_jit_paren_scalar (jit_matrix *mat, double *indicies,
276  octave_idx_type idx_count)
277 {
278  // FIXME: Replace this with a more optimal version
279  try
280  {
281  Array<idx_vector> idx;
282  make_indices (indicies, idx_count, idx);
283 
284  Array<double> ret = mat->array->index (idx);
285  return ret.xelem (0);
286  }
287  catch (const octave_execution_exception&)
288  {
290  return 0;
291  }
292 }
293 
294 extern "C" jit_matrix
296  octave_idx_type idx_count, double value)
297 {
298  // FIXME: Replace this with a more optimal version
299  jit_matrix ret;
300  try
301  {
302  Array<idx_vector> idx;
303  make_indices (indices, idx_count, idx);
304 
305  Matrix temp (1, 1);
306  temp.xelem(0) = value;
307  mat->array->assign (idx, temp);
308  ret.update (mat->array);
309  }
310  catch (const octave_execution_exception&)
311  {
313  }
314 
315  return ret;
316 }
317 
318 extern "C" jit_matrix
320  double value)
321 {
322  NDArray *array = mat->array;
323  bool done = false;
324 
325  // optimize for the simple case (no resizing and no errors)
326  if (*array->jit_ref_count () == 1
327  && index->all_elements_are_ints ())
328  {
329  // this code is similar to idx_vector::fill, but we avoid allocating an
330  // idx_vector and its associated rep
331  octave_idx_type start = static_cast<octave_idx_type> (index->base) - 1;
332  octave_idx_type step = static_cast<octave_idx_type> (index->inc);
333  octave_idx_type nelem = index->nelem;
334  octave_idx_type final = start + nelem * step;
335  if (step < 0)
336  {
337  step = -step;
338  std::swap (final, start);
339  }
340 
341  if (start >= 0 && final < mat->slice_len)
342  {
343  done = true;
344 
345  double *data = array->jit_slice_data ();
346  if (step == 1)
347  std::fill (data + start, data + start + nelem, value);
348  else
349  {
350  for (octave_idx_type i = start; i < final; i += step)
351  data[i] = value;
352  }
353  }
354  }
355 
356  if (! done)
357  {
358  idx_vector idx (*index);
359  NDArray avalue (dim_vector (1, 1));
360  avalue.xelem (0) = value;
361  array->assign (idx, avalue);
362  }
363 
364  jit_matrix ret;
365  ret.update (array);
366  return ret;
367 }
368 
369 extern "C" double
371  octave_idx_type count)
372 {
373  octave_idx_type ndim = mat->dimensions[-1];
374  if (ndim == count)
375  return mat->dimensions[idx];
376  else if (ndim > count)
377  {
378  if (idx == count - 1)
379  {
380  double ret = mat->dimensions[idx];
381  for (octave_idx_type i = idx + 1; i < ndim; ++i)
382  ret *= mat->dimensions[idx];
383  return ret;
384  }
385 
386  return mat->dimensions[idx];
387  }
388  else // ndim < count
389  return idx < ndim ? mat->dimensions[idx] : 1;
390 }
391 
392 extern "C" octave_base_value *
394 {
395  octave_value undef;
396  octave_base_value *ret = undef.internal_rep ();
397  ret->grab ();
398 
399  return ret;
400 }
401 
402 extern "C" Complex
404 {
405  if (lhs.imag () == 0 && rhs.imag() == 0)
406  return Complex (lhs.real () * rhs.real (), 0);
407 
408  return lhs * rhs;
409 }
410 
411 extern "C" Complex
413 {
414  // see src/OPERATORS/op-cs-cs.cc
415  if (rhs == 0.0)
417 
418  return lhs / rhs;
419 }
420 
421 // FIXME: CP form src/xpow.cc
422 static inline int
423 xisint (double x)
424 {
425  return (D_NINT (x) == x
426  && ((x >= 0 && x < std::numeric_limits<int>::max ())
427  || (x <= 0 && x > std::numeric_limits<int>::min ())));
428 }
429 
430 extern "C" Complex
431 octave_jit_pow_scalar_scalar (double lhs, double rhs)
432 {
433  // FIXME: almost CP from src/xpow.cc
434  if (lhs < 0.0 && ! xisint (rhs))
435  return std::pow (Complex (lhs), rhs);
436  return std::pow (lhs, rhs);
437 }
438 
439 extern "C" Complex
441 {
442  if (lhs.imag () == 0 && rhs.imag () == 0)
443  return octave_jit_pow_scalar_scalar (lhs.real (), rhs.real ());
444  return std::pow (lhs, rhs);
445 }
446 
447 extern "C" Complex
449 {
450  if (lhs.imag () == 0)
451  return octave_jit_pow_scalar_scalar (lhs.real (), rhs);
452  return std::pow (lhs, rhs);
453 }
454 
455 extern "C" Complex
457 {
458  if (rhs.imag () == 0)
459  return octave_jit_pow_scalar_scalar (lhs, rhs.real ());
460  return std::pow (lhs, rhs);
461 }
462 
463 extern "C" void
465 {
466  std::cout << *m << std::endl;
467 }
468 
469 static void
471 {
472  error ("incorrect type information given to the JIT compiler");
473 }
474 
475 // FIXME: Add support for multiple outputs
476 extern "C" octave_base_value *
478  octave_base_value **argin, jit_type *result_type)
479 {
480  octave_value_list ovl (nargin);
481  for (size_t i = 0; i < nargin; ++i)
482  ovl.xelem (i) = octave_value (argin[i]);
483 
484  ovl = fn (ovl, 1);
485 
486  // FIXME: Check result_type somehow
487  if (result_type)
488  {
489  if (ovl.length () < 1)
490  {
491  gripe_bad_result ();
492  return 0;
493  }
494 
495  octave_value result = ovl.xelem(0);
496  octave_base_value *ret = result.internal_rep ();
497  ret->grab ();
498  return ret;
499  }
500 
501  if (! (ovl.length () == 0
502  || (ovl.length () == 1 && ovl.xelem (0).is_undefined ())))
503  gripe_bad_result ();
504 
505  return 0;
506 }
507 
508 // -------------------- jit_range --------------------
509 bool
511 {
512  Range r (*this);
513  return r.all_elements_are_ints ();
514 }
515 
516 std::ostream&
517 operator<< (std::ostream& os, const jit_range& rng)
518 {
519  return os << "Range[" << rng.base << ", " << rng.limit << ", " << rng.inc
520  << ", " << rng.nelem << "]";
521 }
522 
523 // -------------------- jit_matrix --------------------
524 
525 std::ostream&
526 operator<< (std::ostream& os, const jit_matrix& mat)
527 {
528  return os << "Matrix[" << mat.ref_count << ", " << mat.slice_data << ", "
529  << mat.slice_len << ", " << mat.dimensions << ", "
530  << mat.array << "]";
531 }
532 
533 // -------------------- jit_type --------------------
534 jit_type::jit_type (const std::string& aname, jit_type *aparent,
535  llvm::Type *allvm_type, bool askip_paren, int aid) :
536  mname (aname), mparent (aparent), llvm_type (allvm_type), mid (aid),
537  mdepth (aparent ? aparent->mdepth + 1 : 0), mskip_paren (askip_paren)
538 {
539  std::memset (msret, 0, sizeof (msret));
540  std::memset (mpointer_arg, 0, sizeof (mpointer_arg));
541  std::memset (mpack, 0, sizeof (mpack));
542  std::memset (munpack, 0, sizeof (munpack));
543 
544  for (size_t i = 0; i < jit_convention::length; ++i)
545  mpacked_type[i] = llvm_type;
546 }
547 
548 llvm::Type *
550 {
551  return llvm_type ? llvm_type->getPointerTo () : 0;
552 }
553 
554 // -------------------- jit_function --------------------
555 jit_function::jit_function () : module (0), llvm_function (0), mresult (0),
556  call_conv (jit_convention::length),
557  mcan_error (false)
558 {}
559 
560 jit_function::jit_function (llvm::Module *amodule,
561  jit_convention::type acall_conv,
562  const llvm::Twine& aname, jit_type *aresult,
563  const std::vector<jit_type *>& aargs)
564  : module (amodule), mresult (aresult), args (aargs), call_conv (acall_conv),
565  mcan_error (false)
566 {
567  llvm::SmallVector<llvm::Type *, 15> llvm_args;
568 
569  llvm::Type *rtype = llvm::Type::getVoidTy (context);
570  if (mresult)
571  {
572  rtype = mresult->packed_type (call_conv);
573  if (sret ())
574  {
575  llvm_args.push_back (rtype->getPointerTo ());
576  rtype = llvm::Type::getVoidTy (context);
577  }
578  }
579 
580  for (std::vector<jit_type *>::const_iterator iter = args.begin ();
581  iter != args.end (); ++iter)
582  {
583  jit_type *ty = *iter;
584  assert (ty);
585  llvm::Type *argty = ty->packed_type (call_conv);
586  if (ty->pointer_arg (call_conv))
587  argty = argty->getPointerTo ();
588 
589  llvm_args.push_back (argty);
590  }
591 
592  // we mark all functinos as external linkage because this prevents llvm
593  // from getting rid of always inline functions
594  llvm::FunctionType *ft = llvm::FunctionType::get (rtype, llvm_args, false);
595  llvm_function = llvm::Function::Create (ft, llvm::Function::ExternalLinkage,
596  aname, module);
597 
598  if (sret ())
599  {
600 #ifdef FUNCTION_ADDATTRIBUTE_ARG_IS_ATTRIBUTES
601  llvm::AttrBuilder attr_builder;
602  attr_builder.addAttribute (llvm::Attributes::StructRet);
603  llvm::Attributes attrs = llvm::Attributes::get(context, attr_builder);
604  llvm_function->addAttribute (1, attrs);
605 #else
606  llvm_function->addAttribute (1, llvm::Attribute::StructRet);
607 #endif
608  }
609 
611 #ifdef FUNCTION_ADDFNATTR_ARG_IS_ATTRIBUTES
612  llvm_function->addFnAttr (llvm::Attributes::AlwaysInline);
613 #else
614  llvm_function->addFnAttr (llvm::Attribute::AlwaysInline);
615 #endif
616 }
617 
619  const std::vector<jit_type *>& aargs)
620  : module (fn.module), llvm_function (fn.llvm_function), mresult (aresult),
621  args (aargs), call_conv (fn.call_conv), mcan_error (fn.mcan_error)
622 {
623 }
624 
626  : module (fn.module), llvm_function (fn.llvm_function), mresult (fn.mresult),
627  args (fn.args), call_conv (fn.call_conv), mcan_error (fn.mcan_error)
628 {}
629 
630 void
632 {
633  if (! llvm_function)
634  return;
635 
636  llvm_function->eraseFromParent ();
637  llvm_function = 0;
638 }
639 
640 std::string
641 jit_function::name (void) const
642 {
643  return llvm_function->getName ();
644 }
645 
646 llvm::BasicBlock *
647 jit_function::new_block (const std::string& aname,
648  llvm::BasicBlock *insert_before)
649 {
650  return llvm::BasicBlock::Create (context, aname, llvm_function,
651  insert_before);
652 }
653 
654 llvm::Value *
656  const std::vector<jit_value *>& in_args) const
657 {
658  if (! valid ())
659  throw jit_fail_exception ("Call not implemented");
660 
661  assert (in_args.size () == args.size ());
662  std::vector<llvm::Value *> llvm_args (args.size ());
663  for (size_t i = 0; i < in_args.size (); ++i)
664  llvm_args[i] = in_args[i]->to_llvm ();
665 
666  return call (builder, llvm_args);
667 }
668 
669 llvm::Value *
671  const std::vector<llvm::Value *>& in_args) const
672 {
673  if (! valid ())
674  throw jit_fail_exception ("Call not implemented");
675 
676  assert (in_args.size () == args.size ());
677  llvm::SmallVector<llvm::Value *, 10> llvm_args;
678  llvm_args.reserve (in_args.size () + sret ());
679 
680  llvm::BasicBlock *insert_block = builder.GetInsertBlock ();
681  llvm::Function *parent = insert_block->getParent ();
682  assert (parent);
683 
684  // we insert allocas inside the prelude block to prevent stack overflows
685  llvm::BasicBlock& prelude = parent->getEntryBlock ();
686  llvm::IRBuilder<> pre_builder (&prelude, prelude.begin ());
687 
688  llvm::AllocaInst *sret_mem = 0;
689  if (sret ())
690  {
691  sret_mem = pre_builder.CreateAlloca (mresult->packed_type (call_conv));
692  llvm_args.push_back (sret_mem);
693  }
694 
695  for (size_t i = 0; i < in_args.size (); ++i)
696  {
697  llvm::Value *arg = in_args[i];
699  if (convert)
700  arg = convert (builder, arg);
701 
702  if (args[i]->pointer_arg (call_conv))
703  {
704  llvm::Type *ty = args[i]->packed_type (call_conv);
705  llvm::Value *alloca = pre_builder.CreateAlloca (ty);
706  builder.CreateStore (arg, alloca);
707  arg = alloca;
708  }
709 
710  llvm_args.push_back (arg);
711  }
712 
713  llvm::CallInst *callinst = builder.CreateCall (llvm_function, llvm_args);
714  llvm::Value *ret = callinst;
715 
716  if (sret ())
717  {
718 #ifdef CALLINST_ADDATTRIBUTE_ARG_IS_ATTRIBUTES
719  llvm::AttrBuilder attr_builder;
720  attr_builder.addAttribute(llvm::Attributes::StructRet);
721  llvm::Attributes attrs = llvm::Attributes::get(context, attr_builder);
722  callinst->addAttribute (1, attrs);
723 #else
724  callinst->addAttribute (1, llvm::Attribute::StructRet);
725 #endif
726  ret = builder.CreateLoad (sret_mem);
727  }
728 
729  if (mresult)
730  {
732  if (unpack)
733  ret = unpack (builder, ret);
734  }
735 
736  return ret;
737 }
738 
739 llvm::Value *
741 {
742  assert (idx < args.size ());
743 
744  // FIXME: We should be treating arguments like a list, not a vector. Shouldn't
745  // matter much for now, as the number of arguments shouldn't be much bigger
746  // than 4
747  llvm::Function::arg_iterator iter = llvm_function->arg_begin ();
748  if (sret ())
749  ++iter;
750 
751  for (size_t i = 0; i < idx; ++i, ++iter);
752 
753  if (args[idx]->pointer_arg (call_conv))
754  return builder.CreateLoad (iter);
755 
756  return iter;
757 }
758 
759 void
761  bool verify)
762 {
763  assert (! rval == ! mresult);
764 
765  if (rval)
766  {
768  if (convert)
769  rval = convert (builder, rval);
770 
771  if (sret ())
772  {
773  builder.CreateStore (rval, llvm_function->arg_begin ());
774  builder.CreateRetVoid ();
775  }
776  else
777  builder.CreateRet (rval);
778  }
779  else
780  builder.CreateRetVoid ();
781 
782  if (verify)
783  llvm::verifyFunction (*llvm_function);
784 }
785 
786 void
787 jit_function::do_add_mapping (llvm::ExecutionEngine *engine, void *fn)
788 {
789  assert (valid ());
790  engine->addGlobalMapping (llvm_function, fn);
791 }
792 
793 std::ostream&
794 operator<< (std::ostream& os, const jit_function& fn)
795 {
796  llvm::Function *lfn = fn.to_llvm ();
797  os << "jit_function: cc=" << fn.call_conv;
798  llvm::raw_os_ostream llvm_out (os);
799  lfn->print (llvm_out);
800  llvm_out.flush ();
801  return os;
802 }
803 
804 // -------------------- jit_operation --------------------
806 {
807  for (generated_map::iterator iter = generated.begin ();
808  iter != generated.end (); ++iter)
809  {
810  delete iter->first;
811  delete iter->second;
812  }
813 }
814 
815 void
817  const std::vector<jit_type*>& args)
818 {
819  if (args.size () >= overloads.size ())
820  overloads.resize (args.size () + 1);
821 
822  Array<jit_function>& over = overloads[args.size ()];
823  dim_vector dv (over.dims ());
824  Array<octave_idx_type> idx = to_idx (args);
825  bool must_resize = false;
826 
827  if (dv.length () != idx.numel ())
828  {
829  dv.resize (idx.numel ());
830  must_resize = true;
831  }
832 
833  for (octave_idx_type i = 0; i < dv.length (); ++i)
834  if (dv(i) <= idx(i))
835  {
836  must_resize = true;
837  dv(i) = idx(i) + 1;
838  }
839 
840  if (must_resize)
841  over.resize (dv);
842 
843  over(idx) = func;
844 }
845 
846 const jit_function&
847 jit_operation::overload (const std::vector<jit_type*>& types) const
848 {
849  static jit_function null_overload;
850  for (size_t i =0; i < types.size (); ++i)
851  if (! types[i])
852  return null_overload;
853 
854  if (types.size () >= overloads.size ())
855  return do_generate (types);
856 
857  const Array<jit_function>& over = overloads[types.size ()];
858  dim_vector dv (over.dims ());
859  Array<octave_idx_type> idx = to_idx (types);
860  for (octave_idx_type i = 0; i < dv.length (); ++i)
861  if (idx(i) >= dv(i))
862  return do_generate (types);
863 
864  const jit_function& ret = over(idx);
865  if (! ret.valid ())
866  return do_generate (types);
867 
868  return ret;
869 }
870 
872 jit_operation::to_idx (const std::vector<jit_type*>& types) const
873 {
874  octave_idx_type numel = types.size ();
875  numel = std::max (2, numel);
876 
877  Array<octave_idx_type> idx (dim_vector (1, numel));
878  for (octave_idx_type i = 0; i < static_cast<octave_idx_type> (types.size ());
879  ++i)
880  idx(i) = types[i]->type_id ();
881 
882  if (types.size () == 0)
883  idx(0) = idx(1) = 0;
884  if (types.size () == 1)
885  {
886  idx(1) = idx(0);
887  idx(0) = 0;
888  }
889 
890  return idx;
891 }
892 
893 const jit_function&
895 {
896  static jit_function null_overload;
897  generated_map::const_iterator find = generated.find (&types);
898  if (find != generated.end ())
899  {
900  if (find->second)
901  return *find->second;
902  else
903  return null_overload;
904  }
905 
906  jit_function *ret = generate (types);
907  generated[new signature_vec (types)] = ret;
908  return ret ? *ret : null_overload;
909 }
910 
911 jit_function *
913 {
914  return 0;
915 }
916 
917 bool
920 {
921  const signature_vec& l = *lhs;
922  const signature_vec& r = *rhs;
923 
924  if (l.size () < r.size ())
925  return true;
926  else if (l.size () > r.size ())
927  return false;
928 
929  for (size_t i = 0; i < l.size (); ++i)
930  {
931  if (l[i]->type_id () < r[i]->type_id ())
932  return true;
933  else if (l[i]->type_id () > r[i]->type_id ())
934  return false;
935  }
936 
937  return false;
938 }
939 
940 // -------------------- jit_index_operation --------------------
941 jit_function *
943 {
944  if (types.size () > 2 && types[0] == jit_typeinfo::get_matrix ())
945  {
946  // indexing a matrix with scalars
948  for (size_t i = 1; i < types.size (); ++i)
949  if (types[i] != scalar)
950  return 0;
951 
952  return generate_matrix (types);
953  }
954 
955  return 0;
956 }
957 
958 llvm::Value *
960  const jit_function &fn, size_t start_idx,
961  size_t end_idx) const
962 {
963  size_t n = end_idx - start_idx;
964  llvm::Type *scalar_t = jit_typeinfo::get_scalar_llvm ();
965  llvm::ArrayType *array_t = llvm::ArrayType::get (scalar_t, n);
966  llvm::Value *array = llvm::UndefValue::get (array_t);
967  for (size_t i = start_idx; i < end_idx; ++i)
968  {
969  llvm::Value *idx = fn.argument (builder, i);
970  array = builder.CreateInsertValue (array, idx, i - start_idx);
971  }
972 
973  llvm::Value *array_mem = builder.CreateAlloca (array_t);
974  builder.CreateStore (array, array_mem);
975  return builder.CreateBitCast (array_mem, scalar_t->getPointerTo ());
976 }
977 
978 // -------------------- jit_paren_subsref --------------------
979 jit_function *
981 {
982  std::stringstream ss;
983  ss << "jit_paren_subsref_matrix_scalar" << (types.size () - 1);
984 
987  ss.str (), scalar, types);
988  fn->mark_can_error ();
989  llvm::BasicBlock *body = fn->new_block ();
990  llvm::IRBuilder<> builder (body);
991 
992  llvm::Value *array = create_arg_array (builder, *fn, 1, types.size ());
993  jit_type *index = jit_typeinfo::get_index ();
994  llvm::Value *nelem = llvm::ConstantInt::get (index->to_llvm (),
995  types.size () - 1);
996  llvm::Value *mat = fn->argument (builder, 0);
997  llvm::Value *ret = paren_scalar.call (builder, mat, array, nelem);
998  fn->do_return (builder, ret);
999  return fn;
1000 }
1001 
1002 void
1004 {
1005  std::vector<jit_type *> types (3);
1006  types[0] = jit_typeinfo::get_matrix ();
1007  types[1] = jit_typeinfo::get_scalar_ptr ();
1008  types[2] = jit_typeinfo::get_index ();
1009 
1012  "octave_jit_paren_scalar", scalar, types);
1015 }
1016 
1017 // -------------------- jit_paren_subsasgn --------------------
1018 jit_function *
1020 {
1021  std::stringstream ss;
1022  ss << "jit_paren_subsasgn_matrix_scalar" << (types.size () - 2);
1023 
1024  jit_type *matrix = jit_typeinfo::get_matrix ();
1026  ss.str (), matrix, types);
1027  fn->mark_can_error ();
1028  llvm::BasicBlock *body = fn->new_block ();
1029  llvm::IRBuilder<> builder (body);
1030 
1031  llvm::Value *array = create_arg_array (builder, *fn, 1, types.size () - 1);
1032  jit_type *index = jit_typeinfo::get_index ();
1033  llvm::Value *nelem = llvm::ConstantInt::get (index->to_llvm (),
1034  types.size () - 2);
1035 
1036  llvm::Value *mat = fn->argument (builder, 0);
1037  llvm::Value *value = fn->argument (builder, types.size () - 1);
1038  llvm::Value *ret = paren_scalar.call (builder, mat, array, nelem, value);
1039  fn->do_return (builder, ret);
1040  return fn;
1041 }
1042 
1043 void
1045 {
1046  if (paren_scalar.valid ())
1047  return;
1048 
1049  jit_type *matrix = jit_typeinfo::get_matrix ();
1050  std::vector<jit_type *> types (4);
1051  types[0] = matrix;
1052  types[1] = jit_typeinfo::get_scalar_ptr ();
1053  types[2] = jit_typeinfo::get_index ();
1054  types[3] = jit_typeinfo::get_scalar ();
1055 
1057  "octave_jit_paren_scalar", matrix, types);
1060 }
1061 
1062 // -------------------- jit_typeinfo --------------------
1063 void
1064 jit_typeinfo::initialize (llvm::Module *m, llvm::ExecutionEngine *e)
1065 {
1066  new jit_typeinfo (m, e);
1067 }
1068 
1069 // wrap function names to simplify jit_typeinfo::create_external
1070 #define JIT_FN(fn) engine, &fn, #fn
1071 
1072 jit_typeinfo::jit_typeinfo (llvm::Module *m, llvm::ExecutionEngine *e)
1073  : module (m), engine (e), next_id (0),
1074  builder (*new llvm::IRBuilderD (context))
1075 {
1076  instance = this;
1077 
1078  // FIXME: We should be registering types like in octave_value_typeinfo
1079  llvm::Type *any_t = llvm::StructType::create (context, "octave_base_value");
1080  any_t = any_t->getPointerTo ();
1081 
1082  llvm::Type *scalar_t = llvm::Type::getDoubleTy (context);
1083  llvm::Type *bool_t = llvm::Type::getInt1Ty (context);
1084  llvm::Type *string_t = llvm::Type::getInt8Ty (context);
1085  string_t = string_t->getPointerTo ();
1086  llvm::Type *index_t = llvm::Type::getIntNTy (context,
1087  sizeof(octave_idx_type) * 8);
1088 
1089  llvm::StructType *range_t = llvm::StructType::create (context, "range");
1090  std::vector<llvm::Type *> range_contents (4, scalar_t);
1091  range_contents[3] = index_t;
1092  range_t->setBody (range_contents);
1093 
1094  llvm::Type *refcount_t = llvm::Type::getIntNTy (context, sizeof(int) * 8);
1095 
1096  llvm::StructType *matrix_t = llvm::StructType::create (context, "matrix");
1097  llvm::Type *matrix_contents[5];
1098  matrix_contents[0] = refcount_t->getPointerTo ();
1099  matrix_contents[1] = scalar_t->getPointerTo ();
1100  matrix_contents[2] = index_t;
1101  matrix_contents[3] = index_t->getPointerTo ();
1102  matrix_contents[4] = string_t;
1103  matrix_t->setBody (llvm::makeArrayRef (matrix_contents, 5));
1104 
1105  llvm::Type *complex_t = llvm::ArrayType::get (scalar_t, 2);
1106 
1107  // complex_ret is what is passed to C functions in order to get calling
1108  // convention right
1109  llvm::Type *cmplx_inner_cont[] = {scalar_t, scalar_t};
1110  llvm::StructType *cmplx_inner = llvm::StructType::create (cmplx_inner_cont);
1111 
1112  complex_ret = llvm::StructType::create (context, "complex_ret");
1113  {
1114  llvm::Type *contents[] = {cmplx_inner};
1115  complex_ret->setBody (contents);
1116  }
1117 
1118  // create types
1119  any = new_type ("any", 0, any_t);
1120  matrix = new_type ("matrix", any, matrix_t);
1121  complex = new_type ("complex", any, complex_t);
1122  scalar = new_type ("scalar", complex, scalar_t);
1123  scalar_ptr = new_type ("scalar_ptr", 0, scalar_t->getPointerTo ());
1124  any_ptr = new_type ("any_ptr", 0, any_t->getPointerTo ());
1125  range = new_type ("range", any, range_t);
1126  string = new_type ("string", any, string_t);
1127  boolean = new_type ("bool", any, bool_t);
1128  index = new_type ("index", any, index_t);
1129 
1130  create_int (8);
1131  create_int (16);
1132  create_int (32);
1133  create_int (64);
1134 
1135  casts.resize (next_id + 1);
1136  identities.resize (next_id + 1);
1137 
1138  // specify calling conventions
1139  // FIXME: We should detect architecture and do something sane based on that
1140  // here we assume x86 or x86_64
1143 
1144  range->mark_sret (jit_convention::external);
1145  range->mark_pointer_arg (jit_convention::external);
1146 
1150 
1151  if (sizeof (void *) == 4)
1153 
1156 
1157  // bind global variables
1158  lerror_state = new llvm::GlobalVariable (*module, bool_t, false,
1159  llvm::GlobalValue::ExternalLinkage,
1160  0, "error_state");
1161  engine->addGlobalMapping (lerror_state,
1162  reinterpret_cast<void *> (&error_state));
1163 
1164  // sig_atomic_type is going to be some sort of integer
1165  sig_atomic_type = llvm::Type::getIntNTy (context, sizeof(sig_atomic_t) * 8);
1167  = new llvm::GlobalVariable (*module, sig_atomic_type, false,
1168  llvm::GlobalValue::ExternalLinkage, 0,
1169  "octave_interrupt_state");
1170  engine->addGlobalMapping (loctave_interrupt_state,
1171  reinterpret_cast<void *> (&octave_interrupt_state));
1172 
1173  // generic call function
1174  {
1175  jit_type *int_t = intN (sizeof (octave_builtin::fcn) * 8);
1176  any_call = create_external (JIT_FN (octave_jit_call), any, int_t, int_t,
1177  any_ptr, int_t);
1178  }
1179 
1180  // any with anything is an any op
1181  jit_function fn;
1182  jit_type *binary_op_type = intN (sizeof (octave_value::binary_op) * 8);
1183  llvm::Type *llvm_bo_type = binary_op_type->to_llvm ();
1185  any, binary_op_type, any, any);
1186  any_binary.mark_can_error ();
1188  for (size_t i = 0; i < octave_value::num_binary_ops; ++i)
1189  {
1190  octave_value::binary_op op = static_cast<octave_value::binary_op> (i);
1191  std::string op_name = octave_value::binary_op_as_string (op);
1192  binary_ops[i].stash_name ("binary" + op_name);
1193  }
1194 
1196  for (size_t i = 0; i < octave_value::num_unary_ops; ++i)
1197  {
1198  octave_value::unary_op op = static_cast<octave_value::unary_op> (i);
1199  std::string op_name = octave_value::unary_op_as_string (op);
1200  unary_ops[i].stash_name ("unary" + op_name);
1201  }
1202 
1203  for (int op = 0; op < octave_value::num_binary_ops; ++op)
1204  {
1205  llvm::Twine fn_name ("octave_jit_binary_any_any_");
1206  fn_name = fn_name + llvm::Twine (op);
1207 
1208  fn = create_internal (fn_name, any, any, any);
1209  fn.mark_can_error ();
1210  llvm::BasicBlock *block = fn.new_block ();
1211  builder.SetInsertPoint (block);
1212  llvm::APInt op_int(sizeof (octave_value::binary_op) * 8, op,
1213  std::numeric_limits<octave_value::binary_op>::is_signed);
1214  llvm::Value *op_as_llvm = llvm::ConstantInt::get (llvm_bo_type, op_int);
1215  llvm::Value *ret = any_binary.call (builder, op_as_llvm,
1216  fn.argument (builder, 0),
1217  fn.argument (builder, 1));
1218  fn.do_return (builder, ret);
1219  binary_ops[op].add_overload (fn);
1220  }
1221 
1222  // grab matrix
1224  grab_fn.add_overload (fn);
1225 
1228  grab_fn.add_overload (create_identity (any_ptr));
1229  grab_fn.add_overload (create_identity (boolean));
1232 
1233  // release any
1235  release_fn.add_overload (fn);
1236  release_fn.stash_name ("release");
1237 
1238  // release matrix
1240  release_fn.add_overload (fn);
1241 
1242  // destroy
1244  destroy_fn.stash_name ("destroy");
1249 
1250  // now for binary scalar operations
1251  add_binary_op (scalar, octave_value::op_add, llvm::Instruction::FAdd);
1252  add_binary_op (scalar, octave_value::op_sub, llvm::Instruction::FSub);
1253  add_binary_op (scalar, octave_value::op_mul, llvm::Instruction::FMul);
1254  add_binary_op (scalar, octave_value::op_el_mul, llvm::Instruction::FMul);
1255 
1256  add_binary_fcmp (scalar, octave_value::op_lt, llvm::CmpInst::FCMP_ULT);
1257  add_binary_fcmp (scalar, octave_value::op_le, llvm::CmpInst::FCMP_ULE);
1258  add_binary_fcmp (scalar, octave_value::op_eq, llvm::CmpInst::FCMP_UEQ);
1259  add_binary_fcmp (scalar, octave_value::op_ge, llvm::CmpInst::FCMP_UGE);
1260  add_binary_fcmp (scalar, octave_value::op_gt, llvm::CmpInst::FCMP_UGT);
1261  add_binary_fcmp (scalar, octave_value::op_ne, llvm::CmpInst::FCMP_UNE);
1262 
1264  gripe_div0.mark_can_error ();
1265 
1266  // divide is annoying because it might error
1267  fn = create_internal ("octave_jit_div_scalar_scalar", scalar, scalar, scalar);
1268  fn.mark_can_error ();
1269 
1270  llvm::BasicBlock *body = fn.new_block ();
1271  builder.SetInsertPoint (body);
1272  {
1273  llvm::BasicBlock *warn_block = fn.new_block ("warn");
1274  llvm::BasicBlock *normal_block = fn.new_block ("normal");
1275 
1276  llvm::Value *zero = llvm::ConstantFP::get (scalar_t, 0);
1277  llvm::Value *check = builder.CreateFCmpUEQ (zero, fn.argument (builder, 1));
1278  builder.CreateCondBr (check, warn_block, normal_block);
1279 
1280  builder.SetInsertPoint (warn_block);
1281  gripe_div0.call (builder);
1282  builder.CreateBr (normal_block);
1283 
1284  builder.SetInsertPoint (normal_block);
1285  llvm::Value *ret = builder.CreateFDiv (fn.argument (builder, 0),
1286  fn.argument (builder, 1));
1287  fn.do_return (builder, ret);
1288  }
1289  binary_ops[octave_value::op_div].add_overload (fn);
1290  binary_ops[octave_value::op_el_div].add_overload (fn);
1291 
1292  // ldiv is the same as div with the operators reversed
1293  fn = mirror_binary (fn);
1294  binary_ops[octave_value::op_ldiv].add_overload (fn);
1295  binary_ops[octave_value::op_el_ldiv].add_overload (fn);
1296 
1297  // In general, the result of scalar ^ scalar is a complex number. We might be
1298  // able to improve on this if we keep track of the range of values varaibles
1299  // can take on.
1301  scalar);
1302  binary_ops[octave_value::op_pow].add_overload (fn);
1303  binary_ops[octave_value::op_el_pow].add_overload (fn);
1304 
1305  // now for unary scalar operations
1306  // FIXME: Impelment not
1307  fn = create_internal ("octave_jit_++", scalar, scalar);
1308  body = fn.new_block ();
1309  builder.SetInsertPoint (body);
1310  {
1311  llvm::Value *one = llvm::ConstantFP::get (scalar_t, 1);
1312  llvm::Value *val = fn.argument (builder, 0);
1313  val = builder.CreateFAdd (val, one);
1314  fn.do_return (builder, val);
1315  }
1316  unary_ops[octave_value::op_incr].add_overload (fn);
1317 
1318  fn = create_internal ("octave_jit_--", scalar, scalar);
1319  body = fn.new_block ();
1320  builder.SetInsertPoint (body);
1321  {
1322  llvm::Value *one = llvm::ConstantFP::get (scalar_t, 1);
1323  llvm::Value *val = fn.argument (builder, 0);
1324  val = builder.CreateFSub (val, one);
1325  fn.do_return (builder, val);
1326  }
1327  unary_ops[octave_value::op_decr].add_overload (fn);
1328 
1329  fn = create_internal ("octave_jit_uminus", scalar, scalar);
1330  body = fn.new_block ();
1331  builder.SetInsertPoint (body);
1332  {
1333  llvm::Value *mone = llvm::ConstantFP::get (scalar_t, -1);
1334  llvm::Value *val = fn.argument (builder, 0);
1335  val = builder.CreateFMul (val, mone);
1336  fn.do_return (builder, val);
1337  }
1338 
1339  fn = create_identity (scalar);
1340  unary_ops[octave_value::op_uplus].add_overload (fn);
1341  unary_ops[octave_value::op_transpose].add_overload (fn);
1342  unary_ops[octave_value::op_hermitian].add_overload (fn);
1343 
1344  // now for binary complex operations
1345  fn = create_internal ("octave_jit_+_complex_complex", complex, complex,
1346  complex);
1347  body = fn.new_block ();
1348  builder.SetInsertPoint (body);
1349  {
1350  llvm::Value *lhs = fn.argument (builder, 0);
1351  llvm::Value *rhs = fn.argument (builder, 1);
1352  llvm::Value *real = builder.CreateFAdd (complex_real (lhs),
1353  complex_real (rhs));
1354  llvm::Value *imag = builder.CreateFAdd (complex_imag (lhs),
1355  complex_imag (rhs));
1356  fn.do_return (builder, complex_new (real, imag));
1357  }
1358  binary_ops[octave_value::op_add].add_overload (fn);
1359 
1360  fn = create_internal ("octave_jit_-_complex_complex", complex, complex,
1361  complex);
1362  body = fn.new_block ();
1363  builder.SetInsertPoint (body);
1364  {
1365  llvm::Value *lhs = fn.argument (builder, 0);
1366  llvm::Value *rhs = fn.argument (builder, 1);
1367  llvm::Value *real = builder.CreateFSub (complex_real (lhs),
1368  complex_real (rhs));
1369  llvm::Value *imag = builder.CreateFSub (complex_imag (lhs),
1370  complex_imag (rhs));
1371  fn.do_return (builder, complex_new (real, imag));
1372  }
1373  binary_ops[octave_value::op_sub].add_overload (fn);
1374 
1376  complex, complex, complex);
1377  binary_ops[octave_value::op_mul].add_overload (fn);
1378  binary_ops[octave_value::op_el_mul].add_overload (fn);
1379 
1381  complex, complex, complex);
1382  complex_div.mark_can_error ();
1383  binary_ops[octave_value::op_div].add_overload (fn);
1384  binary_ops[octave_value::op_ldiv].add_overload (fn);
1385 
1387  complex, complex);
1388  binary_ops[octave_value::op_pow].add_overload (fn);
1389  binary_ops[octave_value::op_el_pow].add_overload (fn);
1390 
1391  fn = create_internal ("octave_jit_*_scalar_complex", complex, scalar,
1392  complex);
1393  jit_function mul_scalar_complex = fn;
1394  body = fn.new_block ();
1395  builder.SetInsertPoint (body);
1396  {
1397  llvm::BasicBlock *complex_mul = fn.new_block ("complex_mul");
1398  llvm::BasicBlock *scalar_mul = fn.new_block ("scalar_mul");
1399 
1400  llvm::Value *fzero = llvm::ConstantFP::get (scalar_t, 0);
1401  llvm::Value *lhs = fn.argument (builder, 0);
1402  llvm::Value *rhs = fn.argument (builder, 1);
1403 
1404  llvm::Value *cmp = builder.CreateFCmpUEQ (complex_imag (rhs), fzero);
1405  builder.CreateCondBr (cmp, scalar_mul, complex_mul);
1406 
1407  builder.SetInsertPoint (scalar_mul);
1408  llvm::Value *temp = complex_real (rhs);
1409  temp = builder.CreateFMul (lhs, temp);
1410  fn.do_return (builder, complex_new (temp, fzero), false);
1411 
1412 
1413  builder.SetInsertPoint (complex_mul);
1414  temp = complex_new (builder.CreateFMul (lhs, complex_real (rhs)),
1415  builder.CreateFMul (lhs, complex_imag (rhs)));
1416  fn.do_return (builder, temp);
1417  }
1418  binary_ops[octave_value::op_mul].add_overload (fn);
1419  binary_ops[octave_value::op_el_mul].add_overload (fn);
1420 
1421 
1422  fn = mirror_binary (mul_scalar_complex);
1423  binary_ops[octave_value::op_mul].add_overload (fn);
1424  binary_ops[octave_value::op_el_mul].add_overload (fn);
1425 
1426  fn = create_internal ("octave_jit_+_scalar_complex", complex, scalar,
1427  complex);
1428  body = fn.new_block ();
1429  builder.SetInsertPoint (body);
1430  {
1431  llvm::Value *lhs = fn.argument (builder, 0);
1432  llvm::Value *rhs = fn.argument (builder, 1);
1433  llvm::Value *real = builder.CreateFAdd (lhs, complex_real (rhs));
1434  fn.do_return (builder, complex_real (rhs, real));
1435  }
1436  binary_ops[octave_value::op_add].add_overload (fn);
1437 
1438  fn = mirror_binary (fn);
1439  binary_ops[octave_value::op_add].add_overload (fn);
1440 
1441  fn = create_internal ("octave_jit_-_complex_scalar", complex, complex,
1442  scalar);
1443  body = fn.new_block ();
1444  builder.SetInsertPoint (body);
1445  {
1446  llvm::Value *lhs = fn.argument (builder, 0);
1447  llvm::Value *rhs = fn.argument (builder, 1);
1448  llvm::Value *real = builder.CreateFSub (complex_real (lhs), rhs);
1449  fn.do_return (builder, complex_real (lhs, real));
1450  }
1451  binary_ops[octave_value::op_sub].add_overload (fn);
1452 
1453  fn = create_internal ("octave_jit_-_scalar_complex", complex, scalar,
1454  complex);
1455  body = fn.new_block ();
1456  builder.SetInsertPoint (body);
1457  {
1458  llvm::Value *lhs = fn.argument (builder, 0);
1459  llvm::Value *rhs = fn.argument (builder, 1);
1460  llvm::Value *real = builder.CreateFSub (lhs, complex_real (rhs));
1461  fn.do_return (builder, complex_real (rhs, real));
1462  }
1463  binary_ops[octave_value::op_sub].add_overload (fn);
1464 
1466  complex);
1467  binary_ops[octave_value::op_pow].add_overload (fn);
1468  binary_ops[octave_value::op_el_pow].add_overload (fn);
1469 
1471  complex, scalar);
1472  binary_ops[octave_value::op_pow].add_overload (fn);
1473  binary_ops[octave_value::op_el_pow].add_overload (fn);
1474 
1475  // now for binary index operators
1476  add_binary_op (index, octave_value::op_add, llvm::Instruction::Add);
1477 
1478  // and binary bool operators
1479  add_binary_op (boolean, octave_value::op_el_or, llvm::Instruction::Or);
1480  add_binary_op (boolean, octave_value::op_el_and, llvm::Instruction::And);
1481 
1482  // now for printing functions
1483  print_fn.stash_name ("print");
1484  add_print (any, reinterpret_cast<void *> (&octave_jit_print_any));
1485  add_print (scalar, reinterpret_cast<void *> (&octave_jit_print_scalar));
1486 
1487  // initialize for loop
1488  for_init_fn.stash_name ("for_init");
1489 
1490  fn = create_internal ("octave_jit_for_range_init", index, range);
1491  body = fn.new_block ();
1492  builder.SetInsertPoint (body);
1493  {
1494  llvm::Value *zero = llvm::ConstantInt::get (index_t, 0);
1495  fn.do_return (builder, zero);
1496  }
1498 
1499  // bounds check for for loop
1500  for_check_fn.stash_name ("for_check");
1501 
1502  fn = create_internal ("octave_jit_for_range_check", boolean, range, index);
1503  body = fn.new_block ();
1504  builder.SetInsertPoint (body);
1505  {
1506  llvm::Value *nelem
1507  = builder.CreateExtractValue (fn.argument (builder, 0), 3);
1508  llvm::Value *idx = fn.argument (builder, 1);
1509  llvm::Value *ret = builder.CreateICmpULT (idx, nelem);
1510  fn.do_return (builder, ret);
1511  }
1513 
1514  // index variabe for for loop
1515  for_index_fn.stash_name ("for_index");
1516 
1517  fn = create_internal ("octave_jit_for_range_idx", scalar, range, index);
1518  body = fn.new_block ();
1519  builder.SetInsertPoint (body);
1520  {
1521  llvm::Value *idx = fn.argument (builder, 1);
1522  llvm::Value *didx = builder.CreateSIToFP (idx, scalar_t);
1523  llvm::Value *rng = fn.argument (builder, 0);
1524  llvm::Value *base = builder.CreateExtractValue (rng, 0);
1525  llvm::Value *inc = builder.CreateExtractValue (rng, 2);
1526 
1527  llvm::Value *ret = builder.CreateFMul (didx, inc);
1528  ret = builder.CreateFAdd (base, ret);
1529  fn.do_return (builder, ret);
1530  }
1532 
1533  // logically true
1534  logically_true_fn.stash_name ("logically_true");
1535 
1536  jit_function gripe_nantl
1538  gripe_nantl.mark_can_error ();
1539 
1540  fn = create_internal ("octave_jit_logically_true_scalar", boolean, scalar);
1541  fn.mark_can_error ();
1542 
1543  body = fn.new_block ();
1544  builder.SetInsertPoint (body);
1545  {
1546  llvm::BasicBlock *error_block = fn.new_block ("error");
1547  llvm::BasicBlock *normal_block = fn.new_block ("normal");
1548 
1549  llvm::Value *check = builder.CreateFCmpUNE (fn.argument (builder, 0),
1550  fn.argument (builder, 0));
1551  builder.CreateCondBr (check, error_block, normal_block);
1552 
1553  builder.SetInsertPoint (error_block);
1554  gripe_nantl.call (builder);
1555  builder.CreateBr (normal_block);
1556  builder.SetInsertPoint (normal_block);
1557 
1558  llvm::Value *zero = llvm::ConstantFP::get (scalar_t, 0);
1559  llvm::Value *ret = builder.CreateFCmpONE (fn.argument (builder, 0), zero);
1560  fn.do_return (builder, ret);
1561  }
1563 
1564  // logically_true boolean
1565  fn = create_identity (boolean);
1567 
1568  // make_range
1569  // FIXME: May be benificial to implement all in LLVM
1570  make_range_fn.stash_name ("make_range");
1571  jit_function compute_nelem
1573  index, scalar, scalar, scalar);
1574 
1575 
1576  fn = create_internal ("octave_jit_make_range", range, scalar, scalar, scalar);
1577  body = fn.new_block ();
1578  builder.SetInsertPoint (body);
1579  {
1580  llvm::Value *base = fn.argument (builder, 0);
1581  llvm::Value *limit = fn.argument (builder, 1);
1582  llvm::Value *inc = fn.argument (builder, 2);
1583  llvm::Value *nelem = compute_nelem.call (builder, base, limit, inc);
1584 
1585  llvm::Value *dzero = llvm::ConstantFP::get (scalar_t, 0);
1586  llvm::Value *izero = llvm::ConstantInt::get (index_t, 0);
1587  llvm::Value *rng = llvm::ConstantStruct::get (range_t, dzero, dzero, dzero,
1588  izero, NULL);
1589  rng = builder.CreateInsertValue (rng, base, 0);
1590  rng = builder.CreateInsertValue (rng, limit, 1);
1591  rng = builder.CreateInsertValue (rng, inc, 2);
1592  rng = builder.CreateInsertValue (rng, nelem, 3);
1593  fn.do_return (builder, rng);
1594  }
1596 
1597  // paren_subsref
1598  jit_type *jit_int = intN (sizeof (int) * 8);
1599  llvm::Type *int_t = jit_int->to_llvm ();
1600  jit_function ginvalid_index
1603  0, jit_int, jit_int, index,
1604  index);
1605 
1606  fn = create_internal ("()subsref", scalar, matrix, scalar);
1607  fn.mark_can_error ();
1608 
1609  body = fn.new_block ();
1610  builder.SetInsertPoint (body);
1611  {
1612  llvm::Value *one = llvm::ConstantInt::get (index_t, 1);
1613  llvm::Value *ione;
1614  if (index_t == int_t)
1615  ione = one;
1616  else
1617  ione = llvm::ConstantInt::get (int_t, 1);
1618 
1619  llvm::Value *undef = llvm::UndefValue::get (scalar_t);
1620  llvm::Value *mat = fn.argument (builder, 0);
1621  llvm::Value *idx = fn.argument (builder, 1);
1622 
1623  // convert index to scalar to integer, and check index >= 1
1624  llvm::Value *int_idx = builder.CreateFPToSI (idx, index_t);
1625  llvm::Value *check_idx = builder.CreateSIToFP (int_idx, scalar_t);
1626  llvm::Value *cond0 = builder.CreateFCmpUNE (idx, check_idx);
1627  llvm::Value *cond1 = builder.CreateICmpSLT (int_idx, one);
1628  llvm::Value *cond = builder.CreateOr (cond0, cond1);
1629 
1630  llvm::BasicBlock *done = fn.new_block ("done");
1631  llvm::BasicBlock *conv_error = fn.new_block ("conv_error", done);
1632  llvm::BasicBlock *normal = fn.new_block ("normal", done);
1633  builder.CreateCondBr (cond, conv_error, normal);
1634 
1635  builder.SetInsertPoint (conv_error);
1636  ginvalid_index.call (builder);
1637  builder.CreateBr (done);
1638 
1639  builder.SetInsertPoint (normal);
1640  llvm::Value *len
1641  = builder.CreateExtractValue (mat, llvm::ArrayRef<unsigned> (2));
1642  cond = builder.CreateICmpSGT (int_idx, len);
1643 
1644 
1645  llvm::BasicBlock *bounds_error = fn.new_block ("bounds_error", done);
1646  llvm::BasicBlock *success = fn.new_block ("success", done);
1647  builder.CreateCondBr (cond, bounds_error, success);
1648 
1649  builder.SetInsertPoint (bounds_error);
1650  gindex_range.call (builder, ione, ione, int_idx, len);
1651  builder.CreateBr (done);
1652 
1653  builder.SetInsertPoint (success);
1654  llvm::Value *data = builder.CreateExtractValue (mat,
1655  llvm::ArrayRef<unsigned> (1));
1656  llvm::Value *gep = builder.CreateInBoundsGEP (data, int_idx);
1657  llvm::Value *ret = builder.CreateLoad (gep);
1658  builder.CreateBr (done);
1659 
1660  builder.SetInsertPoint (done);
1661 
1662  llvm::PHINode *merge = llvm::PHINode::Create (scalar_t, 3);
1663  builder.Insert (merge);
1664  merge->addIncoming (undef, conv_error);
1665  merge->addIncoming (undef, bounds_error);
1666  merge->addIncoming (ret, success);
1667  fn.do_return (builder, merge);
1668  }
1670 
1671  // paren subsasgn
1672  paren_subsasgn_fn.stash_name ("()subsasgn");
1673 
1674  jit_function resize_paren_subsasgn
1676  index, scalar);
1677 
1678  fn = create_internal ("octave_jit_paren_subsasgn", matrix, matrix, scalar,
1679  scalar);
1680  fn.mark_can_error ();
1681  body = fn.new_block ();
1682  builder.SetInsertPoint (body);
1683  {
1684  llvm::Value *one = llvm::ConstantInt::get (index_t, 1);
1685 
1686  llvm::Value *mat = fn.argument (builder, 0);
1687  llvm::Value *idx = fn.argument (builder, 1);
1688  llvm::Value *value = fn.argument (builder, 2);
1689 
1690  llvm::Value *int_idx = builder.CreateFPToSI (idx, index_t);
1691  llvm::Value *check_idx = builder.CreateSIToFP (int_idx, scalar_t);
1692  llvm::Value *cond0 = builder.CreateFCmpUNE (idx, check_idx);
1693  llvm::Value *cond1 = builder.CreateICmpSLT (int_idx, one);
1694  llvm::Value *cond = builder.CreateOr (cond0, cond1);
1695 
1696  llvm::BasicBlock *done = fn.new_block ("done");
1697 
1698  llvm::BasicBlock *conv_error = fn.new_block ("conv_error", done);
1699  llvm::BasicBlock *normal = fn.new_block ("normal", done);
1700  builder.CreateCondBr (cond, conv_error, normal);
1701  builder.SetInsertPoint (conv_error);
1702  ginvalid_index.call (builder);
1703  builder.CreateBr (done);
1704 
1705  builder.SetInsertPoint (normal);
1706  llvm::Value *len = builder.CreateExtractValue (mat, 2);
1707  cond0 = builder.CreateICmpSGT (int_idx, len);
1708 
1709  llvm::Value *rcount = builder.CreateExtractValue (mat, 0);
1710  rcount = builder.CreateLoad (rcount);
1711  cond1 = builder.CreateICmpSGT (rcount, one);
1712  cond = builder.CreateOr (cond0, cond1);
1713 
1714  llvm::BasicBlock *bounds_error = fn.new_block ("bounds_error", done);
1715  llvm::BasicBlock *success = fn.new_block ("success", done);
1716  builder.CreateCondBr (cond, bounds_error, success);
1717 
1718  // resize on out of bounds access
1719  builder.SetInsertPoint (bounds_error);
1720  llvm::Value *resize_result = resize_paren_subsasgn.call (builder, mat,
1721  int_idx, value);
1722  builder.CreateBr (done);
1723 
1724  builder.SetInsertPoint (success);
1725  llvm::Value *data
1726  = builder.CreateExtractValue (mat, llvm::ArrayRef<unsigned> (1));
1727  llvm::Value *gep = builder.CreateInBoundsGEP (data, int_idx);
1728  builder.CreateStore (value, gep);
1729  builder.CreateBr (done);
1730 
1731  builder.SetInsertPoint (done);
1732 
1733  llvm::PHINode *merge = llvm::PHINode::Create (matrix_t, 3);
1734  builder.Insert (merge);
1735  merge->addIncoming (mat, conv_error);
1736  merge->addIncoming (resize_result, bounds_error);
1737  merge->addIncoming (mat, success);
1738  fn.do_return (builder, merge);
1739  }
1741 
1743  matrix, range, scalar);
1744  fn.mark_can_error ();
1746 
1747  end1_fn.stash_name ("end1");
1748  fn = create_internal ("octave_jit_end1_matrix", scalar, matrix, index, index);
1749  body = fn.new_block ();
1750  builder.SetInsertPoint (body);
1751  {
1752  llvm::Value *mat = fn.argument (builder, 0);
1753  llvm::Value *ret = builder.CreateExtractValue (mat, 2);
1754  fn.do_return (builder, builder.CreateSIToFP (ret, scalar_t));
1755  }
1756  end1_fn.add_overload (fn);
1757 
1758  end_fn.stash_name ("end");
1760  index);
1761  end_fn.add_overload (fn);
1762 
1763  // -------------------- create_undef --------------------
1764  create_undef_fn.stash_name ("create_undef");
1767 
1768  casts[any->type_id ()].stash_name ("(any)");
1769  casts[scalar->type_id ()].stash_name ("(scalar)");
1770  casts[complex->type_id ()].stash_name ("(complex)");
1771  casts[matrix->type_id ()].stash_name ("(matrix)");
1772  casts[range->type_id ()].stash_name ("(range)");
1773 
1774  // cast any <- matrix
1776  casts[any->type_id ()].add_overload (fn);
1777 
1778  // cast matrix <- any
1780  casts[matrix->type_id ()].add_overload (fn);
1781 
1782  // cast any <- range
1784  casts[any->type_id ()].add_overload (fn);
1785 
1786  // cast range <- any
1788  casts[range->type_id ()].add_overload (fn);
1789 
1790  // cast any <- scalar
1792  casts[any->type_id ()].add_overload (fn);
1793 
1794  // cast scalar <- any
1796  casts[scalar->type_id ()].add_overload (fn);
1797 
1798  // cast any <- complex
1800  casts[any->type_id ()].add_overload (fn);
1801 
1802  // cast complex <- any
1804  casts[complex->type_id ()].add_overload (fn);
1805 
1806  // cast complex <- scalar
1807  fn = create_internal ("octave_jit_cast_complex_scalar", complex, scalar);
1808  body = fn.new_block ();
1809  builder.SetInsertPoint (body);
1810  {
1811  llvm::Value *zero = llvm::ConstantFP::get (scalar_t, 0);
1812  fn.do_return (builder, complex_new (fn.argument (builder, 0), zero));
1813  }
1814  casts[complex->type_id ()].add_overload (fn);
1815 
1816  // cast scalar <- complex
1817  fn = create_internal ("octave_jit_cast_scalar_complex", scalar, complex);
1818  body = fn.new_block ();
1819  builder.SetInsertPoint (body);
1820  fn.do_return (builder, complex_real (fn.argument (builder, 0)));
1821  casts[scalar->type_id ()].add_overload (fn);
1822 
1823  // cast any <- any
1824  fn = create_identity (any);
1825  casts[any->type_id ()].add_overload (fn);
1826 
1827  // cast scalar <- scalar
1828  fn = create_identity (scalar);
1829  casts[scalar->type_id ()].add_overload (fn);
1830 
1831  // cast complex <- complex
1832  fn = create_identity (complex);
1833  casts[complex->type_id ()].add_overload (fn);
1834 
1835  // -------------------- builtin functions --------------------
1836  add_builtin ("#unknown_function");
1837  unknown_function = builtins["#unknown_function"];
1838 
1839  add_builtin ("sin");
1840  register_intrinsic ("sin", llvm::Intrinsic::sin, scalar, scalar);
1841  register_generic ("sin", matrix, matrix);
1842 
1843  add_builtin ("cos");
1844  register_intrinsic ("cos", llvm::Intrinsic::cos, scalar, scalar);
1845  register_generic ("cos", matrix, matrix);
1846 
1847  add_builtin ("exp");
1848  register_intrinsic ("exp", llvm::Intrinsic::cos, scalar, scalar);
1849  register_generic ("exp", matrix, matrix);
1850 
1851  add_builtin ("balance");
1852  register_generic ("balance", matrix, matrix);
1853 
1854  add_builtin ("cond");
1855  register_generic ("cond", scalar, matrix);
1856 
1857  add_builtin ("det");
1858  register_generic ("det", scalar, matrix);
1859 
1860  add_builtin ("norm");
1861  register_generic ("norm", scalar, matrix);
1862 
1863  add_builtin ("rand");
1864  register_generic ("rand", matrix, scalar);
1865  register_generic ("rand", matrix, std::vector<jit_type *> (2, scalar));
1866 
1867  add_builtin ("magic");
1868  register_generic ("magic", matrix, scalar);
1869  register_generic ("magic", matrix, std::vector<jit_type *> (2, scalar));
1870 
1871  add_builtin ("eye");
1872  register_generic ("eye", matrix, scalar);
1873  register_generic ("eye", matrix, std::vector<jit_type *> (2, scalar));
1874 
1875  add_builtin ("mod");
1876  register_generic ("mod", scalar, std::vector<jit_type *> (2, scalar));
1877 
1878  casts.resize (next_id + 1);
1879  jit_function any_id = create_identity (any);
1881  any, any);
1882  jit_function release_any = get_release (any);
1883  std::vector<jit_type *> args;
1884  args.resize (1);
1885 
1886  for (std::map<std::string, jit_type *>::iterator iter = builtins.begin ();
1887  iter != builtins.end (); ++iter)
1888  {
1889  jit_type *btype = iter->second;
1890  args[0] = btype;
1891 
1892  grab_fn.add_overload (jit_function (grab_any, btype, args));
1893  release_fn.add_overload (jit_function (release_any, 0, args));
1894  casts[any->type_id ()].add_overload (jit_function (any_id, any, args));
1895 
1896  args[0] = any;
1897  casts[btype->type_id ()].add_overload (jit_function (any_id, btype,
1898  args));
1899  }
1900 }
1901 
1902 const jit_function&
1904 {
1905  jit_const_index *ccount = dynamic_cast<jit_const_index *> (count);
1906  if (ccount && ccount->value () == 1)
1907  return end1_fn.overload (value->type (), idx->type (), count->type ());
1908 
1909  return end_fn.overload (value->type (), idx->type (), count->type ());
1910 }
1911 
1912 jit_type*
1913 jit_typeinfo::new_type (const std::string& name, jit_type *parent,
1914  llvm::Type *llvm_type, bool skip_paren)
1915 {
1916  jit_type *ret = new jit_type (name, parent, llvm_type, skip_paren, next_id++);
1917  id_to_type.push_back (ret);
1918  return ret;
1919 }
1920 
1921 void
1923 {
1924  std::stringstream name;
1925  name << "octave_jit_print_" << ty->name ();
1926  jit_function fn = create_external (engine, fptr, name.str (),
1927  0, intN (8), ty);
1928  print_fn.add_overload (fn);
1929 }
1930 
1931 // FIXME: cp between add_binary_op, add_binary_icmp, and add_binary_fcmp
1932 void
1933 jit_typeinfo::add_binary_op (jit_type *ty, int op, int llvm_op)
1934 {
1935  std::stringstream fname;
1936  octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op);
1937  fname << "octave_jit_" << octave_value::binary_op_as_string (ov_op)
1938  << "_" << ty->name ();
1939 
1940  jit_function fn = create_internal (fname.str (), ty, ty, ty);
1941  llvm::BasicBlock *block = fn.new_block ();
1942  builder.SetInsertPoint (block);
1943  llvm::Instruction::BinaryOps temp
1944  = static_cast<llvm::Instruction::BinaryOps>(llvm_op);
1945 
1946  llvm::Value *ret = builder.CreateBinOp (temp, fn.argument (builder, 0),
1947  fn.argument (builder, 1));
1948  fn.do_return (builder, ret);
1949  binary_ops[op].add_overload (fn);
1950 }
1951 
1952 void
1954 {
1955  std::stringstream fname;
1956  octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op);
1957  fname << "octave_jit" << octave_value::binary_op_as_string (ov_op)
1958  << "_" << ty->name ();
1959 
1960  jit_function fn = create_internal (fname.str (), boolean, ty, ty);
1961  llvm::BasicBlock *block = fn.new_block ();
1962  builder.SetInsertPoint (block);
1963  llvm::CmpInst::Predicate temp
1964  = static_cast<llvm::CmpInst::Predicate>(llvm_op);
1965  llvm::Value *ret = builder.CreateICmp (temp, fn.argument (builder, 0),
1966  fn.argument (builder, 1));
1967  fn.do_return (builder, ret);
1968  binary_ops[op].add_overload (fn);
1969 }
1970 
1971 void
1973 {
1974  std::stringstream fname;
1975  octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op);
1976  fname << "octave_jit" << octave_value::binary_op_as_string (ov_op)
1977  << "_" << ty->name ();
1978 
1979  jit_function fn = create_internal (fname.str (), boolean, ty, ty);
1980  llvm::BasicBlock *block = fn.new_block ();
1981  builder.SetInsertPoint (block);
1982  llvm::CmpInst::Predicate temp
1983  = static_cast<llvm::CmpInst::Predicate>(llvm_op);
1984  llvm::Value *ret = builder.CreateFCmp (temp, fn.argument (builder, 0),
1985  fn.argument (builder, 1));
1986  fn.do_return (builder, ret);
1987  binary_ops[op].add_overload (fn);
1988 }
1989 
1992  jit_type *ret,
1993  const std::vector<jit_type *>& args)
1994 {
1995  jit_function result (module, cc, name, ret, args);
1996  return result;
1997 }
1998 
2001 {
2002  size_t id = type->type_id ();
2003  if (id >= identities.size ())
2004  identities.resize (id + 1);
2005 
2006  if (! identities[id].valid ())
2007  {
2008  std::stringstream name;
2009  name << "id_" << type->name ();
2010 
2011  jit_function fn = create_internal (name.str (), type, type);
2012  llvm::BasicBlock *body = fn.new_block ();
2013  builder.SetInsertPoint (body);
2014  fn.do_return (builder, fn.argument (builder, 0));
2015  return identities[id] = fn;
2016  }
2017 
2018  return identities[id];
2019 }
2020 
2021 llvm::Value *
2023 {
2024  return abuilder.CreateLoad (lerror_state);
2025 }
2026 
2027 llvm::Value *
2029 {
2030  llvm::LoadInst *val = abuilder.CreateLoad (loctave_interrupt_state);
2031  val->setVolatile (true);
2032  return abuilder.CreateICmpSGT (val, abuilder.getInt32 (0));
2033 }
2034 
2035 void
2036 jit_typeinfo::add_builtin (const std::string& name)
2037 {
2038  jit_type *btype = new_type (name, any, any->to_llvm (), true);
2039  builtins[name] = btype;
2040 
2041  octave_builtin *ov_builtin = find_builtin (name);
2042  if (ov_builtin)
2043  ov_builtin->stash_jit (*btype);
2044 }
2045 
2046 void
2047 jit_typeinfo::register_intrinsic (const std::string& name, size_t iid,
2048  jit_type *result,
2049  const std::vector<jit_type *>& args)
2050 {
2051  jit_type *builtin_type = builtins[name];
2052  size_t nargs = args.size ();
2053  llvm::SmallVector<llvm::Type *, 5> llvm_args (nargs);
2054  for (size_t i = 0; i < nargs; ++i)
2055  llvm_args[i] = args[i]->to_llvm ();
2056 
2057  llvm::Intrinsic::ID id = static_cast<llvm::Intrinsic::ID> (iid);
2058  llvm::Function *ifun = llvm::Intrinsic::getDeclaration (module, id,
2059  llvm_args);
2060  std::stringstream fn_name;
2061  fn_name << "octave_jit_" << name;
2062 
2063  std::vector<jit_type *> args1 (nargs + 1);
2064  args1[0] = builtin_type;
2065  std::copy (args.begin (), args.end (), args1.begin () + 1);
2066 
2067  // The first argument will be the Octave function, but we already know that
2068  // the function call is the equivalent of the intrinsic, so we ignore it and
2069  // call the intrinsic with the remaining arguments.
2070  jit_function fn = create_internal (fn_name.str (), result, args1);
2071  llvm::BasicBlock *body = fn.new_block ();
2072  builder.SetInsertPoint (body);
2073 
2074  llvm::SmallVector<llvm::Value *, 5> fargs (nargs);
2075  for (size_t i = 0; i < nargs; ++i)
2076  fargs[i] = fn.argument (builder, i + 1);
2077 
2078  llvm::Value *ret = builder.CreateCall (ifun, fargs);
2079  fn.do_return (builder, ret);
2081 }
2082 
2084 jit_typeinfo::find_builtin (const std::string& name)
2085 {
2086  // FIXME: Finalize what we want to store in octave_builtin, then add functions
2087  // to access these values in octave_value
2088  octave_value ov_builtin = symbol_table::find (name);
2089  return dynamic_cast<octave_builtin *> (ov_builtin.internal_rep ());
2090 }
2091 
2092 void
2093 jit_typeinfo::register_generic (const std::string& name, jit_type *result,
2094  const std::vector<jit_type *>& args)
2095 {
2096  octave_builtin *builtin = find_builtin (name);
2097  if (! builtin)
2098  return;
2099 
2100  std::vector<jit_type *> fn_args (args.size () + 1);
2101  fn_args[0] = builtins[name];
2102  std::copy (args.begin (), args.end (), fn_args.begin () + 1);
2103  jit_function fn = create_internal (name, result, fn_args);
2104  fn.mark_can_error ();
2105  llvm::BasicBlock *block = fn.new_block ();
2106  builder.SetInsertPoint (block);
2107  llvm::Type *any_t = any->to_llvm ();
2108  llvm::ArrayType *array_t = llvm::ArrayType::get (any_t, args.size ());
2109  llvm::Value *array = llvm::UndefValue::get (array_t);
2110  for (size_t i = 0; i < args.size (); ++i)
2111  {
2112  llvm::Value *arg = fn.argument (builder, i + 1);
2113  jit_function agrab = get_grab (args[i]);
2114  if (agrab.valid ())
2115  arg = agrab.call (builder, arg);
2116  jit_function acast = cast (any, args[i]);
2117  array = builder.CreateInsertValue (array, acast.call (builder, arg), i);
2118  }
2119 
2120  llvm::Value *array_mem = builder.CreateAlloca (array_t);
2121  builder.CreateStore (array, array_mem);
2122  array = builder.CreateBitCast (array_mem, any_t->getPointerTo ());
2123 
2124  jit_type *jintTy = intN (sizeof (octave_builtin::fcn) * 8);
2125  llvm::Type *intTy = jintTy->to_llvm ();
2126  size_t fcn_int = reinterpret_cast<size_t> (builtin->function ());
2127  llvm::Value *fcn = llvm::ConstantInt::get (intTy, fcn_int);
2128  llvm::Value *nargin = llvm::ConstantInt::get (intTy, args.size ());
2129  size_t result_int = reinterpret_cast<size_t> (result);
2130  llvm::Value *res_llvm = llvm::ConstantInt::get (intTy, result_int);
2131  llvm::Value *ret = any_call.call (builder, fcn, nargin, array, res_llvm);
2132 
2133  jit_function cast_result = cast (result, any);
2134  fn.do_return (builder, cast_result.call (builder, ret));
2136 }
2137 
2140 {
2141  jit_function ret = create_internal (fn.name () + "_reverse",
2142  fn.result (), fn.argument_type (1),
2143  fn.argument_type (0));
2144  if (fn.can_error ())
2145  ret.mark_can_error ();
2146 
2147  llvm::BasicBlock *body = ret.new_block ();
2148  builder.SetInsertPoint (body);
2149  llvm::Value *result = fn.call (builder, ret.argument (builder, 1),
2150  ret.argument (builder, 0));
2151  if (ret.result ())
2152  ret.do_return (builder, result);
2153  else
2154  ret.do_return (builder);
2155 
2156  return ret;
2157 }
2158 
2159 llvm::Value *
2161 {
2162  llvm::Type *complex_ret = instance->complex_ret;
2163  llvm::Value *real = bld.CreateExtractValue (cplx, 0);
2164  llvm::Value *imag = bld.CreateExtractValue (cplx, 1);
2165  llvm::Value *ret = llvm::UndefValue::get (complex_ret);
2166 
2167  unsigned int re_idx[] = {0, 0};
2168  unsigned int im_idx[] = {0, 1};
2169  ret = bld.CreateInsertValue (ret, real, re_idx);
2170  return bld.CreateInsertValue (ret, imag, im_idx);
2171 }
2172 
2173 llvm::Value *
2175 {
2176  unsigned int re_idx[] = {0, 0};
2177  unsigned int im_idx[] = {0, 1};
2178 
2179  llvm::Type *complex_t = get_complex ()->to_llvm ();
2180  llvm::Value *real = bld.CreateExtractValue (result, re_idx);
2181  llvm::Value *imag = bld.CreateExtractValue (result, im_idx);
2182  llvm::Value *ret = llvm::UndefValue::get (complex_t);
2183 
2184  ret = bld.CreateInsertValue (ret, real, 0);
2185  return bld.CreateInsertValue (ret, imag, 1);
2186 }
2187 
2188 llvm::Value *
2190 {
2191  return builder.CreateExtractValue (cx, 0);
2192 }
2193 
2194 llvm::Value *
2195 jit_typeinfo::complex_real (llvm::Value *cx, llvm::Value *real)
2196 {
2197  return builder.CreateInsertValue (cx, real, 0);
2198 }
2199 
2200 llvm::Value *
2202 {
2203  return builder.CreateExtractValue (cx, 1);
2204 }
2205 
2206 llvm::Value *
2207 jit_typeinfo::complex_imag (llvm::Value *cx, llvm::Value *imag)
2208 {
2209  return builder.CreateInsertValue (cx, imag, 1);
2210 }
2211 
2212 llvm::Value *
2213 jit_typeinfo::complex_new (llvm::Value *real, llvm::Value *imag)
2214 {
2215  llvm::Value *ret = llvm::UndefValue::get (complex->to_llvm ());
2216  ret = complex_real (ret, real);
2217  return complex_imag (ret, imag);
2218 }
2219 
2220 void
2222 {
2223  std::stringstream tname;
2224  tname << "int" << nbits;
2225  ints[nbits] = new_type (tname.str (), any, llvm::Type::getIntNTy (context,
2226  nbits));
2227 }
2228 
2229 jit_type *
2230 jit_typeinfo::intN (size_t nbits) const
2231 {
2232  std::map<size_t, jit_type *>::const_iterator iter = ints.find (nbits);
2233  if (iter != ints.end ())
2234  return iter->second;
2235 
2236  throw jit_fail_exception ("No such integer type");
2237 }
2238 
2239 jit_type *
2241 {
2242  if (ov.is_function ())
2243  {
2244  // FIXME: This is ugly, we need to finalize how we want to to this, then
2245  // have octave_value fully support the needed functionality
2246  octave_builtin *builtin
2247  = dynamic_cast<octave_builtin *> (ov.internal_rep ());
2248  return builtin && builtin->to_jit () ? builtin->to_jit ()
2249  : unknown_function;
2250  }
2251 
2252  if (ov.is_range ())
2253  return get_range ();
2254 
2255  if (ov.is_double_type () && ! ov.is_complex_type ())
2256  {
2257  if (ov.is_real_scalar ())
2258  return get_scalar ();
2259 
2260  if (ov.is_matrix_type ())
2261  return get_matrix ();
2262  }
2263 
2264  if (ov.is_complex_scalar ())
2265  {
2266  Complex cv = ov.complex_value ();
2267 
2268  // We don't really represent complex values, instead we represent
2269  // complex_or_scalar. If the imag value is zero, we assume a scalar.
2270  if (cv.imag () != 0)
2271  return get_complex ();
2272  }
2273 
2274  return get_any ();
2275 }
2276 
2277 #endif