diff --git a/include/vast/CodeGen/CodeGenDriver.hpp b/include/vast/CodeGen/CodeGenDriver.hpp index b7d273e170..675159a261 100644 --- a/include/vast/CodeGen/CodeGenDriver.hpp +++ b/include/vast/CodeGen/CodeGenDriver.hpp @@ -42,6 +42,7 @@ namespace vast::cg { explicit driver( acontext_t &actx , std::unique_ptr< mcontext_t > mctx + , options_t opts , std::unique_ptr< codegen_builder > bld , std::unique_ptr< meta_generator > mg , std::unique_ptr< symbol_generator > sg @@ -49,11 +50,12 @@ namespace vast::cg { ) : actx(actx) , mctx(std::move(mctx)) + , opts(opts) , bld(std::move(bld)) , mg(std::move(mg)) , sg(std::move(sg)) , visitor(std::move(visitor)) - , generator(mk_module_generator()) + , generator(mk_module_generator(opts)) {} void emit(clang::DeclGroupRef decls); @@ -73,6 +75,8 @@ namespace vast::cg { acontext_t &actx; std::unique_ptr< mcontext_t > mctx; + [[maybe_unused]] options_t opts; + symbol_tables scopes; // @@ -86,7 +90,7 @@ namespace vast::cg { // // module generation state // - module_generator mk_module_generator(); + module_generator mk_module_generator(const options_t &opts); module_generator generator; }; diff --git a/include/vast/CodeGen/CodeGenFunction.hpp b/include/vast/CodeGen/CodeGenFunction.hpp index f3f289b9ab..42ab685c09 100644 --- a/include/vast/CodeGen/CodeGenFunction.hpp +++ b/include/vast/CodeGen/CodeGenFunction.hpp @@ -6,6 +6,7 @@ #include "vast/CodeGen/ScopeContext.hpp" #include "vast/CodeGen/ScopeGenerator.hpp" +#include "vast/CodeGen/CodeGenOptions.hpp" #include "vast/Dialect/HighLevel/HighLevelOps.hpp" @@ -13,21 +14,18 @@ namespace vast::cg { struct module_context; - struct function_codegen_options - { - bool has_strict_return; - uint8_t optimization_level; - }; - // // function generation // struct function_context : function_scope { - using function_scope::function_scope; + function_context(scope_context *parent, const options_t &opts) + : function_scope(parent), opts(opts) + {} + virtual ~function_context() = default; - function_codegen_options opts; + const options_t &opts; }; struct function_generator : scope_generator< function_generator, function_context > diff --git a/include/vast/CodeGen/CodeGenModule.hpp b/include/vast/CodeGen/CodeGenModule.hpp index 9015caf317..ae0a16b28b 100644 --- a/include/vast/CodeGen/CodeGenModule.hpp +++ b/include/vast/CodeGen/CodeGenModule.hpp @@ -17,6 +17,9 @@ VAST_UNRELAX_WARNINGS #include "vast/CodeGen/ScopeContext.hpp" #include "vast/CodeGen/ScopeGenerator.hpp" #include "vast/CodeGen/CodeGenMeta.hpp" +#include "vast/CodeGen/CodeGenOptions.hpp" + +#include "vast/CodeGen/CodeGenFunction.hpp" #include "vast/Dialect/Dialects.hpp" #include "vast/Dialect/Core/CoreAttributes.hpp" @@ -25,20 +28,23 @@ VAST_UNRELAX_WARNINGS namespace vast::cg { - using source_language = core::SourceLanguage; - void set_target_triple(owning_module_ref &mod, std::string triple); void set_source_language(owning_module_ref &mod, source_language lang); + owning_module_ref mk_module(acontext_t &actx, mcontext_t &mctx); + owning_module_ref mk_module_with_attrs(acontext_t &actx, mcontext_t &mctx, source_language lang); + struct module_context : module_scope { explicit module_context( symbol_tables &scopes - , owning_module_ref mod + , const options_t &opts , acontext_t &actx + , mcontext_t &mctx ) : module_scope(scopes) + , opts(opts) , actx(actx) - , mod(std::move(mod)) + , mod(mk_module_with_attrs(actx, mctx, opts.lang)) {} virtual ~module_context() = default; @@ -47,13 +53,12 @@ namespace vast::cg { operation lookup_global(symbol_name name) const; + const options_t &opts; + acontext_t &actx; owning_module_ref mod; }; - owning_module_ref mk_module(acontext_t &actx, mcontext_t &mctx); - owning_module_ref mk_module_with_attrs(acontext_t &actx, mcontext_t &mctx, source_language lang); - struct module_generator : scope_generator< module_generator, module_context > { using base = scope_generator< module_generator, module_context >; @@ -61,12 +66,12 @@ namespace vast::cg { explicit module_generator( acontext_t &actx , mcontext_t &mctx - , source_language lang + , const options_t &opts , codegen_builder &bld , visitor_view visitor , symbol_tables &scopes ) - : base(visitor, bld, scopes, mk_module_with_attrs(actx, mctx, lang), actx) + : base(visitor, bld, scopes, opts, actx, mctx) {} virtual ~module_generator() = default; diff --git a/include/vast/CodeGen/CodeGenOptions.hpp b/include/vast/CodeGen/CodeGenOptions.hpp new file mode 100644 index 0000000000..e29561c16f --- /dev/null +++ b/include/vast/CodeGen/CodeGenOptions.hpp @@ -0,0 +1,21 @@ +// Copyright (c) 2024, Trail of Bits, Inc. + +#pragma once + + +#include "vast/Dialect/Core/CoreAttributes.hpp" + +namespace vast::cg { + + using source_language = core::SourceLanguage; + + struct options_t + { + source_language lang; + unsigned int optimization_level : 2; + + // function emition optionsi + unsigned int has_strict_return : 1; + }; + +} // namespace vast::cg diff --git a/include/vast/CodeGen/ScopeGenerator.hpp b/include/vast/CodeGen/ScopeGenerator.hpp index 3b85c7f8e7..add5b887d7 100644 --- a/include/vast/CodeGen/ScopeGenerator.hpp +++ b/include/vast/CodeGen/ScopeGenerator.hpp @@ -16,10 +16,12 @@ namespace vast::cg { auto & self() { return *static_cast< generator_type * >(this); } - template< typename child_generator > - child_generator &make_child() { + template< typename child_generator, typename ...args_t > + child_generator &make_child(args_t &&...args) { auto &parent = self(); - parent.hook_child(std::make_unique< child_generator >(visitor, bld, &parent)); + parent.hook_child(std::make_unique< child_generator >( + visitor, bld, &parent, std::forward< args_t >(args)... + )); return parent.template last_child< child_generator >(); } diff --git a/lib/vast/CodeGen/CodeGenDriver.cpp b/lib/vast/CodeGen/CodeGenDriver.cpp index 48eec25110..3ef9c7ee8a 100644 --- a/lib/vast/CodeGen/CodeGenDriver.cpp +++ b/lib/vast/CodeGen/CodeGenDriver.cpp @@ -14,6 +14,8 @@ VAST_UNRELAX_WARNINGS #include "vast/CodeGen/UnreachableVisitor.hpp" #include "vast/CodeGen/UnsupportedVisitor.hpp" +#include "vast/CodeGen/CodeGenModule.hpp" + namespace vast::cg { void driver::emit(clang::DeclGroupRef decls) { generator.emit(decls); } void driver::emit(clang::Decl *decl) { generator.emit(decl); } @@ -76,14 +78,27 @@ namespace vast::cg { auto sg = mk_symbol_generator(actx); auto visitor = mk_visitor(vargs, *mctx, *bld, *mg, *sg); + options_t copts = { + .lang = cc::get_source_language(actx.getLangOpts()), + .optimization_level = opts.codegen.OptimizationLevel, + // function emition options + .has_strict_return = opts.codegen.StrictReturn, + }; + return std::make_unique< driver >( - actx, std::move(mctx), std::move(bld), std::move(mg), std::move(sg), std::move(visitor) + actx, + std::move(mctx), + std::move(copts), + std::move(bld), + std::move(mg), + std::move(sg), + std::move(visitor) ); } - module_generator driver::mk_module_generator() { + module_generator driver::mk_module_generator(const options_t &opts) { return module_generator( - actx, *mctx, cc::get_source_language(actx.getLangOpts()), *bld, visitor_view(*visitor), scopes + actx, *mctx, opts, *bld, visitor_view(*visitor), scopes ); } diff --git a/lib/vast/CodeGen/CodeGenModule.cpp b/lib/vast/CodeGen/CodeGenModule.cpp index 98a2ef3a1b..ed0c4b0f23 100644 --- a/lib/vast/CodeGen/CodeGenModule.cpp +++ b/lib/vast/CodeGen/CodeGenModule.cpp @@ -113,7 +113,7 @@ namespace vast::cg } void module_generator::emit(clang::FunctionDecl *decl) { - auto &fg = make_child< function_generator >(); + auto &fg = make_child< function_generator >(opts); fg.do_emit(mod->getBodyRegion(), decl); }