Skip to content

Commit

Permalink
[cpp] skeleton for codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
harrand committed May 16, 2024
1 parent 56bb2d0 commit e31bd01
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
2 changes: 2 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ add_executable(psyc
src/ast.hpp
src/builtin.cpp
src/builtin.hpp
src/codegen.cpp
src/codegen.hpp
src/config.hpp
src/diag.hpp
src/error.hpp
Expand Down
53 changes: 53 additions & 0 deletions cpp/src/codegen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "codegen.hpp"
#include "diag.hpp"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/LLVMContext.h"

#include "llvm/ADT/APInt.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/TargetParser/Host.h"
#include "llvm/MC/TargetRegistry.h"

namespace code
{
std::string output::dump_ir() const
{
if(this->codegen_handle == nullptr)
{
return "<no output>";
}
std::string ir_string;
llvm::raw_string_ostream os{ir_string};
reinterpret_cast<llvm::Module*>(this->codegen_handle)->print(os, nullptr);
return ir_string;
}

std::string output::get_output_filename() const
{
return this->module_name + ".o";
}

void output::write_to_object_file(std::filesystem::path output_dir)
{
std::string object_filename = (output_dir / this->get_output_filename()).string();

diag::error(error_code::nyi, "writing to object file is not yet implemented. i was told to write to \"{}\"", object_filename);
}

output generate(const semal::output& input, std::string module_name)
{
return
{
.codegen_handle = nullptr,
.module_name = module_name
};
}
}
24 changes: 24 additions & 0 deletions cpp/src/codegen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef PSYC_CODEGEN_HPP
#define PSYC_CODEGEN_HPP
#include "semal.hpp"

namespace code
{
struct output
{
void* codegen_handle = nullptr;
std::string module_name;
std::string dump_ir() const;
std::string get_output_filename() const;
void write_to_object_file(std::filesystem::path output_dir);
};

output generate(const semal::output& input, std::string module_name = "<unnamed_module>");

struct state
{
std::unordered_map<std::filesystem::path, output> codegend_input_files = {};
};
}

#endif // PSYC_CODEGEN_HPP
20 changes: 15 additions & 5 deletions cpp/src/psyc_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#include "lex.hpp"
#include "parse.hpp"
#include "semal.hpp"
#include "codegen.hpp"
#include "timer.hpp"
#include "diag.hpp"
#include "type.hpp"
#include <filesystem>
#include <span>
#include <string_view>
Expand Down Expand Up @@ -89,14 +89,24 @@ int main(int argc, char** argv)
t.semal = timer::elapsed_millis();

// codegen
timer::start();
code::state codegen;
for(const auto& [input_file, semantic_output] : semal.analysed_input_files)
{
codegen.codegend_input_files[input_file] = code::generate(semantic_output, input_file.stem().string());
if(args.should_dump_ir)
{
std::cout << "==========================\n";
std::cout << "ir for " << input_file << ":\n";
std::cout << codegen.codegend_input_files[input_file].dump_ir();
std::cout << "\n==========================\n\n";
}
}

t.codegen = timer::elapsed_millis();
// link

t.print();

type ty = type::from_primitive(primitive_type::u8);
ty = ty.pointer_to().pointer_to(qualifier_const);
std::cout << "type: \n" << ty.name();
return 0;
}

Expand Down

0 comments on commit e31bd01

Please sign in to comment.