Start a Modula-2 experiment

This commit is contained in:
2022-06-11 22:52:13 +02:00
parent f5c4a27a6d
commit 910261b408
28 changed files with 844 additions and 2574 deletions

125
Rakefile
View File

@@ -2,87 +2,82 @@ require 'pathname'
require 'rake/clean'
require 'open3'
DFLAGS = ['--warn-no-deprecated', '-L/usr/lib64/gcc-12']
BINARY = 'build/bin/elna'
TESTS = FileList['tests/*.eln'].flat_map do |test|
build = Pathname.new 'build'
test_basename = Pathname.new(test).basename('')
M2C = 'gm2' # Modula-2 compiler.
BOOT_OBJECTS = FileList['boot/*.mod']
.map do |source|
Pathname.new(source).basename.sub_ext('.o')
end
[build + 'riscv' + test_basename].map { |path| path.sub_ext('').to_path }
def source_for_object(out_file)
path = Pathname.new(out_file).relative_path_from('build')
result = ['build/boot']
definition = File.join('boot', path.basename.sub_ext('.def'))
result << definition if File.exist? definition
implementation = path.sub_ext('.mod').to_path
implementation = File.join 'build', implementation unless File.exist? implementation
result << implementation
end
SOURCES = FileList['source/**/*.d']
directory 'build'
directory 'build/boot'
directory 'build/self'
CLEAN.include 'build'
CLEAN.include '.dub'
rule(/build\/riscv\/[^\/\.]+$/ => ->(file) { test_for_out(file, '.o') }) do |t|
sh '/opt/riscv/bin/riscv32-unknown-elf-ld',
'-o', t.name,
'-L/opt/riscv/lib/gcc/riscv32-unknown-elf/11.1.0',
'-L/opt/riscv/riscv32-unknown-elf/lib',
'/opt/riscv/riscv32-unknown-elf/lib/crt0.o',
'/opt/riscv/lib/gcc/riscv32-unknown-elf/11.1.0/crtbegin.o',
t.source,
'--start-group', '-lc', '-lgloss', '--end-group',
'/opt/riscv/lib/gcc/riscv32-unknown-elf/11.1.0/crtend.o'
rule(/build\/.+\.o$/ => ->(file) { source_for_object(file) }) do |t|
sources = t.prerequisites.filter { |f| f.end_with? '.mod' }
sh M2C, '-c', '-I', 'boot', '-o', t.name, *sources
end
rule(/build\/riscv\/.+\.o$/ => ->(file) { test_for_object(file, '.eln') }) do |t|
Pathname.new(t.name).dirname.mkpath
sh BINARY, '-o', t.name, t.source
end
rule(/build\/self\/.+\.mod$/ => [
'build/self', 'build/boot/Compiler',
->(file) { File.join('boot', Pathname.new(file).basename) }
]) do |t|
sources, compiler = t.prerequisites
.reject { |f| File.directory? f }
.partition { |f| f.end_with? '.mod' }
file BINARY => SOURCES do |t|
sh({ 'DFLAGS' => (DFLAGS * ' ') }, 'dub', 'build', '--compiler=gdc-12')
end
File.open t.name, 'w' do |output|
puts
puts(compiler * ' ')
task default: BINARY
Open3.popen2(*compiler) do |cl_in, cl_out|
cl_in.write File.read(*sources)
cl_in.close
desc 'Run all tests and check the results'
task test: TESTS
task test: BINARY do
TESTS.each do |test|
expected = Pathname
.new(test)
.sub_ext('.txt')
.sub(/^build\/[[:alpha:]]+\//, 'tests/expectations/')
.read
.to_i
puts "Running #{test}"
if test.include? '/riscv/'
system('/opt/riscv/bin/spike',
'/opt/riscv/riscv32-unknown-elf/bin/pk', test,
{ out: '/dev/null' })
else
raise 'Unsupported test platform'
IO.copy_stream cl_out, output
cl_out.close
end
actual = $?.exitstatus
fail "#{test}: Expected #{expected}, got #{actual}" unless expected == actual
end
end
desc 'Run unittest blocks'
task unittest: SOURCES do |t|
sh('dub', 'test', '--compiler=gdc-12')
['boot', 'self'].each do |sub|
compiler_binary = Pathname.new('build') + sub + 'Compiler'
file compiler_binary.to_path => BOOT_OBJECTS.map { |file| File.join('build', sub, file) } do |t|
sh M2C, '-o', t.name, *t.prerequisites
end
compiler_object = compiler_binary.sub_ext('.o')
file compiler_object.to_path => source_for_object(compiler_object) do |t|
sources = t.prerequisites.filter { |f| f.end_with? '.mod' }
sh M2C, '-fscaffold-main', '-c', '-I', 'boot', '-o', t.name, *sources
end
end
def test_for_object(out_file, extension)
test_source = Pathname
.new(out_file)
.sub_ext(extension)
.sub(/^build\/[[:alpha:]]+\//, 'tests/')
.to_path
[test_source, BINARY]
end
task default: 'build/self/Compiler'
task default: 'build/self/Compiler.mod'
task default: 'boot/Compiler.mod'
task :default do |t|
exe, previous_output, source = t.prerequisites
def test_for_out(out_file, extension)
Pathname
.new(out_file)
.sub_ext(extension)
.to_path
cat_arguments = ['cat', source]
diff_arguments = ['diff', '-Nur', '--text', previous_output, '-']
puts [cat_arguments * ' ', exe, diff_arguments * ' '].join(' | ')
Open3.pipeline(cat_arguments, exe, diff_arguments)
end