No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

as.y 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. %union {
  2. int nombre;
  3. }
  4. %{
  5. #include "tables.h"
  6. #include <stdio.h>
  7. FILE * file;
  8. %}
  9. %token tMUL tDIV tADD tSUB tINF tSUP tEQU
  10. %token tAFC tCPY tAFCA tCPYA
  11. %token tREAD tWR
  12. %token tJMP tJMF
  13. %token tGET tPRI
  14. %token tCALL tRET
  15. %token tSTOP
  16. %token<nombre> tNB
  17. %%
  18. Programme : Instruction Programme;
  19. Programme : Instruction;
  20. Instruction : tMUL tNB tNB tNB {increment_time();
  21. int reg_dest = get_reg_write($2, file);
  22. int reg_src1 = get_reg_read($3, file);
  23. int reg_src2 = get_reg_read($4, file);
  24. fprintf(file, "MUL %d %d %d\n", reg_dest, reg_src1, reg_src2);};
  25. Instruction : tADD tNB tNB tNB {increment_time();
  26. int reg_dest = get_reg_write($2, file);
  27. int reg_src1 = get_reg_read($3, file);
  28. int reg_src2 = get_reg_read($4, file);
  29. fprintf(file, "ADD %d %d %d\n", reg_dest, reg_src1, reg_src2);};
  30. Instruction : tDIV tNB tNB tNB {increment_time();
  31. int reg_dest = get_reg_write($2, file);
  32. int reg_src1 = get_reg_read($3, file);
  33. int reg_src2 = get_reg_read($4, file);
  34. fprintf(file, "DIV %d %d %d\n", reg_dest, reg_src1, reg_src2);};
  35. Instruction : tSUB tNB tNB tNB {increment_time();
  36. int reg_dest = get_reg_write($2, file);
  37. int reg_src1 = get_reg_read($3, file);
  38. int reg_src2 = get_reg_read($4, file);
  39. fprintf(file, "SUB %d %d %d\n", reg_dest, reg_src1, reg_src2);};
  40. Instruction : tINF tNB tNB tNB {increment_time();
  41. int reg_dest = get_reg_write($2, file);
  42. int reg_src1 = get_reg_read($3, file);
  43. int reg_src2 = get_reg_read($4, file);
  44. fprintf(file, "INF %d %d %d\n", reg_dest, reg_src1, reg_src2);};
  45. Instruction : tSUP tNB tNB tNB {increment_time();
  46. int reg_dest = get_reg_write($2, file);
  47. int reg_src1 = get_reg_read($3, file);
  48. int reg_src2 = get_reg_read($4, file);
  49. fprintf(file, "SUP %d %d %d\n", reg_dest, reg_src1, reg_src2);};
  50. Instruction : tEQU tNB tNB tNB {increment_time();
  51. int reg_dest = get_reg_write($2, file);
  52. int reg_src1 = get_reg_read($3, file);
  53. int reg_src2 = get_reg_read($4, file);
  54. fprintf(file, "EQU %d %d %d\n", reg_dest, reg_src1, reg_src2);};
  55. Instruction : tAFC tNB tNB {increment_time();
  56. int reg_dest = get_reg_write($2, file);
  57. fprintf(file, "AFC %d %d\n", reg_dest, $3);};
  58. Instruction : tCPY tNB tNB {increment_time();
  59. int reg_dest = get_reg_write($2, file);
  60. int reg_src = get_reg_read($3, file);
  61. fprintf(file, "CPY %d %d\n", reg_dest, reg_src);};
  62. Instruction : tAFCA tNB tNB {increment_time();
  63. int reg_dest = get_reg_write($2, file);
  64. fprintf(file, "AFCA %d %d\n", reg_dest, $3);};
  65. Instruction : tCPYA tNB tNB {increment_time();
  66. int reg_dest = get_reg_write($2, file);
  67. int reg_src = get_reg_read($3, file);
  68. fprintf(file, "CPYA %d %d\n", reg_dest, reg_src);};
  69. Instruction : tJMP tNB {increment_time();
  70. fprintf(file, "JMP %d\n", $2);};
  71. Instruction : tJMF tNB tNB {increment_time();
  72. int reg_src = get_reg_read($2, file);
  73. int reg_aux = get_reg_write(-1, file);
  74. fprintf(file, "SUB %d %d %d\n", reg_aux, reg_aux, reg_src);
  75. fprintf(file, "JMZ %d\n", $3);};
  76. Instruction : tWR tNB tNB {increment_time();
  77. int reg_dest = get_reg_write($2, file);
  78. int reg_addr = get_reg_read($3, file);
  79. fprintf(file, "LOADA %d %d\n", reg_dest, reg_addr);};
  80. Instruction : tREAD tNB tNB {increment_time();
  81. int reg_addr = get_reg_read($2, file);
  82. int reg_value = get_reg_read($3, file);
  83. fprintf(file, "STOREA %d %d\n", reg_addr, reg_value);};
  84. Instruction : tGET tNB {increment_time();
  85. int reg_dest = get_reg_write($2, file);
  86. fprintf(file, "GET %d\n", reg_dest);};
  87. Instruction : tPRI tNB {increment_time();
  88. int reg_src = get_reg_read($2, file);
  89. fprintf(file, "PRI %d\n", reg_src);};
  90. Instruction : tCALL tNB tNB {increment_time();
  91. flush_and_init(file);
  92. fprintf(file, "CALL %d %d\n", $2, $3);};
  93. Instruction : tRET {increment_time();
  94. flush_and_init(file);
  95. fprintf(file, "RET\n");};
  96. Instruction : tSTOP {increment_time();
  97. fprintf(file, "STOP\n");};
  98. %%
  99. int main(void) {
  100. file = stdout;
  101. init();
  102. yyparse();
  103. return 0;
  104. }