le compilateur super performant
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.

yacc_util.c 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "yacc_util.h"
  2. void write_affectation(SymbolTable* symbol_table, InstructionTable *instruction_table, char* symbol_dest, SymbolItem* src) {
  3. mark_symbol_initialized(symbol_table, symbol_dest);
  4. SymbolItem *dest = get_symbol_item(symbol_table, symbol_dest);
  5. add_instruction(instruction_table, COP, dest->address, src->address, 0);
  6. }
  7. const SymbolItem* write_op(SymbolTable* symbol_table, InstructionTable *instruction_table, Instruction instruction, SymbolItem *op1, SymbolItem *op2) {
  8. const SymbolItem* dest = add_temp_symbol(symbol_table, TYPE_INT);
  9. add_instruction(instruction_table, instruction, dest->address, op1->address, op2->address);
  10. return dest;
  11. }
  12. const SymbolItem* write_negation(SymbolTable* symbol_table, InstructionTable *instruction_table, SymbolItem *op2) {
  13. // Create temp variable with 0
  14. const SymbolItem* op1 = init_temp_symbol(symbol_table, instruction_table, 0);
  15. // Make the 0 - variable operation
  16. add_instruction(instruction_table, SOU, op1->address, op1->address, op2->address);
  17. return op1;
  18. }
  19. const SymbolItem* init_temp_symbol(SymbolTable* symbol_table, InstructionTable *instruction_table, int constant) {
  20. const SymbolItem* dest = add_temp_symbol(symbol_table, TYPE_INT);
  21. add_instruction(instruction_table, AFC, dest->address, constant, 0);
  22. return dest;
  23. }
  24. void write_print(InstructionTable *instruction_table, SymbolItem *src) {
  25. add_instruction(instruction_table, PRI, src->address, 0, 0);
  26. }
  27. int update_jmf(InstructionTable *table, int address) {
  28. if (address > table->index) {
  29. return -1;
  30. }
  31. table->table[address].arg2 = get_current_address(table);
  32. return 0;
  33. }
  34. int write_jmf(InstructionTable *table, SymbolItem* cond) {
  35. int addr = get_current_address(table);
  36. add_instruction(table, JMF, cond->address, 0, 0);
  37. return addr;
  38. }
  39. const SymbolItem* write_not_condition(SymbolTable* symbol_table, InstructionTable *instruction_table, const SymbolItem* cond) {
  40. // Create temp variable with 0
  41. const SymbolItem* op1 = init_temp_symbol(symbol_table, instruction_table, 0);
  42. // Check if the condition is equal to 0 (makes the negation)
  43. add_instruction(instruction_table, EQU, op1->address, op1->address, cond->address);
  44. return op1;
  45. }
  46. const SymbolItem* write_condition(SymbolTable* symbol_table, InstructionTable *instruction_table, Condition* op, SymbolItem* cond1, SymbolItem* cond2) {
  47. const SymbolItem *dest = write_op(symbol_table, instruction_table, op->instruction, cond1, cond2);
  48. if (op->not) {
  49. return write_not_condition(symbol_table, instruction_table, dest);
  50. }
  51. return dest;
  52. }