riscv-collab / riscv-openocd

Fork of OpenOCD that has RISC-V support
Other
446 stars 324 forks source link

Code improvement: Refactor functions that select implentation based on debug spec version #765

Open JanMatCodasip opened 1 year ago

JanMatCodasip commented 1 year ago

Note to self: In functions like resume_go(), consider the use of the following:

    // Current code (excerpt from resume_go()):
    if (!r->get_hart_state) {
        struct target_type *tt = get_target_type(target);
        result = tt->resume(target, current, address, handle_breakpoints,
                debug_execution);
    } else {
        result = riscv_resume_go_all_harts(target);
    }

    // ------------------------------------------------------------------------

    // Proposed code after re-factor:
    if (r->version == DM_VERSION_0_11) {  /* pseudo-code */
        result = riscv011_resume(target, /* ... */ );  
            /* Use a direct call, drop all function pointers from struct target_type.
             * Benefits: 
             * - Easier to read code, 
             * - easier to navigate, 
             * - possibility for static analysis tools to do better analysis when they know 
             *   what function gets called in compile time already. */

    } else if (r->version == DM_VERSION_0_13 || r->version == DM_VERSION_1_0) { /* pseudo-code */
        result = riscv013_resume_go_all_harts(target); 
    }
    else {
        assert(0);
    }   
timsifive commented 1 year ago

All the logic to do with choosing 0.11 and 0.13 is a mess, and really could be improved.