Skip to content

Commit

Permalink
cc: Decouple action and consumer.
Browse files Browse the repository at this point in the history
  • Loading branch information
xlauko committed Oct 25, 2023
1 parent 4df1ffb commit be0de72
Show file tree
Hide file tree
Showing 6 changed files with 452 additions and 402 deletions.
44 changes: 12 additions & 32 deletions include/vast/Frontend/Action.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ VAST_RELAX_WARNINGS
VAST_UNRELAX_WARNINGS

#include "vast/Util/Common.hpp"
#include "vast/Frontend/FrontendAction.hpp"
#include "vast/Frontend/CompilerInstance.hpp"
#include "vast/CodeGen/Generator.hpp"
#include "vast/Frontend/FrontendAction.hpp"
#include "vast/Frontend/Options.hpp"

namespace llvm {
class LLVMIRContext;
Expand All @@ -25,37 +25,17 @@ namespace mlir {

namespace vast::cc {

namespace opt {
constexpr string_ref emit_llvm = "emit-llvm";
constexpr string_ref emit_obj = "emit-obj";
constexpr string_ref emit_asm = "emit-asm";

constexpr string_ref emit_mlir = "emit-mlir";

constexpr string_ref emit_locs = "emit-locs";

constexpr string_ref opt_pipeline = "pipeline";

constexpr string_ref disable_vast_verifier = "disable-vast-verifier";
constexpr string_ref vast_verify_diags = "verify-diags";
constexpr string_ref disable_emit_cxx_default = "disable-emit-cxx-default";

bool emit_only_mlir(const vast_args &vargs);
bool emit_only_llvm(const vast_args &vargs);

} // namespace opt

struct vast_gen_consumer;
struct vast_consumer;

struct vast_gen_action : frontend_action {
virtual ~vast_gen_action() = default;
struct vast_action : frontend_action {
virtual ~vast_action() = default;

vast_gen_consumer *consumer;
vast_consumer *consumer;
output_type action;

protected:

vast_gen_action(output_type action, const vast_args &vargs);
vast_action(output_type action, const vast_args &vargs);

std::unique_ptr< clang::ASTConsumer >
CreateASTConsumer(compiler_instance &ci, string_ref input) override;
Expand All @@ -65,7 +45,7 @@ namespace vast::cc {
void EndSourceFileAction() override;

private:
friend struct vast_gen_consumer;
friend struct vast_consumer;

owning_module_ref mlir_module;

Expand All @@ -79,7 +59,7 @@ namespace vast::cc {
//
// Emit assembly
//
struct emit_assembly_action : vast_gen_action {
struct emit_assembly_action : vast_action {
explicit emit_assembly_action(const vast_args &vargs);
private:
virtual void anchor();
Expand All @@ -88,7 +68,7 @@ namespace vast::cc {
//
// Emit LLVM
//
struct emit_llvm_action : vast_gen_action {
struct emit_llvm_action : vast_action {
explicit emit_llvm_action(const vast_args &vargs);
private:
virtual void anchor();
Expand All @@ -97,7 +77,7 @@ namespace vast::cc {
//
// Emit MLIR
//
struct emit_mlir_action : vast_gen_action {
struct emit_mlir_action : vast_action {
explicit emit_mlir_action(const vast_args &vargs);
private:
virtual void anchor();
Expand All @@ -106,7 +86,7 @@ namespace vast::cc {
//
// Emit obj
//
struct emit_obj_action : vast_gen_action {
struct emit_obj_action : vast_action {
explicit emit_obj_action(const vast_args &vargs);
private:
virtual void anchor();
Expand Down
113 changes: 113 additions & 0 deletions include/vast/Frontend/Consumer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright (c) 2023-present, Trail of Bits, Inc.

#pragma once

#include "vast/Util/Warnings.hpp"

VAST_RELAX_WARNINGS
#include <clang/AST/ASTConsumer.h>
#include <clang/CodeGen/BackendUtil.h>
VAST_UNRELAX_WARNINGS

#include "vast/CodeGen/Generator.hpp"
#include "vast/Frontend/Diagnostics.hpp"
#include "vast/Frontend/FrontendAction.hpp"
#include "vast/Frontend/Options.hpp"

namespace vast::cc {

using virtual_file_system_ptr = llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem >;

using output_stream_ptr = std::unique_ptr< llvm::raw_pwrite_stream >;

using clang_ast_consumer = clang::ASTConsumer;

// TODO: Introduce helper wrapper on top of `vast_args`?
enum class target_dialect { high_level, low_level, llvm };

using backend = clang::BackendAction;

struct vast_consumer : clang_ast_consumer
{
using vast_generator_ptr = std::unique_ptr< cg::vast_generator >;

vast_consumer(
output_type act, diagnostics_engine &diags, virtual_file_system_ptr vfs,
header_search_options &hopts, codegen_options &copts, target_options &topts,
language_options &lopts,
// frontend_options &fopts,
const vast_args &vargs, output_stream_ptr os
)
: action(act)
, diags(diags)
, vfs(vfs)
, header_search_opts(hopts)
, codegen_opts(copts)
, target_opts(topts)
, lang_opts(lopts)
// , frontend_opts(fopts)
, vargs(vargs)
, output_stream(std::move(os))
, generator(std::make_unique< cg::vast_generator >(diags, codegen_opts, lang_opts)
) {}

void Initialize(acontext_t &ctx) override;

bool HandleTopLevelDecl(clang::DeclGroupRef decls) override;

void HandleCXXStaticMemberVarInstantiation(clang::VarDecl * /* decl */) override;

void HandleInlineFunctionDefinition(clang::FunctionDecl * /* decl */) override;

void HandleInterestingDecl(clang::DeclGroupRef /* decl */) override;

void HandleTranslationUnit(acontext_t &acontext) override;

void HandleTagDeclDefinition(clang::TagDecl *decl) override;

// void HandleTagDeclRequiredDefinition(clang::TagDecl */* decl */) override {
// VAST_UNIMPLEMENTED;
// }

void CompleteTentativeDefinition(clang::VarDecl *decl) override;

void CompleteExternalDeclaration(clang::VarDecl * /* decl */) override;

void AssignInheritanceModel(clang::CXXRecordDecl * /* decl */) override;

void HandleVTable(clang::CXXRecordDecl * /* decl */) override;

private:

void emit_backend_output(
backend backend_action, owning_module_ref mlir_module, mcontext_t *mctx
);

void emit_mlir_output(target_dialect target, owning_module_ref mod, mcontext_t *mctx);

void compile_via_vast(vast_module mod, mcontext_t *mctx);

virtual void anchor() {}

output_type action;

diagnostics_engine &diags;

virtual_file_system_ptr vfs;

// options
const header_search_options &header_search_opts;
const codegen_options &codegen_opts;
const target_options &target_opts;
const language_options &lang_opts;
// const frontend_options &frontend_opts;

const vast_args &vargs;

output_stream_ptr output_stream;

acontext_t *acontext = nullptr;

vast_generator_ptr generator;
};
} // namespace vast::cc
19 changes: 19 additions & 0 deletions include/vast/Frontend/Options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,23 @@ namespace vast::cc

std::pair< vast_args, argv_storage > filter_args(const argv_storage_base &args);

namespace opt {
constexpr string_ref emit_llvm = "emit-llvm";
constexpr string_ref emit_obj = "emit-obj";
constexpr string_ref emit_asm = "emit-asm";

constexpr string_ref emit_mlir = "emit-mlir";

constexpr string_ref emit_locs = "emit-locs";

constexpr string_ref opt_pipeline = "pipeline";

constexpr string_ref disable_vast_verifier = "disable-vast-verifier";
constexpr string_ref vast_verify_diags = "verify-diags";
constexpr string_ref disable_emit_cxx_default = "disable-emit-cxx-default";

bool emit_only_mlir(const vast_args &vargs);
bool emit_only_llvm(const vast_args &vargs);
} // namespace opt

} // namespace vast::cc
Loading

0 comments on commit be0de72

Please sign in to comment.