void print_instructions(instructionlist_t instructions, int indent) { unsigned i = 0; for (; i + 1 < instructions.size; ++i) print_instruction(list_nth(instructions, i), indent); if (instructions.size > 0) print_instruction(list_nth(instructions, i), indent); }
int main() { unsigned int program[SZ]= {0x20340000, 0x62340000, 0x30050084, 0x50560004, 0x10000000}; print_instruction(0x60320000); printf("\n"); print_instruction(0x800000c8); printf("\n\n"); disassemble(program, SZ); return 0; }
int main() { print_instruction(0x00000000); printf("\n"); print_instruction(0x10000000); printf("\n"); print_instruction(0x90000000); printf("\n"); print_instruction(0xd0000000); printf("\n"); return 0; }
//The main part of the game running problems and movement etc. void run(roomGrid *rg, progress *pz) { SDL_Window *window = NULL; char *instructions_list[NUM_INSTRUCTIONS]; initialise_SDL_component(window, rg); get_instructions(instructions_list); Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, AUDIO_S16SYS, MUSIC_CONST_ONE, MUSIC_CONST_TWO); Mix_Chunk *mus = Mix_LoadWAV("20141124b.wav"); //Prints the opening scene. print_instruction(rg, instructions_list, 0, 10); draw(rg, pz, mus, instructions_list); atexit(SDL_Quit); //Mix_HaltMusic(); //Mix_FreeChunk(mus); //Mix_CloseAudio(); // Mix_Quit(); IMG_Quit(); SDL_Quit(); }
void print_text(int dlvl, instruction *instr) { if (verbosity >= dlvl) while (instr) { print_instruction(instr); instr = instr->next; } }
void neill_notes(roomGrid *room_grid, char *instructions_list[NUM_INSTRUCTIONS]) { room_grid -> finished = 0; //Prints the instructions for the Neill's Notes cutscene. print_instruction(room_grid, instructions_list, 10, 22); }
void printInstructions(){ int i; numElem *tmpNum; stringElem *tmpStr; userfunc *tmpFunc; printf("\n- [NUM ARRAY] -\n"); for(tmpNum = numConsts, i=0; tmpNum ; tmpNum = tmpNum->next, i++){ printf("[%3d] %f\n", i, tmpNum->num); } printf("\n- [STRING ARRAY] -\n"); for(tmpStr = stringConsts, i=0; tmpStr ; tmpStr = tmpStr->next, i++){ printf("[%3d] %s\n", i, tmpStr->s); } printf("\n- [USER FUNC ARRAY] -\n"); for(tmpFunc = userFuncs, i=0; tmpFunc ; tmpFunc = tmpFunc->next, i++){ printf("[%3d] %s, address: %d, locals: %d\n", i, tmpFunc->id,tmpFunc->address, tmpFunc->localSize); } printf("\n- [LIB FUNC ARRAY] -\n"); for(tmpStr = namedLibfuncs, i=0; tmpStr ; tmpStr = tmpStr->next, i++){ printf("[%3d] %s\n", i, tmpStr->s); } printf("\n- [INSTRUCTIONS] -\n"); printf("[index // opcode // \tresult(type),offset // \targ1(type),offset // \typearg2(type),offset]\n"); for(i = 0; i < currInstruction;i++){ printf("[%3d] ",i); print_instruction(Instructions+i); } }
instruction decode_instruction(word val) { instruction i; word a, b; /* Basic op codes: * bbbbbbaaaaaaoooooo */ i.op_code = val & 0x000F; i.type = BASIC; a = (val & 0x03F0) >> 4; b = (val & 0xFC00) >> 10; if (i.op_code == 0x00) { /* Non-basic op codes: * aaaaaaoooooo0000 */ i.op_code = a; i.type = NONBASIC; i.a = get_value(b, &tempA); } else { i.a = get_value(a, &tempA); i.b = get_value(b, &tempB); } if (verbose) print_instruction(i); return i; }
void print_program(Machine *pmach) { printf("\n*** PROGRAM (size: %d) ***\n", pmach->_textsize); for(unsigned i = 0; i < pmach->_textsize; i++) { printf("0x%04x: 0x%08x \t ", i, pmach->_text[i]._raw); print_instruction(pmach->_text[i], i); printf("\n"); } }
void print_instructions(const Instructions* instr) { printf("Instructions:\n"); for (int i = 0; i < instr->size; i++) { print_instruction(&instr->instr[i]); } }
/*! * Les instructions sont affichées sous forme symbolique, précédées de leur adresse. .* * \param pmach la machine en cours d'exécution */ void print_program(Machine *pmach) { printf("\n*** PROGRAM (size: %d) ***\n",pmach->_textsize); for(int i = 0 ; i < pmach->_textsize ; i++) { printf("0x%04x: 0x%08x\t", i, pmach->_text[i]._raw); print_instruction(pmach->_text[i],pmach->_text[i].instr_absolute._address); putchar('\n'); } }
static void test_instruction(Instr instr, int expval) { int retval= print_instruction(instr); if (expval) printf("\n"); if (retval != expval) { printf("print_instruction() returned %d!\n", retval); printf("Test failed\n"); exit(-1); } }
void display_pipeline() { // call this function from within gdb to print out status of pipeline // if you encounter a bug, or to visualize pipeline operation // (this is a good way to "verify" your pipeline model makes sense!) printf("========================================\n"); printf("pipeline status at end of cycle %u:\n", sim_cycle); printf("========================================\n"); printf("PC := 0x%6x\n", g_fetch_pc ); printf("IF/ID := "); print_instruction( g_piperegister[IF_ID_REGISTER] ); printf("ID/EX := "); print_instruction( g_piperegister[ID_EX_REGISTER] ); printf("EX/MEM := "); print_instruction( g_piperegister[EX_MEM_REGISTER] ); printf("MEM/WB := "); print_instruction( g_piperegister[MEM_WB_REGISTER] ); printf("\n"); }
void first_problem(roomGrid *room_grid, char *instructions_list[NUM_INSTRUCTIONS]) { room_grid -> finished = 0; wrong_right correct_indicator = incorrect; char *correct_answer = "fan < too hot"; //Prints first part of the instructions. print_instruction(room_grid, instructions_list, 22, 27); //Now we go to the input screen for the text. while(correct_indicator != correct && room_grid -> problem_quitter == off){ input_screen(room_grid, &correct_indicator, correct_answer, 13); } //Prints second part of the instructions. if(room_grid -> problem_quitter == off){ print_instruction(room_grid, instructions_list, 27, 29); } }
/*! * \param pmach la machine en cours d'exécution */ void print_program(Machine *pmach) { printf("\n*** PROGRAM (size: %d) ***\n", pmach->_textsize); int i; for(i = 0; i < pmach->_textsize; ++i) { printf("0x%.4x: 0x%.8x\t", i, pmach->_text[i]._raw); print_instruction(pmach->_text[i], i); printf("\n"); } }
int main() { int retval; Instr instr= { BLT, 0x1ff8, 1, 2, 3 }; retval= print_instruction(instr); printf("\n"); if (retval != 1) { printf("print_instruction() returned %d!\n", retval); printf("Test failed\n"); return -1; } instr.addr_or_const= 0x2000; retval= print_instruction(instr); if (retval != 0) { printf("print_instruction() returned %d!\n", retval); printf("Test failed\n"); return -1; } printf("Test finished!\n"); return 0; }
int main() { int retval; Instr instr= {ADD, 0, 3, 5, 9}; retval= print_instruction(instr); printf("\n"); if (retval != 1) { printf("print_instruction() returned %d!\n", retval); printf("Test failed\n"); return -1; } printf("Test finished!\n"); return 0; }
int disassemble(const Instr program[], int num_elements){ int i=0; /*testing if program is null, or if num_elements is invalid*/ if(program==0||num_elements<0||num_elements>2048){ return 0; }else{ for(i=0;i<num_elements;i++){ /*if the instruction was invalid calc. in print_instruction*/ if(print_instruction(program[i])==0){ return 0; } printf("\n"); } return 1; } }
void print_program(program * program) { int label_i = 0; int i, j; for (i = 0; i < program->inst_count; ++i) { for (j = 0; j < program->label_count; ++j) { if (program->labels[j]->index == i) { printf("%s:\n", program->labels[j]->name); } } printf("%d ", i); print_instruction(program->insts[i]); } return; }
/*! * Affichage des instructions du programme * Les instructions sont affichées sous forme symbolique, précédées de leur adresse. .* * \param pmach la machine en cours d'exécution */ void print_program(Machine *pmach){ printf("\n### PROGRAM ###\n\n"); //pour chaque instruction on affiche : for(int i = 0 ; i < pmach->_textsize ; i++){ //l'adresse printf("\t0x%04x : 0x%08x\t", i, (pmach->_text[i])._raw); //l'instruction print_instruction(pmach->_text[i], i); //saut de ligne printf("\n"); } //taille du programme printf("\n Program size : %d\n",pmach->_textsize); }
static unsigned disasm(unsigned seg, unsigned off, char *buffer) { BYTE *segp = &memory[(seg << 4)]; struct Disasm *d; instruction_byte = GetMemInc(segp, off); d = &disasm_table[instruction_byte]; if (d->supp != NULL) print_instruction(segp, off, d->supp, buffer); else sprintf(buffer, (d->flags & DF_NOSPACE) ? "%s" : "%-6s ", itext[d->text]); if (d->type != NULL) off = (d->type)(segp, off, &buffer[strlen(buffer)]); return off; }
int print_insn_tic54x(bfd_vma memaddr, disassemble_info *info) { bfd_byte opbuf[2]; unsigned short opcode; int status, size; const template* tm; status = (*info->read_memory_func) (memaddr, opbuf, 2, info); if (status != 0) { (*info->memory_error_func) (status, memaddr, info); return -1; } opcode = bfd_getl16 (opbuf); tm = tic54x_get_insn (info, memaddr, opcode, &size); info->bytes_per_line = 2; info->bytes_per_chunk = 2; info->octets_per_byte = 2; info->display_endian = BFD_ENDIAN_LITTLE; if (tm->flags & FL_PAR) { if (!print_parallel_instruction (info, memaddr, opcode, tm, size)) return -1; } else { if (!print_instruction (info, memaddr, opcode, (char *) tm->name, tm->operand_types, size, (tm->flags & FL_EXT))) return -1; } return (size * 2); }
//The main part of the game running problems and movement etc. void run_main_game(roomGrid *room_grid, progress *puzzle, Chicken *hen) { char *instructions_list[NUM_INSTRUCTIONS]; get_instructions(instructions_list); Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, AUDIO_S16SYS, MUSIC_CONST_ONE, MUSIC_CONST_TWO); room_grid -> mus = Mix_LoadWAV("20141124b.wav"); //Prints the opening scene. print_instruction(room_grid, instructions_list, 0, 10); draw(room_grid, puzzle, instructions_list, hen); atexit(SDL_Quit); //Mix_HaltMusic(); //Mix_FreeChunk(mus); //Mix_CloseAudio(); // Mix_Quit(); IMG_Quit(); SDL_Quit(); }
int main(int argc, char **argv) { if (argc == 1) { fprintf(stderr, "You need to supply a binary as an argument e.g ./zdb <bin>"); exit(-1); } const char *path = argv[1]; const char *name = strrchr(path, '/'); if (name) { name += 1; } else { name = path; } pid_t pid; switch(pid = fork()) { // error case -1: { perror("fork()"); exit(-1); } // this is our child case 0: { ptrace(PTRACE_TRACEME, 1337, NULL); execl(path, name, NULL); perror("execl()"); exit(-1); } } int status; long opcode[4]; long previous_ip; wait(&status); while(1) { if(WIFEXITED(status) || (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL)) { printf("terminated"); exit(0); } if(WIFSTOPPED(status)) { long rip = ptrace(PTRACE_PEEKUSER, pid, 8 * RIP, NULL)-1; printf("Instruction stopped at: 0x%lx\n", rip); } /* read our user's command in */ zdb_command command = next_command(); switch(command.instruction) { case NEXT: { long ip = ptrace(PTRACE_PEEKUSER, pid, 8 * RIP, NULL); if(previous_ip) { long opcode_len = ip - previous_ip; if(opcode_len > 0 && opcode_len < 16) print_instruction(&opcode, opcode_len); } opcode[0] = ptrace(PTRACE_PEEKDATA, pid, ip, NULL); opcode[1] = ptrace(PTRACE_PEEKDATA, pid, ip + sizeof(long), NULL); previous_ip = ip; ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL); wait(&status); break; } case DUMP: dump_registers(pid); break; case HEXDUMP: { long addr; unsigned int size; sscanf(command.args, "%lx %u", &addr, &size); hexdump_region(addr, size, pid, true); break; } case STACK_TRACE: dump_stack_trace(pid); break; case QUIT: { printf("quitting zdb"); exit(0); } default: fprintf(stderr, "invalid or unknown command\n"); } } }
//--------------------------------------------------------------------------------- char * t_mep_chromosome::to_C_double(bool simplified, double *data, int problem_type) { char *prog = new char[(code_length + num_constants + num_total_variables) * 100 + 1000]; char tmp_s[100]; prog[0] = 0; strcat(prog, "#include <math.h>\n"); strcat(prog, "#include <stdio.h>\n"); strcat(prog, "\n"); strcat(prog, "void mepx(double *x /*inputs*/, double *outputs)"); strcat(prog, "\n"); strcat(prog, "{"); strcat(prog, "\n"); // here I have to declare the constants if (num_constants) { strcat(prog, "//constants ..."); strcat(prog, "\n"); sprintf(tmp_s, " double constants[%ld];", num_constants); strcat(prog, tmp_s); strcat(prog, "\n"); for (int i = 0; i < num_constants; i++) { sprintf(tmp_s, " constants[%d] = %lf;", i, constants_double[i]); strcat(prog, tmp_s); strcat(prog, "\n"); } strcat(prog, "\n"); } if (simplified) { strcat(prog, " double prg["); sprintf(tmp_s, "%d", num_utilized); strcat(prog, tmp_s); strcat(prog, "];"); strcat(prog, "\n"); for (int i = 0; i < num_utilized; i++) { sprintf(tmp_s, " prg[%d] = ", i); strcat(prog, tmp_s); if (s_prg[i].op < 0) { print_instruction(s_prg[i].op, s_prg[i].adr1, s_prg[i].adr2, s_prg[i].adr3, s_prg[i].adr4, tmp_s); strcat(prog, tmp_s); strcat(prog, "\n"); } else { // a variable if (s_prg[i].op < num_total_variables) { sprintf(tmp_s, "x[%d];", s_prg[i].op); strcat(prog, tmp_s); strcat(prog, "\n"); } else { sprintf(tmp_s, "constants[%d];", s_prg[i].op - num_total_variables); strcat(prog, tmp_s); strcat(prog, "\n"); } } } strcat(prog, "\n"); if (problem_type == PROBLEM_REGRESSION) sprintf(tmp_s, " outputs[0] = prg[%d];", num_utilized - 1); else sprintf(tmp_s, " if (prg[%d] <= %lf)\n outputs[0] = 0;\n else\n outputs[0] = 1;", num_utilized - 1, best_class_threshold); strcat(prog, tmp_s); } else { // not simplified strcat(prog, " double prg["); sprintf(tmp_s, "%ld", code_length); strcat(prog, tmp_s); strcat(prog, "];"); strcat(prog, "\n"); for (int i = 0; i < code_length; i++) { sprintf(tmp_s, " prg[%d] = ", i); strcat(prog, tmp_s); if (prg[i].op < 0) { print_instruction(prg[i].op, prg[i].adr1, prg[i].adr2, prg[i].adr3, prg[i].adr4, tmp_s); strcat(prog, tmp_s); strcat(prog, "\n"); } else { // a variable if (prg[i].op < num_total_variables) { sprintf(tmp_s, "x[%d];", prg[i].op); strcat(prog, tmp_s); strcat(prog, "\n"); } else { sprintf(tmp_s, "constants[%d];", prg[i].op - num_total_variables); strcat(prog, tmp_s); strcat(prog, "\n"); } } } strcat(prog, "\n"); if (problem_type == PROBLEM_REGRESSION) sprintf(tmp_s, " outputs[0] = prg[%d];", best); else { // wxLogDebug(wxString() << best << " " << best_class_threshold); sprintf(tmp_s, " if (prg[%d] <= %lg)\n outputs[0] = 0;\n else\n outputs[0] = 1;", best, best_class_threshold); // wxLogDebug(wxString(tmp_s)); } strcat(prog, tmp_s); } strcat(prog, "\n"); strcat(prog, "}\n"); strcat(prog, "\n"); strcat(prog, "int main(void)\n"); strcat(prog, "{\n"); strcat(prog, "\n"); strcat(prog, "//example of utilization ...\n"); strcat(prog, "\n"); sprintf(tmp_s, " double x[%d];\n", num_total_variables); strcat(prog, tmp_s); for (int i = 0; i < num_total_variables; i++) { sprintf(tmp_s, " x[%d] = %lf;\n", i, data[i]); strcat(prog, tmp_s); } strcat(prog, "\n"); strcat(prog, " double outputs[1];\n"); strcat(prog, "\n"); strcat(prog, " mepx(x, outputs);\n"); strcat(prog, " printf(\"%lf\", outputs[0]);\n"); strcat(prog, " getchar();\n"); strcat(prog, "}\n"); return prog; }
int exec(Memory entry_p) { Instruction insn; unsigned long clk = 0; uint32_t cmod; if(signal(SIGINT, signal_on_sigint) == SIG_ERR) { err(EXIT_FAILURE, "signal SIGINT"); } step_by_step = false; exec_finish = false; /* initialize internal variable */ memory_is_fault = 0; memory_io_writeback = 0; instruction_prefetch_flush(); /* setup system registers */ PSR = 0; cmod = (PSR & PSR_CMOD_MASK); PCR = entry_p; next_PCR = 0xffffffff; KSPR = (Memory)STACK_DEFAULT; #if !NO_DEBUG /* internal debug variable */ traceback_next = 0; FLAGR.flags = 0x80000000; prev_FLAGR.flags = 0x80000000; for(unsigned int i = 0; i < breakp_next; i++) { NOTICE("Break point[%d]: 0x%08x\n", i, breakp[i]); } #endif NOTICE("Execution Start: entry = 0x%08x\n", PCR); do { /* choose stack */ if(cmod != (PSR & PSR_CMOD_MASK)) { SPR = !cmod ? USPR : KSPR; } cmod = (PSR & PSR_CMOD_MASK); #if !NO_DEBUG /* break point check */ for(unsigned int i = 0; i < breakp_next; i++) { if(PCR == breakp[i]) { step_by_step = true; break; } } #endif /* instruction fetch */ insn.value = instruction_fetch(PCR); if(memory_is_fault) { /* fault fetch */ DEBUGINT("[FAULT] Instruction fetch: %08x\n", PCR); goto fault; } #if !NO_DEBUG if(DEBUG || step_by_step) { puts("---"); print_instruction(insn); } #endif /* execution */ insn_dispatch(insn); fault: if(memory_is_fault) { /* faulting memory access */ interrupt_dispatch_nonmask(memory_is_fault); next_PCR = PCR; memory_io_writeback = 0; memory_is_fault = 0; } else if(memory_io_writeback) { /* sync io */ io_store(memory_io_writeback); memory_io_writeback = 0; } /* writeback SP */ if(cmod) { USPR = SPR; } else { KSPR = SPR; } #if !NO_DEBUG if(step_by_step) { step_by_step_pause(); } else { if(DEBUG && DEBUG_REG) { print_registers(); } if(DEBUG_TRACE) { print_traceback(); } if(DEBUG_STACK) { print_stack(SPR); } if(DEBUG_DPS) { dps_info(); } } #endif if(!(clk & MONITOR_RECV_INTERVAL_MASK)) { if((PSR & PSR_IM_ENABLE) && IDT_ISENABLE(IDT_DPS_LS_NUM)) { dps_sci_recv(); } if(MONITOR) { monitor_method_recv(); monitor_send_queue(); } } /* next */ if(next_PCR != 0xffffffff) { #if !NO_DEBUG /* alignment check */ if(next_PCR & 0x3) { abort_sim(); errx(EXIT_FAILURE, "invalid branch addres. %08x", next_PCR); } #endif PCR = next_PCR; next_PCR = 0xffffffff; } else { PCR += 4; } /* interrupt check */ interrupt_dispatcher(); #if !NO_DEBUG /* for invalid flags checking */ prev_FLAGR.flags = FLAGR.flags; FLAGR._invalid |= 1; #endif /* next cycle */ clk++; } while(!(PCR == 0 && GR[31] == 0 && DEBUG_EXIT_B0) && !exec_finish); /* DEBUG_EXIT_B0: exit if b rret && rret == 0 */ NOTICE("---- Program Terminated ----\n"); print_instruction(insn); print_registers(); return 0; }
void format_machine_inst() { int next_inst_byte_meaning = one_byte_opcode; int i, tot_inst_len = 0; unsigned char inst_curr_byte; /* None of the flags below are currently used but may be in the near future so are left here */ boolean_t lock_prefix_seen; boolean_t rep_e_prefix_seen; boolean_t repne_prefix_seen; boolean_t operand_size_prefix_seen; boolean_t address_size_prefix_seen; /* Start Parsing the Instruction Buffer */ instidx = 0; prev_idx = 0; while(instidx < code_idx) { switch(next_inst_byte_meaning) { /* Can be a Prefix, or opcode !! */ case one_byte_opcode : inst_curr_byte = code_buf[instidx++]; instruction.opcode_mnemonic = (char *)mnemonic_list[inst_curr_byte]; switch(inst_curr_byte) { /* If Prefixes, set corresponding Flag and continue... */ case I386_INS_Two_Byte_Escape_Prefix : next_inst_byte_meaning = two_byte_opcode; break; case I386_INS_REX_PREFIX_None : case I386_INS_REX_PREFIX__B : case I386_INS_REX_PREFIX__X : case I386_INS_REX_PREFIX__X_B : case I386_INS_REX_PREFIX__R : case I386_INS_REX_PREFIX__R_B : case I386_INS_REX_PREFIX__R_X : case I386_INS_REX_PREFIX__R_X_B : case I386_INS_REX_PREFIX__W : case I386_INS_REX_PREFIX__W_B : case I386_INS_REX_PREFIX__W_X : case I386_INS_REX_PREFIX__W_X_B : case I386_INS_REX_PREFIX__W_R : case I386_INS_REX_PREFIX__W_R_B : case I386_INS_REX_PREFIX__W_R_X : case I386_INS_REX_PREFIX__W_R_X_B : rex_prefix.Base = (inst_curr_byte & 0x01); /* rex_prefix.Index = (inst_curr_byte & 0x02) ? 1 : 0; Not currently used */ rex_prefix.Reg = (inst_curr_byte & 0x04) ? 1 : 0; rex_prefix.Word64 = (inst_curr_byte & 0x08) ? 1 : 0; if (rex_prefix.Word64) { instruction.opcode_suffix = 'q'; instruction.reg_prefix = 'r'; } else { instruction.opcode_suffix = 'l'; instruction.reg_prefix = 'e'; } break; case I386_INS_LOCK_Prefix : lock_prefix_seen = TRUE; break; case I386_INS_REPNE_Prefix : repne_prefix_seen = TRUE; break; case I386_INS_REP_E_Prefix : rep_e_prefix_seen = TRUE; break; case I386_INS_Operand_Size_Prefix : operand_size_prefix_seen = TRUE; break; case I386_INS_Address_Size_Prefix : address_size_prefix_seen = TRUE; break; /* Now the instructions having Opcode Extension in the modrm.reg field.. */ case I386_INS_Grp1_Ev_Iv_Prefix : case I386_INS_Grp2_Ev_Iv_Prefix : instruction.destination_operand_class = memory_class; instruction.source_operand_class = immediate_class; instruction.has_immediate = double_word_immediate; instruction.num_operands = grp_prefix + 2; next_inst_byte_meaning = modrm_sib_bytes; break; case I386_INS_Grp1_Eb_Ib_Prefix : case I386_INS_Grp1_Ev_Ib_Prefix : case I386_INS_Grp2_Eb_Ib_Prefix : instruction.opcode_suffix = 'b'; instruction.destination_operand_class = memory_class; instruction.source_operand_class = immediate_class; instruction.has_immediate = one_byte_immediate; instruction.num_operands = grp_prefix + 2; next_inst_byte_meaning = modrm_sib_bytes; break; case I386_INS_Grp2_Eb_1_Prefix : case I386_INS_Grp2_Ev_1_Prefix : case I386_INS_Grp2_Eb_CL_Prefix : case I386_INS_Grp2_Ev_CL_Prefix : next_inst_byte_meaning = modrm_sib_bytes; assertpro(FALSE); /* Not taking care of this case for now - not used!! */ break; case I386_INS_Grp3_Eb_Prefix : modrm_byte.byte = code_buf[instidx + 1]; if (modrm_byte.modrm.reg_opcode < 2) { instruction.destination_operand_class = memory_class; instruction.source_operand_class = immediate_class; instruction.has_immediate = one_byte_immediate; instruction.num_operands = grp_prefix + 2; } else { instruction.source_operand_class = memory_class; instruction.opcode_suffix = 'b'; instruction.num_operands = grp_prefix + 1; } next_inst_byte_meaning = modrm_sib_bytes; break; case I386_INS_Grp3_Ev_Prefix : modrm_byte.byte = code_buf[instidx + 1]; if (modrm_byte.modrm.reg_opcode < 2) { instruction.destination_operand_class = memory_class; instruction.source_operand_class = immediate_class; instruction.has_immediate = double_word_immediate; instruction.num_operands = grp_prefix + 2; } else { instruction.source_operand_class = memory_class; instruction.num_operands = grp_prefix + 1; } next_inst_byte_meaning = modrm_sib_bytes; break; case I386_INS_Grp4_Prefix : case I386_INS_Grp5_Prefix : instruction.source_operand_class = memory_class; instruction.num_operands = grp_prefix + 1; next_inst_byte_meaning = modrm_sib_bytes; break; /* Now the instructions : Mainly those who have been used in the code generation in .c * files. */ /* Ins :: OPCODE */ case (I386_INS_PUSH_eAX + I386_REG_RAX) : case (I386_INS_PUSH_eAX + I386_REG_RCX) : case (I386_INS_PUSH_eAX + I386_REG_RDX) : case (I386_INS_PUSH_eAX + I386_REG_RBX) : case (I386_INS_PUSH_eAX + I386_REG_RSP) : case (I386_INS_PUSH_eAX + I386_REG_RBP) : case (I386_INS_PUSH_eAX + I386_REG_RSI) : case (I386_INS_PUSH_eAX + I386_REG_RDI) : instruction.opcode_suffix = ' '; instruction.reg_prefix = 'r'; instruction.num_operands = 1; instruction.source_operand_class = register_class; instruction.source_operand_reg = (char *) register_list[8 * rex_prefix.Base + inst_curr_byte - I386_INS_PUSH_eAX]; print_instruction(); break; case (I386_INS_POP_eAX + I386_REG_RAX) : case (I386_INS_POP_eAX + I386_REG_RCX) : case (I386_INS_POP_eAX + I386_REG_RDX) : case (I386_INS_POP_eAX + I386_REG_RBX) : case (I386_INS_POP_eAX + I386_REG_RSP) : case (I386_INS_POP_eAX + I386_REG_RBP) : case (I386_INS_POP_eAX + I386_REG_RSI) : case (I386_INS_POP_eAX + I386_REG_RDI) : instruction.opcode_suffix = ' '; instruction.reg_prefix = 'r'; instruction.num_operands = 1; instruction.source_operand_class = register_class; instruction.source_operand_reg = (char *) register_list[8 * rex_prefix.Base + inst_curr_byte - I386_INS_POP_eAX]; print_instruction(); break; case I386_INS_NOP__ : case I386_INS_MOVSB_Xb_Yb : print_instruction(); break; case I386_INS_MOVSW_D_Xv_Yv: assert(rex_prefix.Word64); /* Expect this to actually be MOVSQ */ instruction.opcode_mnemonic = "REP MOVS"; print_instruction(); break; /* Ins :: OPCODE disp8(%rip) */ case I386_INS_JZ_Jb : case I386_INS_JNL_Jb : case I386_INS_JNLE_Jb : case I386_INS_JLE_Jb : case I386_INS_JL_Jb : case I386_INS_JMP_Jb : case I386_INS_JNZ_Jb : instruction.opcode_suffix = ' '; instruction.reg_rip = TRUE; instruction.source_operand_class = memory_class; set_memory_reg(); instruction.num_operands = 1; next_inst_byte_meaning = one_byte_offset; break; /* Ins :: OPCODE disp32(%rip) */ case I386_INS_CALL_Jv : case I386_INS_JMP_Jv : instruction.opcode_suffix = ' '; instruction.reg_rip = TRUE; instruction.source_operand_class = memory_class; set_memory_reg(); instruction.num_operands = 1; next_inst_byte_meaning = double_word_offset; break; /* Ins :: OPCODE IMM8 */ case I386_INS_PUSH_Ib : instruction.opcode_suffix = 'b'; instruction.num_operands = 1; instruction.source_operand_class = immediate_class; next_inst_byte_meaning = one_byte_immediate; break; /* Ins :: OPCODE IMM32/64 */ case I386_INS_PUSH_Iv : instruction.opcode_suffix = 'l'; instruction.num_operands = 1; instruction.source_operand_class = immediate_class; if (rex_prefix.Word64 == 0) next_inst_byte_meaning = double_word_immediate; else next_inst_byte_meaning = quad_word_immediate; break; case I386_INS_CMP_eAX_Iv : instruction.num_operands = 2; instruction.destination_operand_class = register_class; instruction.destination_operand_reg = (char *)register_list[I386_REG_RAX]; instruction.source_operand_class = immediate_class; if (rex_prefix.Word64 == 0) next_inst_byte_meaning = double_word_immediate; else next_inst_byte_meaning = quad_word_immediate; break; case (I386_INS_MOV_eAX + I386_REG_RAX) : case (I386_INS_MOV_eAX + I386_REG_RCX) : case (I386_INS_MOV_eAX + I386_REG_RDX) : case (I386_INS_MOV_eAX + I386_REG_RBX) : case (I386_INS_MOV_eAX + I386_REG_RSP) : case (I386_INS_MOV_eAX + I386_REG_RBP) : case (I386_INS_MOV_eAX + I386_REG_RSI) : case (I386_INS_MOV_eAX + I386_REG_RDI) : instruction.num_operands = 2; instruction.destination_operand_class = register_class; instruction.destination_operand_reg = (char *) register_list[8 * rex_prefix.Base + inst_curr_byte - I386_INS_MOV_eAX]; instruction.source_operand_class = immediate_class; if (rex_prefix.Word64 == 0) next_inst_byte_meaning = double_word_immediate; else next_inst_byte_meaning = quad_word_immediate; break; /* Ins :: OPCODE ModRM (Reg, Mem)/(No_IMM) */ case I386_INS_LEA_Gv_M : case I386_INS_MOV_Gv_Ev : case I386_INS_CMP_Gv_Ev : case I386_INS_XOR_Gv_Ev : case I386_INS_MOVSXD_Gv_Ev : instruction.num_operands = 2; instruction.source_operand_class = memory_class; instruction.destination_operand_class = register_class; next_inst_byte_meaning = modrm_sib_bytes; break; /* Ins :: OPCODE ModRM (Mem, Reg)/(No_IMM) */ case I386_INS_MOV_Ev_Gv : instruction.num_operands = 2; instruction.source_operand_class = register_class; instruction.destination_operand_class = memory_class; next_inst_byte_meaning = modrm_sib_bytes; break; /* Ins :: OPCODE ModRM (Mem, IMM) */ case I386_INS_MOV_Ev_Iv : instruction.num_operands = 2; instruction.destination_operand_class = memory_class; instruction.has_immediate = double_word_immediate; instruction.source_operand_class = immediate_class; next_inst_byte_meaning = modrm_sib_bytes; break; default : assertpro(FALSE); } break; case two_byte_opcode : inst_curr_byte = code_buf[instidx++]; switch(inst_curr_byte) { case I386_INS_JO_Jv : case I386_INS_JNO_Jv : case I386_INS_JB_Jv : case I386_INS_JNB_Jv : case I386_INS_JZ_Jv : case I386_INS_JNZ_Jv : case I386_INS_JBE_Jv : case I386_INS_JNBE_Jv : case I386_INS_JS_Jv : case I386_INS_JNS_Jv : case I386_INS_JP_Jv : case I386_INS_JNP_Jv : case I386_INS_JL_Jv : case I386_INS_JNL_Jv : case I386_INS_JLE_Jv : case I386_INS_JNLE_Jv : instruction.reg_rip = TRUE; instruction.opcode_mnemonic = (char *) mnemonic_list_2b[inst_curr_byte]; instruction.source_operand_class = memory_class; set_memory_reg(); instruction.num_operands = 1; next_inst_byte_meaning = double_word_offset; break; default : assertpro(FALSE); } break; case modrm_sib_bytes : inst_curr_byte = code_buf[instidx++]; modrm_byte.byte = inst_curr_byte; if (instruction.num_operands >= grp_prefix) /* Means reg_opcode = op ext */ { switch((unsigned char) code_buf[instidx - 2]) { case I386_INS_Grp1_Eb_Ib_Prefix : case I386_INS_Grp1_Ev_Iv_Prefix : case I386_INS_Grp1_Ev_Ib_Prefix : instruction.opcode_mnemonic = (char *)mnemonic_list_g1[modrm_byte.modrm.reg_opcode]; break; case I386_INS_Grp2_Eb_Ib_Prefix : case I386_INS_Grp2_Ev_Iv_Prefix : case I386_INS_Grp2_Eb_1_Prefix : case I386_INS_Grp2_Ev_1_Prefix : case I386_INS_Grp2_Eb_CL_Prefix : case I386_INS_Grp2_Ev_CL_Prefix : instruction.opcode_mnemonic = (char *)mnemonic_list_g2[modrm_byte.modrm.reg_opcode]; break; case I386_INS_Grp3_Eb_Prefix : case I386_INS_Grp3_Ev_Prefix : instruction.opcode_mnemonic = (char *)mnemonic_list_g3[modrm_byte.modrm.reg_opcode]; break; case I386_INS_Grp4_Prefix : instruction.opcode_mnemonic = (char *)mnemonic_list_g4[modrm_byte.modrm.reg_opcode]; break; case I386_INS_Grp5_Prefix : instruction.opcode_suffix = ' '; instruction.opcode_mnemonic = (char *)mnemonic_list_g5[modrm_byte.modrm.reg_opcode]; break; } } else set_register_reg(); set_memory_reg(); /* Handle the SIB byte ! */ if ((modrm_byte.modrm.mod != I386_MOD32_REGISTER) && (modrm_byte.modrm.r_m == I386_REG_SIB_FOLLOWS)) { inst_curr_byte = code_buf[instidx++]; sib_byte.byte = inst_curr_byte; /* Assert that the SIB is not used for any complex addressing but is actually a dummy */ assert((sib_byte.sib.base == I386_REG_ESP) || (sib_byte.sib.base == I386_REG_EBP)); assert(sib_byte.sib.ss == I386_SS_TIMES_1); assert(sib_byte.sib.index == I386_REG_NO_INDEX); if (instruction.source_operand_class == memory_class) instruction.source_operand_reg = (char *)register_list[sib_byte.sib.base + 8 * rex_prefix.Base]; else if (instruction.destination_operand_class == memory_class) instruction.destination_operand_reg = (char *)register_list[sib_byte.sib.base + 8 * rex_prefix.Base]; switch(modrm_byte.modrm.mod) { case I386_MOD32_BASE : if (sib_byte.sib.base == I386_REG_disp32_NO_BASE) { clear_memory_reg(); next_inst_byte_meaning = double_word_offset; } else if (instruction.has_immediate) next_inst_byte_meaning = instruction.has_immediate; else { print_instruction(); next_inst_byte_meaning = one_byte_opcode; } break; case I386_MOD32_BASE_DISP_8 : next_inst_byte_meaning = one_byte_offset; break; case I386_MOD32_BASE_DISP_32 : next_inst_byte_meaning = double_word_offset; break; default : assertpro(FALSE); } } else /* No SIB */ { switch(modrm_byte.modrm.mod) { case I386_MOD32_BASE : if (modrm_byte.modrm.r_m == I386_REG_disp32_FROM_RIP) { instruction.reg_rip = TRUE; set_memory_reg(); next_inst_byte_meaning = double_word_offset; } else { instruction.offset = 0; if (instruction.has_immediate) next_inst_byte_meaning = instruction.has_immediate; else { print_instruction(); next_inst_byte_meaning = one_byte_opcode; } } break; case I386_MOD32_BASE_DISP_8 : next_inst_byte_meaning = one_byte_offset; break; case I386_MOD32_BASE_DISP_32 : next_inst_byte_meaning = double_word_offset; break; case I386_MOD32_REGISTER : if (instruction.source_operand_class == memory_class) instruction.source_operand_class = register_class; else if (instruction.destination_operand_class == memory_class) instruction.destination_operand_class = register_class; if (instruction.has_immediate) next_inst_byte_meaning = instruction.has_immediate; else { print_instruction(); next_inst_byte_meaning = one_byte_opcode; } break; default : assertpro(FALSE); } } break; case one_byte_immediate : instruction.immediate = code_buf[instidx++]; print_instruction(); next_inst_byte_meaning = one_byte_opcode; break; case double_word_immediate : instruction.immediate = *((int *)&code_buf[instidx]); instidx += 4; print_instruction(); next_inst_byte_meaning = one_byte_opcode; break; case quad_word_immediate : instruction.immediate = *((long *)&code_buf[instidx]); instidx += 8; print_instruction(); next_inst_byte_meaning = one_byte_opcode; break; case one_byte_offset : instruction.offset = code_buf[instidx++]; if (instruction.has_immediate) next_inst_byte_meaning = instruction.has_immediate; else { print_instruction(); next_inst_byte_meaning = one_byte_opcode; } break; case double_word_offset : instruction.offset = *((int *)&code_buf[instidx]); instidx += 4; if (instruction.has_immediate) next_inst_byte_meaning = instruction.has_immediate; else { print_instruction(); next_inst_byte_meaning = one_byte_opcode; } break; case quad_word_offset : instruction.offset = *((long *)&code_buf[instidx]); instidx += 8; if (instruction.has_immediate) next_inst_byte_meaning = instruction.has_immediate; else { print_instruction(); next_inst_byte_meaning = one_byte_opcode; } break; default : assertpro(FALSE); } } }
/*! * \param msg message à afficher * \param pmach machine en cours d'exécution * \param instr instruction en cours * \param addr adresse de l'instruction en cours d'exécution */ void trace(const char *msg, Machine *pmach, Instruction instr, unsigned addr) { printf("TRACE: %s: 0x%04x: ",msg, addr); print_instruction(instr, addr); printf("\n"); }
int main(int argc, char **argv) { char *output_file = NULL; char *entry_table_file = NULL; FILE *output = stdout; FILE *export_file; struct brw_program_instruction *entry, *entry1, *tmp_entry; int err, inst_offset; char o; void *mem_ctx; while ((o = getopt_long(argc, argv, "e:l:o:g:abW", longopts, NULL)) != -1) { switch (o) { case 'o': if (strcmp(optarg, "-") != 0) output_file = optarg; break; case 'g': { char *dec_ptr, *end_ptr; unsigned long decimal; gen_level = strtol(optarg, &dec_ptr, 10) * 10; if (*dec_ptr == '.') { decimal = strtoul(++dec_ptr, &end_ptr, 10); if (end_ptr != dec_ptr && *end_ptr == '\0') { if (decimal > 10) { fprintf(stderr, "Invalid Gen X decimal version\n"); exit(1); } gen_level += decimal; } } if (gen_level < 40 || gen_level > 90) { usage(); exit(1); } break; } case 'a': advanced_flag = 1; break; case 'b': binary_like_output = 1; break; case 'e': need_export = 1; if (strcmp(optarg, "-") != 0) export_filename = optarg; break; case 'l': if (strcmp(optarg, "-") != 0) entry_table_file = optarg; break; case 'W': warning_flags |= WARN_ALL; break; default: usage(); exit(1); } } argc -= optind; argv += optind; if (argc != 1) { usage(); exit(1); } if (strcmp(argv[0], "-") != 0) { input_filename = argv[0]; yyin = fopen(input_filename, "r"); if (yyin == NULL) { perror("Couldn't open input file"); exit(1); } } brw_init_context(&genasm_brw_context, gen_level); mem_ctx = ralloc_context(NULL); brw_init_compile(&genasm_brw_context, &genasm_compile, mem_ctx); err = yyparse(); if (strcmp(argv[0], "-")) fclose(yyin); yylex_destroy(); if (err || errors) exit (1); if (output_file) { output = fopen(output_file, "w"); if (output == NULL) { perror("Couldn't open output file"); exit(1); } } if (read_entry_file(entry_table_file)) { fprintf(stderr, "Read entry file error\n"); exit(1); } inst_offset = 0 ; for (entry = compiled_program.first; entry != NULL; entry = entry->next) { entry->inst_offset = inst_offset; entry1 = entry->next; if (entry1 && is_label(entry1) && is_entry_point(entry1)) { // insert NOP instructions until (inst_offset+1) % 4 == 0 while (((inst_offset+1) % 4) != 0) { tmp_entry = calloc(sizeof(*tmp_entry), 1); tmp_entry->insn.gen.header.opcode = BRW_OPCODE_NOP; entry->next = tmp_entry; tmp_entry->next = entry1; entry = tmp_entry; tmp_entry->inst_offset = ++inst_offset; } } if (!is_label(entry)) inst_offset++; } for (entry = compiled_program.first; entry; entry = entry->next) if (is_label(entry)) add_label(entry); if (need_export) { if (export_filename) { export_file = fopen(export_filename, "w"); } else { export_file = fopen("export.inc", "w"); } for (entry = compiled_program.first; entry != NULL; entry = entry->next) { if (is_label(entry)) fprintf(export_file, "#define %s_IP %d\n", label_name(entry), (IS_GENx(5) ? 2 : 1)*(entry->inst_offset)); } fclose(export_file); } for (entry = compiled_program.first; entry; entry = entry->next) { struct relocation *reloc = &entry->reloc; if (!is_relocatable(entry)) continue; if (reloc->first_reloc_target) reloc->first_reloc_offset = label_to_addr(reloc->first_reloc_target, entry->inst_offset) - entry->inst_offset; if (reloc->second_reloc_target) reloc->second_reloc_offset = label_to_addr(reloc->second_reloc_target, entry->inst_offset) - entry->inst_offset; if (reloc->second_reloc_offset) { // this is a branch instruction with two offset arguments set_branch_two_offsets(entry, reloc->first_reloc_offset, reloc->second_reloc_offset); } else if (reloc->first_reloc_offset) { set_branch_one_offset(entry, reloc->first_reloc_offset); } } if (binary_like_output) fprintf(output, "%s", binary_prepend); for (entry = compiled_program.first; entry != NULL; entry = entry1) { entry1 = entry->next; if (!is_label(entry)) print_instruction(output, &entry->insn.gen); else free(entry->insn.label.name); free(entry); } if (binary_like_output) fprintf(output, "};"); free_entry_point_table(entry_point_table); free_hash_table(declared_register_table); free_label_table(label_table); fflush (output); if (ferror (output)) { perror ("Could not flush output file"); if (output_file) unlink (output_file); err = 1; } return err; }