int prep_instruction(t_chp *cur, t_vm *vm) { t_param par; int i; int pc_save; if (cur->inst == NULL) return (SUCCESS); pc_save = cur->pc - 1; i = -1; if (no_bytecode_inst(cur->inst->hexcode)) { par.val[0] = get_param(vm, cur, T_DIR); exec_instruction_no_code(cur, vm, &par, pc_save); } else { get_bytecode(vm->mem[circ(cur->pc++)], par.type); while (++i < cur->inst->nb_args) { par.val[i] = get_param(vm, cur, par.type[i]); } exec_instruction_coded(cur, vm, &par, pc_save); } return (SUCCESS); }
void process_block_decl(char **decl, int nlines) { char *name,*cur,*region_name,operand_name[40]; struct memory_region *bytes, *operands; initialize(); sprintf(operand_name,"ops%i",operand_region_counter++); cur = decl[0]+1; eat_whitespace(&cur); name=grab_identifier(&cur); region_name = bytecode_malloc(strlen(name)+2); strcpy(region_name,"#"); strcat(region_name,name); bytes = mr_new(region_name,3 + (nlines+17)/8); operands = mr_new(operand_name,nlines*2); mr_mark_ptr(bytes,1,operands); bytes->data[0] = 0; int current_operand = 0; int current_byte = 0; int current_word = 2; int i; uint64_t word = 0; char *byte = (char *)&word; for(i=1;i<nlines;i++) { char *cur_line = decl[i]; *byte++ = get_bytecode(&cur_line); cur_line++; eat_whitespace(&cur_line); while(*cur_line != '\0') { process_argument(operands,¤t_operand,&cur_line); eat_whitespace(&cur_line); } if(++current_byte==8) { current_byte = 0; bytes->data[current_word] = word; byte = (char *)&word; current_word++; word = 0; } } bytes->data[current_word] = word; bytes->data[current_word+1] = 0; *byte = 0; }
void bc_dump(FILE *fptr, byte *pc) { intptr_t begin = (intptr_t) pc; while (true) { fprintf(fptr, "%d: ", (int) ((intptr_t) pc - begin)); int bc = get_bytecode(pc); switch (bc) { case BC_INVALID: fprintf(fptr, "invalid"); break; case BC_SHIFT: fprintf(fptr, "shift [delta = %d]", get_payload(pc, 0)); break; case BC_ADD: fprintf(fptr, "add [value = %d]", get_payload(pc, 0)); break; case BC_MOVE_VALUE: fprintf(fptr, "move_value [value = %d]", get_payload(pc, 0)); break; case BC_ZERO: fprintf(fptr, "zero"); break; case BC_OUTPUT: fprintf(fptr, "output"); break; case BC_INPUT: fprintf(fptr, "input"); break; case BC_LOOP_BEGIN: fprintf(fptr, "loop-begin [counter-idx = %d] [length = %d]", get_payload(pc, 0), get_payload(pc, 1)); break; case BC_LOOP_END: fprintf(fptr, "loop-end [length = %d]", get_payload(pc, 0)); break; case BC_COMPILED_LOOP: fprintf(fptr, "compiled-loop"); break; case BC_HLT: fprintf(fptr, "hlt"); goto end; } fprintf(fptr, "\n"); pc += get_total_length(bc); } end: fprintf(fptr, "\n"); }