Skip to content

Commit

Permalink
Fix a deserialization bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
asoffer committed Nov 17, 2023
1 parent 7300425 commit 15eb4c8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 56 deletions.
105 changes: 50 additions & 55 deletions ir/builtin_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "type/type.h"

namespace ic {

namespace {
nth::NoDestructor<IrFunction> Function([] {
IrFunction f(1, 1);
f.append<TypeKind>();
Expand Down Expand Up @@ -69,57 +69,60 @@ nth::NoDestructor<IrFunction> ForeignType([] {
return f;
}());

nth::NoDestructor<std::vector<std::string>> BuiltinNamesImpl;

} // namespace

std::span<std::string const> BuiltinNames() { return *BuiltinNamesImpl; }

Module BuiltinModule() {
uint32_t next_id = 0;

Module m;
m.Insert(
Identifier("opaque"),
{.qualified_type = type::QualifiedType::Constant(type::Function(
type::Parameters(std::vector<type::ParametersType::Parameter>{}),
{type::Type_})),
.value = {jasmin::Value(&*Opaque)}});
global_function_registry.Register(
FunctionId(ModuleId::Builtin(), LocalFunctionId(next_id++)), &*Opaque);

m.Insert(
Identifier("arguments"),
{.qualified_type = type::QualifiedType::Constant(type::Function(
type::Parameters(std::vector<type::ParametersType::Parameter>{}),
{type::Slice(type::Slice(type::Char))})),
.value = {jasmin::Value(&*Arguments)}});
global_function_registry.Register(
FunctionId(ModuleId::Builtin(), LocalFunctionId(next_id++)), &*Arguments);

m.Insert(Identifier("ascii_encode"),
{.qualified_type = type::QualifiedType::Constant(type::Function(
type::Parameters(std::vector<type::ParametersType::Parameter>{
{.type = type::U8}}),
{type::Char})),
.value = {jasmin::Value(&*AsciiEncodeFn)}});
global_function_registry.Register(
FunctionId(ModuleId::Builtin(), LocalFunctionId(next_id++)),
&*AsciiEncodeFn);

m.Insert(Identifier("ascii_decode"),
{.qualified_type = type::QualifiedType::Constant(type::Function(
type::Parameters(std::vector<type::ParametersType::Parameter>{
{.type = type::Char}}),
{type::U8})),
.value = {jasmin::Value(&*AsciiDecodeFn)}});
global_function_registry.Register(
FunctionId(ModuleId::Builtin(), LocalFunctionId(next_id++)),
&*AsciiDecodeFn);

m.Insert(Identifier("slice"),
{.qualified_type = type::QualifiedType::Constant(type::Function(
type::Parameters(std::vector<type::ParametersType::Parameter>{
{.type = type::BufPtr(type::Char)}, {.type = type::U64}}),
{type::Slice(type::Char)})),
.value = {jasmin::Value(&*Slice)}});
global_function_registry.Register(
FunctionId(ModuleId::Builtin(), LocalFunctionId(next_id++)), &*Slice);
auto Register = [&](std::string_view name, type::Type t,
IrFunction const& f) {
m.Insert(Identifier(name),
{.qualified_type = type::QualifiedType::Constant(t),
.value = {jasmin::Value(&f)}});
global_function_registry.Register(
FunctionId(ModuleId::Builtin(), LocalFunctionId(next_id++)), &f);
BuiltinNamesImpl->emplace_back(name);
};

Register("opaque",
type::Function(
type::Parameters(std::vector<type::ParametersType::Parameter>{}),
{type::Type_}),
*Opaque);

Register("arguments",
type::Function(
type::Parameters(std::vector<type::ParametersType::Parameter>{}),
{type::Slice(type::Slice(type::Char))}),
*Arguments);

Register("ascii_encode",
type::Function(
type::Parameters(std::vector<type::ParametersType::Parameter>{
{.type = type::U8}}),
{type::Char}),
*AsciiEncodeFn);

Register("ascii_decode",
type::Function(
type::Parameters(std::vector<type::ParametersType::Parameter>{
{.type = type::Char}}),
{type::U8}),
*AsciiDecodeFn);

Register("slice",
type::Function(
type::Parameters(std::vector<type::ParametersType::Parameter>{
{.type = type::BufPtr(type::Char)}, {.type = type::U64}}),
{type::Slice(type::Char)}),
*Slice);

// TODO: There's something wrong with registration happening after this point.
m.Insert(
Identifier("foreign"),
{.qualified_type = type::QualifiedType::Constant(type::GenericFunction(
Expand All @@ -129,17 +132,9 @@ Module BuiltinModule() {
FunctionId(ModuleId::Builtin(), LocalFunctionId(next_id++)),
&*ForeignType);

// TODO: There's something wrong with registration happening after this point.
global_function_registry.Register(
FunctionId(ModuleId::Builtin(), LocalFunctionId(next_id++)), &*Foreign);

m.Insert(Identifier("function"),
{.qualified_type =
type::QualifiedType::Constant(type::Pattern(type::Type_)),
.value = {jasmin::Value(&*Function)}});
global_function_registry.Register(
FunctionId(ModuleId::Builtin(), LocalFunctionId(next_id++)), &*Function);

return m;
}

Expand Down
5 changes: 5 additions & 0 deletions ir/builtin_module.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#ifndef ICARUS_IR_BUILTIN_MODULE_H
#define ICARUS_IR_BUILTIN_MODULE_H

#include <span>
#include <string_view>

#include "ir/module.h"

namespace ic {

Module BuiltinModule();

std::span<std::string const> BuiltinNames();

} // namespace ic

#endif // ICARUS_IR_BUILTIN_MODULE_H
2 changes: 1 addition & 1 deletion ir/deserialize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ bool Deserializer::DeserializeFunction(ModuleProto const& module_proto,
NTH_REQUIRE((v.debug), builtin_module_ != nullptr);
// TODO: Are local functions the same as module symbols?
auto entry = builtin_module_->Lookup(
Identifier(function_id.local_function().value()));
Identifier(BuiltinNames()[function_id.local_function().value()]));
if (entry.qualified_type.type() == type::Error) { return false; }
if (entry.value.size() != 1) { return false; }
f.raw_append(entry.value[0]);
Expand Down
2 changes: 2 additions & 0 deletions ir/module.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ message InstructionProto {
ADD_POINTER = 33;
LOAD_PROGRAM_ARGUMENTS = 34;
DUPLICATE = 35;
ASCII_ENCODE = 36;
ASCII_DECODE = 37;
}
OpCode op_code = 1;
repeated uint64 content = 2;
Expand Down

0 comments on commit 15eb4c8

Please sign in to comment.