Compilateur/Tables/Instructions/tab_instruc.c
2021-05-09 14:58:36 +02:00

106 lines
2.5 KiB
C

#include "tab_instruc.h"
int current_index = 1;
struct operation_t tab_op[MAXTAILLE];
void add_operation(enum opcode_t opcode, int arg1, int arg2, int arg3){
if (current_index == MAXTAILLE){
printf("Taillemax tableau operations atteinte\n");
}
else{
struct operation_t new_op = {opcode, arg1, arg2, arg3};
tab_op[current_index] = new_op;
current_index++;
}
}
void create_jump_to_main(int line){
struct operation_t new_op = {JMP,line, 0, 0};
tab_op[0] = new_op;
}
char * get_asm_line_from_op(struct operation_t op){
char * buffer = malloc(sizeof(char)*200);
switch (op.opcode){
case (ADD):
sprintf(buffer,"ADD %d %d %d\n",op.arg1, op.arg2, op.arg3);
break;
case (MUL):
sprintf(buffer,"MUL %d %d %d\n",op.arg1, op.arg2, op.arg3);
break;
case (SOU):
sprintf(buffer,"SOU %d %d %d\n",op.arg1, op.arg2, op.arg3);
break;
case (DIV):
sprintf(buffer,"DIV %d %d %d\n",op.arg1, op.arg2, op.arg3);
break;
case (COP):
sprintf(buffer,"COP %d %d\n",op.arg1, op.arg2);
break;
case (AFC):
sprintf(buffer,"AFC %d %d\n",op.arg1, op.arg2);
break;
case (AFCA):
sprintf(buffer,"AFCA %d %d\n",op.arg1, op.arg2);
break;
case (JMP):
sprintf(buffer,"JMP %d\n",op.arg1);
break;
case (JMF):
sprintf(buffer,"JMF %d %d\n",op.arg1, op.arg2);
break;
case (INF):
sprintf(buffer,"INF %d %d %d\n",op.arg1, op.arg2, op.arg3);
break;
case (SUP):
sprintf(buffer,"SUP %d %d %d\n",op.arg1, op.arg2, op.arg3);
break;
case (EQU):
sprintf(buffer,"DIV %d %d %d\n",op.arg1, op.arg2, op.arg3);
break;
case (PRI):
sprintf(buffer,"PRI %d\n",op.arg1);
break;
case (READ):
sprintf(buffer,"READ %d %d\n",op.arg1, op.arg2);
break;
case (WR):
sprintf(buffer,"WR %d %d\n",op.arg1, op.arg2);
break;
case (CALL):
sprintf(buffer,"CALL %d %d\n",op.arg1, op.arg2);
break;
case (RET):
sprintf(buffer,"RET\n");
break;
case (GET):
sprintf(buffer,"GET %d\n",op.arg1);
break;
case (STOP):
sprintf(buffer,"STOP %d\n", op.arg1);
break;
}
return buffer;
}
void create_asm(){
FILE * output = fopen("output.txt","w");
for (int i = 0; i < current_index; i++){
char * line = get_asm_line_from_op(tab_op[i]);
fputs(line, output);
free(line);
}
}
int get_current_index(){return current_index;}
void patch(int index, int arg){
if (tab_op[index].opcode == JMP){
tab_op[index].arg1 = arg;
}
else if (tab_op[index].opcode == JMF){
tab_op[index].arg2 = arg;
}
}