* xvasprintf.c: New file.
[official-gcc.git] / gcc / jit / libgccjit++.h
blobbaed99ef1d1bc3bc98b3f1f3065359918b46561d
1 /* A C++ API for libgccjit, purely as inline wrapper functions.
2 Copyright (C) 2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef LIBGCCJIT_PLUS_PLUS_H
21 #define LIBGCCJIT_PLUS_PLUS_H
23 #include "libgccjit.h"
25 #include <limits>
26 #include <ostream>
27 #include <vector>
29 /****************************************************************************
30 C++ API
31 ****************************************************************************/
33 namespace gccjit
35 /* Indentation indicates inheritance. */
36 class context;
37 class error;
38 class object;
39 class location;
40 class field;
41 class type;
42 class struct_;
43 class function;
44 class block;
45 class rvalue;
46 class lvalue;
47 class param;
49 /* Errors within the API become C++ exceptions of this class. */
50 class error
54 class object
56 public:
57 context get_context () const;
59 std::string get_debug_string () const;
61 protected:
62 object ();
63 object (gcc_jit_object *obj);
65 gcc_jit_object *get_inner_object () const;
67 private:
68 gcc_jit_object *m_inner_obj;
71 inline std::ostream& operator << (std::ostream& stream, const object &obj);
73 /* Some client code will want to supply source code locations, others
74 won't. To avoid doubling the number of entrypoints, everything
75 accepting a location also has a default argument. To do this, the
76 other classes need to see that "location" has a default constructor,
77 hence we need to declare it first. */
78 class location : public object
80 public:
81 location ();
82 location (gcc_jit_location *loc);
84 gcc_jit_location *get_inner_location () const;
87 class context
89 public:
90 static context acquire ();
91 context ();
92 context (gcc_jit_context *ctxt);
94 gccjit::context new_child_context ();
96 gcc_jit_context *get_inner_context () { return m_inner_ctxt; }
98 void release ();
100 gcc_jit_result *compile ();
102 void dump_to_file (const std::string &path,
103 bool update_locations);
105 void set_str_option (enum gcc_jit_str_option opt,
106 const char *value);
108 void set_int_option (enum gcc_jit_int_option opt,
109 int value);
111 void set_bool_option (enum gcc_jit_bool_option opt,
112 int value);
114 location
115 new_location (const std::string &filename,
116 int line,
117 int column);
119 type get_type (enum gcc_jit_types kind);
120 type get_int_type (size_t num_bytes, int is_signed);
122 /* A way to map a specific int type, using the compiler to
123 get the details automatically e.g.:
124 gccjit::type type = get_int_type <my_int_type_t> (); */
125 template <typename T>
126 type get_int_type ();
128 type new_array_type (type element_type, int num_elements,
129 location loc = location ());
131 field new_field (type type_, const std::string &name,
132 location loc = location ());
134 struct_ new_struct_type (const std::string &name,
135 std::vector<field> &fields,
136 location loc = location ());
138 struct_ new_opaque_struct_type (const std::string &name,
139 location loc = location ());
141 param new_param (type type_,
142 const std::string &name,
143 location loc = location ());
145 function new_function (enum gcc_jit_function_kind kind,
146 type return_type,
147 const std::string &name,
148 std::vector<param> &params,
149 int is_variadic,
150 location loc = location ());
152 function get_builtin_function (const std::string &name);
154 lvalue new_global (type type_,
155 const std::string &name,
156 location loc = location ());
158 rvalue new_rvalue (type numeric_type,
159 int value) const;
160 rvalue zero (type numeric_type) const;
161 rvalue one (type numeric_type) const;
162 rvalue new_rvalue (type numeric_type,
163 double value) const;
164 rvalue new_rvalue (type pointer_type,
165 void *value) const;
166 rvalue new_rvalue (const std::string &value) const;
168 /* Generic unary operations... */
169 rvalue new_unary_op (enum gcc_jit_unary_op op,
170 type result_type,
171 rvalue a,
172 location loc = location ());
174 /* ...and shorter ways to spell the various specific kinds of
175 unary op. */
176 rvalue new_minus (type result_type,
177 rvalue a,
178 location loc = location ());
179 rvalue new_bitwise_negate (type result_type,
180 rvalue a,
181 location loc = location ());
182 rvalue new_logical_negate (type result_type,
183 rvalue a,
184 location loc = location ());
186 /* Generic binary operations... */
187 rvalue new_binary_op (enum gcc_jit_binary_op op,
188 type result_type,
189 rvalue a, rvalue b,
190 location loc = location ());
192 /* ...and shorter ways to spell the various specific kinds of
193 binary op. */
194 rvalue new_plus (type result_type,
195 rvalue a, rvalue b,
196 location loc = location ());
197 rvalue new_minus (type result_type,
198 rvalue a, rvalue b,
199 location loc = location ());
200 rvalue new_mult (type result_type,
201 rvalue a, rvalue b,
202 location loc = location ());
203 rvalue new_divide (type result_type,
204 rvalue a, rvalue b,
205 location loc = location ());
206 rvalue new_modulo (type result_type,
207 rvalue a, rvalue b,
208 location loc = location ());
209 rvalue new_bitwise_and (type result_type,
210 rvalue a, rvalue b,
211 location loc = location ());
212 rvalue new_bitwise_xor (type result_type,
213 rvalue a, rvalue b,
214 location loc = location ());
215 rvalue new_bitwise_or (type result_type,
216 rvalue a, rvalue b,
217 location loc = location ());
218 rvalue new_logical_and (type result_type,
219 rvalue a, rvalue b,
220 location loc = location ());
221 rvalue new_logical_or (type result_type,
222 rvalue a, rvalue b,
223 location loc = location ());
225 /* Generic comparisons... */
226 rvalue new_comparison (enum gcc_jit_comparison op,
227 rvalue a, rvalue b,
228 location loc = location ());
229 /* ...and shorter ways to spell the various specific kinds of
230 comparison. */
231 rvalue new_eq (rvalue a, rvalue b,
232 location loc = location ());
233 rvalue new_ne (rvalue a, rvalue b,
234 location loc = location ());
235 rvalue new_lt (rvalue a, rvalue b,
236 location loc = location ());
237 rvalue new_le (rvalue a, rvalue b,
238 location loc = location ());
239 rvalue new_gt (rvalue a, rvalue b,
240 location loc = location ());
241 rvalue new_ge (rvalue a, rvalue b,
242 location loc = location ());
244 /* The most general way of creating a function call. */
245 rvalue new_call (function func,
246 std::vector<rvalue> &args,
247 location loc = location ());
249 /* In addition, we provide a series of overloaded "new_call" methods
250 for specific numbers of args (from 0 - 6), to avoid the need for
251 client code to manually build a vector. */
252 rvalue new_call (function func,
253 location loc = location ());
254 rvalue new_call (function func,
255 rvalue arg0,
256 location loc = location ());
257 rvalue new_call (function func,
258 rvalue arg0, rvalue arg1,
259 location loc = location ());
260 rvalue new_call (function func,
261 rvalue arg0, rvalue arg1, rvalue arg2,
262 location loc = location ());
263 rvalue new_call (function func,
264 rvalue arg0, rvalue arg1, rvalue arg2,
265 rvalue arg3,
266 location loc = location ());
267 rvalue new_call (function func,
268 rvalue arg0, rvalue arg1, rvalue arg2,
269 rvalue arg3, rvalue arg4,
270 location loc = location ());
271 rvalue new_call (function func,
272 rvalue arg0, rvalue arg1, rvalue arg2,
273 rvalue arg3, rvalue arg4, rvalue arg5,
274 location loc = location ());
276 rvalue new_cast (rvalue expr,
277 type type_,
278 location loc = location ());
280 lvalue new_array_access (rvalue ptr,
281 rvalue index,
282 location loc = location ());
284 private:
285 gcc_jit_context *m_inner_ctxt;
288 class field : public object
290 public:
291 field ();
292 field (gcc_jit_field *inner);
294 gcc_jit_field *get_inner_field () const;
297 class type : public object
299 public:
300 type ();
301 type (gcc_jit_type *inner);
303 gcc_jit_type *get_inner_type () const;
305 type get_pointer ();
306 type get_volatile ();
308 // Shortcuts for getting values of numeric types:
309 rvalue zero ();
310 rvalue one ();
313 class struct_ : public type
315 public:
316 struct_ ();
317 struct_ (gcc_jit_struct *inner);
319 gcc_jit_struct *get_inner_struct () const;
322 class function : public object
324 public:
325 function ();
326 function (gcc_jit_function *func);
328 gcc_jit_function *get_inner_function () const;
330 void dump_to_dot (const std::string &path);
332 param get_param (int index) const;
334 block new_block ();
335 block new_block (const std::string &name);
337 lvalue new_local (type type_,
338 const std::string &name,
339 location loc = location ());
341 /* A series of overloaded operator () with various numbers of arguments
342 for a very terse way of creating a call to this function. The call
343 is created within the same context as the function itself, which may
344 not be what you want. */
345 rvalue operator() (location loc = location ());
346 rvalue operator() (rvalue arg0,
347 location loc = location ());
348 rvalue operator() (rvalue arg0, rvalue arg1,
349 location loc = location ());
350 rvalue operator() (rvalue arg0, rvalue arg1, rvalue arg2,
351 location loc = location ());
354 class block : public object
356 public:
357 block ();
358 block (gcc_jit_block *inner);
360 gcc_jit_block *get_inner_block () const;
362 function get_function () const;
364 void add_eval (rvalue rvalue,
365 location loc = location ());
367 void add_assignment (lvalue lvalue,
368 rvalue rvalue,
369 location loc = location ());
371 void add_assignment_op (lvalue lvalue,
372 enum gcc_jit_binary_op op,
373 rvalue rvalue,
374 location loc = location ());
376 /* A way to add a function call to the body of a function being
377 defined, with various numbers of args. */
378 rvalue add_call (function other,
379 location loc = location ());
380 rvalue add_call (function other,
381 rvalue arg0,
382 location loc = location ());
383 rvalue add_call (function other,
384 rvalue arg0, rvalue arg1,
385 location loc = location ());
386 rvalue add_call (function other,
387 rvalue arg0, rvalue arg1, rvalue arg2,
388 location loc = location ());
389 rvalue add_call (function other,
390 rvalue arg0, rvalue arg1, rvalue arg2, rvalue arg3,
391 location loc = location ());
393 void add_comment (const std::string &text,
394 location loc = location ());
396 void end_with_conditional (rvalue boolval,
397 block on_true,
398 block on_false,
399 location loc = location ());
401 void end_with_jump (block target,
402 location loc = location ());
404 void end_with_return (rvalue rvalue,
405 location loc = location ());
406 void end_with_return (location loc = location ());
410 class rvalue : public object
412 public:
413 rvalue ();
414 rvalue (gcc_jit_rvalue *inner);
415 gcc_jit_rvalue *get_inner_rvalue () const;
417 type get_type ();
419 rvalue access_field (field field,
420 location loc = location ());
422 lvalue dereference_field (field field,
423 location loc = location ());
425 lvalue dereference (location loc = location ());
427 rvalue cast_to (type type_,
428 location loc = location ());
430 /* Array access. */
431 lvalue operator[] (rvalue index);
432 lvalue operator[] (int index);
435 class lvalue : public rvalue
437 public:
438 lvalue ();
439 lvalue (gcc_jit_lvalue *inner);
441 gcc_jit_lvalue *get_inner_lvalue () const;
443 lvalue access_field (field field,
444 location loc = location ());
446 rvalue get_address (location loc = location ());
449 class param : public lvalue
451 public:
452 param ();
453 param (gcc_jit_param *inner);
455 gcc_jit_param *get_inner_param () const;
459 /* Overloaded operators, for those who want the most terse API
460 (at the possible risk of being a little too magical).
462 In each case, the first parameter is used to determine which context
463 owns the resulting expression, and, where appropriate, what the
464 latter's type is. */
466 /* Unary operators. */
467 rvalue operator- (rvalue a); // unary minus
468 rvalue operator~ (rvalue a); // unary bitwise negate
469 rvalue operator! (rvalue a); // unary logical negate
471 /* Binary operators. */
472 rvalue operator+ (rvalue a, rvalue b);
473 rvalue operator- (rvalue a, rvalue b);
474 rvalue operator* (rvalue a, rvalue b);
475 rvalue operator/ (rvalue a, rvalue b);
476 rvalue operator% (rvalue a, rvalue b);
477 rvalue operator& (rvalue a, rvalue b); // bitwise and
478 rvalue operator^ (rvalue a, rvalue b); // bitwise_xor
479 rvalue operator| (rvalue a, rvalue b); // bitwise_or
480 rvalue operator&& (rvalue a, rvalue b); // logical_and
481 rvalue operator|| (rvalue a, rvalue b); // logical_or
483 /* Comparisons. */
484 rvalue operator== (rvalue a, rvalue b);
485 rvalue operator!= (rvalue a, rvalue b);
486 rvalue operator< (rvalue a, rvalue b);
487 rvalue operator<= (rvalue a, rvalue b);
488 rvalue operator> (rvalue a, rvalue b);
489 rvalue operator>= (rvalue a, rvalue b);
491 /* Dereferencing. */
492 lvalue operator* (rvalue ptr);
495 /****************************************************************************
496 Implementation of the API
497 ****************************************************************************/
498 namespace gccjit {
500 // class context
501 inline context context::acquire ()
503 return context (gcc_jit_context_acquire ());
505 inline context::context () : m_inner_ctxt (NULL) {}
506 inline context::context (gcc_jit_context *inner) : m_inner_ctxt (inner)
508 if (!inner)
509 throw error ();
512 inline gccjit::context
513 context::new_child_context ()
515 return context (gcc_jit_context_new_child_context (m_inner_ctxt));
518 inline void
519 context::release ()
521 gcc_jit_context_release (m_inner_ctxt);
522 m_inner_ctxt = NULL;
525 inline gcc_jit_result *
526 context::compile ()
528 gcc_jit_result *result = gcc_jit_context_compile (m_inner_ctxt);
529 if (!result)
530 throw error ();
531 return result;
534 inline void
535 context::dump_to_file (const std::string &path,
536 bool update_locations)
538 gcc_jit_context_dump_to_file (m_inner_ctxt,
539 path.c_str (),
540 update_locations);
543 inline void
544 context::set_str_option (enum gcc_jit_str_option opt,
545 const char *value)
547 gcc_jit_context_set_str_option (m_inner_ctxt, opt, value);
551 inline void
552 context::set_int_option (enum gcc_jit_int_option opt,
553 int value)
555 gcc_jit_context_set_int_option (m_inner_ctxt, opt, value);
559 inline void
560 context::set_bool_option (enum gcc_jit_bool_option opt,
561 int value)
563 gcc_jit_context_set_bool_option (m_inner_ctxt, opt, value);
567 inline location
568 context::new_location (const std::string &filename,
569 int line,
570 int column)
572 return location (gcc_jit_context_new_location (m_inner_ctxt,
573 filename.c_str (),
574 line,
575 column));
578 inline type
579 context::get_type (enum gcc_jit_types kind)
581 return type (gcc_jit_context_get_type (m_inner_ctxt, kind));
584 inline type
585 context::get_int_type (size_t num_bytes, int is_signed)
587 return type (gcc_jit_context_get_int_type (m_inner_ctxt,
588 num_bytes,
589 is_signed));
592 template <typename T>
593 inline type
594 context::get_int_type ()
596 return get_int_type (sizeof (T), std::numeric_limits<T>::is_signed);
599 inline type
600 context::new_array_type (type element_type, int num_elements, location loc)
602 return type (gcc_jit_context_new_array_type (
603 m_inner_ctxt,
604 loc.get_inner_location (),
605 element_type.get_inner_type (),
606 num_elements));
609 inline field
610 context::new_field (type type_, const std::string &name, location loc)
612 return field (gcc_jit_context_new_field (m_inner_ctxt,
613 loc.get_inner_location (),
614 type_.get_inner_type (),
615 name.c_str ()));
618 inline struct_
619 context::new_struct_type (const std::string &name,
620 std::vector<field> &fields,
621 location loc)
623 /* Treat std::vector as an array, relying on it not being resized: */
624 field *as_array_of_wrappers = &fields[0];
626 /* Treat the array as being of the underlying pointers, relying on
627 the wrapper type being such a pointer internally. */
628 gcc_jit_field **as_array_of_ptrs =
629 reinterpret_cast<gcc_jit_field **> (as_array_of_wrappers);
631 return struct_ (gcc_jit_context_new_struct_type (m_inner_ctxt,
632 loc.get_inner_location (),
633 name.c_str (),
634 fields.size (),
635 as_array_of_ptrs));
638 inline struct_
639 context::new_opaque_struct_type (const std::string &name,
640 location loc)
642 return struct_ (gcc_jit_context_new_opaque_struct (
643 m_inner_ctxt,
644 loc.get_inner_location (),
645 name.c_str ()));
648 inline param
649 context::new_param (type type_,
650 const std::string &name,
651 location loc)
653 return param (gcc_jit_context_new_param (m_inner_ctxt,
654 loc.get_inner_location (),
655 type_.get_inner_type (),
656 name.c_str ()));
659 inline function
660 context::new_function (enum gcc_jit_function_kind kind,
661 type return_type,
662 const std::string &name,
663 std::vector<param> &params,
664 int is_variadic,
665 location loc)
667 /* Treat std::vector as an array, relying on it not being resized: */
668 param *as_array_of_wrappers = &params[0];
670 /* Treat the array as being of the underlying pointers, relying on
671 the wrapper type being such a pointer internally. */
672 gcc_jit_param **as_array_of_ptrs =
673 reinterpret_cast<gcc_jit_param **> (as_array_of_wrappers);
675 return function (gcc_jit_context_new_function (m_inner_ctxt,
676 loc.get_inner_location (),
677 kind,
678 return_type.get_inner_type (),
679 name.c_str (),
680 params.size (),
681 as_array_of_ptrs,
682 is_variadic));
685 inline function
686 context::get_builtin_function (const std::string &name)
688 return function (gcc_jit_context_get_builtin_function (m_inner_ctxt,
689 name.c_str ()));
692 inline lvalue
693 context::new_global (type type_,
694 const std::string &name,
695 location loc)
697 return lvalue (gcc_jit_context_new_global (m_inner_ctxt,
698 loc.get_inner_location (),
699 type_.get_inner_type (),
700 name.c_str ()));
703 inline rvalue
704 context::new_rvalue (type numeric_type,
705 int value) const
707 return rvalue (
708 gcc_jit_context_new_rvalue_from_int (m_inner_ctxt,
709 numeric_type.get_inner_type (),
710 value));
713 inline rvalue
714 context::zero (type numeric_type) const
716 return rvalue (gcc_jit_context_zero (m_inner_ctxt,
717 numeric_type.get_inner_type ()));
720 inline rvalue
721 context::one (type numeric_type) const
723 return rvalue (gcc_jit_context_one (m_inner_ctxt,
724 numeric_type.get_inner_type ()));
727 inline rvalue
728 context::new_rvalue (type numeric_type,
729 double value) const
731 return rvalue (
732 gcc_jit_context_new_rvalue_from_double (m_inner_ctxt,
733 numeric_type.get_inner_type (),
734 value));
737 inline rvalue
738 context::new_rvalue (type pointer_type,
739 void *value) const
741 return rvalue (
742 gcc_jit_context_new_rvalue_from_ptr (m_inner_ctxt,
743 pointer_type.get_inner_type (),
744 value));
747 inline rvalue
748 context::new_rvalue (const std::string &value) const
750 return rvalue (
751 gcc_jit_context_new_string_literal (m_inner_ctxt, value.c_str ()));
754 inline rvalue
755 context::new_unary_op (enum gcc_jit_unary_op op,
756 type result_type,
757 rvalue a,
758 location loc)
760 return rvalue (gcc_jit_context_new_unary_op (m_inner_ctxt,
761 loc.get_inner_location (),
763 result_type.get_inner_type (),
764 a.get_inner_rvalue ()));
766 inline rvalue
767 context::new_minus (type result_type,
768 rvalue a,
769 location loc)
771 return rvalue (new_unary_op (GCC_JIT_UNARY_OP_MINUS,
772 result_type, a, loc));
774 inline rvalue
775 context::new_bitwise_negate (type result_type,
776 rvalue a,
777 location loc)
779 return rvalue (new_unary_op (GCC_JIT_UNARY_OP_BITWISE_NEGATE,
780 result_type, a, loc));
782 inline rvalue
783 context::new_logical_negate (type result_type,
784 rvalue a,
785 location loc)
787 return rvalue (new_unary_op (GCC_JIT_UNARY_OP_LOGICAL_NEGATE,
788 result_type, a, loc));
791 inline rvalue
792 context::new_binary_op (enum gcc_jit_binary_op op,
793 type result_type,
794 rvalue a, rvalue b,
795 location loc)
797 return rvalue (gcc_jit_context_new_binary_op (m_inner_ctxt,
798 loc.get_inner_location (),
800 result_type.get_inner_type (),
801 a.get_inner_rvalue (),
802 b.get_inner_rvalue ()));
804 inline rvalue
805 context::new_plus (type result_type,
806 rvalue a, rvalue b,
807 location loc)
809 return new_binary_op (GCC_JIT_BINARY_OP_PLUS,
810 result_type, a, b, loc);
812 inline rvalue
813 context::new_minus (type result_type,
814 rvalue a, rvalue b,
815 location loc)
817 return new_binary_op (GCC_JIT_BINARY_OP_MINUS,
818 result_type, a, b, loc);
820 inline rvalue
821 context::new_mult (type result_type,
822 rvalue a, rvalue b,
823 location loc)
825 return new_binary_op (GCC_JIT_BINARY_OP_MULT,
826 result_type, a, b, loc);
828 inline rvalue
829 context::new_divide (type result_type,
830 rvalue a, rvalue b,
831 location loc)
833 return new_binary_op (GCC_JIT_BINARY_OP_DIVIDE,
834 result_type, a, b, loc);
836 inline rvalue
837 context::new_modulo (type result_type,
838 rvalue a, rvalue b,
839 location loc)
841 return new_binary_op (GCC_JIT_BINARY_OP_MODULO,
842 result_type, a, b, loc);
844 inline rvalue
845 context::new_bitwise_and (type result_type,
846 rvalue a, rvalue b,
847 location loc)
849 return new_binary_op (GCC_JIT_BINARY_OP_BITWISE_AND,
850 result_type, a, b, loc);
852 inline rvalue
853 context::new_bitwise_xor (type result_type,
854 rvalue a, rvalue b,
855 location loc)
857 return new_binary_op (GCC_JIT_BINARY_OP_BITWISE_XOR,
858 result_type, a, b, loc);
860 inline rvalue
861 context::new_bitwise_or (type result_type,
862 rvalue a, rvalue b,
863 location loc)
865 return new_binary_op (GCC_JIT_BINARY_OP_BITWISE_OR,
866 result_type, a, b, loc);
868 inline rvalue
869 context::new_logical_and (type result_type,
870 rvalue a, rvalue b,
871 location loc)
873 return new_binary_op (GCC_JIT_BINARY_OP_LOGICAL_AND,
874 result_type, a, b, loc);
876 inline rvalue
877 context::new_logical_or (type result_type,
878 rvalue a, rvalue b,
879 location loc)
881 return new_binary_op (GCC_JIT_BINARY_OP_LOGICAL_OR,
882 result_type, a, b, loc);
885 inline rvalue
886 context::new_comparison (enum gcc_jit_comparison op,
887 rvalue a, rvalue b,
888 location loc)
890 return rvalue (gcc_jit_context_new_comparison (m_inner_ctxt,
891 loc.get_inner_location (),
893 a.get_inner_rvalue (),
894 b.get_inner_rvalue ()));
896 inline rvalue
897 context::new_eq (rvalue a, rvalue b,
898 location loc)
900 return new_comparison (GCC_JIT_COMPARISON_EQ,
901 a, b, loc);
903 inline rvalue
904 context::new_ne (rvalue a, rvalue b,
905 location loc)
907 return new_comparison (GCC_JIT_COMPARISON_NE,
908 a, b, loc);
910 inline rvalue
911 context::new_lt (rvalue a, rvalue b,
912 location loc)
914 return new_comparison (GCC_JIT_COMPARISON_LT,
915 a, b, loc);
917 inline rvalue
918 context::new_le (rvalue a, rvalue b,
919 location loc)
921 return new_comparison (GCC_JIT_COMPARISON_LE,
922 a, b, loc);
924 inline rvalue
925 context::new_gt (rvalue a, rvalue b,
926 location loc)
928 return new_comparison (GCC_JIT_COMPARISON_GT,
929 a, b, loc);
931 inline rvalue
932 context::new_ge (rvalue a, rvalue b,
933 location loc)
935 return new_comparison (GCC_JIT_COMPARISON_GE,
936 a, b, loc);
939 inline rvalue
940 context::new_call (function func,
941 std::vector<rvalue> &args,
942 location loc)
944 /* Treat std::vector as an array, relying on it not being resized: */
945 rvalue *as_array_of_wrappers = &args[0];
947 /* Treat the array as being of the underlying pointers, relying on
948 the wrapper type being such a pointer internally. */
949 gcc_jit_rvalue **as_array_of_ptrs =
950 reinterpret_cast<gcc_jit_rvalue **> (as_array_of_wrappers);
951 return gcc_jit_context_new_call (m_inner_ctxt,
952 loc.get_inner_location (),
953 func.get_inner_function (),
954 args.size (),
955 as_array_of_ptrs);
957 inline rvalue
958 context::new_call (function func,
959 location loc)
961 std::vector<rvalue> args;
962 return new_call (func, args, loc);
965 inline rvalue
966 context::new_call (function func,
967 rvalue arg0,
968 location loc)
970 std::vector<rvalue> args(1);
971 args[0] = arg0;
972 return new_call (func, args, loc);
974 inline rvalue
975 context::new_call (function func,
976 rvalue arg0, rvalue arg1,
977 location loc)
979 std::vector<rvalue> args(2);
980 args[0] = arg0;
981 args[1] = arg1;
982 return new_call (func, args, loc);
984 inline rvalue
985 context::new_call (function func,
986 rvalue arg0, rvalue arg1, rvalue arg2,
987 location loc)
989 std::vector<rvalue> args(3);
990 args[0] = arg0;
991 args[1] = arg1;
992 args[2] = arg2;
993 return new_call (func, args, loc);
995 inline rvalue
996 context::new_call (function func,
997 rvalue arg0, rvalue arg1, rvalue arg2,
998 rvalue arg3,
999 location loc)
1001 std::vector<rvalue> args(4);
1002 args[0] = arg0;
1003 args[1] = arg1;
1004 args[2] = arg2;
1005 args[3] = arg3;
1006 return new_call (func, args, loc);
1008 inline rvalue
1009 context::new_call (function func,
1010 rvalue arg0, rvalue arg1, rvalue arg2,
1011 rvalue arg3, rvalue arg4,
1012 location loc)
1014 std::vector<rvalue> args(5);
1015 args[0] = arg0;
1016 args[1] = arg1;
1017 args[2] = arg2;
1018 args[3] = arg3;
1019 args[4] = arg4;
1020 return new_call (func, args, loc);
1022 inline rvalue
1023 context::new_call (function func,
1024 rvalue arg0, rvalue arg1, rvalue arg2,
1025 rvalue arg3, rvalue arg4, rvalue arg5,
1026 location loc)
1028 std::vector<rvalue> args(6);
1029 args[0] = arg0;
1030 args[1] = arg1;
1031 args[2] = arg2;
1032 args[3] = arg3;
1033 args[4] = arg4;
1034 args[5] = arg5;
1035 return new_call (func, args, loc);
1038 inline rvalue
1039 context::new_cast (rvalue expr,
1040 type type_,
1041 location loc)
1043 return rvalue (gcc_jit_context_new_cast (m_inner_ctxt,
1044 loc.get_inner_location (),
1045 expr.get_inner_rvalue (),
1046 type_.get_inner_type ()));
1049 inline lvalue
1050 context::new_array_access (rvalue ptr,
1051 rvalue index,
1052 location loc)
1054 return lvalue (gcc_jit_context_new_array_access (m_inner_ctxt,
1055 loc.get_inner_location (),
1056 ptr.get_inner_rvalue (),
1057 index.get_inner_rvalue ()));
1060 // class object
1061 inline context
1062 object::get_context () const
1064 return context (gcc_jit_object_get_context (m_inner_obj));
1067 inline std::string
1068 object::get_debug_string () const
1070 return gcc_jit_object_get_debug_string (m_inner_obj);
1073 inline object::object () : m_inner_obj (NULL) {}
1074 inline object::object (gcc_jit_object *obj) : m_inner_obj (obj)
1076 if (!obj)
1077 throw error ();
1080 inline gcc_jit_object *
1081 object::get_inner_object () const
1083 return m_inner_obj;
1086 inline std::ostream&
1087 operator << (std::ostream& stream, const object &obj)
1089 return stream << obj.get_debug_string ();
1092 // class location
1093 inline location::location () : object () {}
1094 inline location::location (gcc_jit_location *loc)
1095 : object (gcc_jit_location_as_object (loc))
1098 inline gcc_jit_location *
1099 location::get_inner_location () const
1101 /* Manual downcast: */
1102 return reinterpret_cast<gcc_jit_location *> (get_inner_object ());
1105 // class field
1106 inline field::field () : object () {}
1107 inline field::field (gcc_jit_field *inner)
1108 : object (gcc_jit_field_as_object (inner))
1111 inline gcc_jit_field *
1112 field::get_inner_field () const
1114 /* Manual downcast: */
1115 return reinterpret_cast<gcc_jit_field *> (get_inner_object ());
1118 // class type
1119 inline type::type () : object () {}
1120 inline type::type (gcc_jit_type *inner)
1121 : object (gcc_jit_type_as_object (inner))
1124 inline gcc_jit_type *
1125 type::get_inner_type () const
1127 /* Manual downcast: */
1128 return reinterpret_cast<gcc_jit_type *> (get_inner_object ());
1131 inline type
1132 type::get_pointer ()
1134 return type (gcc_jit_type_get_pointer (get_inner_type ()));
1137 inline type
1138 type::get_volatile ()
1140 return type (gcc_jit_type_get_volatile (get_inner_type ()));
1143 inline rvalue
1144 type::zero ()
1146 return get_context ().new_rvalue (*this, 0);
1149 inline rvalue
1150 type::one ()
1152 return get_context ().new_rvalue (*this, 1);
1155 // class struct_
1156 inline struct_::struct_ () : type (NULL) {}
1157 inline struct_::struct_ (gcc_jit_struct *inner) :
1158 type (gcc_jit_struct_as_type (inner))
1162 inline gcc_jit_struct *
1163 struct_::get_inner_struct () const
1165 /* Manual downcast: */
1166 return reinterpret_cast<gcc_jit_struct *> (get_inner_object ());
1169 // class function
1170 inline function::function () : object () {}
1171 inline function::function (gcc_jit_function *inner)
1172 : object (gcc_jit_function_as_object (inner))
1175 inline gcc_jit_function *
1176 function::get_inner_function () const
1178 /* Manual downcast: */
1179 return reinterpret_cast<gcc_jit_function *> (get_inner_object ());
1182 inline void
1183 function::dump_to_dot (const std::string &path)
1185 gcc_jit_function_dump_to_dot (get_inner_function (),
1186 path.c_str ());
1189 inline param
1190 function::get_param (int index) const
1192 return param (gcc_jit_function_get_param (get_inner_function (),
1193 index));
1196 inline block
1197 function::new_block ()
1199 return block (gcc_jit_function_new_block (get_inner_function (),
1200 NULL));
1203 inline block
1204 function::new_block (const std::string &name)
1206 return block (gcc_jit_function_new_block (get_inner_function (),
1207 name.c_str ()));
1210 inline lvalue
1211 function::new_local (type type_,
1212 const std::string &name,
1213 location loc)
1215 return lvalue (gcc_jit_function_new_local (get_inner_function (),
1216 loc.get_inner_location (),
1217 type_.get_inner_type (),
1218 name.c_str ()));
1221 inline function
1222 block::get_function () const
1224 return function (gcc_jit_block_get_function ( get_inner_block ()));
1227 inline void
1228 block::add_eval (rvalue rvalue,
1229 location loc)
1231 gcc_jit_block_add_eval (get_inner_block (),
1232 loc.get_inner_location (),
1233 rvalue.get_inner_rvalue ());
1236 inline void
1237 block::add_assignment (lvalue lvalue,
1238 rvalue rvalue,
1239 location loc)
1241 gcc_jit_block_add_assignment (get_inner_block (),
1242 loc.get_inner_location (),
1243 lvalue.get_inner_lvalue (),
1244 rvalue.get_inner_rvalue ());
1247 inline void
1248 block::add_assignment_op (lvalue lvalue,
1249 enum gcc_jit_binary_op op,
1250 rvalue rvalue,
1251 location loc)
1253 gcc_jit_block_add_assignment_op (get_inner_block (),
1254 loc.get_inner_location (),
1255 lvalue.get_inner_lvalue (),
1257 rvalue.get_inner_rvalue ());
1260 inline void
1261 block::add_comment (const std::string &text,
1262 location loc)
1264 gcc_jit_block_add_comment (get_inner_block (),
1265 loc.get_inner_location (),
1266 text.c_str ());
1269 inline void
1270 block::end_with_conditional (rvalue boolval,
1271 block on_true,
1272 block on_false,
1273 location loc)
1275 gcc_jit_block_end_with_conditional (get_inner_block (),
1276 loc.get_inner_location (),
1277 boolval.get_inner_rvalue (),
1278 on_true.get_inner_block (),
1279 on_false.get_inner_block ());
1282 inline void
1283 block::end_with_jump (block target,
1284 location loc)
1286 gcc_jit_block_end_with_jump (get_inner_block (),
1287 loc.get_inner_location (),
1288 target.get_inner_block ());
1291 inline void
1292 block::end_with_return (rvalue rvalue,
1293 location loc)
1295 gcc_jit_block_end_with_return (get_inner_block (),
1296 loc.get_inner_location (),
1297 rvalue.get_inner_rvalue ());
1300 inline void
1301 block::end_with_return (location loc)
1303 gcc_jit_block_end_with_void_return (get_inner_block (),
1304 loc.get_inner_location ());
1307 inline rvalue
1308 block::add_call (function other,
1309 location loc)
1311 rvalue c = get_context ().new_call (other, loc);
1312 add_eval (c);
1313 return c;
1315 inline rvalue
1316 block::add_call (function other,
1317 rvalue arg0,
1318 location loc)
1320 rvalue c = get_context ().new_call (other, arg0, loc);
1321 add_eval (c);
1322 return c;
1324 inline rvalue
1325 block::add_call (function other,
1326 rvalue arg0, rvalue arg1,
1327 location loc)
1329 rvalue c = get_context ().new_call (other, arg0, arg1, loc);
1330 add_eval (c);
1331 return c;
1333 inline rvalue
1334 block::add_call (function other,
1335 rvalue arg0, rvalue arg1, rvalue arg2,
1336 location loc)
1338 rvalue c = get_context ().new_call (other, arg0, arg1, arg2, loc);
1339 add_eval (c);
1340 return c;
1343 inline rvalue
1344 block::add_call (function other,
1345 rvalue arg0, rvalue arg1, rvalue arg2, rvalue arg3,
1346 location loc)
1348 rvalue c = get_context ().new_call (other, arg0, arg1, arg2, arg3, loc);
1349 add_eval (c);
1350 return c;
1353 inline rvalue
1354 function::operator() (location loc)
1356 return get_context ().new_call (*this, loc);
1358 inline rvalue
1359 function::operator() (rvalue arg0,
1360 location loc)
1362 return get_context ().new_call (*this,
1363 arg0,
1364 loc);
1366 inline rvalue
1367 function::operator() (rvalue arg0, rvalue arg1,
1368 location loc)
1370 return get_context ().new_call (*this,
1371 arg0, arg1,
1372 loc);
1374 inline rvalue
1375 function::operator() (rvalue arg0, rvalue arg1, rvalue arg2,
1376 location loc)
1378 return get_context ().new_call (*this,
1379 arg0, arg1, arg2,
1380 loc);
1383 // class block
1384 inline block::block () : object () {}
1385 inline block::block (gcc_jit_block *inner)
1386 : object (gcc_jit_block_as_object (inner))
1389 inline gcc_jit_block *
1390 block::get_inner_block () const
1392 /* Manual downcast: */
1393 return reinterpret_cast<gcc_jit_block *> (get_inner_object ());
1396 // class rvalue
1397 inline rvalue::rvalue () : object () {}
1398 inline rvalue::rvalue (gcc_jit_rvalue *inner)
1399 : object (gcc_jit_rvalue_as_object (inner))
1402 inline gcc_jit_rvalue *
1403 rvalue::get_inner_rvalue () const
1405 /* Manual downcast: */
1406 return reinterpret_cast<gcc_jit_rvalue *> (get_inner_object ());
1409 inline type
1410 rvalue::get_type ()
1412 return type (gcc_jit_rvalue_get_type (get_inner_rvalue ()));
1415 inline rvalue
1416 rvalue::access_field (field field,
1417 location loc)
1419 return rvalue (gcc_jit_rvalue_access_field (get_inner_rvalue (),
1420 loc.get_inner_location (),
1421 field.get_inner_field ()));
1424 inline lvalue
1425 rvalue::dereference_field (field field,
1426 location loc)
1428 return lvalue (gcc_jit_rvalue_dereference_field (get_inner_rvalue (),
1429 loc.get_inner_location (),
1430 field.get_inner_field ()));
1433 inline lvalue
1434 rvalue::dereference (location loc)
1436 return lvalue (gcc_jit_rvalue_dereference (get_inner_rvalue (),
1437 loc.get_inner_location ()));
1440 inline rvalue
1441 rvalue::cast_to (type type_,
1442 location loc)
1444 return get_context ().new_cast (*this, type_, loc);
1447 inline lvalue
1448 rvalue::operator[] (rvalue index)
1450 return get_context ().new_array_access (*this, index);
1453 inline lvalue
1454 rvalue::operator[] (int index)
1456 context ctxt = get_context ();
1457 type int_t = ctxt.get_int_type <int> ();
1458 return ctxt.new_array_access (*this,
1459 ctxt.new_rvalue (int_t,
1460 index));
1463 // class lvalue : public rvalue
1464 inline lvalue::lvalue () : rvalue () {}
1465 inline lvalue::lvalue (gcc_jit_lvalue *inner)
1466 : rvalue (gcc_jit_lvalue_as_rvalue (inner))
1469 inline gcc_jit_lvalue *
1470 lvalue::get_inner_lvalue () const
1472 /* Manual downcast: */
1473 return reinterpret_cast<gcc_jit_lvalue *> (get_inner_object ());
1476 inline lvalue
1477 lvalue::access_field (field field, location loc)
1479 return lvalue (gcc_jit_lvalue_access_field (get_inner_lvalue (),
1480 loc.get_inner_location (),
1481 field.get_inner_field ()));
1484 inline rvalue
1485 lvalue::get_address (location loc)
1487 return rvalue (gcc_jit_lvalue_get_address (get_inner_lvalue (),
1488 loc.get_inner_location ()));
1491 // class param : public lvalue
1492 inline param::param () : lvalue () {}
1493 inline param::param (gcc_jit_param *inner)
1494 : lvalue (gcc_jit_param_as_lvalue (inner))
1497 /* Overloaded operators. */
1498 // Unary operators
1499 inline rvalue operator- (rvalue a)
1501 return a.get_context ().new_minus (a.get_type (), a);
1503 inline rvalue operator~ (rvalue a)
1505 return a.get_context ().new_bitwise_negate (a.get_type (), a);
1507 inline rvalue operator! (rvalue a)
1509 return a.get_context ().new_logical_negate (a.get_type (), a);
1512 // Binary operators
1513 inline rvalue operator+ (rvalue a, rvalue b)
1515 return a.get_context ().new_plus (a.get_type (), a, b);
1517 inline rvalue operator- (rvalue a, rvalue b)
1519 return a.get_context ().new_minus (a.get_type (), a, b);
1521 inline rvalue operator* (rvalue a, rvalue b)
1523 return a.get_context ().new_mult (a.get_type (), a, b);
1525 inline rvalue operator/ (rvalue a, rvalue b)
1527 return a.get_context ().new_divide (a.get_type (), a, b);
1529 inline rvalue operator% (rvalue a, rvalue b)
1531 return a.get_context ().new_modulo (a.get_type (), a, b);
1533 inline rvalue operator& (rvalue a, rvalue b)
1535 return a.get_context ().new_bitwise_and (a.get_type (), a, b);
1537 inline rvalue operator^ (rvalue a, rvalue b)
1539 return a.get_context ().new_bitwise_xor (a.get_type (), a, b);
1541 inline rvalue operator| (rvalue a, rvalue b)
1543 return a.get_context ().new_bitwise_or (a.get_type (), a, b);
1545 inline rvalue operator&& (rvalue a, rvalue b)
1547 return a.get_context ().new_logical_and (a.get_type (), a, b);
1549 inline rvalue operator|| (rvalue a, rvalue b)
1551 return a.get_context ().new_logical_or (a.get_type (), a, b);
1554 /* Comparisons. */
1555 inline rvalue operator== (rvalue a, rvalue b)
1557 return a.get_context ().new_eq (a, b);
1559 inline rvalue operator!= (rvalue a, rvalue b)
1561 return a.get_context ().new_ne (a, b);
1563 inline rvalue operator< (rvalue a, rvalue b)
1565 return a.get_context ().new_lt (a, b);
1567 inline rvalue operator<= (rvalue a, rvalue b)
1569 return a.get_context ().new_le (a, b);
1571 inline rvalue operator> (rvalue a, rvalue b)
1573 return a.get_context ().new_gt (a, b);
1575 inline rvalue operator>= (rvalue a, rvalue b)
1577 return a.get_context ().new_ge (a, b);
1580 /* Dereferencing. */
1581 inline lvalue operator* (rvalue ptr)
1583 return ptr.dereference ();
1586 } // namespace gccjit
1588 #endif /* #ifndef LIBGCCJIT_PLUS_PLUS_H */