i386: Add -masm=intel profiling support [PR113122]
[official-gcc.git] / libcc1 / libcp1.cc
blobec3eec2c606153e6162026345ddb7a27c8c10ef4
1 /* The library used by gdb.
2 Copyright (C) 2014-2024 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 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 #include <cc1plugin-config.h>
21 #include <vector>
22 #include <string>
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <sys/wait.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30 #include <stdlib.h>
31 #include "marshall-cp.hh"
32 #include "rpc.hh"
33 #include "connection.hh"
34 #include "names.hh"
35 #include "callbacks.hh"
36 #include "libiberty.h"
37 #include "compiler-name.hh"
38 #include "compiler.hh"
39 #include "gdbctx.hh"
41 // The C compiler context that we hand back to our caller.
42 struct libcp1 : public cc1_plugin::base_gdb_plugin<gcc_cp_context>
44 explicit libcp1 (const gcc_cp_fe_vtable *);
46 void add_callbacks () override;
48 gcc_cp_oracle_function *binding_oracle = nullptr;
49 gcc_cp_symbol_address_function *address_oracle = nullptr;
50 gcc_cp_enter_leave_user_expr_scope_function *enter_scope = nullptr;
51 gcc_cp_enter_leave_user_expr_scope_function *leave_scope = nullptr;
52 void *oracle_datum = nullptr;
55 libcp1::libcp1 (const gcc_cp_fe_vtable *cv)
56 : cc1_plugin::base_gdb_plugin<gcc_cp_context> ("libcp1plugin",
57 CP_COMPILER_NAME,
58 GCC_CP_FE_VERSION_0)
60 cp_ops = cv;
65 // Enclose these functions in an anonymous namespace because they
66 // shouldn't be exported, but they can't be static because they're
67 // used as template arguments.
68 namespace {
69 // This is a wrapper function that is called by the RPC system and
70 // that then forwards the call to the library user. Note that the
71 // return value is not used; the type cannot be 'void' due to
72 // limitations in our simple RPC.
73 int
74 cp_call_binding_oracle (cc1_plugin::connection *conn,
75 enum gcc_cp_oracle_request request,
76 const char *identifier)
78 libcp1 *self = (libcp1 *) (((libcp1::local_connection *) conn)->back_ptr);
80 self->binding_oracle (self->oracle_datum, self, request, identifier);
81 return 1;
84 // This is a wrapper function that is called by the RPC system and
85 // that then forwards the call to the library user.
86 gcc_address
87 cp_call_symbol_address (cc1_plugin::connection *conn, const char *identifier)
89 libcp1 *self = (libcp1 *) (((libcp1::local_connection *) conn)->back_ptr);
91 return self->address_oracle (self->oracle_datum, self, identifier);
94 int
95 cp_call_enter_scope (cc1_plugin::connection *conn)
97 libcp1 *self = (libcp1 *) (((libcp1::local_connection *) conn)->back_ptr);
99 self->enter_scope (self->oracle_datum, self);
100 return 1;
104 cp_call_leave_scope (cc1_plugin::connection *conn)
106 libcp1 *self = (libcp1 *) (((libcp1::local_connection *) conn)->back_ptr);
108 self->leave_scope (self->oracle_datum, self);
109 return 1;
111 } /* anonymous namespace */
115 static void
116 set_callbacks (struct gcc_cp_context *s,
117 gcc_cp_oracle_function *binding_oracle,
118 gcc_cp_symbol_address_function *address_oracle,
119 gcc_cp_enter_leave_user_expr_scope_function *enter_scope,
120 gcc_cp_enter_leave_user_expr_scope_function *leave_scope,
121 void *datum)
123 libcp1 *self = (libcp1 *) s;
125 self->binding_oracle = binding_oracle;
126 self->address_oracle = address_oracle;
127 self->enter_scope = enter_scope;
128 self->leave_scope = leave_scope;
129 self->oracle_datum = datum;
132 static const struct gcc_cp_fe_vtable cp_vtable =
134 GCC_CP_FE_VERSION_0,
135 set_callbacks,
137 #define GCC_METHOD0(R, N) \
138 cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N>,
139 #define GCC_METHOD1(R, N, A) \
140 cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A>,
141 #define GCC_METHOD2(R, N, A, B) \
142 cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B>,
143 #define GCC_METHOD3(R, N, A, B, C) \
144 cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B, C>,
145 #define GCC_METHOD4(R, N, A, B, C, D) \
146 cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B, C, D>,
147 #define GCC_METHOD5(R, N, A, B, C, D, E) \
148 cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B, C, D, E>,
149 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
150 cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B, C, D, E, F, G>,
152 #include "gcc-cp-fe.def"
154 #undef GCC_METHOD0
155 #undef GCC_METHOD1
156 #undef GCC_METHOD2
157 #undef GCC_METHOD3
158 #undef GCC_METHOD4
159 #undef GCC_METHOD5
160 #undef GCC_METHOD7
165 void
166 libcp1::add_callbacks ()
168 cc1_plugin::callback_ftype *fun
169 = cc1_plugin::invoker<int, enum gcc_cp_oracle_request,
170 const char *>::invoke<cp_call_binding_oracle>;
171 connection->add_callback ("binding_oracle", fun);
173 fun = cc1_plugin::invoker<gcc_address,
174 const char *>::invoke<cp_call_symbol_address>;
175 connection->add_callback ("address_oracle", fun);
177 fun = cc1_plugin::invoker<int>::invoke<cp_call_enter_scope>;
178 connection->add_callback ("enter_scope", fun);
180 fun = cc1_plugin::invoker<int>::invoke<cp_call_leave_scope>;
181 connection->add_callback ("leave_scope", fun);
184 extern "C" gcc_cp_fe_context_function gcc_cp_fe_context;
186 #ifdef __GNUC__
187 #pragma GCC visibility push(default)
188 #endif
190 extern "C"
191 struct gcc_cp_context *
192 gcc_cp_fe_context (enum gcc_base_api_version base_version,
193 enum gcc_cp_api_version cp_version)
195 if ((base_version != GCC_FE_VERSION_0 && base_version != GCC_FE_VERSION_1)
196 || cp_version != GCC_CP_FE_VERSION_0)
197 return NULL;
199 return new libcp1 (&cp_vtable);