1627f7eb2Smrg /* Build executable statement trees.
2*4c3eb207Smrg Copyright (C) 2000-2020 Free Software Foundation, Inc.
3627f7eb2Smrg Contributed by Andy Vaught
4627f7eb2Smrg
5627f7eb2Smrg This file is part of GCC.
6627f7eb2Smrg
7627f7eb2Smrg GCC is free software; you can redistribute it and/or modify it under
8627f7eb2Smrg the terms of the GNU General Public License as published by the Free
9627f7eb2Smrg Software Foundation; either version 3, or (at your option) any later
10627f7eb2Smrg version.
11627f7eb2Smrg
12627f7eb2Smrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13627f7eb2Smrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
14627f7eb2Smrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15627f7eb2Smrg for more details.
16627f7eb2Smrg
17627f7eb2Smrg You should have received a copy of the GNU General Public License
18627f7eb2Smrg along with GCC; see the file COPYING3. If not see
19627f7eb2Smrg <http://www.gnu.org/licenses/>. */
20627f7eb2Smrg
21627f7eb2Smrg /* Executable statements are strung together into a singly linked list
22627f7eb2Smrg of code structures. These structures are later translated into GCC
23627f7eb2Smrg GENERIC tree structures and from there to executable code for a
24627f7eb2Smrg target. */
25627f7eb2Smrg
26627f7eb2Smrg #include "config.h"
27627f7eb2Smrg #include "system.h"
28627f7eb2Smrg #include "coretypes.h"
29627f7eb2Smrg #include "gfortran.h"
30627f7eb2Smrg
31627f7eb2Smrg gfc_code new_st;
32627f7eb2Smrg
33627f7eb2Smrg
34627f7eb2Smrg /* Zeroes out the new_st structure. */
35627f7eb2Smrg
36627f7eb2Smrg void
gfc_clear_new_st(void)37627f7eb2Smrg gfc_clear_new_st (void)
38627f7eb2Smrg {
39627f7eb2Smrg memset (&new_st, '\0', sizeof (new_st));
40627f7eb2Smrg new_st.op = EXEC_NOP;
41627f7eb2Smrg }
42627f7eb2Smrg
43627f7eb2Smrg
44627f7eb2Smrg /* Get a gfc_code structure, initialized with the current locus
45627f7eb2Smrg and a statement code 'op'. */
46627f7eb2Smrg
47627f7eb2Smrg gfc_code *
gfc_get_code(gfc_exec_op op)48627f7eb2Smrg gfc_get_code (gfc_exec_op op)
49627f7eb2Smrg {
50627f7eb2Smrg gfc_code *c;
51627f7eb2Smrg
52627f7eb2Smrg c = XCNEW (gfc_code);
53627f7eb2Smrg c->op = op;
54627f7eb2Smrg c->loc = gfc_current_locus;
55627f7eb2Smrg return c;
56627f7eb2Smrg }
57627f7eb2Smrg
58627f7eb2Smrg
59627f7eb2Smrg /* Given some part of a gfc_code structure, append a set of code to
60627f7eb2Smrg its tail, returning a pointer to the new tail. */
61627f7eb2Smrg
62627f7eb2Smrg gfc_code *
gfc_append_code(gfc_code * tail,gfc_code * new_code)63627f7eb2Smrg gfc_append_code (gfc_code *tail, gfc_code *new_code)
64627f7eb2Smrg {
65627f7eb2Smrg if (tail != NULL)
66627f7eb2Smrg {
67627f7eb2Smrg while (tail->next != NULL)
68627f7eb2Smrg tail = tail->next;
69627f7eb2Smrg
70627f7eb2Smrg tail->next = new_code;
71627f7eb2Smrg }
72627f7eb2Smrg
73627f7eb2Smrg while (new_code->next != NULL)
74627f7eb2Smrg new_code = new_code->next;
75627f7eb2Smrg
76627f7eb2Smrg return new_code;
77627f7eb2Smrg }
78627f7eb2Smrg
79627f7eb2Smrg
80627f7eb2Smrg /* Free a single code structure, but not the actual structure itself. */
81627f7eb2Smrg
82627f7eb2Smrg void
gfc_free_statement(gfc_code * p)83627f7eb2Smrg gfc_free_statement (gfc_code *p)
84627f7eb2Smrg {
85627f7eb2Smrg if (p->expr1)
86627f7eb2Smrg gfc_free_expr (p->expr1);
87627f7eb2Smrg if (p->expr2)
88627f7eb2Smrg gfc_free_expr (p->expr2);
89627f7eb2Smrg
90627f7eb2Smrg switch (p->op)
91627f7eb2Smrg {
92627f7eb2Smrg case EXEC_NOP:
93627f7eb2Smrg case EXEC_END_BLOCK:
94627f7eb2Smrg case EXEC_END_NESTED_BLOCK:
95627f7eb2Smrg case EXEC_ASSIGN:
96627f7eb2Smrg case EXEC_INIT_ASSIGN:
97627f7eb2Smrg case EXEC_GOTO:
98627f7eb2Smrg case EXEC_CYCLE:
99627f7eb2Smrg case EXEC_RETURN:
100627f7eb2Smrg case EXEC_END_PROCEDURE:
101627f7eb2Smrg case EXEC_IF:
102627f7eb2Smrg case EXEC_PAUSE:
103627f7eb2Smrg case EXEC_STOP:
104627f7eb2Smrg case EXEC_ERROR_STOP:
105627f7eb2Smrg case EXEC_EXIT:
106627f7eb2Smrg case EXEC_WHERE:
107627f7eb2Smrg case EXEC_IOLENGTH:
108627f7eb2Smrg case EXEC_POINTER_ASSIGN:
109627f7eb2Smrg case EXEC_DO_WHILE:
110627f7eb2Smrg case EXEC_CONTINUE:
111627f7eb2Smrg case EXEC_TRANSFER:
112627f7eb2Smrg case EXEC_LABEL_ASSIGN:
113627f7eb2Smrg case EXEC_ENTRY:
114627f7eb2Smrg case EXEC_ARITHMETIC_IF:
115627f7eb2Smrg case EXEC_CRITICAL:
116627f7eb2Smrg case EXEC_SYNC_ALL:
117627f7eb2Smrg case EXEC_SYNC_IMAGES:
118627f7eb2Smrg case EXEC_SYNC_MEMORY:
119627f7eb2Smrg case EXEC_LOCK:
120627f7eb2Smrg case EXEC_UNLOCK:
121627f7eb2Smrg case EXEC_EVENT_POST:
122627f7eb2Smrg case EXEC_EVENT_WAIT:
123627f7eb2Smrg case EXEC_FAIL_IMAGE:
124627f7eb2Smrg case EXEC_CHANGE_TEAM:
125627f7eb2Smrg case EXEC_END_TEAM:
126627f7eb2Smrg case EXEC_FORM_TEAM:
127627f7eb2Smrg case EXEC_SYNC_TEAM:
128627f7eb2Smrg break;
129627f7eb2Smrg
130627f7eb2Smrg case EXEC_BLOCK:
131627f7eb2Smrg gfc_free_namespace (p->ext.block.ns);
132627f7eb2Smrg gfc_free_association_list (p->ext.block.assoc);
133627f7eb2Smrg break;
134627f7eb2Smrg
135627f7eb2Smrg case EXEC_COMPCALL:
136627f7eb2Smrg case EXEC_CALL_PPC:
137627f7eb2Smrg case EXEC_CALL:
138627f7eb2Smrg case EXEC_ASSIGN_CALL:
139627f7eb2Smrg gfc_free_actual_arglist (p->ext.actual);
140627f7eb2Smrg break;
141627f7eb2Smrg
142627f7eb2Smrg case EXEC_SELECT:
143627f7eb2Smrg case EXEC_SELECT_TYPE:
144*4c3eb207Smrg case EXEC_SELECT_RANK:
145627f7eb2Smrg if (p->ext.block.case_list)
146627f7eb2Smrg gfc_free_case_list (p->ext.block.case_list);
147627f7eb2Smrg break;
148627f7eb2Smrg
149627f7eb2Smrg case EXEC_DO:
150627f7eb2Smrg gfc_free_iterator (p->ext.iterator, 1);
151627f7eb2Smrg break;
152627f7eb2Smrg
153627f7eb2Smrg case EXEC_ALLOCATE:
154627f7eb2Smrg case EXEC_DEALLOCATE:
155627f7eb2Smrg gfc_free_alloc_list (p->ext.alloc.list);
156627f7eb2Smrg break;
157627f7eb2Smrg
158627f7eb2Smrg case EXEC_OPEN:
159627f7eb2Smrg gfc_free_open (p->ext.open);
160627f7eb2Smrg break;
161627f7eb2Smrg
162627f7eb2Smrg case EXEC_CLOSE:
163627f7eb2Smrg gfc_free_close (p->ext.close);
164627f7eb2Smrg break;
165627f7eb2Smrg
166627f7eb2Smrg case EXEC_BACKSPACE:
167627f7eb2Smrg case EXEC_ENDFILE:
168627f7eb2Smrg case EXEC_REWIND:
169627f7eb2Smrg case EXEC_FLUSH:
170627f7eb2Smrg gfc_free_filepos (p->ext.filepos);
171627f7eb2Smrg break;
172627f7eb2Smrg
173627f7eb2Smrg case EXEC_INQUIRE:
174627f7eb2Smrg gfc_free_inquire (p->ext.inquire);
175627f7eb2Smrg break;
176627f7eb2Smrg
177627f7eb2Smrg case EXEC_WAIT:
178627f7eb2Smrg gfc_free_wait (p->ext.wait);
179627f7eb2Smrg break;
180627f7eb2Smrg
181627f7eb2Smrg case EXEC_READ:
182627f7eb2Smrg case EXEC_WRITE:
183627f7eb2Smrg gfc_free_dt (p->ext.dt);
184627f7eb2Smrg break;
185627f7eb2Smrg
186627f7eb2Smrg case EXEC_DT_END:
187627f7eb2Smrg /* The ext.dt member is a duplicate pointer and doesn't need to
188627f7eb2Smrg be freed. */
189627f7eb2Smrg break;
190627f7eb2Smrg
191627f7eb2Smrg case EXEC_DO_CONCURRENT:
192627f7eb2Smrg case EXEC_FORALL:
193627f7eb2Smrg gfc_free_forall_iterator (p->ext.forall_iterator);
194627f7eb2Smrg break;
195627f7eb2Smrg
196627f7eb2Smrg case EXEC_OACC_DECLARE:
197627f7eb2Smrg if (p->ext.oacc_declare)
198627f7eb2Smrg gfc_free_oacc_declare_clauses (p->ext.oacc_declare);
199627f7eb2Smrg break;
200627f7eb2Smrg
201627f7eb2Smrg case EXEC_OACC_PARALLEL_LOOP:
202627f7eb2Smrg case EXEC_OACC_PARALLEL:
203627f7eb2Smrg case EXEC_OACC_KERNELS_LOOP:
204627f7eb2Smrg case EXEC_OACC_KERNELS:
205*4c3eb207Smrg case EXEC_OACC_SERIAL_LOOP:
206*4c3eb207Smrg case EXEC_OACC_SERIAL:
207627f7eb2Smrg case EXEC_OACC_DATA:
208627f7eb2Smrg case EXEC_OACC_HOST_DATA:
209627f7eb2Smrg case EXEC_OACC_LOOP:
210627f7eb2Smrg case EXEC_OACC_UPDATE:
211627f7eb2Smrg case EXEC_OACC_WAIT:
212627f7eb2Smrg case EXEC_OACC_CACHE:
213627f7eb2Smrg case EXEC_OACC_ENTER_DATA:
214627f7eb2Smrg case EXEC_OACC_EXIT_DATA:
215627f7eb2Smrg case EXEC_OACC_ROUTINE:
216627f7eb2Smrg case EXEC_OMP_CANCEL:
217627f7eb2Smrg case EXEC_OMP_CANCELLATION_POINT:
218627f7eb2Smrg case EXEC_OMP_CRITICAL:
219627f7eb2Smrg case EXEC_OMP_DISTRIBUTE:
220627f7eb2Smrg case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
221627f7eb2Smrg case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
222627f7eb2Smrg case EXEC_OMP_DISTRIBUTE_SIMD:
223627f7eb2Smrg case EXEC_OMP_DO:
224627f7eb2Smrg case EXEC_OMP_DO_SIMD:
225627f7eb2Smrg case EXEC_OMP_END_SINGLE:
226627f7eb2Smrg case EXEC_OMP_ORDERED:
227627f7eb2Smrg case EXEC_OMP_PARALLEL:
228627f7eb2Smrg case EXEC_OMP_PARALLEL_DO:
229627f7eb2Smrg case EXEC_OMP_PARALLEL_DO_SIMD:
230627f7eb2Smrg case EXEC_OMP_PARALLEL_SECTIONS:
231627f7eb2Smrg case EXEC_OMP_PARALLEL_WORKSHARE:
232627f7eb2Smrg case EXEC_OMP_SECTIONS:
233627f7eb2Smrg case EXEC_OMP_SIMD:
234627f7eb2Smrg case EXEC_OMP_SINGLE:
235627f7eb2Smrg case EXEC_OMP_TARGET:
236627f7eb2Smrg case EXEC_OMP_TARGET_DATA:
237627f7eb2Smrg case EXEC_OMP_TARGET_ENTER_DATA:
238627f7eb2Smrg case EXEC_OMP_TARGET_EXIT_DATA:
239627f7eb2Smrg case EXEC_OMP_TARGET_PARALLEL:
240627f7eb2Smrg case EXEC_OMP_TARGET_PARALLEL_DO:
241627f7eb2Smrg case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
242627f7eb2Smrg case EXEC_OMP_TARGET_SIMD:
243627f7eb2Smrg case EXEC_OMP_TARGET_TEAMS:
244627f7eb2Smrg case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
245627f7eb2Smrg case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
246627f7eb2Smrg case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
247627f7eb2Smrg case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
248627f7eb2Smrg case EXEC_OMP_TARGET_UPDATE:
249627f7eb2Smrg case EXEC_OMP_TASK:
250627f7eb2Smrg case EXEC_OMP_TASKLOOP:
251627f7eb2Smrg case EXEC_OMP_TASKLOOP_SIMD:
252627f7eb2Smrg case EXEC_OMP_TEAMS:
253627f7eb2Smrg case EXEC_OMP_TEAMS_DISTRIBUTE:
254627f7eb2Smrg case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
255627f7eb2Smrg case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
256627f7eb2Smrg case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
257627f7eb2Smrg case EXEC_OMP_WORKSHARE:
258627f7eb2Smrg gfc_free_omp_clauses (p->ext.omp_clauses);
259627f7eb2Smrg break;
260627f7eb2Smrg
261627f7eb2Smrg case EXEC_OMP_END_CRITICAL:
262627f7eb2Smrg free (CONST_CAST (char *, p->ext.omp_name));
263627f7eb2Smrg break;
264627f7eb2Smrg
265627f7eb2Smrg case EXEC_OMP_FLUSH:
266627f7eb2Smrg gfc_free_omp_namelist (p->ext.omp_namelist);
267627f7eb2Smrg break;
268627f7eb2Smrg
269627f7eb2Smrg case EXEC_OACC_ATOMIC:
270627f7eb2Smrg case EXEC_OMP_ATOMIC:
271627f7eb2Smrg case EXEC_OMP_BARRIER:
272627f7eb2Smrg case EXEC_OMP_MASTER:
273627f7eb2Smrg case EXEC_OMP_END_NOWAIT:
274627f7eb2Smrg case EXEC_OMP_TASKGROUP:
275627f7eb2Smrg case EXEC_OMP_TASKWAIT:
276627f7eb2Smrg case EXEC_OMP_TASKYIELD:
277627f7eb2Smrg break;
278627f7eb2Smrg
279627f7eb2Smrg default:
280627f7eb2Smrg gfc_internal_error ("gfc_free_statement(): Bad statement");
281627f7eb2Smrg }
282627f7eb2Smrg }
283627f7eb2Smrg
284627f7eb2Smrg
285627f7eb2Smrg /* Free a code statement and all other code structures linked to it. */
286627f7eb2Smrg
287627f7eb2Smrg void
gfc_free_statements(gfc_code * p)288627f7eb2Smrg gfc_free_statements (gfc_code *p)
289627f7eb2Smrg {
290627f7eb2Smrg gfc_code *q;
291627f7eb2Smrg
292627f7eb2Smrg for (; p; p = q)
293627f7eb2Smrg {
294627f7eb2Smrg q = p->next;
295627f7eb2Smrg
296627f7eb2Smrg if (p->block)
297627f7eb2Smrg gfc_free_statements (p->block);
298627f7eb2Smrg gfc_free_statement (p);
299627f7eb2Smrg free (p);
300627f7eb2Smrg }
301627f7eb2Smrg }
302627f7eb2Smrg
303627f7eb2Smrg
304627f7eb2Smrg /* Free an association list (of an ASSOCIATE statement). */
305627f7eb2Smrg
306627f7eb2Smrg void
gfc_free_association_list(gfc_association_list * assoc)307627f7eb2Smrg gfc_free_association_list (gfc_association_list* assoc)
308627f7eb2Smrg {
309627f7eb2Smrg if (!assoc)
310627f7eb2Smrg return;
311627f7eb2Smrg
312627f7eb2Smrg gfc_free_association_list (assoc->next);
313627f7eb2Smrg free (assoc);
314627f7eb2Smrg }
315