Fix diagnostic for primitive types

This commit is contained in:
2025-02-11 01:37:55 +01:00
parent 4bf88a92e8
commit 33aca4cc07
19 changed files with 473 additions and 626 deletions

View File

@ -42,6 +42,7 @@ elna_OBJS = \
elna/elna-convert.o \
elna/elna-diagnostic.o \
elna/elna-tree.o \
elna/elna-builtins.o \
elna/ast.o \
elna/driver.o \
elna/lexer.o \

View File

@ -29,7 +29,7 @@ compilers="elna1\$(exeext)"
target_libs=""
gtfiles="\$(srcdir)/elna/gcc/elna1.cc"
gtfiles="\$(srcdir)/elna/gcc/elna1.cc \$(srcdir)/elna/include/elna/gcc/elna1.h"
lang_requires_boot_languages=c++

31
gcc/elna-builtins.cc Normal file
View File

@ -0,0 +1,31 @@
#include "elna/gcc/elna-builtins.h"
#include "elna/gcc/elna1.h"
#include "stor-layout.h"
#include "stringpool.h"
#include "elna/gcc/elna-tree.h"
namespace elna
{
namespace gcc
{
void init_ttree()
{
elna_int_type_node = long_integer_type_node;
elna_word_type_node = size_type_node;
elna_char_type_node = unsigned_char_type_node;
elna_bool_type_node = boolean_type_node;
elna_byte_type_node = make_unsigned_type(8);
elna_float_type_node = double_type_node;
elna_string_type_node = make_node(RECORD_TYPE);
tree_chain record_chain;
record_chain.append(build_field(UNKNOWN_LOCATION, elna_string_type_node, "length", elna_word_type_node));
record_chain.append(build_field(UNKNOWN_LOCATION, elna_string_type_node, "ptr",
build_pointer_type_for_mode(elna_char_type_node, VOIDmode, true)));
TYPE_FIELDS(elna_string_type_node) = record_chain.head();
layout_type(elna_string_type_node);
}
}
}

View File

