Add semantic passes

This commit is contained in:
2024-12-23 13:54:11 +01:00
parent f080b75c52
commit 40306ac986
27 changed files with 1220 additions and 325 deletions

99
gcc/Make-lang.in Normal file
View File

@ -0,0 +1,99 @@
GCCELNA_INSTALL_NAME := $(shell echo gccelna|sed '$(program_transform_name)')
GCCELNA_TARGET_INSTALL_NAME := $(target_noncanonical)-$(shell echo gccelna|sed '$(program_transform_name)')
elna: elna1$(exeext)
.PHONY: elna
# Driver
GCCELNA_OBJS = \
$(GCC_OBJS) \
elna/elna-spec.o \
$(END)
gccelna$(exeext): $(GCCELNA_OBJS) $(EXTRA_GCC_OBJS) libcommon-target.a $(LIBDEPS)
+$(LINKER) $(ALL_LINKERFLAGS) $(LDFLAGS) -o $@ \
$(GCCELNA_OBJS) $(EXTRA_GCC_OBJS) libcommon-target.a \
$(EXTRA_GCC_LIBS) $(LIBS)
# The compiler proper
elna_OBJS = \
elna/elna1.o \
elna/generic-visitor.o \
elna/ast.o \
elna/driver.o \
elna/lexer.o \
elna/parser.o \
elna/result.o \
elna/semantic.o \
elna/symbol_table.o \
elna/types.o \
$(END)
elna1$(exeext): attribs.o $(elna_OBJS) $(BACKEND) $(LIBDEPS)
+$(LLINKER) $(ALL_LINKERFLAGS) $(LDFLAGS) -o $@ \
attribs.o $(elna_OBJS) $(BACKEND) $(LIBS) $(BACKENDLIBS)
elna.all.cross:
elna.start.encap: gccelna$(exeext)
elna.rest.encap:
# No elna-specific selftests.
selftest-elna:
elna.install-common: installdirs
-rm -f $(DESTDIR)$(bindir)/$(GCCELNA_INSTALL_NAME)$(exeext)
$(INSTALL_PROGRAM) gccelna$(exeext) $(DESTDIR)$(bindir)/$(GCCELNA_INSTALL_NAME)$(exeext)
rm -f $(DESTDIR)$(bindir)/$(GCCELNA_TARGET_INSTALL_NAME)$(exeext); \
( cd $(DESTDIR)$(bindir) && \
$(LN) $(GCCELNA_INSTALL_NAME)$(exeext) $(GCCELNA_TARGET_INSTALL_NAME)$(exeext) ); \
# Required goals, they still do nothing
elna.install-man:
elna.install-info:
elna.install-pdf:
elna.install-plugin:
elna.install-html:
elna.info:
elna.dvi:
elna.pdf:
elna.html:
elna.man:
elna.mostlyclean:
elna.clean:
elna.distclean:
elna.maintainer-clean:
# make uninstall
elna.uninstall:
-rm -f gccelna$(exeext) elna1$(exeext)
-rm -f $(elna_OBJS)
# Used for handling bootstrap
elna.stage1: stage1-start
-mv elna/*$(objext) stage1/elna
elna.stage2: stage2-start
-mv elna/*$(objext) stage2/elna
elna.stage3: stage3-start
-mv elna/*$(objext) stage3/elna
elna.stage4: stage4-start
-mv elna/*$(objext) stage4/elna
elna.stageprofile: stageprofile-start
-mv elna/*$(objext) stageprofile/elna
elna.stagefeedback: stagefeedback-start
-mv elna/*$(objext) stagefeedback/elna
ELNA_INCLUDES = -I $(srcdir)/elna -I $(srcdir)/elna/generated
CFLAGS-elna/elna1.o += $(ELNA_INCLUDES)
elna/%.o: elna/source/%.cc
$(COMPILE) $(ELNA_INCLUDES) $<
$(POSTCOMPILE)
elna/%.o: elna/generated/%.cc
$(COMPILE) $(ELNA_INCLUDES) $<
$(POSTCOMPILE)

13
gcc/config-lang.in Normal file
View File

@ -0,0 +1,13 @@
# gcc-src/gcc/config/config-lang.in
language="elna"
compilers="elna1\$(exeext)"
target_libs=""
gtfiles="\$(srcdir)/elna/elna1.cc"
lang_requires_boot_languages=c++
# Do not build by default
build_by_default="no"

16
gcc/elna-spec.cc Normal file
View File

@ -0,0 +1,16 @@
void
lang_specific_driver (struct cl_decoded_option ** /* in_decoded_options */,
unsigned int * /* in_decoded_options_count */,
int * /*in_added_libraries */)
{
}
/* Called before linking. Returns 0 on success and -1 on failure. */
int
lang_specific_pre_link (void)
{
return 0;
}
/* Number of extra output files that lang_specific_pre_link may generate. */
int lang_specific_extra_outfiles = 0;

