PIPS
print_code_smalltalk.c
Go to the documentation of this file.
1 /*
2 
3  $Id: print_code_smalltalk.c 23065 2016-03-02 09:05:50Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 #ifdef HAVE_CONFIG_H
25  #include "pips_config.h"
26 #endif
27 /*
28  *
29  * This phase is used for PHRASE project.
30  *
31  *
32  * NB: The PHRASE project is an attempt to automatically (or semi-automatically)
33  * transform high-level language for partial evaluation in reconfigurable logic
34  * (such as FPGAs or DataPaths).
35  *
36  * This pass is used in context of PHRASE project for synthetisation of
37  * reconfigurable logic for a portion of initial code. This function can be
38  * viewed as a Smalltalk pretty-printer of a subset of Fortran.
39  *
40  * alias print_code_smalltalk 'Smalltalk Pretty-Printer'
41  *
42  * print_code_smalltalk > MODULE.smalltalk_code
43  * < PROGRAM.entities
44  * < MODULE.code
45  *
46  * The Smalltalk code will be available in SMALLTALK_CODE_FILE
47  *
48  * NB: This code is highly inspired from PRINT_C_CODE phase written by nguyen
49  *
50  */
51 
52 #include <stdio.h>
53 #include <ctype.h>
54 
55 #include "genC.h"
56 #include "linear.h"
57 #include "ri.h"
58 #include "effects.h"
59 
60 #include "resources.h"
61 
62 #include "misc.h"
63 #include "ri-util.h"
64 #include "prettyprint.h"
65 #include "effects-util.h"
66 #include "pipsdbm.h"
67 #include "text-util.h"
68 
69 
70 #define STPRETTY ".st"
71 
72 #include "smalltalk-defs.h"
73 
74 #define RESULT_NAME "result"
75 
76 #define current_module_is_a_function() \
77  (entity_function_p(get_current_module_entity()))
78 
79 static string st_statement(statement s);
80 static string st_sequence(sequence seq);
81 static string st_call(call c);
82 static string st_expression(expression);
83 static string st_reference(reference r);
84 static string st_test(test t);
85 static string st_loop(loop l);
86 static string st_whileloop(whileloop w);
87 static string st_forloop(forloop f);
88 
89 
90 /**
91  * Return beautified string representing name for entity var
92  */
93 static string st_entity_local_name(entity var)
94 {
95  const char* name;
96 
97  pips_debug(6,"st_entity_local_name was : %s\n",entity_local_name(var));
98 
100  var != get_current_module_entity() &&
103  name = RESULT_NAME;
104  }
105  else
106  {
107  name = entity_local_name(var);
108 
109  /* Delete all the prefixes */
110 
111  if (strstr(name,STRUCT_PREFIX) != NULL)
112  name = strstr(name,STRUCT_PREFIX) + 1;
113  if (strstr(name,UNION_PREFIX) != NULL)
114  name = strstr(name,UNION_PREFIX) + 1;
115  if (strstr(name,ENUM_PREFIX) != NULL)
116  name = strstr(name,ENUM_PREFIX) + 1;
117  if (strstr(name,TYPEDEF_PREFIX) != NULL)
118  name = strstr(name,TYPEDEF_PREFIX) + 1;
119  if (strstr(name,MEMBER_SEP_STRING) != NULL)
120  name = strstr(name,MEMBER_SEP_STRING) + 1;
121  if (strstr(name,MAIN_PREFIX) != NULL)
122  name = strstr(name,MAIN_PREFIX) + 1;
123  }
124  pips_debug(6,"st_entity_local_name is now : %s\n",name);
125  return strdup(name);
126 }
127 
128 
129 /**
130  * Return string representing expression enclosed by parenthesis
131  */
133 {
134  string result = OPENBRACKET;
136 
137  bool first = true;
138  MAP(EXPRESSION,e,
139  {
140  if (brace_expression_p(e))
141  result = strdup(concatenate(result,first?"":",",st_brace_expression_as_string(e),NULL));
142  else
143  result = strdup(concatenate(result,first?"":",",
144  expression_to_string(e),NULL));
145  first = false;
146  },args);
147  result = strdup(concatenate(result,CLOSEBRACKET,NULL));
148  return result;
149 }
150 
151 /**
152  * Return a string representing dimension reference for a
153  * dimension dim and an expression e
154  * This function automatically convert bounds in fortran to bounds
155  * starting from 0 by doing new_reference = old_reference - lower
156  * This function is valid even in case of non-directly-cumputable expressions
157  */
158 static string st_dimension_reference_as_string (dimension dim, expression old_expression) {
159 
160  intptr_t low, old;
161  string slow = NULL;
162  string sold = NULL;
163  bool low_given_by_expression = false;
164  bool old_given_by_expression = false;
165  string result = strdup(EMPTY);
166 
167  expression elow = dimension_lower(dim);
168  expression eold = old_expression;
169 
170  if (expression_integer_value(elow, &low)) {
171  low_given_by_expression = true;
172  }
173  else {
174  low_given_by_expression = false;
175  slow = st_expression(elow);
176  }
177  if (expression_integer_value(eold, &old)) {
178  old_given_by_expression = true;
179  }
180  else {
181  old_given_by_expression = false;
182  sold = st_expression(eold);
183  }
184 
185  if (low_given_by_expression) {
186  if (old_given_by_expression) {
187  pips_debug(5,"old=%"PRIdPTR" low=%"PRIdPTR"\n", old, low);
188  string istr = int2a(old-low);
189  result = strdup(concatenate(result, istr, NULL));
190  free(istr);
191  }
192  else {
193  pips_debug(5,"sold=%s low=%"PRIdPTR"\n", sold, low);
194  string istr = int2a(low);
195  result = strdup(concatenate(result, sold,"-",istr, NULL));
196  free(istr);
197  }
198  }
199  else {
200  if (old_given_by_expression) {
201  pips_debug(5,"old=%"PRIdPTR" slow=%s\n", old, slow);
202  string istr = int2a(old);
203  result = strdup(concatenate(result, istr,"-",
204  OPENBRACE,slow,CLOSEBRACE, NULL));
205  free(istr);
206  }
207  else {
208  pips_debug(5,"sold=%s slow=%s\n", sold, slow);
209  result = strdup(concatenate(result, sold,"-",OPENBRACE,slow,
210  CLOSEBRACE, NULL));
211  }
212  }
213 
214  return result;
215 }
216 
217 /**
218  * Return a string representing dimension bounds of a dimension dim
219  * This function automatically convert bounds in fortran to bounds
220  * starting from 0 by doing upbound = (upper - lower + 1)
221  * This function is valid even in case of non-directly-cumputable expressions
222  */
224 
225  intptr_t low, up;
226  string slow = NULL;
227  string sup = NULL;
228  bool low_given_by_expression = false;
229  bool up_given_by_expression = false;
230  string result = strdup(EMPTY);
231 
232  expression elow = dimension_lower(dim);
233  expression eup = dimension_upper(dim);
234 
235  if (expression_integer_value(elow, &low)) {
236  low_given_by_expression = true;
237  }
238  else {
239  low_given_by_expression = false;
240  slow = st_expression(elow);
241  }
242  if (expression_integer_value(eup, &up)) {
243  up_given_by_expression = true;
244  }
245  else {
246  up_given_by_expression = false;
247  sup = st_expression(eup);
248  }
249 
250  if (low_given_by_expression) {
251  if (up_given_by_expression) {
252  pips_debug(5,"up=%"PRIdPTR" low=%"PRIdPTR"\n", up, low);
253  string istr = int2a(up-low+1);
254  result = strdup(concatenate(result, istr, NULL));
255  free(istr);
256  }
257  else {
258  string istr = int2a(low-1);
259  pips_debug(5,"sup=%s low=%"PRIdPTR"\n", sup, low);
260  result = strdup(concatenate(result, sup,"-",istr, NULL));
261  free(istr);
262  }
263  }
264  else {
265  if (up_given_by_expression) {
266  pips_debug(5,"up=%"PRIdPTR" slow=%s\n", up, slow);
267  string istr = int2a(up+1);
268  result = strdup(concatenate(result, istr,"-",
269  OPENBRACE,slow,CLOSEBRACE, NULL));
270  free(istr);
271  }
272  else {
273  pips_debug(5,"sup=%s slow=%s\n", sup, slow);
274  result = strdup(concatenate(result, sup,"-",OPENBRACE,slow,
275  CLOSEBRACE,"+1", NULL));
276  }
277  }
278 
279  return result;
280 }
281 
282 /**
283  * Return string representing array initialization
284  * for variable svar in SMALLTALK
285  */
286 static string st_dim_string (string svar, list ldim)
287 {
288  string result = "";
289  int dimensions = 0;
290 
291  dimensions = gen_length(ldim);
292 
293  pips_debug(5,"Dimension : %d \n", dimensions);
294 
295  if (dimensions == 0) {
296  return strdup(result);
297  }
298 
299  else if (dimensions == 1) {
300 
301  dimension dim = DIMENSION(gen_nth(0,ldim));
302 
303  result = strdup(concatenate(svar, SPACE, SETVALUE, SPACE,
304  ARRAY, SPACE,
305  ARRAY_NEW, SPACE,
306  st_dimension_bound_as_string (dim), NULL));
307  return result;
308  }
309 
310  else if (dimensions == 2) {
311 
312  dimension dim = DIMENSION(gen_nth(0,ldim));
313  dimension dim2 = DIMENSION(gen_nth(1,ldim));
314 
315  result = strdup(concatenate(svar, SPACE, SETVALUE, SPACE,
316  ARRAY2D, SPACE,
319  NULL));
320  return result;
321  }
322 
323  else {
324  result = strdup("More than 2-dimensionals arrays not handled !");
325  return result;
326  }
327 }
328 
329 /**
330  * Return a string C-like representation of basic b
331  */
332 static string c_basic_string(basic b)
333 {
334  string result = "UNKNOWN_BASIC" SPACE;
335  switch (basic_tag(b))
336  {
337  case is_basic_int:
338  {
339  pips_debug(2,"Basic int\n");
340  switch (basic_int(b))
341  {
342  case 1: result = "char" SPACE;
343  break;
344  case 2: result = "short" SPACE;
345  break;
346  case 4: result = "int" SPACE;
347  break;
348  case 6: result = "long" SPACE;
349  break;
350  case 8: result = "long long" SPACE;
351  break;
352  case 11: result = "unsigned char" SPACE;
353  break;
354  case 12: result = "unsigned short" SPACE;
355  break;
356  case 14: result = "unsigned int" SPACE;
357  break;
358  case 16: result = "unsigned long" SPACE;
359  break;
360  case 18: result = "unsigned long long" SPACE;
361  break;
362  case 21: result = "signed char" SPACE;
363  break;
364  case 22: result = "signed short" SPACE;
365  break;
366  case 24: result = "signed int" SPACE;
367  break;
368  case 26: result = "signed long" SPACE;
369  break;
370  case 28: result = "signed long long" SPACE;
371  break;
372  }
373  break;
374  }
375  case is_basic_float:
376  switch (basic_float(b))
377  {
378  case 4: result = "float" SPACE;
379  break;
380  case 8: result = "double" SPACE;
381  break;
382  }
383  break;
384  case is_basic_logical:
385  result = "int" SPACE;
386  break;
387  case is_basic_string:
388  result = "char" SPACE;
389  break;
390  case is_basic_bit:
391  {
392  result = "Basic bit not handled";
393  break;
394  }
395  case is_basic_pointer:
396  {
397  result = "Basic pointer not handled";
398  break;
399  }
400  case is_basic_derived:
401  {
402  result = "Basic derived not handled";
403  break;
404  }
405  case is_basic_typedef:
406  {
407  result = "Basic typedef not handled";
408  break;
409  }
410  default:
411  pips_internal_error("case not handled");
412  }
413  return strdup(result);
414 }
415 
416 /**
417  * Return a string representing Smalltalk declaration for
418  * entity (constant or variable) var
419  * NB: old function this_entity_cdeclaration(entity var)
420  */
421 static string st_declaration(entity var)
422 {
423  string result = "Undefined entity";
424  const char* name = entity_local_name(var);
425  type t = entity_type(var);
426  storage s = entity_storage(var);
427 
428  switch (storage_tag(s)) {
429  case is_storage_rom:
430  {
431  string svar = st_entity_local_name(var);
432  result = strdup(svar);
433  free(svar);
434  }
435  default:
436  break;
437  }
438  switch (type_tag(t)) {
439  case is_type_variable:
440  {
441  string svar;
442  svar = st_entity_local_name(var);
443  result = strdup(svar);
444  free(svar);
445  break;
446  }
447  case is_type_struct:
448  {
449  result = strdup(concatenate(name, ": undefined STRUCT in SMALLTALK", NULL));
450  break;
451  }
452  case is_type_union:
453  {
454  result = strdup(concatenate(name, ": undefined UNION in SMALLTALK", NULL));
455  break;
456  }
457  case is_type_enum:
458  {
459  result = strdup(concatenate(name, ": undefined ENUM in SMALLTALK", NULL));
460  break;
461  }
462  default:
463  break;
464  }
465 
466  return result? result: strdup("");
467 }
468 
469 /**
470  * Return a string representing Smalltalk declaration
471  * initialisation for entity (constant or variable) var
472  */
473 static string st_declaration_init(entity var)
474 {
475  string result = NULL;
476  type t = entity_type(var);
477  storage s = entity_storage(var);
478 
479  pips_debug(2,"st_declaration_init for entity : %s\n",entity_name(var));
480 
481  switch (storage_tag(s)) {
482  case is_storage_rom:
483  {
484  /* This is a constant, we must initialize it */
485 
486  value va = entity_initial(var);
487 
488  if (!value_undefined_p(va))
489  {
490  constant c = NULL;
491  pips_debug(4,"Constant with defined value\n");
492  if (value_constant_p(va))
493  c = value_constant(va);
494  else if (value_symbolic_p(va))
496  if (c)
497  {
498  if (constant_int_p(c))
499  {
500  string sval = int2a(constant_int(c));
501  string svar = st_entity_local_name(var);
502  pips_debug(4,"Constant is an integer\n");
503  result = strdup(concatenate(svar, SPACE, SETVALUE
504  SPACE, sval, NULL));
505 
506  free(svar);
507  free(sval);
508  return result;
509  }
510  else
511  {
512  string svar = st_entity_local_name(var);
513  pips_debug(4,"Type of constant not handled\n");
514  result = strdup(concatenate(svar, SPACE, SETVALUE
515  SPACE, "undefined", NULL));
516  }
517  }
518  }
519  break;
520  }
521  default:
522  break;
523  }
524  switch (type_tag(t)) {
525  case is_type_variable:
526  {
527  int dimensions;
528  variable v = type_variable(t);
529  string svar;
530  value val = entity_initial(var);
531 
532  svar = st_entity_local_name(var);
533 
534  dimensions = gen_length(variable_dimensions(v));
535  pips_debug(4,"Dimensions: %zd\n", gen_length(variable_dimensions(v)));
536 
537  if (dimensions == 0) {
538 
539  if (!value_undefined_p(val)) {
540  if (value_expression_p(val)) {
541  /* This variable must be initialized
542  * Anyway, i don't know how to initialize a variable
543  * at declaration in Fortran !!! */
545  if (brace_expression_p(exp))
547  else
548  result = strdup(concatenate(result,SETVALUE,expression_to_string(exp),NULL));
549  }
550  }
551  }
552 
553  else if (dimensions < 3) {
554  pips_debug(2,"Init for arrays \n");
555  result = strdup(st_dim_string (svar, variable_dimensions(v)));
556  }
557 
558  else {
559  pips_debug(2,"Arrays dimension > 2 not handled\n");
560  }
561 
562  free(svar);
563  break;
564  }
565  case is_type_struct:
566  {
567  result = "undefined STRUCT in SMALLTALK";
568  break;
569  }
570  case is_type_union:
571  {
572  result = "undefined UNION in SMALLTALK";
573  break;
574  }
575  case is_type_enum:
576  {
577  result = "undefined ENUM in SMALLTALK";
578  break;
579  }
580  default:
581  break;
582  }
583 
584  return result;
585 }
586 
587 /**
588  * Return a string representing Smalltalk declaration
589  * initialisation for entity (constant or variable) var
590  */
591 static string st_declaration_comment(entity var)
592 {
593  string comment = "Undefined entity";
594  string svar = st_entity_local_name(var);
595  type t = entity_type(var);
596 
597  pips_debug(2,"st_declaration_comment for entity : %s\n",entity_name(var));
598 
599  switch (type_tag(t)) {
600  case is_type_variable:
601  {
602  int dimensions;
603  variable v = type_variable(t);
604  string st = c_basic_string(variable_basic(v));
605 
606  dimensions = gen_length(variable_dimensions(v));
607  pips_debug(4,"Dimensions: %zd\n", gen_length(variable_dimensions(v)));
608 
609  if (dimensions == 0) {
610  comment = strdup(concatenate(COMMENT, svar, ",", st, COMMENT, NULL));
611  }
612 
613  else if (dimensions < 3) {
614 
615  if (dimensions == 1) {
616  comment = strdup(concatenate(COMMENT, svar, ",", st, ", 1 dimension", COMMENT, NULL));
617  }
618  else if (dimensions == 2) {
619  comment = strdup(concatenate(COMMENT, svar, ",", st, ", 2 dimensions", COMMENT, NULL));
620  }
621  }
622 
623  else {
624  comment = strdup(concatenate(COMMENT, svar, ",", st, ", Arrays dimension > 2 not handled", COMMENT, NULL));
625  }
626 
627  break;
628  }
629  case is_type_struct:
630  {
631  comment = strdup(concatenate(COMMENT, svar, " : undefined STRUCT in SMALLTALK", COMMENT, NULL));
632  break;
633  }
634  case is_type_union:
635  {
636  comment = strdup(concatenate(COMMENT, svar, " : undefined UNION in SMALLTALK", COMMENT, NULL));
637  break;
638  }
639  case is_type_enum:
640  {
641  comment = strdup(concatenate(COMMENT, svar, " : undefined ENUM in SMALLTALK", COMMENT, NULL));
642  break;
643  }
644  default:
645  comment = strdup(concatenate(COMMENT, svar, " : undefined declaration in SMALLTALK", COMMENT, NULL));
646  }
647 
648  free(svar);
649  return comment;
650 }
651 
652 /**
653  * This function return a bool indicating if related entity e
654  * represents a constant
655  */
656 static bool constant_p(entity e)
657 {
658  /* Constant variables */
659  return storage_rom_p(entity_storage(e)) &&
662 }
663 
664 /**
665  * This function return a bool indicating if related entity e
666  * represents a variable
667  */
668 static bool variable_p(entity e)
669 {
670  storage s = entity_storage(e);
671  return type_variable_p(entity_type(e)) &&
672  (storage_ram_p(s) || storage_return_p(s));
673 }
674 
675 /**
676  * This function return a bool indicating if related entity e
677  * represents an argument
678  */
679 static bool argument_p(entity e)
680 {
681  /* Formal variables */
682  return type_variable_p(entity_type(e)) &&
684 }
685 
686 /**
687  * Return string representing arguments declaration
688  * written in SmallTalk style
689  */
690 static string
692  bool (*consider_this_entity)(entity),
693  string separator,
694  bool lastsep)
695 {
696  string result = strdup("");
697  code c;
698  bool first = true;
699 
700  /* Assert that entity represent a value code */
701  pips_assert("it is a code", value_code_p(entity_initial(module)));
702 
704  MAP(ENTITY, var,
705  {
706  debug(2, "\n Prettyprinter declaration for argument :",st_entity_local_name(var));
707  if (consider_this_entity(var))
708  {
709  string old = result;
710  string svar = strdup(concatenate("with:",st_entity_local_name(var), NULL));
711  result = strdup(concatenate(old, !first && !lastsep? separator: "",
712  svar, lastsep? separator: "", NULL));
713  free(old);
714  free(svar);
715  first = false;
716  }
717  },code_declarations(c));
718  return result;
719 }
720 
721 /**
722  * Return string representing variables or constants declaration
723  * written in SmallTalk style
724  */
725 static string
727  bool (*consider_this_entity)(entity),
728  string separator,
729  bool lastsep)
730 {
731  string result = strdup("");
732  code c;
733  bool first = true;
734 
735  /* Assert that entity represent a value code */
736  pips_assert("it is a code", value_code_p(entity_initial(module)));
737 
739  MAP(ENTITY, var,
740  {
741  debug(2, "\n Prettyprinter declaration for variable :",st_entity_local_name(var));
742  if (consider_this_entity(var))
743  {
744  string old = result;
745  string svar = st_declaration(var);
746  result = strdup(concatenate(old, !first && !lastsep? separator: "",
747  svar, lastsep? separator: "", NULL));
748  free(old);
749  free(svar);
750  first = false;
751  }
752  },code_declarations(c));
753  return result;
754 }
755 
756 /**
757  * Return string representing variables or constants declaration
758  * initialisation written in SmallTalk style
759  */
760 static string
762  bool (*consider_this_entity)(entity),
763  string separator,
764  bool lastsep)
765 {
766  string result = strdup("");
767  code c;
768  bool first = true;
769 
770  /* Assert that entity represent a value code */
771  pips_assert("it is a code", value_code_p(entity_initial(module)));
772 
774  MAP(ENTITY, var,
775  {
776  debug(2, "Prettyprinter declaration initialisation for variable :",st_entity_local_name(var));
777  if (consider_this_entity(var))
778  {
779  string old = result;
780  string svar = st_declaration_init(var);
781  if (svar != NULL) {
782  result = strdup(concatenate(old, !first && !lastsep? separator: "",
783  svar, lastsep? separator: "", NULL));
784  }
785  else {
786  result = strdup(result);
787  }
788  free(old);
789  free(svar);
790  first = false;
791  }
792  },code_declarations(c));
793  return result;
794 }
795 
796 /**
797  * Return string representing variables or constants declaration
798  * initialisation written in SmallTalk style
799  */
800 static string
802  bool (*consider_this_entity)(entity),
803  string separator,
804  bool lastsep)
805 {
806  string result = strdup("");
807  code c;
808  bool first = true;
809 
810  /* Assert that entity represent a value code */
811  pips_assert("it is a code", value_code_p(entity_initial(module)));
812 
814  MAP(ENTITY, var,
815  {
816  debug(2, "Prettyprinter declaration initialisation for variable :",st_entity_local_name(var));
817  if (consider_this_entity(var))
818  {
819  string old = result;
820  string svar = st_declaration_comment(var);
821  result = strdup(concatenate(old, !first && !lastsep? separator: "",
822  svar, lastsep? separator: "", NULL));
823  free(old);
824  free(svar);
825  first = false;
826  }
827  },code_declarations(c));
828  return result;
829 }
830 
831 /**
832  * Generate header for SMALLTALK module
833  */
834 static string st_header(entity module)
835 {
836  string result, svar, args;
837 
838  pips_assert("it is a function", type_functional_p(entity_type(module)));
839 
841 
842  /* Generates the arguments declarations */
843  args = st_arguments(module,
844  argument_p,
845  SPACE,
846  true);
847 
848  result = strdup(concatenate(svar, SPACE, args, NL,
849  COMMENT, "Automatically generated with PIPS", COMMENT,
850  NL, NULL));
851 
852  return result;
853 }
854 
855 /*********************************************************
856  * Generate SMALLTALK code as String from module
857  * root statement
858  *********************************************************/
859 
861 {
862  string st_head, st_variables, st_constants;
863  string st_variables_init, st_constants_init;
864  string st_variables_comment;
865  string st_body, result;
866 
867  ifdebug(2) {
868  printf("Module statement: \n");
869  print_statement(stat);
870  printf("and declarations: \n");
872  }
873 
874  /* HEAD generates the header */
875  st_head = st_header(module);
876  ifdebug(3) {
877  printf("HEAD: \n");
878  printf("%s \n", st_head);
879  }
880 
881  /* Generates the variables declarations */
882  /* What about declarations associated to statements ??? */
883  st_variables = st_declarations(module,
884  variable_p,
885  SPACE,
886  true);
887  ifdebug(3) {
888  printf("VARIABLES: \n");
889  printf("%s \n", st_variables);
890  }
891 
892  /* Generates the constant declarations */
893  st_constants = st_declarations (module,
894  constant_p,
895  SPACE,
896  true);
897  ifdebug(3) {
898  printf("CONSTANTS: \n");
899  printf("%s \n", st_constants);
900  }
901 
902  /* Generates the variables declarations initialisation */
903  st_variables_init = st_declarations_init (module,
904  variable_p,
905  STSEMICOLON,
906  true);
907  ifdebug(3) {
908  printf("VARIABLES INIT: \n");
909  printf("%s \n", st_variables_init);
910  }
911 
912  /* Generates the variables declarations comments */
913  st_variables_comment = st_declarations_comment (module,
914  variable_p,
915  NL,
916  true);
917  ifdebug(3) {
918  printf("VARIABLES COMMENT: \n");
919  printf("%s \n", st_variables_comment);
920  }
921 
922  /* Generates the constant declarations initialisation */
923  st_constants_init = st_declarations_init (module,
924  constant_p,
925  STSEMICOLON,
926  true);
927  ifdebug(3) {
928  printf("CONSTANTS INIT: \n");
929  printf("%s \n", st_constants_init);
930  }
931 
932  /* Generates the body */
933  st_body = st_statement(stat);
934  ifdebug(3) {
935  printf("BODY: \n");
936  printf("%s \n", st_body);
937  }
938 
939  result = strdup(concatenate(st_head, NL,
940  st_variables_comment, NL
941  BEGINTEMPVAR, st_constants,
942  st_variables, ENDTEMPVAR, NL, NL,
943  st_constants_init, NL,
944  st_variables_init, NL,
945  st_body, NL,
946  NULL));
947 
948  free(st_head);
949  free(st_variables);
950  free(st_constants);
951  free(st_body);
952 
953  return result;
954 }
955 
956 /*************************************************************** EXPRESSIONS */
957 
959 
960 struct s_ppt
961 {
962  char * intrinsic;
963  char * c;
965 };
966 
968 
969 /**
970  * Return string representation for a list of expression le
971  * representing an assignement, asserting that le is a list
972  * of expressions containing exactly TWO expressions
973  */
974 static string ppt_assignement (string in_smalltalk, list le)
975 {
976  string result, svar;
977  expression e1, e2;
978  string s1, s2;
979  bool p1, p2, pr1, pr2;
980  syntax s;
981  reference r;
982  entity var;
983  type t;
984  variable v;
985  list ldim;
986 
987  pips_assert("2 arguments to assignment call", gen_length(le)==2);
988 
989  e1 = EXPRESSION(CAR(le));
990  s = expression_syntax(e1);
991  pips_assert("assignment call: first expression is reference",
993 
994 
995  r = syntax_reference(s);
996  var = reference_variable(r);
997  t = entity_type(var);
998  v = type_variable(t);
999  ldim = variable_dimensions(v);
1000 
1001  svar = st_entity_local_name(var);
1002 
1003  e2 = EXPRESSION(CAR(CDR(le)));
1005  s2 = st_expression(e2);
1006 
1007 
1008  if (gen_length(ldim) == 0) {
1009  /* This is a scalar variable */
1010 
1012  s1 = st_reference(r);
1013  result = strdup(concatenate(p1? OPENPAREN: EMPTY, s1, p1? CLOSEPAREN: EMPTY,
1014  SPACE, in_smalltalk, SPACE,
1015  p2? OPENPAREN: EMPTY, s2, p2? CLOSEPAREN: EMPTY,
1016  NULL));
1017  free(s1);
1018  }
1019 
1020  else if (gen_length(ldim) == 1) {
1021 
1022  dimension dim = DIMENSION(gen_nth(0,ldim));
1025 
1026  dim = DIMENSION(gen_nth(0,ldim));
1027 
1028  result = strdup(concatenate(svar, SPACE, ARRAY_AT_PUT_1, SPACE,
1029  pr1? OPENPAREN: EMPTY,
1031  pr1? CLOSEPAREN: EMPTY, SPACE,
1033  p2? OPENPAREN: EMPTY, s2, p2? CLOSEPAREN: EMPTY,
1034  NULL));
1035  }
1036 
1037  else if (gen_length(ldim) == 2) {
1038 
1039  dimension dim1 = DIMENSION(gen_nth(0,ldim));
1041  dimension dim2 = DIMENSION(gen_nth(1,ldim));
1045 
1047  pr1? OPENPAREN: EMPTY,
1049  pr1? CLOSEPAREN: EMPTY, SPACE,
1051  pr2? OPENPAREN: EMPTY,
1053  pr2? CLOSEPAREN: EMPTY, SPACE,
1055  p2? OPENPAREN: EMPTY, s2, p2? CLOSEPAREN: EMPTY,
1056  NULL));
1057  }
1058 
1059  else {
1060  result = strdup("Arrays more than 2D are not handled !");
1061  }
1062 
1063  free(s2);
1064 
1065  return result;
1066 }
1067 
1068 /**
1069  * Return string representation for a list of expression le
1070  * representing a BINARY relation, asserting that le is a list
1071  * of expressions containing exactly TWO expressions
1072  */
1073 static string ppt_binary(string in_smalltalk, list le)
1074 {
1075  string result;
1076  expression e1, e2;
1077  string s1, s2;
1078  bool p1, p2;
1079 
1080  pips_assert("2 arguments to binary call", gen_length(le)==2);
1081 
1082  e1 = EXPRESSION(CAR(le));
1084  s1 = st_expression(e1);
1085 
1086  e2 = EXPRESSION(CAR(CDR(le)));
1088  s2 = st_expression(e2);
1089 
1090  result = strdup(concatenate(p1? OPENPAREN: EMPTY, s1, p1? CLOSEPAREN: EMPTY,
1091  SPACE, in_smalltalk, SPACE,
1092  p2? OPENPAREN: EMPTY, s2, p2? CLOSEPAREN: EMPTY,
1093  NULL));
1094 
1095  free(s1);
1096  free(s2);
1097 
1098  return result;
1099 }
1100 
1101 /**
1102  * Return string representation for a list of expression le
1103  * representing a UNARY relation, asserting that le is a list
1104  * of expressions containing exactly ONE expression
1105  */
1106 static string ppt_unary(string in_smalltalk, list le)
1107 {
1108  string e, result;
1109  pips_assert("one arg to unary call", gen_length(le)==1);
1110  e = st_expression(EXPRESSION(CAR(le)));
1111  result = strdup(concatenate(in_smalltalk, SPACE, e, NULL));
1112  free(e);
1113  return result;
1114 }
1115 
1116 /**
1117  * Return string representation for a list of expression le
1118  * representing a UNARY POST relation, asserting that le is a list
1119  * of expressions containing exactly ONE expression
1120  */
1121 static string ppt_unary_post(string in_smalltalk, list le)
1122 {
1123  string e, result;
1124  pips_assert("one arg to unary post call", gen_length(le)==1);
1125  e = st_expression(EXPRESSION(CAR(le)));
1126  result = strdup(concatenate(e, SPACE, in_smalltalk, NULL));
1127  free(e);
1128  return result;
1129 }
1130 
1131 static string ppt_call(string in_smalltalk, list le)
1132 {
1133  string scall, old;
1134  if (le == NIL)
1135  {
1136  scall = strdup(concatenate(in_smalltalk, NULL));
1137  }
1138  else
1139  {
1140  bool first = true;
1141  scall = strdup(concatenate(in_smalltalk, OPENPAREN, NULL));
1142 
1143  /* Attention: not like this for io statements*/
1144  MAP(EXPRESSION, e,
1145  {
1146  string arg = st_expression(e);
1147  old = scall;
1148  scall = strdup(concatenate(old, first? "": ", ", arg, NULL));
1149  free(arg);
1150  free(old);
1151  first = false;
1152  },le);
1153 
1154  old = scall;
1155  scall = strdup(concatenate(old, CLOSEPAREN, NULL));
1156  free(old);
1157  }
1158  return scall;
1159 }
1160 
1161 /**
1162  * This data structure encodes the differents intrinsic allowing
1163  * to convert fortran code to smalltalk code
1164  */
1165 static struct s_ppt intrinsic_to_smalltalk[] =
1166 {
1167  { "+", "+", ppt_binary },
1168  { "-", "-", ppt_binary },
1169  { "/", "/", ppt_binary },
1170  { "*", "*", ppt_binary },
1171  { "--", "-", ppt_unary },
1172  { "**", "**", ppt_binary },
1173  { "=", SETVALUE, ppt_assignement },
1174  { ".OR.", "||", ppt_binary },
1175  { ".AND.", "&&", ppt_binary },
1176  { ".NOT.", "!", ppt_unary },
1177  { ".LT.", "<", ppt_binary },
1178  { ".GT.", ">", ppt_binary },
1179  { ".LE.", "<=", ppt_binary },
1180  { ".GE.", ">=", ppt_binary },
1181  { ".EQ.", "==", ppt_binary },
1182  { ".NE.", "!=", ppt_binary },
1183  { ".EQV.", "==", ppt_binary },
1184  { ".NEQV.", "!=", ppt_binary },
1185  { ".", ".", ppt_binary },
1186  { "->", "->", ppt_binary},
1187  { "post++", "++", ppt_unary_post },
1188  {"post--", "--" , ppt_unary_post },
1189  {"++pre", "++" , ppt_unary },
1190  {"--pre", "--" , ppt_unary },
1191  {"&", "&" , ppt_unary },
1192  {"*indirection", "*" , ppt_unary },
1193  {"+unary", "+", ppt_unary },
1194  {"-unary", "-", ppt_unary },
1195  {"~", "~", ppt_unary },
1196  {"!", "!", ppt_unary },
1197  {"%", "%" , ppt_binary },
1198  {"+C", "+" , ppt_binary },
1199  {"-C", "-", ppt_binary },
1200  {"<<", "<<", ppt_binary },
1201  {">>", ">>", ppt_binary },
1202  {"<", "<" , ppt_binary },
1203  {">", ">" , ppt_binary },
1204  {"<=", "<=", ppt_binary },
1205  {">=", ">=", ppt_binary },
1206  {"==", "==", ppt_binary },
1207  {"!=", "!=", ppt_binary },
1208  {"&bitand", "&", ppt_binary},
1209  {"^", "^", ppt_binary },
1210  {"|", "|", ppt_binary },
1211  {"&&", "&&", ppt_binary },
1212  {"||", "||", ppt_binary },
1213  {"*=", "*=", ppt_binary },
1214  {"/=", "/=", ppt_binary },
1215  {"%=", "%=", ppt_binary },
1216  {"+=", "+=", ppt_binary },
1217  {"-=", "-=", ppt_binary },
1218  {"<<=", "<<=" , ppt_binary },
1219  {">>=", ">>=", ppt_binary },
1220  {"&=", "&=", ppt_binary },
1221  {"^=", "^=", ppt_binary },
1222  {"|=","|=" , ppt_binary },
1223  { NULL, NULL, ppt_call }
1224 };
1225 
1226 /**
1227  * Return the prettyprinter structure for SmallTalk
1228  */
1229 
1230 static struct s_ppt * get_ppt(entity f)
1231 {
1232  const char* called = entity_local_name(f);
1233  struct s_ppt * table = intrinsic_to_smalltalk;
1234  while (table->intrinsic && !same_string_p(called, table->intrinsic))
1235  table++;
1236  return table;
1237 }
1238 
1239 /**
1240  * Return bool indicating if expression e must be enclosed
1241  * in parenthesis
1242  */
1244 {
1245  syntax s = expression_syntax(e);
1246  switch (syntax_tag(s))
1247  {
1248  case is_syntax_call:
1249  {
1250  struct s_ppt * p = get_ppt(call_function(syntax_call(s)));
1251  return p->ppt==ppt_binary;
1252  }
1253  case is_syntax_reference:
1254  case is_syntax_range:
1255  default:
1256  return false;
1257  }
1258 }
1259 
1260 /**
1261  * This method returns Smalltalk-like string representation (pretty-print)
1262  * for a statement s
1263  */
1264 static string st_statement(statement s)
1265 {
1266  string result;
1269  ifdebug(3) {
1270  printf("\nCurrent statement : \n");
1271  print_statement(s);
1272  }
1273  switch (instruction_tag(i))
1274  {
1275  case is_instruction_test:
1276  {
1277  test t = instruction_test(i);
1278  pips_debug(2, "Instruction TEST\n");
1279  result = st_test(t);
1280  break;
1281  }
1283  {
1284  sequence seq = instruction_sequence(i);
1285  pips_debug(2, "Instruction SEQUENCE\n");
1286  result = st_sequence(seq);
1287  break;
1288  }
1289  case is_instruction_loop:
1290  {
1291  loop l = instruction_loop(i);
1292  pips_debug(2, "Instruction LOOP\n");
1293  result = st_loop(l);
1294  break;
1295  }
1297  {
1299  pips_debug(2, "Instruction WHILELOOP\n");
1300  result = st_whileloop(w);
1301  break;
1302  }
1304  {
1306  pips_debug(2, "Instruction FORLOOP\n");
1307  result = st_forloop(f);
1308  break;
1309  }
1310  case is_instruction_call:
1311  {
1312  string scall = st_call(instruction_call(i));
1313  pips_debug(2, "Instruction CALL\n");
1314  result = strdup(concatenate(scall, STSEMICOLON, NULL));
1315  break;
1316  }
1318  {
1319  /*unstructured u = instruction_unstructured(i);*/
1320  pips_debug(2, "Instruction UNSTRUTURED\n");
1321  result = strdup(concatenate(COMMENT,
1322  "UNSTRUCTURED: Instruction not implementable in SMALLTALK",
1323  COMMENT, NL, NULL));
1324  break;
1325  }
1326  case is_instruction_goto:
1327  {
1328  /*statement g = instruction_goto(i);*/
1329  pips_debug(2, "Instruction GOTO\n");
1330  result = strdup(concatenate(COMMENT,
1331  "GOTO: Instruction not implementable in SMALLTALK",
1332  COMMENT, NL, NULL));
1333  break;
1334  }
1335  /* add switch, forloop break, continue, return instructions here*/
1336  default:
1337  pips_user_warning("Instruction NOT IMPLEMENTED\n");
1338  result = strdup(concatenate(COMMENT, " Instruction not implemented" NL, NULL));
1339  break;
1340  }
1341 
1342  if (!ENDP(l))
1343  {
1344  string decl = "";
1345  MAP(ENTITY, var,
1346  {
1347  string svar;
1348  debug(2, "\n In block declaration for variable :",st_entity_local_name(var));
1349  svar = st_declaration(var);
1350  decl = strdup(concatenate(decl, svar, STSEMICOLON, NULL));
1351  free(svar);
1352  },l);
1353  result = strdup(concatenate(decl,result,NULL));
1354  }
1355 
1356  return result;
1357 }
1358 
1359 /**
1360  * This method returns Smalltalk-like string representation (pretty-print)
1361  * for a statement s which is a Test Statement (IF/THEN/ELSE)
1362  */
1363 static string st_test(test t)
1364 {
1365  string result;
1366  bool no_false;
1367  string cond, strue, sfalse;
1368 
1369  cond = st_expression(test_condition(t));
1370  strue = st_statement(test_true(t));
1371  no_false = empty_statement_p(test_false(t));
1372 
1373  sfalse = no_false? NULL: st_statement(test_false(t));
1374 
1375  if (no_false) {
1376  result = strdup(concatenate(OPENPAREN, cond, CLOSEPAREN, NL,
1377  ST_IFTRUE, SPACE, OPENBRACKET, NL, strue,
1379  NULL));
1380  }
1381  else {
1382  result = strdup(concatenate(OPENPAREN, cond, CLOSEPAREN, NL,
1383  ST_IFTRUE, SPACE, OPENBRACKET, NL, strue,
1384  CLOSEBRACKET, NL,
1385  ST_IFFALSE, SPACE, OPENBRACKET, NL, sfalse,
1387  NULL));
1388  }
1389  free(cond);
1390  free(strue);
1391  if (sfalse) free(sfalse);
1392  return result;
1393 }
1394 
1395 /**
1396  * This method returns Smalltalk-like string representation (pretty-print)
1397  * for a statement s which is a Sequence Statement
1398  * (an ordered set of sequential statements)
1399  */
1400 static string st_sequence(sequence seq)
1401 {
1402  string result = strdup(EMPTY);
1403  MAP(STATEMENT, s,
1404  {
1405  string oldresult = result;
1406  string current = st_statement(s);
1407  if (current != NULL) {
1408  result = strdup(concatenate(oldresult, current, NULL));
1409  }
1410  else {
1411  result = strdup(oldresult);
1412  }
1413  free(current);
1414  free(oldresult);
1415  }, sequence_statements(seq));
1416  return result;
1417 }
1418 
1419 /**
1420  * This method returns Smalltalk-like string representation (pretty-print)
1421  * for a statement s which is a Loop Statement (DO...ENDDO)
1422  */
1423 static string st_loop(loop l)
1424 {
1425  string result, initialisation, loopbody, incrementation;
1426  string body = st_statement(loop_body(l));
1427  string index = st_entity_local_name(loop_index(l));
1428  range r = loop_range(l);
1429  string low = st_expression(range_lower(r));
1430  string up = st_expression(range_upper(r));
1431  string inc = st_expression(range_increment(r));
1432  intptr_t incasint;
1433 
1434  initialisation = strdup(concatenate(index, SPACE, SETVALUE, SPACE, low, STSEMICOLON, NULL));
1435 
1436  if (expression_integer_value(range_increment(r), &incasint)) {
1437  string istr;
1438  if (incasint >= 0) {
1439  istr = int2a(incasint);
1440  inc = strdup(concatenate(ST_PLUS, SPACE,
1441  istr, NULL));
1442  }
1443  else {
1444  istr = int2a(-incasint);
1445  inc = strdup(concatenate(ST_MINUS, SPACE,
1446  istr, NULL));
1447  }
1448  free(istr);
1449  }
1450  else {
1451  inc = strdup(concatenate(ST_PLUS, SPACE,
1452  st_expression(range_increment(r)), NULL));
1453  }
1454 
1455  incrementation = strdup(concatenate(index, SPACE, SETVALUE, SPACE, index, SPACE, inc, NULL));
1456 
1457  loopbody = strdup(concatenate(OPENBRACKET, index, SPACE, ST_LE,
1458  SPACE, up, CLOSEBRACKET, SPACE,
1460  body, incrementation,
1461  STSEMICOLON, CLOSEBRACKET, NULL));
1462 
1463 
1464  result = strdup(concatenate(initialisation, loopbody, STSEMICOLON, NULL));
1465 
1466  free(initialisation);
1467  free(incrementation);
1468  free(loopbody);
1469  free(body);
1470  free(low);
1471  free(up);
1472  free(index);
1473  return result;
1474 }
1475 
1476 /**
1477  * This method returns Smalltalk-like string representation (pretty-print)
1478  * for a statement s which is a While-Loop Statement (DO WHILE...ENDDO)
1479  */
1480 static string st_whileloop(whileloop w)
1481 {
1482  string result;
1483  string body = st_statement(whileloop_body(w));
1484  string cond = st_expression(whileloop_condition(w));
1485  /*evaluation eval = whileloop_evaluation(w);*/
1486 
1487  result = strdup(concatenate(OPENBRACKET, cond, CLOSEBRACKET, SPACE,
1489  body,
1490  CLOSEBRACKET, STSEMICOLON, NULL));
1491 
1492  free(cond);
1493  free(body);
1494  return result;
1495 }
1496 
1497 /**
1498  * This method returns Smalltalk-like string representation (pretty-print)
1499  * for a statement s which is a For-Loop Statement (I don't know how to
1500  * specify in fortran !!!)
1501  */
1502 static string st_forloop(forloop f)
1503 {
1504  string result, loopbody;
1505  string body = st_statement(forloop_body(f));
1507  string cond = st_expression(forloop_condition(f));
1508  string inc = st_expression(forloop_increment(f));
1509  result = strdup(concatenate("for (", init, ";",cond,";",inc,") {" NL,
1510  body, "}" NL, NULL));
1511 
1512  loopbody = strdup(concatenate(OPENBRACKET, cond, CLOSEBRACKET, SPACE,
1514  body, inc,
1515  STSEMICOLON, CLOSEBRACKET, NULL));
1516 
1517 
1518  result = strdup(concatenate(init, loopbody, STSEMICOLON, NULL));
1519 
1520  free(loopbody);
1521  free(inc);
1522  free(cond);
1523  free(init);
1524  free(body);
1525  return result;
1526 }
1527 
1528 /**
1529  * This method returns Smalltalk-like string representation (pretty-print)
1530  * for a statement s which is a Call Statement (a code line)
1531  */
1532 static string st_call(call c)
1533 {
1534  entity called = call_function(c);
1535  struct s_ppt * ppt = get_ppt(called);
1536  string result;
1537 
1538  /* special case... */
1539  if (same_string_p(entity_local_name(called), "STOP")) {
1540  result = NULL;
1541  }
1542  else if (same_string_p(entity_local_name(called), "CONTINUE")) {
1543  result = NULL;
1544  }
1545  else if (same_string_p(entity_local_name(called), "RETURN"))
1546  {
1548  result = strdup(RETURNVALUE " 0");
1549  else if (current_module_is_a_function())
1550  result = strdup(RETURNVALUE SPACE RESULT_NAME);
1551  else
1552  result = strdup(RETURNVALUE);
1553  }
1554  else if (call_constant_p(c))
1555  {
1556  result = st_entity_local_name(called);
1557  }
1558  else
1559  {
1560  string s = st_entity_local_name(called);
1561  result = ppt->ppt(ppt->c? ppt->c: s, call_arguments(c));
1562  free(s);
1563  }
1564 
1565  return result;
1566 }
1567 
1568 /**
1569  * This function return a string representation of a reference r.
1570  * A reference is an array element, considering non-array variables
1571  * (scalar variables) are 0-dimension arrays elements. We must here
1572  * differently manage scalar, 1-D arrays (using SmallTalk Array
1573  * class) and 2-D arrays (using SmallTalk Array2D).
1574  *
1575  * NB: in Fortran, the indexes are reversed
1576  */
1577 static string st_reference(reference r)
1578 {
1579  string result = strdup(EMPTY), svar;
1580 
1581  entity var = reference_variable(r);
1582  type t = entity_type(var);
1583  variable v = type_variable(t);
1584  list ldim = variable_dimensions(v);
1585  bool pr, pr1, pr2;
1586 
1588 
1589  if (gen_length(ldim) == 0) {
1590 
1591  /* This is a scalar variable, no need to manage array indices */
1592  result = strdup(st_entity_local_name(var));
1593  free(svar);
1594  return result;
1595  }
1596 
1597  else if (gen_length(ldim) == 1) {
1598 
1599  dimension dim = DIMENSION(gen_nth(0,ldim));
1602 
1603  dim = DIMENSION(gen_nth(0,ldim));
1604 
1605  result = strdup(concatenate(OPENPAREN, svar, SPACE, ARRAY_AT, SPACE,
1606  pr? OPENPAREN: EMPTY,
1608  pr? CLOSEPAREN: EMPTY,
1609  CLOSEPAREN, NULL));
1610  }
1611 
1612  else if (gen_length(ldim) == 2) {
1613 
1614  dimension dim1 = DIMENSION(gen_nth(0,ldim));
1616  dimension dim2 = DIMENSION(gen_nth(1,ldim));
1620 
1621  result = strdup(concatenate(OPENPAREN, svar,
1623  pr1? OPENPAREN: EMPTY,
1625  pr1? CLOSEPAREN: EMPTY,
1627  pr2? OPENPAREN: EMPTY,
1629  pr2? CLOSEPAREN: EMPTY,
1630  CLOSEPAREN, NULL));
1631  }
1632 
1633  else {
1634 
1635  result = strdup(concatenate(COMMENT, "Arrays more than 2D are not handled !",
1636  COMMENT, NULL));
1637  }
1638 
1639  free(svar);
1640  return result;
1641 
1642 }
1643 
1644 static string st_expression(expression e)
1645 {
1646  string result = NULL;
1647  syntax s = expression_syntax(e);
1648  switch (syntax_tag(s))
1649  {
1650  case is_syntax_call:
1651  result = st_call(syntax_call(s));
1652  break;
1653  case is_syntax_range:
1654  result = strdup("range not implemented");
1655  break;
1656  case is_syntax_reference:
1657  result = st_reference(syntax_reference(s));
1658  break;
1659  /* add cast, sizeof here */
1660  default:
1661  pips_internal_error("unexpected syntax tag");
1662  }
1663  return result;
1664 }
1665 
1666 /*********************************************************
1667  * Phase main
1668  *********************************************************/
1669 
1671 {
1672  FILE * out;
1673  string ppt, smalltalkcode, dir, filename;
1674  entity module;
1675  statement stat;
1676 
1677  /* We first build the future resource file, with a .st */
1678  smalltalkcode = db_build_file_resource_name(DBR_SMALLTALK_CODE_FILE, module_name, STPRETTY);
1681  filename = strdup(concatenate(dir, "/", smalltalkcode, NULL));
1682  stat = (statement) db_get_memory_resource(DBR_CODE, module_name, true);
1683 
1686 
1687  debug_on("SMALLTALK_PRETTYPRINTER_DEBUG_LEVEL");
1688  pips_debug(1, "Begin SMALLTALK prettyprinter for %s\n", entity_name(module));
1689  ppt = smalltalk_code_string(module, stat);
1690  pips_debug(1, "End SMALLTALK prettyprinter for %s\n", entity_name(module));
1691 
1692  pips_debug(3, "What i got is \n%s\n", ppt);
1693 
1694  /* save to file */
1695  out = safe_fopen(filename, "w");
1696  fprintf(out, "/* SMALLTALK pretty print for module %s. */\n%s", module_name, ppt);
1697  safe_fclose(out, filename);
1698 
1699  free(ppt);
1700  free(dir);
1701  free(filename);
1702 
1703  DB_PUT_FILE_RESOURCE(DBR_SMALLTALK_CODE_FILE, module_name, smalltalkcode);
1704 
1707 
1708  return true;
1709 }
static FILE * out
Definition: alias_check.c:128
#define SPACE
Definition: codegen.c:216
struct _newgen_struct_statement_ * statement
Definition: cloning.h:21
const char * module_name(const char *s)
Return the module part of an entity name.
Definition: entity_names.c:296
FILE * safe_fopen(const char *filename, const char *what)
Definition: file.c:67
int safe_fclose(FILE *stream, const char *filename)
Definition: file.c:77
#define call_constant_p(C)
Definition: flint_check.c:51
static void comment(string_buffer code, spoc_hardware_type hw, dagvtx v, int stage, int side, bool flip)
Definition: freia_spoc.c:52
void free(void *)
void reset_current_module_entity(void)
Reset the current module entity.
Definition: static.c:97
void reset_current_module_statement(void)
Reset the current module statement.
Definition: static.c:221
statement set_current_module_statement(statement)
Set the current module statement.
Definition: static.c:165
entity set_current_module_entity(entity)
static.c
Definition: static.c:66
entity get_current_module_entity(void)
Get the entity of the current module.
Definition: static.c:85
#define ENDP(l)
Test if a list is empty.
Definition: newgen_list.h:66
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
size_t gen_length(const list l)
Definition: list.c:150
#define CAR(pcons)
Get the value of the first element of a list.
Definition: newgen_list.h:92
#define CDR(pcons)
Get the list less its first element.
Definition: newgen_list.h:111
gen_chunk gen_nth(int n, const list l)
to be used as ENTITY(gen_nth(3, l))...
Definition: list.c:710
#define MAP(_map_CASTER, _map_item, _map_code, _map_list)
Apply/map an instruction block on all the elements of a list (old fashioned)
Definition: newgen_list.h:226
string db_get_memory_resource(const char *rname, const char *oname, bool pure)
Return the pointer to the resource, whatever it is.
Definition: database.c:755
#define DB_PUT_FILE_RESOURCE
Put a file resource into the current workspace database.
Definition: pipsdbm-local.h:85
bool empty_statement_p(statement)
Test if a statement is empty.
Definition: statement.c:391
#define CLOSEPAREN
#define OPENPAREN
string db_build_file_resource_name(const char *rname, const char *oname, const char *suffix)
returns an allocated file name for a file resource.
Definition: lowlevel.c:169
#define debug_on(env)
Definition: misc-local.h:157
#define pips_debug
these macros use the GNU extensions that allow variadic macros, including with an empty list.
Definition: misc-local.h:145
#define pips_user_warning
Definition: misc-local.h:146
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define pips_internal_error
Definition: misc-local.h:149
void debug(const int the_expected_debug_level, const char *calling_function_name, const char *a_message_format,...)
ARARGS0.
Definition: debug.c:189
#define TYPEDEF_PREFIX
Definition: naming-local.h:62
#define MAIN_PREFIX
Definition: naming-local.h:32
#define UNION_PREFIX
Definition: naming-local.h:58
#define ENUM_PREFIX
Definition: naming-local.h:60
#define MEMBER_SEP_STRING
Definition: naming-local.h:53
#define STRUCT_PREFIX
Definition: naming-local.h:56
string concatenate(const char *,...)
Return the concatenation of the given strings.
Definition: string.c:183
#define same_string_p(s1, s2)
char * string
STRING.
Definition: newgen_types.h:39
struct cons * list
Definition: newgen_types.h:106
int f(int off1, int off2, int n, float r[n], float a[n], float b[n])
Definition: offsets.c:15
static char * module
Definition: pips.c:74
string db_get_current_workspace_directory(void)
Definition: workspace.c:96
string expression_to_string(expression e)
Definition: expression.c:77
void print_statement(statement)
Print a statement on stderr.
Definition: statement.c:98
static bool variable_p(entity e)
This function return a bool indicating if related entity e represents a variable.
static string st_entity_local_name(entity var)
Return beautified string representing name for entity var.
static string st_reference(reference r)
This function return a string representation of a reference r.
static struct s_ppt * get_ppt(entity f)
Return the prettyprinter structure for SmallTalk.
static bool argument_p(entity e)
This function return a bool indicating if related entity e represents an argument.
static string ppt_unary(string in_smalltalk, list le)
Return string representation for a list of expression le representing a UNARY relation,...
static string st_dimension_reference_as_string(dimension dim, expression old_expression)
Return a string representing dimension reference for a dimension dim and an expression e This functio...
static string st_expression(expression)
static string st_declaration_comment(entity var)
Return a string representing Smalltalk declaration initialisation for entity (constant or variable) v...
static bool expression_needs_parenthesis_p(expression)
Return bool indicating if expression e must be enclosed in parenthesis.
static string st_header(entity module)
Generate header for SMALLTALK module.
static string st_whileloop(whileloop w)
This method returns Smalltalk-like string representation (pretty-print) for a statement s which is a ...
bool print_code_smalltalk(const char *module_name)
print_code_smalltalk.c
static string ppt_call(string in_smalltalk, list le)
static string st_sequence(sequence seq)
This method returns Smalltalk-like string representation (pretty-print) for a statement s which is a ...
static string ppt_unary_post(string in_smalltalk, list le)
Return string representation for a list of expression le representing a UNARY POST relation,...
static string st_loop(loop l)
This method returns Smalltalk-like string representation (pretty-print) for a statement s which is a ...
static string st_test(test t)
This method returns Smalltalk-like string representation (pretty-print) for a statement s which is a ...
static string st_declarations(entity module, bool(*consider_this_entity)(entity), string separator, bool lastsep)
Return string representing variables or constants declaration written in SmallTalk style.
string(* prettyprinter)(string, list)
static string st_statement(statement s)
This method returns Smalltalk-like string representation (pretty-print) for a statement s.
static string st_dimension_bound_as_string(dimension dim)
Return a string representing dimension bounds of a dimension dim This function automatically convert ...
static string ppt_assignement(string in_smalltalk, list le)
Return string representation for a list of expression le representing an assignement,...
static string st_declarations_comment(entity module, bool(*consider_this_entity)(entity), string separator, bool lastsep)
Return string representing variables or constants declaration initialisation written in SmallTalk sty...
static struct s_ppt intrinsic_to_smalltalk[]
This data structure encodes the differents intrinsic allowing to convert fortran code to smalltalk co...
static string st_call(call c)
This method returns Smalltalk-like string representation (pretty-print) for a statement s which is a ...
static bool constant_p(entity e)
This function return a bool indicating if related entity e represents a constant.
#define current_module_is_a_function()
#define RESULT_NAME
static string st_declarations_init(entity module, bool(*consider_this_entity)(entity), string separator, bool lastsep)
Return string representing variables or constants declaration initialisation written in SmallTalk sty...
static string ppt_binary(string in_smalltalk, list le)
Return string representation for a list of expression le representing a BINARY relation,...
static string c_basic_string(basic b)
Return a string C-like representation of basic b.
static string st_dim_string(string svar, list ldim)
Return string representing array initialization for variable svar in SMALLTALK.
static string smalltalk_code_string(entity module, statement stat)
static string st_brace_expression_as_string(expression exp)
Return string representing expression enclosed by parenthesis.
#define STPRETTY
This phase is used for PHRASE project.
static string st_forloop(forloop f)
This method returns Smalltalk-like string representation (pretty-print) for a statement s which is a ...
static string st_arguments(entity module, bool(*consider_this_entity)(entity), string separator, bool lastsep)
Return string representing arguments declaration written in SmallTalk style.
static string st_declaration(entity var)
Return a string representing Smalltalk declaration for entity (constant or variable) var NB: old func...
static string st_declaration_init(entity var)
Return a string representing Smalltalk declaration initialisation for entity (constant or variable) v...
const char * entity_local_name(entity e)
entity_local_name modified so that it does not core when used in vect_fprint, since someone thought t...
Definition: entity.c:453
entity module_name_to_entity(const char *mn)
This is an alias for local_name_to_top_level_entity.
Definition: entity.c:1479
bool entity_main_module_p(entity e)
Definition: entity.c:700
static int init
Maximal value set for Fortran 77.
Definition: entity.c:320
void print_entities(list l)
Definition: entity.c:167
bool expression_integer_value(expression e, intptr_t *pval)
Definition: eval.c:792
bool brace_expression_p(expression e)
Return bool indicating if expression e is a brace expression.
Definition: expression.c:3384
#define type_functional_p(x)
Definition: ri.h:2950
#define value_undefined_p(x)
Definition: ri.h:3017
#define loop_body(x)
Definition: ri.h:1644
@ is_basic_derived
Definition: ri.h:579
@ is_basic_string
Definition: ri.h:576
@ is_basic_float
Definition: ri.h:572
@ is_basic_bit
Definition: ri.h:577
@ is_basic_pointer
Definition: ri.h:578
@ is_basic_int
Definition: ri.h:571
@ is_basic_logical
Definition: ri.h:573
@ is_basic_typedef
Definition: ri.h:580
#define value_code_p(x)
Definition: ri.h:3065
#define storage_formal_p(x)
Definition: ri.h:2522
#define value_constant(x)
Definition: ri.h:3073
#define syntax_reference(x)
Definition: ri.h:2730
#define syntax_tag(x)
Definition: ri.h:2727
#define forloop_initialization(x)
Definition: ri.h:1366
#define call_function(x)
Definition: ri.h:709
#define reference_variable(x)
Definition: ri.h:2326
#define basic_int(x)
Definition: ri.h:616
#define range_upper(x)
Definition: ri.h:2290
#define storage_tag(x)
Definition: ri.h:2515
#define type_tag(x)
Definition: ri.h:2940
#define forloop_increment(x)
Definition: ri.h:1370
#define ENTITY(x)
ENTITY.
Definition: ri.h:2755
#define symbolic_constant(x)
Definition: ri.h:2599
#define constant_int(x)
Definition: ri.h:850
#define instruction_loop(x)
Definition: ri.h:1520
#define test_false(x)
Definition: ri.h:2837
#define dimension_lower(x)
Definition: ri.h:980
#define basic_tag(x)
Definition: ri.h:613
#define type_variable(x)
Definition: ri.h:2949
#define entity_storage(x)
Definition: ri.h:2794
#define code_declarations(x)
Definition: ri.h:784
@ is_syntax_range
Definition: ri.h:2692
@ is_syntax_call
Definition: ri.h:2693
@ is_syntax_reference
Definition: ri.h:2691
#define range_increment(x)
Definition: ri.h:2292
#define storage_ram_p(x)
Definition: ri.h:2519
#define value_constant_p(x)
Definition: ri.h:3071
#define value_symbolic(x)
Definition: ri.h:3070
#define EXPRESSION(x)
EXPRESSION.
Definition: ri.h:1217
@ is_storage_rom
Definition: ri.h:2494
#define constant_int_p(x)
Definition: ri.h:848
@ is_instruction_goto
Definition: ri.h:1473
@ is_instruction_unstructured
Definition: ri.h:1475
@ is_instruction_whileloop
Definition: ri.h:1472
@ is_instruction_test
Definition: ri.h:1470
@ is_instruction_call
Definition: ri.h:1474
@ is_instruction_sequence
Definition: ri.h:1469
@ is_instruction_forloop
Definition: ri.h:1477
@ is_instruction_loop
Definition: ri.h:1471
#define instruction_tag(x)
Definition: ri.h:1511
#define value_symbolic_p(x)
Definition: ri.h:3068
#define entity_name(x)
Definition: ri.h:2790
#define test_true(x)
Definition: ri.h:2835
#define sequence_statements(x)
Definition: ri.h:2360
#define dimension_upper(x)
Definition: ri.h:982
#define reference_indices(x)
Definition: ri.h:2328
#define value_code(x)
Definition: ri.h:3067
#define instruction_sequence(x)
Definition: ri.h:1514
#define instruction_forloop(x)
Definition: ri.h:1538
#define syntax_call(x)
Definition: ri.h:2736
#define basic_float(x)
Definition: ri.h:619
#define test_condition(x)
Definition: ri.h:2833
#define instruction_whileloop(x)
Definition: ri.h:1523
#define range_lower(x)
Definition: ri.h:2288
#define variable_dimensions(x)
Definition: ri.h:3122
#define whileloop_body(x)
Definition: ri.h:3162
#define statement_declarations(x)
Definition: ri.h:2460
#define statement_instruction(x)
Definition: ri.h:2458
#define instruction_call(x)
Definition: ri.h:1529
#define loop_range(x)
Definition: ri.h:1642
#define storage_rom_p(x)
Definition: ri.h:2525
#define forloop_condition(x)
Definition: ri.h:1368
#define call_arguments(x)
Definition: ri.h:711
#define instruction_test(x)
Definition: ri.h:1517
@ is_type_enum
Definition: ri.h:2907
@ is_type_variable
Definition: ri.h:2900
@ is_type_union
Definition: ri.h:2906
@ is_type_struct
Definition: ri.h:2905
#define whileloop_condition(x)
Definition: ri.h:3160
#define entity_type(x)
Definition: ri.h:2792
#define value_expression_p(x)
Definition: ri.h:3080
#define expression_syntax(x)
Definition: ri.h:1247
#define storage_return_p(x)
Definition: ri.h:2516
#define type_variable_p(x)
Definition: ri.h:2947
#define forloop_body(x)
Definition: ri.h:1372
#define value_expression(x)
Definition: ri.h:3082
#define loop_index(x)
Definition: ri.h:1640
#define variable_basic(x)
Definition: ri.h:3120
#define STATEMENT(x)
STATEMENT.
Definition: ri.h:2413
#define entity_initial(x)
Definition: ri.h:2796
#define COMMENT
Definition: sc_lex.c:786
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
char * strdup()
int printf()
s1
Definition: set.c:247
#define ifdebug(n)
Definition: sg.c:47
#define ARRAY_AT_PUT_1
#define ST_PLUS
#define ARRAY2D_AT_AT_PUT_2
#define ARRAY2D_AT_AT_1
#define RETURNVALUE
#define BEGINTEMPVAR
#define ARRAY2D_NEW2
#define ENDTEMPVAR
#define ST_LE
#define SETVALUE
#define ARRAY2D_AT_AT_PUT_3
#define ST_WHILETRUE
#define ARRAY2D_AT_AT_2
#define ARRAY2D
#define STSEMICOLON
#define ST_IFFALSE
#define ST_MINUS
#define ARRAY_AT
#define ARRAY2D_NEW1
#define ARRAY2D_AT_AT_PUT_1
#define ARRAY_NEW
#define ARRAY
#define ARRAY_AT_PUT_2
#define ST_IFTRUE
#define intptr_t
Definition: stdint.in.h:294
static size_t current
Definition: string.c:115
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
char * intrinsic
prettyprinter ppt
char * int2a(int)
util.c
Definition: util.c:42
#define exp
Avoid some warnings from "gcc -Wshadow".
Definition: vasnprintf.c:207
#define NL
Definition: xml_output.c:48
#define CLOSEBRACKET
#define OPENBRACE
#define EMPTY
#define OPENBRACKET
#define CLOSEBRACE