Risc-v emulator format output function implementation method, device and equipment, and storage medium

By employing a jump table-based dynamic translation method in the RISC-V emulator, the problem of inaccurate formatted output of the printf function was solved, realizing an accurate formatted output function in the RISC-V emulator, thus improving development efficiency and the accuracy of program verification.

CN120066474BActive Publication Date: 2026-03-24WUHAN UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-01-20
Publication Date
2026-03-24

AI Technical Summary

Technical Problem

Existing RISC-V emulators cannot accurately handle different types of output, such as floating-point numbers, integers, and strings, as well as different placeholders, when implementing the printf function, resulting in inaccurate formatted output.

Method used

The RISC-V source code is translated into IR-format code files using a dynamic translation method based on jump tables. The first and second jump tables are used to process the strings corresponding to the printf function and the %s placeholder, respectively, to generate the second jump table to ensure accuracy. The translated code is then executed by a just-in-time compiler.

Benefits of technology

It implements accurate formatted output functions in the RISC-V emulator, improving development efficiency and the accuracy of program verification, reducing hardware dependence, and promoting cross-platform development.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120066474B_ABST
    Figure CN120066474B_ABST
Patent Text Reader

Abstract

A RISC-V emulator format output function implementation method, device and equipment and storage medium are disclosed, and belong to the technical field of emulators. The method comprises the following steps: obtaining a RISC-V source code to be simulated, inputting the RISC-V source code to be simulated into a compiler, and obtaining a first code file translated by the compiler; in the case that a format string of a first printf function includes a string placeholder %s, translating the first code file into a second code file comprises translating the first printf function into an IR form based on a first jump table and a second jump table corresponding to %s in the format string of the first printf function; after the first code file is translated into the second code file, the second code file is executed by using a just-in-time compiler. The method can accurately and effectively implement the printf function in the RISC-V emulator.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This disclosure relates to the field of simulator technology, and in particular to a method, apparatus, device and storage medium for implementing formatted output functions of a RISC-V simulator. Background Technology

[0002] The formatted output function, also known as the printf function, is a function with variable-length arguments and needs to support complex data formatting. For example, when implementing the printf function in a RISC-V emulator, it is necessary to accurately handle different types of output, such as floating-point numbers, integers, and strings, as well as different placeholders (such as %d, %c, %s, etc.).

[0003] In related technologies, RISC-V simulators typically translate RISC-V code directly into a code format that can run on the target architecture. For the printf function, a simple translation cannot meet the format requirements of the printf function, which may lead to problems in implementing the printf function in a RISC-V simulator. Summary of the Invention

[0004] This disclosure provides a method, apparatus, device, and storage medium for implementing a formatted output function in a RISC-V emulator, which can accurately and effectively implement the printf function in a RISC-V emulator. The technical solution includes at least the following:

[0005] In a first aspect, a method for implementing formatted output functions in a RISC-V emulator is provided, comprising: obtaining RISC-V source code to be simulated, and inputting the RISC-V source code to be simulated into a compiler to obtain a first code file translated by the compiler, wherein the first code file is a binary file implemented in C language; translating the first code file into a second code file, wherein the second code file is a code file in intermediate representation (IR) form, wherein translating the first code file into the second code file includes translating multiple printf functions in the first code file into IR form based on a first jump table, wherein, if the format string of the first printf function includes a string placeholder %s, translating the first code file into the second code file includes translating the first printf function into IR form based on the first jump table and a second jump table corresponding to %s in the format string of the first printf function; and executing the second code file using a just-in-time (JIT) compiler after the first code file is translated into the second code file.

[0006] Optionally, translating the first printf function into IR form based on the first jump table and the second jump table includes: obtaining multiple strings from the rodata segment data of the first code file and saving the multiple strings and the address corresponding to each string to a mapping table; translating each string in the mapping table into IR form; obtaining the format string of the first printf function during the process of translating each string in the mapping table into IR form; determining the first address corresponding to the format string of the first printf function based on the value of the parameter register x10 and the first jump table, wherein the first jump table is used to list the address corresponding to each string in the first code file; generating a second jump table based on %s in the format string of the first printf function, wherein the second jump table is called by %s in the format string of the first printf function and is used to list the address corresponding to each string in the first code file; determining the second address corresponding to %s in the format string of the first printf function based on the value of the parameter register corresponding to %s in the format string of the first printf function and the second jump table; and translating the first printf function into IR form based on the first address and the second address.