252
gcc/elna1.cc Normal file
View File

@ -0,0 +1,252 @@
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "target.h"
#include "tree.h"
#include "tree-iterator.h"
#include "gimple-expr.h"
#include "diagnostic.h"
#include "opts.h"
#include "fold-const.h"
#include "stor-layout.h"
#include "debug.h"
#include "convert.h"
#include "langhooks.h"
#include "langhooks-def.h"
#include "common/common-target.h"
#include <fstream>
#include <elna/source/driver.h>
#include "elna/source/semantic.h"
#include "elna/gcc/generic-visitor.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;
};
/* Language-dependent contents of an identifier. This must include a
tree_identifier. */
struct GTY (()) lang_identifier
{
struct tree_identifier common;
};
/* The resulting tree type. */
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
{
union tree_node GTY ((tag ("0"), desc ("tree_node_structure (&%h)"))) generic;
struct lang_identifier GTY ((tag ("1"))) identifier;
};
/* We don't use language_function. */
struct GTY (()) language_function
{
int dummy;
};
/* 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)
{
return expr;
}
/* Language hooks. */
static bool
elna_langhook_init (void)
{
/* NOTE: Newer versions of GCC use only:
build_common_tree_nodes (false);
See Eugene's comment in the comments section. */
build_common_tree_nodes (false);
/* I don't know why this has to be done explicitly. */
void_list_node = build_tree_list (NULL_TREE, void_type_node);
build_common_builtin_nodes ();
return true;
}
constexpr std::size_t pointer_size = 4;
static void
elna_parse_file (const char *filename)
{
std::ifstream file{ filename, std::ios::in };
if (!file)
{
fatal_error (UNKNOWN_LOCATION, "cannot open filename %s: %m", filename);
}
elna::source::driver driver{ filename };
elna::source::lexer lexer(file);
yy::parser parser(lexer, driver);
if (auto result = parser())
{
for (const auto& error : driver.errors())
{
linemap_add (line_table, LC_ENTER, 0, filename, 1);
linemap_line_start (line_table, error->line (), 0);
auto gcc_location = linemap_position_for_column (line_table, error->column ());
linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
error_at (gcc_location, error->what ().c_str ());
}
return;
}
auto symbol_table = elna::source::add_builtin_symbols();
elna::source::name_analysis_visitor name_analysis_visitor{ symbol_table, filename, pointer_size };
elna::source::type_analysis_visitor type_analysis_visitor{ symbol_table, filename, pointer_size };
elna::gcc::generic_visitor generic_visitor;
name_analysis_visitor.visit(driver.tree.get());
type_analysis_visitor.visit(driver.tree.get());
generic_visitor.visit(driver.tree.get());
}
static void
elna_langhook_parse_file (void)
{
for (int i = 0; i < num_in_fnames; i++)
{
elna_parse_file (in_fnames[i]);
}
}
static tree
elna_langhook_type_for_mode (enum machine_mode mode, int unsignedp)
{
if (mode == TYPE_MODE (float_type_node))
return float_type_node;
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;
if (mode == TYPE_MODE (intHI_type_node))
return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
if (mode == TYPE_MODE (intSI_type_node))
return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
if (mode == TYPE_MODE (intDI_type_node))
return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
if (mode == TYPE_MODE (intTI_type_node))
return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
if (mode == TYPE_MODE (integer_type_node))
return unsignedp ? unsigned_type_node : integer_type_node;
if (mode == TYPE_MODE (long_integer_type_node))
return unsignedp ? long_unsigned_type_node : long_integer_type_node;
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 NULL;
}
static tree
elna_langhook_type_for_size (unsigned int bits ATTRIBUTE_UNUSED,
int unsignedp ATTRIBUTE_UNUSED)
{
gcc_unreachable ();
return NULL;
}
/* Record a builtin function. We just ignore builtin functions. */
static tree
elna_langhook_builtin_function (tree decl)
{
return decl;
}
static bool
elna_langhook_global_bindings_p (void)
{
gcc_unreachable ();
return true;
}
static tree
elna_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
{
gcc_unreachable ();
}
static tree
elna_langhook_getdecls (void)
{
return NULL;
}
#undef LANG_HOOKS_NAME
#define LANG_HOOKS_NAME "Elna"
#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
#undef LANG_HOOKS_TYPE_FOR_SIZE
#define LANG_HOOKS_TYPE_FOR_SIZE elna_langhook_type_for_size
#undef LANG_HOOKS_BUILTIN_FUNCTION
#define LANG_HOOKS_BUILTIN_FUNCTION elna_langhook_builtin_function
#undef LANG_HOOKS_GLOBAL_BINDINGS_P
#define LANG_HOOKS_GLOBAL_BINDINGS_P elna_langhook_global_bindings_p
#undef LANG_HOOKS_PUSHDECL
#define LANG_HOOKS_PUSHDECL elna_langhook_pushdecl
#undef LANG_HOOKS_GETDECLS
#define LANG_HOOKS_GETDECLS elna_langhook_getdecls
struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
#include "gt-elna-elna1.h"
#include "gtype-elna.h"

