axe_errors.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Andrea Di Biagio
  3. * Politecnico di Milano, 2007
  4. *
  5. * axe_errors.c
  6. * Formal Languages & Compilers Machine, 2007/2008
  7. *
  8. */
  9. #include "axe_errors.h"
  10. #include "axe_utils.h"
  11. #include "cflow_constants.h"
  12. /* @see Acse.y for these declarations */
  13. extern int num_warning;
  14. extern int num_error;
  15. extern int line_num;
  16. extern int errorcode;
  17. extern int cflow_errorcode;
  18. static void abortProgram();
  19. static void printErrorMessage(int errorcode);
  20. void abortProgram()
  21. {
  22. if (num_error > 0)
  23. {
  24. fprintf(stderr, "Input file contains some errors. "
  25. "No assembly file written\n");
  26. fprintf(stderr, "**%d error(s) found \n\n", num_error);
  27. }
  28. /* finalize all the data structures */
  29. shutdownCompiler(-1);
  30. }
  31. void printWarningMessage(int warningcode)
  32. {
  33. char *msg;
  34. switch (warningcode)
  35. {
  36. case WARN_DIVISION_BY_ZERO : msg = "warning: division by zero"; break;
  37. default : msg = "<invalid warning>"; break;
  38. }
  39. /* print out to the standard error the warning message */
  40. printMessage(msg);
  41. /* update the value of num_warning */
  42. num_warning++;
  43. }
  44. static void printErrorMessage(int errorcode)
  45. {
  46. char *msg;
  47. switch(errorcode)
  48. {
  49. case AXE_OUT_OF_MEMORY : msg = "error: Out of memory"; break;
  50. case AXE_PROGRAM_NOT_INITIALIZED :
  51. msg = "error: Program not initialized"; break;
  52. case AXE_INVALID_INSTRUCTION :
  53. msg = "error: Invalid instruction"; break;
  54. case AXE_VARIABLE_ID_UNSPECIFIED :
  55. msg = "error: Variable ID unspecified"; break;
  56. case AXE_VARIABLE_ALREADY_DECLARED :
  57. msg = "error: Variable already declared"; break;
  58. case AXE_INVALID_TYPE : msg = "error: Invalid type"; break;
  59. case AXE_FOPEN_ERROR : msg = "error: fopen failed"; break;
  60. case AXE_FCLOSE_ERROR : msg = "error: fclose failed"; break;
  61. case AXE_INVALID_INPUT_FILE : msg = "error: Wrong file pointer"; break;
  62. case AXE_FWRITE_ERROR : msg = "error: Error while writing on file"; break;
  63. case AXE_INVALID_DATA_FORMAT : msg = "error: Invalid data format"; break;
  64. case AXE_INVALID_OPCODE : msg = "error: Invalid opcode found"; break;
  65. case AXE_INVALID_REGISTER_INFO :
  66. msg = "error: Invalid register infos"; break;
  67. case AXE_INVALID_LABEL : msg = "error: Invalid label found"; break;
  68. case AXE_INVALID_LABEL_MANAGER :
  69. msg = "error: Invalid label manager"; break;
  70. case AXE_INVALID_ARRAY_SIZE : msg = "error: Invalid array size"; break;
  71. case AXE_INVALID_VARIABLE : msg = "error: Invalid variable found"; break;
  72. case AXE_INVALID_ADDRESS : msg = "error: Invalid address"; break;
  73. case AXE_INVALID_EXPRESSION :
  74. msg = "error: Invalid expression found"; break;
  75. case AXE_UNKNOWN_VARIABLE : msg = "error: Unknown variable found"; break;
  76. case AXE_SY_TABLE_ERROR :
  77. msg = "error: Symbol table returned an errorcode"; break;
  78. case AXE_NULL_DECLARATION : msg = "error: NULL declaration found"; break;
  79. case AXE_LABEL_ALREADY_ASSIGNED :
  80. msg = "error: label already assigned"; break;
  81. case AXE_INVALID_CFLOW_GRAPH : msg = "error: Invalid "
  82. "control-dataflow graph informations"; break;
  83. case AXE_INVALID_REG_ALLOC : msg = "error: Invalid "
  84. "register allocator instance found"; break;
  85. case AXE_REG_ALLOC_ERROR : msg = "error: "
  86. "register allocation failed"; break;
  87. case AXE_TRANSFORM_ERROR : msg = "error: "
  88. "Invalid operation while modifying the instructions"; break;
  89. case AXE_SYNTAX_ERROR : msg = "error: "
  90. "Syntax error found"; break;
  91. case AXE_UNKNOWN_ERROR : msg = "error: Unknown error"; break;
  92. default : msg = "<invalid errorcode>"; break;
  93. }
  94. /* print out to the standard error the error message */
  95. printMessage(msg);
  96. /* update the value of num_error */
  97. num_error++;
  98. }
  99. void printMessage(const char *msg)
  100. {
  101. if (line_num != -1)
  102. fprintf(stderr, "\nAt line %d , %s.\n", line_num, msg);
  103. else
  104. fprintf(stderr, "%s.\n", msg);
  105. }
  106. void notifyError(int axe_errorcode)
  107. {
  108. errorcode = axe_errorcode;
  109. checkConsistency();
  110. }
  111. void checkConsistency()
  112. {
  113. /* test if an error occurred */
  114. if (errorcode != AXE_OK) {
  115. printErrorMessage(errorcode);
  116. abortProgram();
  117. }
  118. if (cflow_errorcode != CFLOW_OK)
  119. {
  120. fprintf(stderr, "An error occurred while working with the "
  121. "cflow graph. cflow_errorcode = %d"
  122. "\n(see axe_cflow_graph.[ch])\n", cflow_errorcode);
  123. printErrorMessage(AXE_UNKNOWN_ERROR);
  124. abortProgram();
  125. }
  126. }