diff --git a/lib/Compiler.cpp b/lib/Compiler.cpp index 579a8a1..d279818 100644 --- a/lib/Compiler.cpp +++ b/lib/Compiler.cpp @@ -1,4 +1,5 @@ #include "Compiler.hpp" +#include #include #include #include @@ -68,11 +69,17 @@ namespace wg } llvm::verifyModule(*m_module); + pass.run(*m_module); dest.flush(); } + void Compiler::init_package(std::shared_ptr node) + { + m_pkg->scan(node, *m_sym); + } + llvm::Value* Compiler::compile(std::shared_ptr node) { switch (node->type()) @@ -94,11 +101,21 @@ namespace wg return nullptr; } break; + case NODE_DIR: { + if (node->child(0)->repr() == "import") + { + std::string pkg_name = node->child(1)->repr(); + std::cout << "-> " << pkg_name << std::endl; + } + + return nullptr; + } break; + case NODE_RETURN: { return m_builder->CreateRet(compile(node->child(0))); } break; - case NODE_EXTERN: { + /*case NODE_EXTERN: { auto ident = node->child(0)->repr(); auto params = node->child(1); auto ret = node->child(2); @@ -143,53 +160,13 @@ namespace wg m_sym->declare_prototype(ident, fun_type, node->loc()); return fun; - } break; + } break;*/ case NODE_FUNDECL: { auto ident = node->child(0)->repr(); - auto params = node->child(1); - auto ret = node->child(2); - - std::vector names; - std::vector types; - - for (size_t i=0; isize(); i++) - { - auto param = params->child(i); - - if (param->type() == NODE_IDENT) - { - names.push_back(param->repr()); - } - else if (param->type() == NODE_TYPE) - { - auto ty = llvm::Type::getInt32Ty(*m_context); - - for (auto name: names) - { - m_sym->declare(name, ty, node->loc()); - types.push_back(ty); - } - - names.clear(); - } - } - - llvm::Type* ret_type = llvm::Type::getVoidTy(*m_context); auto body = node->child(3); + auto fun = m_module-> getFunction(ident); - if (ret->size() > 0) - { - ret_type = llvm::Type::getInt32Ty(*m_context); - } - - auto fun_type = llvm::FunctionType::get(ret_type, types, false); - auto fun = llvm::Function::Create(fun_type, - llvm::Function::ExternalLinkage, - ident, - *m_module); - - m_sym->declare(ident, fun_type, node->loc()); llvm::BasicBlock* old_bb = m_builder->GetInsertBlock(); diff --git a/lib/Compiler.hpp b/lib/Compiler.hpp index 001e789..5e1d5fd 100644 --- a/lib/Compiler.hpp +++ b/lib/Compiler.hpp @@ -8,6 +8,7 @@ #include "commons.hpp" #include "Node.hpp" #include "SymTable.hpp" +#include "Package.hpp" namespace wg { @@ -18,7 +19,7 @@ namespace wg virtual ~Compiler(); void gen(std::filesystem::path obj); - + void init_package(std::shared_ptr node); llvm::Value* compile(std::shared_ptr node); private: std::unique_ptr m_context = @@ -31,6 +32,8 @@ namespace wg std::make_unique("my module", *m_context); std::unique_ptr m_sym = std::make_unique(); + std::unique_ptr m_pkg = std::make_unique(*m_context, + *m_module); }; } diff --git a/lib/Package.cpp b/lib/Package.cpp new file mode 100644 index 0000000..3d253ad --- /dev/null +++ b/lib/Package.cpp @@ -0,0 +1,81 @@ +#include +#include + +#include "Package.hpp" + +namespace wg +{ + /*explicit*/ Package::Package(llvm::LLVMContext& context, + llvm::Module& mod) + : m_context { context } + , m_module { mod } + { + } + + /*virtual*/ Package::~Package() + { + } + + void Package::scan(std::shared_ptr node, SymTable& sym) + { + switch (node->type()) + { + case NODE_DIR: { + if (node->child(0)->repr() == "package") + { + m_name = node->child(1)->repr(); + } + } break; + + case NODE_FUNDECL: { + std::string name = node->child(0)->repr(); + std::vector params; + std::vector> buffer; + + for (size_t i=0; ichild(1)->size(); i++) + { + auto param = node->child(1)->child(i); + + if (param->type() == NODE_IDENT) + { + buffer.push_back(param); + } + else if (param->type() == NODE_TYPE) + { + for (auto p: buffer) + { + params.push_back((llvm::Type*) + llvm::Type::getInt32Ty(m_context)); + } + + buffer.clear(); + } + } + + auto* ret = llvm::Type::getVoidTy(m_context); + + if (node->child(2)->size() > 0) + { + ret = (llvm::Type*) llvm::Type::getInt32Ty(m_context); + } + + auto* fun_ty = llvm::FunctionType::get(ret, params, false); + + sym.declare_prototype(name, fun_ty, node->loc()); + + llvm::Function::Create(fun_ty, + llvm::Function::ExternalLinkage, + name, + m_module); + + } break; + + default: { + for (size_t i=0; isize(); i++) + { + scan(node->child(i), sym); + } + } break; + } + } +} diff --git a/lib/Package.hpp b/lib/Package.hpp new file mode 100644 index 0000000..99d7a73 --- /dev/null +++ b/lib/Package.hpp @@ -0,0 +1,28 @@ +#ifndef wg_PACKAGE_HPP +#define wg_PACKAGE_HPP + +#include "commons.hpp" + +#include "SymTable.hpp" +#include "Node.hpp" +#include + +namespace wg +{ + class Package + { + public: + explicit Package(llvm::LLVMContext& context, + llvm::Module& mod); + virtual ~Package(); + + void scan(std::shared_ptr node, SymTable& sym); + + private: + llvm::LLVMContext& m_context; + llvm::Module& m_module; + std::string m_name; + }; +} + +#endif diff --git a/meson.build b/meson.build index 71608b3..ece59ac 100644 --- a/meson.build +++ b/meson.build @@ -7,6 +7,14 @@ project('wongola', 'cpp_std=c++17' ]) +shared_library( + 'wongola-std', + sources: [ + 'std/io.cpp' + ], + install: true +) + wongola_lib = static_library( 'wongola', sources: [ @@ -16,6 +24,7 @@ wongola_lib = static_library( 'lib/Compiler.cpp', 'lib/Loc.cpp', 'lib/SymTable.cpp', + 'lib/Package.cpp', ], dependencies: [ dependency('LLVM') diff --git a/src/main.cpp b/src/main.cpp index 86f9174..be68271 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include std::filesystem::path name_to_obj(std::string const& path) { @@ -41,6 +42,7 @@ void load(std::string const& path) auto ast = parser.parse(tokens); wg::Compiler compiler; + compiler.init_package(ast); compiler.compile(ast); compiler.gen(name_to_obj(path)); } @@ -97,8 +99,9 @@ int main(int argc, char** argv) std::stringstream ss; ss << "clang++" << " -o " << output_path << " "; - ss << objname; + ss << objname; + //ss << " -lwongola-std "; system(ss.str().c_str()); std::filesystem::remove(objname); diff --git a/std/io.cpp b/std/io.cpp new file mode 100644 index 0000000..58e54d7 --- /dev/null +++ b/std/io.cpp @@ -0,0 +1,10 @@ +#include "io.hpp" +#include + +extern "C" void hello(int x) +{ + for (int i=0; i