{"id":16456,"date":"2016-11-21T15:36:05","date_gmt":"2016-11-21T20:36:05","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=16456"},"modified":"2016-12-20T10:15:49","modified_gmt":"2016-12-20T15:15:49","slug":"constexpr-8cc-compile-time-c-compiler","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/11\/21\/constexpr-8cc-compile-time-c-compiler\/","title":{"rendered":"Constexpr-8cc: Compile-time C Compiler"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/kw-udon\/constexpr-8cc\">Keiichi Watanabe<\/a> (via <a href=\"https:\/\/news.ycombinator.com\/item?id=12988696\">Hacker News<\/a>):<\/p>\r\n<blockquote cite=\"https:\/\/github.com\/kw-udon\/constexpr-8cc\"><p>Constant expressions in C++ are expressions that can be evaluated at compile-time. In C++14, by relaxing constrains, constant expressions became so powerful that a C compiler can be implemented in!<\/p>\r\n<p>In constexpr-8cc, the main routine for compilations of C programs is implemented in a C++14 <code>constexpr<\/code> function. Therefore, if you compile <tt>8cc.cpp<\/tt> to a binary file by g++, compilation of a C program will be performed as a compile-time computation and the result of this C compilation will be embedded into the generated binary.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>When you see <a href=\"https:\/\/github.com\/kw-udon\/constexpr-8cc\/blob\/master\/8cc.hpp\"><code>8cc.hpp<\/code><\/a>, you will know this program was not written by hand.\r\nActually, I used <a href=\"https:\/\/github.com\/shinh\/elvm\">ELVM Compiler Infrastructure<\/a> to generate it.\r\nI just implemented a translator from ELVM IR to C++14 constexpr <a href=\"https:\/\/github.com\/shinh\/elvm\/pull\/15\">here<\/a>.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Keiichi Watanabe (via Hacker News): Constant expressions in C++ are expressions that can be evaluated at compile-time. In C++14, by relaxing constrains, constant expressions became so powerful that a C compiler can be implemented in! In constexpr-8cc, the main routine for compilations of C programs is implemented in a C++14 constexpr function. Therefore, if you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[326,255,71],"class_list":["post-16456","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-c-plus-plus","tag-compiler","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=16456"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16456\/revisions"}],"predecessor-version":[{"id":16457,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16456\/revisions\/16457"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=16456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=16456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=16456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}