* Code generation: How to define file-scope static variables? @ 2022-11-28 21:28 Robert Dubner 2022-11-28 23:01 ` David Malcolm 0 siblings, 1 reply; 4+ messages in thread From: Robert Dubner @ 2022-11-28 21:28 UTC (permalink / raw) To: gcc; +Cc: 'Bob Dubner' I am part of a team working on a COBOL front end for GCC. By reverse engineering other front ends, I learned, some months ago, how to create a function_decl GENERIC node that is the root of a GENERIC tree describing an entire function. By calling the routine cgraph_node::finalize_function() with that function_decl, the assembly language for that function is created, and all is well. But now I need to be able to create the equivalent of a file-scope static variable in C. This C program file: ////////////////// static int dubner_at_work = 123454321; int main(int argc, char **argv) { } ////////////////// produces, in part, this assembly language: ############### .file "ccc.c" .text .data .align 4 .type dubner_at_work, @object .size dubner_at_work, 4 dubner_at_work: .long 123454321 .text .globl main .type main, @function [...] ############### In my own GENERIC generation code, I believe that I am creating a proper translation_unit_decl that contains the block and the vars nodes for specifying "dubner_at_work". But I have been unable, after several days of looking, to figure out the equivalent of "cgraph_node::finalize_function" for a translation_unit_decl. The resulting assembly language doesn't have a definition for "dubner_at_work". Can anybody describe how I can tell the downstream processing that I need the translation_unit_decl to actually define storage? Thanks very much, Bob Dubner. ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Code generation: How to define file-scope static variables? 2022-11-28 21:28 Code generation: How to define file-scope static variables? Robert Dubner @ 2022-11-28 23:01 ` David Malcolm 2022-11-29 2:54 ` Robert Dubner 0 siblings, 1 reply; 4+ messages in thread From: David Malcolm @ 2022-11-28 23:01 UTC (permalink / raw) To: Robert Dubner, gcc; +Cc: 'Bob Dubner' On Mon, 2022-11-28 at 15:28 -0600, Robert Dubner wrote: > I am part of a team working on a COBOL front end for GCC. > > By reverse engineering other front ends, I learned, some months ago, > how > to create a function_decl GENERIC node that is the root of a GENERIC > tree > describing an entire function. > > By calling the routine cgraph_node::finalize_function() with that > function_decl, the assembly language for that function is created, > and all > is well. > > But now I need to be able to create the equivalent of a file-scope > static > variable in C. > > This C program file: > > ////////////////// > static int dubner_at_work = 123454321; > int main(int argc, char **argv) > { > } > ////////////////// > > produces, in part, this assembly language: > > ############### > .file "ccc.c" > .text > .data > .align 4 > .type dubner_at_work, @object > .size dubner_at_work, 4 > dubner_at_work: > .long 123454321 > .text > .globl main > .type main, @function > [...] > ############### > > In my own GENERIC generation code, I believe that I am creating a > proper > translation_unit_decl that contains the block and the vars nodes for > specifying "dubner_at_work". > > But I have been unable, after several days of looking, to figure out > the > equivalent of "cgraph_node::finalize_function" for a > translation_unit_decl. The resulting assembly language doesn't have > a > definition for "dubner_at_work". > > Can anybody describe how I can tell the downstream processing that I > need > the translation_unit_decl to actually define storage? You might find libgccjit's gcc/jit/jit-playback.cc helpful for this, as it tends to contain minimal code to build trees (generally simplified/reverse-engineered from the C frontend). playback::context::global_new_decl makes the VAR_DECL node, and such trees are added to the jit playback::context's m_globals. In playback::context::replay, we have: /* Finalize globals. See how FORTRAN 95 does it in gfc_be_parse_file() for a simple reference. */ FOR_EACH_VEC_ELT (m_globals, i, global) rest_of_decl_compilation (global, true, true); wrapup_global_declarations (m_globals.address(), m_globals.length()); So you'll probably want to do something similar for your globals. Caveat: this is all reverse-engineered by me/others from the C frontend (and I haven't touched this code in a while), so I may be missing things here. Dave ^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: Code generation: How to define file-scope static variables? 2022-11-28 23:01 ` David Malcolm @ 2022-11-29 2:54 ` Robert Dubner 2022-11-30 17:10 ` Robert Dubner 0 siblings, 1 reply; 4+ messages in thread From: Robert Dubner @ 2022-11-29 2:54 UTC (permalink / raw) To: David Malcolm, gcc; +Cc: Bob Dubner David, thank you very much. That looks very much like what I was hoping for. I'll dig into it tomorrow. Heartfelt thanks, Bob Dubner. -----Original Message----- From: David Malcolm <dmalcolm@redhat.com> Sent: Monday, November 28, 2022 18:01 To: Robert Dubner <rdubner@symas.com>; gcc@gcc.gnu.org Cc: 'Bob Dubner' <rdubner@dubner.com> Subject: Re: Code generation: How to define file-scope static variables? On Mon, 2022-11-28 at 15:28 -0600, Robert Dubner wrote: > I am part of a team working on a COBOL front end for GCC. > > By reverse engineering other front ends, I learned, some months ago, > how to create a function_decl GENERIC node that is the root of a > GENERIC tree describing an entire function. > > By calling the routine cgraph_node::finalize_function() with that > function_decl, the assembly language for that function is created, and > all is well. > > But now I need to be able to create the equivalent of a file-scope > static variable in C. > > This C program file: > > ////////////////// > static int dubner_at_work = 123454321; int main(int argc, char **argv) > { > } > ////////////////// > > produces, in part, this assembly language: > > ############### > .file "ccc.c" > .text > .data > .align 4 > .type dubner_at_work, @object > .size dubner_at_work, 4 > dubner_at_work: > .long 123454321 > .text > .globl main > .type main, @function > [...] > ############### > > In my own GENERIC generation code, I believe that I am creating a > proper translation_unit_decl that contains the block and the vars > nodes for specifying "dubner_at_work". > > But I have been unable, after several days of looking, to figure out > the equivalent of "cgraph_node::finalize_function" for a > translation_unit_decl. The resulting assembly language doesn't have a > definition for "dubner_at_work". > > Can anybody describe how I can tell the downstream processing that I > need the translation_unit_decl to actually define storage? You might find libgccjit's gcc/jit/jit-playback.cc helpful for this, as it tends to contain minimal code to build trees (generally simplified/reverse-engineered from the C frontend). playback::context::global_new_decl makes the VAR_DECL node, and such trees are added to the jit playback::context's m_globals. In playback::context::replay, we have: /* Finalize globals. See how FORTRAN 95 does it in gfc_be_parse_file() for a simple reference. */ FOR_EACH_VEC_ELT (m_globals, i, global) rest_of_decl_compilation (global, true, true); wrapup_global_declarations (m_globals.address(), m_globals.length()); So you'll probably want to do something similar for your globals. Caveat: this is all reverse-engineered by me/others from the C frontend (and I haven't touched this code in a while), so I may be missing things here. Dave ^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: Code generation: How to define file-scope static variables? 2022-11-29 2:54 ` Robert Dubner @ 2022-11-30 17:10 ` Robert Dubner 0 siblings, 0 replies; 4+ messages in thread From: Robert Dubner @ 2022-11-30 17:10 UTC (permalink / raw) To: David Malcolm, gcc; +Cc: Bob Dubner David, for completion I am updating this message with what I've learned. I do this in the hope that I might assist the next poor souls that find themselves reverse-engineering front ends because they are creating their own front end. I claim only that this worked for me. I can't you tell what assumptions I've been making, because I don't know what I don't know. That said: I found that for file-static variables, the trick is to create a var_decl for the variable with TREE_STATIC(var_decl)=1, and then to call rest_of_decl_compilation (var_decl, true, false); // top_level is true; at_end is false Having done that for an integer_type var_decl with the name "dubner_at_work", and with an initial value of 123454321, I now see this at the very beginning of the generated .s file: ###################### .file "call-scope-1.cbl" .text .Ltext0: .file 0 "/home/bob/repos/gcc-cobol/gcc/cobol/failures/call-scope-1" "call-scope-1.cbl" .data .align 4 .type dubner_at_work, @object .size dubner_at_work, 4 dubner_at_work: .long 123454321 .section .rodata [...] ###################### And that's exactly what I wanted. Thanks for your help. It was your mention of "rest_of_compilation" that ended this marathon investigation, and I really appreciate it. Bob Dubner -----Original Message----- From: Gcc <gcc-bounces+rdubner=symas.com@gcc.gnu.org> On Behalf Of Robert Dubner Sent: Monday, November 28, 2022 21:55 To: David Malcolm <dmalcolm@redhat.com>; gcc@gcc.gnu.org Cc: Bob Dubner <rdubner@dubner.com> Subject: RE: Code generation: How to define file-scope static variables? David, thank you very much. That looks very much like what I was hoping for. I'll dig into it tomorrow. Heartfelt thanks, Bob Dubner. -----Original Message----- From: David Malcolm <dmalcolm@redhat.com> Sent: Monday, November 28, 2022 18:01 To: Robert Dubner <rdubner@symas.com>; gcc@gcc.gnu.org Cc: 'Bob Dubner' <rdubner@dubner.com> Subject: Re: Code generation: How to define file-scope static variables? On Mon, 2022-11-28 at 15:28 -0600, Robert Dubner wrote: > I am part of a team working on a COBOL front end for GCC. > > By reverse engineering other front ends, I learned, some months ago, > how to create a function_decl GENERIC node that is the root of a > GENERIC tree describing an entire function. > > By calling the routine cgraph_node::finalize_function() with that > function_decl, the assembly language for that function is created, and > all is well. > > But now I need to be able to create the equivalent of a file-scope > static variable in C. > > This C program file: > > ////////////////// > static int dubner_at_work = 123454321; int main(int argc, char **argv) > { > } > ////////////////// > > produces, in part, this assembly language: > > ############### > .file "ccc.c" > .text > .data > .align 4 > .type dubner_at_work, @object > .size dubner_at_work, 4 > dubner_at_work: > .long 123454321 > .text > .globl main > .type main, @function > [...] > ############### > > In my own GENERIC generation code, I believe that I am creating a > proper translation_unit_decl that contains the block and the vars > nodes for specifying "dubner_at_work". > > But I have been unable, after several days of looking, to figure out > the equivalent of "cgraph_node::finalize_function" for a > translation_unit_decl. The resulting assembly language doesn't have a > definition for "dubner_at_work". > > Can anybody describe how I can tell the downstream processing that I > need the translation_unit_decl to actually define storage? You might find libgccjit's gcc/jit/jit-playback.cc helpful for this, as it tends to contain minimal code to build trees (generally simplified/reverse-engineered from the C frontend). playback::context::global_new_decl makes the VAR_DECL node, and such trees are added to the jit playback::context's m_globals. In playback::context::replay, we have: /* Finalize globals. See how FORTRAN 95 does it in gfc_be_parse_file() for a simple reference. */ FOR_EACH_VEC_ELT (m_globals, i, global) rest_of_decl_compilation (global, true, true); wrapup_global_declarations (m_globals.address(), m_globals.length()); So you'll probably want to do something similar for your globals. Caveat: this is all reverse-engineered by me/others from the C frontend (and I haven't touched this code in a while), so I may be missing things here. Dave ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-11-30 17:10 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2022-11-28 21:28 Code generation: How to define file-scope static variables? Robert Dubner 2022-11-28 23:01 ` David Malcolm 2022-11-29 2:54 ` Robert Dubner 2022-11-30 17:10 ` Robert Dubner
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).