[0007] Optionally, the method further includes: determining the return address of the first printf function based on a global variable of return value and a third jump table, wherein the third jump table is used to list the return address of each printf function in the first code file.

[0008] Optionally, the method further includes: if the format string of the first printf function further includes at least one first placeholder, obtaining the value of the parameter register corresponding to each first placeholder, wherein the first placeholder is any placeholder other than %s; and translating the first printf function into IR form based on the first address, the second address, and the value of the parameter register corresponding to each first placeholder.

[0009] Secondly, a RISC-V emulator formatted output function implementation device is also provided, comprising: an acquisition module, used to acquire the RISC-V source code to be simulated and input the RISC-V source code to be simulated into a compiler to obtain a first code file translated by the compiler, wherein the first code file is a binary file implemented in C language; a translation module, used to translate the first code file into a second code file, wherein the second code file is a code file in intermediate representation (IR) form, wherein translating the first code file into the second code file includes translating multiple printf functions in the first code file into IR form based on a first jump table, wherein, if the format string of the first printf function includes a string placeholder %s, translating the first code file into the second code file includes translating the first printf function into IR form based on the first jump table and a second jump table corresponding to %s in the format string of the first printf function; and an execution module, used to execute the second code file using a just-in-time (JIT) compiler after the first code file is translated into the second code file.

[0010] Optionally, the translation module is further configured to: acquire multiple strings from the rodata segment data of the first code file; save the multiple strings and the address corresponding to each string to a mapping table; translate each string in the mapping table into IR form; during the process of translating each string in the mapping table into IR form, acquire the format string of the first printf function; determine the first address corresponding to the format string of the first printf function based on the value of the parameter register x10 and the first jump table, wherein the first jump table is used to list the address corresponding to each string in the first code file; generate a second jump table based on %s in the format string of the first printf function, wherein the second jump table is called by %s in the format string of the first printf function, wherein the second jump table is used to list the address corresponding to each string in the first code file; determine the second address corresponding to %s in the format string of the first printf function based on the value of the parameter register corresponding to %s in the format string of the first printf function and the second jump table; and translate the first printf function into IR form based on the first address and the second address.

[0011] Optionally, the translation module is further configured to determine the return address of the first printf function based on the return value global variable and the third jump table, wherein the third jump table is used to list the return address of each printf function in the first code file.

[0012] Optionally, the translation module is further configured to, when the format string of the first printf function includes at least one first placeholder, obtain the value of the parameter register corresponding to each first placeholder, wherein the first placeholder is any placeholder other than %s; and translate the first printf function into IR form based on the first address, the second address, and the value of the parameter register corresponding to each first placeholder.

[0013] Thirdly, a computer device is also provided, comprising: a memory and a processor, wherein the memory stores at least one computer program, the at least one computer program being loaded and executed by the processor to perform the RISC-V emulator formatted output function implementation method described in the above embodiments.

[0014] Fourthly, a computer-readable storage medium is also provided, wherein at least one computer program is stored in the computer-readable storage medium, the at least one computer program being loaded and executed by a processor to perform the RISC-V emulator formatted output function implementation method described in the above embodiments.

[0015] Fifthly, a computer program product is provided, including a computer program / instructions that, when executed by a processor, implement the method described in the first aspect.

[0016] The beneficial effects of the technical solutions provided in this disclosure include at least the following:

[0017] In this embodiment, by using a first jump table, multiple printf functions in the first code file can be accurately translated into IR form. This is equivalent to a jump table-based translation rather than a static translation, which is a dynamic translation and effectively ensures the accuracy of the translated printf functions. For the string placeholder %s, since its corresponding parameter is a string, and the specific string cannot be determined before execution, this embodiment uses a first jump table and a second jump table corresponding to %s in the format string of the first printf function to translate the first printf function with %s into IR form. This is equivalent to a translation based on two jump tables (the first jump table is used to translate the entire printf function, and the second jump table is used to translate the string corresponding to %s), thus effectively ensuring the accuracy of the final translated printf function. Attached Figure Description

[0018] To more clearly illustrate the technical solutions in the embodiments of this disclosure, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this disclosure. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0019] Figure 1 A flowchart illustrating an exemplary embodiment of the present disclosure of a method for implementing a RISC-V emulator formatted output function is shown.

[0020] Figure 2 A flowchart illustrating another exemplary embodiment of the present disclosure shows a method for implementing a RISC-V emulator formatted output function;

[0021] Figure 3 This illustration shows a schematic diagram of the structure of a RISC-V emulator formatted output function implementation apparatus provided in an exemplary embodiment of the present disclosure;

[0022] Figure 4 This is a schematic diagram of the structure of a computer device provided in an embodiment of this disclosure. Detailed Implementation

[0023] Unless otherwise defined, the technical or scientific terms used herein shall have the ordinary meaning as understood by one of ordinary skill in the art to which this disclosure pertains. The terms “first,” “second,” “third,” and similar terms used in this patent application specification and claims do not indicate any order, quantity, or importance, but are merely used to distinguish different components. Similarly, the terms “an” or “a” and similar terms do not indicate a quantity limitation, but rather indicate the presence of at least one. The terms “comprising” or “including” and similar terms mean that the elements or objects preceding “comprising” or “including” encompass the elements or objects listed following “comprising” or “including” and their equivalents, but do not exclude other elements or objects.

[0024] To make the objectives, technical solutions, and advantages of this disclosure clearer, the embodiments of this disclosure will be described in further detail below with reference to the accompanying drawings.

[0025] In this embodiment of the disclosure, the RISC-V emulator runs on an x86-64 host, that is, the RISC-V emulator is used to implement the execution of RISC-V code on an x86-64 host.

[0026] Figure 1 A flowchart illustrating an exemplary embodiment of this disclosure provides a method for implementing a formatted output function of a RISC-V emulator, which can be executed by a computer device. See also... Figure 1The method includes:

[0027] In step 101, the RISC-V source code to be simulated is obtained and input into the compiler to obtain the first code file after translation by the compiler.

[0028] The first code file is a binary file implemented in C language.

[0029] Here, the RISC-V source code to be simulated can be input by the user into the simulator. After the user inputs the RISC-V source code to be simulated into the simulator, the simulator will first input the RISC-V source code to be simulated into the compiler to obtain the C language binary code file translated by the compiler.

[0030] In step 102, the first code file is translated into the second code file.

[0031] The second code file is an intermediate representation (IR) file. IR is a key component of the LLVM (Low Level Virtual Machine) project. LLVM IR exists in three forms: assembly language form, binary file form, and memory representation form. In this embodiment, the second code file is an assembly language IR file.

[0032] Translating the first code file into the second code file includes: translating multiple printf functions in the first code file into IR form based on the first jump table; and when the format string of the first printf function includes the string placeholder %s, translating the first code file into the second code file includes: translating the first printf function into IR form based on the first jump table and the second jump table corresponding to %s in the format string of the first printf function.

[0033] The first printf function is one of multiple printf functions.

[0034] The printf function typically consists of two parts: a format string and output arguments. The format string is a string, and the output arguments correspond one-to-one with the placeholders in the format string. When the format string contains k placeholders, there are corresponding k output arguments.

[0035] The printf function is typically formatted as: printf("format string", output parameter).

[0036] During the translation of multiple printf functions in the first code file into IR form, it is uncertain which string the format string corresponds to, and which data the placeholder in the format string corresponds to, before the second code file is executed. Directly translating the printf functions statically into IR form would result in execution errors.

[0037] In step 103, after the first code file is translated into the second code file, the second code file is executed using a just-in-time compiler.

[0038] In this embodiment, by using a first jump table, multiple printf functions in the first code file can be accurately translated into IR form. This is equivalent to a jump table-based translation rather than a static translation, which is a dynamic translation and effectively ensures the accuracy of the translated printf functions. For the string placeholder %s, since its corresponding parameter is a string, and the specific string cannot be determined before execution, this embodiment uses a first jump table and a second jump table corresponding to %s in the format string of the first printf function to translate the first printf function with %s into IR form. This is equivalent to a translation based on two jump tables (the first jump table is used to translate the entire printf function, and the second jump table is used to translate the string corresponding to %s), thus effectively ensuring the accuracy of the final translated printf function.

[0039] Figure 2 A flowchart illustrating a method for implementing a RISC-V emulator formatted output function according to another exemplary embodiment of this disclosure is shown, the method being executable by a computer device. See also Figure 2 The method includes:

[0040] In step 201, the RISC-V source code to be simulated is obtained and input into the compiler to obtain the first code file translated by the compiler.

[0041] The first code file is a binary file implemented in C language.

