plugin-driver.c
1 //! @file plugin-driver.c
2 //! @author J. Marcel van der Veer
3
4 //! @section Copyright
5 //!
6 //! This file is part of Algol68G - an Algol 68 compiler-interpreter.
7 //! Copyright 2001-2025 J. Marcel van der Veer [algol68g@xs4all.nl].
8
9 //! @section License
10 //!
11 //! This program is free software; you can redistribute it and/or modify it
12 //! under the terms of the GNU General Public License as published by the
13 //! Free Software Foundation; either version 3 of the License, or
14 //! (at your option) any later version.
15 //!
16 //! This program is distributed in the hope that it will be useful, but
17 //! WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 //! or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 //! more details. You should have received a copy of the GNU General Public
20 //! License along with this program. If not, see [http://www.gnu.org/licenses/].
21
22 //! @section Synopsis
23 //!
24 //! Plugin compiler driver.
25
26 #include "a68g.h"
27
28 #include "a68g-optimiser.h"
29 #include "a68g-options.h"
30 #include "a68g-parser.h"
31 #include "a68g-prelude.h"
32 #include "a68g-plugin.h"
33 #include "a68g-genie.h"
34
35 //! @brief Emit code for plugin compiler.
36
37 void plugin_driver_code (void)
38 {
39 if (ERROR_COUNT (&A68G_JOB) == 0 && OPTION_OPT_LEVEL (&A68G_JOB) > NO_OPTIMISE) {
40 announce_phase ("plugin code generator");
41 int num = 0;
42 renumber_nodes (TOP_NODE (&A68G_JOB), &num);
43 A68G (node_register) = (NODE_T **) get_heap_space ((size_t) num * sizeof (NODE_T));
44 ABEND (A68G (node_register) == NO_REF, ERROR_ACTION, __func__);
45 register_nodes (TOP_NODE (&A68G_JOB));
46 FILE_OBJECT_FD (&A68G_JOB) = open (FILE_OBJECT_NAME (&A68G_JOB), O_WRONLY | O_CREAT | O_TRUNC, A68G_PROTECTION);
47 ABEND (FILE_OBJECT_FD (&A68G_JOB) == -1, ERROR_ACTION, FILE_OBJECT_NAME (&A68G_JOB));
48 FILE_OBJECT_OPENED (&A68G_JOB) = A68G_TRUE;
49 plugin_driver_emit (FILE_OBJECT_FD (&A68G_JOB));
50 ASSERT (close (FILE_OBJECT_FD (&A68G_JOB)) == 0);
51 FILE_OBJECT_OPENED (&A68G_JOB) = A68G_FALSE;
52 }
53 }
54
55 //! @brief Compile emitted code.
56
57 void plugin_driver_compile (void)
58 {
59 #if defined (BUILD_A68G_COMPILER)
60 // Compilation on Linux, BSD.
61 // Build shared library using gcc or clang.
62 // TODO: One day this should be all portable between platforms.
63 // Only compile if the A68 compiler found no errors (constant folder for instance).
64 if (ERROR_COUNT (&A68G_JOB) == 0 && OPTION_OPT_LEVEL (&A68G_JOB) > 0 && !OPTION_RUN_SCRIPT (&A68G_JOB)) {
65 BUFFER cmd, options;
66 BUFCLR (cmd);
67 BUFCLR (options);
68 if (OPTION_RERUN (&A68G_JOB) == A68G_FALSE) {
69 announce_phase ("plugin compiler");
70 errno = 0;
71 ASSERT (a68g_bufprt (options, SNPRINTF_SIZE, "%s %s", optimisation_option (), A68G_GCC_OPTIONS) >= 0);
72 #if defined (HAVE_PIC)
73 a68g_bufcat (options, " ", BUFFER_SIZE);
74 a68g_bufcat (options, HAVE_PIC, BUFFER_SIZE);
75 #endif
76
77 // Before Apple Silicon Mac:
78 //
79 // ASSERT (a68g_bufprt (cmd, SNPRINTF_SIZE, "%s -I%s %s -c -o \"%s\" \"%s\"", C_COMPILER, INCLUDE_DIR, options, FILE_BINARY_NAME (&A68G_JOB), FILE_OBJECT_NAME (&A68G_JOB)) >= 0);
80 // ABEND (system (cmd) != 0, ERROR_ACTION, cmd);
81 // ASSERT (a68g_bufprt (cmd, SNPRINTF_SIZE, "ld -export-dynamic -shared -o \"%s\" \"%s\"", FILE_PLUGIN_NAME (&A68G_JOB), FILE_BINARY_NAME (&A68G_JOB)) >= 0);
82 // ABEND (system (cmd) != 0, ERROR_ACTION, cmd);
83 //
84 // Apple Silicon Mac patches kindly provided by Neil Matthew.
85
86 ASSERT (a68g_bufprt (cmd, SNPRINTF_SIZE, "%s %s %s -c -o \"%s\" \"%s\"", C_COMPILER, INCLUDE_DIR, options, FILE_BINARY_NAME (&A68G_JOB), FILE_OBJECT_NAME (&A68G_JOB)) >= 0);
87 if (OPTION_VERBOSE (&A68G_JOB)) {
88 ASSERT (a68g_bufprt (A68G (output_line), SNPRINTF_SIZE, "%s: %s", A68G (a68g_cmd_name), cmd) >= 0);
89 io_close_tty_line ();
90 WRITE (A68G_STDOUT, A68G (output_line));
91 }
92 ABEND (system (cmd) != 0, ERROR_ACTION, cmd);
93 ASSERT (a68g_bufprt (cmd, SNPRINTF_SIZE, "ld %s -o \"%s\" \"%s\"", EXPORT_DYNAMIC_FLAGS, FILE_PLUGIN_NAME (&A68G_JOB), FILE_BINARY_NAME (&A68G_JOB)) >= 0);
94 if (OPTION_VERBOSE (&A68G_JOB)) {
95 ASSERT (a68g_bufprt (A68G (output_line), SNPRINTF_SIZE, "%s: %s", A68G (a68g_cmd_name), cmd) >= 0);
96 io_close_tty_line ();
97 WRITE (A68G_STDOUT, A68G (output_line));
98 }
99 ABEND (system (cmd) != 0, ERROR_ACTION, cmd);
100 a68g_rm (FILE_BINARY_NAME (&A68G_JOB));
101 }
102 }
103 #endif
104 }
105
106 //! @brief Start interpreter with compiled plugin.
107
108 void plugin_driver_genie (void)
109 {
110 #if defined (BUILD_A68G_COMPILER)
111 if (OPTION_RUN_SCRIPT (&A68G_JOB)) {
112 rewrite_script_source ();
113 }
114 void *compile_plugin = NULL;
115 if (OPTION_OPT_LEVEL (&A68G_JOB) > 0) {
116 char plugin_name[BUFFER_SIZE];
117 void *a68g_plugin;
118 struct stat srcstat, objstat;
119 int ret;
120 announce_phase ("plugin dynamic linker");
121 ASSERT (a68g_bufprt (plugin_name, SNPRINTF_SIZE, "%s", FILE_PLUGIN_NAME (&A68G_JOB)) >= 0);
122 // Correction when pwd is outside LD_PLUGIN_PATH.
123 // The DL cannot be loaded if it is.
124 if (strcmp (plugin_name, a68g_basename (plugin_name)) == 0) {
125 ASSERT (a68g_bufprt (plugin_name, SNPRINTF_SIZE, "./%s", FILE_PLUGIN_NAME (&A68G_JOB)) >= 0);
126 }
127 // Check whether we are doing something rash.
128 ret = stat (FILE_SOURCE_NAME (&A68G_JOB), &srcstat);
129 ABEND (ret != 0, ERROR_ACTION, FILE_SOURCE_NAME (&A68G_JOB));
130 ret = stat (plugin_name, &objstat);
131 ABEND (ret != 0, ERROR_ACTION, plugin_name);
132 if (OPTION_RERUN (&A68G_JOB)) {
133 ABEND (ST_MTIME (&srcstat) > ST_MTIME (&objstat), "plugin outdates source", "cannot RERUN");
134 }
135 // First load a68g itself so compiler code can resolve a68g symbols.
136 a68g_plugin = dlopen (NULL, RTLD_NOW | RTLD_GLOBAL);
137 ABEND (a68g_plugin == NULL, ERROR_CANNOT_OPEN_PLUGIN, dlerror ());
138 // Then load compiler code.
139 compile_plugin = dlopen (plugin_name, RTLD_NOW | RTLD_GLOBAL);
140 ABEND (compile_plugin == NULL, ERROR_CANNOT_OPEN_PLUGIN, dlerror ());
141 }
142 genie (compile_plugin);
143 // Unload compiler plugin.
144 if (compile_plugin != (void *) NULL) {
145 int ret = dlclose (compile_plugin);
146 ABEND (ret != 0, ERROR_ACTION, dlerror ());
147 }
148 #endif
149 }
150
151 //! @brief Clean files from plugin compiler.
152
153 void plugin_driver_clean (int emitted)
154 {
155 #if defined (BUILD_A68G_COMPILER)
156 announce_phase ("clean up intermediate files");
157 if (OPTION_OPT_LEVEL (&A68G_JOB) >= OPTIMISE_0 && OPTION_REGRESSION_TEST (&A68G_JOB) && !OPTION_KEEP (&A68G_JOB)) {
158 if (emitted) {
159 a68g_rm (FILE_OBJECT_NAME (&A68G_JOB));
160 }
161 a68g_rm (FILE_PLUGIN_NAME (&A68G_JOB));
162 }
163 if (OPTION_RUN_SCRIPT (&A68G_JOB) && !OPTION_KEEP (&A68G_JOB)) {
164 if (emitted) {
165 a68g_rm (FILE_OBJECT_NAME (&A68G_JOB));
166 }
167 a68g_rm (FILE_SOURCE_NAME (&A68G_JOB));
168 a68g_rm (FILE_PLUGIN_NAME (&A68G_JOB));
169 } else if (OPTION_COMPILE (&A68G_JOB)) {
170 build_script ();
171 if (!OPTION_KEEP (&A68G_JOB)) {
172 if (emitted) {
173 a68g_rm (FILE_OBJECT_NAME (&A68G_JOB));
174 }
175 a68g_rm (FILE_PLUGIN_NAME (&A68G_JOB));
176 }
177 } else if (OPTION_OPT_LEVEL (&A68G_JOB) == OPTIMISE_0 && !OPTION_KEEP (&A68G_JOB)) {
178 if (emitted) {
179 a68g_rm (FILE_OBJECT_NAME (&A68G_JOB));
180 }
181 a68g_rm (FILE_PLUGIN_NAME (&A68G_JOB));
182 } else if (OPTION_OPT_LEVEL (&A68G_JOB) > OPTIMISE_0 && !OPTION_KEEP (&A68G_JOB)) {
183 if (emitted) {
184 a68g_rm (FILE_OBJECT_NAME (&A68G_JOB));
185 }
186 } else if (OPTION_RERUN (&A68G_JOB) && !OPTION_KEEP (&A68G_JOB)) {
187 if (emitted) {
188 a68g_rm (FILE_OBJECT_NAME (&A68G_JOB));
189 }
190 }
191 #else
192 (void) emitted;
193 #endif
194 }
© 2002-2025 J.M. van der Veer (jmvdveer@xs4all.nl)
|