{"id":287,"date":"2009-12-09T06:29:44","date_gmt":"2009-12-09T14:29:44","guid":{"rendered":"http:\/\/www.airs.com\/blog\/?p=287"},"modified":"2009-12-09T06:29:44","modified_gmt":"2009-12-09T14:29:44","slug":"a-gcc-frontend","status":"publish","type":"post","link":"https:\/\/www.airs.com\/blog\/archives\/287","title":{"rendered":"A Gcc Frontend"},"content":{"rendered":"<p>When writing the gccgo frontend I had to figure out how to write a new gcc frontend.  This is a largely undocumented procedure.   Unfortunately, I did not take notes as I went along.  However, here are some retrospective comments.<\/p>\n<p>Every gcc frontend needs a set of language hooks.  This is done by including <code>\"langhooks-def.h\"<\/code> and writing <code>struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;<\/code>.  The specific language hooks are defined via various <code>LANG_HOOKS_xxx<\/code> macros.<\/p>\n<p>Some language hooks are required even though for a language like Go there is nothing for them to do: <code>LANG_HOOKS_GLOBAL_BINDINGS_P<\/code>, <code>LANG_HOOKS_PUSHDECL<\/code>, <code>LANG_HOOKS_GETDECLS<\/code>.  Also <code>LANG_HOOKS_TYPE_FOR_MODE<\/code> and <code>LANG_HOOKS_TYPE_FOR_SIZE<\/code> must be defined and must do something reasonable.  The <code>LANG_HOOKS_INIT<\/code> function must call some functions: <code>build_common_tree_nodes<\/code>, <code>set_sizetype<\/code>, <code>build_common_tree_nodes_2<\/code>, <code>build_common_builtin_nodes<\/code>.  It must also set the global variable <code>void_list_node<\/code>.  As far as I know all these steps are required and none of them are documented.  The <code>LANG_HOOKS_POST_OPTIONS<\/code> hook must set <code>flag_excess_precision_cmdline<\/code>.<\/p>\n<p>The main language hook is <code>LANG_HOOKS_PARSE_FILE<\/code>.  It will find the input file names in the global variables <code>in_fnames<\/code> and <code>num_in_fnames<\/code>.  At that point the frontend can take over and do the actual parsing and initial compilation.<\/p>\n<p>After <code>LANG_HOOKS_PARSE_FILE<\/code> creates a complete parse tree (in a global variable) and returns, the rest of the work is done by <code>LANG_HOOKS_WRITE_GLOBALS<\/code>.  The gccgo frontend generates GENERIC, although these days it could be modified to generate GIMPLE instead.  This basically means creating appropriate <code>DECL<\/code> nodes for all the global types, variables, and functions.  For a function, the frontend must create a <code>cfun<\/code> structure via <code>push_struct_function<\/code> or similar, and it must set <code>current_function_decl<\/code>.  The frontend must not only create the <code>FUNCTION_DECL<\/code>, it must fill in the <code>DECL_RESULT<\/code> field with a <code>RESULT_DECL<\/code>.  After creating the GENERIC or GIMPLE, it should call <code>cgraph_finalize_function<\/code>.<\/p>\n<p>After all the functions have been finalized and the global variables created, the frontend must call <code>cgraph_finalize_compilation_unit<\/code>.  That is where the middle-end really takes over and generates code.  The frontend must finish by calling <code>wrapup_global_declarations<\/code>, <code>check_global_declarations<\/code>, and <code>emit_debug_global_declarations<\/code>.<\/p>\n<p>The above is probably slightly inaccurate, and I&#8217;m sure I&#8217;ve left out some details.  And, of course, the frontend interfaces changes with each new gcc release.  However, if you are interested in writing a gcc frontend, I hope this will give you a start.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When writing the gccgo frontend I had to figure out how to write a new gcc frontend. This is a largely undocumented procedure. Unfortunately, I did not take notes as I went along. However, here are some retrospective comments. Every gcc frontend needs a set of language hooks. This is done by including &#8220;langhooks-def.h&#8221; and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-287","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/comments?post=287"}],"version-history":[{"count":3,"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/posts\/287\/revisions"}],"predecessor-version":[{"id":290,"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/posts\/287\/revisions\/290"}],"wp:attachment":[{"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/media?parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/categories?post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/tags?post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}