[0042] The details of step 201 are the same as those in step 101 above, and will not be described in detail here.

[0043] In step 202, the first code file is translated into the second code file.

[0044] The second code file is an intermediate code file in IR format.

[0045] Translating the first code file into the second code file includes: translating multiple printf functions in the first code file into IR form based on the first jump table; and when the format string of the first printf function includes the string placeholder %s, translating the first code file into the second code file includes: translating the first printf function into IR form based on the first jump table and the second jump table corresponding to %s in the format string of the first printf function.

[0046] The first printf function is one of multiple printf functions.

[0047] Optionally, based on the first jump table and the second jump table corresponding to %s in the format string of the first printf function, the first printf function is translated into IR form, including the following steps ae.

[0048] Step a: Obtain multiple strings from the rodata segment data of the first code file, and save the multiple strings and the address corresponding to each string to the mapping table.

[0049] After the compiler translates the RISC-V source code to be simulated into the first code file, the rodata segment in the first code file corresponds to the data segment in the RISC-V source code. The data segment contains multiple strings, which include both format strings from the printf function and regular strings from the RISC-V source code (that is, strings that are not format strings).

[0050] When saving strings and their corresponding addresses to the mapping table, only valid characters are stored. RISC-V strings are stored and processed according to ASCII encoding. Generally, valid characters fall into two categories: First, the character is less than or equal to 126 and greater than or equal to 33. Second, the character is a space, tab, forward slash, quotation mark, etc. If a character satisfies either of these two conditions, it is considered a valid character.

[0051] In a RISC-V environment, strings are usually terminated by a null character ('\0'). When a character in a string is stored up to the null character, it means that the string has been stored.

[0052] Step b involves translating each string in the mapping table into IR form.

[0053] Since the mapping table includes both the format string from the printf function and the regular strings from the RISC-V source code, we can first translate all the characters in the mapping table into IR form, and then perform additional processing on the format string in the printf function through step cg, thereby translating each character in the mapping table into IR form.

[0054] When translating a string in a mapping table into IR form, the string needs to be saved as a global variable. For example, the global variable name can be in the form of @.str + address. For instance, if the string "keypair" is stored at address 0x00008280 (hexadecimal, equivalent to decimal 33408), then the global variable corresponding to this string is @.str33408. The process of declaring this string as a global variable can be represented as:

[0055] @.str33408 = private unnamed_addr constant [8 x i8] c"keypair\00". The character set is 8 bits (7 characters in the string plus a null byte at the end), hence 8 x i8. Each string ends with '\0', which must be represented by the ASCII code \00. The rest is the fixed content for declaring the string.

[0056] Step c: In the process of translating each string in the mapping table into IR form, obtain the format string of the first printf function.

[0057] The format string for the first printf function includes %s.

[0058] For example, the format string can be determined using regular expression matching. For instance, regular expression matching can be used to match placeholders such as %d and %s; the string containing these placeholders is usually the format string. Each printf function has only one format string, therefore the format string for the first printf function can be any string matched by regular expression matching.

[0059] Step d: Based on the value of parameter register x10 and the first jump table, determine the first address corresponding to the format string of the first printf function.

[0060] The first jump table lists the addresses corresponding to each string in the first code file. This first jump table can be shared for any given format string.

[0061] In a static scenario, the data corresponding to the format string of each printf function is uncertain. However, in a dynamic scenario (i.e., during code execution), for RISC-V code, the data corresponding to the format string of the printf function is determined in real time by the value in the parameter register x10. Based on the real-time value in the parameter register x10, it is possible to determine which string in the first jump table the current printf function's format string should jump to.

[0062] Therefore, in this embodiment of the disclosure, based on the value of parameter register x10 and the first jump table, it is possible to determine which string the format string of the first printf function needs to jump to, and then the first address where the string is located can be determined (for example, the first address where the string is located can be determined by a mapping table).

[0063] Step e generates a second jump table based on %s in the format string of the first printf function.

[0064] The second jump table is called by the %s format string in the first printf function. The second jump table is used to list the address corresponding to each string in the first code file.

[0065] For the %s in the format string, the corresponding output parameter is a string rather than a single character or integer, so an additional jump table needs to be generated. Furthermore, since the way each %s calls the string is different from the way the format string calls the string, the jump table corresponding to %s cannot directly use the first jump table corresponding to the format string. Instead, a new jump table, namely the second jump table, needs to be generated.

