Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *********************************************
- * 314 Principles of Programming Languages *
- * Spring 2017 *
- * Author: Ulrich Kremer *
- * Student Version *
- *********************************************
- */
- #include <assert.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "InstrUtils.h"
- #include "Utils.h"
- Instruction *RemoveInstr(Instruction* head,Instruction* target);
- void EvaluateInstr(Instruction* target,Instruction*head);
- void SearchMemoryInstr (Instruction* start, Instruction* head,int reg, int offset);
- void SearchRegisterInstr(Instruction* start, Instruction* head,int reg);
- int main()
- {
- Instruction *head,*tail,*ptr;
- puts("<1>");
- head = ReadInstructionList(stdin);
- puts("<1>");
- if (!head) {
- WARNING("No instructions\n");
- exit(EXIT_FAILURE);
- }
- puts("<2>");
- tail = head->prev;
- ptr = head;
- do{
- ptr -> critical = 'n';
- ptr = ptr->next;
- }
- while(ptr!=head);
- ptr = head->prev;
- do{
- EvaluateInstr(ptr,head);
- if(ptr->critical=='n'){
- RemoveInstr(head,ptr);
- }
- }while(ptr!=tail);
- /* YOUR CODE GOES HERE */
- if (head)
- PrintInstructionList(stdout, head);
- return EXIT_SUCCESS;
- }
- //deletes Instruction and returns new head if there is only one target left in the linked list then it will return Null
- Instruction *RemoveInstr(Instruction* head,Instruction* target){
- if(target->next==target){
- free(target);
- printf("Deleted final isntruction. Prepare for Errors");
- return NULL;
- }
- (target->prev)->next = target -> next;
- (target->next)->prev= target -> prev;
- printf("Deleting Instruction - #%d\n",target->opcode);
- if(target == head){
- Instruction* holder = target->next;
- free(target);
- return holder;
- }
- else{
- free(target);
- return head;
- }
- }
- //marks appropriate values as critical
- void EvaluateInstr(Instruction* target,Instruction* head){
- if(target->opcode==OUTPUTAI){
- target->critical = 'y';
- SearchMemoryInstr(target,head,target->field1,target->field2);
- return;
- }
- if(target->critical=='n')
- return;
- switch(target->opcode){
- case LOADI:
- break;
- case LOADAI:
- SearchMemoryInstr(target,head,target->field1,target->field2);
- break;
- case STOREAI:
- SearchRegisterInstr(target,head,target->field1);
- break;
- case ADD:
- case SUB:
- case MUL:
- case DIV:
- SearchRegisterInstr(target,head,target->field1);
- SearchRegisterInstr(target,head,target->field2);
- break;
- default:
- break;
- }
- return;
- }
- //Searches for an instr that sets the location passed in memory
- //returns 1 if found
- //returns 0 if did not find
- void SearchMemoryInstr (Instruction* start, Instruction* head,int reg, int offset){
- Instruction* ptr = start;
- while(ptr!=head->prev){
- if(ptr->opcode==STOREAI){
- if(ptr->field2==reg &&ptr->field3==offset){
- printf("BUGTEST: MARKED INSTR %d, AS CRITICAL/n",ptr->opcode);
- ptr-> critical ='y';
- return;
- }
- }
- ptr = ptr->prev;
- }
- printf("ERROR did not find register%d,offset%d\n",reg,offset);
- return;
- }
- void SearchRegisterInstr(Instruction* start, Instruction* head,int reg){
- Instruction* ptr = start;
- while(ptr!=head->prev){
- switch(start->opcode){
- case LOADI:
- if(ptr->field2==reg){
- printf("BUGTEST: MARKED INSTR %d, AS CRITICAL/n",ptr->opcode);
- ptr->critical='y';
- return;
- }
- break;
- case LOADAI:
- if(ptr->field3==reg){
- printf("BUGTEST: MARKED INSTR %d, AS CRITICAL/n",ptr->opcode);
- ptr->critical='y';
- return;
- }
- case ADD:
- case SUB:
- case MUL:
- case DIV:
- if(ptr->field3==reg){
- printf("BUGTEST: MARKED INSTR %d, AS CRITICAL/n",ptr->opcode);
- ptr->critical='y';
- return;
- }
- break;
- default:
- break;
- }
- ptr = ptr->prev;
- }
- printf("ERROR did not find register%d\n",reg);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement