elna/gcc/elna1.cc

229 lines
6.1 KiB
C++
Raw Normal View History

/* Language-dependent hooks for Elna.
2025-02-12 00:56:21 +01:00
Copyright (C) 2025 Free Software Foundation, Inc.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
2024-12-23 13:54:11 +01:00
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "target.h"
2025-02-11 01:37:55 +01:00
#include "function.h"
#include "tree.h"
#include "elna/gcc/elna1.h"
2024-12-23 13:54:11 +01:00
#include "diagnostic.h"
#include "opts.h"
#include "debug.h"
#include "langhooks.h"
#include "langhooks-def.h"
#include <fstream>
2025-02-11 01:37:55 +01:00
#include "elna/boot/driver.h"
2024-12-29 22:28:53 +01:00
#include "elna/gcc/elna-tree.h"
2024-12-27 23:38:25 +01:00
#include "elna/gcc/elna-generic.h"
#include "elna/gcc/elna-diagnostic.h"
2025-02-11 01:37:55 +01:00
#include "elna/gcc/elna-builtins.h"
2024-12-23 13:54:11 +01:00
#include "parser.hh"
2025-02-11 01:37:55 +01:00
tree elna_global_trees[ELNA_TI_MAX];
2025-02-12 00:56:21 +01:00
hash_map<nofree_string_hash, tree> *elna_global_decls = nullptr;
2024-12-23 13:54:11 +01:00
/* The resulting tree type. */
2025-02-12 20:47:47 +01:00
union GTY ((desc("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
chain_next("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), "
"TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN "
"(&%h.generic)) : NULL"))) lang_tree_node
2024-12-23 13:54:11 +01:00
{
2025-01-03 22:18:35 +01:00
union tree_node GTY ((tag ("0"), desc ("tree_node_structure (&%h)"))) generic;
2024-12-23 13:54:11 +01:00
};
/* Language hooks. */
2024-12-27 23:38:25 +01:00
static bool elna_langhook_init(void)
2024-12-23 13:54:11 +01:00
{
2024-12-29 22:28:53 +01:00
build_common_tree_nodes(false);
2025-02-12 00:56:21 +01:00
2025-02-11 01:37:55 +01:00
elna::gcc::init_ttree();
2025-02-12 00:56:21 +01:00
elna_global_decls = hash_map<nofree_string_hash, tree>::create_ggc(default_hash_map_size);
2025-02-11 01:37:55 +01:00
2024-12-29 22:28:53 +01:00
build_common_builtin_nodes();
2024-12-23 13:54:11 +01:00
2024-12-27 23:38:25 +01:00
return true;
2024-12-23 13:54:11 +01:00
}
2024-12-29 22:28:53 +01:00
static void elna_parse_file(const char *filename)
2024-12-23 13:54:11 +01:00
{
2024-12-27 23:38:25 +01:00
std::ifstream file{ filename, std::ios::in };
2024-12-23 13:54:11 +01:00
2024-12-27 23:38:25 +01:00
if (!file)
2024-12-23 13:54:11 +01:00
{
2025-01-03 22:18:35 +01:00
fatal_error(UNKNOWN_LOCATION, "cannot open filename %s: %m", filename);
2024-12-23 13:54:11 +01:00
}
2025-01-31 09:46:17 +01:00
elna::boot::driver driver{ filename };
elna::boot::lexer lexer(file);
2024-12-27 23:38:25 +01:00
yy::parser parser(lexer, driver);
2024-12-23 13:54:11 +01:00
2024-12-27 23:38:25 +01:00
linemap_add(line_table, LC_ENTER, 0, filename, 1);
2025-01-03 22:18:35 +01:00
if (parser())
2024-12-23 13:54:11 +01:00
{
2024-12-27 23:38:25 +01:00
for (const auto& error : driver.errors())
2024-12-23 13:54:11 +01:00
{
2024-12-28 14:33:35 +01:00
auto gcc_location = elna::gcc::get_location(&error->position);
2024-12-23 13:54:11 +01:00
2024-12-27 23:38:25 +01:00
error_at(gcc_location, error->what().c_str());
2024-12-23 13:54:11 +01:00
}
}
2024-12-27 23:38:25 +01:00
else
{
2025-02-17 19:36:25 +01:00
elna::gcc::generic_visitor generic_visitor{ std::make_shared<elna::gcc::symbol_table>() };
2024-12-27 23:38:25 +01:00
generic_visitor.visit(driver.tree.get());
}
linemap_add(line_table, LC_LEAVE, 0, NULL, 0);
2024-12-23 13:54:11 +01:00
}
2024-12-29 22:28:53 +01:00
static void elna_langhook_parse_file(void)
2024-12-23 13:54:11 +01:00
{
2025-01-03 22:18:35 +01:00
for (unsigned int i = 0; i < num_in_fnames; i++)
2024-12-23 13:54:11 +01:00
{
2025-01-03 22:18:35 +01:00
elna_parse_file(in_fnames[i]);
2024-12-23 13:54:11 +01:00
}
}
2025-01-03 22:18:35 +01:00
static tree elna_langhook_type_for_mode(enum machine_mode mode, int unsignedp)
2024-12-23 13:54:11 +01:00
{
2025-01-03 22:18:35 +01:00
if (mode == TYPE_MODE(float_type_node))
2024-12-23 13:54:11 +01:00
{
2025-01-03 22:18:35 +01:00
return float_type_node;
2024-12-23 13:54:11 +01:00
}
2025-01-03 22:18:35 +01:00
else if (mode == TYPE_MODE(double_type_node))
{
return double_type_node;
}
if (mode == TYPE_MODE(intQI_type_node))
{
return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
}
else if (mode == TYPE_MODE(intHI_type_node))
{
return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
}
else if (mode == TYPE_MODE(intSI_type_node))
{
return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
}
else if (mode == TYPE_MODE(intDI_type_node))
{
return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
}
else if (mode == TYPE_MODE(intTI_type_node))
{
return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
}
else if (mode == TYPE_MODE(integer_type_node))
{
return unsignedp ? unsigned_type_node : integer_type_node;
}
else if (mode == TYPE_MODE(long_integer_type_node))
{
return unsignedp ? long_unsigned_type_node : long_integer_type_node;
}
else if (mode == TYPE_MODE(long_long_integer_type_node))
{
return unsignedp
? long_long_unsigned_type_node
: long_long_integer_type_node;
}
if (COMPLEX_MODE_P(mode))
{
if (mode == TYPE_MODE(complex_float_type_node))
{
return complex_float_type_node;
}
if (mode == TYPE_MODE(complex_double_type_node))
{
return complex_double_type_node;
}
if (mode == TYPE_MODE(complex_long_double_type_node))
{
return complex_long_double_type_node;
}
if (mode == TYPE_MODE(complex_integer_type_node) && !unsignedp)
{
return complex_integer_type_node;
}
}
/* gcc_unreachable */
return nullptr;
2024-12-23 13:54:11 +01:00
}
static bool global_bindings_p(void)
2024-12-23 13:54:11 +01:00
{
return current_function_decl == NULL_TREE;
2024-12-23 13:54:11 +01:00
}
2025-02-12 00:56:21 +01:00
static tree pushdecl(tree decl)
2025-02-11 01:37:55 +01:00
{
2025-02-12 00:56:21 +01:00
return decl;
2025-02-11 01:37:55 +01:00
}
2025-02-12 00:56:21 +01:00
static tree elna_langhook_builtin_function(tree decl)
2025-02-11 01:37:55 +01:00
{
2025-02-12 00:56:21 +01:00
elna_global_decls->put(IDENTIFIER_POINTER(DECL_NAME(decl)), decl);
2025-02-11 01:37:55 +01:00
return decl;
}
2025-02-12 00:56:21 +01:00
/* Creates an expression whose value is that of EXPR, converted to type TYPE.
This function implements all reasonable scalar conversions. */
tree convert(tree type, tree expr)
2024-12-23 13:54:11 +01:00
{
2025-02-12 00:56:21 +01:00
if (error_operand_p(type) || error_operand_p(expr))
{
return error_mark_node;
}
if (TREE_TYPE(expr) == type)
{
return expr;
}
return error_mark_node;
2024-12-23 13:54:11 +01:00
}
#undef LANG_HOOKS_NAME
#define LANG_HOOKS_NAME "GNU Elna"
2024-12-23 13:54:11 +01:00
#undef LANG_HOOKS_INIT
#define LANG_HOOKS_INIT elna_langhook_init
#undef LANG_HOOKS_PARSE_FILE
#define LANG_HOOKS_PARSE_FILE elna_langhook_parse_file
#undef LANG_HOOKS_TYPE_FOR_MODE
#define LANG_HOOKS_TYPE_FOR_MODE elna_langhook_type_for_mode
2025-02-12 00:56:21 +01:00
#undef LANG_HOOKS_GETDECLS
#define LANG_HOOKS_GETDECLS hook_tree_void_null
2024-12-23 13:54:11 +01:00
#undef LANG_HOOKS_BUILTIN_FUNCTION
2025-02-11 01:37:55 +01:00
#define LANG_HOOKS_BUILTIN_FUNCTION elna_langhook_builtin_function
#undef LANG_HOOKS_IDENTIFIER_SIZE
#define LANG_HOOKS_IDENTIFIER_SIZE sizeof(struct tree_identifier)
2024-12-23 13:54:11 +01:00
struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
#include "gt-elna-elna1.h"
#include "gtype-elna.h"