[0066] Optionally, if the format string contains placeholders other than %s, step e further includes: if the format string of the first printf function also contains at least one first placeholder, obtaining the value of the parameter register corresponding to each first placeholder; the first placeholder is any placeholder other than %s.

[0067] Accordingly, in this case, step g becomes: based on the first address, the second address, and the value of the parameter register corresponding to each first placeholder, translate the first printf function into IR form.

[0068] When a first placeholder exists in the format string, the value in the parameter register corresponding to that placeholder can be directly passed to the first placeholder during translation.

[0069] Step f: Based on the value of the parameter register corresponding to %s in the format string of the first printf function and the second jump table, determine the second address corresponding to %s in the format string of the first printf function.

[0070] For RISC-V code, the parameter registers corresponding to placeholders are x11-x17, with x12 being the placeholder for %s. If a format string contains more than 7 placeholders, the excess parameters will be passed through the stack.

[0071] Therefore, the value of %s in the format string of the first printf function is also the value of parameter register x12. Substituting the value of parameter register x12 into the second jump table will determine the string corresponding to %s in the format string of the first printf function.

[0072] Step g: Based on the first address and the second address, translate the first printf function into IR form.

[0073] It should be noted that the value of the parameter register in step ag above is determined in real time during the execution of the second code file. Similarly, the first and second addresses determined based on the values ​​of the two parameter registers (x10 and x12) are also determined in real time during the execution of the second code file. During static translation, only the jump table generation is actually performed; the actual first and second addresses are determined only during the dynamic execution of the second code file. Once both the first and second addresses are determined, the first printf function can be translated accurately. During the dynamic execution of the second code file, if a jump instruction is encountered with the jump label "printf," it indicates that the formatted output function has been executed. Since the corresponding jump table has been generated during static translation, steps d and f can then be executed to determine the first and second addresses, thus enabling the accurate execution of the printf function.

[0074] For other printf functions besides the first printf function, the above steps can also be used to accurately translate them into IR form.

[0075] The following example illustrates the first and second jump tables. In actual code, the first and second jump tables are treated as a whole, with the second jump table nested within the first jump table. You can jump to the second jump table through a string in the first jump table. Furthermore, if a format string contains multiple %s, then that format string will correspond to multiple second jump tables (that is, one %s corresponds to one jump table).

[0076] For example, a formatted string, after being translated into IR form in the mapping table (that is, after being saved as a global variable), has the name @.str33441= private unnamed_addr constant [17 x i8] c"[#sn(%u)],\20[%s]\0A\00". This string has 17 characters, each character being 8 bits, hence the name 17 x i8.

[0077] The following is a portion of the jump table.

[0078] callPrintfTable:

[0079] %printfIndex0 = load i32, ptr @printfIndex0

