int interpret_line(char *str, t_info *info, int i) { int j; int start_reading; int found_command; start_reading = check_for_label(str); found_command = 0; j = start_reading; if (str[0] == '\0') return (0); if (ft_strncmp(NAME_CMD_STRING, str, ft_strlen(NAME_CMD_STRING)) == 0) return (0); if (ft_strncmp(COMMENT_CMD_STRING, str, ft_strlen(COMMENT_CMD_STRING)) == 0) return (0); while (str[j] != '\0' && str[j] != COMMENT_CHAR) { if (found_command == 0 && str[j] != ' ' && str[j] != '\t') { info->line_nbr = i + 1; if (get_command(info, str, &j, &found_command) == -1) return (-1); else return (0); } j++; } return (0); }
/*** set_label_addresses * This function is used to initially cycle through the input file looking for labels. This is necessary, * because if, for example, you come across a beq instruction that references a label that hasn't been * declared yet, the compilation will fail. ***/ struct instruction* set_label_addresses(struct instruction *inst, FILE *input) { char string[BUFFER_SIZE]; char label[BUFFER_SIZE]; char* c; while(fscanf(input, "%s", string) != EOF) { if (check_for_label(string) == 1) strcpy(label, string); else strcpy(label, ""); inst = add_ll_node(inst, label); c = fgets(string, BUFFER_SIZE, input); } return inst; }
//done void check_line_syntax (char* token) { printf("check_line_syntax('%s')\n", token); //check if its a label token = check_for_label(token); printf(" my token is %s \n ", token); //store the op in an int if(token == NULL) return; int myop = util_get_opcode(token); printf("opcode is: %d\n", myop ); //store it itno my data structure.. currInfo -> opcode = myop; if(myop == OP_BR){ currInfo->reg1 = util_parse_cond(token+2); token = next_token(); currInfo->reference = strdup(token); //get_operand(op,token); } LC3_inst_t* inst = lc3_get_inst_info(myop); printf("inst is: %p \n" , inst); int position = 0; if(strcasecmp(inst ->forms[0].name,token) != 0){ position = 1; } /*if(inst ->forms[1].name != NULL &&strcasecmp(inst ->forms[1].name,token) == 0){ position = 1; if(strcasecmp(inst ->forms[0].name,token) != 0) position = 1; }*/ if(myop != OP_BR){ currInfo->form=position;} printf("%d\n whats my position", position); operands_t format = inst -> forms[position].operands; scan_operands(format); }
/*** process_file * This function is the main input file processing loop. It takes the instruction string in ASM and turns it into * machine code. First, the op-code and corresponding funct are determined and then the register names are read in and processed. * The offset and jsec values are determined using other functions. ***/ struct instruction* process_file(struct instruction *inst, FILE *input) { char string[BUFFER_SIZE]; //note that the current instruction address is tracked. this is for use with branching and jumps. int op, r1, r2, r3, funct, offset, jsec, address=0x4000; while(fscanf(input, "%s", string) != EOF) { if (check_for_label(string) == 1) { fscanf(input, "%s", string); } set_op_funct(&op, &funct, string); if (op == 0) //add, sub, and, or, slt, sltu { fscanf(input, "%s", string); r3 = get_reg(string); fscanf(input, "%s", string); r1 = get_reg(string); fscanf(input, "%s", string); r2 = get_reg(string); offset = 0; jsec = 0; } else if (op == 2) //j { r1 = 0; r2 = 0; r3 = 0; offset = 0; fscanf(input, "%s", string); jsec = find_label_address(inst, strcat(string,":")); } else if (op == 4) //beq { fscanf(input, "%s", string); r1 = get_reg(string); fscanf(input, "%s", string); r2 = get_reg(string); r3 = 0; fscanf(input, "%s", string); offset = calculate_offset(address, find_label_address(inst, strcat(string,":"))); jsec = 0; } else if (op == 8) //addi { fscanf(input, "%s", string); r1 = get_reg(string); fscanf(input, "%s", string); r2 = get_reg(string); r3 = 0; fscanf(input, "%d", &offset); jsec = 0; } else if (op == 10 || op == 11) //slti, sltiu { fscanf(input, "%s", string); r2 = get_reg(string); fscanf(input, "%s", string); r1 = get_reg(string); r3 = 0; fscanf(input, "%d", &offset); jsec = 0; } else if (op == 15) //lui { fscanf(input, "%s", string); r1 = 0; r2 = get_reg(string); r3 = 0; fscanf(input, "%d", &offset); jsec = 0; } else if (op == 35 || op == 43) //lw, sw { fscanf(input, "%s", string); r2 = get_reg(string); fscanf(input, "%s", string); int offset, reg; get_mem_offset_and_word_reg(string, &offset, ®); r1 = reg; r3 = 0; offset = offset; jsec = 0; } inst = modify_ll_node(inst, address, op, r1, r2, r3, funct, offset, jsec); address += 0x4; //the current address is incremented by 4 after every instruction is processed } return inst; }