axe_io_manager.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Andrea Di Biagio
  3. * Politecnico di Milano, 2007
  4. *
  5. * axe_io_manager.c
  6. * Formal Languages & Compilers Machine, 2007/2008
  7. *
  8. */
  9. #include <stdlib.h>
  10. #include "axe_io_manager.h"
  11. static t_io_infos * allocOutputInfos();
  12. t_io_infos * initializeOutputInfos(int argc, char **argv)
  13. {
  14. t_io_infos *result;
  15. /* create a new instance of `t_io_infos' */
  16. result = allocOutputInfos();
  17. if (result == NULL)
  18. return NULL;
  19. argc--;
  20. argv++;
  21. if (argc > 0)
  22. {
  23. result->input_file = fopen(argv[0], "r");
  24. if (result->input_file == NULL)
  25. {
  26. fprintf( stderr, "File not found : %s.\n", argv[0]);
  27. exit(-1);
  28. }
  29. #ifndef NDEBUG
  30. fprintf(stdout, "Input file: %s. \n", argv[0]);
  31. #endif
  32. }
  33. #ifndef NDEBUG
  34. else
  35. fprintf(stdout, "Input file: %s. \n", "standard input");
  36. #endif
  37. /* update the value of yyin */
  38. yyin = result->input_file;
  39. if (argc == 2)
  40. result->output_file_name = argv[1];
  41. else
  42. result->output_file_name = "output.asm";
  43. #ifndef NDEBUG
  44. fprintf(stdout, "Output will be written on file : "
  45. "\"%s\". \n", result->output_file_name);
  46. fprintf(stdout, "The Symbol Table will be written on file : "
  47. "\"%s\". \n", "sy_table.out");
  48. fprintf(stdout, "Intermediate code will be written on file : "
  49. "\"%s\". \n", "output.cfg");
  50. fprintf(stdout, "control/dataflow informations will "
  51. "be written on file : \"%s\". \n", "dataflow.cfg");
  52. fprintf(stdout, "Output of the register allocator "
  53. "will be written on file : \"%s\". \n\n", "regalloc.out");
  54. result->reg_alloc_output = fopen("regalloc.out", "w");
  55. if (result->reg_alloc_output == NULL)
  56. fprintf( stderr, "WARNING : Unable to create file: %s.\n", "regalloc.out");
  57. result->cfg_1 = fopen("output.cfg", "w");
  58. result->cfg_2 = fopen("dataflow.cfg", "w");
  59. result->syTable_output = fopen("sy_table.out", "w");
  60. if (result->cfg_1 == NULL)
  61. fprintf( stderr, "WARNING : Unable to create file: %s.\n", "output.cfg");
  62. if (result->cfg_2 == NULL)
  63. fprintf( stderr, "WARNING : Unable to create file: %s.\n", "dataflow.cfg");
  64. if (result->syTable_output == NULL)
  65. fprintf( stderr, "WARNING : Unable to create file: %s.\n", "sy_table.out");
  66. #endif
  67. return result;
  68. }
  69. t_io_infos * allocOutputInfos()
  70. {
  71. t_io_infos *result;
  72. /* Allocate memory for an instance of `t_output_infos' */
  73. result = (t_io_infos *)
  74. _AXE_ALLOC_FUNCTION(sizeof(t_io_infos));
  75. /* test if _AXE_ALLOC_FUNCTION returned a null pointer */
  76. if (result == NULL)
  77. return NULL;
  78. /* initialize the instance internal data */
  79. result->output_file_name = NULL;
  80. result->input_file = stdin;
  81. #ifndef NDEBUG
  82. result->cfg_1 = stdout;
  83. result->cfg_2 = stdout;
  84. result->reg_alloc_output = stdout;
  85. result->syTable_output = stdout;
  86. #endif
  87. /* return the result */
  88. return result;
  89. }
  90. void finalizeOutputInfos(t_io_infos *infos)
  91. {
  92. if (infos == NULL)
  93. return;
  94. if (infos->input_file != NULL)
  95. fclose(infos->input_file);
  96. #ifndef NDEBUG
  97. if (infos->cfg_1 != NULL)
  98. fclose(infos->cfg_1);
  99. if (infos->cfg_2 != NULL)
  100. fclose(infos->cfg_2);
  101. if (infos->reg_alloc_output != NULL)
  102. fclose(infos->reg_alloc_output);
  103. if (infos->syTable_output != NULL)
  104. fclose(infos->syTable_output);
  105. #endif
  106. _AXE_FREE_FUNCTION(infos);
  107. }