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 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. %union {
  2. int nombre;
  3. }
  4. %{
  5. #include "tables.h"
  6. #include <stdio.h>
  7. FILE * file;
  8. FILE * file2;
  9. %}
  10. %token tMUL tDIV tADD tSUB
  11. %token tAFC tCPY
  12. %token<nombre> tNB
  13. %%
  14. Programme : Instruction Programme;
  15. Programme : Instruction;
  16. Instruction : tMUL tNB tNB tNB {increment_time();
  17. int added_instruction = 0;
  18. int reg_src1 = get_reg_read($3, &added_instruction);
  19. int reg_src2 = get_reg_read($4, &added_instruction);
  20. int reg_dest = get_reg_write($2, &added_instruction);
  21. add_instruction(MUL, reg_dest, reg_src1, reg_src2);
  22. new_instruction(added_instruction + 1);};
  23. Instruction : tADD tNB tNB tNB {increment_time();
  24. int added_instruction = 0;
  25. int reg_src1 = get_reg_read($3, &added_instruction);
  26. int reg_src2 = get_reg_read($4, &added_instruction);
  27. int reg_dest = get_reg_write($2, &added_instruction);
  28. add_instruction(ADD, reg_dest, reg_src1, reg_src2);
  29. new_instruction(added_instruction + 1);};
  30. Instruction : tDIV tNB tNB tNB {increment_time();
  31. int added_instruction = 0;
  32. int reg_src1 = get_reg_read($3, &added_instruction);
  33. int reg_src2 = get_reg_read($4, &added_instruction);
  34. int reg_dest = get_reg_write($2, &added_instruction);
  35. add_instruction(DIV, reg_dest, reg_src1, reg_src2);
  36. new_instruction(added_instruction + 1);};
  37. Instruction : tSUB tNB tNB tNB {increment_time();
  38. int added_instruction = 0;
  39. int reg_src1 = get_reg_read($3, &added_instruction);
  40. int reg_src2 = get_reg_read($4, &added_instruction);
  41. int reg_dest = get_reg_write($2, &added_instruction);
  42. add_instruction(SUB, reg_dest, reg_src1, reg_src2);
  43. new_instruction(added_instruction + 1);};
  44. Instruction : tAFC tNB tNB {increment_time();
  45. int added_instruction = 0;
  46. int reg_dest = get_reg_write($2, &added_instruction);
  47. add_instruction(AFC, reg_dest, $3, 0);
  48. new_instruction(added_instruction + 1);};
  49. Instruction : tCPY tNB tNB {increment_time();
  50. int added_instruction = 0;
  51. int reg_src = get_reg_read($3, &added_instruction);
  52. int reg_dest = get_reg_write($2, &added_instruction);
  53. add_instruction(CPY, reg_dest, reg_src, 0);
  54. new_instruction(added_instruction + 1);};
  55. %%
  56. #include <string.h>
  57. int main(int argc, char *argv[]) {
  58. char *output_filename = "output_cross";
  59. if (argc >= 2) {
  60. output_filename = argv[1];
  61. }
  62. char *asm_filename = malloc(strlen(output_filename) + 5);
  63. char *bin_filename = malloc(strlen(output_filename) + 9);
  64. strcpy(asm_filename, output_filename);
  65. strcpy(bin_filename, output_filename);
  66. strcat(asm_filename, ".asm");
  67. strcat(bin_filename, "_bin.txt");
  68. file = fopen(asm_filename, "w");
  69. file2 = fopen(bin_filename, "w");
  70. init();
  71. yyparse();
  72. flush_and_init();
  73. write_asm(file);
  74. write_code_machine(file2);
  75. free(asm_filename);
  76. free(bin_filename);
  77. return 0;
  78. }