65 lines
2.6 KiB
C
65 lines
2.6 KiB
C
#include "yacc_util.h"
|
|
|
|
void write_affectation(SymbolTable* symbol_table, InstructionTable *instruction_table, char* symbol_dest, SymbolItem* src) {
|
|
mark_symbol_initialized(symbol_table, symbol_dest);
|
|
SymbolItem *dest = get_symbol_item(symbol_table, symbol_dest);
|
|
add_instruction(instruction_table, COP, dest->address, src->address, 0);
|
|
}
|
|
|
|
const SymbolItem* write_op(SymbolTable* symbol_table, InstructionTable *instruction_table, Instruction instruction, SymbolItem *op1, SymbolItem *op2) {
|
|
const SymbolItem* dest = add_temp_symbol(symbol_table, TYPE_INT);
|
|
add_instruction(instruction_table, instruction, dest->address, op1->address, op2->address);
|
|
return dest;
|
|
}
|
|
|
|
const SymbolItem* write_negation(SymbolTable* symbol_table, InstructionTable *instruction_table, SymbolItem *op2) {
|
|
// Create temp variable with 0
|
|
const SymbolItem* op1 = init_temp_symbol(symbol_table, instruction_table, 0);
|
|
// Make the 0 - variable operation
|
|
add_instruction(instruction_table, SOU, op1->address, op1->address, op2->address);
|
|
return op1;
|
|
}
|
|
|
|
const SymbolItem* init_temp_symbol(SymbolTable* symbol_table, InstructionTable *instruction_table, int constant) {
|
|
const SymbolItem* dest = add_temp_symbol(symbol_table, TYPE_INT);
|
|
add_instruction(instruction_table, AFC, dest->address, constant, 0);
|
|
return dest;
|
|
}
|
|
|
|
void write_print(InstructionTable *instruction_table, SymbolItem *src) {
|
|
add_instruction(instruction_table, PRI, src->address, 0, 0);
|
|
}
|
|
|
|
int update_jmf(InstructionTable *table, int address) {
|
|
if (address > table->index) {
|
|
return -1;
|
|
}
|
|
table->table[address].arg2 = get_current_address(table);
|
|
return 0;
|
|
}
|
|
|
|
int write_jmf(InstructionTable *table, SymbolItem* cond) {
|
|
int addr = get_current_address(table);
|
|
add_instruction(table, JMF, cond->address, 0, 0);
|
|
return addr;
|
|
}
|
|
|
|
const SymbolItem* write_not_condition(SymbolTable* symbol_table, InstructionTable *instruction_table, const SymbolItem* cond) {
|
|
// Create temp variable with 0
|
|
const SymbolItem* op1 = init_temp_symbol(symbol_table, instruction_table, 0);
|
|
// Check if the condition is equal to 0 (makes the negation)
|
|
add_instruction(instruction_table, EQU, op1->address, op1->address, cond->address);
|
|
return op1;
|
|
}
|
|
|
|
const SymbolItem* write_condition(SymbolTable* symbol_table, InstructionTable *instruction_table, Condition* op, SymbolItem* cond1, SymbolItem* cond2) {
|
|
const SymbolItem *dest = write_op(symbol_table, instruction_table, op->instruction, cond1, cond2);
|
|
if (op->not) {
|
|
return write_not_condition(symbol_table, instruction_table, dest);
|
|
}
|
|
return dest;
|
|
}
|
|
|
|
|
|
|
|
|