RISC-V simulator formatted output function implementation method and device, equipment and storage medium
By translating the printf function into IR form based on the first jump table and the second jump table in the RISC-V emulator, the format output problem when implementing the printf function in the prior art is solved, and accurate and effective printf function execution is achieved.
Patent Information
- Application Number
- CN202510083830.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-20
- Publication Date
- 2025-05-30
- Estimated Expiration
- 2045-01-20
AI Technical Summary
When implementing the printf function, the existing RISC-V emulators are difficult to accurately process different types of outputs such as floating point numbers, integers, and strings, as well as handle different placeholders, resulting in implementation problems.
By obtaining the RISC-V source code to be simulated, input it into the compiler, obtaining the binary file implemented in C language, and translating it into a code file representing the IR form in the middle. The specific steps include translating the printf function into IR form based on the first jump table and the second jump table, especially when the formatted string contains %s, and realizing dynamic translation through a combination of two jump tables.
The printf function is executed accurately and efficiently in the RISC-V emulator, ensuring the accuracy of the output format and the accuracy of the translation, and solving the difficulties in implementing the printf function in the prior art.
Smart Images

Figure CN120066474A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the technical field of emulators, and particularly to a method, apparatus, device, and storage medium for implementing a formatted output function of a RISC-V emulator. Background Art
[0002] The formatted output function, namely 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 process different types of outputs such as floating-point numbers, integers, and strings, as well as handle different placeholders (such as %d, %c, %s, etc.).
[0003] In the related art, the RISC-V emulator usually directly translates the RISC-V code into a form of code that can run on the target architecture. For the printf function, simple translation cannot meet these format requirements of the printf function, resulting in possible problems in implementing the printf function in the RISC-V emulator. Summary of the Invention
[0004] The present disclosure provides a method, apparatus, device, and storage medium for implementing a formatted output function of a RISC-V emulator, which can accurately and effectively implement the printf function in the RISC-V emulator. The technical solutions at least include the following: In a first aspect, a method for implementing a formatted output function of a RISC-V emulator is provided, including: obtaining the RISC-V source code to be emulated, and inputting the RISC-V source code to be emulated into a compiler to obtain a first code file translated by the compiler, where the first code file is a binary file implemented in the C language; translating the first code file into a second code file, where the second code file is a code file in the form of an intermediate representation IR. The translating the first code file into the second code file includes translating multiple printf functions in the first code file into the IR form based on a first jump table. When the formatted string of the first printf function includes the string placeholder %s, the translating the first code file into the second code file includes translating the first printf function into the IR form based on the first jump table and a second jump table corresponding to %s in the formatted 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 a just-in-time compiler.
[0005] Optionally, translating the first printf function into IR form based on the first jump table and the second jump table includes: obtaining multiple strings in the rodata segment data of the first code file, and saving the multiple strings and the address corresponding to each string into a mapping table; translating each string in the mapping table into IR form; in the process of translating each string in the mapping table into IR form, obtaining the format string of the first printf function; determining a first address corresponding to the format string of the first printf function based on the value of parameter register x10 and the first jump table, where the first jump table is used to list the addresses 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, where the second jump table is called by %s in the format string of the first printf function and is used to list the addresses corresponding to each string in the first code file; determining a 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.
[0006] Optionally, the method further includes: determining the return address of the first printf function based on a return value global variable and a third jump table, where the third jump table is used to list the return addresses of each printf function in the first code file.
[0007] Optionally, the method further includes: when 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, where 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.
[0008] In a second aspect, there is also provided an apparatus for implementing a formatted output function of a RISC-V emulator, including: an acquisition module, configured to acquire 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 after being translated by the compiler, where the first code file is a binary file implemented in the C language; a translation module, configured to translate the first code file into a second code file, where the second code file is a code file in the form of intermediate representation IR. The translating the first code file into the second code file includes translating multiple printf functions in the first code file into the IR form based on a first jump table. When the formatted string of the first printf function includes a string placeholder %s, the translating the first code file into the second code file includes translating the first printf function into the IR form based on the first jump table and a second jump table corresponding to %s in the formatted string of the first printf function; an execution module, configured to, after the first code file is translated into the second code file, execute the second code file using a just-in-time compiler.
[0009] Optionally, the translation module is further configured to acquire multiple strings in the rodata segment data of the first code file, and save the multiple strings and the address corresponding to each string into a mapping table; translate each string in the mapping table into the IR form; during the process of translating each string in the mapping table into the IR form, acquire the formatted string of the first printf function; determine a first address corresponding to the formatted string of the first printf function based on the value of the parameter register x10 and the first jump table, where the first jump table is used to list the addresses corresponding to each string in the first code file; generate a second jump table based on %s in the formatted string of the first printf function, where the second jump table is called by %s in the formatted string of the first printf function, and the second jump table is used to list the addresses corresponding to each string in the first code file; determine a second address corresponding to %s in the formatted string of the first printf function based on the value of the parameter register corresponding to %s in the formatted string of the first printf function and the second jump table; translate the first printf function into the IR form based on the first address and the second address.
[0010] Optionally, the translation module is further configured to determine the return address of the first printf function based on a return value global variable and a third jump table, where the third jump table is used to list the return addresses of each printf function in the first code file.
[0011] Optionally, the translation module is further configured to, when the format string of the first printf function further includes at least one first placeholder, obtain the value of the parameter register corresponding to each first placeholder, where 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.
[0012] In a third aspect, a computer device is further provided, including: a memory and a processor, where at least one computer program is stored in the memory, and the at least one computer program is loaded and executed by the processor to implement the method for implementing the RISC-V emulator formatted output function in the foregoing embodiments.
[0013] In a fourth aspect, a computer-readable storage medium is further provided, where at least one computer program is stored in the computer-readable storage medium, and the at least one computer program is loaded and executed by a processor to implement the method for implementing the RISC-V emulator formatted output function in the foregoing embodiments.
[0014] In a fifth aspect, a computer program product is provided, including computer programs / instructions, where when the computer programs / instructions are executed by a processor, the method described in the first aspect is implemented.
[0015] The beneficial effects brought by the technical solutions provided in the embodiments of the present disclosure at least include: In the embodiments of the present disclosure, based on the first jump table, multiple printf functions in the first code file can be accurately translated into IR form, which is equivalent to dynamic translation based on the jump table rather than static translation. Dynamic translation based on the jump table can effectively ensure the accuracy of the translated printf function. For the string placeholder %s, since its corresponding parameter is a string and the specific string cannot be determined before execution, in the embodiments of the present disclosure, through 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 with %s is translated into IR form, which is equivalent to dynamic translation based on two jump tables (where 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), thereby effectively ensuring the accuracy of the finally translated printf function. Description of the Drawings
[0016] To more clearly illustrate the technical solutions in the embodiments of the present disclosure, the following will briefly introduce the accompanying drawings required for the description of the embodiments. Obviously, the accompanying drawings in the following description are only some embodiments of the present disclosure. For those of ordinary skill in the art, without creative efforts, other accompanying drawings can be obtained based on these drawings.
[0017] Figure 1 Shows a flowchart of a method for implementing a formatted output function of a RISC-V emulator provided by an exemplary embodiment of the present disclosure; Figure 2 Shows a flowchart of a method for implementing a formatted output function of a RISC-V emulator provided by another exemplary embodiment of the present disclosure; Figure 3 Shows a schematic structural diagram of a device for implementing a formatted output function of a RISC-V emulator provided by an exemplary embodiment of the present disclosure; Figure 4 Is a schematic structural diagram of a computer device provided by an embodiment of the present disclosure. Detailed implementation manners
[0018] Unless otherwise defined, the technical terms or scientific terms used herein shall have the ordinary meanings understood by those of ordinary skill in the art to which the present disclosure belongs. The "first", "second", "third" and similar terms used in the specification and claims of the present patent application do not indicate any order, quantity or importance, but are only used to distinguish different components. Similarly, the terms such as "a" or "one" do not indicate a quantity limitation, but mean that there is at least one. The terms such as "comprising" or "including" mean that the elements or objects appearing before "comprising" or "including" cover the elements or objects listed after "comprising" or "including" and their equivalents, and do not exclude other elements or objects.
[0019] To make the purpose, technical solutions and advantages of the present disclosure clearer, the following will further describe the embodiments of the present disclosure in detail with reference to the accompanying drawings.
[0020] In the embodiments of the present disclosure, the RISC-V emulator runs on an x86-64 host, that is, the RISC-V emulator is used to execute RISC-V code on the x86-64 host.
[0021] Figure 1 Shows a flowchart of a method for implementing a formatted output function of a RISC-V emulator provided by an exemplary embodiment of the present disclosure, and this method can be executed by a computer device. Refer to Figure 1 This method includes: In step 101, obtain 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.
[0022] The first code file is a binary file implemented in the C language.
[0023] Here, the RISC-V source code to be simulated can be input by the user to the simulator. After the user inputs the RISC-V source code to be simulated to the simulator, the simulator will first input the RISC-V source code to be simulated into the compiler to obtain a C language binary code file translated by the compiler.
[0024] In step 102, translate the first code file into a second code file.
[0025] The second code file is a code file in the form of intermediate representation IR (Intermediate Representation). IR is a key component in the LLVM (Low Level Virtual Machine) project. There are three forms of IR in LLVM: assembly language form, binary file form, and memory representation form. The second code file in this embodiment of the present disclosure is an IR file in the form of assembly language.
[0026] Translating the first code file into the second code file includes: based on the first jump table, translating multiple printf functions in the first code file into the IR form; 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: based on the first jump table and the second jump table corresponding to %s in the format string of the first printf function, translating the first printf function into the IR form.
[0027] Wherein, the first printf function is one of the multiple printf functions.
[0028] The printf function generally includes two parts: a format string and output parameters. The format string belongs to a string of characters, and the output parameters correspond one-to-one with the placeholders in the format string. When there are k placeholders in the format string, there are correspondingly k corresponding output parameters.
[0029] The format of the printf function is usually: printf(“format string”, output parameter).
[0030] In the process of translating multiple printf functions in the first code file into IR form, before executing the second code file, it is uncertain which string the format string corresponds to, and it is also uncertain which data the placeholders in the format string correspond to. If the printf function is directly statically translated and directly translated into IR form, it will cause execution errors.
[0031] 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.
[0032] In the embodiments of the present disclosure, based on the first jump table, multiple printf functions in the first code file can be accurately translated into IR form. This is equivalent to a translation based on the jump table rather than a static translation. The translation based on the jump table belongs to a type of dynamic translation, which can effectively ensure the accuracy of the translated printf function. For the string placeholder %s, since its corresponding parameter is a string and it is also uncertain which specific string it corresponds to before execution, in the embodiments of the present disclosure, through 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 with %s is translated into IR form. This is equivalent to a translation based on two jump tables (where 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), thereby effectively ensuring the accuracy of the finally translated printf function.
[0033] Figure 2 The flowchart of the method for implementing the formatted output function of the RISC-V emulator provided by another exemplary embodiment of the present disclosure is shown. This method can be executed by a computer device. Refer to Figure 2 , this method includes: In step 201, the RISC-V source code to be simulated is obtained, and the RISC-V source code to be simulated is input into a compiler to obtain the first code file translated by the compiler.
[0034] The first code file is a binary file implemented in the C language.
[0035] For the relevant content of step 201, refer to the foregoing step 101, which is not elaborated here.
[0036] In step 202, the first code file is translated into the second code file.
[0037] The second code file is a code file in the intermediate representation IR form.
[0038] Translating the first code file into the second code file includes: based on the first jump table, translating multiple printf functions in the first code file into IR form; 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: based on the first jump table and the second jump table corresponding to %s in the format string of the first printf function, translating the first printf function into IR form.
[0039] Wherein, the first printf function is one of the multiple printf functions.
[0040] Optionally, based on the first jump table and the second jump table corresponding to %s in the format string of the first printf function, translating the first printf function into IR form includes the following steps a - e.
[0041] Step a, obtain multiple strings in the rodata segment data of the first code file, and save the multiple strings and the address corresponding to each string into a mapping table.
[0042] 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. There are multiple strings in the data segment, and these strings include both the format strings in the printf functions and the regular strings (that is, strings that are not format strings) in the RISC - V source code.
[0043] When saving the string and the address corresponding to the string into the mapping table, only save valid characters. RISC - V strings are stored and processed in ASCII encoding. Generally, valid characters include two cases: the first case, the character is less than or equal to 126 and greater than or equal to 33. The second case, the character is a space, tab, slash, quote, etc. If a character meets any of the above two cases, it means that the character is a valid character.
[0044] In the RISC - V environment, strings usually end with a null character ('\0'). When the characters in a string are stored until the null character, it means that the string storage is complete.
[0045] Step b, translate each string in the mapping table into IR form.
[0046] Since the mapping table includes both the format strings in the printf function and the regular strings in the RISC-V source code, each character in the mapping table can be translated into the IR form first, and then the format strings in the printf function can be additionally processed through steps c-g, so as to realize the translation of each character in the mapping table into the IR form.
[0047] When translating the strings in the mapping table into the IR form, the strings need to be saved as global variables. Exemplarily, the global variable name can be in the form of @.str + address. For example, if the string "keypair" is stored at 0x00008280 (the address is in hexadecimal, equivalent to 33408 in decimal), the corresponding global variable for this string is @.str33408. The process of declaring this string as a global variable can be expressed as: @.str33408 = private unnamed_addr constant [8 x i8] c"keypair\00". The characters are 8-bit (7-bit characters in the string plus 1-bit null character at the end), so it is 8 x i8. Each string ends with '\0', which needs to be represented by the ASCII code \00. The other content is the fixed content for declaring the string.
[0048] Step c, in the process of translating each string in the mapping table into the IR form, obtain the format string of the first printf function.
[0049] The format string of the first printf function includes %s.
[0050] Exemplarily, the determination of the format string can be achieved through regular matching. For example, regular matching can be used to match placeholders such as %d and %s. Usually, the string with placeholders is the format string. Any printf function has only one format string, so the format string of the first printf function can be any string matched by regular matching.
[0051] Step d, based on the value of the parameter register x10 and the first jump table, determine the first address corresponding to the format string of the first printf function.
[0052] The first jump table is used to list the addresses corresponding to each string in the first code file. For any format string, the first jump table can be shared.
[0053] In the static case, the data corresponding to the format string of each printf function is indeterminate. In the dynamic case (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 format string of the current printf function needs to jump to.
[0054] Therefore, in the embodiments of the present disclosure, based on the value of the 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 further determine the first address where the string is located (for example, the first address where the string is located can be determined through a mapping table).
[0055] Step e: Generate a second jump table based on the %s in the format string of the first printf function.
[0056] The second jump table is called by the %s in the format string of the first printf function, and the second jump table is used to list the addresses corresponding to each string in the first code file.
[0057] For %s in the format string, its corresponding output parameter is a string rather than a single character or an integer. Therefore, an additional jump table needs to be generated. Since the calling method of each %s for a string is different from the calling method of the format string for a string, the jump table corresponding to %s cannot directly use the first jump table corresponding to the format string, but a new jump table, that is, the second jump table, needs to be generated.
[0058] Optionally, for the case where there are other placeholders in the format string besides %s, step e further includes: when 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; the first placeholder is any placeholder other than %s.
[0059] Correspondingly, 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.
[0060] For the case where there is a first placeholder in the format string, when translating, the value in the parameter register corresponding to the placeholder can be directly passed into the first placeholder.
[0061] Step f: 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.
[0062] For RISC-V code, the parameter registers corresponding to the placeholders are x11 - x17, and the placeholder corresponding to %s is x12. If the number of placeholders in a format string exceeds 7, the extra parameters are passed through the stack.
[0063] Therefore, the value of the parameter register corresponding to %s in the format string of the first printf function here is also the value of the parameter register x12. Substituting the value of the parameter register x12 into the second jump table can determine the string corresponding to %s in the format string of the first printf function.
[0064] Step g: Translate the first printf function into IR form based on the first address and the second address.
[0065] It should be noted that the values of the parameter registers in the above steps a - g are determined in real time during the execution of the second code file. Similarly, the first address and the second address 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 the static translation process, only the action of generating the jump table is actually performed. The real first address and second address can be determined only when the second code file is dynamically executed. After both the first address and the second address are determined, the first printf function can be accurately translated. When the second code file is dynamically executed, if a jump instruction is encountered and the jump label of this jump instruction is printf, it means that the formatted output function is executed. Since the corresponding jump table has been generated during the static translation process, at this time, execute steps d and f to determine the first address and the second address, and then the printf function can be accurately executed.
[0066] For other printf functions except the first printf function, the above steps a - g can also be used to accurately translate them into IR form.
[0067] Next, an example is given for the first jump table and the second jump table. In the actual code process, the first jump table and the second jump table are an integral part. The second jump table is nested in the first jump table. Through the string in the first jump table, it is possible to jump to the second jump table. And if a format string has multiple %s, then this format string will correspondingly correspond to multiple second jump tables (that is, one %s corresponds to one jump table).
[0068] Exemplarily, after a certain format string is translated into IR form in the mapping table (i.e., after being saved as a global variable), its name is @.str33441 = private unnamed_addr constant [17 x i8] c"[#sn(%u)],\20[%s]\0A\00". There are 17 characters in this string, and each character is 8 bits, so it is 17 x i8.
[0069] The following is part of the jump table.
[0070] callPrintfTable: %printfIndex0 = load i32, ptr @printfIndex0 switch i32 %printfIndex0, label %default i32 33144, label %printf_call_33144 … i32 33441, label %printf_call_33441 … / / This part is the content corresponding to the first jump table printf_call_33441: %printfIndex2_33441 = load i32, ptr @printfIndex2 switch i32 %printfIndex2_33441, label %default i32 33144, label %printf_call_a33441_a33144 i32 33168, label %printf_call_a33441_a33168 ... / / This part is the content corresponding to the second jump table.
[0071] In the case of static translation, only the first jump table and the second jump table need to exist. In the case of dynamic execution, it is necessary to first obtain the value of the parameter register corresponding to the formatted string, that is, the value in x10. Assume that according to the value in the parameter register x10, it is possible to determine the jump to the string printf_call_33441 in the first jump table; then it is possible to read the value of the parameter register corresponding to %s, that is, the value in x12. Assume that according to the value in the parameter register x12, it is possible to determine the jump to the string printf_call_a33441_a33144 in the second jump table. At this time, the formatted string is basically translated, and the value corresponding to the placeholder %u has not been determined. It is only necessary to obtain the value of the parameter register corresponding to %u (that is, x11) and pass the value of the parameter register corresponding to %u to the position of %u.
[0072] Among them, printfIndex is a variable used to store the parameters in the parameter register. printfIndex0 represents the value of the parameter in the parameter register x10, and printfIndex2 represents the value of the parameter in the parameter register x12.
[0073] 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, where the third jump table is used to list the return addresses of each printf function in the first code file.
[0074] Here, each printf function needs to determine the return address after the jump (the return address is the address of the next instruction of the printf function) so as to continue to execute the second code file. When determining the return address, according to the value of the current return value global variable, it is possible to determine which address in the third jump table needs to be jumped to, and the 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 the dynamic execution of the code. In the case of static translation, only the process of generating the third jump table exists.
[0075] For any printf function, the first jump table and the third jump table can be shared, which is equivalent to only generating one first jump table and the third jump table and directly calling them later. Since the calling method of %s is different, the second jump table needs to be generated separately during the translation process.
[0076] In some embodiments, the return address can be stored in the form of a label.
[0077] Normally, in RISC-V, to jump into the printf function, the jal jump instruction is needed, and to jump from the printf function to the return address, the jalr jump instruction is needed. In the embodiments of the present disclosure, since the first code file is translated into the second code file in IR form, the jalr jump instruction is not required, and the determined return address can be directly used.
[0078] Exemplarily, the first printf function needs to first 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 part of the example code for determining the return address.
[0079] printf_call_a33441_a33144: %retx1_a33441_a33144 = load i32, ptr @printfIndex8 switch i32 %retx1_a33441_a33144, label %default i32 20534, label %printf_call_a33441_a33144_a20534 i32 20590, label %printf_call_a33441_a33144_a20590 i32 20646, label %printf_call_a33441_a33144_a20646 i32 20718, label %printf_call_a33441_a33144_a20718 … / / This part is the content corresponding to the third jump table.
[0080] printf_call_a33441_a33144_a20534: %x11_1323 = load i32, ptr @x11 call i32 (ptr,...) @printf(ptr @.str33441, i32 %x11_1323, ptr @.str33144) br label %_L292 During the dynamic execution process, after the translation of the first printf function is completed, 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 to jump to the string printf_call_a33441_a33144_a20534 in the third jump table, that is, the return address is 20534 (0x00005036). This return address can exist in the code in the form of a label, and the label is _L292.
[0081] For the other code in the first code file except the printf function, it can be translated into the IR form in the manner of related technologies, and finally the translated second code file is obtained.
[0082] 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.
[0083] In implementation, the lli tool in LLVM can be used, and this tool can use a just-in-time compiler (JIT) to execute the second code file.
[0084] By using a just-in-time compiler to execute the second code file, it is possible to simulate the program behavior of the RISC-V architecture in the x86 architecture without actual hardware, and there is no need to translate the second code file into an x86 binary file before execution, which improves the efficiency of the RISC-V emulator. By accurately implementing printf in the RISC-V emulator, the development efficiency can be greatly improved, helping developers to verify and debug programs, reducing hardware dependence, and promoting cross-platform development.
[0085] The following is the device embodiment of the present application. For the details not described in detail in the device embodiment, reference can be made to the above method embodiment.
[0086] Figure 3 The structural schematic diagram of the RISC-V emulator formatted output function implementation device provided by an exemplary embodiment of the present disclosure is shown. Refer to Figure 3 This RISC-V emulator formatted output function implementation device 300 includes: an acquisition module 301, a translation module 302, and an execution module 303.
[0087] 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 being translated by the compiler. The first code file is a binary file implemented in the C language.
[0088] The translation module 302 is used to translate the first code file into a second code file, where the second code file is a code file in the 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 the IR form based on the first jump table. 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 the 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.
[0089] The execution module 303 is used to execute the second code file using a just-in-time compiler after the first code file is translated into the second code file.
[0090] Optionally, the translation module 302 is further used to obtain multiple strings in the rodata segment data of the first code file, and save the multiple strings and the address corresponding to each string into a mapping table; translate each string in the mapping table into the IR form; during the process of translating each string in the mapping table into the 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, where the first jump table is 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, where the second jump table is called by %s in the format string of the first printf function and is 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; translate the first printf function into the IR form based on the first address and the second address.
[0091] Optionally, the translation module 302 is further used to determine the return address of the first printf function based on the return value global variable and a third jump table, where the third jump table is used to list the return addresses of each printf function in the first code file.
[0092] Optionally, when the format string of the first printf function further includes at least one first placeholder, the translation module 302 is further used to obtain the value of the parameter register corresponding to each first placeholder, where the first placeholder is any placeholder other than %s; translate the first printf function into the IR form based on the first address, the second address, and the value of the parameter register corresponding to each first placeholder.
[0093] It should be noted that when implementing the RISC-V emulator formatting output function by the device for implementing the RISC-V emulator formatting output function provided in the above embodiments, only the division of the above functional modules is used for illustration. In actual applications, the above functions can be allocated to different functional modules according to needs, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. In addition, the device for implementing the RISC-V emulator formatting output function provided in the above embodiments and the embodiments of the method for implementing the RISC-V emulator formatting output function belong to the same concept. For the specific implementation process, please refer to the method embodiments and will not be elaborated here.
[0094] The division of modules in the embodiments of the present disclosure is illustrative. It is only a logical function division. In actual implementation, there may be other division methods. In addition, in each embodiment of the present disclosure, the functional modules can be integrated in a processor, can exist separately physically, or two or more modules can be integrated into one module. The above integrated modules can be implemented in the form of hardware or in the form of software functional modules.
[0095] If the integrated module is implemented in the form of a software functional module and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the present 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. The computer software product is stored in a storage medium and includes several instructions to enable a terminal device (which can be a personal computer, a mobile phone, or a communication device, etc.) or a processor to execute all or part of the steps of the method in each embodiment of the present disclosure. The foregoing storage medium includes: various media such as USB flash drives, mobile hard disks, read-only memories (ROMs), random access memories (RAMs), magnetic disks, or optical discs that can store program codes.
[0096] Figure 4 is a schematic structural diagram of a computer device provided in an embodiment of the present disclosure. As Figure 4 shown, the computer device 400 includes: a processor 401 and a memory 402.
[0097] The processor 401 may include one or more processing cores, such as a quad-core processor, an octa-core processor, etc. The processor 401 may be implemented in at least one of the following hardware forms: DSP (Digital Signal Processing), FPGA (Field-Programmable Gate Array), and PLA (Programmable Logic Array). The processor 401 may also include a main processor and a coprocessor. The main processor is a processor for processing data in the wake state, also known as the CPU (Central Processing Unit); the coprocessor is a low-power processor for processing data in the standby state. In some embodiments, the processor 401 may be integrated with a GPU (Graphics Processing Unit), and the GPU is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 401 may further include an AI (Artificial Intelligence) processor, which is used to process computational operations related to machine learning.
[0098] The memory 402 may include one or more computer-readable storage media, and the computer-readable storage media may be non-transitory. The memory 402 may further include high-speed random access memory and non-volatile memory, such as one or more disk storage devices and flash storage devices. In some embodiments, the non-transitory computer-readable storage media in the memory 402 is used to store at least one instruction, and the at least one instruction is used to be executed by the processor 401 to implement the method for implementing the RISC-V emulator formatting output function provided in the embodiments of the present disclosure.
[0099] Those skilled in the art can understand that Figure 4 the structure shown in does not constitute a limitation on the computer device 400, and it may include more or fewer components than shown in the figure, or combine certain components, or adopt a different component layout.
[0100] The embodiments of the present disclosure also provide a non-transitory computer-readable storage medium. When the instructions in the storage medium are executed by the processor of the computer device, the computer device can execute the method for implementing the RISC-V emulator formatting output function provided in the embodiments of the present disclosure.
[0101] The embodiments of the present disclosure also provide a computer program product, including a computer program / instructions. When the computer program / instructions are executed by the processor, the method for implementing the RISC-V emulator formatting output function provided in the embodiments of the present disclosure is implemented.
[0102] The foregoing are only optional embodiments of the present disclosure and are not intended to limit the present disclosure. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principle of the present disclosure shall be included within the protection scope of the present disclosure.
Claims
1. A method for implementing a formatted output function of a RISC-V simulator, characterized in that: The method comprises: Obtaining a 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; The first code file is translated into a second code file, wherein the second code file is a code file in an intermediate representation (IR) form, wherein the translating the first code file into the second code file includes translating a plurality of printf functions in the first code file into an IR form based on a first jump table, and when a formatting string of the first printf function includes a string placeholder %s, the translating the first code file into the second code file includes translating the first printf function into an IR form based on the first jump table and a second jump table corresponding to the %s in the formatting string of the first printf function; After the first code file is translated into a second code file, the second code file is executed using a just-in-time compiler.
2. The method according to claim 1, characterized in that The translating the first printf function into an IR form based on the first jump table and the second jump table includes: Acquire multiple character strings in the rodata segment data of the first code file, and save the multiple character strings and the address corresponding to each of the character strings into a mapping table; Translate each of the strings in the mapping table into IR form; In the process of translating each of the strings in the mapping table into IR form, obtaining a format string of the first printf function; Determine a first address corresponding to the formatted 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; Based on %s in the format string of the first printf function, generating a second jump table, wherein the second jump table is called by %s in the format string of the first printf function, and the second jump table is used to list the address corresponding to each string in the first code file; Determine a second address corresponding to %s in the formatted string of the first printf function based on the value of the parameter register corresponding to %s in the formatted string of the first printf function and the second jump table; The first printf function is translated into IR form based on the first address and the second address.
3. The method according to claim 1 or 2, characterized in that: The method further comprises: The return address of the first printf function is determined based on the return value global variable and the third jump table, where the third jump table is used to list the return address of each printf function in the first code file.
4. The method according to claim 1 or 2, characterized in that: The method further comprises: In a case where the format string of the first printf function further includes at least one first placeholder, obtaining a value of a parameter register corresponding to each first placeholder, wherein the first placeholder is any placeholder except %s; The first printf function is translated into IR form based on the first address, the second address, and the value of the parameter register corresponding to each of the first placeholders.
5. A RISC-V simulator formatted output function implementation device, characterized in that: The device comprises: An acquisition module is used to acquire a 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, configured to translate the first code file into a second code file, the second code file being a code file in an intermediate representation (IR) form, wherein the translating the first code file into the second code file comprises translating a plurality of printf functions in the first code file into an IR form based on a first jump table, and when a formatting string of the first printf function comprises a string placeholder %s, the translating the first code file into the second code file comprises translating the first printf function into an IR form based on the first jump table and a second jump table corresponding to %s in the formatting string of the first printf function; The execution module is used to execute the second code file by using a just-in-time compiler after the first code file is translated into the second code file.
6. The device according to claim 5, characterized in that The translation module is also used to: Acquire multiple character strings in the rodata segment data of the first code file, and save the multiple character strings and the address corresponding to each of the character strings into a mapping table; Translate each of the strings in the mapping table into IR form; In the process of translating each of the strings in the mapping table into IR form, obtaining a format string of the first printf function; Determine a first address corresponding to the formatted 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; Based on %s in the format string of the first printf function, generating a second jump table, wherein the second jump table is called by %s in the format string of the first printf function, and the second jump table is used to list the address corresponding to each string in the first code file; Determine a second address corresponding to %s in the formatted string of the first printf function based on the value of the parameter register corresponding to %s in the formatted string of the first printf function and the second jump table; The first printf function is translated into IR form based on the first address and the second address.
7. The device according to claim 5 or 6, characterized in that The translation module is also used to: The return address of the first printf function is determined based on the return value global variable and the third jump table, where the third jump table is used to list the return address of each printf function in the first code file.
8. A computer device, characterized in that: The computer device comprises: a memory and a processor, wherein at least one computer program is stored in the memory, and the at least one computer program is loaded and executed by the processor to implement the method according to any one of claims 1 to 4.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores at least one computer program, and the at least one computer program is loaded and executed by a processor to implement the method according to any one of claims 1 to 4.
10. A computer program product comprising a computer program / instructions, characterized in that When the computer program / instructions are executed by a processor, the method according to any one of claims 1 to 4 is implemented.
Citation Information
Patent Citations
Method and device for code conversion from assembly language to high-level language
CN102830975A
Hardware acceleration instruction determination method and system, electronic equipment and storage medium
CN118860487A
Simulation system and method based on digital signal processor
CN118860762A
RISC-V architecture-oriented binary program verification method and system
CN118916886A
Method and apparatus for generating code for scheduling the execution of binary code
US20050235270A1