Add GCC backend glue

This commit is contained in:
Eugen Wissner 2024-12-27 10:51:46 +01:00
parent 40306ac986
commit 51f5603c4a
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
24 changed files with 601 additions and 329 deletions

1
.ruby-version Normal file
View File

@ -0,0 +1 @@
3.3.6

View File

@ -3,19 +3,17 @@ project(Elna)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 14)
# find_package(Boost CONFIG COMPONENTS process program_options REQUIRED)
find_package(Boost CONFIG COMPONENTS process program_options REQUIRED)
find_package(FLEX REQUIRED)
find_package(BISON REQUIRED)
# include_directories(${Boost_INCLUDE_DIR})
FLEX_TARGET(lexer source/lexer.ll ${CMAKE_CURRENT_BINARY_DIR}/lexer.cc)
BISON_TARGET(parser source/parser.yy ${CMAKE_CURRENT_BINARY_DIR}/parser.cc)
add_flex_bison_dependency(lexer parser)
add_executable(elna cli/main.cc
add_library(elna-frontend
source/ast.cc include/elna/source/ast.h
source/types.cc include/elna/source/types.h
source/driver.cc include/elna/source/driver.h
@ -24,5 +22,15 @@ add_executable(elna cli/main.cc
source/semantic.cc include/elna/source/semantic.h
${BISON_parser_OUTPUTS} ${FLEX_lexer_OUTPUTS}
)
target_include_directories(elna PRIVATE ${CMAKE_CURRENT_BINARY_DIR} include)
# target_link_libraries(elna LINK_PUBLIC ${Boost_LIBRARIES})
target_include_directories(elna-frontend PRIVATE ${CMAKE_CURRENT_BINARY_DIR} include)
target_compile_options(elna-frontend PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions -fno-rtti>
)
add_executable(elna cli/main.cc)
target_link_libraries(elna PRIVATE elna-frontend)
target_include_directories(elna PRIVATE ${CMAKE_CURRENT_BINARY_DIR} include ${Boost_INCLUDE_DIR})
target_link_libraries(elna LINK_PUBLIC ${Boost_LIBRARIES})
target_compile_options(elna PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions -fno-rtti>
)

View File

@ -9,13 +9,14 @@ int main()
{
elna::source::driver driver{ "-" };
std::istringstream inp(R"(
var x: Int;
proc f();
begin
end;
var x: Int;
begin
x := 4 + 2
end.
)");
@ -26,7 +27,7 @@ int main()
{
for (const auto& error : driver.errors())
{
std::cerr << error->path().string() << ':'
std::cerr << error->path << ':'
<< error->line() << ':' << error->column()
<< ": error: " << error->what()
<< '.' << std::endl;
@ -41,15 +42,7 @@ int main()
for (auto& definition : driver.tree->definitions())
{
if (auto const_definition = dynamic_cast<elna::source::constant_definition *>(definition.get()))
{
std::cout << "const " << const_definition->identifier() << " = "
<< const_definition->body().number() << std::endl;
}
else if (auto proc_definition = dynamic_cast<elna::source::procedure_definition *>(definition.get()))
{
std::cout << "const " << proc_definition->identifier() << "()" << std::endl;
}
std::cout << "Definition identifier: " << definition->identifier() << std::endl;
}
return 0;
}

View File

@ -10,10 +10,12 @@ namespace gcc
{
void generic_visitor::visit(source::call_statement *statement)
{
empty_visitor::visit(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)
this->current_expression
};
tree fndecl_type_param[] = {
build_pointer_type (build_qualified_type(char_type_node, TYPE_QUAL_CONST)) /* const char* */
@ -34,7 +36,7 @@ namespace gcc
{
tree main_fndecl_type_param[] = {
integer_type_node,
build_pointer_type (build_pointer_type (char_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);
@ -64,5 +66,41 @@ namespace gcc
cgraph_node::finalize_function(main_fndecl, true);
}
void generic_visitor::visit(source::integer_literal *literal)
{
current_expression = build_int_cst_type(integer_type_node, literal->number());
}
void generic_visitor::visit(source::binary_expression *expression)
{
expression->lhs().accept(this);
auto left = this->current_expression;
expression->rhs().accept(this);
auto right = this->current_expression;
tree_code operator_code{};
switch (expression->operation())
{
case source::binary_operator::sum:
operator_code = PLUS_EXPR;
break;
case source::binary_operator::subtraction:
operator_code = MINUS_EXPR;
break;
case source::binary_operator::division:
operator_code = TRUNC_DIV_EXPR;
break;
case source::binary_operator::multiplication:
operator_code = MULT_EXPR;
break;
default:
gcc_unreachable();
}
this->current_expression = build2(operator_code, integer_type_node, left, right);
}
}
}

View File

@ -1,250 +0,0 @@
#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"

View File

@ -1,16 +0,0 @@
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;

View File

@ -15,10 +15,13 @@ namespace gcc
class generic_visitor final : public source::empty_visitor
{
tree current_statements{ NULL_TREE };
tree current_expression{ NULL_TREE };
public:
void visit(source::program *program) override;
void visit(source::call_statement *statement) override;
void visit(source::integer_literal *literal) override;
void visit(source::binary_expression *expression) override;
};
}
}

View File

@ -1,5 +1,9 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#pragma once
#include <cstdint>
#include <memory>
#include "elna/source/result.h"
#include "elna/source/types.h"

View File

@ -1,3 +1,6 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#pragma once
#include <list>
@ -16,7 +19,7 @@ namespace source
public:
syntax_error(const std::string& message,
const std::filesystem::path& input_file, const yy::location& location);
const char *input_file, const yy::location& location);
virtual std::string what() const override;
};
@ -24,12 +27,12 @@ namespace source
class driver
{
std::list<std::unique_ptr<struct error>> m_errors;
const std::filesystem::path input_file;
const char *input_file;
public:
std::unique_ptr<program> tree;
driver(const std::filesystem::path& input_file);
driver(const char *input_file);
void error(const yy::location& loc, const std::string& message);
const std::list<std::unique_ptr<struct error>>& errors() const noexcept;

View File

@ -1,7 +1,9 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#pragma once
#include <cstddef>
#include <filesystem>
#include "elna/source/types.h"
namespace elna
@ -26,7 +28,6 @@ namespace source
class error
{
position m_position;
std::filesystem::path m_path;
protected:
/**
@ -35,9 +36,11 @@ namespace source
* \param path Source file name.
* \param position Error position in the source text.
*/
error(const std::filesystem::path& path, const position position);
error(const char *path, const position position);
public:
const char *path;
virtual ~error() noexcept = default;
/// Error text.
@ -48,9 +51,6 @@ namespace source
/// Error column in the source text.
std::size_t column() const noexcept;
/// Source file name.
const std::filesystem::path& path() const noexcept;
};
class name_collision final : public error
@ -65,7 +65,7 @@ namespace source
* \param current Current symbol position.
* \param previous Position of the previously defined symbol.
*/
name_collision(const std::string& name, const std::filesystem::path& path,
name_collision(const std::string& name, const char *path,
const position current, const position previous);
std::string what() const override;
@ -92,7 +92,7 @@ namespace source
* \param path Source file name.
* \param position Operation position.
*/
type_mismatch(std::shared_ptr<const type> got, operation kind, const std::filesystem::path& path,
type_mismatch(std::shared_ptr<const type> got, operation kind, const char *path,
const struct position position);
std::string what() const override;

View File

@ -1,3 +1,6 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#pragma once
#include <list>
@ -11,7 +14,7 @@ namespace source
class name_analysis_visitor final : public empty_visitor
{
std::shared_ptr<symbol_table> table;
const std::filesystem::path filename;
const char *filename;
std::list<std::unique_ptr<error>> m_errors;
const std::size_t pointer_size;
@ -23,7 +26,7 @@ namespace source
* \param path Source filename.
* \param target_pointer_size Pointer size on the target platform.
*/
name_analysis_visitor(std::shared_ptr<symbol_table> table, const std::filesystem::path& filename,
name_analysis_visitor(std::shared_ptr<symbol_table> table, const char *filename,
const std::size_t target_pointer_size);
/**
@ -62,7 +65,7 @@ namespace source
class type_analysis_visitor final : public empty_visitor
{
std::shared_ptr<symbol_table> table;
const std::filesystem::path filename;
const char *filename;
const std::size_t pointer_size;
std::list<std::unique_ptr<error>> m_errors;
@ -72,7 +75,7 @@ namespace source
* \param path Source filename.
* \param target_pointer_size Pointer size on the target platform.
*/
type_analysis_visitor(std::shared_ptr<symbol_table> table, const std::filesystem::path& filename,
type_analysis_visitor(std::shared_ptr<symbol_table> table, const char *filename,
const std::size_t target_pointer_size);
/**

View File

@ -1,3 +1,6 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#pragma once
#include <cstdint>

View File

@ -1,3 +1,6 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#pragma once
#include <memory>

335
rakelib/cross.rake Normal file
View File

@ -0,0 +1,335 @@
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/. -}
require 'pathname'
require 'uri'
require 'net/http'
require 'rake/clean'
require 'open3'
require 'etc'
require_relative 'shared'
GCC_VERSION = "14.2.0"
BINUTILS_VERSION = '2.43.1'
GLIBC_VERSION = '2.40'
KERNEL_VERSION = '5.15.166'
CLOBBER.include TMP
class BuildTarget
attr_accessor(:build, :gcc, :target, :tmp)
def gxx
@gcc.gsub 'c', '+'
end
def sysroot
tmp + 'sysroot'
end
def rootfs
tmp + 'rootfs'
end
def tools
tmp + 'tools'
end
end
def gcc_verbose(gcc_binary)
read, write = IO.pipe
sh({'LANG' => 'C'}, gcc_binary, '--verbose', err: write)
write.close
output = read.read
read.close
output
end
def find_build_target(gcc_version, task)
gcc_binary = 'gcc'
output = gcc_verbose gcc_binary
if output.start_with? 'Apple clang'
gcc_binary = "gcc-#{gcc_version.split('.').first}"
output = gcc_verbose gcc_binary
end
result = output
.lines
.each_with_object(BuildTarget.new) do |line, accumulator|
if line.start_with? 'Target: '
accumulator.build = line.split(' ').last.strip
elsif line.start_with? 'COLLECT_GCC'
accumulator.gcc = line.split('=').last.strip
end
end
result.tmp = TMP
task.with_defaults target: 'riscv32-unknown-linux-gnu'
result.target = task[:target]
result
end
def download_and_unarchive(url, target)
case File.extname url.path
when '.bz2'
archive_type = '-j'
root_directory = File.basename url.path, '.tar.bz2'
when '.xz'
archive_type = '-J'
root_directory = File.basename url.path, '.tar.xz'
else
raise "Unsupported archive type #{url.path}."
end
Net::HTTP.start(url.host, url.port, use_ssl: url.scheme == 'https') do |http|
request = Net::HTTP::Get.new url.request_uri
http.request request do |response|
case response
when Net::HTTPRedirection
download_and_unarchive URI.parse(response['location'])
when Net::HTTPSuccess
Open3.popen2 'tar', '-C', target.to_path, archive_type, '-xv' do |stdin, stdout, wait_thread|
Thread.new do
stdout.each { |line| puts line }
end
response.read_body do |chunk|
stdin.write chunk
end
stdin.close
wait_thread.value
end
else
response.error!
end
end
end
target + root_directory
end
namespace :cross do
desc 'Build cross binutils'
task :binutils, [:target] do |_, args|
options = find_build_target GCC_VERSION, args
options.tools.mkpath
source_directory = download_and_unarchive(
URI.parse("https://ftp.gnu.org/gnu/binutils/binutils-#{BINUTILS_VERSION}.tar.xz"),
options.tools)
cwd = source_directory.dirname + 'build-binutils'
cwd.mkpath
options.rootfs.mkpath
env = {
'CC' => options.gcc,
'CXX' => options.gxx
}
configure_options = [
"--prefix=#{options.rootfs.realpath}",
"--target=#{options.target}",
'--disable-nls',
'--enable-gprofng=no',
'--disable-werror',
'--enable-default-hash-style=gnu',
'--disable-libquadmath'
]
configure = source_directory.relative_path_from(cwd) + 'configure'
sh env, configure.to_path, *configure_options, chdir: cwd.to_path
sh env, 'make', '-j', Etc.nprocessors.to_s, chdir: cwd.to_path
sh env, 'make', 'install', chdir: cwd.to_path
end
desc 'Build stage 1 GCC'
task :gcc1, [:target] do |_, args|
options = find_build_target GCC_VERSION, args
options.tools.mkpath
source_directory = download_and_unarchive(
URI.parse("https://gcc.gnu.org/pub/gcc/releases/gcc-#{GCC_VERSION}/gcc-#{GCC_VERSION}.tar.xz"),
options.tools)
cwd = source_directory.dirname + 'build-gcc'
cwd.mkpath
options.rootfs.mkpath
options.sysroot.mkpath
sh 'contrib/download_prerequisites', chdir: source_directory.to_path
configure_options = [
"--prefix=#{options.rootfs.realpath}",
"--with-sysroot=#{options.sysroot.realpath}",
'--enable-languages=c,c++',
'--disable-shared',
'--with-arch=rv32imafdc',
'--with-abi=ilp32d',
'--with-tune=rocket',
'--with-isa-spec=20191213',
'--disable-bootstrap',
'--disable-multilib',
'--disable-libmudflap',
'--disable-libssp',
'--disable-libquadmath',
'--disable-libsanitizer',
'--disable-threads',
'--disable-libatomic',
'--disable-libgomp',
'--disable-libvtv',
'--disable-libstdcxx',
'--disable-nls',
'--with-newlib',
'--without-headers',
"--target=#{options.target}",
"--build=#{options.build}",
"--host=#{options.build}"
]
flags = '-O2 -fPIC'
env = {
'CC' => options.gcc,
'CXX' => options.gxx,
'CFLAGS' => flags,
'CXXFLAGS' => flags,
'PATH' => "#{options.rootfs.realpath + 'bin'}:#{ENV['PATH']}"
}
configure = source_directory.relative_path_from(cwd) + 'configure'
sh env, configure.to_path, *configure_options, chdir: cwd.to_path
sh env, 'make', '-j', Etc.nprocessors.to_s, chdir: cwd.to_path
sh env, 'make', 'install', chdir: cwd.to_path
end
desc 'Copy glibc headers'
task :headers, [:target] do |_, args|
options = find_build_target GCC_VERSION, args
options.tools.mkpath
source_directory = download_and_unarchive(
URI.parse("https://ftp.gnu.org/gnu/glibc/glibc-#{GLIBC_VERSION}.tar.xz"),
options.tools)
include_directory = options.tools + 'include'
include_directory.mkpath
cp (source_directory + 'elf/elf.h'), (include_directory + 'elf.h')
end
desc 'Build linux kernel'
task :kernel, [:target] do |_, args|
options = find_build_target GCC_VERSION, args
options.tools.mkpath
cwd = download_and_unarchive(
URI.parse("https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-#{KERNEL_VERSION}.tar.xz"),
options.tools)
env = {
'CROSS_COMPILE' => "#{options.target}-",
'ARCH' => 'riscv',
'PATH' => "#{options.rootfs.realpath + 'bin'}:#{ENV['PATH']}",
'HOSTCFLAGS' => "-D_UUID_T -D__GETHOSTUUID_H -I#{options.tools.realpath + 'include'}"
}
sh env, 'make', 'rv32_defconfig', chdir: cwd.to_path
sh env, 'make', '-j', Etc.nprocessors.to_s, chdir: cwd.to_path
sh env, 'make', 'headers', chdir: cwd.to_path
user_directory = options.sysroot + 'usr'
user_directory.mkpath
cp_r (cwd + 'usr/include'), (user_directory + 'include')
end
desc 'Build glibc'
task :glibc, [:target] do |_, args|
options = find_build_target GCC_VERSION, args
source_directory = options.tools + "glibc-#{GLIBC_VERSION}"
configure_options = [
'--prefix=/usr',
"--host=#{options.target}",
"--target=#{options.target}",
"--build=#{options.build}",
"--enable-kernel=#{KERNEL_VERSION}",
"--with-headers=#{options.sysroot.realpath + 'usr/include'}",
'--disable-nscd',
'--disable-libquadmath',
'--disable-libitm',
'--disable-werror',
'libc_cv_forced_unwind=yes'
]
bin = options.rootfs.realpath + 'bin'
env = {
'PATH' => "#{bin}:#{ENV['PATH']}",
'MAKE' => 'make' # Otherwise it uses gnumake which can be different and too old.
}
cwd = source_directory.dirname + 'build-glibc'
cwd.mkpath
configure = source_directory.relative_path_from(cwd) +'./configure'
sh env, configure.to_path, *configure_options, chdir: cwd.to_path
sh env, 'make', '-j', Etc.nprocessors.to_s, chdir: cwd.to_path
sh env, 'make', "install_root=#{options.sysroot.realpath}", 'install', chdir: cwd.to_path
end
desc 'Build stage 2 GCC'
task :gcc2, [:target] do |_, args|
options = find_build_target GCC_VERSION, args
source_directory = options.tools + "gcc-#{GCC_VERSION}"
cwd = options.tools + 'build-gcc'
rm_rf cwd
cwd.mkpath
configure_options = [
"--prefix=#{options.rootfs.realpath}",
"--with-sysroot=#{options.sysroot.realpath}",
'--enable-languages=c,c++,lto',
'--enable-lto',
'--enable-shared',
'--with-arch=rv32imafdc',
'--with-abi=ilp32d',
'--with-tune=rocket',
'--with-isa-spec=20191213',
'--disable-bootstrap',
'--disable-multilib',
'--enable-checking=release',
'--disable-libssp',
'--disable-libquadmath',
'--enable-threads=posix',
'--with-default-libstdcxx-abi=new',
'--disable-nls',
"--target=#{options.target}",
"--build=#{options.build}",
"--host=#{options.build}"
]
flags = '-O2 -fPIC'
env = {
'CFLAGS' => flags,
'CXXFLAGS' => flags,
'PATH' => "#{options.rootfs.realpath + 'bin'}:#{ENV['PATH']}"
}
configure = source_directory.relative_path_from(cwd) + 'configure'
sh env, configure.to_path, *configure_options, chdir: cwd.to_path
sh env, 'make', '-j', Etc.nprocessors.to_s, chdir: cwd.to_path
sh env, 'make', 'install', chdir: cwd.to_path
end
task :init, [:target] do |_, args|
options = find_build_target GCC_VERSION, args
env = {
'PATH' => "#{options.rootfs.realpath + 'bin'}:#{ENV['PATH']}"
}
sh env, 'riscv32-unknown-linux-gnu-gcc',
'-ffreestanding', '-static',
'-o', (options.tools + 'init').to_path,
'tools/init.c'
end
end
desc 'Build cross toolchain'
task cross: [
'cross:binutils',
'cross:gcc1',
'cross:headers',
'cross:kernel',
'cross:glibc',
'cross:gcc2',
'cross:init'
] do
end

5
rakelib/shared.rb Normal file
View File

@ -0,0 +1,5 @@
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/. -}
TMP = Pathname.new('./build')

100
rakelib/tester.rake Normal file
View File

@ -0,0 +1,100 @@
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/. -}
require 'open3'
require 'rake/clean'
require_relative 'shared'
CLEAN.include(TMP + 'riscv')
LINKER = 'build/rootfs/riscv32-unknown-linux-gnu/bin/ld'
AS = 'build/rootfs/riscv32-unknown-linux-gnu/bin/as'
namespace :test do
test_sources = FileList['tests/vm/*.elna', 'tests/vm/*.s']
compiler = `cabal list-bin elna`.strip
object_directory = TMP + 'riscv/tests'
root_directory = TMP + 'riscv/root'
executable_directory = root_directory + 'tests'
expectation_directory = root_directory + 'expectations'
init = TMP + 'riscv/root/init'
builtin = TMP + 'riscv/builtin.o'
directory root_directory
directory object_directory
directory executable_directory
directory expectation_directory
file builtin => ['tools/builtin.s', object_directory] do |task|
sh AS, '-o', task.name, task.prerequisites.first
end
test_files = test_sources.flat_map do |test_source|
test_basename = File.basename(test_source, '.*')
test_object = object_directory + test_basename.ext('.o')
file test_object => [test_source, object_directory] do |task|
case File.extname(task.prerequisites.first)
when '.s'
sh AS, '-mno-relax', '-o', task.name, task.prerequisites.first
when '.elna'
sh compiler, '--output', task.name, task.prerequisites.first
else
raise "Unknown source file extension #{task.prerequisites.first}"
end
end
test_executable = executable_directory + test_basename
file test_executable => [test_object, executable_directory, builtin] do |task|
objects = task.prerequisites.filter { |prerequisite| File.file? prerequisite }
sh LINKER, '-o', test_executable.to_path, *objects
end
expectation_name = test_basename.ext '.txt'
source_expectation = "tests/expectations/#{expectation_name}"
target_expectation = expectation_directory + expectation_name
file target_expectation => [source_expectation, expectation_directory] do
cp source_expectation, target_expectation
end
[test_executable, target_expectation]
end
file init => [root_directory] do |task|
cp (TMP + 'tools/init'), task.name
end
# Directories should come first.
test_files.unshift executable_directory, expectation_directory, init
file (TMP + 'riscv/root.cpio') => test_files do |task|
root_files = task.prerequisites
.map { |prerequisite| Pathname.new(prerequisite).relative_path_from(root_directory).to_path }
File.open task.name, 'wb' do |cpio_file|
cpio_options = {
chdir: root_directory.to_path
}
cpio_stream = Open3.popen2 'cpio', '-o', '--format=newc', cpio_options do |stdin, stdout, wait_thread|
stdin.write root_files.join("\n")
stdin.close
stdout.each { |chunk| cpio_file.write chunk }
wait_thread.value
end
end
end
task :vm => (TMP + 'riscv/root.cpio') do |task|
kernels = FileList.glob(TMP + 'tools/linux-*/arch/riscv/boot/Image')
sh 'qemu-system-riscv32',
'-nographic',
'-M', 'virt',
'-bios', 'default',
'-kernel', kernels.first,
'-append', 'quiet panic=1',
'-initrd', task.prerequisites.first,
'-no-reboot'
end
end

View File

@ -1,3 +1,6 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include "elna/source/ast.h"
namespace elna

View File

@ -1,3 +1,6 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include "elna/source/driver.h"
namespace elna
@ -13,7 +16,7 @@ namespace source
}
syntax_error::syntax_error(const std::string& message,
const std::filesystem::path& input_file, const yy::location& location)
const char *input_file, const yy::location& location)
: error(input_file, make_position(location)), message(message)
{
}
@ -23,7 +26,7 @@ namespace source
return message;
}
driver::driver(const std::filesystem::path& input_file)
driver::driver(const char *input_file)
: input_file(input_file)
{
}

View File

@ -1,3 +1,8 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*/
%{
#define YY_NO_UNISTD_H
#define YY_USER_ACTION this->location.columns(yyleng);

View File

@ -1,3 +1,8 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*/
%require "3.2"
%language "c++"
@ -70,7 +75,7 @@
%type <std::vector<std::unique_ptr<elna::source::declaration>>> variable_declarations variable_declaration_part
formal_parameter_list;
%type <std::unique_ptr<elna::source::type_expression>> type_expression;
%type <std::unique_ptr<elna::source::expression>> expression pointer summand factor address;
%type <std::unique_ptr<elna::source::expression>> expression pointer summand factor address comparand;
%type <std::vector<std::unique_ptr<elna::source::expression>>> expressions actual_parameter_list;
%type <std::unique_ptr<elna::source::variable_expression>> variable_expression;
%type <std::unique_ptr<elna::source::compound_statement>> compound_statement;
@ -81,24 +86,25 @@
%type <std::unique_ptr<elna::source::statement>> statement;
%type <std::vector<std::unique_ptr<elna::source::statement>>> statements optional_statements;
%type <std::unique_ptr<elna::source::procedure_definition>> procedure_definition;
%type <std::vector<std::unique_ptr<elna::source::procedure_definition>>> procedure_definitions;
%type <std::vector<std::unique_ptr<elna::source::procedure_definition>>> procedure_definitions
procedure_definition_part;
%type <std::unique_ptr<elna::source::block>> block;
%%
program: constant_definition_part variable_declaration_part procedure_definitions statement DOT
program: constant_definition_part procedure_definition_part variable_declaration_part statement DOT
{
std::vector<std::unique_ptr<elna::source::definition>> definitions($1.size() + $3.size());
std::vector<std::unique_ptr<elna::source::definition>> definitions($1.size() + $2.size());
std::vector<std::unique_ptr<elna::source::definition>>::iterator definition = definitions.begin();
for (auto& constant : $1)
{
*definition++ = std::move(constant);
}
for (auto& constant : $3)
for (auto& procedure : $2)
{
*definition++ = std::move(constant);
*definition++ = std::move(procedure);
}
driver.tree = std::make_unique<elna::source::program>(elna::source::position{},
std::move(definitions), std::move($2),
std::move(definitions), std::move($3),
std::move($4));
}
block: constant_definition_part variable_declaration_part statement
@ -127,6 +133,9 @@ procedure_definitions:
$$.emplace($$.cbegin(), std::move($1));
}
| procedure_definition { $$.emplace_back(std::move($1)); }
procedure_definition_part:
/* no procedure definitions */ {}
| procedure_definitions { std::swap($$, $1); }
integer_literal: NUMBER
{
$$ = std::make_unique<elna::source::integer_literal>(elna::source::make_position(@1), $1);
@ -191,38 +200,50 @@ factor:
std::move($2), '@');
}
| address { $$ = std::move($1); }
comparand:
summand PLUS summand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '+');
}
| summand MINUS summand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '-');
}
| summand { $$ = std::move($1); }
expression:
summand EQUALS summand
comparand EQUALS comparand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '=');
}
| summand NOT_EQUAL summand
| comparand NOT_EQUAL comparand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), 'n');
}
| summand LESS_THAN summand
| comparand LESS_THAN comparand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '<');
}
| summand GREATER_THAN summand
| comparand GREATER_THAN comparand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '>');
}
| summand LESS_EQUAL summand
| comparand LESS_EQUAL comparand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '<');
}
| summand GREATER_EQUAL summand
| comparand GREATER_EQUAL comparand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '>');
}
| summand { $$ = std::move($1); }
| comparand { $$ = std::move($1); }
expressions:
expression COMMA expressions
{

View File

@ -1,11 +1,14 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include "elna/source/result.h"
namespace elna
{
namespace source
{
error::error(const std::filesystem::path& path, const position position)
: m_position(position), m_path(path)
error::error(const char *path, const position position)
: m_position(position), path(path)
{
}
@ -19,12 +22,7 @@ namespace source
return this->m_position.column;
}
const std::filesystem::path& error::path() const noexcept
{
return this->m_path;
}
name_collision::name_collision(const std::string& name, const std::filesystem::path& path,
name_collision::name_collision(const std::string& name, const char *path,
const position current, const position previous)
: error(path, current), name(name), previous(previous)
{
@ -36,7 +34,7 @@ namespace source
}
type_mismatch::type_mismatch(std::shared_ptr<const type> got, operation kind,
const std::filesystem::path& path, const struct position position)
const char *path, const struct position position)
: error(path, position), kind(kind), got(got)
{
}

View File

@ -1,3 +1,6 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include "elna/source/semantic.h"
#include "elna/source/result.h"
#include <cstdlib>
@ -7,7 +10,7 @@ namespace elna
namespace source
{
name_analysis_visitor::name_analysis_visitor(std::shared_ptr<symbol_table> table,
const std::filesystem::path& filename, const std::size_t target_pointer_size)
const char *filename, const std::size_t target_pointer_size)
: table(table), filename(filename), pointer_size(target_pointer_size)
{
}
@ -133,7 +136,7 @@ namespace source
}
type_analysis_visitor::type_analysis_visitor(std::shared_ptr<symbol_table> table,
const std::filesystem::path& filename, const std::size_t target_pointer_size)
const char *filename, const std::size_t target_pointer_size)
: table(table), filename(filename), pointer_size(target_pointer_size)
{
}

View File

@ -1,3 +1,6 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include "elna/source/types.h"
#include "elna/source/symbol_table.h"

View File

@ -1,3 +1,6 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include <elna/source/types.h>
namespace elna