68
gcc/generic-visitor.cc Normal file
View File

@ -0,0 +1,68 @@
#include "elna/gcc/generic-visitor.h"
#include "input.h"
#include "cgraph.h"
#include "gimplify.h"
namespace elna
{
namespace gcc
{
void generic_visitor::visit(source::call_statement *statement)
{
const char *format_integer = "%d\n";
tree args[] = {
build_string_literal(strlen(format_integer) + 1, format_integer),
build_int_cst_type(integer_type_node, 8)
};
tree fndecl_type_param[] = {
build_pointer_type (build_qualified_type(char_type_node, TYPE_QUAL_CONST)) /* const char* */
};
tree fndecl_type = build_varargs_function_type_array(integer_type_node, 1, fndecl_type_param);
tree printf_fn_decl = build_fn_decl("printf", fndecl_type);
DECL_EXTERNAL (printf_fn_decl) = 1;
tree printf_fn = build1(ADDR_EXPR, build_pointer_type(fndecl_type), printf_fn_decl);
tree stmt = build_call_array(integer_type_node, printf_fn, 2, args);
append_to_statement_list(stmt, &this->current_statements);
}
void generic_visitor::visit(source::program *program)
{
tree main_fndecl_type_param[] = {
integer_type_node,
build_pointer_type (build_pointer_type (char_type_node))
};
tree main_fndecl_type = build_function_type_array(integer_type_node, 2, main_fndecl_type_param);
tree main_fndecl = build_fn_decl("main", main_fndecl_type);
tree resdecl = build_decl(UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, integer_type_node);
DECL_RESULT(main_fndecl) = resdecl;
tree set_result = build2(INIT_EXPR, void_type_node, DECL_RESULT(main_fndecl),
build_int_cst_type(integer_type_node, 0));
tree return_stmt = build1(RETURN_EXPR, void_type_node, set_result);
this->current_statements = alloc_stmt_list();
empty_visitor::visit(program);
append_to_statement_list(return_stmt, &this->current_statements);
tree new_block = build_block(NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
tree bind_expr = build3(BIND_EXPR, void_type_node, NULL_TREE, this->current_statements, new_block);
BLOCK_SUPERCONTEXT(new_block) = main_fndecl;
DECL_INITIAL(main_fndecl) = new_block;
DECL_SAVED_TREE(main_fndecl) = bind_expr;
DECL_EXTERNAL(main_fndecl) = 0;
DECL_PRESERVE_P(main_fndecl) = 1;
gimplify_function_tree(main_fndecl);
cgraph_node::finalize_function(main_fndecl, true);
}
}
}

3
gcc/lang-specs.h Normal file
View File

@ -0,0 +1,3 @@
/* gcc-src/gcc/config/lang-specs.in */
{".elna", "@elna", 0, 1, 0},
{"@elna", "elna1 %i %(cc1_options) %{!fsyntax-only:%(invoke_as)}", 0, 1, 0},

250
gcc/tiny1.cc Normal file
View File

@ -0,0 +1,250 @@
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "target.h"
#include "tree.h"
#include "tree-iterator.h"
#include "gimple-expr.h"
#include "diagnostic.h"
#include "opts.h"
#include "fold-const.h"
#include "cgraph.h"
#include "gimplify.h"
#include "stor-layout.h"
#include "debug.h"
#include "convert.h"
#include "langhooks.h"
#include "langhooks-def.h"
#include "common/common-target.h"
/* Language-dependent contents of a type. */
struct GTY (()) lang_type
{
char dummy;
};
/* Language-dependent contents of a decl. */
struct GTY (()) lang_decl
{
char dummy;
};
/* Language-dependent contents of an identifier. This must include a
tree_identifier. */
struct GTY (()) lang_identifier
{
struct tree_identifier common;
};
/* The resulting tree type. */
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
{
union tree_node GTY ((tag ("0"), desc ("tree_node_structure (&%h)"))) generic;
struct lang_identifier GTY ((tag ("1"))) identifier;
};
/* We don't use language_function. */
struct GTY (()) language_function
{
int dummy;
};
/* 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)
{
return expr;
}
/* Language hooks. */
static bool
elna_langhook_init (void)
{
/* NOTE: Newer versions of GCC use only:
build_common_tree_nodes (false);
See Eugene's comment in the comments section. */
build_common_tree_nodes (false);
/* I don't know why this has to be done explicitly. */
void_list_node = build_tree_list (NULL_TREE, void_type_node);
build_common_builtin_nodes ();
return true;
}
static void
elna_parse_file (const char *filename)
{
FILE *file = fopen (filename, "r");
if (file == NULL)
{
fatal_error (UNKNOWN_LOCATION, "cannot open filename %s: %m", filename);
}
tree main_fndecl_type_param[] = {
integer_type_node,
build_pointer_type (build_pointer_type (char_type_node))
};
tree main_fndecl_type = build_function_type_array (integer_type_node, 2, main_fndecl_type_param);
tree main_fndecl = build_fn_decl ("main", main_fndecl_type);
tree resdecl = build_decl (UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, integer_type_node);
DECL_RESULT (main_fndecl) = resdecl;
tree set_result
= build2 (INIT_EXPR, void_type_node, DECL_RESULT (main_fndecl),
build_int_cst_type (integer_type_node, 3));
tree return_stmt = build1 (RETURN_EXPR, void_type_node, set_result);
tree list = alloc_stmt_list ();
append_to_statement_list (return_stmt, &list);
tree new_block = build_block (NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
tree bind_expr = build3 (BIND_EXPR, void_type_node, NULL_TREE, list, new_block);
BLOCK_SUPERCONTEXT (new_block) = main_fndecl;
DECL_INITIAL (main_fndecl) = new_block;
DECL_SAVED_TREE (main_fndecl) = bind_expr;
DECL_EXTERNAL (main_fndecl) = 0;
DECL_PRESERVE_P (main_fndecl) = 1;
gimplify_function_tree (main_fndecl);
cgraph_node::finalize_function (main_fndecl, true);
fclose(file);
}
static void
elna_langhook_parse_file (void)
{
for (int i = 0; i < num_in_fnames; i++)
{
elna_parse_file (in_fnames[i]);
}
}
static tree
elna_langhook_type_for_mode (enum machine_mode mode, int unsignedp)
{
if (mode == TYPE_MODE (float_type_node))
return float_type_node;
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;
if (mode == TYPE_MODE (intHI_type_node))
return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
if (mode == TYPE_MODE (intSI_type_node))
return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
if (mode == TYPE_MODE (intDI_type_node))
return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
if (mode == TYPE_MODE (intTI_type_node))
return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
if (mode == TYPE_MODE (integer_type_node))
return unsignedp ? unsigned_type_node : integer_type_node;
if (mode == TYPE_MODE (long_integer_type_node))
return unsignedp ? long_unsigned_type_node : long_integer_type_node;
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 NULL;
}
static tree
elna_langhook_type_for_size (unsigned int bits ATTRIBUTE_UNUSED,
int unsignedp ATTRIBUTE_UNUSED)
{
gcc_unreachable ();
return NULL;
}
/* Record a builtin function. We just ignore builtin functions. */
static tree
elna_langhook_builtin_function (tree decl)
{
return decl;
}
static bool
elna_langhook_global_bindings_p (void)
{
gcc_unreachable ();
return true;
}
static tree
elna_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
{
gcc_unreachable ();
}
static tree
elna_langhook_getdecls (void)
{
return NULL;
}
#undef LANG_HOOKS_NAME
#define LANG_HOOKS_NAME "Tiny"
#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
#undef LANG_HOOKS_TYPE_FOR_SIZE
#define LANG_HOOKS_TYPE_FOR_SIZE elna_langhook_type_for_size
#undef LANG_HOOKS_BUILTIN_FUNCTION
#define LANG_HOOKS_BUILTIN_FUNCTION elna_langhook_builtin_function
#undef LANG_HOOKS_GLOBAL_BINDINGS_P
#define LANG_HOOKS_GLOBAL_BINDINGS_P elna_langhook_global_bindings_p
#undef LANG_HOOKS_PUSHDECL
#define LANG_HOOKS_PUSHDECL elna_langhook_pushdecl
#undef LANG_HOOKS_GETDECLS
#define LANG_HOOKS_GETDECLS elna_langhook_getdecls
struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
#include "gt-tiny-tiny1.h"
#include "gtype-tiny.h"

16
gcc/tinyspec.cc Normal file
View File

@ -0,0 +1,16 @@
void
lang_specific_driver (struct cl_decoded_option ** /* in_decoded_options */,
unsigned int * /* in_decoded_options_count */,
int * /*in_added_libraries */)
{
}
/* Called before linking. Returns 0 on success and -1 on failure. */
int
lang_specific_pre_link (void)
{
return 0;
}
/* Number of extra output files that lang_specific_pre_link may generate. */
int lang_specific_extra_outfiles = 0;