Branch data Line data Source code
1 : : /* m2pp.c pretty print trees, output in Modula-2 where possible.
2 : :
3 : : Copyright (C) 2007-2024 Free Software Foundation, Inc.
4 : : Contributed by Gaius Mulley <gaius@glam.ac.uk>.
5 : :
6 : : This file is part of GNU Modula-2.
7 : :
8 : : GNU Modula-2 is free software; you can redistribute it and/or modify
9 : : it under the terms of the GNU General Public License as published by
10 : : the Free Software Foundation; either version 3, or (at your option)
11 : : any later version.
12 : :
13 : : GNU Modula-2 is distributed in the hope that it will be useful, but
14 : : WITHOUT ANY WARRANTY; without even the implied warranty of
15 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : : General Public License for more details.
17 : :
18 : : You should have received a copy of the GNU General Public License
19 : : along with GNU Modula-2; see the file COPYING3. If not see
20 : : <http://www.gnu.org/licenses/>. */
21 : :
22 : : #include "gcc-consolidation.h"
23 : :
24 : : #include "../m2-tree.h"
25 : : #include "../gm2-lang.h"
26 : :
27 : : #include "m2tree.h"
28 : : #include "m2expr.h"
29 : : #include "m2type.h"
30 : : #include "m2decl.h"
31 : : #include "m2options.h"
32 : : #include "m2langdump.h"
33 : :
34 : : #define M2PP_C
35 : : #include "m2pp.h"
36 : :
37 : : #define GM2
38 : :
39 : : const char *m2pp_dump_description[M2PP_DUMP_END] =
40 : : {
41 : : "interactive user invoked output",
42 : : "modula-2 gimple trees pre genercize",
43 : : "modula-2 gimple trees post genercize",
44 : : };
45 : :
46 : : namespace modula2 {
47 : :
48 : : #undef DEBUGGING
49 : :
50 : : typedef struct pretty_t
51 : : {
52 : : m2pp_dump_kind output;
53 : : bool needs_space;
54 : : bool needs_indent;
55 : : int curpos;
56 : : int indent;
57 : : bool issued_begin;
58 : : bool in_vars;
59 : : bool in_types;
60 : : tree block;
61 : : int bits;
62 : : } pretty;
63 : :
64 : : typedef struct m2stack_t
65 : : {
66 : : tree value;
67 : : struct m2stack_t *next;
68 : : } stack;
69 : :
70 : : /* Prototypes. */
71 : :
72 : : static pretty *initPretty (m2pp_dump_kind kind, int bits);
73 : : static pretty *dupPretty (pretty *s);
74 : : static int getindent (pretty *s);
75 : : static void setindent (pretty *s, int n);
76 : : static int getcurpos (pretty *s);
77 : : static void m2pp_identifier (pretty *s, tree t);
78 : : static void m2pp_needspace (pretty *s);
79 : : static void m2pp_function (pretty *s, tree t);
80 : : static void m2pp_function_header (pretty *s, tree t);
81 : : static void m2pp_function_vars (pretty *s, tree t);
82 : : static void m2pp_statement_sequence (pretty *s, tree t);
83 : : static void m2pp_print (pretty *s, const char *p);
84 : : static void m2pp_print_char (pretty *s, char ch);
85 : : static void m2pp_parameter (pretty *s, tree t);
86 : : static void m2pp_type (pretty *s, tree t);
87 : : static void m2pp_ident_pointer (pretty *s, tree t);
88 : : static void m2pp_set_type (pretty *s, tree t);
89 : : static void m2pp_enum (pretty *s, tree t);
90 : : static void m2pp_array (pretty *s, tree t);
91 : : static void m2pp_subrange (pretty *s, tree t);
92 : : static void m2pp_gimpified (pretty *s, tree t);
93 : : static void m2pp_pointer_type (pretty *s, tree t);
94 : : static void m2pp_record_type (pretty *s, tree t);
95 : : static void m2pp_union_type (pretty *s, tree t);
96 : : static void m2pp_simple_type (pretty *s, tree t);
97 : : static void m2pp_expression (pretty *s, tree t);
98 : : static void m2pp_relop (pretty *s, tree t, const char *p);
99 : : static void m2pp_simple_expression (pretty *s, tree t);
100 : : static void m2pp_statement_sequence (pretty *s, tree t);
101 : : static void m2pp_unknown (pretty *s, const char *s1, const char *s2);
102 : : static void m2pp_statement (pretty *s, tree t);
103 : : static void m2pp_assignment (pretty *s, tree t);
104 : : static void m2pp_designator (pretty *s, tree t);
105 : : static void m2pp_conditional (pretty *s, tree t);
106 : : static void m2pp_label_expr (pretty *s, tree t);
107 : : static void m2pp_label_decl (pretty *s, tree t);
108 : : static void m2pp_goto (pretty *s, tree t);
109 : : static void m2pp_list (pretty *s, tree t);
110 : : static void m2pp_offset (pretty *s, tree t);
111 : : static void m2pp_indirect_ref (pretty *s, tree t);
112 : : static void m2pp_integer_cst (pretty *s, tree t);
113 : : static void m2pp_real_cst (pretty *s, tree t);
114 : : static void m2pp_string_cst (pretty *s, tree t);
115 : : static void m2pp_integer (pretty *s, tree t);
116 : : static void m2pp_addr_expr (pretty *s, tree t);
117 : : static void m2pp_nop (pretty *s, tree t);
118 : : static void m2pp_convert (pretty *s, tree t);
119 : : static void m2pp_var_decl (pretty *s, tree t);
120 : : static void m2pp_binary (pretty *s, tree t, const char *p);
121 : : static void m2pp_unary (pretty *s, tree t, const char *p);
122 : : static void m2pp_call_expr (pretty *s, tree t);
123 : : static void m2pp_procedure_call (pretty *s, tree t);
124 : : static void m2pp_ssa (pretty *s, tree t);
125 : : static void m2pp_block (pretty *s, tree t);
126 : : static void m2pp_block_list (pretty *s, tree t);
127 : : static void m2pp_var_list (pretty *s, tree t);
128 : : static void m2pp_bind_expr (pretty *s, tree t);
129 : : static void m2pp_return_expr (pretty *s, tree t);
130 : : static void m2pp_result_decl (pretty *s, tree t);
131 : : static void m2pp_try_block (pretty *s, tree t);
132 : : static void m2pp_cleanup_point_expr (pretty *s, tree t);
133 : : static void m2pp_handler (pretty *s, tree t);
134 : : static void m2pp_component_ref (pretty *s, tree t);
135 : : static void m2pp_array_ref (pretty *s, tree t);
136 : : static void m2pp_begin (pretty *s);
137 : : static void m2pp_var (pretty *s);
138 : : static void m2pp_types (pretty *s);
139 : : static void m2pp_decl_expr (pretty *s, tree t);
140 : : static void m2pp_var_type_decl (pretty *s, tree t);
141 : : static void m2pp_non_lvalue_expr (pretty *s, tree t);
142 : : static void m2pp_procedure_type (pretty *s, tree t);
143 : : static void m2pp_param_type (pretty *s, tree t);
144 : : static void m2pp_type_lowlevel (pretty *s, tree t);
145 : : static void m2pp_try_catch_expr (pretty *s, tree t);
146 : : static void m2pp_throw (pretty *s, tree t);
147 : : static void m2pp_catch_expr (pretty *s, tree t);
148 : : static void m2pp_try_finally_expr (pretty *s, tree t);
149 : : static void m2pp_complex (pretty *s, tree t);
150 : : static void killPretty (pretty *s);
151 : : static void m2pp_compound_expression (pretty *s, tree t);
152 : : static void m2pp_target_expression (pretty *s, tree t);
153 : : static void m2pp_constructor (pretty *s, tree t);
154 : : static void m2pp_translation (pretty *s, tree t);
155 : : static void m2pp_module_block (pretty *s, tree t);
156 : : static void push (tree t);
157 : : static void pop (void);
158 : : static bool begin_printed (tree t);
159 : : static void m2pp_decl_list (pretty *s, tree t);
160 : : static void m2pp_loc (pretty *s, tree t);
161 : : static FILE *getoutput (pretty *s);
162 : :
163 : :
164 : : void pet (tree t);
165 : : void m2pp_integer (pretty *s, tree t);
166 : :
167 : : extern void stop (void);
168 : :
169 : : static stack *stackPtr = NULL;
170 : : static FILE *m2pp_output_file[M2PP_DUMP_END];
171 : :
172 : : /* do_pf helper function for pf. */
173 : :
174 : : void
175 : 0 : do_pf (tree t, int bits)
176 : : {
177 : 0 : pretty *state = initPretty (M2PP_DUMP_STDOUT, bits);
178 : :
179 : 0 : if (TREE_CODE (t) == TRANSLATION_UNIT_DECL)
180 : 0 : m2pp_translation (state, t);
181 : 0 : else if (TREE_CODE (t) == BLOCK)
182 : 0 : m2pp_module_block (state, t);
183 : 0 : else if (TREE_CODE (t) == FUNCTION_DECL)
184 : 0 : m2pp_function (state, t);
185 : : else
186 : 0 : m2pp_statement_sequence (state, t);
187 : 0 : killPretty (state);
188 : 0 : }
189 : :
190 : : /* pf print function. Expected to be printed interactively from
191 : : the debugger: print modula2::pf(func), or to be called from code. */
192 : :
193 : : void
194 : 0 : pf (tree t)
195 : : {
196 : 0 : do_pf (t, false);
197 : 0 : }
198 : :
199 : : /* pe print expression. Expected to be printed interactively from
200 : : the debugger: print modula2::pe(expr), or to be called from code. */
201 : :
202 : : void
203 : 0 : pe (tree t)
204 : : {
205 : 0 : pretty *state = initPretty (M2PP_DUMP_STDOUT, 0);
206 : :
207 : 0 : m2pp_expression (state, t);
208 : 0 : m2pp_needspace (state);
209 : 0 : m2pp_print (state, ";\n");
210 : 0 : killPretty (state);
211 : 0 : }
212 : :
213 : : /* pet print expression and its type. Expected to be printed
214 : : interactively from the debugger: print modula2::pet(expr), or to
215 : : be called from code. */
216 : :
217 : : void
218 : 0 : pet (tree t)
219 : : {
220 : 0 : pretty *state = initPretty (M2PP_DUMP_STDOUT, 0);
221 : :
222 : 0 : m2pp_expression (state, t);
223 : 0 : m2pp_needspace (state);
224 : 0 : m2pp_print (state, ":");
225 : 0 : m2pp_type (state, TREE_TYPE (t));
226 : 0 : m2pp_print (state, ";\n");
227 : 0 : killPretty (state);
228 : 0 : }
229 : :
230 : : /* pt print type. Expected to be printed interactively from the
231 : : debugger: print pt(expr), or to be called from code. */
232 : :
233 : : void
234 : 0 : pt (tree t)
235 : : {
236 : 0 : pretty *state = initPretty (M2PP_DUMP_STDOUT, 0);
237 : 0 : m2pp_type (state, t);
238 : 0 : m2pp_needspace (state);
239 : 0 : m2pp_print (state, ";\n");
240 : 0 : killPretty (state);
241 : 0 : }
242 : :
243 : : /* ptl print type low level. Expected to be printed interactively
244 : : from the debugger: print ptl(type), or to be called from code. */
245 : :
246 : : void
247 : 0 : ptl (tree t)
248 : : {
249 : 0 : pretty *state = initPretty (M2PP_DUMP_STDOUT, 0);
250 : 0 : m2pp_type_lowlevel (state, t);
251 : 0 : m2pp_needspace (state);
252 : 0 : m2pp_print (state, ";\n");
253 : 0 : killPretty (state);
254 : 0 : }
255 : :
256 : : /* ptcl print TREE_CHAINed list. */
257 : :
258 : : void
259 : 0 : ptcl (tree t)
260 : : {
261 : 0 : pretty *state = initPretty (M2PP_DUMP_STDOUT, 0);
262 : :
263 : 0 : m2pp_decl_list (state, t);
264 : 0 : m2pp_print (state, "\n");
265 : 0 : killPretty (state);
266 : 0 : }
267 : :
268 : : /* loc if tree has a location then display it within a comment. */
269 : :
270 : : static void
271 : 0 : m2pp_loc (pretty *s, tree t)
272 : : {
273 : 0 : if (CAN_HAVE_LOCATION_P (t))
274 : : {
275 : 0 : if (EXPR_HAS_LOCATION (t))
276 : : {
277 : 0 : if (EXPR_LOCATION (t) == UNKNOWN_LOCATION)
278 : 0 : m2pp_print (s, "(* missing location1 *)\n");
279 : : else
280 : : {
281 : 0 : expanded_location l = expand_location (EXPR_LOCATION (t));
282 : :
283 : 0 : m2pp_print (s, "(* ");
284 : 0 : m2pp_print (s, l.file);
285 : 0 : m2pp_print (s, ":");
286 : 0 : fprintf (getoutput (s), "%d", l.line);
287 : 0 : m2pp_print (s, " *)");
288 : 0 : m2pp_print (s, "\n");
289 : : }
290 : : }
291 : : else
292 : : {
293 : 0 : m2pp_print (s, "(* missing location2 *)\n");
294 : : }
295 : : }
296 : 0 : }
297 : :
298 : : /* m2pp_decl_list prints a TREE_CHAINed list for a decl node. */
299 : :
300 : : static void
301 : 0 : m2pp_decl_list (pretty *s, tree t)
302 : : {
303 : 0 : tree u = t;
304 : :
305 : 0 : m2pp_print (s, "(");
306 : 0 : m2pp_needspace (s);
307 : 0 : while (t != NULL_TREE)
308 : : {
309 : 0 : m2pp_identifier (s, t);
310 : 0 : t = TREE_CHAIN (t);
311 : 0 : if (t == u || t == NULL_TREE)
312 : : break;
313 : 0 : m2pp_print (s, ",");
314 : 0 : m2pp_needspace (s);
315 : : }
316 : 0 : m2pp_needspace (s);
317 : 0 : m2pp_print (s, ")");
318 : 0 : }
319 : :
320 : : static void
321 : 0 : m2pp_decl_bool (pretty *s, tree t)
322 : : {
323 : 0 : if (TREE_STATIC (t))
324 : 0 : m2pp_print (s, "static, ");
325 : 0 : if (DECL_EXTERNAL (t))
326 : 0 : m2pp_print (s, "external, ");
327 : 0 : if (DECL_SEEN_IN_BIND_EXPR_P (t))
328 : 0 : m2pp_print (s, "in bind expr, ");
329 : 0 : }
330 : :
331 : : void
332 : 0 : pv (tree t)
333 : : {
334 : 0 : if (t)
335 : : {
336 : 0 : enum tree_code code = TREE_CODE (t);
337 : :
338 : 0 : if (code == PARM_DECL)
339 : : {
340 : 0 : pretty *state = initPretty (M2PP_DUMP_STDOUT, 0);
341 : 0 : m2pp_identifier (state, t);
342 : 0 : m2pp_needspace (state);
343 : 0 : m2pp_print (state, "<parm_decl context = ");
344 : 0 : m2pp_identifier (state, DECL_CONTEXT (t));
345 : 0 : if (DECL_ABSTRACT_ORIGIN (t) == t)
346 : 0 : m2pp_print (state, ">\n");
347 : : else
348 : : {
349 : 0 : m2pp_print (state, ", abstract origin = ");
350 : 0 : m2pp_identifier (state, DECL_ABSTRACT_ORIGIN (t));
351 : 0 : m2pp_print (state, ">\n");
352 : 0 : modula2::pv (DECL_ABSTRACT_ORIGIN (t));
353 : : }
354 : 0 : killPretty (state);
355 : : }
356 : 0 : if (code == VAR_DECL)
357 : : {
358 : 0 : pretty *state = initPretty (M2PP_DUMP_STDOUT, 0);
359 : 0 : m2pp_identifier (state, t);
360 : 0 : m2pp_needspace (state);
361 : 0 : m2pp_print (state, "(* <var_decl context = ");
362 : 0 : m2pp_identifier (state, DECL_CONTEXT (t));
363 : 0 : m2pp_decl_bool (state, t);
364 : 0 : if (DECL_ABSTRACT_ORIGIN (t) == t)
365 : 0 : m2pp_print (state, "> *)\n");
366 : : else
367 : : {
368 : 0 : m2pp_print (state, ", abstract origin = ");
369 : 0 : m2pp_identifier (state, DECL_ABSTRACT_ORIGIN (t));
370 : 0 : m2pp_print (state, "> *)\n");
371 : 0 : modula2::pv (DECL_ABSTRACT_ORIGIN (t));
372 : : }
373 : 0 : killPretty (state);
374 : : }
375 : : }
376 : 0 : }
377 : :
378 : : #if defined(GM2_MAINTAINER)
379 : :
380 : : /* remember an internal debugging hook. */
381 : : static tree rememberF = NULL;
382 : :
383 : : static void
384 : : remember (tree t)
385 : : {
386 : : rememberF = t;
387 : : printf ("type: watch *((tree *) %p) != %p\n", (void *)&DECL_SAVED_TREE (t),
388 : : (void *)DECL_SAVED_TREE (t));
389 : : }
390 : : #endif
391 : :
392 : : /* push pushes tree t onto stack. */
393 : :
394 : : static void
395 : 0 : push (tree t)
396 : : {
397 : 0 : stack *s = (stack *)xmalloc (sizeof (stack));
398 : :
399 : 0 : s->value = t;
400 : 0 : s->next = stackPtr;
401 : 0 : stackPtr = s;
402 : 0 : }
403 : :
404 : : /* pop pops a tree, from the stack. */
405 : :
406 : : static void
407 : 0 : pop (void)
408 : : {
409 : 0 : stack *s = stackPtr;
410 : :
411 : 0 : stackPtr = stackPtr->next;
412 : 0 : free (s);
413 : 0 : }
414 : :
415 : : /* being_printed returns true if t is held on the stack. */
416 : :
417 : : static bool
418 : 0 : begin_printed (tree t)
419 : : {
420 : 0 : stack *s = stackPtr;
421 : :
422 : 0 : while (s != NULL)
423 : : {
424 : 0 : if (s->value == t)
425 : : return true;
426 : : else
427 : 0 : s = s->next;
428 : : }
429 : : return false;
430 : : }
431 : :
432 : : /* dupPretty duplicate and return a copy of state s. */
433 : :
434 : : static pretty *
435 : 0 : dupPretty (pretty *s)
436 : : {
437 : 0 : pretty *p = initPretty (s->output, s->bits);
438 : 0 : *p = *s;
439 : 0 : return p;
440 : : }
441 : :
442 : : /* initPretty initialise the state of the pretty printer. */
443 : :
444 : : static pretty *
445 : 0 : initPretty (m2pp_dump_kind kind, int bits)
446 : : {
447 : 0 : pretty *state = (pretty *)xmalloc (sizeof (pretty));
448 : 0 : state->output = kind;
449 : 0 : state->needs_space = false;
450 : 0 : state->needs_indent = false;
451 : 0 : state->curpos = 0;
452 : 0 : state->indent = 0;
453 : 0 : state->issued_begin = false;
454 : 0 : state->in_vars = false;
455 : 0 : state->in_types = false;
456 : 0 : state->block = NULL_TREE;
457 : 0 : state->bits = bits;
458 : 0 : return state;
459 : : }
460 : :
461 : : /* killPretty cleans up the state. */
462 : :
463 : : static void
464 : 0 : killPretty (pretty *s)
465 : : {
466 : 0 : fflush (getoutput (s));
467 : 0 : free (s);
468 : 0 : }
469 : :
470 : : /* getindent returns the current indent value. */
471 : :
472 : : static int
473 : 0 : getindent (pretty *s)
474 : : {
475 : 0 : return s->indent;
476 : : }
477 : :
478 : : /* setindent sets the current indent to, n. */
479 : :
480 : : static void
481 : 0 : setindent (pretty *s, int n)
482 : : {
483 : 0 : s->indent = n;
484 : 0 : }
485 : :
486 : : /* getcurpos returns the current cursor position. */
487 : :
488 : : static int
489 : 0 : getcurpos (pretty *s)
490 : : {
491 : 0 : if (s->needs_space)
492 : 0 : return s->curpos + 1;
493 : : else
494 : 0 : return s->curpos;
495 : : }
496 : :
497 : : static FILE *
498 : 0 : getoutput (pretty *s)
499 : : {
500 : 0 : return m2pp_output_file[s->output];
501 : : }
502 : :
503 : : /* m2pp_type_lowlevel prints out the low level details of a
504 : : fundamental type. */
505 : :
506 : : static void
507 : 0 : m2pp_type_lowlevel (pretty *s, tree t)
508 : : {
509 : 0 : if (TREE_CODE (t) == INTEGER_TYPE)
510 : : {
511 : 0 : m2pp_print (s, "min");
512 : 0 : m2pp_needspace (s);
513 : 0 : m2pp_integer_cst (s, TYPE_MIN_VALUE (t));
514 : 0 : m2pp_print (s, ", max");
515 : 0 : m2pp_needspace (s);
516 : 0 : m2pp_integer_cst (s, TYPE_MAX_VALUE (t));
517 : 0 : m2pp_print (s, ", type size unit");
518 : 0 : m2pp_needspace (s);
519 : 0 : m2pp_integer_cst (s, TYPE_SIZE_UNIT (t));
520 : 0 : m2pp_print (s, ", type size");
521 : 0 : m2pp_needspace (s);
522 : 0 : m2pp_integer_cst (s, TYPE_SIZE (t));
523 : :
524 : 0 : fprintf (getoutput (s),
525 : : ", precision %d, mode %d, align %d, user align %d",
526 : 0 : TYPE_PRECISION (t), TYPE_MODE (t), TYPE_ALIGN (t),
527 : 0 : TYPE_USER_ALIGN (t));
528 : :
529 : 0 : m2pp_needspace (s);
530 : 0 : if (TYPE_UNSIGNED (t))
531 : 0 : m2pp_print (s, "unsigned");
532 : : else
533 : 0 : m2pp_print (s, "signed");
534 : : }
535 : 0 : }
536 : :
537 : : /* m2pp_var emit a VAR if necessary. */
538 : :
539 : : static void
540 : 0 : m2pp_var (pretty *s)
541 : : {
542 : 0 : if (!s->in_vars)
543 : : {
544 : 0 : s->in_vars = true;
545 : 0 : m2pp_print (s, "VAR\n");
546 : 0 : setindent (s, getindent (s) + 3);
547 : : }
548 : 0 : }
549 : :
550 : : /* m2pp_types emit a TYPE if necessary. */
551 : :
552 : : static void
553 : 0 : m2pp_types (pretty *s)
554 : : {
555 : 0 : if (!s->in_types)
556 : : {
557 : 0 : s->in_types = true;
558 : 0 : m2pp_print (s, "TYPE\n");
559 : 0 : setindent (s, getindent (s) + 3);
560 : : }
561 : 0 : }
562 : :
563 : : #ifdef DEBUGGING
564 : : /* hextree displays the critical fields for function, block and
565 : : bind_expr trees in raw hex. */
566 : :
567 : : static void
568 : : hextree (tree t)
569 : : {
570 : : if (t == NULL_TREE)
571 : : return;
572 : :
573 : : if (TREE_CODE (t) == BLOCK)
574 : : {
575 : : printf ("(* BLOCK %p *)\n", (void *)t);
576 : : printf ("BLOCK_VARS (t) = %p\n", (void *)BLOCK_VARS (t));
577 : : printf ("BLOCK_SUPERCONTEXT (t) = %p\n",
578 : : (void *)BLOCK_SUPERCONTEXT (t));
579 : : }
580 : : if (TREE_CODE (t) == BIND_EXPR)
581 : : {
582 : : printf ("(* BIND_EXPR %p *)\n", (void *)t);
583 : : printf ("BIND_EXPR_VARS (t) = %p\n", (void *)BIND_EXPR_VARS (t));
584 : : printf ("BIND_EXPR_BLOCK (t) = %p\n", (void *)BIND_EXPR_BLOCK (t));
585 : : printf ("BIND_EXPR_BODY (t) = %p\n", (void *)BIND_EXPR_BODY (t));
586 : : }
587 : : if (TREE_CODE (t) == FUNCTION_DECL)
588 : : {
589 : : printf ("(* FUNCTION_DECL %p *)\n", (void *)t);
590 : : printf ("DECL_INITIAL (t) = %p\n", (void *)DECL_INITIAL (t));
591 : : printf ("DECL_SAVED_TREE (t) = %p\n", (void *)DECL_SAVED_TREE (t));
592 : : hextree (DECL_INITIAL (t));
593 : : hextree (DECL_SAVED_TREE (t));
594 : : }
595 : : if (VAR_P (t))
596 : : {
597 : : pretty *state = initPretty (M2PP_DUMP_STDOUT, 0);
598 : :
599 : : printf ("(* VAR_DECL %p <", (void *)t);
600 : : if (DECL_SEEN_IN_BIND_EXPR_P (t))
601 : : printf ("b");
602 : : if (DECL_EXTERNAL (t))
603 : : printf ("e");
604 : : if (TREE_STATIC (t))
605 : : printf ("s");
606 : : printf ("> context = %p*)\n", (void *)decl_function_context (t));
607 : : m2pp_type (state, TREE_TYPE (t));
608 : : m2pp_needspace (state);
609 : : m2pp_print (state, ";\n");
610 : : killPretty (state);
611 : : }
612 : : if (TREE_CODE (t) == PARM_DECL)
613 : : {
614 : : pretty *state = initPretty (M2PP_DUMP_STDOUT, 0);
615 : :
616 : : printf ("(* PARM_DECL %p <", (void *)t);
617 : : printf ("> context = %p*)\n", (void *)decl_function_context (t));
618 : : m2pp_type (state, TREE_TYPE (t));
619 : : m2pp_needspace (state);
620 : : m2pp_print (state, ";\n");
621 : : killPretty (state);
622 : : }
623 : : }
624 : : #endif
625 : :
626 : : /* translation produce a pseudo implementation module from the tree t. */
627 : :
628 : : static void
629 : 0 : m2pp_translation (pretty *s, tree t)
630 : : {
631 : 0 : tree block = DECL_INITIAL (t);
632 : :
633 : 0 : m2pp_print (s, "IMPLEMENTATION MODULE ");
634 : 0 : m2pp_identifier (s, t);
635 : 0 : m2pp_print (s, "\n\n");
636 : :
637 : 0 : if (block != NULL)
638 : : {
639 : 0 : m2pp_module_block (s, block);
640 : 0 : m2pp_print (s, "\n");
641 : : }
642 : :
643 : 0 : m2pp_print (s, "\n");
644 : 0 : m2pp_print (s, "END ");
645 : 0 : m2pp_identifier (s, t);
646 : 0 : m2pp_print (s, ".\n");
647 : 0 : }
648 : :
649 : : static void
650 : 0 : m2pp_module_block (pretty *s, tree t)
651 : : {
652 : 0 : t = BLOCK_VARS (t);
653 : :
654 : 0 : if (t != NULL_TREE)
655 : 0 : for (; t != NULL_TREE; t = TREE_CHAIN (t))
656 : : {
657 : 0 : switch (TREE_CODE (t))
658 : : {
659 : 0 : case FUNCTION_DECL:
660 : 0 : if (!DECL_EXTERNAL (t))
661 : : {
662 : 0 : pretty *p = dupPretty (s);
663 : 0 : fprintf (getoutput (s), "\n");
664 : 0 : p->in_vars = false;
665 : 0 : p->in_types = false;
666 : 0 : m2pp_function (p, t);
667 : 0 : killPretty (p);
668 : 0 : fprintf (getoutput (s), "\n");
669 : 0 : s->in_vars = false;
670 : 0 : s->in_types = false;
671 : : }
672 : : break;
673 : :
674 : 0 : case TYPE_DECL:
675 : 0 : {
676 : 0 : int o = getindent (s);
677 : 0 : int p;
678 : :
679 : 0 : m2pp_print (s, "\n");
680 : 0 : m2pp_types (s);
681 : 0 : setindent (s, o + 3);
682 : 0 : m2pp_identifier (s, t);
683 : 0 : m2pp_print (s, " = ");
684 : 0 : p = getcurpos (s);
685 : 0 : setindent (s, p);
686 : 0 : m2pp_type (s, TREE_TYPE (t));
687 : 0 : setindent (s, o);
688 : 0 : m2pp_needspace (s);
689 : 0 : m2pp_print (s, ";\n");
690 : 0 : s->in_vars = false;
691 : : }
692 : 0 : break;
693 : :
694 : 0 : case VAR_DECL:
695 : 0 : m2pp_var (s);
696 : 0 : m2pp_identifier (s, t);
697 : 0 : m2pp_needspace (s);
698 : 0 : m2pp_print (s, ":");
699 : 0 : m2pp_needspace (s);
700 : 0 : m2pp_type (s, TREE_TYPE (t));
701 : 0 : m2pp_needspace (s);
702 : 0 : m2pp_print (s, ";\n");
703 : 0 : s->in_types = false;
704 : 0 : break;
705 : :
706 : 0 : case DECL_EXPR:
707 : 0 : fprintf (getoutput (s), "is this node legal here? \n");
708 : 0 : m2pp_decl_expr (s, t);
709 : 0 : break;
710 : :
711 : 0 : default:
712 : 0 : m2pp_unknown (s, __FUNCTION__, get_tree_code_name (TREE_CODE (t)));
713 : : }
714 : : }
715 : 0 : }
716 : :
717 : : /* m2pp_begin emit a BEGIN if necessary. */
718 : :
719 : : static void
720 : 0 : m2pp_begin (pretty *s)
721 : : {
722 : 0 : if (!s->issued_begin)
723 : : {
724 : 0 : if (s->in_vars || s->in_types)
725 : : {
726 : 0 : setindent (s, getindent (s) - 3);
727 : 0 : m2pp_print (s, "BEGIN\n");
728 : 0 : setindent (s, getindent (s) + 3);
729 : : }
730 : : else
731 : : {
732 : 0 : m2pp_print (s, "BEGIN\n");
733 : 0 : setindent (s, getindent (s) + 3);
734 : : }
735 : 0 : s->issued_begin = true;
736 : 0 : s->in_vars = false;
737 : 0 : s->in_types = false;
738 : : }
739 : 0 : }
740 : :
741 : : /* m2pp_function walk over the function. */
742 : :
743 : : static void
744 : 0 : m2pp_function (pretty *s, tree t)
745 : : {
746 : 0 : m2pp_function_header (s, t);
747 : 0 : m2pp_function_vars (s, t);
748 : 0 : m2pp_statement_sequence (s, DECL_SAVED_TREE (t));
749 : 0 : if (TREE_CODE (t) == FUNCTION_DECL)
750 : : {
751 : 0 : m2pp_begin (s);
752 : 0 : setindent (s, getindent (s) - 3);
753 : 0 : m2pp_print (s, "END");
754 : 0 : m2pp_needspace (s);
755 : 0 : m2pp_identifier (s, t);
756 : 0 : m2pp_needspace (s);
757 : 0 : m2pp_print (s, ";\n");
758 : : }
759 : 0 : }
760 : :
761 : : /* m2pp_bind_expr displays the bind expr tree node. */
762 : :
763 : : static void
764 : 0 : m2pp_bind_expr (pretty *s, tree t)
765 : : {
766 : 0 : if (TREE_CODE (t) == BIND_EXPR)
767 : : {
768 : 0 : if (BIND_EXPR_VARS (t))
769 : : {
770 : 0 : m2pp_print (s, "(* variables in bind_expr *)\n");
771 : 0 : m2pp_var (s);
772 : 0 : m2pp_var_list (s, BIND_EXPR_VARS (t));
773 : : }
774 : 0 : if (BIND_EXPR_BLOCK (t))
775 : : {
776 : 0 : m2pp_print (s, "(* bind_expr_block *)\n");
777 : 0 : m2pp_statement_sequence (s, BIND_EXPR_BLOCK (t));
778 : 0 : m2pp_needspace (s);
779 : 0 : m2pp_print (s, "; \n");
780 : : }
781 : 0 : m2pp_statement_sequence (s, BIND_EXPR_BODY (t));
782 : : }
783 : 0 : }
784 : :
785 : : /* m2pp_block_list iterates over the list of blocks. */
786 : :
787 : : static void
788 : 0 : m2pp_block_list (pretty *s, tree t)
789 : : {
790 : 0 : for (; t; t = BLOCK_CHAIN (t))
791 : 0 : m2pp_block (s, t);
792 : 0 : }
793 : :
794 : : /* m2pp_block prints the VARiables and the TYPEs inside a block. */
795 : :
796 : : static void
797 : 0 : m2pp_block (pretty *s, tree t)
798 : : {
799 : 0 : if ((BLOCK_VARS (t) != NULL_TREE) && (s->block != BLOCK_VARS (t)))
800 : : {
801 : 0 : s->block = BLOCK_VARS (t);
802 : 0 : m2pp_print (s, "(* block variables *)\n");
803 : 0 : m2pp_var (s);
804 : 0 : m2pp_var_list (s, BLOCK_VARS (t));
805 : : }
806 : 0 : }
807 : :
808 : : /* m2pp_var_type_decl displays the variable and type declaration. */
809 : :
810 : : static void
811 : 0 : m2pp_var_type_decl (pretty *s, tree t)
812 : : {
813 : 0 : m2pp_identifier (s, t);
814 : 0 : m2pp_needspace (s);
815 : 0 : m2pp_print (s, ":");
816 : 0 : m2pp_needspace (s);
817 : 0 : m2pp_type (s, TREE_TYPE (t));
818 : 0 : m2pp_needspace (s);
819 : 0 : m2pp_print (s, ";\n");
820 : 0 : }
821 : :
822 : : /* m2pp_var_list print a variable list. */
823 : :
824 : : static void
825 : 0 : m2pp_var_list (pretty *s, tree t)
826 : : {
827 : 0 : if (t != NULL_TREE)
828 : 0 : for (; t; t = TREE_CHAIN (t))
829 : : {
830 : 0 : if (TREE_CODE (t) == FUNCTION_DECL)
831 : : {
832 : 0 : pretty *p = dupPretty (s);
833 : 0 : fprintf (getoutput (s), "\n");
834 : 0 : p->in_vars = false;
835 : 0 : p->in_types = false;
836 : 0 : m2pp_function (p, t);
837 : 0 : killPretty (p);
838 : 0 : fprintf (getoutput (s), "\n");
839 : : }
840 : 0 : else if (TREE_CODE (t) == TYPE_DECL)
841 : 0 : m2pp_identifier (s, t);
842 : 0 : else if (TREE_CODE (t) == DECL_EXPR)
843 : : {
844 : 0 : fprintf (getoutput (s), "is this node legal here? \n");
845 : : // is it legal to have a DECL_EXPR here ?
846 : 0 : m2pp_var_type_decl (s, DECL_EXPR_DECL (t));
847 : : }
848 : : else
849 : 0 : m2pp_var_type_decl (s, t);
850 : : }
851 : 0 : }
852 : :
853 : : #if 0
854 : : /* m2pp_type_list print a variable list. */
855 : :
856 : : static void
857 : : m2pp_type_list (pretty *s, tree t)
858 : : {
859 : : if (t != NULL_TREE)
860 : : for (; t; t = TREE_CHAIN (t))
861 : : {
862 : : m2pp_identifier (s, t);
863 : : m2pp_needspace (s);
864 : : m2pp_print (s, "=");
865 : : m2pp_needspace (s);
866 : : m2pp_type (s, TREE_TYPE (t));
867 : : m2pp_needspace (s);
868 : : m2pp_print (s, ";\n");
869 : : }
870 : : }
871 : : #endif
872 : :
873 : : /* m2pp_needspace sets appropriate flag to true. */
874 : :
875 : : static void
876 : 0 : m2pp_needspace (pretty *s)
877 : : {
878 : 0 : s->needs_space = true;
879 : 0 : }
880 : :
881 : : /* m2pp_identifer prints an identifier. */
882 : :
883 : : static void
884 : 0 : m2pp_identifier (pretty *s, tree t)
885 : : {
886 : 0 : if (t)
887 : : {
888 : 0 : if (TREE_CODE (t) == COMPONENT_REF)
889 : 0 : m2pp_component_ref (s, t);
890 : 0 : else if (DECL_NAME (t) && IDENTIFIER_POINTER (DECL_NAME (t)))
891 : 0 : m2pp_ident_pointer (s, DECL_NAME (t));
892 : : else
893 : : {
894 : 0 : char name[100];
895 : :
896 : 0 : if (TREE_CODE (t) == CONST_DECL)
897 : 0 : snprintf (name, 100, "C_%u", DECL_UID (t));
898 : : else
899 : 0 : snprintf (name, 100, "D_%u", DECL_UID (t));
900 : 0 : m2pp_print (s, name);
901 : 0 : if (TREE_TYPE (t) != NULL_TREE)
902 : : {
903 : 0 : m2pp_needspace (s);
904 : 0 : m2pp_print (s, "(* type:");
905 : 0 : m2pp_needspace (s);
906 : 0 : m2pp_simple_type (s, TREE_TYPE (t));
907 : 0 : m2pp_needspace (s);
908 : : #if 0
909 : : m2pp_type_lowlevel (s, TREE_TYPE (t));
910 : : m2pp_needspace (s);
911 : : #endif
912 : 0 : m2pp_print (s, "*)");
913 : : }
914 : : }
915 : : }
916 : 0 : }
917 : :
918 : : /* m2pp_ident_pointer displays an ident pointer. */
919 : :
920 : : static void
921 : 0 : m2pp_ident_pointer (pretty *s, tree t)
922 : : {
923 : 0 : if (t)
924 : 0 : m2pp_print (s, IDENTIFIER_POINTER (t));
925 : 0 : }
926 : :
927 : : /* m2pp_parameter prints out a param decl tree. */
928 : :
929 : : static void
930 : 0 : m2pp_parameter (pretty *s, tree t)
931 : : {
932 : 0 : if (TREE_CODE (t) == PARM_DECL)
933 : : {
934 : 0 : if (TREE_TYPE (t) && (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE))
935 : : {
936 : 0 : m2pp_print (s, "VAR");
937 : 0 : m2pp_needspace (s);
938 : 0 : m2pp_identifier (s, t);
939 : 0 : m2pp_print (s, ":");
940 : 0 : m2pp_needspace (s);
941 : 0 : m2pp_simple_type (s, TREE_TYPE (TREE_TYPE (t)));
942 : : }
943 : : else
944 : : {
945 : 0 : m2pp_identifier (s, t);
946 : 0 : m2pp_print (s, ":");
947 : 0 : m2pp_needspace (s);
948 : 0 : m2pp_simple_type (s, TREE_TYPE (t));
949 : : }
950 : : }
951 : 0 : }
952 : :
953 : : /* m2pp_param_type prints out the type of parameter. */
954 : :
955 : : static void
956 : 0 : m2pp_param_type (pretty *s, tree t)
957 : : {
958 : 0 : if (t && (TREE_CODE (t) == REFERENCE_TYPE))
959 : : {
960 : 0 : m2pp_print (s, "VAR");
961 : 0 : m2pp_needspace (s);
962 : 0 : m2pp_simple_type (s, TREE_TYPE (t));
963 : : }
964 : : else
965 : 0 : m2pp_simple_type (s, t);
966 : 0 : }
967 : :
968 : : /* m2pp_procedure_type displays a procedure type. */
969 : :
970 : : static void
971 : 0 : m2pp_procedure_type (pretty *s, tree t)
972 : : {
973 : 0 : push (t);
974 : 0 : if (TREE_CODE (t) == FUNCTION_TYPE)
975 : : {
976 : 0 : tree i = TYPE_ARG_TYPES (t);
977 : 0 : tree returnType = TREE_TYPE (TREE_TYPE (t));
978 : :
979 : 0 : m2pp_needspace (s);
980 : 0 : m2pp_print (s, "PROCEDURE");
981 : 0 : m2pp_needspace (s);
982 : 0 : if (i != NULL_TREE)
983 : : {
984 : 0 : int o = getindent (s);
985 : 0 : int p;
986 : 0 : bool first = true;
987 : :
988 : 0 : m2pp_print (s, "(");
989 : 0 : p = getcurpos (s);
990 : 0 : setindent (s, p);
991 : 0 : while (i != NULL_TREE)
992 : : {
993 : 0 : if (TREE_CHAIN (i) == NULL_TREE)
994 : : {
995 : 0 : if (TREE_VALUE (i) == void_type_node)
996 : : /* Ignore void_type_node at the end. */
997 : : ;
998 : : else
999 : : {
1000 : 0 : m2pp_param_type (s, TREE_VALUE (i));
1001 : 0 : m2pp_print (s, ", ...");
1002 : : }
1003 : : break;
1004 : : }
1005 : : else
1006 : : {
1007 : 0 : if (!first)
1008 : : {
1009 : 0 : m2pp_print (s, ",");
1010 : 0 : m2pp_needspace (s);
1011 : : }
1012 : 0 : m2pp_param_type (s, TREE_VALUE (i));
1013 : : }
1014 : 0 : i = TREE_CHAIN (i);
1015 : 0 : first = false;
1016 : : }
1017 : 0 : m2pp_print (s, ")");
1018 : 0 : setindent (s, o);
1019 : : }
1020 : 0 : else if (returnType != NULL_TREE)
1021 : : {
1022 : 0 : m2pp_needspace (s);
1023 : 0 : m2pp_print (s, "()");
1024 : : }
1025 : 0 : if (returnType != NULL_TREE)
1026 : : {
1027 : 0 : m2pp_needspace (s);
1028 : 0 : m2pp_print (s, ": ");
1029 : 0 : m2pp_simple_type (s, returnType);
1030 : : }
1031 : : }
1032 : 0 : pop ();
1033 : 0 : }
1034 : :
1035 : : /* m2pp_comment_header displays a simple header with some critical
1036 : : tree info. */
1037 : :
1038 : : static void
1039 : 0 : m2pp_comment_header (pretty *s, tree t)
1040 : : {
1041 : 0 : int o = getindent (s);
1042 : :
1043 : 0 : m2pp_print (s, "(*\n");
1044 : 0 : setindent (s, o + 3);
1045 : 0 : m2pp_identifier (s, t);
1046 : 0 : m2pp_needspace (s);
1047 : 0 : m2pp_print (s, "-");
1048 : 0 : m2pp_needspace (s);
1049 : 0 : if (TREE_PUBLIC (t))
1050 : : {
1051 : 0 : m2pp_needspace (s);
1052 : 0 : m2pp_print (s, "public,");
1053 : : }
1054 : 0 : if (TREE_STATIC (t))
1055 : : {
1056 : 0 : m2pp_needspace (s);
1057 : 0 : m2pp_print (s, "static,");
1058 : : }
1059 : 0 : if (DECL_EXTERNAL (t))
1060 : : {
1061 : 0 : m2pp_needspace (s);
1062 : 0 : m2pp_print (s, "extern");
1063 : : }
1064 : 0 : m2pp_print (s, "\n");
1065 : 0 : setindent (s, o);
1066 : 0 : m2pp_print (s, "*)\n\n");
1067 : 0 : }
1068 : :
1069 : : /* m2pp_function_header displays the function header. */
1070 : :
1071 : : static void
1072 : 0 : m2pp_function_header (pretty *s, tree t)
1073 : : {
1074 : 0 : push (t);
1075 : 0 : if (TREE_CODE (t) == FUNCTION_DECL)
1076 : : {
1077 : 0 : tree i = DECL_ARGUMENTS (t);
1078 : 0 : tree returnType = TREE_TYPE (TREE_TYPE (t));
1079 : :
1080 : 0 : m2pp_comment_header (s, t);
1081 : 0 : m2pp_print (s, "PROCEDURE ");
1082 : 0 : m2pp_identifier (s, t);
1083 : 0 : m2pp_needspace (s);
1084 : 0 : if (i != NULL_TREE)
1085 : : {
1086 : 0 : int o = getindent (s);
1087 : 0 : int p;
1088 : :
1089 : 0 : m2pp_print (s, "(");
1090 : 0 : p = getcurpos (s);
1091 : 0 : setindent (s, p);
1092 : 0 : while (i != NULL_TREE)
1093 : : {
1094 : 0 : m2pp_parameter (s, i);
1095 : 0 : i = TREE_CHAIN (i);
1096 : 0 : if (i != NULL_TREE)
1097 : 0 : m2pp_print (s, ";\n");
1098 : : }
1099 : 0 : m2pp_print (s, ")");
1100 : 0 : m2pp_needspace (s);
1101 : 0 : setindent (s, o);
1102 : : }
1103 : 0 : else if (returnType != void_type_node)
1104 : : {
1105 : 0 : m2pp_print (s, "()");
1106 : 0 : m2pp_needspace (s);
1107 : : }
1108 : 0 : if (returnType != void_type_node)
1109 : : {
1110 : 0 : m2pp_print (s, ": ");
1111 : 0 : m2pp_simple_type (s, returnType);
1112 : 0 : m2pp_needspace (s);
1113 : : }
1114 : 0 : m2pp_print (s, "; ");
1115 : 0 : m2pp_loc (s, t);
1116 : 0 : m2pp_print (s, "\n");
1117 : : }
1118 : 0 : pop ();
1119 : 0 : }
1120 : :
1121 : : /* m2pp_add_var adds a variable into a list as defined by, data. */
1122 : :
1123 : : static tree
1124 : 0 : m2pp_add_var (tree *tp, int *walk_subtrees, void *data)
1125 : : {
1126 : 0 : tree t = *tp;
1127 : 0 : pretty *s = (pretty *)data;
1128 : 0 : enum tree_code code = TREE_CODE (t);
1129 : :
1130 : 0 : if (code == VAR_DECL)
1131 : : {
1132 : 0 : m2pp_var (s);
1133 : 0 : m2pp_identifier (s, t);
1134 : 0 : m2pp_needspace (s);
1135 : 0 : m2pp_print (s, ":");
1136 : 0 : m2pp_needspace (s);
1137 : 0 : m2pp_type (s, TREE_TYPE (t));
1138 : 0 : m2pp_needspace (s);
1139 : 0 : m2pp_print (s, ";\n");
1140 : : }
1141 : 0 : if (code == SSA_NAME)
1142 : : {
1143 : 0 : m2pp_var (s);
1144 : 0 : m2pp_ssa (s, t);
1145 : 0 : m2pp_identifier (s, SSA_NAME_VAR (t));
1146 : 0 : m2pp_needspace (s);
1147 : 0 : m2pp_print (s, ":");
1148 : 0 : m2pp_needspace (s);
1149 : 0 : m2pp_type (s, TREE_TYPE (t));
1150 : 0 : m2pp_needspace (s);
1151 : 0 : m2pp_print (s, ";\n");
1152 : : }
1153 : :
1154 : 0 : *walk_subtrees = 1;
1155 : 0 : return NULL_TREE;
1156 : : }
1157 : :
1158 : : /* m2pp_function_vars displays variables as defined by the function
1159 : : tree. */
1160 : :
1161 : : static void
1162 : 0 : m2pp_function_vars (pretty *s, tree t)
1163 : : {
1164 : 0 : walk_tree_without_duplicates (&t, m2pp_add_var, s);
1165 : :
1166 : 0 : if (TREE_CODE (t) == FUNCTION_DECL && DECL_INITIAL (t))
1167 : : {
1168 : 0 : m2pp_print (s, "(* variables in function_decl (decl_initial) *)\n");
1169 : 0 : m2pp_var (s);
1170 : 0 : m2pp_statement_sequence (s, DECL_INITIAL (t));
1171 : : }
1172 : 0 : }
1173 : :
1174 : : /* m2pp_print print out a string p interpreting '\n' and
1175 : : adjusting the fields within state s. */
1176 : :
1177 : : static void
1178 : 0 : m2pp_print (pretty *s, const char *p)
1179 : : {
1180 : 0 : if (p)
1181 : : {
1182 : 0 : int l = strlen (p);
1183 : 0 : int i = 0;
1184 : :
1185 : 0 : if (s->needs_space)
1186 : : {
1187 : 0 : fprintf (getoutput (s), " ");
1188 : 0 : s->needs_space = false;
1189 : 0 : s->curpos++;
1190 : : }
1191 : :
1192 : 0 : while (i < l)
1193 : : {
1194 : 0 : if (p[i] == '\n')
1195 : : {
1196 : 0 : s->needs_indent = true;
1197 : 0 : s->curpos = 0;
1198 : 0 : fprintf (getoutput (s), "\n");
1199 : : }
1200 : : else
1201 : : {
1202 : 0 : if (s->needs_indent)
1203 : : {
1204 : 0 : if (s->indent > 0)
1205 : 0 : fprintf (getoutput (s), "%*c", s->indent, ' ');
1206 : 0 : s->needs_indent = false;
1207 : 0 : s->curpos += s->indent;
1208 : : }
1209 : 0 : s->curpos++;
1210 : 0 : fputc (p[i], getoutput (s));
1211 : : }
1212 : 0 : i++;
1213 : : }
1214 : : }
1215 : 0 : }
1216 : :
1217 : : /* m2pp_print_char prints out a character ch obeying needs_space
1218 : : and needs_indent. */
1219 : :
1220 : : static void
1221 : 0 : m2pp_print_char (pretty *s, char ch)
1222 : : {
1223 : 0 : if (s->needs_space)
1224 : : {
1225 : 0 : fprintf (getoutput (s), " ");
1226 : 0 : s->needs_space = false;
1227 : 0 : s->curpos++;
1228 : : }
1229 : 0 : if (s->needs_indent)
1230 : : {
1231 : 0 : if (s->indent > 0)
1232 : 0 : fprintf (getoutput (s), "%*c", s->indent, ' ');
1233 : 0 : s->needs_indent = false;
1234 : 0 : s->curpos += s->indent;
1235 : : }
1236 : 0 : if (ch == '\n')
1237 : : {
1238 : 0 : s->curpos++;
1239 : 0 : fputc ('\\', getoutput (s));
1240 : 0 : fputc ('n', getoutput (s));
1241 : : }
1242 : : else
1243 : 0 : fputc (ch, getoutput (s));
1244 : 0 : s->curpos++;
1245 : 0 : }
1246 : :
1247 : : /* m2pp_integer display the appropriate integer type. */
1248 : :
1249 : : #if defined(GM2)
1250 : : void
1251 : 0 : m2pp_integer (pretty *s, tree t)
1252 : : {
1253 : 0 : if (t == m2type_GetM2ZType ())
1254 : 0 : m2pp_print (s, "M2ZTYPE");
1255 : 0 : else if (t == m2type_GetM2LongIntType ())
1256 : 0 : m2pp_print (s, "LONGINT");
1257 : 0 : else if (t == m2type_GetM2IntegerType ())
1258 : 0 : m2pp_print (s, "INTEGER");
1259 : 0 : else if (t == m2type_GetM2ShortIntType ())
1260 : 0 : m2pp_print (s, "SHORTINT");
1261 : 0 : else if (t == m2type_GetLongIntType ())
1262 : 0 : m2pp_print (s, "long int");
1263 : 0 : else if (t == m2type_GetIntegerType ())
1264 : 0 : m2pp_print (s, "int");
1265 : 0 : else if (t == m2type_GetShortIntType ())
1266 : 0 : m2pp_print (s, "short");
1267 : 0 : else if (t == m2type_GetM2LongCardType ())
1268 : 0 : m2pp_print (s, "LONGCARD");
1269 : 0 : else if (t == m2type_GetM2CardinalType ())
1270 : 0 : m2pp_print (s, "CARDINAL");
1271 : 0 : else if (t == m2type_GetM2ShortCardType ())
1272 : 0 : m2pp_print (s, "SHORTCARD");
1273 : 0 : else if (t == m2type_GetCardinalType ())
1274 : 0 : m2pp_print (s, "CARDINAL");
1275 : 0 : else if (t == m2type_GetPointerType ())
1276 : 0 : m2pp_print (s, "ADDRESS");
1277 : 0 : else if (t == m2type_GetByteType ())
1278 : 0 : m2pp_print (s, "BYTE");
1279 : 0 : else if (t == m2type_GetCharType ())
1280 : 0 : m2pp_print (s, "CHAR");
1281 : 0 : else if (t == m2type_GetBitsetType ())
1282 : 0 : m2pp_print (s, "BITSET");
1283 : 0 : else if (t == m2type_GetBitnumType ())
1284 : 0 : m2pp_print (s, "BITNUM");
1285 : : else
1286 : : {
1287 : 0 : if (TYPE_UNSIGNED (t))
1288 : 0 : m2pp_print (s, "CARDINAL");
1289 : : else
1290 : 0 : m2pp_print (s, "INTEGER");
1291 : 0 : m2pp_integer_cst (s, TYPE_SIZE (t));
1292 : : }
1293 : 0 : }
1294 : : #else
1295 : : void
1296 : : m2pp_integer (pretty *s, tree t ATTRIBUTE_UNUSED)
1297 : : {
1298 : : m2pp_print (s, "INTEGER");
1299 : : }
1300 : : #endif
1301 : :
1302 : : /* m2pp_complex display the actual complex type. */
1303 : :
1304 : : #if defined(GM2)
1305 : : static void
1306 : 0 : m2pp_complex (pretty *s, tree t)
1307 : : {
1308 : 0 : if (t == m2type_GetM2ComplexType ())
1309 : 0 : m2pp_print (s, "COMPLEX");
1310 : 0 : else if (t == m2type_GetM2LongComplexType ())
1311 : 0 : m2pp_print (s, "LONGCOMPLEX");
1312 : 0 : else if (t == m2type_GetM2ShortComplexType ())
1313 : 0 : m2pp_print (s, "SHORTCOMPLEX");
1314 : 0 : else if (t == m2type_GetM2CType ())
1315 : 0 : m2pp_print (s, "C'omplex' type");
1316 : 0 : else if (t == m2type_GetM2Complex32 ())
1317 : 0 : m2pp_print (s, "COMPLEX32");
1318 : 0 : else if (t == m2type_GetM2Complex64 ())
1319 : 0 : m2pp_print (s, "COMPLEX64");
1320 : 0 : else if (t == m2type_GetM2Complex96 ())
1321 : 0 : m2pp_print (s, "COMPLEX96");
1322 : 0 : else if (t == m2type_GetM2Complex128 ())
1323 : 0 : m2pp_print (s, "COMPLEX128");
1324 : : else
1325 : 0 : m2pp_print (s, "unknown COMPLEX type");
1326 : 0 : }
1327 : :
1328 : : #else
1329 : :
1330 : : static void
1331 : : m2pp_complex (pretty *s, tree t ATTRIBUTE_UNUSED)
1332 : : {
1333 : : m2pp_print (s, "a COMPLEX type");
1334 : : }
1335 : : #endif
1336 : :
1337 : : void
1338 : 0 : m2pp_real_type (pretty *s, tree t)
1339 : : {
1340 : 0 : if (t == m2type_GetRealType ())
1341 : 0 : m2pp_print (s, "C double");
1342 : 0 : else if (t == m2type_GetShortRealType ())
1343 : 0 : m2pp_print (s, "C float");
1344 : 0 : else if (t == m2type_GetLongRealType ())
1345 : 0 : m2pp_print (s, "C long double");
1346 : 0 : else if (t == m2type_GetM2RealType ())
1347 : 0 : m2pp_print (s, "REAL");
1348 : 0 : else if (t == m2type_GetM2ShortRealType ())
1349 : 0 : m2pp_print (s, "SHORTREAL");
1350 : 0 : else if (t == m2type_GetM2LongRealType ())
1351 : 0 : m2pp_print (s, "LONGREAL");
1352 : 0 : else if (t == m2type_GetM2Real128 ())
1353 : 0 : m2pp_print (s, "REAL128");
1354 : 0 : else if (t == m2type_GetM2Real64 ())
1355 : 0 : m2pp_print (s, "REAL64");
1356 : 0 : else if (t == m2type_GetM2Real32 ())
1357 : 0 : m2pp_print (s, "REAL32");
1358 : 0 : else if (t == m2type_GetM2RType ())
1359 : 0 : m2pp_print (s, "R Type");
1360 : : else
1361 : 0 : m2pp_print (s, "unknown REAL");
1362 : 0 : }
1363 : :
1364 : : /* m2pp_type prints a full type. */
1365 : :
1366 : : void
1367 : 0 : m2pp_type (pretty *s, tree t)
1368 : : {
1369 : 0 : if (begin_printed (t))
1370 : : {
1371 : 0 : m2pp_print (s, "<...>");
1372 : 0 : return;
1373 : : }
1374 : 0 : if ((TREE_CODE (t) != FIELD_DECL) && (TREE_CODE (t) != TYPE_DECL))
1375 : 0 : m2pp_gimpified (s, t);
1376 : 0 : switch (TREE_CODE (t))
1377 : : {
1378 : 0 : case INTEGER_TYPE:
1379 : 0 : m2pp_integer (s, t);
1380 : 0 : break;
1381 : 0 : case REAL_TYPE:
1382 : 0 : m2pp_real_type (s, t);
1383 : 0 : break;
1384 : 0 : case ENUMERAL_TYPE:
1385 : 0 : m2pp_enum (s, t);
1386 : 0 : break;
1387 : 0 : case UNION_TYPE:
1388 : 0 : m2pp_union_type (s, t);
1389 : 0 : break;
1390 : 0 : case RECORD_TYPE:
1391 : 0 : m2pp_record_type (s, t);
1392 : 0 : break;
1393 : 0 : case ARRAY_TYPE:
1394 : 0 : m2pp_array (s, t);
1395 : 0 : break;
1396 : : #if 0
1397 : : case FUNCTION_TYPE:
1398 : : m2pp_function_type (s, t);
1399 : : break;
1400 : : #endif
1401 : 0 : case TYPE_DECL:
1402 : 0 : m2pp_identifier (s, t);
1403 : 0 : break;
1404 : 0 : case POINTER_TYPE:
1405 : 0 : m2pp_pointer_type (s, t);
1406 : 0 : break;
1407 : : #if defined(GM2)
1408 : 0 : case SET_TYPE:
1409 : 0 : m2pp_set_type (s, t);
1410 : 0 : break;
1411 : : #endif
1412 : 0 : case VOID_TYPE:
1413 : 0 : m2pp_print (s, "ADDRESS");
1414 : 0 : break;
1415 : 0 : case COMPLEX_TYPE:
1416 : 0 : m2pp_complex (s, t);
1417 : 0 : break;
1418 : 0 : default:
1419 : 0 : m2pp_unknown (s, __FUNCTION__, get_tree_code_name (TREE_CODE (t)));
1420 : : }
1421 : : }
1422 : :
1423 : : /* m2pp_set_type prints out the set type. */
1424 : :
1425 : : static void
1426 : 0 : m2pp_set_type (pretty *s, tree t)
1427 : : {
1428 : 0 : push (t);
1429 : 0 : m2pp_print (s, "SET OF");
1430 : 0 : m2pp_needspace (s);
1431 : 0 : m2pp_type (s, TREE_TYPE (t));
1432 : 0 : pop ();
1433 : 0 : }
1434 : :
1435 : : /* m2pp_enum print out the enumeration type. */
1436 : :
1437 : : static void
1438 : 0 : m2pp_enum (pretty *s, tree t)
1439 : : {
1440 : 0 : tree chain_p = TYPE_VALUES (t);
1441 : :
1442 : 0 : push (t);
1443 : 0 : m2pp_print (s, "(");
1444 : 0 : while (chain_p)
1445 : : {
1446 : 0 : m2pp_ident_pointer (s, TREE_PURPOSE (chain_p));
1447 : 0 : chain_p = TREE_CHAIN (chain_p);
1448 : 0 : if (chain_p)
1449 : 0 : m2pp_print (s, ", ");
1450 : : }
1451 : 0 : m2pp_print (s, ")");
1452 : 0 : pop ();
1453 : 0 : }
1454 : :
1455 : : /* m2pp_array prints out the array type. */
1456 : :
1457 : : static void
1458 : 0 : m2pp_array (pretty *s, tree t)
1459 : : {
1460 : 0 : push (t);
1461 : 0 : m2pp_print (s, "ARRAY");
1462 : 0 : m2pp_needspace (s);
1463 : 0 : m2pp_subrange (s, TYPE_DOMAIN (t));
1464 : 0 : m2pp_needspace (s);
1465 : 0 : m2pp_print (s, "OF");
1466 : 0 : m2pp_needspace (s);
1467 : 0 : m2pp_type (s, TREE_TYPE (t));
1468 : 0 : pop ();
1469 : 0 : }
1470 : :
1471 : : /* m2pp_subrange prints out the subrange, but probably the lower
1472 : : bound will always be zero. */
1473 : :
1474 : : static void
1475 : 0 : m2pp_subrange (pretty *s, tree t)
1476 : : {
1477 : 0 : tree min = TYPE_MIN_VALUE (t);
1478 : 0 : tree max = TYPE_MAX_VALUE (t);
1479 : :
1480 : 0 : m2pp_print (s, "[");
1481 : 0 : m2pp_expression (s, min);
1482 : 0 : m2pp_print (s, "..");
1483 : 0 : m2pp_expression (s, max);
1484 : 0 : m2pp_print (s, "]");
1485 : 0 : }
1486 : :
1487 : : /* m2pp_gimplified print out a gimplified comment. */
1488 : :
1489 : : static void
1490 : 0 : m2pp_gimpified (pretty *s, tree t)
1491 : : {
1492 : 0 : if (!TYPE_SIZES_GIMPLIFIED (t))
1493 : : {
1494 : 0 : m2pp_print (s, "(* <!g> *)");
1495 : 0 : m2pp_needspace (s);
1496 : : }
1497 : 0 : }
1498 : :
1499 : : /* m2pp_printer_type display the pointer type. */
1500 : :
1501 : : static void
1502 : 0 : m2pp_pointer_type (pretty *s, tree t)
1503 : : {
1504 : 0 : push (t);
1505 : 0 : if (TREE_CODE (t) == POINTER_TYPE)
1506 : : {
1507 : 0 : if (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1508 : 0 : m2pp_procedure_type (s, TREE_TYPE (t));
1509 : 0 : else if (t == ptr_type_node)
1510 : 0 : m2pp_print (s, "ADDRESS");
1511 : : else
1512 : : {
1513 : 0 : m2pp_print (s, "POINTER TO");
1514 : 0 : m2pp_needspace (s);
1515 : 0 : m2pp_type (s, TREE_TYPE (t));
1516 : : }
1517 : : }
1518 : 0 : pop ();
1519 : 0 : }
1520 : :
1521 : : /* m2pp_record_alignment prints out whether this record is aligned
1522 : : (packed). */
1523 : :
1524 : : static void
1525 : 0 : m2pp_record_alignment (pretty *s, tree t)
1526 : : {
1527 : 0 : if (TYPE_PACKED (t))
1528 : 0 : m2pp_print (s, "<* bytealignment (0) *>\n");
1529 : 0 : }
1530 : :
1531 : : static unsigned int
1532 : 0 : m2pp_getaligned (tree t)
1533 : : {
1534 : 0 : if (DECL_P (t))
1535 : : {
1536 : 0 : if (DECL_USER_ALIGN (t))
1537 : 0 : return DECL_ALIGN (t);
1538 : : }
1539 : 0 : else if (TYPE_P (t))
1540 : : {
1541 : 0 : if (TYPE_USER_ALIGN (t))
1542 : 0 : return TYPE_ALIGN (t);
1543 : : }
1544 : : return 0;
1545 : : }
1546 : :
1547 : : static void
1548 : 0 : m2pp_recordfield_alignment (pretty *s, tree t)
1549 : : {
1550 : 0 : unsigned int aligned = m2pp_getaligned (t);
1551 : :
1552 : 0 : if (aligned != 0)
1553 : : {
1554 : 0 : int o = getindent (s);
1555 : 0 : int p = getcurpos (s);
1556 : 0 : m2pp_needspace (s);
1557 : 0 : m2pp_print (s, "<* bytealignment (");
1558 : 0 : setindent (s, p + 18);
1559 : :
1560 : 0 : fprintf (getoutput (s), "%d", aligned / BITS_PER_UNIT);
1561 : :
1562 : 0 : m2pp_print (s, ")");
1563 : 0 : m2pp_needspace (s);
1564 : 0 : setindent (s, p);
1565 : 0 : m2pp_print (s, "*>");
1566 : 0 : setindent (s, o);
1567 : : }
1568 : 0 : }
1569 : :
1570 : : static void
1571 : 0 : m2pp_recordfield_bitfield (pretty *s, tree t)
1572 : : {
1573 : 0 : if ((TREE_CODE (t) == FIELD_DECL) && DECL_PACKED (t))
1574 : : {
1575 : 0 : m2pp_print (s, " (* packed");
1576 : 0 : if (DECL_NONADDRESSABLE_P (t))
1577 : 0 : m2pp_print (s, ", non-addressible");
1578 : 0 : if (DECL_BIT_FIELD (t))
1579 : 0 : m2pp_print (s, ", bit-field");
1580 : 0 : m2pp_print (s, ", offset: ");
1581 : 0 : m2pp_expression (s, DECL_FIELD_OFFSET (t));
1582 : 0 : m2pp_print (s, ", bit offset:");
1583 : 0 : m2pp_expression (s, DECL_FIELD_BIT_OFFSET (t));
1584 : 0 : m2pp_print (s, " *) ");
1585 : : }
1586 : 0 : }
1587 : :
1588 : : /* m2pp_record_type displays the record type. */
1589 : :
1590 : : static void
1591 : 0 : m2pp_record_type (pretty *s, tree t)
1592 : : {
1593 : 0 : push (t);
1594 : 0 : if (TREE_CODE (t) == RECORD_TYPE)
1595 : : {
1596 : 0 : tree i;
1597 : 0 : int o = getindent (s);
1598 : 0 : int p = getcurpos (s);
1599 : :
1600 : 0 : m2pp_print (s, "RECORD\n");
1601 : 0 : setindent (s, p + 3);
1602 : 0 : m2pp_record_alignment (s, t);
1603 : 0 : for (i = TYPE_FIELDS (t); i != NULL_TREE; i = TREE_CHAIN (i))
1604 : : {
1605 : 0 : m2pp_identifier (s, i);
1606 : 0 : m2pp_print (s, " : ");
1607 : 0 : m2pp_type (s, TREE_TYPE (i));
1608 : 0 : m2pp_recordfield_bitfield (s, i);
1609 : 0 : m2pp_recordfield_alignment (s, i);
1610 : 0 : m2pp_print (s, ";\n");
1611 : : }
1612 : 0 : setindent (s, p);
1613 : 0 : m2pp_print (s, "END");
1614 : 0 : setindent (s, o);
1615 : : }
1616 : 0 : pop ();
1617 : 0 : }
1618 : :
1619 : : /* m2pp_record_type displays the record type. */
1620 : :
1621 : : static void
1622 : 0 : m2pp_union_type (pretty *s, tree t)
1623 : : {
1624 : 0 : push (t);
1625 : 0 : if (TREE_CODE (t) == UNION_TYPE)
1626 : : {
1627 : 0 : tree i;
1628 : 0 : int o = getindent (s);
1629 : 0 : int p = getcurpos (s);
1630 : :
1631 : 0 : m2pp_print (s, "CASE .. OF\n");
1632 : 0 : setindent (s, p + 3);
1633 : 0 : m2pp_record_alignment (s, t);
1634 : 0 : for (i = TYPE_FIELDS (t); i != NULL_TREE; i = TREE_CHAIN (i))
1635 : : {
1636 : 0 : m2pp_identifier (s, i);
1637 : 0 : m2pp_print (s, " : ");
1638 : 0 : m2pp_type (s, TREE_TYPE (i));
1639 : 0 : m2pp_recordfield_bitfield (s, i);
1640 : 0 : m2pp_print (s, ";\n");
1641 : : }
1642 : 0 : setindent (s, p);
1643 : 0 : m2pp_print (s, "END");
1644 : 0 : setindent (s, o);
1645 : : }
1646 : 0 : pop ();
1647 : 0 : }
1648 : :
1649 : : /* m2pp_print_mode. */
1650 : :
1651 : : static void
1652 : 0 : m2pp_print_mode (pretty *s, tree t)
1653 : : {
1654 : 0 : int mode = SCALAR_FLOAT_TYPE_MODE (t);
1655 : 0 : char buf[100];
1656 : :
1657 : 0 : snprintf (buf, sizeof (buf), "%d", mode);
1658 : 0 : m2pp_print (s, "<*");
1659 : 0 : m2pp_needspace (s);
1660 : 0 : m2pp_print (s, buf);
1661 : 0 : m2pp_needspace (s);
1662 : 0 : m2pp_print (s, "*>");
1663 : 0 : }
1664 : :
1665 : : /* m2pp_simple_type. */
1666 : :
1667 : : static void
1668 : 0 : m2pp_simple_type (pretty *s, tree t)
1669 : : {
1670 : 0 : if (begin_printed (t))
1671 : : {
1672 : 0 : m2pp_print (s, "<...>");
1673 : 0 : return;
1674 : : }
1675 : :
1676 : 0 : m2pp_gimpified (s, t);
1677 : 0 : switch (TREE_CODE (t))
1678 : : {
1679 : 0 : case INTEGER_TYPE:
1680 : 0 : m2pp_integer (s, t);
1681 : 0 : break;
1682 : 0 : case REAL_TYPE:
1683 : 0 : m2pp_real_type (s, t);
1684 : 0 : m2pp_print_mode (s, t);
1685 : 0 : break;
1686 : 0 : case BOOLEAN_TYPE:
1687 : 0 : m2pp_print (s, "BOOLEAN");
1688 : 0 : break;
1689 : 0 : case VOID_TYPE:
1690 : 0 : m2pp_print (s, "ADDRESS");
1691 : 0 : break;
1692 : 0 : case TYPE_DECL:
1693 : 0 : m2pp_identifier (s, t);
1694 : 0 : break;
1695 : 0 : case POINTER_TYPE:
1696 : 0 : m2pp_pointer_type (s, t);
1697 : 0 : break;
1698 : 0 : case RECORD_TYPE:
1699 : 0 : m2pp_record_type (s, t);
1700 : 0 : break;
1701 : 0 : case UNION_TYPE:
1702 : 0 : m2pp_union_type (s, t);
1703 : 0 : break;
1704 : 0 : case ENUMERAL_TYPE:
1705 : 0 : m2pp_enum (s, t);
1706 : 0 : break;
1707 : 0 : case COMPLEX_TYPE:
1708 : 0 : m2pp_complex (s, t);
1709 : 0 : break;
1710 : 0 : default:
1711 : 0 : m2pp_unknown (s, __FUNCTION__, get_tree_code_name (TREE_CODE (t)));
1712 : : }
1713 : : }
1714 : :
1715 : : /* m2pp_float issue a VAL (type, expr) expression. */
1716 : :
1717 : : static void
1718 : 0 : m2pp_float (pretty *s, tree t)
1719 : : {
1720 : 0 : m2pp_needspace (s);
1721 : 0 : m2pp_print (s, "VAL (");
1722 : 0 : m2pp_simple_type (s, TREE_TYPE (t));
1723 : 0 : m2pp_print (s, ", ");
1724 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
1725 : 0 : m2pp_print (s, ")");
1726 : 0 : }
1727 : :
1728 : : /* m2pp_expression display an expression. */
1729 : :
1730 : : static void
1731 : 0 : m2pp_expression (pretty *s, tree t)
1732 : : {
1733 : 0 : enum tree_code code = TREE_CODE (t);
1734 : :
1735 : 0 : switch (code)
1736 : : {
1737 : 0 : case EQ_EXPR:
1738 : 0 : m2pp_relop (s, t, "=");
1739 : 0 : break;
1740 : 0 : case NE_EXPR:
1741 : 0 : m2pp_relop (s, t, "#");
1742 : 0 : break;
1743 : 0 : case LE_EXPR:
1744 : 0 : m2pp_relop (s, t, "<=");
1745 : 0 : break;
1746 : 0 : case GE_EXPR:
1747 : 0 : m2pp_relop (s, t, ">=");
1748 : 0 : break;
1749 : 0 : case LT_EXPR:
1750 : 0 : m2pp_relop (s, t, "<");
1751 : 0 : break;
1752 : 0 : case GT_EXPR:
1753 : 0 : m2pp_relop (s, t, ">");
1754 : 0 : break;
1755 : 0 : case FLOAT_EXPR:
1756 : 0 : m2pp_float (s, t);
1757 : 0 : break;
1758 : 0 : default:
1759 : 0 : m2pp_simple_expression (s, t);
1760 : : }
1761 : 0 : }
1762 : :
1763 : : /* m2pp_relop displays the lhs relop rhs. */
1764 : :
1765 : : static void
1766 : 0 : m2pp_relop (pretty *s, tree t, const char *p)
1767 : : {
1768 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
1769 : 0 : m2pp_needspace (s);
1770 : 0 : m2pp_print (s, p);
1771 : 0 : m2pp_needspace (s);
1772 : 0 : m2pp_expression (s, TREE_OPERAND (t, 1));
1773 : 0 : }
1774 : :
1775 : : /* m2pp_compound_expression handle compound expression tree. */
1776 : :
1777 : : static void
1778 : 0 : m2pp_compound_expression (pretty *s, tree t)
1779 : : {
1780 : 0 : m2pp_print (s, "compound expression {");
1781 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
1782 : 0 : m2pp_print (s, " (* result ignored *), ");
1783 : 0 : m2pp_expression (s, TREE_OPERAND (t, 1));
1784 : 0 : m2pp_print (s, "}");
1785 : 0 : m2pp_needspace (s);
1786 : 0 : }
1787 : :
1788 : : /* m2pp_target_expression handle target expression tree. */
1789 : :
1790 : : static void
1791 : 0 : m2pp_target_expression (pretty *s, tree t)
1792 : : {
1793 : 0 : m2pp_print (s, "{");
1794 : 0 : m2pp_needspace (s);
1795 : 0 : if (TREE_OPERAND (t, 0) != NULL_TREE)
1796 : : {
1797 : 0 : m2pp_print (s, "(* target *) ");
1798 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
1799 : 0 : m2pp_print (s, ",");
1800 : 0 : m2pp_needspace (s);
1801 : : }
1802 : 0 : if (TREE_OPERAND (t, 1) != NULL_TREE)
1803 : : {
1804 : 0 : m2pp_print (s, "(* initializer *) ");
1805 : 0 : m2pp_expression (s, TREE_OPERAND (t, 1));
1806 : 0 : m2pp_print (s, ",");
1807 : 0 : m2pp_needspace (s);
1808 : : }
1809 : 0 : if (TREE_OPERAND (t, 2) != NULL_TREE)
1810 : : {
1811 : 0 : m2pp_print (s, "(* cleanup *) ");
1812 : 0 : m2pp_expression (s, TREE_OPERAND (t, 2));
1813 : 0 : m2pp_print (s, ",");
1814 : 0 : m2pp_needspace (s);
1815 : : }
1816 : 0 : if (TREE_OPERAND (t, 3) != NULL_TREE)
1817 : : {
1818 : 0 : m2pp_print (s, "(* saved initializer *) ");
1819 : 0 : m2pp_expression (s, TREE_OPERAND (t, 3));
1820 : 0 : m2pp_print (s, ",");
1821 : 0 : m2pp_needspace (s);
1822 : : }
1823 : 0 : m2pp_print (s, "}");
1824 : 0 : m2pp_needspace (s);
1825 : 0 : }
1826 : :
1827 : : /* m2pp_constructor print out a constructor. */
1828 : :
1829 : : static void
1830 : 0 : m2pp_constructor (pretty *s, tree t)
1831 : : {
1832 : 0 : tree purpose, value;
1833 : 0 : unsigned HOST_WIDE_INT ix;
1834 : :
1835 : 0 : m2pp_print (s, "{ ");
1836 : 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), ix, purpose, value)
1837 : : {
1838 : 0 : m2pp_print (s, "(index: ");
1839 : 0 : m2pp_simple_expression (s, purpose);
1840 : 0 : m2pp_print (s, ") ");
1841 : 0 : m2pp_simple_expression (s, value);
1842 : 0 : m2pp_print (s, ", ");
1843 : : }
1844 : 0 : m2pp_print (s, "}");
1845 : 0 : m2pp_print (s, "(* type: ");
1846 : 0 : setindent (s, getindent (s) + 8);
1847 : 0 : m2pp_type (s, TREE_TYPE (t));
1848 : 0 : setindent (s, getindent (s) - 8);
1849 : 0 : m2pp_print (s, " *)\n");
1850 : 0 : }
1851 : :
1852 : : /* m2pp_complex_expr handle GCC complex_expr tree. */
1853 : :
1854 : : static void
1855 : 0 : m2pp_complex_expr (pretty *s, tree t)
1856 : : {
1857 : 0 : if (TREE_CODE (t) == COMPLEX_CST)
1858 : : {
1859 : 0 : m2pp_print (s, "CMPLX(");
1860 : 0 : m2pp_needspace (s);
1861 : 0 : m2pp_expression (s, TREE_REALPART (t));
1862 : 0 : m2pp_print (s, ",");
1863 : 0 : m2pp_needspace (s);
1864 : 0 : m2pp_expression (s, TREE_IMAGPART (t));
1865 : 0 : m2pp_print (s, ")");
1866 : : }
1867 : : else
1868 : : {
1869 : 0 : m2pp_print (s, "CMPLX(");
1870 : 0 : m2pp_needspace (s);
1871 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
1872 : 0 : m2pp_print (s, ",");
1873 : 0 : m2pp_needspace (s);
1874 : 0 : m2pp_expression (s, TREE_OPERAND (t, 1));
1875 : 0 : m2pp_print (s, ")");
1876 : : }
1877 : 0 : }
1878 : :
1879 : : /* m2pp_imagpart_expr handle imagpart_expr tree. */
1880 : :
1881 : : static void
1882 : 0 : m2pp_imagpart_expr (pretty *s, tree t)
1883 : : {
1884 : 0 : m2pp_print (s, "IM(");
1885 : 0 : m2pp_needspace (s);
1886 : 0 : if (TREE_CODE (t) == IMAGPART_EXPR)
1887 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
1888 : 0 : else if (TREE_CODE (t) == COMPLEX_CST)
1889 : 0 : m2pp_expression (s, TREE_IMAGPART (t));
1890 : 0 : m2pp_needspace (s);
1891 : 0 : m2pp_print (s, ")");
1892 : 0 : }
1893 : :
1894 : : /* m2pp_realpart_expr handle imagpart_expr tree. */
1895 : :
1896 : : static void
1897 : 0 : m2pp_realpart_expr (pretty *s, tree t)
1898 : : {
1899 : 0 : m2pp_print (s, "RE(");
1900 : 0 : m2pp_needspace (s);
1901 : 0 : if (TREE_CODE (t) == REALPART_EXPR)
1902 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
1903 : 0 : else if (TREE_CODE (t) == COMPLEX_CST)
1904 : 0 : m2pp_expression (s, TREE_REALPART (t));
1905 : 0 : m2pp_needspace (s);
1906 : 0 : m2pp_print (s, ")");
1907 : 0 : }
1908 : :
1909 : : /* m2pp_bit_ior_expr generate a C style bit or. */
1910 : :
1911 : : static void
1912 : 0 : m2pp_bit_ior_expr (pretty *s, tree t)
1913 : : {
1914 : 0 : m2pp_binary (s, t, "|");
1915 : 0 : }
1916 : :
1917 : : /* m2pp_truth_expr. */
1918 : :
1919 : : static void
1920 : 0 : m2pp_truth_expr (pretty *s, tree t, const char *op)
1921 : : {
1922 : 0 : m2pp_print (s, "(");
1923 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
1924 : 0 : m2pp_print (s, ")");
1925 : 0 : m2pp_needspace (s);
1926 : 0 : m2pp_print (s, op);
1927 : 0 : m2pp_needspace (s);
1928 : 0 : m2pp_print (s, "(");
1929 : 0 : m2pp_expression (s, TREE_OPERAND (t, 1));
1930 : 0 : m2pp_print (s, ")");
1931 : 0 : }
1932 : :
1933 : : /* m2pp_simple_expression handle GCC expression tree. */
1934 : :
1935 : : static void
1936 : 0 : m2pp_simple_expression (pretty *s, tree t)
1937 : : {
1938 : 0 : enum tree_code code = TREE_CODE (t);
1939 : :
1940 : 0 : switch (code)
1941 : : {
1942 : 0 : case ERROR_MARK:
1943 : 0 : m2pp_print (s, "(* !!! ERROR NODE !!! *)");
1944 : 0 : break;
1945 : 0 : case CONSTRUCTOR:
1946 : 0 : m2pp_constructor (s, t);
1947 : 0 : break;
1948 : 0 : case IDENTIFIER_NODE:
1949 : 0 : m2pp_ident_pointer (s, t);
1950 : 0 : break;
1951 : 0 : case PARM_DECL:
1952 : 0 : m2pp_identifier (s, t);
1953 : 0 : break;
1954 : 0 : case FIELD_DECL:
1955 : 0 : m2pp_identifier (s, t);
1956 : 0 : break;
1957 : 0 : case TREE_LIST:
1958 : 0 : m2pp_list (s, t);
1959 : 0 : break;
1960 : 0 : case BLOCK:
1961 : 0 : m2pp_print (s, "(* BLOCK NODE *)");
1962 : 0 : break;
1963 : 0 : case OFFSET_TYPE:
1964 : 0 : m2pp_offset (s, t);
1965 : 0 : break;
1966 : 0 : case INTEGER_CST:
1967 : 0 : m2pp_integer_cst (s, t);
1968 : 0 : break;
1969 : 0 : case REAL_CST:
1970 : 0 : m2pp_real_cst (s, t);
1971 : 0 : break;
1972 : 0 : case STRING_CST:
1973 : 0 : m2pp_string_cst (s, t);
1974 : 0 : break;
1975 : 0 : case INDIRECT_REF:
1976 : 0 : m2pp_indirect_ref (s, t);
1977 : 0 : break;
1978 : 0 : case ADDR_EXPR:
1979 : 0 : m2pp_addr_expr (s, t);
1980 : 0 : break;
1981 : 0 : case NOP_EXPR:
1982 : 0 : m2pp_nop (s, t);
1983 : 0 : break;
1984 : 0 : case CONVERT_EXPR:
1985 : 0 : m2pp_convert (s, t);
1986 : 0 : break;
1987 : 0 : case VAR_DECL:
1988 : 0 : m2pp_var_decl (s, t);
1989 : 0 : break;
1990 : 0 : case RESULT_DECL:
1991 : 0 : m2pp_result_decl (s, t);
1992 : 0 : break;
1993 : 0 : case PLUS_EXPR:
1994 : 0 : m2pp_binary (s, t, "+");
1995 : 0 : break;
1996 : 0 : case MINUS_EXPR:
1997 : 0 : m2pp_binary (s, t, "-");
1998 : 0 : break;
1999 : 0 : case MULT_EXPR:
2000 : 0 : m2pp_binary (s, t, "*");
2001 : 0 : break;
2002 : 0 : case FLOOR_DIV_EXPR:
2003 : 0 : case CEIL_DIV_EXPR:
2004 : 0 : case TRUNC_DIV_EXPR:
2005 : 0 : case ROUND_DIV_EXPR:
2006 : 0 : m2pp_binary (s, t, "DIV");
2007 : 0 : break;
2008 : 0 : case FLOOR_MOD_EXPR:
2009 : 0 : case CEIL_MOD_EXPR:
2010 : 0 : case TRUNC_MOD_EXPR:
2011 : 0 : case ROUND_MOD_EXPR:
2012 : 0 : m2pp_binary (s, t, "MOD");
2013 : 0 : break;
2014 : 0 : case NEGATE_EXPR:
2015 : 0 : m2pp_unary (s, t, "-");
2016 : 0 : break;
2017 : 0 : case CALL_EXPR:
2018 : 0 : m2pp_call_expr (s, t);
2019 : 0 : break;
2020 : 0 : case SSA_NAME:
2021 : 0 : m2pp_ssa (s, t);
2022 : 0 : break;
2023 : 0 : case COMPONENT_REF:
2024 : 0 : m2pp_component_ref (s, t);
2025 : 0 : break;
2026 : 0 : case RETURN_EXPR:
2027 : 0 : m2pp_return_expr (s, t);
2028 : 0 : break;
2029 : 0 : case ARRAY_REF:
2030 : 0 : m2pp_array_ref (s, t);
2031 : 0 : break;
2032 : 0 : case NON_LVALUE_EXPR:
2033 : 0 : m2pp_non_lvalue_expr (s, t);
2034 : 0 : break;
2035 : 0 : case EXPR_STMT:
2036 : 0 : m2pp_expression (s, EXPR_STMT_EXPR (t));
2037 : 0 : break;
2038 : : #if 0
2039 : : case EXC_PTR_EXPR:
2040 : : m2pp_print (s, "GCC_EXCEPTION_OBJECT");
2041 : : break;
2042 : : #endif
2043 : 0 : case INIT_EXPR:
2044 : 0 : case MODIFY_EXPR:
2045 : 0 : m2pp_assignment (s, t);
2046 : 0 : break;
2047 : 0 : case COMPOUND_EXPR:
2048 : 0 : m2pp_compound_expression (s, t);
2049 : 0 : break;
2050 : 0 : case TARGET_EXPR:
2051 : 0 : m2pp_target_expression (s, t);
2052 : 0 : break;
2053 : 0 : case THROW_EXPR:
2054 : 0 : m2pp_throw (s, t);
2055 : 0 : break;
2056 : 0 : case FUNCTION_DECL:
2057 : 0 : m2pp_identifier (s, t);
2058 : 0 : break;
2059 : 0 : case COMPLEX_EXPR:
2060 : 0 : m2pp_complex_expr (s, t);
2061 : 0 : break;
2062 : 0 : case REALPART_EXPR:
2063 : 0 : m2pp_realpart_expr (s, t);
2064 : 0 : break;
2065 : 0 : case IMAGPART_EXPR:
2066 : 0 : m2pp_imagpart_expr (s, t);
2067 : 0 : break;
2068 : 0 : case CONST_DECL:
2069 : 0 : m2pp_identifier (s, t);
2070 : 0 : break;
2071 : 0 : case POINTER_PLUS_EXPR:
2072 : 0 : m2pp_binary (s, t, "+");
2073 : 0 : break;
2074 : 0 : case CLEANUP_POINT_EXPR:
2075 : 0 : m2pp_cleanup_point_expr (s, t);
2076 : 0 : break;
2077 : 0 : case BIT_IOR_EXPR:
2078 : 0 : m2pp_bit_ior_expr (s, t);
2079 : 0 : break;
2080 : 0 : case TRUTH_ANDIF_EXPR:
2081 : 0 : m2pp_truth_expr (s, t, "AND");
2082 : 0 : break;
2083 : 0 : case TRUTH_ORIF_EXPR:
2084 : 0 : m2pp_truth_expr (s, t, "OR");
2085 : 0 : break;
2086 : 0 : default:
2087 : 0 : m2pp_unknown (s, __FUNCTION__, get_tree_code_name (code));
2088 : : }
2089 : 0 : }
2090 : :
2091 : : /* non_lvalue_expr indicates that operand 0 is not an lvalue. */
2092 : :
2093 : : static void
2094 : 0 : m2pp_non_lvalue_expr (pretty *s, tree t)
2095 : : {
2096 : 0 : m2pp_needspace (s);
2097 : 0 : m2pp_print (s, "assert_non_lvalue(");
2098 : 0 : m2pp_needspace (s);
2099 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
2100 : 0 : m2pp_needspace (s);
2101 : 0 : m2pp_print (s, ")");
2102 : 0 : }
2103 : :
2104 : : /* m2pp_array_ref prints out the array reference. */
2105 : :
2106 : : static void
2107 : 0 : m2pp_array_ref (pretty *s, tree t)
2108 : : {
2109 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
2110 : 0 : m2pp_print (s, "[");
2111 : 0 : m2pp_expression (s, TREE_OPERAND (t, 1));
2112 : 0 : m2pp_print (s, "]");
2113 : 0 : }
2114 : :
2115 : : /* m2pp_ssa prints out the ssa variable name. */
2116 : :
2117 : : static void
2118 : 0 : m2pp_ssa (pretty *s, tree t)
2119 : : {
2120 : 0 : m2pp_identifier (s, SSA_NAME_VAR (t));
2121 : 0 : }
2122 : :
2123 : : /* m2pp_binary print the binary operator, p, and lhs, rhs. */
2124 : :
2125 : : static void
2126 : 0 : m2pp_binary (pretty *s, tree t, const char *p)
2127 : : {
2128 : 0 : tree left = TREE_OPERAND (t, 0);
2129 : 0 : tree right = TREE_OPERAND (t, 1);
2130 : :
2131 : 0 : m2pp_expression (s, left);
2132 : 0 : m2pp_needspace (s);
2133 : 0 : m2pp_print (s, p);
2134 : 0 : m2pp_needspace (s);
2135 : 0 : m2pp_expression (s, right);
2136 : 0 : }
2137 : :
2138 : : /* m2pp_unary print the unary operator, p, and expression. */
2139 : :
2140 : : static void
2141 : 0 : m2pp_unary (pretty *s, tree t, const char *p)
2142 : : {
2143 : 0 : tree expr = TREE_OPERAND (t, 0);
2144 : :
2145 : 0 : m2pp_needspace (s);
2146 : 0 : m2pp_print (s, p);
2147 : 0 : m2pp_expression (s, expr);
2148 : 0 : }
2149 : :
2150 : : /* m2pp_integer_cst displays the integer constant. */
2151 : :
2152 : : static void
2153 : 0 : m2pp_integer_cst (pretty *s, tree t)
2154 : : {
2155 : 0 : char val[100];
2156 : :
2157 : 0 : snprintf (val, 100, HOST_WIDE_INT_PRINT_UNSIGNED, TREE_INT_CST_LOW (t));
2158 : 0 : m2pp_print (s, val);
2159 : 0 : }
2160 : :
2161 : : /* m2pp_real_cst displays the real constant. */
2162 : :
2163 : : static void
2164 : 0 : m2pp_real_cst (pretty *s, tree t ATTRIBUTE_UNUSED)
2165 : : {
2166 : 0 : m2pp_print (s, "<unknown real>");
2167 : 0 : }
2168 : :
2169 : : /* m2pp_string_cst displays the real constant. */
2170 : :
2171 : : static void
2172 : 0 : m2pp_string_cst (pretty *s, tree t)
2173 : : {
2174 : 0 : const char *p = TREE_STRING_POINTER (t);
2175 : 0 : int i = 0;
2176 : :
2177 : 0 : m2pp_print (s, "\"");
2178 : 0 : while (p[i] != '\0')
2179 : : {
2180 : 0 : m2pp_print_char (s, p[i]);
2181 : 0 : i++;
2182 : : }
2183 : 0 : m2pp_print (s, "\"");
2184 : 0 : }
2185 : :
2186 : : /* m2pp_statement_sequence iterates over a statement list
2187 : : displaying each statement in turn. */
2188 : :
2189 : : static void
2190 : 0 : m2pp_statement_sequence (pretty *s, tree t)
2191 : : {
2192 : 0 : if (t != NULL_TREE)
2193 : : {
2194 : 0 : if (TREE_CODE (t) == STATEMENT_LIST)
2195 : : {
2196 : 0 : tree_stmt_iterator i;
2197 : 0 : m2pp_print (s, "(* statement list *)\n");
2198 : :
2199 : 0 : for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
2200 : 0 : m2pp_statement (s, *tsi_stmt_ptr (i));
2201 : : }
2202 : : else
2203 : 0 : m2pp_statement (s, t);
2204 : : }
2205 : 0 : }
2206 : :
2207 : : /* m2pp_unknown displays an error message. */
2208 : :
2209 : : static void
2210 : 0 : m2pp_unknown (pretty *s, const char *s1, const char *s2)
2211 : : {
2212 : 0 : m2pp_begin (s);
2213 : 0 : m2pp_print (s, s1);
2214 : 0 : m2pp_needspace (s);
2215 : 0 : m2pp_print (s, s2);
2216 : 0 : m2pp_needspace (s);
2217 : 0 : }
2218 : :
2219 : : /* m2pp_throw displays a throw statement. */
2220 : :
2221 : : static void
2222 : 0 : m2pp_throw (pretty *s, tree t)
2223 : : {
2224 : 0 : tree expr = TREE_OPERAND (t, 0);
2225 : :
2226 : 0 : m2pp_begin (s);
2227 : 0 : if (expr == NULL_TREE)
2228 : 0 : m2pp_print (s, "THROW ;\n");
2229 : : else
2230 : : {
2231 : 0 : m2pp_print (s, "THROW (");
2232 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
2233 : 0 : m2pp_print (s, ")\n");
2234 : : }
2235 : 0 : }
2236 : :
2237 : : /* m2pp_catch_expr attempts to reconstruct a catch expr. */
2238 : :
2239 : : static void
2240 : 0 : m2pp_catch_expr (pretty *s, tree t)
2241 : : {
2242 : 0 : tree types = CATCH_TYPES (t);
2243 : 0 : tree body = CATCH_BODY (t);
2244 : :
2245 : 0 : m2pp_print (s, "(* CATCH expression ");
2246 : 0 : if (types != NULL_TREE)
2247 : : {
2248 : 0 : m2pp_print (s, "(");
2249 : 0 : m2pp_expression (s, types);
2250 : 0 : m2pp_print (s, ")");
2251 : : }
2252 : 0 : m2pp_print (s, "*)\n");
2253 : 0 : m2pp_print (s, "(* catch body *)\n");
2254 : 0 : m2pp_statement_sequence (s, body);
2255 : 0 : m2pp_print (s, "(* end catch body *)\n");
2256 : 0 : }
2257 : :
2258 : : /* m2pp_try_finally_expr attemts to reconstruct a try finally expr. */
2259 : :
2260 : : static void
2261 : 0 : m2pp_try_finally_expr (pretty *s, tree t)
2262 : : {
2263 : 0 : m2pp_begin (s);
2264 : 0 : m2pp_print (s, "(* try_finally_expr *)\n");
2265 : 0 : setindent (s, getindent (s) + 3);
2266 : 0 : m2pp_statement_sequence (s, TREE_OPERAND (t, 0));
2267 : 0 : setindent (s, getindent (s) - 3);
2268 : 0 : m2pp_print (s,
2269 : : "(* finally (cleanup which is executed after the above) *)\n");
2270 : 0 : setindent (s, getindent (s) + 3);
2271 : 0 : m2pp_statement_sequence (s, TREE_OPERAND (t, 1));
2272 : 0 : setindent (s, getindent (s) - 3);
2273 : 0 : m2pp_print (s, "(* end try_finally_expr *)\n");
2274 : 0 : }
2275 : :
2276 : : /* m2pp_if_stmt pretty print a if_stmt tree. Modula-2 does not use this to
2277 : : generate IF THEN ELSE END statements, instead it uses labels and gotos. */
2278 : :
2279 : : static void
2280 : 0 : m2pp_if_stmt (pretty *s, tree t)
2281 : : {
2282 : 0 : m2pp_print (s, "(* An if_stmt node. *)\n");
2283 : 0 : m2pp_print (s, "IF ");
2284 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
2285 : 0 : m2pp_print (s, "\n");
2286 : 0 : m2pp_print (s, "THEN\n");
2287 : 0 : setindent (s, getindent (s) + 3);
2288 : 0 : m2pp_statement_sequence (s, TREE_OPERAND (t, 1));
2289 : 0 : setindent (s, getindent (s) - 3);
2290 : 0 : m2pp_print (s, "ELSE\n");
2291 : 0 : setindent (s, getindent (s) + 3);
2292 : 0 : m2pp_statement_sequence (s, TREE_OPERAND (t, 2));
2293 : 0 : setindent (s, getindent (s) - 3);
2294 : 0 : m2pp_print (s, "END\n");
2295 : 0 : }
2296 : :
2297 : : static void
2298 : 0 : m2pp_asm_expr (pretty *state, tree node)
2299 : : {
2300 : 0 : m2pp_begin (state);
2301 : 0 : m2pp_print (state, "ASM");
2302 : 0 : m2pp_needspace (state);
2303 : 0 : if (ASM_VOLATILE_P (node))
2304 : : {
2305 : 0 : m2pp_print (state, "VOLATILE");
2306 : 0 : m2pp_needspace (state);
2307 : : }
2308 : 0 : m2pp_print (state, "(");
2309 : 0 : m2pp_expression (state, ASM_STRING (node));
2310 : 0 : m2pp_print (state, ":");
2311 : 0 : m2pp_needspace (state);
2312 : 0 : m2pp_expression (state, ASM_OUTPUTS (node));
2313 : 0 : m2pp_print (state, ":");
2314 : 0 : m2pp_needspace (state);
2315 : 0 : m2pp_expression (state, ASM_INPUTS (node));
2316 : 0 : if (ASM_CLOBBERS (node) != NULL)
2317 : : {
2318 : 0 : m2pp_print (state, ":");
2319 : 0 : m2pp_needspace (state);
2320 : 0 : m2pp_expression (state, ASM_CLOBBERS (node));
2321 : : }
2322 : 0 : m2pp_print (state, ");\n");
2323 : 0 : }
2324 : :
2325 : : /* m2pp_statement attempts to reconstruct a statement. */
2326 : :
2327 : : static void
2328 : 0 : m2pp_statement (pretty *s, tree t)
2329 : : {
2330 : 0 : enum tree_code code = TREE_CODE (t);
2331 : :
2332 : 0 : m2pp_loc (s, t);
2333 : 0 : switch (code)
2334 : : {
2335 : 0 : case COND_EXPR:
2336 : 0 : m2pp_conditional (s, t);
2337 : 0 : break;
2338 : 0 : case LABEL_EXPR:
2339 : 0 : m2pp_label_expr (s, t);
2340 : 0 : break;
2341 : 0 : case LABEL_DECL:
2342 : 0 : m2pp_label_decl (s, t);
2343 : 0 : break;
2344 : 0 : case GOTO_EXPR:
2345 : 0 : m2pp_goto (s, t);
2346 : 0 : break;
2347 : 0 : case INIT_EXPR:
2348 : 0 : case MODIFY_EXPR:
2349 : 0 : m2pp_assignment (s, t);
2350 : 0 : break;
2351 : 0 : case CALL_EXPR:
2352 : 0 : m2pp_procedure_call (s, t);
2353 : 0 : break;
2354 : 0 : case BLOCK:
2355 : 0 : m2pp_block_list (s, t);
2356 : 0 : break;
2357 : 0 : case BIND_EXPR:
2358 : 0 : m2pp_bind_expr (s, t);
2359 : 0 : break;
2360 : 0 : case RETURN_EXPR:
2361 : 0 : m2pp_return_expr (s, t);
2362 : 0 : break;
2363 : 0 : case DECL_EXPR:
2364 : 0 : m2pp_decl_expr (s, t);
2365 : 0 : break;
2366 : 0 : case TRY_BLOCK:
2367 : 0 : m2pp_try_block (s, t);
2368 : 0 : break;
2369 : 0 : case HANDLER:
2370 : 0 : m2pp_handler (s, t);
2371 : 0 : break;
2372 : 0 : case CLEANUP_POINT_EXPR:
2373 : 0 : m2pp_cleanup_point_expr (s, t);
2374 : 0 : break;
2375 : 0 : case THROW_EXPR:
2376 : 0 : m2pp_throw (s, t);
2377 : 0 : break;
2378 : 0 : case TRY_CATCH_EXPR:
2379 : 0 : m2pp_try_catch_expr (s, t);
2380 : 0 : break;
2381 : 0 : case TRY_FINALLY_EXPR:
2382 : 0 : m2pp_try_finally_expr (s, t);
2383 : 0 : break;
2384 : 0 : case CATCH_EXPR:
2385 : 0 : m2pp_catch_expr (s, t);
2386 : 0 : break;
2387 : 0 : case ASM_EXPR:
2388 : 0 : m2pp_asm_expr (s, t);
2389 : 0 : break;
2390 : 0 : case IF_STMT:
2391 : 0 : m2pp_if_stmt (s, t);
2392 : 0 : break;
2393 : 0 : case ERROR_MARK:
2394 : 0 : m2pp_print (s, "<ERROR CODE>\n");
2395 : 0 : break;
2396 : 0 : default:
2397 : 0 : m2pp_unknown (s, __FUNCTION__, get_tree_code_name (TREE_CODE (t)));
2398 : : }
2399 : 0 : }
2400 : :
2401 : : /* m2pp_try_catch_expr is used after gimplification. */
2402 : :
2403 : : static void
2404 : 0 : m2pp_try_catch_expr (pretty *s, tree t)
2405 : : {
2406 : 0 : m2pp_print (s, "(* try_catch_expr begins *)\n");
2407 : 0 : m2pp_statement_sequence (s, TREE_OPERAND (t, 0));
2408 : 0 : setindent (s, 0);
2409 : 0 : m2pp_print (s, "EXCEPT\n");
2410 : 0 : setindent (s, 3);
2411 : 0 : m2pp_statement_sequence (s, TREE_OPERAND (t, 1));
2412 : 0 : m2pp_print (s, "(* try_catch_expr ends *)\n");
2413 : 0 : }
2414 : :
2415 : : /* m2pp_cleanup_point_expr emits a comment indicating a GCC
2416 : : cleanup_point_expr is present. */
2417 : :
2418 : : static void
2419 : 0 : m2pp_cleanup_point_expr (pretty *s, tree t)
2420 : : {
2421 : 0 : m2pp_begin (s);
2422 : 0 : m2pp_print (s, "(* Cleanup point begins. *)\n");
2423 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
2424 : 0 : m2pp_print (s, "(* Cleanup point ends. *)\n");
2425 : 0 : }
2426 : :
2427 : : /* m2pp_decl_expr displays a local declaration. */
2428 : :
2429 : : static void
2430 : 0 : m2pp_decl_expr (pretty *s, tree t)
2431 : : {
2432 : 0 : m2pp_var (s);
2433 : 0 : m2pp_print (s, "(* Variable in decl_expr. *)\n");
2434 : 0 : m2pp_var_type_decl (s, DECL_EXPR_DECL (t));
2435 : 0 : }
2436 : :
2437 : : /* m2pp_procedure_call print a call to a procedure. */
2438 : :
2439 : : static void
2440 : 0 : m2pp_procedure_call (pretty *s, tree t)
2441 : : {
2442 : 0 : m2pp_begin (s);
2443 : 0 : m2pp_call_expr (s, t);
2444 : 0 : m2pp_needspace (s);
2445 : 0 : m2pp_print (s, ";\n");
2446 : 0 : }
2447 : :
2448 : : /* args displays each argument in an iter list by calling expression. */
2449 : :
2450 : : static void
2451 : 0 : m2pp_args (pretty *s, tree e)
2452 : : {
2453 : 0 : call_expr_arg_iterator iter;
2454 : 0 : tree arg;
2455 : :
2456 : 0 : m2pp_print (s, "(");
2457 : 0 : m2pp_needspace (s);
2458 : 0 : FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
2459 : : {
2460 : 0 : m2pp_expression (s, arg);
2461 : 0 : if (more_call_expr_args_p (&iter))
2462 : : {
2463 : 0 : m2pp_print (s, ",");
2464 : 0 : m2pp_needspace (s);
2465 : : }
2466 : : }
2467 : 0 : m2pp_print (s, ")");
2468 : 0 : }
2469 : :
2470 : : /* m2pp_call_expr print a call to a procedure or function. */
2471 : :
2472 : : static void
2473 : 0 : m2pp_call_expr (pretty *s, tree t)
2474 : : {
2475 : 0 : tree call = CALL_EXPR_FN (t);
2476 : 0 : tree args = TREE_OPERAND (t, 1);
2477 : 0 : tree type = TREE_TYPE (t);
2478 : 0 : bool has_return_type = true;
2479 : 0 : tree proc;
2480 : :
2481 : 0 : if (type && VOID_TYPE_P (type))
2482 : 0 : has_return_type = false;
2483 : :
2484 : 0 : if (TREE_CODE (call) == ADDR_EXPR || TREE_CODE (call) == NON_LVALUE_EXPR)
2485 : 0 : proc = TREE_OPERAND (call, 0);
2486 : : else
2487 : : proc = call;
2488 : :
2489 : 0 : m2pp_expression (s, proc);
2490 : 0 : if (args || has_return_type)
2491 : 0 : m2pp_args (s, t);
2492 : 0 : }
2493 : :
2494 : : /* m2pp_return_expr displays the return statement. */
2495 : :
2496 : : static void
2497 : 0 : m2pp_return_expr (pretty *s, tree t)
2498 : : {
2499 : 0 : tree e = TREE_OPERAND (t, 0);
2500 : :
2501 : 0 : m2pp_begin (s);
2502 : 0 : if (e == NULL_TREE)
2503 : : {
2504 : 0 : m2pp_print (s, "RETURN");
2505 : : }
2506 : 0 : else if (TREE_CODE (e) == MODIFY_EXPR || (TREE_CODE (e) == INIT_EXPR))
2507 : : {
2508 : 0 : m2pp_assignment (s, e);
2509 : 0 : m2pp_print (s, "RETURN");
2510 : 0 : m2pp_needspace (s);
2511 : 0 : m2pp_expression (s, TREE_OPERAND (e, 0));
2512 : : }
2513 : : else
2514 : : {
2515 : 0 : m2pp_print (s, "RETURN");
2516 : 0 : m2pp_needspace (s);
2517 : 0 : m2pp_expression (s, e);
2518 : : }
2519 : 0 : m2pp_needspace (s);
2520 : 0 : m2pp_print (s, ";\n");
2521 : 0 : }
2522 : :
2523 : : /* m2pp_try_block displays the try block. */
2524 : :
2525 : : static void
2526 : 0 : m2pp_try_block (pretty *s, tree t)
2527 : : {
2528 : 0 : tree stmts = TRY_STMTS (t);
2529 : 0 : tree handlers = TRY_HANDLERS (t);
2530 : :
2531 : 0 : m2pp_begin (s);
2532 : 0 : m2pp_print (s, "(* TRY *)\n");
2533 : 0 : m2pp_statement_sequence (s, stmts);
2534 : 0 : setindent (s, 0);
2535 : 0 : m2pp_print (s, "EXCEPT\n");
2536 : 0 : setindent (s, 3);
2537 : 0 : m2pp_statement_sequence (s, handlers);
2538 : 0 : m2pp_print (s, "(* END TRY *)\n");
2539 : 0 : }
2540 : :
2541 : : /* m2pp_try_block displays the handler block. */
2542 : :
2543 : : static void
2544 : 0 : m2pp_handler (pretty *s, tree t)
2545 : : {
2546 : 0 : tree parms = HANDLER_PARMS (t);
2547 : 0 : tree body = HANDLER_BODY (t);
2548 : 0 : tree type = HANDLER_TYPE (t);
2549 : :
2550 : 0 : m2pp_print (s, "(* handler *)\n");
2551 : 0 : if (parms != NULL_TREE)
2552 : : {
2553 : 0 : m2pp_print (s, "(* handler parameter has a type (should be NULL_TREE) "
2554 : : "in Modula-2 *)\n");
2555 : 0 : m2pp_print (s, "CATCH (");
2556 : 0 : m2pp_expression (s, parms);
2557 : 0 : m2pp_print (s, ")\n");
2558 : : }
2559 : 0 : if (type != NULL_TREE)
2560 : 0 : m2pp_print (s, "(* handler type (should be NULL_TREE) in Modula-2 *)\n");
2561 : 0 : m2pp_statement_sequence (s, body);
2562 : 0 : }
2563 : :
2564 : : /* m2pp_assignment prints out the assignment statement. */
2565 : :
2566 : : static void
2567 : 0 : m2pp_assignment (pretty *s, tree t)
2568 : : {
2569 : 0 : int o;
2570 : :
2571 : 0 : m2pp_begin (s);
2572 : :
2573 : : /* Print the types of des and expr. */
2574 : 0 : m2pp_type (s, TREE_TYPE (TREE_OPERAND (t, 0)));
2575 : 0 : m2pp_needspace (s);
2576 : 0 : m2pp_print (s, ":=");
2577 : 0 : m2pp_needspace (s);
2578 : 0 : m2pp_type (s, TREE_TYPE (TREE_OPERAND (t, 1)));
2579 : 0 : m2pp_needspace (s);
2580 : 0 : m2pp_print (s, ";\n");
2581 : : /* Print the assignment statement. */
2582 : 0 : m2pp_designator (s, TREE_OPERAND (t, 0));
2583 : 0 : m2pp_needspace (s);
2584 : 0 : m2pp_print (s, ":=");
2585 : 0 : m2pp_needspace (s);
2586 : 0 : o = getindent (s);
2587 : 0 : setindent (s, getcurpos (s) + 1);
2588 : 0 : m2pp_expression (s, TREE_OPERAND (t, 1));
2589 : 0 : m2pp_needspace (s);
2590 : 0 : m2pp_print (s, ";\n");
2591 : 0 : setindent (s, o);
2592 : 0 : }
2593 : :
2594 : : /* m2pp_designator displays the lhs of an assignment. */
2595 : :
2596 : : static void
2597 : 0 : m2pp_designator (pretty *s, tree t)
2598 : : {
2599 : 0 : m2pp_expression (s, t);
2600 : 0 : }
2601 : :
2602 : : /* m2pp_indirect_ref displays the indirect operator. */
2603 : :
2604 : : static void
2605 : 0 : m2pp_indirect_ref (pretty *s, tree t)
2606 : : {
2607 : 0 : m2pp_print (s, "(");
2608 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
2609 : 0 : m2pp_print (s, ")^");
2610 : 0 : }
2611 : :
2612 : : /* m2pp_conditional builds an IF THEN ELSE END. With more work
2613 : : this should be moved into statement sequence which could look for
2614 : : repeat and while loops. */
2615 : :
2616 : : static void
2617 : 0 : m2pp_conditional (pretty *s, tree t)
2618 : : {
2619 : 0 : int o;
2620 : :
2621 : 0 : m2pp_begin (s);
2622 : 0 : m2pp_print (s, "IF");
2623 : 0 : m2pp_needspace (s);
2624 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
2625 : 0 : m2pp_print (s, "\nTHEN\n");
2626 : 0 : o = getindent (s);
2627 : 0 : setindent (s, o + 3);
2628 : 0 : m2pp_statement_sequence (s, TREE_OPERAND (t, 1));
2629 : 0 : setindent (s, o);
2630 : 0 : if (TREE_OPERAND (t, 2) != NULL_TREE)
2631 : : {
2632 : 0 : m2pp_print (s, "ELSE\n");
2633 : 0 : setindent (s, o + 3);
2634 : 0 : m2pp_statement_sequence (s, TREE_OPERAND (t, 2));
2635 : 0 : setindent (s, o);
2636 : : }
2637 : 0 : m2pp_print (s, "END ;\n");
2638 : 0 : }
2639 : :
2640 : : /* m2pp_label_decl displays a label. Again should be moved into
2641 : : statement sequence to determine proper loop constructs. */
2642 : :
2643 : : static void
2644 : 0 : m2pp_label_decl (pretty *s, tree t)
2645 : : {
2646 : 0 : m2pp_begin (s);
2647 : 0 : m2pp_print (s, "(* label ");
2648 : 0 : m2pp_identifier (s, t);
2649 : 0 : m2pp_print (s, ": *)\n");
2650 : 0 : }
2651 : :
2652 : : /* m2pp_label_expr skips the LABEL_EXPR to find the LABEL_DECL. */
2653 : :
2654 : : static void
2655 : 0 : m2pp_label_expr (pretty *s, tree t)
2656 : : {
2657 : 0 : m2pp_begin (s);
2658 : 0 : m2pp_statement (s, TREE_OPERAND (t, 0));
2659 : 0 : }
2660 : :
2661 : : /* m2pp_goto displays a goto statement. Again should be moved into
2662 : : statement sequence to determine proper loop constructs. */
2663 : :
2664 : : static void
2665 : 0 : m2pp_goto (pretty *s, tree t)
2666 : : {
2667 : 0 : m2pp_begin (s);
2668 : 0 : m2pp_print (s, "(* goto ");
2669 : 0 : m2pp_identifier (s, TREE_OPERAND (t, 0));
2670 : 0 : m2pp_print (s, " *)\n");
2671 : 0 : }
2672 : :
2673 : : /* m2pp_list prints a TREE_CHAINed list. */
2674 : :
2675 : : static void
2676 : 0 : m2pp_list (pretty *s, tree t)
2677 : : {
2678 : 0 : tree u = t;
2679 : :
2680 : 0 : m2pp_print (s, "(");
2681 : 0 : m2pp_needspace (s);
2682 : 0 : while (t != NULL_TREE)
2683 : : {
2684 : 0 : m2pp_expression (s, TREE_VALUE (t));
2685 : 0 : t = TREE_CHAIN (t);
2686 : 0 : if (t == u || t == NULL_TREE)
2687 : : break;
2688 : 0 : m2pp_print (s, ",");
2689 : 0 : m2pp_needspace (s);
2690 : : }
2691 : 0 : m2pp_needspace (s);
2692 : 0 : m2pp_print (s, ")");
2693 : 0 : }
2694 : :
2695 : : /* m2pp_offset displays the offset operator. */
2696 : :
2697 : : static void
2698 : 0 : m2pp_offset (pretty *s, tree t)
2699 : : {
2700 : 0 : tree type = TREE_TYPE (t);
2701 : 0 : tree base = TYPE_OFFSET_BASETYPE (t);
2702 : :
2703 : 0 : m2pp_print (s, "OFFSET (");
2704 : 0 : m2pp_type (s, base);
2705 : 0 : m2pp_print (s, ".");
2706 : 0 : m2pp_type (s, type);
2707 : 0 : m2pp_print (s, ")");
2708 : 0 : }
2709 : :
2710 : : /* m2pp_addr_expr create an ADR expression. */
2711 : :
2712 : : static void
2713 : 0 : m2pp_addr_expr (pretty *s, tree t)
2714 : : {
2715 : 0 : m2pp_needspace (s);
2716 : 0 : m2pp_print (s, "ADR (");
2717 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
2718 : 0 : m2pp_print (s, ")");
2719 : 0 : }
2720 : :
2721 : : /* m2pp_nop generate a CAST expression. */
2722 : :
2723 : : static void
2724 : 0 : m2pp_nop (pretty *s, tree t)
2725 : : {
2726 : 0 : m2pp_needspace (s);
2727 : 0 : m2pp_print (s, "CAST (");
2728 : 0 : m2pp_simple_type (s, TREE_TYPE (t));
2729 : 0 : m2pp_print (s, ", ");
2730 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
2731 : 0 : m2pp_print (s, ")");
2732 : 0 : }
2733 : :
2734 : : /* m2pp_convert generate a CONVERT expression. */
2735 : :
2736 : : static void
2737 : 0 : m2pp_convert (pretty *s, tree t)
2738 : : {
2739 : 0 : m2pp_needspace (s);
2740 : 0 : m2pp_print (s, "CONVERT (");
2741 : 0 : m2pp_simple_type (s, TREE_TYPE (t));
2742 : 0 : m2pp_print (s, ", ");
2743 : 0 : m2pp_expression (s, TREE_OPERAND (t, 0));
2744 : 0 : m2pp_print (s, ")");
2745 : 0 : }
2746 : :
2747 : : /* m2pp_var_decl generate a variable. */
2748 : :
2749 : : static void
2750 : 0 : m2pp_var_decl (pretty *s, tree t)
2751 : : {
2752 : 0 : m2pp_identifier (s, t);
2753 : 0 : }
2754 : :
2755 : : /* m2pp_result_decl generate a result declaration (variable). */
2756 : :
2757 : : static void
2758 : 0 : m2pp_result_decl (pretty *s, tree t)
2759 : : {
2760 : 0 : m2pp_identifier (s, t);
2761 : 0 : }
2762 : :
2763 : : /* m2pp_component_ref generate a record field access. */
2764 : :
2765 : : static void
2766 : 0 : m2pp_component_ref (pretty *s, tree t)
2767 : : {
2768 : 0 : m2pp_simple_expression (s, TREE_OPERAND (t, 0));
2769 : 0 : m2pp_print (s, ".");
2770 : 0 : m2pp_simple_expression (s, TREE_OPERAND (t, 1));
2771 : 0 : }
2772 : :
2773 : : }
2774 : :
2775 : : /* Code interface to this module. */
2776 : :
2777 : : /* CreateDumpGimple creates the dump files using the template name. */
2778 : :
2779 : : void
2780 : 0 : m2pp_CreateDumpGimple (char *template_name, int template_len)
2781 : : {
2782 : 0 : int kind = M2PP_DUMP_STDOUT;
2783 : 0 : modula2::m2pp_output_file[kind] = stdout;
2784 : 0 : kind++;
2785 : 0 : for (; kind < M2PP_DUMP_END; kind++)
2786 : : {
2787 : 0 : if (kind == M2PP_DUMP_FD)
2788 : 0 : modula2::m2pp_output_file[kind] = NULL;
2789 : : else
2790 : : {
2791 : 0 : char *name = (char *)alloca (template_len);
2792 : :
2793 : 0 : snprintf (name, template_len, template_name, kind);
2794 : 0 : modula2::m2pp_output_file[kind] = fopen (name, "w");
2795 : 0 : if (modula2::m2pp_output_file[kind] == NULL)
2796 : : {
2797 : 0 : fprintf (stderr, "unable to create dump file %s: %s\n",
2798 : 0 : name, xstrerror (errno));
2799 : 0 : exit (1);
2800 : : }
2801 : 0 : fprintf (modula2::m2pp_output_file[kind], "%s\n\n",
2802 : : m2pp_dump_description[kind]);
2803 : : }
2804 : : }
2805 : 0 : }
2806 : :
2807 : : /* Close all dump files and fflush stdout. */
2808 : :
2809 : : void
2810 : 0 : m2pp_CloseDumpGimple (void)
2811 : : {
2812 : 0 : int kind = M2PP_DUMP_STDOUT;
2813 : 0 : fflush (modula2::m2pp_output_file[kind]);
2814 : 0 : kind++;
2815 : 0 : for (; kind < M2PP_DUMP_END; kind++)
2816 : 0 : if (kind != M2PP_DUMP_FD)
2817 : 0 : fclose (modula2::m2pp_output_file[kind]);
2818 : 0 : }
2819 : :
2820 : : /* m2pp_dump_gimple_pretty create an initPretty object and print
2821 : : fndecl to kind output. */
2822 : :
2823 : : void
2824 : 0 : m2pp_dump_gimple_pretty (m2pp_dump_kind kind, tree fndecl)
2825 : : {
2826 : 0 : modula2::pretty *state = modula2::initPretty (kind, 0);
2827 : :
2828 : 0 : modula2::m2pp_print (state, "\n");
2829 : 0 : if (TREE_CODE (fndecl) == TRANSLATION_UNIT_DECL)
2830 : 0 : modula2::m2pp_translation (state, fndecl);
2831 : 0 : else if (TREE_CODE (fndecl) == BLOCK)
2832 : 0 : modula2::m2pp_module_block (state, fndecl);
2833 : 0 : else if (TREE_CODE (fndecl) == FUNCTION_DECL)
2834 : 0 : modula2::m2pp_function (state, fndecl);
2835 : : else
2836 : 0 : modula2::m2pp_statement_sequence (state, fndecl);
2837 : 0 : modula2::killPretty (state);
2838 : 0 : }
2839 : :
2840 : :
2841 : : /* Generate modula-2 style gimple for fndecl. */
2842 : :
2843 : : void
2844 : 220112 : m2pp_dump_gimple (m2pp_dump_kind kind, tree fndecl)
2845 : : {
2846 : 220112 : if (M2Options_GetDumpGimple ()
2847 : 220112 : && M2LangDump_IsDumpRequiredTree (fndecl, true))
2848 : 0 : m2pp_dump_gimple_pretty (kind, fndecl);
2849 : 220112 : }
2850 : :
2851 : :
2852 : : /* Dump fndecl to a file descriptor. */
2853 : :
2854 : : void
2855 : 0 : m2pp_DumpGimpleFd (int fd, tree fndecl)
2856 : : {
2857 : 0 : FILE *f = fdopen (fd, "a");
2858 : 0 : if (f != NULL)
2859 : : {
2860 : : #if 0
2861 : : modula2::m2pp_output_file[M2PP_DUMP_FD] = f;
2862 : : m2pp_dump_gimple_pretty (M2PP_DUMP_FD, fndecl);
2863 : : fprintf (f, "\n");
2864 : : #endif
2865 : 0 : print_node (f, "m2 tree", fndecl, 1);
2866 : 0 : fprintf (f, "\n\n");
2867 : 0 : fflush (f);
2868 : : #if 0
2869 : : modula2::m2pp_output_file[M2PP_DUMP_FD] = NULL;
2870 : : #endif
2871 : : }
2872 : 0 : }
|