David Malcolm | 35485da | 2014-11-11 21:55:52 +0000 | [diff] [blame] | 1 | /* Core of implementation of libgccjit.so |
Jakub Jelinek | 7adcbaf | 2022-01-03 10:42:10 +0100 | [diff] [blame] | 2 | Copyright (C) 2013-2022 Free Software Foundation, Inc. |
David Malcolm | 35485da | 2014-11-11 21:55:52 +0000 | [diff] [blame] | 3 | Contributed by David Malcolm <dmalcolm@redhat.com>. |
| 4 | |
| 5 | This file is part of GCC. |
| 6 | |
| 7 | GCC is free software; you can redistribute it and/or modify it |
| 8 | under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3, or (at your option) |
| 10 | any later version. |
| 11 | |
| 12 | GCC is distributed in the hope that it will be useful, but |
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with GCC; see the file COPYING3. If not see |
| 19 | <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #ifndef JIT_COMMON_H |
| 22 | #define JIT_COMMON_H |
| 23 | |
| 24 | #include "libgccjit.h" |
| 25 | |
David Malcolm | 860e981 | 2015-01-09 21:45:33 +0000 | [diff] [blame] | 26 | #include "vec.h" |
David Malcolm | 35485da | 2014-11-11 21:55:52 +0000 | [diff] [blame] | 27 | #include "tree.h" |
Andrew MacLeod | c7131fb | 2015-07-08 00:53:03 +0000 | [diff] [blame] | 28 | #include "inchash.h" |
David Malcolm | 35485da | 2014-11-11 21:55:52 +0000 | [diff] [blame] | 29 | #include "tree-iterator.h" |
| 30 | |
| 31 | #ifdef GCC_VERSION |
| 32 | #if GCC_VERSION >= 4001 |
| 33 | #define GNU_PRINTF(M, N) __attribute__ ((format (gnu_printf, (M), (N)))) |
| 34 | #else |
| 35 | #define GNU_PRINTF(M, N) |
| 36 | #endif |
| 37 | #endif |
| 38 | |
Antoni Boucher | af80ea9 | 2022-04-12 17:16:45 -0400 | [diff] [blame] | 39 | const int NUM_GCC_JIT_TYPES = GCC_JIT_TYPE_INT128_T + 1; |
David Malcolm | 35485da | 2014-11-11 21:55:52 +0000 | [diff] [blame] | 40 | |
| 41 | /* This comment is included by the docs. |
| 42 | |
| 43 | In order to allow jit objects to be usable outside of a compile |
| 44 | whilst working with the existing structure of GCC's code the |
| 45 | C API is implemented in terms of a gcc::jit::recording::context, |
| 46 | which records the calls made to it. |
| 47 | |
| 48 | When a gcc_jit_context is compiled, the recording context creates a |
| 49 | playback context. The playback context invokes the bulk of the GCC |
| 50 | code, and within the "frontend" parsing hook, plays back the recorded |
| 51 | API calls, creating GCC tree objects. |
| 52 | |
| 53 | So there are two parallel families of classes: those relating to |
| 54 | recording, and those relating to playback: |
| 55 | |
| 56 | * Visibility: recording objects are exposed back to client code, |
| 57 | whereas playback objects are internal to the library. |
| 58 | |
| 59 | * Lifetime: recording objects have a lifetime equal to that of the |
| 60 | recording context that created them, whereas playback objects only |
| 61 | exist within the frontend hook. |
| 62 | |
| 63 | * Memory allocation: recording objects are allocated by the recording |
| 64 | context, and automatically freed by it when the context is released, |
| 65 | whereas playback objects are allocated within the GC heap, and |
| 66 | garbage-collected; they can own GC-references. |
| 67 | |
| 68 | * Integration with rest of GCC: recording objects are unrelated to the |
| 69 | rest of GCC, whereas playback objects are wrappers around "tree" |
| 70 | instances. Hence you can't ask a recording rvalue or lvalue what its |
| 71 | type is, whereas you can for a playback rvalue of lvalue (since it |
| 72 | can work with the underlying GCC tree nodes). |
| 73 | |
| 74 | * Instancing: There can be multiple recording contexts "alive" at once |
| 75 | (albeit it only one compiling at once), whereas there can only be one |
| 76 | playback context alive at one time (since it interacts with the GC). |
| 77 | |
| 78 | Ultimately if GCC could support multiple GC heaps and contexts, and |
| 79 | finer-grained initialization, then this recording vs playback |
| 80 | distinction could be eliminated. |
| 81 | |
| 82 | During a playback, we associate objects from the recording with |
| 83 | their counterparts during this playback. For simplicity, we store this |
| 84 | within the recording objects, as ``void *m_playback_obj``, casting it to |
| 85 | the appropriate playback object subclass. For these casts to make |
| 86 | sense, the two class hierarchies need to have the same structure. |
| 87 | |
| 88 | Note that the playback objects that ``m_playback_obj`` points to are |
| 89 | GC-allocated, but the recording objects don't own references: |
| 90 | these associations only exist within a part of the code where |
| 91 | the GC doesn't collect, and are set back to NULL before the GC can |
| 92 | run. |
| 93 | |
| 94 | End of comment for inclusion in the docs. */ |
| 95 | |
| 96 | namespace gcc { |
| 97 | |
| 98 | namespace jit { |
| 99 | |
| 100 | class result; |
| 101 | class dump; |
David Malcolm | eb4c16e | 2015-01-08 19:41:07 +0000 | [diff] [blame] | 102 | class logger; |
David Malcolm | eeafb31 | 2014-12-01 18:23:37 +0000 | [diff] [blame] | 103 | class builtins_manager; // declared within jit-builtins.h |
David Malcolm | d1e5f2c | 2014-12-09 20:00:07 +0000 | [diff] [blame] | 104 | class tempdir; |
David Malcolm | 35485da | 2014-11-11 21:55:52 +0000 | [diff] [blame] | 105 | |
| 106 | namespace recording { |
| 107 | |
| 108 | /* Recording types. */ |
| 109 | |
| 110 | /* Indentation indicates inheritance: */ |
| 111 | class context; |
David Malcolm | 35485da | 2014-11-11 21:55:52 +0000 | [diff] [blame] | 112 | class memento; |
| 113 | class string; |
| 114 | class location; |
| 115 | class type; |
| 116 | class function_type; |
| 117 | class compound_type; |
| 118 | class struct_; |
| 119 | class union_; |
David Malcolm | 6069fe7 | 2017-10-04 13:41:01 +0000 | [diff] [blame] | 120 | class vector_type; |
David Malcolm | 35485da | 2014-11-11 21:55:52 +0000 | [diff] [blame] | 121 | class field; |
Andrea Corallo | ee118c1 | 2019-07-04 15:46:00 +0000 | [diff] [blame] | 122 | class bitfield; |
David Malcolm | 35485da | 2014-11-11 21:55:52 +0000 | [diff] [blame] | 123 | class fields; |
| 124 | class function; |
| 125 | class block; |
| 126 | class rvalue; |
| 127 | class lvalue; |
| 128 | class local; |
| 129 | class global; |
| 130 | class param; |
David Malcolm | 15c671a | 2016-05-20 19:12:49 +0000 | [diff] [blame] | 131 | class base_call; |
David Malcolm | 15a65e6 | 2017-09-27 23:57:35 +0000 | [diff] [blame] | 132 | class function_pointer; |
David Malcolm | 35485da | 2014-11-11 21:55:52 +0000 | [diff] [blame] | 133 | class statement; |
David Malcolm | 421d0d0 | 2020-11-12 17:28:17 -0500 | [diff] [blame] | 134 | class extended_asm; |
David Malcolm | ec5d008 | 2015-06-30 20:39:50 +0000 | [diff] [blame] | 135 | class case_; |
David Malcolm | 421d0d0 | 2020-11-12 17:28:17 -0500 | [diff] [blame] | 136 | class top_level_asm; |
David Malcolm | 35485da | 2014-11-11 21:55:52 +0000 | [diff] [blame] | 137 | |
| 138 | /* End of recording types. */ |
| 139 | } |
| 140 | |
| 141 | namespace playback { |
| 142 | /* Playback types. */ |
| 143 | |
| 144 | /* Indentation indicates inheritance: */ |
| 145 | class context; |
| 146 | class wrapper; |
| 147 | class type; |
| 148 | class compound_type; |
| 149 | class field; |
| 150 | class function; |
| 151 | class block; |
| 152 | class rvalue; |
| 153 | class lvalue; |
| 154 | class param; |
| 155 | class source_file; |
| 156 | class source_line; |
| 157 | class location; |
David Malcolm | ec5d008 | 2015-06-30 20:39:50 +0000 | [diff] [blame] | 158 | class case_; |
David Malcolm | 35485da | 2014-11-11 21:55:52 +0000 | [diff] [blame] | 159 | |
| 160 | /* End of playback types. */ |
| 161 | } |
| 162 | |
| 163 | typedef playback::context replayer; |
| 164 | |
| 165 | class dump |
| 166 | { |
| 167 | public: |
| 168 | dump (recording::context &ctxt, |
| 169 | const char *filename, |
| 170 | bool update_locations); |
| 171 | ~dump (); |
| 172 | |
David Malcolm | 86d0ac8 | 2015-01-13 22:14:46 +0000 | [diff] [blame] | 173 | recording::context &get_context () { return m_ctxt; } |
| 174 | |
David Malcolm | 35485da | 2014-11-11 21:55:52 +0000 | [diff] [blame] | 175 | void write (const char *fmt, ...) |
| 176 | GNU_PRINTF(2, 3); |
| 177 | |
| 178 | bool update_locations () const { return m_update_locations; } |
| 179 | |
| 180 | recording::location * |
| 181 | make_location () const; |
| 182 | |
David Malcolm | 53c04ec | 2015-01-27 20:19:36 +0000 | [diff] [blame] | 183 | FILE *get_file () const { return m_file; } |
| 184 | |
David Malcolm | 35485da | 2014-11-11 21:55:52 +0000 | [diff] [blame] | 185 | private: |
| 186 | recording::context &m_ctxt; |
| 187 | const char *m_filename; |
| 188 | bool m_update_locations; |
| 189 | int m_line; |
| 190 | int m_column; |
| 191 | FILE *m_file; |
| 192 | }; |
| 193 | |
David Malcolm | 6a3603e | 2015-06-30 19:38:12 +0000 | [diff] [blame] | 194 | /* A hidden enum of boolean options that are only exposed via API |
| 195 | entrypoints, rather than via gcc_jit_context_set_bool_option. */ |
| 196 | |
| 197 | enum inner_bool_option |
| 198 | { |
| 199 | INNER_BOOL_OPTION_ALLOW_UNREACHABLE_BLOCKS, |
David Malcolm | 9376dd6 | 2015-08-25 20:25:05 +0000 | [diff] [blame] | 200 | INNER_BOOL_OPTION_USE_EXTERNAL_DRIVER, |
Antoni Boucher | 79e1a6f | 2022-04-12 17:23:18 -0400 | [diff] [blame] | 201 | INNER_BOOL_OPTION_PRINT_ERRORS_TO_STDERR, |
David Malcolm | 6a3603e | 2015-06-30 19:38:12 +0000 | [diff] [blame] | 202 | |
| 203 | NUM_INNER_BOOL_OPTIONS |
| 204 | }; |
| 205 | |
Petter Tomner | 3736837 | 2021-11-29 20:44:07 +0100 | [diff] [blame] | 206 | /* Flags for global variables class. For when the playback of the |
| 207 | global need to know what will happen to it later. */ |
| 208 | enum global_var_flags |
| 209 | { |
| 210 | GLOBAL_VAR_FLAGS_NONE = 0, |
| 211 | GLOBAL_VAR_FLAGS_WILL_BE_RVAL_INIT = 1, |
| 212 | GLOBAL_VAR_FLAGS_WILL_BE_BLOB_INIT = 2, |
| 213 | }; |
| 214 | |
David Malcolm | 35485da | 2014-11-11 21:55:52 +0000 | [diff] [blame] | 215 | } // namespace gcc::jit |
| 216 | |
| 217 | } // namespace gcc |
| 218 | |
| 219 | #endif /* JIT_COMMON_H */ |