Skip to content

Commit

Permalink
cg: Pass options into driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
xlauko committed Mar 24, 2024
1 parent 3d27700 commit ab3e6a8
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 26 deletions.
8 changes: 6 additions & 2 deletions include/vast/CodeGen/CodeGenDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,20 @@ 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
, std::unique_ptr< visitor_base > visitor
)
: 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);
Expand All @@ -73,6 +75,8 @@ namespace vast::cg {
acontext_t &actx;
std::unique_ptr< mcontext_t > mctx;

[[maybe_unused]] options_t opts;

symbol_tables scopes;

//
Expand All @@ -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;
};

Expand Down
14 changes: 6 additions & 8 deletions include/vast/CodeGen/CodeGenFunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,26 @@

#include "vast/CodeGen/ScopeContext.hpp"
#include "vast/CodeGen/ScopeGenerator.hpp"
#include "vast/CodeGen/CodeGenOptions.hpp"

#include "vast/Dialect/HighLevel/HighLevelOps.hpp"

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 >
Expand Down
23 changes: 14 additions & 9 deletions include/vast/CodeGen/CodeGenModule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
Expand All @@ -47,26 +53,25 @@ 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 >;

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;
Expand Down
21 changes: 21 additions & 0 deletions include/vast/CodeGen/CodeGenOptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2024, Trail of Bits, Inc.

Check notice on line 1 in include/vast/CodeGen/CodeGenOptions.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter (17, 22.04)

Run clang-format on include/vast/CodeGen/CodeGenOptions.hpp

File include/vast/CodeGen/CodeGenOptions.hpp does not conform to Custom style guidelines. (lines 3)

#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
8 changes: 5 additions & 3 deletions include/vast/CodeGen/ScopeGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 >();
}

Expand Down
21 changes: 18 additions & 3 deletions lib/vast/CodeGen/CodeGenDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down Expand Up @@ -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
);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/vast/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit ab3e6a8

Please sign in to comment.