Skip to content

Commit

Permalink
[cpp] added builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
harrand committed May 15, 2024
1 parent d38ce66 commit 3333842
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 8 deletions.
2 changes: 2 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ add_executable(psyc
src/psyc_main.cpp
src/ast.cpp
src/ast.hpp
src/builtin.cpp
src/builtin.hpp
src/config.hpp
src/diag.hpp
src/error.hpp
Expand Down
1 change: 1 addition & 0 deletions cpp/src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ struct ast
std::vector<variable_declaration> params = {};
std::string ret_type;
bool is_extern = false;
bool is_builtin = false;
constexpr std::string to_string() const
{
std::string params_str = "(";
Expand Down
61 changes: 61 additions & 0 deletions cpp/src/builtin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "builtin.hpp"
#include <array>

std::array<const char*, (int)builtin::_count> builtin_names
{
"malloc",
"free"
};

std::array<semal::function_t, (int)builtin::_count> builtin_functions
{
semal::function_t
{
.return_ty = type::from_primitive(primitive_type::i8).pointer_to(),
.name = "malloc",
.params =
{
semal::local_variable_t
{
.ty = type::from_primitive(primitive_type::u64),
.name = "size_bytes",
.ctx = {},
}
},
.ctx = {},
.is_method = false
},
semal::function_t
{
.return_ty = type::from_primitive(primitive_type::u0),
.name = "free",
.params =
{
semal::local_variable_t
{
.ty = type::from_primitive(primitive_type::i8).pointer_to(),
.name = "ptr",
.ctx = {},
}
},
.ctx = {},
.is_method = false
},
};

builtin try_find_builtin(std::string_view funcname)
{
for(int i = 0; i < (int)builtin::_count; i++)
{
if(std::format("__builtin_{}", builtin_names[i]) == funcname)
{
return (builtin)i;
}
}
return builtin::_undefined;
}

semal::function_t& get_builtin_function(builtin b)
{
return builtin_functions[(int)b];
}
17 changes: 17 additions & 0 deletions cpp/src/builtin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef PSYC_BUILTIN_HPP
#define PSYC_BUILTIN_HPP
#include "ast.hpp"
#include "semal.hpp"

enum class builtin
{
malloc,
free,
_count,
_undefined,
};

// return builtin::_undefined if funcname doesnt correspond to a builtin.
builtin try_find_builtin(std::string_view funcname);
semal::function_t& get_builtin_function(builtin b);
#endif // PSYC_BUILTIN_HPP
16 changes: 9 additions & 7 deletions cpp/src/semal.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "semal.hpp"
#include "type.hpp"
#include "builtin.hpp"
#include "util.hpp"

namespace semal
Expand Down Expand Up @@ -156,6 +157,11 @@ namespace semal
return &funcdata;
}
}
builtin maybe_builtin = try_find_builtin(name);
if(maybe_builtin != builtin::_undefined)
{
return &get_builtin_function(maybe_builtin);
}
return nullptr;
}

Expand Down Expand Up @@ -231,13 +237,14 @@ namespace semal
return nullptr;
}

output analyse_predecl(const ast& tree)
output analyse_predecl(ast tree)
{
output ret;
auto semal_assert = [&tree](ast::path_t path, bool expr, const char* fmt, auto... ts)
{
context{&tree, path}.semal_assert(expr, fmt, ts...);
};

for(std::size_t i = 0; i < tree.root.children.size(); i++)
{
const ast::node& node = tree.root.children[i];
Expand Down Expand Up @@ -569,12 +576,7 @@ namespace semal
type function_call(const data& d, const ast::function_call& payload)
{
const function_t* maybe_function = d.state.try_find_function(payload.function_name.c_str());
if(maybe_function == nullptr)
{
d.assert_that(payload.function_name.starts_with("__builtin_"), std::format("call to undeclared function \"{}\"", payload.function_name));
diag::note("detected call to builtin function \"{}\"", payload.function_name);
return type::undefined();
}
d.assert_that(maybe_function != nullptr, std::format("call to undeclared function \"{}\"", payload.function_name));
return maybe_function->return_ty;
}

Expand Down
2 changes: 1 addition & 1 deletion cpp/src/semal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace semal
type get_type_from_payload(const ast::node::payload_t& payload, const ast& tree, const ast::path_t& path) const;
};

output analyse_predecl(const ast& tree);
output analyse_predecl(ast tree);
output analyse_full(const ast& tree, output predecl = {});

struct state
Expand Down
1 change: 1 addition & 0 deletions cpp/src/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ type type::dereference() const

type type::pointer_to(type_qualifier quals) const
{
diag::assert_that(!this->is_undefined(), error_code::type, "attempt to get pointer to <undefined> type.");
return
{
.ty = util::box<type>{*this},
Expand Down
1 change: 1 addition & 0 deletions samples/scratchpad.psy
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mydata :: struct
{
member : i64 := 5;
}
__builtin_malloc(123);

putchar :: (ch : i8) -> u0 := extern;
dub :: (val : i64) -> i64
Expand Down

0 comments on commit 3333842

Please sign in to comment.