@ -17,6 +17,7 @@ along with GCC; see the file COPYING3. If not see
#include "elna/gcc/elna-diagnostic.h"
#include "elna/gcc/elna-tree.h"
#include "elna/gcc/elna1.h"
namespace elna
{
@ -33,23 +34,27 @@ namespace gcc
{
gcc_assert(TYPE_P(type));
if (type == integer_type_node)
if (type == elna_int_type_node)
{
return "Int";
}
else if (type == unsigned_type_node)
else if (type == elna_word_type_node)
{
return "Word";
}
else if (type == boolean_type_node)
else if (type == elna_bool_type_node)
{
return "Bool";
}
else if (type == double_type_node)
else if (type == elna_byte_type_node)
{
return "Byte";
}
else if (type == elna_float_type_node)
{
return "Float";
}
else if (type == unsigned_char_type_node)
else if (type == elna_char_type_node)
{
return "Char";
}

View File

@ -19,8 +19,10 @@ along with GCC; see the file COPYING3. If not see
#include "elna/gcc/elna-generic.h"
#include "elna/gcc/elna-diagnostic.h"
#include "elna/gcc/elna1.h"
#include "input.h"
#include "ggc.h"
#include "function.h"
#include "cgraph.h"
#include "gimplify.h"
#include "stringpool.h"
@ -42,7 +44,7 @@ namespace gcc
void generic_visitor::visit(boot::call_expression *expression)
{
tree symbol = this->symbol_map->lookup(expression->name());
tree symbol = this->lookup(expression->name());
if (symbol == NULL_TREE)
{
@ -91,13 +93,12 @@ namespace gcc
{
auto body_type = build_type(expression->body());
this->current_expression = build1(CONVERT_EXPR, this->symbol_map->lookup("Word"), size_in_bytes(body_type));
this->current_expression = build1(CONVERT_EXPR, elna_word_type_node, size_in_bytes(body_type));
}
bool generic_visitor::is_numeric_type(tree type)
{
return is_integral_type(type)
|| type == this->symbol_map->lookup("Float");
return is_integral_type(type) || type == elna_float_type_node;
}
void generic_visitor::visit(boot::program *program)
@ -124,11 +125,15 @@ namespace gcc
};
tree declaration_type = build_function_type_array(integer_type_node, 2, parameter_types.data());
tree fndecl = build_fn_decl("main", declaration_type);
current_function_decl = fndecl;
tree resdecl = build_decl(UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, integer_type_node);
DECL_CONTEXT(resdecl) = fndecl;
DECL_RESULT(fndecl) = resdecl;
push_struct_function(fndecl, false);
DECL_STRUCT_FUNCTION(fndecl)->language = ggc_cleared_alloc<language_function>();
DECL_STRUCT_FUNCTION(fndecl)->language->binding_level = ggc_cleared_alloc<binding_level>();
enter_scope();
tree_chain argument_chain;
@ -139,7 +144,7 @@ namespace gcc
get_identifier(argument_name.c_str()), parameter_types[i]);
DECL_CONTEXT(argc_declaration_tree) = fndecl;
DECL_ARG_TYPE(argc_declaration_tree) = parameter_types[i];
this->symbol_map->enter(argument_name, argc_declaration_tree);
argument_chain.append(argc_declaration_tree);
}
DECL_ARGUMENTS(fndecl) = argument_chain.head();
@ -161,7 +166,7 @@ namespace gcc
DECL_EXTERNAL(fndecl) = 0;
DECL_PRESERVE_P(fndecl) = 1;
current_function_decl = NULL_TREE;
pop_cfun();
gimplify_function_tree(fndecl);
cgraph_node::finalize_function(fndecl, true);
}
@ -260,18 +265,57 @@ namespace gcc
return tree_symbol_mapping{ bind_expr, new_block };
}
tree generic_visitor::lookup(const std::string& name)
{
if (name == "Int")
{
return elna_int_type_node;
}
if (name == "Word")
{
return elna_word_type_node;
}
if (name == "Char")
{
return elna_char_type_node;
}
if (name == "Bool")
{
return elna_bool_type_node;
}
if (name == "Byte")
{
return elna_byte_type_node;
}
if (name == "Float")
{
return elna_float_type_node;
}
if (name == "String")
{
return elna_string_type_node;
}
if (current_function_decl != NULL_TREE)
{
for (tree decl = DECL_ARGUMENTS(current_function_decl); decl; decl = DECL_CHAIN(decl))
{
if (name == IDENTIFIER_POINTER(DECL_NAME(decl)))
{
return decl;
}
}
}
return this->symbol_map->lookup(name);
}
void generic_visitor::visit(boot::number_literal<std::int32_t> *literal)
{
auto symbol = this->symbol_map->lookup("Int");
this->current_expression = build_int_cst(symbol, literal->number());
this->current_expression = build_int_cst(elna_int_type_node, literal->number());
}
void generic_visitor::visit(boot::number_literal<std::uint32_t> *literal)
{
auto symbol = this->symbol_map->lookup("Word");
this->current_expression = build_int_cstu(symbol, literal->number());
this->current_expression = build_int_cstu(elna_word_type_node, literal->number());
}
void generic_visitor::visit(boot::number_literal<double> *literal)
@ -291,16 +335,12 @@ namespace gcc
void generic_visitor::visit(boot::number_literal<bool> *boolean)
{
auto symbol = this->symbol_map->lookup("Bool");
this->current_expression = build_int_cst_type(symbol, boolean->number());
this->current_expression = build_int_cst_type(elna_bool_type_node, boolean->number());
}
void generic_visitor::visit(boot::number_literal<unsigned char> *character)
{
auto symbol = this->symbol_map->lookup("Char");
this->current_expression = build_int_cstu(symbol, character->number());
this->current_expression = build_int_cstu(elna_char_type_node, character->number());
}
void generic_visitor::visit(boot::number_literal<nullptr_t> *)
@ -310,11 +350,8 @@ namespace gcc
void generic_visitor::visit(boot::number_literal<std::string> *string)
{
tree index_type = this->symbol_map->lookup("Word");
tree index_constant = build_int_cstu(index_type, string->number().size());
tree element_type = this->symbol_map->lookup("Char");
tree string_type = build_array_type(element_type, build_index_type(index_constant));
tree string_record = this->symbol_map->lookup("String");
tree index_constant = build_int_cstu(elna_word_type_node, string->number().size());
tree string_type = build_array_type(elna_char_type_node, build_index_type(index_constant));
tree string_literal = build_string(string->number().size(), string->number().c_str());
TREE_TYPE(string_literal) = string_type;
@ -322,15 +359,16 @@ namespace gcc
TREE_READONLY(string_literal) = 1;
TREE_STATIC(string_literal) = 1;
string_type = TREE_TYPE(TREE_CHAIN(TYPE_FIELDS(string_record)));
string_literal = build4(ARRAY_REF, element_type, string_literal, integer_zero_node, NULL_TREE, NULL_TREE);
string_type = TREE_TYPE(TREE_CHAIN(TYPE_FIELDS(elna_string_type_node)));
string_literal = build4(ARRAY_REF, elna_char_type_node,
string_literal, integer_zero_node, NULL_TREE, NULL_TREE);
string_literal = build1(ADDR_EXPR, string_type, string_literal);
vec<constructor_elt, va_gc> *elms = NULL;
CONSTRUCTOR_APPEND_ELT(elms, TYPE_FIELDS(string_record), index_constant);
CONSTRUCTOR_APPEND_ELT(elms, TREE_CHAIN(TYPE_FIELDS(string_record)), string_literal);
CONSTRUCTOR_APPEND_ELT(elms, TYPE_FIELDS(elna_string_type_node), index_constant);
CONSTRUCTOR_APPEND_ELT(elms, TREE_CHAIN(TYPE_FIELDS(elna_string_type_node)), string_literal);
this->current_expression = build_constructor(string_record, elms);
this->current_expression = build_constructor(elna_string_type_node, elms);
}
tree generic_visitor::build_arithmetic_operation(boot::binary_expression *expression,
@ -344,23 +382,21 @@ namespace gcc
tree_code operator_code, tree left, tree right)
{
return build_binary_operation(is_numeric_type(TREE_TYPE(left)) || is_pointer_type(TREE_TYPE(left)),
expression, operator_code, left, right, this->symbol_map->lookup("Bool"));
expression, operator_code, left, right, elna_bool_type_node);
}
tree generic_visitor::build_logic_operation(boot::binary_expression *expression,
tree_code operator_code, tree left, tree right)
{
auto symbol = this->symbol_map->lookup("Bool");
return build_binary_operation(TREE_TYPE(left) == symbol,
expression, operator_code, left, right, symbol);
return build_binary_operation(TREE_TYPE(left) == elna_bool_type_node,
expression, operator_code, left, right, elna_bool_type_node);
}
tree generic_visitor::build_equality_operation(boot::binary_expression *expression,
tree_code operator_code, tree left, tree right)
{
return build_binary_operation(true, expression,
operator_code, left, right, this->symbol_map->lookup("Bool"));
operator_code, left, right, elna_bool_type_node);
}
void generic_visitor::visit(boot::binary_expression *expression)
@ -388,7 +424,7 @@ namespace gcc
}
else if (TREE_TYPE(this->current_expression) == ssizetype)
{
this->current_expression = fold_convert(this->symbol_map->lookup("Int"), this->current_expression);
this->current_expression = fold_convert(elna_int_type_node, this->current_expression);
}
return;
}
@ -524,7 +560,7 @@ namespace gcc
{
if (boot::basic_type_expression *basic_type = type.is_basic())
{
tree symbol = this->symbol_map->lookup(basic_type->base_name());
tree symbol = this->lookup(basic_type->base_name());
if (symbol != NULL_TREE && TYPE_P(symbol))
{
@ -654,7 +690,7 @@ namespace gcc
void generic_visitor::visit(boot::variable_expression *expression)
{
auto symbol = this->symbol_map->lookup(expression->name());
auto symbol = this->lookup(expression->name());
if (symbol == NULL_TREE)
{

View File

@ -17,11 +17,11 @@ along with GCC; see the file COPYING3. If not see
#include "elna/gcc/elna-tree.h"
#include "elna/gcc/elna-diagnostic.h"
#include "elna/gcc/elna1.h"
#include "stor-layout.h"
#include "fold-const.h"
#include "diagnostic-core.h"
#include "stringpool.h"
namespace elna
{
@ -144,33 +144,6 @@ namespace gcc
return field_declaration;
}
std::shared_ptr<boot::symbol_table<tree>> builtin_symbol_table()
{
std::shared_ptr<boot::symbol_table<tree>> initial_table =
std::make_shared<boot::symbol_table<tree>>();
initial_table->enter("Int", long_integer_type_node);
initial_table->enter("Word", size_type_node);
initial_table->enter("Bool", boolean_type_node);
initial_table->enter("Float", double_type_node);
initial_table->enter("Char", unsigned_char_type_node);
initial_table->enter("Byte", make_unsigned_type(8));
tree string_record = make_node(RECORD_TYPE);
tree_chain record_chain;
record_chain.append(build_field(UNKNOWN_LOCATION, string_record, "length", initial_table->lookup("Word")));
record_chain.append(build_field(UNKNOWN_LOCATION, string_record, "ptr",
build_pointer_type_for_mode(initial_table->lookup("Char"), VOIDmode, true)));
TYPE_FIELDS(string_record) = record_chain.head();
layout_type(string_record);
initial_table->enter("String", string_record);
return initial_table;
}
tree do_pointer_arithmetic(boot::binary_operator binary_operator, tree left, tree right)
{
if (binary_operator == boot::binary_operator::sum)

View File

@ -19,6 +19,9 @@ along with GCC; see the file COPYING3. If not see
#include "system.h"
#include "coretypes.h"
#include "target.h"
#include "function.h"
#include "tree.h"
#include "elna/gcc/elna1.h"
#include "diagnostic.h"
#include "opts.h"
#include "debug.h"
@ -26,25 +29,14 @@ along with GCC; see the file COPYING3. If not see
#include "langhooks-def.h"
#include <fstream>
#include <elna/boot/driver.h>
#include "elna/boot/driver.h"
#include "elna/gcc/elna-tree.h"
#include "elna/gcc/elna-generic.h"
#include "elna/gcc/elna-diagnostic.h"
#include "elna/gcc/elna-builtins.h"
#include "parser.hh"
/* Language-dependent contents of a type. */
struct GTY (()) lang_type
{
char dummy;
};
/* Language-dependent contents of a decl. */
struct GTY (()) lang_decl
{
char dummy;
};
tree elna_global_trees[ELNA_TI_MAX];
/* The resulting tree type. */
@ -56,18 +48,13 @@ union GTY ((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
union tree_node GTY ((tag ("0"), desc ("tree_node_structure (&%h)"))) generic;
};
/* We don't use language_function. */
struct GTY (()) language_function
{
int dummy;
};
/* Language hooks. */
static bool elna_langhook_init(void)
{
build_common_tree_nodes(false);
elna::gcc::init_ttree();
build_common_builtin_nodes();
return true;
@ -98,7 +85,7 @@ static void elna_parse_file(const char *filename)
}
else
{
elna::gcc::generic_visitor generic_visitor{ elna::gcc::builtin_symbol_table() };
elna::gcc::generic_visitor generic_visitor{ std::make_shared<elna::boot::symbol_table<tree>>() };
generic_visitor.visit(driver.tree.get());
}
@ -185,7 +172,26 @@ static bool global_bindings_p(void)
return current_function_decl == NULL_TREE;
}
static tree pushdecl(tree decl)
tree getdecls(void)
{
if (current_function_decl != NULL_TREE)
{
return f_binding_level->names;
}
return NULL_TREE;
}
tree pushdecl(tree decl)
{
if (current_function_decl != NULL_TREE)
{
TREE_CHAIN(decl) = f_binding_level->names;
f_binding_level->names = decl;
}
return decl;
}
static tree elna_langhook_builtin_function(tree decl)
{
return decl;
}
@ -203,10 +209,7 @@ static tree pushdecl(tree decl)
#define LANG_HOOKS_TYPE_FOR_MODE elna_langhook_type_for_mode
#undef LANG_HOOKS_BUILTIN_FUNCTION
#define LANG_HOOKS_BUILTIN_FUNCTION pushdecl
#undef LANG_HOOKS_GETDECLS
#define LANG_HOOKS_GETDECLS hook_tree_void_null
#define LANG_HOOKS_BUILTIN_FUNCTION elna_langhook_builtin_function
#undef LANG_HOOKS_IDENTIFIER_SIZE
#define LANG_HOOKS_IDENTIFIER_SIZE sizeof(struct tree_identifier)