how to get instruction codes in gcc passes?
Hi all. (I have never used these maillists before. Sorry if something wrong here.) I am newbie in gcc and I need some help. I am performing some research work in theme of code optimization. Now I have to write my own optimization pass for gcc. And in this pass I need to get the instruction codes of the resulting assemble code. I put my pass just before the "NEXT_PASS (pass_final);" in init_optimization_passes function. So I think that asm instructions are almost ready when my pass starts its work. This pass is already inserted into the gcc code and can be started. The gcc is compiled. And I can see my debug stuff in especially generated file when the gcc works. Actually I have no useful code yet, but I just want to get some information for starting the developing. For the beginning I want to do something like this: for (insn = get_insns() ; insn ; insn = NEXT_INSN(insn)) { int code = ...; //I need help in this line!!! myDebugOutputShowCode(++i, code); } I.e. I just want to see the whole list of code of instructions. Like assembler listing. Can you help me and give some advices how to do it? I had a look at some *.md files in gcc sources, but did not found any source of codes of assembler instructions. How does the gcc generates the binary file? Where can it get the binary representation of asm instruction? Where is it described that "nop" is "0F 1F" value for x86 architecture and so on? Thanks for any help.
Re: how to get instruction codes in gcc passes?
On Sun, Jun 13, 2010 at 6:38 PM, Richard Guenther wrote: > On Sun, Jun 13, 2010 at 4:29 PM, Ilya K wrote: >> Hi all. >> (I have never used these maillists before. Sorry if something wrong here.) >> >> I am newbie in gcc and I need some help. >> >> I am performing some research work in theme of code optimization. >> Now I have to write my own optimization pass for gcc. And in this pass >> I need to get the instruction codes of the resulting assemble code. >> >> I put my pass just before the "NEXT_PASS (pass_final);" in >> init_optimization_passes function. So I think that asm instructions >> are almost ready when my pass starts its work. >> This pass is already inserted into the gcc code and can be started. >> The gcc is compiled. And I can see my debug stuff in especially >> generated file when the gcc works. >> Actually I have no useful code yet, but I just want to get some >> information for starting the developing. >> >> For the beginning I want to do something like this: >> for (insn = get_insns() ; insn ; insn = NEXT_INSN(insn)) >> { >> int code = ...; //I need help in this line!!! >> myDebugOutputShowCode(++i, code); >> } >> >> I.e. I just want to see the whole list of code of instructions. Like >> assembler listing. >> >> Can you help me and give some advices how to do it? >> I had a look at some *.md files in gcc sources, but did not found any >> source of codes of assembler instructions. How does the gcc generates >> the binary file? Where can it get the binary representation of asm >> instruction? >> Where is it described that "nop" is "0F 1F" value for x86 architecture >> and so on? > > GCC does not have an integrated assembler and only outputs > assembler source. At the point above you still have RTXen > where you can query INSN_CODE to see what instruction from > the machine description was matched. In the define_insns > there are patterns for generating the assembler source instruction. > > Richard. > >> Thanks for any help. >> > OK. No low-level instruction codes in gcc. I found the definitions of instructions in i386.dm (thanks for this advice): (define_insn "nop" [(const_int 0)] "" "nop" [(set_attr "length" "1") (set_attr "length_immediate" "0") (set_attr "modrm" "0")]) So, the gcc only knows that some specific internal representation should be generated as "nop" string, right? And who is generating the binary file from the asm file? Does gcc call the external program? What the name of this program? Or the sources I need can be found inside the gcc directory? I think that if there are no such instruction-code table inside gcc, I can build my own version of it inside my pass. But I need the sources of some utility which knows how to convert "nop" string into the binary number. Where can I get it?
Re: how to get instruction codes in gcc passes?
On Sun, Jun 13, 2010 at 7:38 PM, Dave Korn wrote: ... > What exact use would it be to you having the opcode bytes known during an > optimisation pass? There may be a better/easier way to do whatever it is > you're trying to do. > > cheers, > DaveK > My main aim is to build platform-dependent optimization based on the minimizing of hamming distance between the instructions. So I need the platform-specific information like instruction codes. Well, may be I should to try to put it into GAS, not into the gcc. I just never looked over these assemblers. I thought that if gcc already have the base of optimizations then this will be a best place to put another one. I do not know now if these assembler tools have a room for optimization. Ilya K
Re: how to get instruction codes in gcc passes?
On 13/06/2010 20:57, Ian Lance Taylor wrote: > Take a look at http://code.google.com/p/mao/ . > Ian Thanks, Ian! This project looks very interesting. I will try to play with it. On Mon, Jun 14, 2010 at 2:28 AM, Dave Korn wrote: > Or in binutils, LD's relaxation infrastructure might be usable to this end. > But I think if you want something so platform dependent as to care about the > bitpatterns of opcodes, it almost certainly ought to live in the assembler or > linker rather than then compiler. > > cheers, > DaveK Yes, I agree that compiler maybe is not the best place for my optimizations. I will try to see if binutils or mao provides better environment for this developing. Thanks for pointing out the places where the low-level optimizations are more suitable. Ilya K
Re: how to get instruction codes in gcc passes?
On Mon, Jun 14, 2010 at 12:25 AM, Basile Starynkevitch wrote: > > Why do you want to optimize the generated assembly code? AFAIK, all > optimization passes in GCC work on some intermediate representation > which is not the assembly code, and many of them work on Gimple. > ... > ... GCC emit textual assembly code. The > assembler (that is binutils, not GCC) know that nop is 0f 1f. > > Cheers. > > PS. I might have some details wrong; I am not very familiar with GCC > back-ends & RTL passes. > > -- > Basile STARYNKEVITCH http://starynkevitch.net/Basile/ > email: basilestarynkevitchnet mobile: +33 6 8501 2359 > 8, rue de la Faiencerie, 92340 Bourg La Reine, France > *** opinions {are only mines, sont seulement les miennes} *** > > Yes, thanks. I have already seen that GCC does not have the instruction codes. Nevertheless I have to work on the low-level. At the back-end. It is a feature of my work :). So, maybe I'll just switch to inserting my code into binutils, or mao project. Ilya K