ADD: pkg first pass.
parent
506961fff5
commit
f7857a5d4b
|
@ -1,4 +1,5 @@
|
|||
#include "Compiler.hpp"
|
||||
#include <llvm/Support/DynamicLibrary.h>
|
||||
#include <llvm/IR/BasicBlock.h>
|
||||
#include <llvm/IR/Verifier.h>
|
||||
#include <llvm/IR/Type.h>
|
||||
|
@ -68,11 +69,17 @@ namespace wg
|
|||
}
|
||||
|
||||
llvm::verifyModule(*m_module);
|
||||
|
||||
pass.run(*m_module);
|
||||
|
||||
dest.flush();
|
||||
}
|
||||
|
||||
void Compiler::init_package(std::shared_ptr<Node> node)
|
||||
{
|
||||
m_pkg->scan(node, *m_sym);
|
||||
}
|
||||
|
||||
llvm::Value* Compiler::compile(std::shared_ptr<Node> 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<std::string> names;
|
||||
std::vector<llvm::Type*> types;
|
||||
|
||||
for (size_t i=0; i<params->size(); 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();
|
||||
|
||||
|
|
|
@ -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> node);
|
||||
llvm::Value* compile(std::shared_ptr<Node> node);
|
||||
private:
|
||||
std::unique_ptr<llvm::LLVMContext> m_context =
|
||||
|
@ -31,6 +32,8 @@ namespace wg
|
|||
std::make_unique<llvm::Module>("my module", *m_context);
|
||||
|
||||
std::unique_ptr<SymTable> m_sym = std::make_unique<SymTable>();
|
||||
std::unique_ptr<Package> m_pkg = std::make_unique<Package>(*m_context,
|
||||
*m_module);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
#include <llvm/IR/Value.h>
|
||||
#include <llvm/IR/Function.h>
|
||||
|
||||
#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> 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<llvm::Type*> params;
|
||||
std::vector<std::shared_ptr<Node>> buffer;
|
||||
|
||||
for (size_t i=0; i<node->child(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; i<node->size(); i++)
|
||||
{
|
||||
scan(node->child(i), sym);
|
||||
}
|
||||
} break;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
#ifndef wg_PACKAGE_HPP
|
||||
#define wg_PACKAGE_HPP
|
||||
|
||||
#include "commons.hpp"
|
||||
|
||||
#include "SymTable.hpp"
|
||||
#include "Node.hpp"
|
||||
#include <llvm/IR/LLVMContext.h>
|
||||
|
||||
namespace wg
|
||||
{
|
||||
class Package
|
||||
{
|
||||
public:
|
||||
explicit Package(llvm::LLVMContext& context,
|
||||
llvm::Module& mod);
|
||||
virtual ~Package();
|
||||
|
||||
void scan(std::shared_ptr<Node> node, SymTable& sym);
|
||||
|
||||
private:
|
||||
llvm::LLVMContext& m_context;
|
||||
llvm::Module& m_module;
|
||||
std::string m_name;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
|
@ -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')
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <Lexer.hpp>
|
||||
#include <Parser.hpp>
|
||||
#include <Compiler.hpp>
|
||||
#include <llvm/Support/DynamicLibrary.h>
|
||||
|
||||
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);
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
#include "io.hpp"
|
||||
#include <iostream>
|
||||
|
||||
extern "C" void hello(int x)
|
||||
{
|
||||
for (int i=0; i<x; i++)
|
||||
{
|
||||
std::cout << "hello ! " << std::endl;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
#ifndef IO_HPP
|
||||
#define IO_HPP
|
||||
|
||||
|
||||
extern "C" void hello(int x);
|
||||
|
||||
#endif
|
Reference in New Issue