type system builtin types

This commit is contained in:
water 2020-08-27 20:53:11 -04:00
parent 1b40f78847
commit 7b6807ad6a
8 changed files with 82 additions and 77 deletions

View file

@ -108,6 +108,11 @@ std::string Type::get_name() const {
}
std::string Type::get_runtime_name() const {
if (!m_allow_in_runtime) {
fmt::print("[TypeSystem] Tried to use type {} as a runtime type, which is not allowed.\n",
get_name());
throw std::runtime_error("get_runtime_name");
}
return m_runtime_name;
}
@ -485,7 +490,7 @@ void StructureType::override_size_in_memory(int size) {
}
int StructureType::get_offset() const {
return 0;
return m_offset;
}
int StructureType::get_in_memory_alignment() const {

View file

@ -68,6 +68,8 @@ class Type {
const MethodInfo& add_new_method(const MethodInfo& info);
std::string print_method_info() const;
void disallow_in_runtime() { m_allow_in_runtime = false; }
virtual ~Type() = default;
protected:
@ -79,6 +81,7 @@ class Type {
std::string m_parent; // the parent type (is empty for none and object)
std::string m_name;
bool m_allow_in_runtime = true;
std::string m_runtime_name;
bool m_is_boxed = false; // does this have runtime type information?
};
@ -222,6 +225,9 @@ class StructureType : public ReferenceType {
protected:
friend class TypeSystem;
void override_offset(int offset) {
m_offset = offset;
}
void override_size_in_memory(
int size); // only to be used for setting up weird types like "structure"
void add_field(const Field& f, int new_size_in_mem) {
@ -235,6 +241,7 @@ class StructureType : public ReferenceType {
bool m_dynamic = false;
int m_size_in_mem = 0;
bool m_pack = false;
int m_offset = 0;
};
class BasicType : public StructureType {

View file

@ -502,7 +502,7 @@ int TypeSystem::add_field_to_type(StructureType* type,
* Add types which are built-in to GOAL.
*/
void TypeSystem::add_builtin_types() {
// some of the basic types having confusing circular dependencies, so this is done manually.
// some of the basic types have confusing circular dependencies, so this is done manually.
// there are no inlined things so its ok to do some things out of order because the actual size
// doesn't really matter.
@ -520,36 +520,38 @@ void TypeSystem::add_builtin_types() {
auto link_block_type = add_builtin_basic("basic", "link-block");
auto kheap_type = add_builtin_structure("structure", "kheap");
auto array_type = add_builtin_basic("basic", "array");
auto pair_type = add_builtin_structure("object", "pair");
auto pair_type = add_builtin_structure("object", "pair", true);
auto process_tree_type = add_builtin_basic("basic", "process-tree");
auto process_type = add_builtin_basic("process-tree", "process");
auto thread_type = add_builtin_basic("basic", "thread");
auto connectable_type = add_builtin_structure("structure", "connectable");
auto stack_frame_type = add_builtin_basic("basic", "stack-frame");
auto file_stream_type = add_builtin_basic("basic", "file-stream");
auto pointer_type = add_builtin_value_type("object", "pointer", 4);
add_builtin_value_type("object", "pointer", 4);
auto inline_array_type = add_builtin_value_type("object", "inline-array", 4);
inline_array_type->set_runtime_type("pointer");
auto number_type = add_builtin_value_type("object", "number", 8); // sign extend?
auto float_type = add_builtin_value_type("number", "float", 4, false, false, RegKind::FLOAT);
auto integer_type = add_builtin_value_type("number", "integer", 8, false, false); // sign extend?
auto binteger_type =
add_builtin_value_type("integer", "binteger", 8, true, false); // sign extend?
auto sinteger_type = add_builtin_value_type("integer", "sinteger", 8, false, true);
auto int8_type = add_builtin_value_type("sinteger", "int8", 1, false, true);
auto int16_type = add_builtin_value_type("sinteger", "int16", 2, false, true);
auto int32_type = add_builtin_value_type("sinteger", "int32", 4, false, true);
auto int64_type = add_builtin_value_type("sinteger", "int64", 8, false, true);
auto int128_type =
add_builtin_value_type("sinteger", "int128", 16, false, true, RegKind::INT_128);
auto uinteger_type = add_builtin_value_type("integer", "uinteger", 8);
auto uint8_type = add_builtin_value_type("uinteger", "uint8", 1);
auto uint16_type = add_builtin_value_type("uinteger", "uint16", 2);
auto uint32_type = add_builtin_value_type("uinteger", "uint32", 4);
auto uint64_type = add_builtin_value_type("uinteger", "uint64", 81);
auto uint128_type =
add_builtin_value_type("uinteger", "uint128", 16, false, false, RegKind::INT_128);
add_builtin_value_type("object", "number", 8); // sign extend?
add_builtin_value_type("number", "float", 4, false, false, RegKind::FLOAT);
add_builtin_value_type("number", "integer", 8, false, false); // sign extend?
add_builtin_value_type("integer", "binteger", 8, true, false); // sign extend?
add_builtin_value_type("integer", "sinteger", 8, false, true);
add_builtin_value_type("sinteger", "int8", 1, false, true);
add_builtin_value_type("sinteger", "int16", 2, false, true);
add_builtin_value_type("sinteger", "int32", 4, false, true);
add_builtin_value_type("sinteger", "int64", 8, false, true);
add_builtin_value_type("sinteger", "int128", 16, false, true, RegKind::INT_128);
add_builtin_value_type("integer", "uinteger", 8);
add_builtin_value_type("uinteger", "uint8", 1);
add_builtin_value_type("uinteger", "uint16", 2);
add_builtin_value_type("uinteger", "uint32", 4);
add_builtin_value_type("uinteger", "uint64", 81);
add_builtin_value_type("uinteger", "uint128", 16, false, false, RegKind::INT_128);
auto int_type = add_builtin_value_type("integer", "int", 8, false, true);
int_type->disallow_in_runtime();
auto uint_type = add_builtin_value_type("uinteger", "uint", 8, false, false);
uint_type->disallow_in_runtime();
// Methods and Fields
@ -608,39 +610,38 @@ void TypeSystem::add_builtin_types() {
// VU FUNCTION
// don't inherit
(void)vu_function_type;
add_field_to_type(vu_function_type, "length", make_typespec("int32")); // todo integer type
add_field_to_type(vu_function_type, "origin", make_typespec("pointer")); // todo sign extend?
add_field_to_type(vu_function_type, "qlength", make_typespec("int32")); // todo integer type
// link block
(void)link_block_type;
builtin_structure_inherit(link_block_type);
add_field_to_type(link_block_type, "allocated-length",
make_typespec("int32")); // todo integer type
add_field_to_type(link_block_type, "version", make_typespec("int32")); // todo integer type
// there's probably some dynamically sized stuff after this...
(void)kheap_type;
// kheap
add_field_to_type(kheap_type, "base", make_typespec("pointer"));
add_field_to_type(kheap_type, "top", make_typespec("pointer"));
add_field_to_type(kheap_type, "current", make_typespec("pointer"));
add_field_to_type(kheap_type, "top-base", make_typespec("pointer"));
// todo
(void)array_type;
(void)pair_type;
// pair
pair_type->override_offset(2);
add_field_to_type(pair_type, "car", make_typespec("object"));
add_field_to_type(pair_type, "cdr", make_typespec("object"));
// todo, with kernel
(void)process_tree_type;
(void)process_type;
(void)thread_type;
(void)connectable_type;
(void)stack_frame_type;
(void)file_stream_type;
(void)pointer_type;
(void)inline_array_type;
(void)number_type; // sign extend?
(void)float_type;
(void)integer_type; // sign extend?
(void)binteger_type; // sign extend?
(void)sinteger_type;
(void)int8_type;
(void)int16_type;
(void)int32_type;
(void)int64_type;
(void)int128_type;
(void)uinteger_type;
(void)uint8_type;
(void)uint16_type;
(void)uint32_type;
(void)uint64_type;
(void)uint128_type;
}
/*!
@ -675,23 +676,6 @@ int TypeSystem::get_next_method_id(Type* type) {
}
}
/*!
* For debugging, todo remove.
*/
int TypeSystem::manual_add_field_to_type(StructureType* type,
const std::string& field_name,
const TypeSpec& field_type,
int offset,
int size,
int alignment) {
Field field(field_name, field_type);
field.set_alignment(alignment);
field.set_offset(offset);
int new_size = type->get_size_in_memory() + size;
type->add_field(field, new_size);
return offset;
}
/*!
* Lookup a field of a type by name
*/
@ -775,8 +759,9 @@ int TypeSystem::get_size_in_type(const Field& field) {
* things in the wrong order.
*/
StructureType* TypeSystem::add_builtin_structure(const std::string& parent,
const std::string& type_name) {
add_type(type_name, std::make_unique<StructureType>(parent, type_name));
const std::string& type_name,
bool boxed) {
add_type(type_name, std::make_unique<StructureType>(parent, type_name, boxed));
return get_type_of_type<StructureType>(type_name);
}

View file

@ -93,14 +93,9 @@ class TypeSystem {
int get_alignment_in_type(const Field& field);
Field lookup_field(const std::string& type_name, const std::string& field_name);
int get_next_method_id(Type* type);
int manual_add_field_to_type(StructureType* type,
const std::string& field_name,
const TypeSpec& field_type,
int offset,
int size,
int alignment);
StructureType* add_builtin_structure(const std::string& parent, const std::string& type_name);
StructureType* add_builtin_structure(const std::string& parent,
const std::string& type_name,
bool boxed = false);
BasicType* add_builtin_basic(const std::string& parent, const std::string& type_name);
ValueType* add_builtin_value_type(const std::string& parent,
const std::string& type_name,
@ -111,7 +106,6 @@ class TypeSystem {
void builtin_structure_inherit(StructureType* st);
std::unordered_map<std::string, std::unique_ptr<Type>> m_types;
std::unordered_map<std::string, Type*> m_global_types;
std::unordered_set<std::string> m_forward_declared_types;
std::vector<std::unique_ptr<Type>> m_old_types;

View file

@ -3,6 +3,7 @@ add_subdirectory(goos)
add_subdirectory(listener)
add_subdirectory(emitter)
add_executable(goalc main.cpp)
add_executable(goalc main.cpp
compiler/Compiler.cpp)
target_link_libraries(goalc util goos)
target_link_libraries(goalc util goos type_system)

View file

@ -0,0 +1 @@
#include "Compiler.h"

12
goalc/compiler/Compiler.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef JAK_COMPILER_H
#define JAK_COMPILER_H
#include "common/type_system/TypeSystem.h"
class Compiler {
public:
private:
TypeSystem m_ts;
};
#endif // JAK_COMPILER_H

View file

@ -4,4 +4,4 @@
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
export NEXT_DIR=$DIR
$DIR/build/test/goalc-test --gtest_color=yes --gtest_brief=1 "$@"
$DIR/build/test/goalc-test --gtest_color=yes "$@"