Skip to content

Commit

Permalink
[cpp] writing to object files wip
Browse files Browse the repository at this point in the history
  • Loading branch information
harrand committed May 21, 2024
1 parent 7439f7d commit 4494344
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
33 changes: 30 additions & 3 deletions cpp/src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,38 @@ namespace code
return this->module_name + ".o";
}

void output::write_to_object_file(std::filesystem::path output_dir) const
void output::write_to_object_file(const build::info& binfo) const
{
std::string object_filename = (output_dir / this->get_output_filename()).string();
std::string object_filename = (binfo.compiler_args.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);
std::string error;
const llvm::Target* target = llvm::TargetRegistry::lookupTarget(binfo.target_triple, error);
if(target == nullptr)
{
diag::error(error_code::codegen, "error while retrieving LLVM output target information(s): {}", error);
}
const char* cpu = "generic";
const char* features = "";
llvm::TargetOptions opt;
auto target_machine = target->createTargetMachine(binfo.target_triple, cpu, features, opt, llvm::Reloc::PIC_);
// configure module (no i have no idea whats going on).
auto* program = static_cast<llvm::Module*>(this->codegen_handle);
program->setDataLayout(target_machine->createDataLayout());
program->setTargetTriple(binfo.target_triple);
std::error_code ec;
llvm::raw_fd_ostream dst(object_filename, ec, llvm::sys::fs::OF_None);
if(ec)
{
diag::error(error_code::codegen, "error while generating object files: {}", ec.message());
}
llvm::legacy::PassManager pass;
auto file_type = llvm::CodeGenFileType::ObjectFile;
if(target_machine->addPassesToEmitFile(pass, dst, nullptr, file_type, false))
{
diag::error(error_code::codegen, "target machine cannot emit a file of this type.");
}
pass.run(*program);
dst.flush();
}

// global state:
Expand Down
3 changes: 2 additions & 1 deletion cpp/src/codegen.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef PSYC_CODEGEN_HPP
#define PSYC_CODEGEN_HPP
#include "semal.hpp"
#include "build.hpp"

namespace code
{
Expand All @@ -10,7 +11,7 @@ namespace code
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) const;
void write_to_object_file(const build::info& binfo) const;
};

// initialise and terminate *once*, at the beginning and end of the compiler program respectively.
Expand Down
12 changes: 5 additions & 7 deletions cpp/src/psyc_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,29 +109,27 @@ int main(int argc, char** argv)
// codegen
timer::start();
code::state codegen;
link::state link;
for(const auto& [input_file, semantic_output] : semal.analysed_input_files)
{
codegen.codegend_input_files[input_file] = code::generate(parse.parsed_input_files[input_file], semantic_output, input_file.string());
codegen.codegend_input_files[input_file].write_to_object_file(binfo);
link.input_output_files[input_file] = codegen.codegend_input_files[input_file].get_output_filename();

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();
code::cleanup();
codegen.codegend_input_files[input_file].codegen_handle = nullptr;
std::cout << "\n==========================\n\n";
}
code::cleanup();
}

t.codegen = timer::elapsed_millis();
// link
timer::start();
link::state link;
for(const auto& [input_file, codegen_output] : codegen.codegend_input_files)
{
codegen_output.write_to_object_file(binfo.compiler_args.output_dir);
link.input_output_files[input_file] = codegen_output.get_output_filename();
}
link.build(binfo);

t.link = timer::elapsed_millis();
Expand Down

0 comments on commit 4494344

Please sign in to comment.