[0080] switch i32 %printfIndex0, label %default [

[0081] i32 33144, label %printf_call_33144

[0082]

[0083] i32 33441, label %printf_call_33441

[0084]

[0085] / / This part is the content corresponding to the first jump table.

[0086] printf_call_33441:

[0087] %printfIndex2_33441 = load i32, ptr @printfIndex2

[0088] switch i32 %printfIndex2_33441, label %default [

[0089] i32 33144, label %printf_call_a33441_a33144

[0090] i32 33168, label %printf_call_a33441_a33168 ...

[0091] / / This part is the content corresponding to the second jump table.

[0092] In static translation, only a first and second jump table are needed. In dynamic execution, the value of the parameter register corresponding to the format string (x10) needs to be obtained first. Assuming the value in parameter register x10 determines the jump to the string `printf_call_33441` in the first jump table, then the value of the parameter register corresponding to `%s` (x12) can be read. Assuming the value in parameter register x12 determines the jump to the string `printf_call_a33441_a33144` in the second jump table, the format string is now basically translated. Only the value corresponding to the placeholder `%u` remains undetermined. The value of the parameter register corresponding to `%u` (x11) needs to be obtained and passed to the position of `%u`.

[0093] Here, printfIndex is a variable used to store the parameters in the parameter register. printfIndex0 represents the value of the parameter in parameter register x10, and printfIndex2 represents the value of the parameter in parameter register x12.

[0094] Optionally, the method further includes: determining the return address of the first printf function based on the return value global variable and the third jump table, wherein the third jump table is used to list the return address of each printf function in the first code file.

[0095] Here, each `printf` function needs to determine its return address after a jump (the return address is the address of the next instruction after the `printf` function) to continue execution of the second code file. When determining the return address, the value of the current return value global variable determines which address in the third jump table to jump to; this determined address in the third jump table is the return address of the first `printf` function. The return value global variable also needs to be determined in real-time during dynamic code execution; in static translation, only the process of generating the third jump table exists.

[0096] For any printf function, the first and third jump tables are shared; that is, only one first and third jump table needs to be generated, and subsequent calls can proceed directly. However, the second jump table needs to be generated separately during the translation process because the calling method of %s is different.

[0097] In some embodiments, the return address may be stored as a label.

[0098] In RISC-V systems, jumping into the `printf` function typically requires the `jal` jump instruction, and jumping from the `printf` function to the return address requires the `jalr` jump instruction. In this embodiment, since the first code file is translated into a second code file in IR form, the `jalr` jump instruction is unnecessary; the determined return address can be used directly.

[0099] For example, the first printf function needs to jump to the string printf_call_a33441 in the first jump table, then jump to the string printf_call_a33441_a33144 in the second jump table, and then determine the return address and jump to the return address. In this case, the following is a partial example code for determining the return address.

[0100] printf_call_a33441_a33144:

[0101] %retx1_a33441_a33144 = load i32, ptr @printfIndex8

[0102] switch i32 %retx1_a33441_a33144, label %default [

[0103] i32 20534, label %printf_call_a33441_a33144_a20534

[0104] i32 20590, label %printf_call_a33441_a33144_a20590

[0105] i32 20646, label %printf_call_a33441_a33144_a20646

[0106] i32 20718, label %printf_call_a33441_a33144_a20718

[0107]

[0108] / / This part is the content corresponding to the third jump table.

[0109] printf_call_a33441_a33144_a20534:

[0110] %x11_1323 = load i32, ptr @x11

[0111] call i32 (ptr, ...) @printf(ptr @.str33441, i32 %x11_1323, ptr @.str33144)

[0112] br label %_L292

[0113] During dynamic execution, after the first printf function is translated, it is necessary to obtain the value of the current return value global variable. Assuming that based on the value of the current return value global variable, it can be determined that the jump will be to the string printf_call_a33441_a33144_a20534 in the third jump table, which is the return address 20534 (0x00005036). This return address can exist in the code in the form of a label, with the label _L292.

[0114] For the code in the first code file other than the printf function, it can be translated into IR form using methods from related technologies, ultimately resulting in the translated second code file.

[0115] In step 203, after the first code file is translated into the second code file, the second code file is executed using a just-in-time compiler.

[0116] In implementation, the lli tool in LLVM can be used, which can execute a second code file using a Just-In-Time (JIT) compiler.

[0117] By employing a just-in-time (JIT) compiler to execute the second code file, it becomes possible to simulate RISC-V architecture program behavior in the x86 architecture without actual hardware, and without needing to translate the second code file into an x86 binary file before execution, thus improving the efficiency of the RISC-V emulator. Accurately implementing `printf` within the RISC-V emulator significantly enhances development efficiency, helps developers verify and debug programs, reduces hardware dependencies, and promotes cross-platform development.

[0118] The following are device embodiments of this application. For details not described in detail in the device embodiments, please refer to the above method embodiments.

[0119] Figure 3 A schematic diagram of a RISC-V emulator formatted output function implementation apparatus provided in an exemplary embodiment of this disclosure is shown. See also: Figure 3 The RISC-V emulator formatted output function implementation device 300 includes: an acquisition module 301, a translation module 302, and an execution module 303.

[0120] The acquisition module 301 is used to acquire the RISC-V source code to be simulated and input the RISC-V source code to be simulated into the compiler to obtain the first code file after translation by the compiler. The first code file is a binary file implemented in C language.

[0121] Translation module 302 is used to translate a first code file into a second code file, the second code file being a code file in intermediate representation (IR) form. Translating the first code file into the second code file includes translating multiple printf functions in the first code file into IR form based on a first jump table. If the format string of the first printf function includes the string placeholder %s, translating the first code file into the second code file includes translating the first printf function into IR form based on the first jump table and a second jump table corresponding to %s in the format string of the first printf function.

[0122] The execution module 303 is used to execute the second code file using a just-in-time compiler after the first code file has been translated into a second code file.

[0123] Optionally, the translation module 302 is further configured to: obtain multiple strings from the rodata segment data of the first code file; save the multiple strings and the address corresponding to each string to a mapping table; translate each string in the mapping table into IR form; during the process of translating each string in the mapping table into IR form, obtain the format string of the first printf function; determine the first address corresponding to the format string of the first printf function based on the value of the parameter register x10 and the first jump table, the first jump table being used to list the addresses corresponding to each string in the first code file; generate a second jump table based on %s in the format string of the first printf function, the second jump table being called by %s in the format string of the first printf function, the second jump table being used to list the addresses corresponding to each string in the first code file; determine the second address corresponding to %s in the format string of the first printf function based on the value of the parameter register corresponding to %s in the format string of the first printf function and the second jump table; and translate the first printf function into IR form based on the first address and the second address.

[0124] Optionally, the translation module 302 is also used to determine the return address of the first printf function based on the return value global variable and the third jump table, the third jump table being used to list the return address of each printf function in the first code file.

[0125] Optionally, the translation module 302 is further configured to, if the format string of the first printf function also includes at least one first placeholder, obtain the value of the parameter register corresponding to each first placeholder, wherein the first placeholder is any placeholder other than %s; and translate the first printf function into IR form based on the first address, the second address, and the value of the parameter register corresponding to each first placeholder.

[0126] It should be noted that the RISC-V emulator formatted output function implementation device provided in the above embodiments is only illustrated by the division of the above functional modules. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. Furthermore, the RISC-V emulator formatted output function implementation device and the RISC-V emulator formatted output function implementation method embodiments belong to the same concept, and the specific implementation process is detailed in the method embodiments, which will not be repeated here.

[0127] The module division in this embodiment is illustrative and represents only one logical functional division. In actual implementation, other division methods are possible. Furthermore, the functional modules in the various embodiments of this disclosure can be integrated into a single processor, exist as separate physical entities, or be integrated into a single module. The integrated modules described above can be implemented in hardware or as software functional modules.

[0128] If the integrated module is implemented as a software functional module and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this disclosure, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a terminal device (which may be a personal computer, mobile phone, or communication device, etc.) or processor to execute all or part of the steps of the methods of the various embodiments of this disclosure. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0129] Figure 4 This is a schematic diagram of the structure of a computer device provided in an embodiment of this disclosure. For example... Figure 4As shown, the computer device 400 includes a processor 401 and a memory 402.

[0130] Processor 401 may include one or more processing cores, such as a quad-core processor, an octa-core processor, etc. Processor 401 may be implemented using at least one hardware form selected from DSP (Digital Signal Processing), FPGA (Field-Programmable Gate Array), and PLA (Programmable Logic Array). Processor 401 may also include a main processor and a coprocessor. The main processor, also known as a CPU (Central Processing Unit), is used to process data in the wake-up state; the coprocessor is a low-power processor used to process data in the standby state. In some embodiments, processor 401 may integrate a GPU (Graphics Processing Unit), which is responsible for rendering and drawing the content to be displayed on the screen. In some embodiments, processor 401 may also include an AI (Artificial Intelligence) processor, which is used to handle computational operations related to machine learning.

[0131] Memory 402 may include one or more computer-readable storage media, which may be non-transitory. Memory 402 may also include high-speed random access memory and non-volatile memory, such as one or more disk storage devices or flash memory devices. In some embodiments, the non-transitory computer-readable storage media in memory 402 is used to store at least one instruction, which is executed by processor 401 to implement the RISC-V emulator formatted output function implementation method provided in the embodiments of this disclosure.

[0132] Those skilled in the art will understand that Figure 4 The structure shown does not constitute a limitation on computer device 400, and may include more or fewer components than shown, or combine certain components, or use different component arrangements.

[0133] This disclosure also provides a non-transitory computer-readable storage medium, which, when the instructions in the storage medium are executed by the processor of a computer device, enables the computer device to execute the RISC-V emulator formatted output function implementation method provided in this disclosure.

[0134] This disclosure also provides a computer program product, including a computer program / instruction, which, when executed by a processor, implements the RISC-V emulator formatted output function implementation method provided in this disclosure.

[0135] The above description is merely an optional embodiment of this disclosure and is not intended to limit this disclosure. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this disclosure should be included within the protection scope of this disclosure.

Claims

1. A method for implementing a formatted output function in a RISC-V emulator, characterized in that, The method includes: Obtain the RISC-V source code to be simulated, and input the RISC-V source code to be simulated into the compiler to obtain the first code file after translation by the compiler. The first code file is a binary file implemented in C language. The first code file is translated into a second code file, where the second code file is an intermediate representation (IR) code file. The translation of the first code file into the second code file includes translating multiple printf functions in the first code file into IR form based on a first jump table. In the case where the format string of the first printf function includes the string placeholder %s, the translation of the first code file into the second code file includes translating the first printf function into IR form based on the first jump table and a second jump table corresponding to %s in the format string of the first printf function. After the first code file is translated into the second code file, the second code file is executed using a just-in-time compiler; The step of translating the first printf function into IR form based on the first jump table and the second jump table includes: Obtain multiple strings from the rodata segment data of the first code file, and save the multiple strings and the address corresponding to each string to a mapping table; Translate each string in the mapping table into IR form; During the process of translating each string in the mapping table into IR form, the format string of the first printf function is obtained; Based on the value of parameter register x10 and the first jump table, the first address corresponding to the format string of the first printf function is determined. The first jump table is used to list the address corresponding to each string in the first code file. Based on the %s in the format string of the first printf function, a second jump table is generated. The second jump table is called by the %s in the format string of the first printf function. The second jump table is used to list the address corresponding to each string in the first code file. Based on the value of the parameter register corresponding to %s in the format string of the first printf function and the second jump table, determine the second address corresponding to %s in the format string of the first printf function; Based on the first address and the second address, the first printf function is translated into IR form.

2. The method according to claim 1, characterized in that, The method further includes: The return address of the first printf function is determined based on the return value global variable and the third jump table, wherein the third jump table is used to list the return address of each printf function in the first code file.

3. The method according to claim 1, characterized in that, The method further includes: If the format string of the first printf function also includes at least one first placeholder, obtain the value of the parameter register corresponding to each first placeholder, wherein the first placeholder is any placeholder other than %s; Based on the first address, the second address, and the value of the parameter register corresponding to each of the first placeholders, the first printf function is translated into IR form.

4. A device for implementing formatted output functions in a RISC-V emulator, characterized in that, The device includes: The acquisition module is used to acquire the RISC-V source code to be simulated and input the RISC-V source code to be simulated into the compiler to obtain the first code file after translation by the compiler. The first code file is a binary file implemented in C language. A translation module is used to translate the first code file into a second code file, wherein the second code file is a code file in intermediate representation (IR) form. The translation of the first code file into the second code file includes translating multiple printf functions in the first code file into IR form based on a first jump table. In the case that the format string of the first printf function includes the string placeholder %s, the translation of the first code file into the second code file includes translating the first printf function into IR form based on the first jump table and a second jump table corresponding to %s in the format string of the first printf function. An execution module is used to execute the second code file using a just-in-time compiler after the first code file has been translated into a second code file; The translation module is also used for: Obtain multiple strings from the rodata segment data of the first code file, and save the multiple strings and the address corresponding to each string to a mapping table; Translate each string in the mapping table into IR form; During the process of translating each string in the mapping table into IR form, the format string of the first printf function is obtained; Based on the value of parameter register x10 and the first jump table, the first address corresponding to the format string of the first printf function is determined. The first jump table is used to list the address corresponding to each string in the first code file. Based on the %s in the format string of the first printf function, a second jump table is generated. The second jump table is called by the %s in the format string of the first printf function. The second jump table is used to list the address corresponding to each string in the first code file. Based on the value of the parameter register corresponding to %s in the format string of the first printf function and the second jump table, determine the second address corresponding to %s in the format string of the first printf function; Based on the first address and the second address, the first printf function is translated into IR form.

5. The apparatus according to claim 4, characterized in that, The translation module is also used for: The return address of the first printf function is determined based on the return value global variable and the third jump table, wherein the third jump table is used to list the return address of each printf function in the first code file.

6. A computer device, characterized in that, The computer device includes a memory and a processor, wherein the memory stores at least one computer program, which is loaded and executed by the processor to implement the method according to any one of claims 1 to 3.

7. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores at least one computer program, which is loaded and executed by a processor to implement the method according to any one of claims 1 to 3.

8. A computer program product comprising a computer program / instructions, characterized in that, When the computer program / instructions are executed by the processor, they implement the method described in any one of claims 1 to 3.

Citation Information

Patent Citations

  • Method and device for code conversion from assembly language to high-level language

    CN102830975A

  • Simulation system and method based on digital signal processor

    CN118860762A