int main() { Instruction *head; Instruction *curr, *prev, *next; head = ReadInstructionList(stdin); if (!head) { ERROR("No instructions\n"); exit(EXIT_FAILURE); } curr = head; prev = head->prev; next = head->next; while(curr && next){ if(prev && prev->opcode == LOADI && curr && curr->opcode == LOADI){ switch(next->opcode){ /*add*/ case ADD: curr->field1 = next->field1; curr->field2 = prev->field2 + curr->field2; /*sub*/ case SUB: curr->field1 = next->field1; if(next->field2 > next->field3){ curr->field2 = curr->field2 - prev->field2; } else{ curr->field2 = curr->field2 - prev->field2; } /*mul*/ case MUL: curr->field1 = next->field1; curr->field2 = curr->field2 * prev->field2; curr->next = next->next; curr->next->prev = curr; curr->prev = prev->prev; curr->prev->next = curr; free(prev); free(next); default: break; } } curr = curr->next; prev = curr->prev; next = curr->next; } PrintInstructionList(stdout, head); DestroyInstructionList(head); return EXIT_SUCCESS; }
int main() { Instruction *head; head = ReadInstructionList(stdin); /* --- FIRST: ALGEBRAIC SIMPLIFICATION PASS --- */ if (!head) { ERROR("No instructions\n"); exit(EXIT_FAILURE); } handle_simplification(head); /* --- SECOND: CONSTANT FOLDING PASS --- */ if (!head) { ERROR("No instructions\n"); exit(EXIT_FAILURE); } handle_folding(head); PrintInstructionList(stdout, head); DestroyInstructionList(head); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { FILE *infile; Instruction *head, *instr; int Memory[5]; int RegisterFile[MAX_REG_NUM]; int input; int instrCounter = 0; /* counts number of executed instructions */ if (argc != 2) { fprintf(stderr, "Use of command:\n run <RISC code file>\n"); exit(EXIT_FAILURE); } memset(Memory, '\0', sizeof(Memory)); infile = fopen(argv[1], "r"); if (!infile) { ERROR("Cannot open input file \"%s\"\n", argv[1]); exit(EXIT_FAILURE); } head = ReadInstructionList(infile); fclose(infile); instr = head; while (instr) { switch (instr->opcode) { case LOAD: /* based on ASCII representation */ /* of variable names */ RegisterFile[instr->field1] = Memory[instr->field2 - 'a']; break; case LOADI: RegisterFile[instr->field1] = instr->field2; break; case STORE: Memory[instr->field1 - 'a'] = RegisterFile[instr->field2]; break; case ADD: RegisterFile[instr->field1] = RegisterFile[instr->field2] + RegisterFile[instr->field3]; break; case SUB: RegisterFile[instr->field1] = RegisterFile[instr->field2] - RegisterFile[instr->field3]; break; case MUL: RegisterFile[instr->field1] = RegisterFile[instr->field2] * RegisterFile[instr->field3]; break; case READ: printf("tinyL>> enter value for \"%c\": ", instr->field1); scanf("%d", &input); Memory[instr->field1 - 'a'] = input; break; case WRITE: printf("tinyL>> %c = %d\n", instr->field1, Memory[instr->field1 - 'a']); break; default: ERROR("Illegal instructions\n"); } instrCounter++; instr = instr->next; } DestroyInstructionList(head); printf("Executed %d instructions\n", instrCounter); return EXIT_SUCCESS; }