Compare commits
No commits in common. "0d3453e7a9fc77d53f8aec5d39c89e1f118bb015" and "5490f6ce1c05d4177e4b021212e0865300d063a8" have entirely different histories.
0d3453e7a9
...
5490f6ce1c
71
Rakefile
71
Rakefile
@ -4,13 +4,8 @@ require 'open3'
|
|||||||
|
|
||||||
DFLAGS = ['--warn-no-deprecated', '-L/usr/lib64/gcc-12']
|
DFLAGS = ['--warn-no-deprecated', '-L/usr/lib64/gcc-12']
|
||||||
BINARY = 'build/bin/elna'
|
BINARY = 'build/bin/elna'
|
||||||
TESTS = FileList['tests/*.eln'].flat_map do |test|
|
TESTS = FileList['tests/*.elna']
|
||||||
build = Pathname.new 'build'
|
.map { |test| (Pathname.new('build') + test).sub_ext('').to_path }
|
||||||
test_basename = Pathname.new(test).basename('')
|
|
||||||
|
|
||||||
[build + 'riscv' + test_basename].map { |path| path.sub_ext('').to_path }
|
|
||||||
end
|
|
||||||
|
|
||||||
SOURCES = FileList['source/**/*.d']
|
SOURCES = FileList['source/**/*.d']
|
||||||
|
|
||||||
directory 'build'
|
directory 'build'
|
||||||
@ -18,27 +13,22 @@ directory 'build'
|
|||||||
CLEAN.include 'build'
|
CLEAN.include 'build'
|
||||||
CLEAN.include '.dub'
|
CLEAN.include '.dub'
|
||||||
|
|
||||||
rule(/build\/riscv\/[^\/\.]+$/ => ->(file) { test_for_out(file, '.o') }) do |t|
|
rule(/build\/tests\/.+/ => ->(file) { test_for_out(file) }) 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', '-lgcc', '-lc', '-lgloss', '--end-group',
|
|
||||||
'/opt/riscv/lib/gcc/riscv32-unknown-elf/11.1.0/crtend.o'
|
|
||||||
end
|
|
||||||
|
|
||||||
rule(/build\/riscv\/.+\.o$/ => ->(file) { test_for_object(file, '.eln') }) do |t|
|
|
||||||
Pathname.new(t.name).dirname.mkpath
|
Pathname.new(t.name).dirname.mkpath
|
||||||
sh BINARY, '-o', t.name, t.source
|
sh BINARY, t.source
|
||||||
|
sh 'gcc', '-o', t.name, "#{t.name}.o"
|
||||||
|
# Open3.pipeline [BINARY, t.source], ['gcc', '-x', 'assembler', '-o', t.name, '-']
|
||||||
end
|
end
|
||||||
|
|
||||||
file BINARY => SOURCES do |t|
|
file BINARY => SOURCES do |t|
|
||||||
sh({ 'DFLAGS' => (DFLAGS * ' ') }, 'dub', 'build', '--compiler=gdc-12')
|
sh({ 'DFLAGS' => (DFLAGS * ' ') }, 'dub', 'build', '--compiler=gdc-12')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
file 'build/tests/sample' => BINARY do |t|
|
||||||
|
sh t.source
|
||||||
|
sh 'gcc', '-o', t.name, 'build/tests/sample.o'
|
||||||
|
end
|
||||||
|
|
||||||
task default: BINARY
|
task default: BINARY
|
||||||
|
|
||||||
desc 'Run all tests and check the results'
|
desc 'Run all tests and check the results'
|
||||||
@ -48,28 +38,20 @@ task test: BINARY do
|
|||||||
expected = Pathname
|
expected = Pathname
|
||||||
.new(test)
|
.new(test)
|
||||||
.sub_ext('.txt')
|
.sub_ext('.txt')
|
||||||
.sub(/^build\/[[:alpha:]]+\//, 'tests/expectations/')
|
.sub(/^build\/tests\//, 'tests/expectations/')
|
||||||
.to_path
|
.read
|
||||||
|
.to_i
|
||||||
|
|
||||||
puts "Running #{test}"
|
puts "Running #{test}"
|
||||||
if test.include? '/riscv/'
|
system test
|
||||||
spike = [
|
actual = $?.exitstatus
|
||||||
'/opt/riscv/bin/spike',
|
|
||||||
'/opt/riscv/riscv32-unknown-elf/bin/pk',
|
|
||||||
test
|
|
||||||
]
|
|
||||||
diff = ['diff', '-Nur', '--color', expected, '-']
|
|
||||||
tail = ['tail', '-n', '1']
|
|
||||||
|
|
||||||
last_stdout, wait_threads = Open3.pipeline_r spike, tail, diff
|
fail "#{test}: Expected #{expected}, got #{actual}" unless expected == actual
|
||||||
else
|
|
||||||
raise 'Unsupported test platform'
|
|
||||||
end
|
end
|
||||||
print last_stdout.read
|
|
||||||
last_stdout.close
|
|
||||||
|
|
||||||
fail unless wait_threads.last.value.exitstatus.zero?
|
# system './build/tests/sample'
|
||||||
end
|
# actual = $?.exitstatus
|
||||||
|
# fail "./build/tests/sample: Expected 3, got #{actual}" unless 3 == actual
|
||||||
end
|
end
|
||||||
|
|
||||||
desc 'Run unittest blocks'
|
desc 'Run unittest blocks'
|
||||||
@ -77,18 +59,11 @@ task unittest: SOURCES do |t|
|
|||||||
sh('dub', 'test', '--compiler=gdc-12')
|
sh('dub', 'test', '--compiler=gdc-12')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_for_object(out_file, extension)
|
def test_for_out(out_file)
|
||||||
test_source = Pathname
|
test_source = Pathname
|
||||||
.new(out_file)
|
.new(out_file)
|
||||||
.sub_ext(extension)
|
.sub_ext('.elna')
|
||||||
.sub(/^build\/[[:alpha:]]+\//, 'tests/')
|
.sub(/^build\//, '')
|
||||||
.to_path
|
.to_path
|
||||||
[test_source, BINARY]
|
[test_source, BINARY]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_for_out(out_file, extension)
|
|
||||||
Pathname
|
|
||||||
.new(out_file)
|
|
||||||
.sub_ext(extension)
|
|
||||||
.to_path
|
|
||||||
end
|
|
||||||
|
@ -1,154 +0,0 @@
|
|||||||
/**
|
|
||||||
* Argument parsing.
|
|
||||||
*/
|
|
||||||
module elna.arguments;
|
|
||||||
|
|
||||||
import std.algorithm;
|
|
||||||
import std.range;
|
|
||||||
import std.sumtype;
|
|
||||||
|
|
||||||
struct ArgumentError
|
|
||||||
{
|
|
||||||
enum Type
|
|
||||||
{
|
|
||||||
expectedOutputFile,
|
|
||||||
noInput,
|
|
||||||
superfluousArguments,
|
|
||||||
}
|
|
||||||
|
|
||||||
private Type type_;
|
|
||||||
private string argument_;
|
|
||||||
|
|
||||||
@property Type type() const @nogc nothrow pure @safe
|
|
||||||
{
|
|
||||||
return this.type_;
|
|
||||||
}
|
|
||||||
|
|
||||||
@property string argument() const @nogc nothrow pure @safe
|
|
||||||
{
|
|
||||||
return this.argument_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void toString(OR)(OR range)
|
|
||||||
if (isOutputRage!OR)
|
|
||||||
{
|
|
||||||
final switch (Type)
|
|
||||||
{
|
|
||||||
case Type.expectedOutputFile:
|
|
||||||
put(range, "Expected an output filename after -o");
|
|
||||||
break;
|
|
||||||
case Type.noInput:
|
|
||||||
put(range, "No input files specified");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Supported compiler arguments.
|
|
||||||
*/
|
|
||||||
struct Arguments
|
|
||||||
{
|
|
||||||
private bool assembler_;
|
|
||||||
private string output_;
|
|
||||||
private string inFile_;
|
|
||||||
|
|
||||||
@property string inFile() @nogc nothrow pure @safe
|
|
||||||
{
|
|
||||||
return this.inFile_;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: Whether to generate assembly instead of an object file.
|
|
||||||
*/
|
|
||||||
@property bool assembler() const @nogc nothrow pure @safe
|
|
||||||
{
|
|
||||||
return this.assembler_;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: Output file.
|
|
||||||
*/
|
|
||||||
@property string output() const @nogc nothrow pure @safe
|
|
||||||
{
|
|
||||||
return this.output_;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse command line arguments.
|
|
||||||
*
|
|
||||||
* The first argument is expected to be the program name (and it is
|
|
||||||
* ignored).
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* arguments = Command line arguments.
|
|
||||||
*
|
|
||||||
* Returns: Parsed arguments or an error.
|
|
||||||
*/
|
|
||||||
static SumType!(ArgumentError, Arguments) parse(string[] arguments)
|
|
||||||
@nogc nothrow pure @safe
|
|
||||||
{
|
|
||||||
if (!arguments.empty)
|
|
||||||
{
|
|
||||||
arguments.popFront;
|
|
||||||
}
|
|
||||||
alias ReturnType = typeof(return);
|
|
||||||
|
|
||||||
return parseArguments(arguments).match!(
|
|
||||||
(Arguments parsed) {
|
|
||||||
if (parsed.inFile is null)
|
|
||||||
{
|
|
||||||
return ReturnType(ArgumentError(ArgumentError.Type.noInput));
|
|
||||||
}
|
|
||||||
else if (!arguments.empty)
|
|
||||||
{
|
|
||||||
return ReturnType(ArgumentError(
|
|
||||||
ArgumentError.Type.superfluousArguments,
|
|
||||||
arguments.front
|
|
||||||
));
|
|
||||||
}
|
|
||||||
return ReturnType(parsed);
|
|
||||||
},
|
|
||||||
(ArgumentError argumentError) => ReturnType(argumentError)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static SumType!(ArgumentError, Arguments) parseArguments(ref string[] arguments)
|
|
||||||
@nogc nothrow pure @safe
|
|
||||||
{
|
|
||||||
Arguments parsed;
|
|
||||||
|
|
||||||
while (!arguments.empty)
|
|
||||||
{
|
|
||||||
if (arguments.front == "-s")
|
|
||||||
{
|
|
||||||
parsed.assembler_ = true;
|
|
||||||
}
|
|
||||||
else if (arguments.front == "-o")
|
|
||||||
{
|
|
||||||
if (arguments.empty)
|
|
||||||
{
|
|
||||||
return typeof(return)(ArgumentError(
|
|
||||||
ArgumentError.Type.expectedOutputFile,
|
|
||||||
arguments.front
|
|
||||||
));
|
|
||||||
}
|
|
||||||
arguments.popFront;
|
|
||||||
parsed.output_ = arguments.front;
|
|
||||||
}
|
|
||||||
else if (arguments.front == "--")
|
|
||||||
{
|
|
||||||
arguments.popFront;
|
|
||||||
parsed.inFile_ = arguments.front;
|
|
||||||
arguments.popFront;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (!arguments.front.startsWith("-"))
|
|
||||||
{
|
|
||||||
parsed.inFile_ = arguments.front;
|
|
||||||
}
|
|
||||||
arguments.popFront;
|
|
||||||
}
|
|
||||||
return typeof(return)(parsed);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,100 +0,0 @@
|
|||||||
module elna.backend;
|
|
||||||
|
|
||||||
import core.stdc.stdio;
|
|
||||||
import elna.elf;
|
|
||||||
import elna.ir;
|
|
||||||
import elna.extended;
|
|
||||||
import elna.riscv;
|
|
||||||
import elna.lexer;
|
|
||||||
import elna.parser;
|
|
||||||
import elna.result;
|
|
||||||
import std.algorithm;
|
|
||||||
import std.sumtype;
|
|
||||||
import std.typecons;
|
|
||||||
import tanya.os.error;
|
|
||||||
import tanya.container.array;
|
|
||||||
import tanya.container.string;
|
|
||||||
import tanya.memory.allocator;
|
|
||||||
|
|
||||||
private Nullable!String readSource(string source) @nogc
|
|
||||||
{
|
|
||||||
enum size_t bufferSize = 255;
|
|
||||||
auto sourceFilename = String(source);
|
|
||||||
|
|
||||||
return readFile(sourceFilename).match!(
|
|
||||||
(ErrorCode errorCode) {
|
|
||||||
perror(sourceFilename.toStringz);
|
|
||||||
return Nullable!String();
|
|
||||||
},
|
|
||||||
(Array!ubyte contents) => nullable(String(cast(char[]) contents.get))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
int generate(string inFile, ref String outputFilename) @nogc
|
|
||||||
{
|
|
||||||
auto sourceText = readSource(inFile);
|
|
||||||
if (sourceText.isNull)
|
|
||||||
{
|
|
||||||
return 3;
|
|
||||||
}
|
|
||||||
auto tokens = lex(sourceText.get.get);
|
|
||||||
if (!tokens.valid)
|
|
||||||
{
|
|
||||||
auto compileError = tokens.error.get;
|
|
||||||
printf("%lu:%lu: %s\n", compileError.line, compileError.column, compileError.message.ptr);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
auto ast = parse(tokens.result);
|
|
||||||
if (!ast.valid)
|
|
||||||
{
|
|
||||||
auto compileError = ast.error.get;
|
|
||||||
printf("%lu:%lu: %s\n", compileError.line, compileError.column, compileError.message.ptr);
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
auto transformVisitor = defaultAllocator.make!TransformVisitor();
|
|
||||||
auto ir = transformVisitor.visit(ast.result);
|
|
||||||
defaultAllocator.dispose(transformVisitor);
|
|
||||||
|
|
||||||
auto handle = File.open(outputFilename.toStringz, BitFlags!(File.Mode)(File.Mode.truncate));
|
|
||||||
if (!handle.valid)
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
auto program = writeNext(ir);
|
|
||||||
auto elf = Elf!ELFCLASS32(move(handle));
|
|
||||||
auto readOnlyData = Array!ubyte(cast(const(ubyte)[]) "%d\n".ptr[0 .. 4]); // With \0.
|
|
||||||
|
|
||||||
elf.addReadOnlyData(String(".CL0"), readOnlyData);
|
|
||||||
elf.addCode(program.name, program.text);
|
|
||||||
|
|
||||||
elf.addExternSymbol(String("printf"));
|
|
||||||
foreach (ref reference; program.symbols)
|
|
||||||
{
|
|
||||||
elf.Rela relocationEntry = {
|
|
||||||
r_offset: cast(elf.Addr) reference.offset
|
|
||||||
};
|
|
||||||
elf.Rela relocationSub = {
|
|
||||||
r_offset: cast(elf.Addr) reference.offset,
|
|
||||||
r_info: R_RISCV_RELAX
|
|
||||||
};
|
|
||||||
|
|
||||||
final switch (reference.target)
|
|
||||||
{
|
|
||||||
case Reference.Target.text:
|
|
||||||
relocationEntry.r_info = R_RISCV_CALL;
|
|
||||||
break;
|
|
||||||
case Reference.Target.high20:
|
|
||||||
relocationEntry.r_info = R_RISCV_HI20;
|
|
||||||
break;
|
|
||||||
case Reference.Target.lower12i:
|
|
||||||
relocationEntry.r_info = R_RISCV_LO12_I;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
elf.relocate(reference.name, relocationEntry, relocationSub);
|
|
||||||
}
|
|
||||||
|
|
||||||
elf.finish();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
1060
source/elna/elf.d
1060
source/elna/elf.d
File diff suppressed because it is too large
Load Diff
@ -3,333 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
module elna.extended;
|
module elna.extended;
|
||||||
|
|
||||||
import core.stdc.errno;
|
|
||||||
import core.stdc.stdio;
|
|
||||||
import std.sumtype;
|
|
||||||
import std.typecons;
|
|
||||||
import tanya.os.error;
|
|
||||||
import tanya.container.array;
|
|
||||||
import tanya.container.string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* File handle abstraction.
|
|
||||||
*/
|
|
||||||
struct File
|
struct File
|
||||||
{
|
{
|
||||||
/// Plattform dependent file type.
|
@disable this(this);
|
||||||
alias Handle = FILE*;
|
|
||||||
|
|
||||||
/// Uninitialized file handle value.
|
|
||||||
enum Handle invalid = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Relative position.
|
|
||||||
*/
|
|
||||||
enum Whence
|
|
||||||
{
|
|
||||||
/// Relative to the start of the file.
|
|
||||||
set = SEEK_SET,
|
|
||||||
/// Relative to the current cursor position.
|
|
||||||
currentt = SEEK_CUR,
|
|
||||||
/// Relative from the end of the file.
|
|
||||||
end = SEEK_END,
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* File open modes.
|
|
||||||
*/
|
|
||||||
enum Mode
|
|
||||||
{
|
|
||||||
/// Open the file for reading.
|
|
||||||
read = 1 << 0,
|
|
||||||
/// Open the file for writing. The stream is positioned at the beginning
|
|
||||||
/// of the file.
|
|
||||||
write = 1 << 1,
|
|
||||||
/// Open the file for writing and remove its contents.
|
|
||||||
truncate = 1 << 2,
|
|
||||||
/// Open the file for writing. The stream is positioned at the end of
|
|
||||||
/// the file.
|
|
||||||
append = 1 << 3,
|
|
||||||
}
|
|
||||||
|
|
||||||
private enum Status
|
|
||||||
{
|
|
||||||
invalid,
|
|
||||||
owned,
|
|
||||||
borrowed,
|
|
||||||
}
|
|
||||||
|
|
||||||
private union Storage
|
|
||||||
{
|
|
||||||
Handle handle;
|
|
||||||
ErrorCode errorCode;
|
|
||||||
}
|
|
||||||
private Storage storage;
|
|
||||||
private Status status = Status.invalid;
|
|
||||||
|
|
||||||
@disable this(scope return ref File f);
|
|
||||||
@disable this();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Closes the file.
|
|
||||||
*/
|
|
||||||
~this() @nogc nothrow
|
|
||||||
{
|
|
||||||
if (this.status == Status.owned)
|
|
||||||
{
|
|
||||||
fclose(this.storage.handle);
|
|
||||||
}
|
|
||||||
this.storage.handle = invalid;
|
|
||||||
this.status = Status.invalid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct the object with the given system handle. The won't be claused
|
|
||||||
* in the descructor if this constructor is used.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* handle = File handle to be wrapped by this structure.
|
|
||||||
*/
|
|
||||||
this(Handle handle) @nogc nothrow pure @safe
|
|
||||||
{
|
|
||||||
this.storage.handle = handle;
|
|
||||||
this.status = Status.borrowed;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: Plattform dependent file handle.
|
|
||||||
*/
|
|
||||||
@property Handle handle() @nogc nothrow pure @trusted
|
|
||||||
{
|
|
||||||
return valid ? this.storage.handle : invalid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: An error code if an error has occurred.
|
|
||||||
*/
|
|
||||||
@property ErrorCode errorCode() @nogc nothrow pure @safe
|
|
||||||
{
|
|
||||||
return valid ? ErrorCode() : this.storage.errorCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: Whether a valid, opened file is represented.
|
|
||||||
*/
|
|
||||||
@property bool valid() @nogc nothrow pure @safe
|
|
||||||
{
|
|
||||||
return this.status != Status.invalid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Transfers the file into invalid state.
|
|
||||||
*
|
|
||||||
* Returns: The old file handle.
|
|
||||||
*/
|
|
||||||
Handle reset() @nogc nothrow pure @safe
|
|
||||||
{
|
|
||||||
if (!valid)
|
|
||||||
{
|
|
||||||
return invalid;
|
|
||||||
}
|
|
||||||
auto oldHandle = handle;
|
|
||||||
|
|
||||||
this.status = Status.invalid;
|
|
||||||
this.storage.errorCode = ErrorCode();
|
|
||||||
|
|
||||||
return oldHandle;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets stream position in the file.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* offset = File offset.
|
|
||||||
* whence = File position to add the offset to.
|
|
||||||
*
|
|
||||||
* Returns: Error code if any.
|
|
||||||
*/
|
|
||||||
ErrorCode seek(size_t offset, Whence whence) @nogc nothrow
|
|
||||||
{
|
|
||||||
if (!valid)
|
|
||||||
{
|
|
||||||
return ErrorCode(ErrorCode.ErrorNo.badDescriptor);
|
|
||||||
}
|
|
||||||
if (fseek(this.storage.handle, offset, whence))
|
|
||||||
{
|
|
||||||
return ErrorCode(cast(ErrorCode.ErrorNo) errno);
|
|
||||||
}
|
|
||||||
return ErrorCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: Current offset or an error.
|
|
||||||
*/
|
|
||||||
SumType!(ErrorCode, size_t) tell() @nogc nothrow
|
|
||||||
{
|
|
||||||
if (!valid)
|
|
||||||
{
|
|
||||||
return typeof(return)(ErrorCode(ErrorCode.ErrorNo.badDescriptor));
|
|
||||||
}
|
|
||||||
auto result = ftell(this.storage.handle);
|
|
||||||
|
|
||||||
if (result < 0)
|
|
||||||
{
|
|
||||||
return typeof(return)(ErrorCode(cast(ErrorCode.ErrorNo) errno));
|
|
||||||
}
|
|
||||||
return typeof(return)(cast(size_t) result);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Params:
|
|
||||||
* buffer = Destination buffer.
|
|
||||||
*
|
|
||||||
* Returns: Bytes read. $(D_PSYMBOL ErrorCode.ErrorNo.success) means that
|
|
||||||
* while reading the file an unknown error has occurred.
|
|
||||||
*/
|
|
||||||
SumType!(ErrorCode, size_t) read(ubyte[] buffer) @nogc nothrow
|
|
||||||
{
|
|
||||||
if (!valid)
|
|
||||||
{
|
|
||||||
return typeof(return)(ErrorCode(ErrorCode.ErrorNo.badDescriptor));
|
|
||||||
}
|
|
||||||
const bytesRead = fread(buffer.ptr, 1, buffer.length, this.storage.handle);
|
|
||||||
if (bytesRead == buffer.length || eof())
|
|
||||||
{
|
|
||||||
return typeof(return)(bytesRead);
|
|
||||||
}
|
|
||||||
return typeof(return)(ErrorCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Params:
|
|
||||||
* buffer = Source buffer.
|
|
||||||
*
|
|
||||||
* Returns: Bytes written. $(D_PSYMBOL ErrorCode.ErrorNo.success) means that
|
|
||||||
* while reading the file an unknown error has occurred.
|
|
||||||
*/
|
|
||||||
SumType!(ErrorCode, size_t) write(const(ubyte)[] buffer) @nogc nothrow
|
|
||||||
{
|
|
||||||
if (!valid)
|
|
||||||
{
|
|
||||||
return typeof(return)(ErrorCode(ErrorCode.ErrorNo.badDescriptor));
|
|
||||||
}
|
|
||||||
const bytesWritten = fwrite(buffer.ptr, 1, buffer.length, this.storage.handle);
|
|
||||||
if (bytesWritten == buffer.length)
|
|
||||||
{
|
|
||||||
return typeof(return)(buffer.length);
|
|
||||||
}
|
|
||||||
return typeof(return)(ErrorCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: EOF status of the file.
|
|
||||||
*/
|
|
||||||
bool eof() @nogc nothrow
|
|
||||||
{
|
|
||||||
return valid && feof(this.storage.handle) != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a file object that will be closed in the destructor.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* filename = The file to open.
|
|
||||||
*
|
|
||||||
* Returns: Opened file or an error.
|
|
||||||
*/
|
|
||||||
static File open(const(char)* filename, BitFlags!Mode mode) @nogc nothrow
|
|
||||||
{
|
|
||||||
char[3] modeBuffer = "\0\0\0";
|
|
||||||
|
|
||||||
if (mode.truncate)
|
|
||||||
{
|
|
||||||
modeBuffer[0] = 'w';
|
|
||||||
if (mode.read)
|
|
||||||
{
|
|
||||||
modeBuffer[1] = '+';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (mode.append)
|
|
||||||
{
|
|
||||||
modeBuffer[0] = 'a';
|
|
||||||
if (mode.read)
|
|
||||||
{
|
|
||||||
modeBuffer[1] = '+';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (mode.read)
|
|
||||||
{
|
|
||||||
modeBuffer[0] = 'r';
|
|
||||||
if (mode.write)
|
|
||||||
{
|
|
||||||
modeBuffer[1] = '+';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
auto newHandle = fopen(filename, modeBuffer.ptr);
|
|
||||||
auto newFile = File(newHandle);
|
|
||||||
|
|
||||||
if (newHandle is null)
|
|
||||||
{
|
|
||||||
newFile.status = Status.invalid;
|
|
||||||
newFile.storage.errorCode = ErrorCode(cast(ErrorCode.ErrorNo) errno);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (mode == BitFlags!Mode(Mode.write))
|
|
||||||
{
|
|
||||||
rewind(newHandle);
|
|
||||||
}
|
|
||||||
newFile.status = Status.owned;
|
|
||||||
}
|
|
||||||
|
|
||||||
return newFile;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads the whole file and returns its contents.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* sourceFilename = Source filename.
|
|
||||||
*
|
|
||||||
* Returns: File contents or an error.
|
|
||||||
*
|
|
||||||
* See_Also: $(D_PSYMBOL File.read)
|
|
||||||
*/
|
|
||||||
SumType!(ErrorCode, Array!ubyte) readFile(String sourceFilename) @nogc
|
|
||||||
{
|
|
||||||
enum size_t bufferSize = 255;
|
|
||||||
auto sourceFile = File.open(sourceFilename.toStringz, BitFlags!(File.Mode)(File.Mode.read));
|
|
||||||
|
|
||||||
if (!sourceFile.valid)
|
|
||||||
{
|
|
||||||
return typeof(return)(sourceFile.errorCode);
|
|
||||||
}
|
|
||||||
Array!ubyte sourceText;
|
|
||||||
size_t totalRead;
|
|
||||||
size_t bytesRead;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
sourceText.length = sourceText.length + bufferSize;
|
|
||||||
const readStatus = sourceFile
|
|
||||||
.read(sourceText[totalRead .. $].get)
|
|
||||||
.match!(
|
|
||||||
(ErrorCode errorCode) => nullable(errorCode),
|
|
||||||
(size_t bytesRead_) {
|
|
||||||
bytesRead = bytesRead_;
|
|
||||||
return Nullable!ErrorCode();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
if (!readStatus.isNull)
|
|
||||||
{
|
|
||||||
return typeof(return)(readStatus.get);
|
|
||||||
}
|
|
||||||
totalRead += bytesRead;
|
|
||||||
}
|
|
||||||
while (bytesRead == bufferSize);
|
|
||||||
|
|
||||||
sourceText.length = totalRead;
|
|
||||||
|
|
||||||
return typeof(return)(sourceText);
|
|
||||||
}
|
}
|
||||||
|
660
source/elna/generator.d
Normal file
660
source/elna/generator.d
Normal file
@ -0,0 +1,660 @@
|
|||||||
|
module elna.generator;
|
||||||
|
|
||||||
|
import core.stdc.stdio;
|
||||||
|
import core.stdc.stdlib;
|
||||||
|
import core.stdc.string;
|
||||||
|
import elna.ir;
|
||||||
|
import tanya.container.array;
|
||||||
|
import tanya.container.string;
|
||||||
|
import tanya.memory.mmappool;
|
||||||
|
import tanya.format;
|
||||||
|
|
||||||
|
/// Unsigned program address.
|
||||||
|
alias Elf64_Addr = void*;
|
||||||
|
/// Unsigned file offset.
|
||||||
|
alias Elf64_Off = ulong;
|
||||||
|
/// Unsigned medium integer.
|
||||||
|
alias Elf64_Half = ushort;
|
||||||
|
/// Unsigned integer.
|
||||||
|
alias Elf64_Word = uint;
|
||||||
|
/// Signed integer.
|
||||||
|
alias Elf64_Sword = int;
|
||||||
|
/// Unsigned long integer.
|
||||||
|
alias Elf64_Xword = ulong;
|
||||||
|
/// Signed long integer.
|
||||||
|
alias Elf64_Sxword = long;
|
||||||
|
|
||||||
|
enum size_t EI_INDENT = 16;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* File header.
|
||||||
|
*/
|
||||||
|
struct Elf64_Ehdr
|
||||||
|
{
|
||||||
|
/// ELF identification.
|
||||||
|
ubyte[EI_INDENT] e_ident;
|
||||||
|
/// Object file type.
|
||||||
|
Elf64_Half e_type;
|
||||||
|
/// Machine type.
|
||||||
|
Elf64_Half e_machine;
|
||||||
|
/// Object file version
|
||||||
|
Elf64_Word e_version;
|
||||||
|
/// Entry point address.
|
||||||
|
Elf64_Addr e_entry;
|
||||||
|
/// Program header offset.
|
||||||
|
Elf64_Off e_phoff;
|
||||||
|
/// Section header offset.
|
||||||
|
Elf64_Off e_shoff;
|
||||||
|
/// Processor-specific flags.
|
||||||
|
Elf64_Word e_flags;
|
||||||
|
/// ELF header size.
|
||||||
|
Elf64_Half e_ehsize;
|
||||||
|
/// Size of program header entry.
|
||||||
|
Elf64_Half e_phentsize;
|
||||||
|
/// Number of program header entries.
|
||||||
|
Elf64_Half e_phnum;
|
||||||
|
/// Size of section header entry.
|
||||||
|
Elf64_Half e_shentsize;
|
||||||
|
/// Number of section header entries.
|
||||||
|
Elf64_Half e_shnum;
|
||||||
|
/// Section name string table index.
|
||||||
|
Elf64_Half e_shstrndx;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Section header.
|
||||||
|
*/
|
||||||
|
struct Elf64_Shdr
|
||||||
|
{
|
||||||
|
/// Section name.
|
||||||
|
Elf64_Word sh_name;
|
||||||
|
/// Section type.
|
||||||
|
Elf64_Word sh_type;
|
||||||
|
/// Section attributes.
|
||||||
|
Elf64_Xword sh_flags;
|
||||||
|
/// Virtual address in memory.
|
||||||
|
Elf64_Addr sh_addr;
|
||||||
|
/// Offset in file.
|
||||||
|
Elf64_Off sh_offset;
|
||||||
|
/// Size of section.
|
||||||
|
Elf64_Xword sh_size;
|
||||||
|
/// Link to other section.
|
||||||
|
Elf64_Word sh_link;
|
||||||
|
/// Miscellaneous information.
|
||||||
|
Elf64_Word sh_info;
|
||||||
|
/// Address alignment boundary.
|
||||||
|
Elf64_Xword sh_addralign;
|
||||||
|
/// Size of entries, if section has table.
|
||||||
|
Elf64_Xword sh_entsize;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Elf64_Sym
|
||||||
|
{
|
||||||
|
/// Symbol name.
|
||||||
|
Elf64_Word st_name;
|
||||||
|
/// Type and Binding attributes.
|
||||||
|
ubyte st_info;
|
||||||
|
/// Reserved.
|
||||||
|
ubyte st_other;
|
||||||
|
/// Section table index.
|
||||||
|
Elf64_Half st_shndx;
|
||||||
|
/// Symbol value.
|
||||||
|
Elf64_Addr st_value;
|
||||||
|
/// Size of object (e.g., common).
|
||||||
|
Elf64_Xword st_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Section Types, sh_type.
|
||||||
|
enum : Elf64_Word
|
||||||
|
{
|
||||||
|
/// Marks an unused section header.
|
||||||
|
SHT_NULL = 0,
|
||||||
|
/// Contains information defined by the program.
|
||||||
|
SHT_PROGBITS = 1,
|
||||||
|
/// Contains a linker symbol table.
|
||||||
|
SHT_SYMTAB = 2,
|
||||||
|
/// Contains a string table.
|
||||||
|
SHT_STRTAB = 3,
|
||||||
|
/// Contains “Rela” type relocation entries.
|
||||||
|
SHT_RELA = 4,
|
||||||
|
/// Contains a symbol hash table
|
||||||
|
SHT_HASH = 5,
|
||||||
|
/// Contains dynamic linking tables
|
||||||
|
SHT_DYNAMIC = 6,
|
||||||
|
/// Contains note information
|
||||||
|
SHT_NOTE = 7,
|
||||||
|
/// Contains uninitialized space; does not occupy any space in the file.
|
||||||
|
SHT_NOBITS = 8,
|
||||||
|
/// Contains "Rel" type relocation entries.
|
||||||
|
SHT_REL = 9,
|
||||||
|
/// Reserved.
|
||||||
|
SHT_SHLIB = 10,
|
||||||
|
/// Contains a dynamic loader symbol table.
|
||||||
|
SHT_DYNSYM = 11,
|
||||||
|
/// Environment-specific use.
|
||||||
|
SHT_LOOS = 0x60000000,
|
||||||
|
SHT_HIOS = 0x6FFFFFFF,
|
||||||
|
/// Processor-specific use.
|
||||||
|
SHT_LOPROC = 0x70000000,
|
||||||
|
SHT_HIPROC = 0x7FFFFFFF,
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Section Attributes, sh_flags.
|
||||||
|
*/
|
||||||
|
enum : Elf64_Xword
|
||||||
|
{
|
||||||
|
/// Section contains writable data.
|
||||||
|
SHF_WRITE = 0x1,
|
||||||
|
/// Section is allocated in memory image of program.
|
||||||
|
SHF_ALLOC = 0x2,
|
||||||
|
/// Section contains executable instructions.
|
||||||
|
SHF_EXECINSTR = 0x4,
|
||||||
|
/// Environment-specific use.
|
||||||
|
SHF_MASKOS = 0x0F000000,
|
||||||
|
/// Processor-specific use.
|
||||||
|
SHF_MASKPROC = 0xF0000000,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum : Elf64_Word
|
||||||
|
{
|
||||||
|
/// Not visible outside the object file.
|
||||||
|
STB_LOCAL = 0,
|
||||||
|
/// Global symbol, visible to all object files.
|
||||||
|
STB_GLOBAL = 1,
|
||||||
|
/// Global scope, but with lower precedence than global symbols.
|
||||||
|
STB_WEAK = 2,
|
||||||
|
/// Environment-specific use.
|
||||||
|
STB_LOOS = 10,
|
||||||
|
STB_HIOS = 12,
|
||||||
|
/// Processor-specific use.
|
||||||
|
STB_LOPROC = 13,
|
||||||
|
STB_HIPROC = 15,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum : Elf64_Word
|
||||||
|
{
|
||||||
|
/// No type specified (e.g., an absolute symbol).
|
||||||
|
STT_NOTYPE = 0,
|
||||||
|
/// Data object.
|
||||||
|
STT_OBJECT = 1,
|
||||||
|
/// Function entry point.
|
||||||
|
STT_FUNC = 2,
|
||||||
|
/// Symbol is associated with a section.
|
||||||
|
STT_SECTION = 3,
|
||||||
|
/// Source file associated with the object file.
|
||||||
|
STT_FILE = 4,
|
||||||
|
/// Environment-specific use.
|
||||||
|
STT_LOOS = 10,
|
||||||
|
STT_HIOS = 12,
|
||||||
|
/// Processor-specific use.
|
||||||
|
STT_LOPROC = 13,
|
||||||
|
STT_HIPROC = 15,
|
||||||
|
}
|
||||||
|
|
||||||
|
Elf64_Ehdr makeFileHeader(Elf64_Off sectionHeaderOffset,
|
||||||
|
Elf64_Half sectionHeaderCount,
|
||||||
|
Elf64_Half stringIndex) @nogc
|
||||||
|
{
|
||||||
|
Elf64_Ehdr header;
|
||||||
|
|
||||||
|
// Magic number.
|
||||||
|
header.e_ident[0] = '\x7f';
|
||||||
|
header.e_ident[1] = 'E';
|
||||||
|
header.e_ident[2] = 'L';
|
||||||
|
header.e_ident[3] = 'F';
|
||||||
|
|
||||||
|
// File class.
|
||||||
|
header.e_ident[4] = EI_CLASS.ELFCLASS64;
|
||||||
|
|
||||||
|
// Data encoding.
|
||||||
|
header.e_ident[5] = EI_DATA.ELFDATA2LSB;
|
||||||
|
|
||||||
|
// Version.
|
||||||
|
header.e_ident[6] = EV_CURRENT;
|
||||||
|
|
||||||
|
// OS/ABI identification.
|
||||||
|
header.e_ident[7] = EI_OSABI.ELFOSABI_SYSV;
|
||||||
|
|
||||||
|
// ABI version.
|
||||||
|
header.e_ident[8] = 0;
|
||||||
|
|
||||||
|
// Size of e_ident[].
|
||||||
|
header.e_ident[15] = 0;
|
||||||
|
|
||||||
|
header.e_type = ET_REL;
|
||||||
|
header.e_machine = 0x3e; // EM_X86_64: AMD x86-64 architecture
|
||||||
|
header.e_version = EV_CURRENT;
|
||||||
|
header.e_entry = null;
|
||||||
|
header.e_phoff = 0;
|
||||||
|
header.e_shoff = sectionHeaderOffset;
|
||||||
|
header.e_flags = 0;
|
||||||
|
header.e_ehsize = Elf64_Ehdr.sizeof;
|
||||||
|
header.e_phentsize = 0;
|
||||||
|
header.e_phnum = 0;
|
||||||
|
header.e_shentsize = Elf64_Shdr.sizeof;
|
||||||
|
header.e_shnum = sectionHeaderCount;
|
||||||
|
header.e_shstrndx = stringIndex;
|
||||||
|
|
||||||
|
return header;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum char[33] sectionStringTable = "\0.symtab\0.strtab\0.shstrtab\0.text\0";
|
||||||
|
|
||||||
|
Elf64_Shdr makeTextHeader(Elf64_Off offset, Elf64_Xword size) @nogc
|
||||||
|
{
|
||||||
|
Elf64_Shdr table;
|
||||||
|
|
||||||
|
table.sh_name = 0x1b;
|
||||||
|
table.sh_type = SHT_PROGBITS;
|
||||||
|
table.sh_flags = SHF_EXECINSTR | SHF_ALLOC;
|
||||||
|
table.sh_addr = null;
|
||||||
|
table.sh_offset = offset;
|
||||||
|
table.sh_size = size;
|
||||||
|
table.sh_link = SHN_UNDEF;
|
||||||
|
table.sh_info = 0;
|
||||||
|
table.sh_addralign = 1;
|
||||||
|
table.sh_entsize = 0;
|
||||||
|
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
Elf64_Shdr makeDataHeader(Elf64_Off offset, Elf64_Xword size) @nogc
|
||||||
|
{
|
||||||
|
Elf64_Shdr table;
|
||||||
|
|
||||||
|
table.sh_name = 0x21;
|
||||||
|
table.sh_type = SHT_PROGBITS;
|
||||||
|
table.sh_flags = SHF_WRITE | SHF_ALLOC;
|
||||||
|
table.sh_addr = null;
|
||||||
|
table.sh_offset = offset;
|
||||||
|
table.sh_size = size;
|
||||||
|
table.sh_link = SHN_UNDEF;
|
||||||
|
table.sh_info = 0;
|
||||||
|
table.sh_addralign = 1;
|
||||||
|
table.sh_entsize = 0;
|
||||||
|
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
Elf64_Shdr makeSymtableHeader(Elf64_Off offset, Elf64_Xword size, Elf64_Word entriesCount) @nogc
|
||||||
|
{
|
||||||
|
Elf64_Shdr table;
|
||||||
|
|
||||||
|
table.sh_name = 0x01;
|
||||||
|
table.sh_type = SHT_SYMTAB;
|
||||||
|
table.sh_flags = 0;
|
||||||
|
table.sh_addr = null;
|
||||||
|
table.sh_offset = offset;
|
||||||
|
table.sh_size = size;
|
||||||
|
table.sh_link = 0x03; // String table used by entries in this section.
|
||||||
|
table.sh_info = entriesCount;
|
||||||
|
table.sh_addralign = 8;
|
||||||
|
table.sh_entsize = Elf64_Sym.sizeof;
|
||||||
|
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
Elf64_Shdr makeStringHeader(Elf64_Word stringIndex, Elf64_Off offset, Elf64_Xword size) @nogc
|
||||||
|
{
|
||||||
|
Elf64_Shdr table;
|
||||||
|
|
||||||
|
table.sh_name = stringIndex;
|
||||||
|
table.sh_type = SHT_STRTAB;
|
||||||
|
table.sh_flags = 0;
|
||||||
|
table.sh_addr = null;
|
||||||
|
table.sh_offset = offset;
|
||||||
|
table.sh_size = size;
|
||||||
|
table.sh_link = SHN_UNDEF;
|
||||||
|
table.sh_info = 0;
|
||||||
|
table.sh_addralign = 1;
|
||||||
|
table.sh_entsize = 0;
|
||||||
|
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
Elf64_Shdr makeInitialHeader() @nogc
|
||||||
|
{
|
||||||
|
Elf64_Shdr table;
|
||||||
|
|
||||||
|
table.sh_name = 0;
|
||||||
|
table.sh_type = SHT_NULL;
|
||||||
|
table.sh_flags = 0;
|
||||||
|
table.sh_addr = null;
|
||||||
|
table.sh_offset = 0;
|
||||||
|
table.sh_size = 0;
|
||||||
|
table.sh_link = SHN_UNDEF;
|
||||||
|
table.sh_info = 0;
|
||||||
|
table.sh_addralign = 0;
|
||||||
|
table.sh_entsize = 0;
|
||||||
|
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
Elf64_Sym makeInitialSymTable() @nogc
|
||||||
|
{
|
||||||
|
Elf64_Sym table;
|
||||||
|
|
||||||
|
table.st_name = 0;
|
||||||
|
table.st_info = 0;
|
||||||
|
table.st_other = 0;
|
||||||
|
table.st_shndx = 0;
|
||||||
|
table.st_value = null;
|
||||||
|
table.st_size = 0;
|
||||||
|
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
Elf64_Sym makeMainSymTable(Elf64_Half textIndex) @nogc
|
||||||
|
{
|
||||||
|
Elf64_Sym table;
|
||||||
|
|
||||||
|
table.st_name = 0x01;
|
||||||
|
table.st_info = ELF32_ST_INFO(STB_GLOBAL, STT_FUNC);
|
||||||
|
table.st_other = 0;
|
||||||
|
table.st_shndx = textIndex;
|
||||||
|
table.st_value = null;
|
||||||
|
table.st_size = 0;
|
||||||
|
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
ubyte ELF32_ST_BIND(ubyte i) @nogc nothrow pure @safe
|
||||||
|
{
|
||||||
|
return i >> 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
ubyte ELF32_ST_TYPE(ubyte i) @nogc nothrow pure @safe
|
||||||
|
{
|
||||||
|
return i & 0xf;
|
||||||
|
}
|
||||||
|
|
||||||
|
ubyte ELF32_ST_INFO(ubyte b, ubyte t) @nogc nothrow pure @safe
|
||||||
|
{
|
||||||
|
return cast(ubyte) ((b << 4) + (t & 0xf));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Special Section Indices.
|
||||||
|
enum : Elf64_Half
|
||||||
|
{
|
||||||
|
/// Used to mark an undefined or meaningless section reference.
|
||||||
|
SHN_UNDEF = 0,
|
||||||
|
/// Processor-specific use.
|
||||||
|
SHN_LOPROC = 0xFF00,
|
||||||
|
SHN_HIPROC = 0xFF1F,
|
||||||
|
/// Environment-specific use.
|
||||||
|
SHN_LOOS = 0xFF20,
|
||||||
|
SHN_HIOS = 0xFF3F,
|
||||||
|
/// Indicates that the corresponding reference is an absolute value.
|
||||||
|
SHN_ABS = 0xFFF1,
|
||||||
|
/**
|
||||||
|
* Indicates a symbol that has been declared as a common block (Fortran
|
||||||
|
* COMMON or C tentative declaration).
|
||||||
|
*/
|
||||||
|
SHN_COMMON = 0xFFF2,
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Object File Classes, e_ident[EI_CLASS].
|
||||||
|
*/
|
||||||
|
enum EI_CLASS : ubyte
|
||||||
|
{
|
||||||
|
/// 32-bit objects.
|
||||||
|
ELFCLASS32 = 1,
|
||||||
|
/// 64-bit objects.
|
||||||
|
ELFCLASS64 = 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ubyte EV_CURRENT = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data Encodings, e_ident[EI_DATA].
|
||||||
|
*/
|
||||||
|
enum EI_DATA : ubyte
|
||||||
|
{
|
||||||
|
/// Object file data structures are little-endian.
|
||||||
|
ELFDATA2LSB = 1,
|
||||||
|
/// Object file data structures are big-endian.
|
||||||
|
ELFDATA2MSB = 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Operating System and ABI Identifiers, e_ident[EI_OSABI].
|
||||||
|
*/
|
||||||
|
enum EI_OSABI : ubyte
|
||||||
|
{
|
||||||
|
/// System V ABI.
|
||||||
|
ELFOSABI_SYSV = 0,
|
||||||
|
/// HP-UX operating system.
|
||||||
|
ELFOSABI_HPUX = 1,
|
||||||
|
/// Standalone (embedded) application.
|
||||||
|
ELFOSABI_STANDALONE = 255,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum : Elf64_Half
|
||||||
|
{
|
||||||
|
ET_NONE = 0, /// No file type.
|
||||||
|
ET_REL = 1, /// Relocatable object file.
|
||||||
|
ET_EXEC = 2, /// Executable file.
|
||||||
|
ET_DYN = 3, /// Shared object file.
|
||||||
|
ET_CORE = 4, /// Core file.
|
||||||
|
ET_LOOS = 0xFE00, /// Environment-specific use.
|
||||||
|
ET_HIOS = 0xFEFF,
|
||||||
|
ET_LOPROC = 0xFF00, /// Processor-specific use.
|
||||||
|
ET_HIPROC = 0xFFFF,
|
||||||
|
}
|
||||||
|
|
||||||
|
private size_t pad(size_t value) @nogc
|
||||||
|
{
|
||||||
|
return (value / 8 + 1) * 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Symbol
|
||||||
|
{
|
||||||
|
String name;
|
||||||
|
Array!ubyte instructions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
.text
|
||||||
|
.globl main
|
||||||
|
.type main, @function
|
||||||
|
main:
|
||||||
|
movl $3, %eax
|
||||||
|
ret
|
||||||
|
*/
|
||||||
|
immutable ubyte[] instructions = [
|
||||||
|
// Opcode of pushq is “0x50 + r”, where “r” is the register opcode.
|
||||||
|
// Register opcode of %rbq is 5.
|
||||||
|
0x50 + 5, // push% %rbp
|
||||||
|
0x48, 0x89, 0xe5, // movq %rsp, %rbp
|
||||||
|
|
||||||
|
0xb8, 0x03, 0x00, 0x00, 0x00, // movl $3, %eax
|
||||||
|
|
||||||
|
// Epilogue.
|
||||||
|
0x48, 0x89, 0xec, // movq %rbp, %rsp
|
||||||
|
0x58 + 5, // popq %rbp
|
||||||
|
0xc3, // ret
|
||||||
|
];
|
||||||
|
|
||||||
|
void writeObject(Definition ast, String outputFilename) @nogc
|
||||||
|
{
|
||||||
|
auto handle = fopen(outputFilename.toStringz, "wb");
|
||||||
|
|
||||||
|
if (handle is null)
|
||||||
|
{
|
||||||
|
perror("writing sample");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
scope (exit)
|
||||||
|
{
|
||||||
|
fclose(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t currentOffset = Elf64_Ehdr.sizeof;
|
||||||
|
Array!Elf64_Shdr sectionHeaders;
|
||||||
|
Array!Elf64_Sym symbolEntries;
|
||||||
|
|
||||||
|
// Prologue
|
||||||
|
Array!ubyte asmTemplate = Array!ubyte(cast(ubyte[]) [
|
||||||
|
// Opcode of pushq is “0x50 + r”, where “r” is the register opcode.
|
||||||
|
// Register opcode of %rbq is 5.
|
||||||
|
0x50 + 5, // pushq %rbp
|
||||||
|
0x48, 0x89, 0xe5, // movq %rsp, %rbp
|
||||||
|
]);
|
||||||
|
int i = 1;
|
||||||
|
foreach (statement; ast.statements[])
|
||||||
|
{
|
||||||
|
if ((cast(Number) statement.subroutine.lhs) !is null)
|
||||||
|
{
|
||||||
|
// Opcode of mov is “0xb8 + r”, where “r” is the register opcode.
|
||||||
|
// Register opcode of %eax is 0.
|
||||||
|
asmTemplate.insertBack(cast(ubyte) 0xb8); // movl $x, %eax; where $x is a number.
|
||||||
|
asmTemplate.insertBack((cast(ubyte*) &(cast(Number) statement.subroutine.lhs).value)[0 .. int.sizeof]);
|
||||||
|
}
|
||||||
|
else if ((cast(Variable) statement.subroutine.lhs) !is null)
|
||||||
|
{
|
||||||
|
// movl -x(%rbp), %ebx; where x is a number.
|
||||||
|
asmTemplate.insertBack(cast(ubyte[]) [0x8b, 0x45]);
|
||||||
|
const disposition = (cast(Variable) statement.subroutine.lhs).counter * (-4);
|
||||||
|
asmTemplate.insertBack((cast(ubyte*) &disposition)[0 .. 1]);
|
||||||
|
}
|
||||||
|
if ((cast(Number) statement.subroutine.rhs) !is null)
|
||||||
|
{
|
||||||
|
// Opcode of mov is “0xb8 + r”, where “r” is the register opcode.
|
||||||
|
// Register opcode of %ebx is 3.
|
||||||
|
asmTemplate.insertBack(cast(ubyte) 0xbb); // movl $x, %ebx; where $x is a number.
|
||||||
|
asmTemplate.insertBack((cast(ubyte*) &(cast(Number) statement.subroutine.rhs).value)[0 .. int.sizeof]);
|
||||||
|
}
|
||||||
|
else if ((cast(Variable) statement.subroutine.rhs) !is null)
|
||||||
|
{
|
||||||
|
// movl -x(%rbp), %ebx; where x is a number.
|
||||||
|
asmTemplate.insertBack(cast(ubyte[]) [0x8b, 0x5d]);
|
||||||
|
const disposition = (cast(Variable) statement.subroutine.rhs).counter * (-4);
|
||||||
|
asmTemplate.insertBack((cast(ubyte*) &disposition)[0 .. 1]);
|
||||||
|
}
|
||||||
|
// Calculate the result and assign it to a variable on the stack.
|
||||||
|
asmTemplate.insertBack(cast(ubyte[]) [0x01, 0xd8]); // add %ebx, %eax
|
||||||
|
|
||||||
|
asmTemplate.insertBack(cast(ubyte[]) [0x89, 0x45]); // movl %eax, -x(%rbp); where x is a number.
|
||||||
|
const disposition = i * (-4);
|
||||||
|
asmTemplate.insertBack((cast(ubyte*) &disposition)[0 .. 1]);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
// Epilogue.
|
||||||
|
asmTemplate.insertBack(cast(ubyte[]) [
|
||||||
|
0x48, 0x89, 0xec, // movq %rbp, %rsp
|
||||||
|
0x58 + 5, // popq %rbp
|
||||||
|
0xc3, // ret
|
||||||
|
]);
|
||||||
|
|
||||||
|
Symbol[1] symbols = [Symbol(String("main"), asmTemplate)];
|
||||||
|
|
||||||
|
sectionHeaders.insertBack(makeInitialHeader());
|
||||||
|
symbolEntries.insertBack(makeInitialSymTable());
|
||||||
|
|
||||||
|
String stringTable = String("\0");
|
||||||
|
foreach (symbol; symbols[])
|
||||||
|
{
|
||||||
|
stringTable.insertBack(symbol.name[]);
|
||||||
|
stringTable.insertBack('\0');
|
||||||
|
|
||||||
|
sectionHeaders.insertBack(makeTextHeader(currentOffset, symbol.instructions.length));
|
||||||
|
currentOffset = pad(currentOffset + symbol.instructions.length);
|
||||||
|
|
||||||
|
symbolEntries.insertBack(makeMainSymTable(cast(Elf64_Half) (sectionHeaders.length - 1)));
|
||||||
|
}
|
||||||
|
|
||||||
|
const symbolTableSize = (symbols.length + 1) * Elf64_Sym.sizeof;
|
||||||
|
sectionHeaders.insertBack(makeSymtableHeader(currentOffset, symbolTableSize, symbols.length));
|
||||||
|
currentOffset += symbolTableSize;
|
||||||
|
|
||||||
|
sectionHeaders.insertBack(makeStringHeader(0x09, currentOffset, stringTable.length));
|
||||||
|
currentOffset += stringTable.length;
|
||||||
|
|
||||||
|
sectionHeaders.insertBack(makeStringHeader(0x11, currentOffset, sectionStringTable.length));
|
||||||
|
currentOffset = pad(currentOffset + sectionStringTable.length);
|
||||||
|
|
||||||
|
auto fileHeader = makeFileHeader(currentOffset, 5, 4);
|
||||||
|
|
||||||
|
version (none)
|
||||||
|
{
|
||||||
|
printf("%.2x\n", cast(uint) currentOffset);
|
||||||
|
}
|
||||||
|
ubyte[8] padding = 0;
|
||||||
|
size_t codeLength = stringTable.length + sectionStringTable.length;
|
||||||
|
|
||||||
|
fwrite(&fileHeader, 8, Elf64_Ehdr.sizeof / 8, handle);
|
||||||
|
foreach (symbol; symbols[])
|
||||||
|
{
|
||||||
|
immutable size_t instructionsLength = pad(symbol.instructions.length);
|
||||||
|
fwrite(symbol.instructions.get.ptr, 1, symbol.instructions.length, handle);
|
||||||
|
fwrite(padding.ptr, 1, instructionsLength - symbol.instructions.length, handle);
|
||||||
|
codeLength += instructionsLength;
|
||||||
|
}
|
||||||
|
fwrite(symbolEntries.get.ptr, Elf64_Sym.sizeof, symbolEntries.length, handle);
|
||||||
|
fwrite(stringTable.get.ptr, 1, stringTable.length, handle);
|
||||||
|
fwrite(sectionStringTable.ptr, 1, sectionStringTable.length, handle);
|
||||||
|
fwrite(padding.ptr, pad(codeLength) - codeLength, 1, handle);
|
||||||
|
fwrite(sectionHeaders.get.ptr, Elf64_Shdr.sizeof, sectionHeaders.length, handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
String generate(Definition ast) @nogc
|
||||||
|
{
|
||||||
|
// Prologue
|
||||||
|
String asmTemplate = ".text
|
||||||
|
.globl main
|
||||||
|
.type main, @function
|
||||||
|
main:
|
||||||
|
pushq %rbp
|
||||||
|
movq %rsp, %rbp
|
||||||
|
";
|
||||||
|
|
||||||
|
/* Allocate space on the stack for local variables.
|
||||||
|
asmTemplate.insertBack(" sub $");
|
||||||
|
asmTemplate.insertBack(format!"{}"(ast.statements.length)[]);
|
||||||
|
asmTemplate.insertBack(", $esp\n"); */
|
||||||
|
|
||||||
|
int i = 1;
|
||||||
|
foreach (statement; ast.statements[])
|
||||||
|
{
|
||||||
|
if ((cast(Number) statement.subroutine.lhs) !is null)
|
||||||
|
{
|
||||||
|
asmTemplate.insertBack(" movl $");
|
||||||
|
asmTemplate.insertBack(format!"{}"((cast(Number) statement.subroutine.lhs).value)[]);
|
||||||
|
asmTemplate.insertBack(", %eax\n");
|
||||||
|
}
|
||||||
|
else if ((cast(Variable) statement.subroutine.lhs) !is null)
|
||||||
|
{
|
||||||
|
asmTemplate.insertBack(" movl -");
|
||||||
|
asmTemplate.insertBack(format!"{}"((cast(Variable) statement.subroutine.lhs).counter * 4)[]);
|
||||||
|
asmTemplate.insertBack("(%rbp), %eax\n");
|
||||||
|
}
|
||||||
|
if ((cast(Number) statement.subroutine.rhs) !is null)
|
||||||
|
{
|
||||||
|
asmTemplate.insertBack(" movl $");
|
||||||
|
asmTemplate.insertBack(format!"{}"((cast(Number) statement.subroutine.rhs).value)[]);
|
||||||
|
asmTemplate.insertBack(", %ebx\n");
|
||||||
|
}
|
||||||
|
else if ((cast(Variable) statement.subroutine.rhs) !is null)
|
||||||
|
{
|
||||||
|
asmTemplate.insertBack(" movl -");
|
||||||
|
asmTemplate.insertBack(format!"{}"((cast(Variable) statement.subroutine.rhs).counter * 4)[]);
|
||||||
|
asmTemplate.insertBack("(%rbp), %ebx\n");
|
||||||
|
}
|
||||||
|
// Calculate the result and assign it to a variable on the stack.
|
||||||
|
asmTemplate.insertBack(" add %ebx, %eax\n");
|
||||||
|
asmTemplate.insertBack(" movl %eax, -");
|
||||||
|
asmTemplate.insertBack(format!"{}"(i * 4)[]);
|
||||||
|
asmTemplate.insertBack("(%rbp)\n");
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Epilogue.
|
||||||
|
asmTemplate.insertBack(" movq %rbp, %rsp
|
||||||
|
popq %rbp
|
||||||
|
ret
|
||||||
|
");
|
||||||
|
|
||||||
|
return asmTemplate;
|
||||||
|
}
|
240
source/elna/ir.d
240
source/elna/ir.d
@ -5,246 +5,108 @@ import tanya.container.array;
|
|||||||
import tanya.container.hashtable;
|
import tanya.container.hashtable;
|
||||||
import tanya.container.string;
|
import tanya.container.string;
|
||||||
import tanya.memory.allocator;
|
import tanya.memory.allocator;
|
||||||
public import elna.parser : BinaryOperator;
|
import tanya.memory.mmappool;
|
||||||
|
|
||||||
/**
|
|
||||||
* Mapping between the parser and IR AST.
|
|
||||||
*/
|
|
||||||
struct ASTMapping
|
|
||||||
{
|
|
||||||
alias Node = .Node;
|
|
||||||
alias Definition = .Definition;
|
|
||||||
alias VariableDeclaration = .VariableDeclaration;
|
|
||||||
alias Statement = Array!(.Statement);
|
|
||||||
alias BangStatement = .Expression;
|
|
||||||
alias Block = .Definition;
|
|
||||||
alias Expression = .Expression;
|
|
||||||
alias Number = .Number;
|
|
||||||
alias Variable = .Variable;
|
|
||||||
alias BinaryExpression = .BinaryExpression;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* IR visitor.
|
|
||||||
*/
|
|
||||||
interface IRVisitor
|
|
||||||
{
|
|
||||||
abstract void visit(Node) @nogc;
|
|
||||||
abstract void visit(Definition) @nogc;
|
|
||||||
abstract void visit(Expression) @nogc;
|
|
||||||
abstract void visit(Statement) @nogc;
|
|
||||||
abstract void visit(Variable) @nogc;
|
|
||||||
abstract void visit(VariableDeclaration) @nogc;
|
|
||||||
abstract void visit(Number) @nogc;
|
|
||||||
abstract void visit(BinaryExpression) @nogc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* AST node.
|
|
||||||
*/
|
|
||||||
abstract class Node
|
|
||||||
{
|
|
||||||
abstract void accept(IRVisitor) @nogc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Definition.
|
* Definition.
|
||||||
*/
|
*/
|
||||||
class Definition : Node
|
class Definition
|
||||||
{
|
{
|
||||||
char[] identifier;
|
char[] identifier;
|
||||||
Array!Statement statements;
|
Array!Statement statements;
|
||||||
Array!VariableDeclaration variableDeclarations;
|
Array!VariableDeclaration variableDeclarations;
|
||||||
|
|
||||||
override void accept(IRVisitor visitor) @nogc
|
|
||||||
{
|
|
||||||
visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Statement : Node
|
class Statement
|
||||||
{
|
{
|
||||||
BinaryExpression expression;
|
Subroutine subroutine;
|
||||||
|
|
||||||
override void accept(IRVisitor visitor) @nogc
|
|
||||||
{
|
|
||||||
visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class Expression : Node
|
abstract class Expression
|
||||||
{
|
{
|
||||||
override void accept(IRVisitor visitor) @nogc
|
|
||||||
{
|
|
||||||
visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Number : Expression
|
class Number : Expression
|
||||||
{
|
{
|
||||||
int value;
|
int value;
|
||||||
|
|
||||||
override void accept(IRVisitor visitor) @nogc
|
|
||||||
{
|
|
||||||
visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Variable : Expression
|
class Variable : Expression
|
||||||
{
|
{
|
||||||
size_t counter;
|
size_t counter;
|
||||||
|
|
||||||
override void accept(IRVisitor visitor) @nogc
|
|
||||||
{
|
|
||||||
visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class VariableDeclaration : Node
|
class VariableDeclaration
|
||||||
{
|
{
|
||||||
String identifier;
|
String identifier;
|
||||||
|
|
||||||
override void accept(IRVisitor visitor) @nogc
|
|
||||||
{
|
|
||||||
visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class BinaryExpression : Node
|
class Subroutine
|
||||||
{
|
{
|
||||||
Expression lhs, rhs;
|
Expression lhs, rhs;
|
||||||
BinaryOperator operator;
|
|
||||||
|
|
||||||
this(Expression lhs, Expression rhs, BinaryOperator operator)
|
|
||||||
@nogc
|
|
||||||
{
|
|
||||||
this.lhs = lhs;
|
|
||||||
this.rhs = rhs;
|
|
||||||
this.operator = operator;
|
|
||||||
}
|
|
||||||
|
|
||||||
override void accept(IRVisitor visitor) @nogc
|
|
||||||
{
|
|
||||||
visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final class TransformVisitor : parser.ParserVisitor!ASTMapping
|
private Number transformNumber(parser.Number number) @nogc
|
||||||
{
|
{
|
||||||
private HashTable!(String, int) constants;
|
return MmapPool.instance.make!Number(number.value);
|
||||||
|
}
|
||||||
|
|
||||||
ASTMapping.Node visit(parser.Node node) @nogc
|
private Variable transformSubroutine(parser.Subroutine subroutine,
|
||||||
{
|
ref Array!Statement statements,
|
||||||
assert(false, "Not implemented");
|
ref HashTable!(String, int) constants) @nogc
|
||||||
}
|
{
|
||||||
|
auto target = MmapPool.instance.make!Subroutine;
|
||||||
|
target.lhs = transformExpression(subroutine.lhs, statements, constants);
|
||||||
|
target.rhs = transformExpression(subroutine.rhs, statements, constants);
|
||||||
|
|
||||||
ASTMapping.Definition visit(parser.Definition definition) @nogc
|
auto newStatement = MmapPool.instance.make!Statement;
|
||||||
{
|
newStatement.subroutine = target;
|
||||||
assert(false, "Not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTMapping.VariableDeclaration visit(parser.VariableDeclaration declaration) @nogc
|
|
||||||
{
|
|
||||||
assert(false, "Not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTMapping.BangStatement visit(parser.BangStatement statement) @nogc
|
|
||||||
{
|
|
||||||
assert(false, "Not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTMapping.Block visit(parser.Block block) @nogc
|
|
||||||
{
|
|
||||||
auto target = defaultAllocator.make!Definition;
|
|
||||||
this.constants = transformConstants(block.definitions);
|
|
||||||
|
|
||||||
target.statements = block.statement.accept(this);
|
|
||||||
target.variableDeclarations = transformVariableDeclarations(block.variableDeclarations);
|
|
||||||
|
|
||||||
return target;
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTMapping.Expression visit(parser.Expression expression) @nogc
|
|
||||||
{
|
|
||||||
assert(false, "Not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTMapping.Number visit(parser.Number number) @nogc
|
|
||||||
{
|
|
||||||
assert(false, "Not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTMapping.Variable visit(parser.Variable variable) @nogc
|
|
||||||
{
|
|
||||||
assert(false, "Not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTMapping.BinaryExpression visit(parser.BinaryExpression) @nogc
|
|
||||||
{
|
|
||||||
assert(false, "Not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
private Number transformNumber(parser.Number number) @nogc
|
|
||||||
{
|
|
||||||
return defaultAllocator.make!Number(number.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Variable binaryExpression(parser.BinaryExpression binaryExpression,
|
|
||||||
ref Array!Statement statements) @nogc
|
|
||||||
{
|
|
||||||
auto target = defaultAllocator.make!BinaryExpression(
|
|
||||||
expression(binaryExpression.lhs, statements),
|
|
||||||
expression(binaryExpression.rhs, statements),
|
|
||||||
binaryExpression.operator
|
|
||||||
);
|
|
||||||
|
|
||||||
auto newStatement = defaultAllocator.make!Statement;
|
|
||||||
newStatement.expression = target;
|
|
||||||
statements.insertBack(newStatement);
|
statements.insertBack(newStatement);
|
||||||
|
|
||||||
auto newVariable = defaultAllocator.make!Variable;
|
auto newVariable = MmapPool.instance.make!Variable;
|
||||||
newVariable.counter = statements.length;
|
newVariable.counter = statements.length;
|
||||||
|
|
||||||
return newVariable;
|
return newVariable;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Expression expression(parser.Expression expression,
|
private Expression transformExpression(parser.Expression expression,
|
||||||
ref Array!Statement statements) @nogc
|
ref Array!Statement statements,
|
||||||
{
|
ref HashTable!(String, int) constants) @nogc
|
||||||
|
{
|
||||||
if ((cast(parser.Number) expression) !is null)
|
if ((cast(parser.Number) expression) !is null)
|
||||||
{
|
{
|
||||||
auto numberExpression = defaultAllocator.make!Number;
|
auto numberExpression = MmapPool.instance.make!Number;
|
||||||
numberExpression.value = (cast(parser.Number) expression).value;
|
numberExpression.value = (cast(parser.Number) expression).value;
|
||||||
|
|
||||||
return numberExpression;
|
return numberExpression;
|
||||||
}
|
}
|
||||||
if ((cast(parser.Variable) expression) !is null)
|
if ((cast(parser.Variable) expression) !is null)
|
||||||
{
|
{
|
||||||
auto numberExpression = defaultAllocator.make!Number;
|
auto numberExpression = MmapPool.instance.make!Number;
|
||||||
numberExpression.value = this.constants[(cast(parser.Variable) expression).identifier];
|
numberExpression.value = constants[(cast(parser.Variable) expression).identifier];
|
||||||
|
|
||||||
return numberExpression;
|
return numberExpression;
|
||||||
}
|
}
|
||||||
else if ((cast(parser.BinaryExpression) expression) !is null)
|
else if ((cast(parser.Subroutine) expression) !is null)
|
||||||
{
|
{
|
||||||
return binaryExpression(cast(parser.BinaryExpression) expression, statements);
|
return transformSubroutine(cast(parser.Subroutine) expression, statements, constants);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
override Array!Statement visit(parser.Statement statement) @nogc
|
Expression transformStatement(parser.Statement statement,
|
||||||
{
|
ref Array!Statement statements,
|
||||||
typeof(return) statements;
|
ref HashTable!(String, int) constants) @nogc
|
||||||
|
{
|
||||||
if ((cast(parser.BangStatement) statement) !is null)
|
if ((cast(parser.BangStatement) statement) !is null)
|
||||||
{
|
{
|
||||||
expression((cast(parser.BangStatement) statement).expression, statements);
|
return transformExpression((cast(parser.BangStatement) statement).expression, statements, constants);
|
||||||
}
|
|
||||||
return statements;
|
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private HashTable!(String, int) transformConstants(ref Array!(parser.Definition) definitions) @nogc
|
HashTable!(String, int) transformConstants(ref Array!(parser.Definition) definitions) @nogc
|
||||||
{
|
{
|
||||||
typeof(return) constants;
|
typeof(return) constants;
|
||||||
|
|
||||||
foreach (definition; definitions[])
|
foreach (definition; definitions[])
|
||||||
@ -253,20 +115,30 @@ final class TransformVisitor : parser.ParserVisitor!ASTMapping
|
|||||||
}
|
}
|
||||||
|
|
||||||
return constants;
|
return constants;
|
||||||
}
|
}
|
||||||
|
|
||||||
Array!VariableDeclaration transformVariableDeclarations(ref Array!(parser.VariableDeclaration) variableDeclarations)
|
Array!VariableDeclaration transformVariableDeclarations(ref Array!(parser.VariableDeclaration) variableDeclarations)
|
||||||
@nogc
|
@nogc
|
||||||
{
|
{
|
||||||
typeof(return) variables;
|
typeof(return) variables;
|
||||||
|
|
||||||
foreach (ref variableDeclaration; variableDeclarations)
|
foreach (ref variableDeclaration; variableDeclarations)
|
||||||
{
|
{
|
||||||
auto newDeclaration = defaultAllocator.make!VariableDeclaration;
|
auto newDeclaration = MmapPool.instance.make!VariableDeclaration;
|
||||||
newDeclaration.identifier = variableDeclaration.identifier;
|
newDeclaration.identifier = variableDeclaration.identifier;
|
||||||
variables.insertBack(newDeclaration);
|
variables.insertBack(newDeclaration);
|
||||||
}
|
}
|
||||||
|
|
||||||
return variables;
|
return variables;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Definition transform(parser.Block block) @nogc
|
||||||
|
{
|
||||||
|
auto target = MmapPool.instance.make!Definition;
|
||||||
|
auto constants = transformConstants(block.definitions);
|
||||||
|
|
||||||
|
transformStatement(block.statement, target.statements, constants);
|
||||||
|
target.variableDeclarations = transformVariableDeclarations(block.variableDeclarations);
|
||||||
|
|
||||||
|
return target;
|
||||||
}
|
}
|
||||||
|
@ -7,13 +7,14 @@ import elna.result;
|
|||||||
import std.range;
|
import std.range;
|
||||||
import tanya.container.array;
|
import tanya.container.array;
|
||||||
import tanya.container.string;
|
import tanya.container.string;
|
||||||
|
import tanya.memory.mmappool;
|
||||||
|
|
||||||
struct Token
|
struct Token
|
||||||
{
|
{
|
||||||
enum Type
|
enum Type
|
||||||
{
|
{
|
||||||
number,
|
number,
|
||||||
operator,
|
subroutine, // Operator.
|
||||||
let,
|
let,
|
||||||
identifier,
|
identifier,
|
||||||
equals,
|
equals,
|
||||||
@ -53,7 +54,7 @@ struct Token
|
|||||||
|
|
||||||
this()(Type type, auto ref String value, Position position)
|
this()(Type type, auto ref String value, Position position)
|
||||||
@nogc nothrow pure @trusted
|
@nogc nothrow pure @trusted
|
||||||
in (type == Type.identifier || type == Type.operator)
|
in (type == Type.identifier)
|
||||||
{
|
{
|
||||||
this(type, position);
|
this(type, position);
|
||||||
this.value_.identifier = value;
|
this.value_.identifier = value;
|
||||||
@ -77,7 +78,7 @@ struct Token
|
|||||||
{
|
{
|
||||||
return this.value_.number;
|
return this.value_.number;
|
||||||
}
|
}
|
||||||
else static if (type == Type.identifier || type == Type.operator)
|
else static if (type == Type.identifier)
|
||||||
{
|
{
|
||||||
return this.value_.identifier;
|
return this.value_.identifier;
|
||||||
}
|
}
|
||||||
@ -160,7 +161,7 @@ struct Source
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Result!(Array!Token) lex(char[] buffer) @nogc
|
Array!Token lex(char[] buffer) @nogc
|
||||||
{
|
{
|
||||||
Array!Token tokens;
|
Array!Token tokens;
|
||||||
auto source = Source(buffer);
|
auto source = Source(buffer);
|
||||||
@ -233,12 +234,9 @@ Result!(Array!Token) lex(char[] buffer) @nogc
|
|||||||
}
|
}
|
||||||
source.popFrontN(i);
|
source.popFrontN(i);
|
||||||
}
|
}
|
||||||
else if (source.front == '+' || source.front == '-')
|
else if (source.front == '+') // Multi-character, random special characters.
|
||||||
{
|
{
|
||||||
String operator;
|
tokens.insertBack(Token(Token.Type.subroutine, source.position));
|
||||||
|
|
||||||
operator.insertBack(source.front);
|
|
||||||
tokens.insertBack(Token(Token.Type.operator, operator, source.position));
|
|
||||||
source.popFront;
|
source.popFront;
|
||||||
}
|
}
|
||||||
else if (source.front == '\n')
|
else if (source.front == '\n')
|
||||||
@ -247,8 +245,8 @@ Result!(Array!Token) lex(char[] buffer) @nogc
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return typeof(return)("Unexptected next character", source.position);
|
return typeof(tokens)(); // Error.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return typeof(return)(tokens);
|
return tokens;
|
||||||
}
|
}
|
||||||
|
@ -5,172 +5,100 @@ import elna.result;
|
|||||||
import tanya.container.array;
|
import tanya.container.array;
|
||||||
import tanya.container.string;
|
import tanya.container.string;
|
||||||
import tanya.memory.allocator;
|
import tanya.memory.allocator;
|
||||||
|
import tanya.memory.mmappool;
|
||||||
|
|
||||||
/**
|
|
||||||
* Parser visitor.
|
|
||||||
*/
|
|
||||||
interface ParserVisitor(Mapping)
|
|
||||||
{
|
|
||||||
Mapping.Node visit(Node) @nogc;
|
|
||||||
Mapping.Definition visit(Definition) @nogc;
|
|
||||||
Mapping.VariableDeclaration visit(VariableDeclaration) @nogc;
|
|
||||||
Mapping.Statement visit(Statement) @nogc;
|
|
||||||
Mapping.BangStatement visit(BangStatement) @nogc;
|
|
||||||
Mapping.Block visit(Block) @nogc;
|
|
||||||
Mapping.Expression visit(Expression) @nogc;
|
|
||||||
Mapping.Number visit(Number) @nogc;
|
|
||||||
Mapping.Variable visit(Variable) @nogc;
|
|
||||||
Mapping.BinaryExpression visit(BinaryExpression) @nogc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* AST node.
|
|
||||||
*/
|
|
||||||
abstract class Node
|
|
||||||
{
|
|
||||||
Mapping.Node accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
|
||||||
{
|
|
||||||
return visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constant definition.
|
* Constant definition.
|
||||||
*/
|
*/
|
||||||
class Definition : Node
|
class Definition
|
||||||
{
|
{
|
||||||
Number number;
|
Number number;
|
||||||
String identifier;
|
String identifier;
|
||||||
|
|
||||||
Mapping.Definition accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
|
||||||
{
|
|
||||||
return visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Variable declaration.
|
* Variable declaration.
|
||||||
*/
|
*/
|
||||||
class VariableDeclaration : Node
|
class VariableDeclaration
|
||||||
{
|
{
|
||||||
String identifier;
|
String identifier;
|
||||||
|
|
||||||
Mapping.VariableDeclaration accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
|
||||||
{
|
|
||||||
return visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class Statement : Node
|
abstract class Statement
|
||||||
{
|
{
|
||||||
Mapping.Statement accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
|
||||||
{
|
|
||||||
return visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class BangStatement : Statement
|
class BangStatement : Statement
|
||||||
{
|
{
|
||||||
Expression expression;
|
Expression expression;
|
||||||
|
|
||||||
Mapping.BangStatement accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
|
||||||
{
|
|
||||||
return visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Block : Node
|
class Block
|
||||||
{
|
{
|
||||||
Array!Definition definitions;
|
Array!Definition definitions;
|
||||||
Array!VariableDeclaration variableDeclarations;
|
Array!VariableDeclaration variableDeclarations;
|
||||||
Statement statement;
|
Statement statement;
|
||||||
|
|
||||||
Mapping.Block accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
|
||||||
{
|
|
||||||
return visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class Expression : Node
|
abstract class Expression
|
||||||
{
|
{
|
||||||
Mapping.Expression accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
|
||||||
{
|
|
||||||
return visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Number : Expression
|
class Number : Expression
|
||||||
{
|
{
|
||||||
int value;
|
int value;
|
||||||
|
|
||||||
Mapping.Number accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
|
||||||
{
|
|
||||||
return visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Variable : Expression
|
class Variable : Expression
|
||||||
{
|
{
|
||||||
String identifier;
|
String identifier;
|
||||||
|
|
||||||
Mapping.Variable accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
|
||||||
{
|
|
||||||
return visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum BinaryOperator
|
class Subroutine : Expression
|
||||||
{
|
|
||||||
sum,
|
|
||||||
subtraction
|
|
||||||
}
|
|
||||||
|
|
||||||
class BinaryExpression : Expression
|
|
||||||
{
|
{
|
||||||
Expression lhs, rhs;
|
Expression lhs, rhs;
|
||||||
BinaryOperator operator;
|
|
||||||
|
|
||||||
this(Expression lhs, Expression rhs, String operator) @nogc
|
|
||||||
{
|
|
||||||
this.lhs = lhs;
|
|
||||||
this.rhs = rhs;
|
|
||||||
if (operator == "+")
|
|
||||||
{
|
|
||||||
this.operator = BinaryOperator.sum;
|
|
||||||
}
|
|
||||||
else if (operator == "-")
|
|
||||||
{
|
|
||||||
this.operator = BinaryOperator.subtraction;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
assert(false, "Invalid binary operator");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Mapping.BinaryExpression accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
|
||||||
{
|
|
||||||
return visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Result!Expression parseFactor(ref Array!Token.Range tokens) @nogc
|
private Result!Expression parseExpression(ref Array!(Token).Range tokens) @nogc
|
||||||
in (!tokens.empty, "Expected factor, got end of stream")
|
in (!tokens.empty, "Expected expression, got end of stream")
|
||||||
{
|
{
|
||||||
if (tokens.front.ofType(Token.Type.identifier))
|
if (tokens.front.ofType(Token.Type.number))
|
||||||
{
|
{
|
||||||
auto variable = defaultAllocator.make!Variable;
|
auto number = MmapPool.instance.make!Number;
|
||||||
|
number.value = tokens.front.value!(Token.Type.number);
|
||||||
|
tokens.popFront;
|
||||||
|
return Result!Expression(number);
|
||||||
|
}
|
||||||
|
else if (tokens.front.ofType(Token.Type.identifier))
|
||||||
|
{
|
||||||
|
auto variable = MmapPool.instance.make!Variable;
|
||||||
variable.identifier = tokens.front.value!(Token.Type.identifier);
|
variable.identifier = tokens.front.value!(Token.Type.identifier);
|
||||||
tokens.popFront;
|
tokens.popFront;
|
||||||
return Result!Expression(variable);
|
return Result!Expression(variable);
|
||||||
}
|
}
|
||||||
else if (tokens.front.ofType(Token.Type.number))
|
else if (tokens.front.ofType(Token.Type.subroutine))
|
||||||
{
|
{
|
||||||
auto number = defaultAllocator.make!Number;
|
auto subroutine = MmapPool.instance.make!Subroutine;
|
||||||
number.value = tokens.front.value!(Token.Type.number);
|
|
||||||
tokens.popFront;
|
tokens.popFront;
|
||||||
return Result!Expression(number);
|
auto expression = parseExpression(tokens);
|
||||||
|
if (expression.valid)
|
||||||
|
{
|
||||||
|
subroutine.lhs = expression.result;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return Result!Expression("Expected left-hand side to be an expression", tokens.front.position);
|
||||||
|
}
|
||||||
|
expression = parseExpression(tokens);
|
||||||
|
if (expression.valid)
|
||||||
|
{
|
||||||
|
subroutine.rhs = expression.result;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return Result!Expression("Expected left-hand side to be an expression", tokens.front.position);
|
||||||
|
}
|
||||||
|
return Result!Expression(subroutine);
|
||||||
}
|
}
|
||||||
else if (tokens.front.ofType(Token.Type.leftParen))
|
else if (tokens.front.ofType(Token.Type.leftParen))
|
||||||
{
|
{
|
||||||
@ -181,44 +109,13 @@ in (!tokens.empty, "Expected factor, got end of stream")
|
|||||||
tokens.popFront;
|
tokens.popFront;
|
||||||
return expression;
|
return expression;
|
||||||
}
|
}
|
||||||
return Result!Expression("Expected a factor", tokens.front.position);
|
return Result!Expression("Expected an expression", tokens.front.position);
|
||||||
}
|
|
||||||
|
|
||||||
private Result!Expression parseTerm(ref Array!(Token).Range tokens) @nogc
|
|
||||||
{
|
|
||||||
return parseFactor(tokens);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Result!Expression parseExpression(ref Array!(Token).Range tokens) @nogc
|
|
||||||
in (!tokens.empty, "Expected expression, got end of stream")
|
|
||||||
{
|
|
||||||
auto term = parseTerm(tokens);
|
|
||||||
if (!term.valid || tokens.empty || !tokens.front.ofType(Token.Type.operator))
|
|
||||||
{
|
|
||||||
return term;
|
|
||||||
}
|
|
||||||
auto operator = tokens.front.value!(Token.Type.operator);
|
|
||||||
tokens.popFront;
|
|
||||||
|
|
||||||
auto expression = parseExpression(tokens);
|
|
||||||
|
|
||||||
if (expression.valid)
|
|
||||||
{
|
|
||||||
auto binaryExpression = defaultAllocator
|
|
||||||
.make!BinaryExpression(term.result, expression.result, operator);
|
|
||||||
|
|
||||||
return Result!Expression(binaryExpression);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return Result!Expression("Expected right-hand side to be an expression", tokens.front.position);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Result!Definition parseDefinition(ref Array!Token.Range tokens) @nogc
|
private Result!Definition parseDefinition(ref Array!Token.Range tokens) @nogc
|
||||||
in (!tokens.empty, "Expected definition, got end of stream")
|
in (!tokens.empty, "Expected definition, got end of stream")
|
||||||
{
|
{
|
||||||
auto definition = defaultAllocator.make!Definition;
|
auto definition = MmapPool.instance.make!Definition;
|
||||||
definition.identifier = tokens.front.value!(Token.Type.identifier); // Copy.
|
definition.identifier = tokens.front.value!(Token.Type.identifier); // Copy.
|
||||||
|
|
||||||
tokens.popFront();
|
tokens.popFront();
|
||||||
@ -226,7 +123,7 @@ in (!tokens.empty, "Expected definition, got end of stream")
|
|||||||
|
|
||||||
if (tokens.front.ofType(Token.Type.number))
|
if (tokens.front.ofType(Token.Type.number))
|
||||||
{
|
{
|
||||||
auto number = defaultAllocator.make!Number;
|
auto number = MmapPool.instance.make!Number;
|
||||||
number.value = tokens.front.value!(Token.Type.number);
|
number.value = tokens.front.value!(Token.Type.number);
|
||||||
definition.number = number;
|
definition.number = number;
|
||||||
tokens.popFront;
|
tokens.popFront;
|
||||||
@ -241,7 +138,7 @@ in (!tokens.empty, "Expected block, got end of stream")
|
|||||||
if (tokens.front.ofType(Token.Type.bang))
|
if (tokens.front.ofType(Token.Type.bang))
|
||||||
{
|
{
|
||||||
tokens.popFront;
|
tokens.popFront;
|
||||||
auto statement = defaultAllocator.make!BangStatement;
|
auto statement = MmapPool.instance.make!BangStatement;
|
||||||
auto expression = parseExpression(tokens);
|
auto expression = parseExpression(tokens);
|
||||||
if (expression.valid)
|
if (expression.valid)
|
||||||
{
|
{
|
||||||
@ -296,7 +193,7 @@ in (!tokens.empty, "Expected variable declarations, got end of stream")
|
|||||||
auto currentToken = tokens.front;
|
auto currentToken = tokens.front;
|
||||||
if (currentToken.ofType(Token.Type.identifier))
|
if (currentToken.ofType(Token.Type.identifier))
|
||||||
{
|
{
|
||||||
auto variableDeclaration = defaultAllocator.make!VariableDeclaration;
|
auto variableDeclaration = MmapPool.instance.make!VariableDeclaration;
|
||||||
variableDeclaration.identifier = currentToken.value!(Token.Type.identifier);
|
variableDeclaration.identifier = currentToken.value!(Token.Type.identifier);
|
||||||
variableDeclarations.insertBack(variableDeclaration);
|
variableDeclarations.insertBack(variableDeclaration);
|
||||||
tokens.popFront;
|
tokens.popFront;
|
||||||
@ -325,7 +222,7 @@ in (!tokens.empty, "Expected variable declarations, got end of stream")
|
|||||||
private Result!Block parseBlock(ref Array!Token.Range tokens) @nogc
|
private Result!Block parseBlock(ref Array!Token.Range tokens) @nogc
|
||||||
in (!tokens.empty, "Expected block, got end of stream")
|
in (!tokens.empty, "Expected block, got end of stream")
|
||||||
{
|
{
|
||||||
auto block = defaultAllocator.make!Block;
|
auto block = MmapPool.instance.make!Block;
|
||||||
if (tokens.front.ofType(Token.Type.let))
|
if (tokens.front.ofType(Token.Type.let))
|
||||||
{
|
{
|
||||||
auto constDefinitions = parseDefinitions(tokens);
|
auto constDefinitions = parseDefinitions(tokens);
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
module elna.result;
|
module elna.result;
|
||||||
|
|
||||||
import std.typecons;
|
import std.typecons;
|
||||||
import tanya.container.array;
|
|
||||||
import tanya.container.string;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Position in the source text.
|
* Position in the source text.
|
||||||
@ -84,24 +82,3 @@ struct Result(T)
|
|||||||
return error.isNull;
|
return error.isNull;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Reference
|
|
||||||
{
|
|
||||||
enum Target
|
|
||||||
{
|
|
||||||
text,
|
|
||||||
high20,
|
|
||||||
lower12i
|
|
||||||
}
|
|
||||||
|
|
||||||
String name;
|
|
||||||
size_t offset;
|
|
||||||
Target target;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Symbol
|
|
||||||
{
|
|
||||||
String name;
|
|
||||||
Array!ubyte text;
|
|
||||||
Array!Reference symbols;
|
|
||||||
}
|
|
||||||
|
@ -1,352 +0,0 @@
|
|||||||
module elna.riscv;
|
|
||||||
|
|
||||||
import elna.extended;
|
|
||||||
import elna.ir;
|
|
||||||
import elna.result;
|
|
||||||
import std.algorithm;
|
|
||||||
import std.typecons;
|
|
||||||
import tanya.container.array;
|
|
||||||
import tanya.container.string;
|
|
||||||
import tanya.memory.allocator;
|
|
||||||
|
|
||||||
enum XRegister : ubyte
|
|
||||||
{
|
|
||||||
zero = 0,
|
|
||||||
ra = 1,
|
|
||||||
sp = 2,
|
|
||||||
gp = 3,
|
|
||||||
tp = 4,
|
|
||||||
t0 = 5,
|
|
||||||
t1 = 6,
|
|
||||||
t2 = 7,
|
|
||||||
s0 = 8,
|
|
||||||
s1 = 9,
|
|
||||||
a0 = 10,
|
|
||||||
a1 = 11,
|
|
||||||
a2 = 12,
|
|
||||||
a3 = 13,
|
|
||||||
a4 = 14,
|
|
||||||
a5 = 15,
|
|
||||||
a6 = 16,
|
|
||||||
a7 = 17,
|
|
||||||
s2 = 18,
|
|
||||||
s3 = 19,
|
|
||||||
s4 = 20,
|
|
||||||
s5 = 21,
|
|
||||||
s6 = 22,
|
|
||||||
s7 = 23,
|
|
||||||
s8 = 24,
|
|
||||||
s9 = 25,
|
|
||||||
s10 = 26,
|
|
||||||
s11 = 27,
|
|
||||||
t3 = 28,
|
|
||||||
t4 = 29,
|
|
||||||
t5 = 30,
|
|
||||||
t6 = 31,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Funct3 : ubyte
|
|
||||||
{
|
|
||||||
addi = 0b000,
|
|
||||||
slti = 0b001,
|
|
||||||
sltiu = 0b011,
|
|
||||||
andi = 0b111,
|
|
||||||
ori = 0b110,
|
|
||||||
xori = 0b100,
|
|
||||||
slli = 0b000,
|
|
||||||
srli = 0b101,
|
|
||||||
srai = 0b101,
|
|
||||||
add = 0b000,
|
|
||||||
slt = 0b010,
|
|
||||||
sltu = 0b011,
|
|
||||||
and = 0b111,
|
|
||||||
or = 0b110,
|
|
||||||
xor = 0b100,
|
|
||||||
sll = 0b001,
|
|
||||||
srl = 0b101,
|
|
||||||
sub = 0b000,
|
|
||||||
sra = 0b101,
|
|
||||||
beq = 0b000,
|
|
||||||
bne = 0b001,
|
|
||||||
blt = 0b100,
|
|
||||||
bltu = 0b110,
|
|
||||||
bge = 0b101,
|
|
||||||
bgeu = 0b111,
|
|
||||||
fence = 0b000,
|
|
||||||
fenceI = 0b001,
|
|
||||||
csrrw = 0b001,
|
|
||||||
csrrs = 0b010,
|
|
||||||
csrrc = 0b011,
|
|
||||||
csrrwi = 0b101,
|
|
||||||
csrrsi = 0b110,
|
|
||||||
csrrci = 0b111,
|
|
||||||
priv = 0b000,
|
|
||||||
sb = 0b000,
|
|
||||||
sh = 0b001,
|
|
||||||
sw = 0b010,
|
|
||||||
lb = 0b000,
|
|
||||||
lh = 0b001,
|
|
||||||
lw = 0b010,
|
|
||||||
lbu = 0b100,
|
|
||||||
lhu = 0b101,
|
|
||||||
jalr = 0b000,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Funct12 : ubyte
|
|
||||||
{
|
|
||||||
ecall = 0b000000000000,
|
|
||||||
ebreak = 0b000000000001,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Funct7 : ubyte
|
|
||||||
{
|
|
||||||
none = 0,
|
|
||||||
sub = 0b0100000
|
|
||||||
}
|
|
||||||
|
|
||||||
enum BaseOpcode : ubyte
|
|
||||||
{
|
|
||||||
opImm = 0b0010011,
|
|
||||||
lui = 0b0110111,
|
|
||||||
auipc = 0b0010111,
|
|
||||||
op = 0b0110011,
|
|
||||||
jal = 0b1101111,
|
|
||||||
jalr = 0b1100111,
|
|
||||||
branch = 0b1100011,
|
|
||||||
load = 0b0000011,
|
|
||||||
store = 0b0100011,
|
|
||||||
miscMem = 0b0001111,
|
|
||||||
system = 0b1110011,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Instruction
|
|
||||||
{
|
|
||||||
private uint instruction;
|
|
||||||
|
|
||||||
this(BaseOpcode opcode) @nogc
|
|
||||||
{
|
|
||||||
this.instruction = opcode;
|
|
||||||
}
|
|
||||||
@disable this();
|
|
||||||
|
|
||||||
ref Instruction i(XRegister rd, Funct3 funct3, XRegister rs1, uint immediate)
|
|
||||||
return scope @nogc
|
|
||||||
{
|
|
||||||
this.instruction |= (rd << 7)
|
|
||||||
| (funct3 << 12)
|
|
||||||
| (rs1 << 15)
|
|
||||||
| (immediate << 20);
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
ref Instruction s(uint imm1, Funct3 funct3, XRegister rs1, XRegister rs2, uint imm2 = 0)
|
|
||||||
return scope @nogc
|
|
||||||
{
|
|
||||||
this.instruction |= (imm1 << 7)
|
|
||||||
| (funct3 << 12)
|
|
||||||
| (rs1 << 15)
|
|
||||||
| (rs2 << 20)
|
|
||||||
| (imm2 << 25);
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
ref Instruction r(XRegister rd, Funct3 funct3, XRegister rs1, XRegister rs2, Funct7 funct7 = Funct7.none)
|
|
||||||
return scope @nogc
|
|
||||||
{
|
|
||||||
this.instruction |= (rd << 7)
|
|
||||||
| (funct3 << 12)
|
|
||||||
| (rs1 << 15)
|
|
||||||
| (rs2 << 20)
|
|
||||||
| (funct7 << 25);
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
ref Instruction u(XRegister rd, uint imm)
|
|
||||||
return scope @nogc
|
|
||||||
{
|
|
||||||
this.instruction |= (rd << 7) | (imm << 12);
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
ubyte[] encode() return scope @nogc
|
|
||||||
{
|
|
||||||
return (cast(ubyte*) (&this.instruction))[0 .. uint.sizeof];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class RiscVVisitor : IRVisitor
|
|
||||||
{
|
|
||||||
Array!Instruction instructions;
|
|
||||||
bool registerInUse;
|
|
||||||
uint variableCounter = 1;
|
|
||||||
Array!Reference references;
|
|
||||||
|
|
||||||
override void visit(Node) @nogc
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
override void visit(Definition definition) @nogc
|
|
||||||
{
|
|
||||||
// Prologue.
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.opImm)
|
|
||||||
.i(XRegister.sp, Funct3.addi, XRegister.sp, cast(uint) -32)
|
|
||||||
);
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.store)
|
|
||||||
.s(28, Funct3.sw, XRegister.sp, XRegister.s0)
|
|
||||||
);
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.store)
|
|
||||||
.s(24, Funct3.sw, XRegister.sp, XRegister.ra)
|
|
||||||
);
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.opImm)
|
|
||||||
.i(XRegister.s0, Funct3.addi, XRegister.sp, 32)
|
|
||||||
);
|
|
||||||
|
|
||||||
foreach (statement; definition.statements[])
|
|
||||||
{
|
|
||||||
statement.accept(this);
|
|
||||||
}
|
|
||||||
foreach (variableDeclaration; definition.variableDeclarations[])
|
|
||||||
{
|
|
||||||
variableDeclaration.accept(this);
|
|
||||||
}
|
|
||||||
// Print the result.
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.opImm)
|
|
||||||
.i(XRegister.a1, Funct3.addi, XRegister.a0, 0)
|
|
||||||
);
|
|
||||||
this.references.insertBack(Reference(String(".CL0"), instructions.length * 4, Reference.Target.high20));
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.lui).u(XRegister.a5, 0)
|
|
||||||
);
|
|
||||||
this.references.insertBack(Reference(String(".CL0"), instructions.length * 4, Reference.Target.lower12i));
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.opImm).i(XRegister.a0, Funct3.addi, XRegister.a5, 0)
|
|
||||||
);
|
|
||||||
this.references.insertBack(Reference(String("printf"), instructions.length * 4, Reference.Target.text));
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.auipc).u(XRegister.ra, 0)
|
|
||||||
);
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.jalr)
|
|
||||||
.i(XRegister.ra, Funct3.jalr, XRegister.ra, 0)
|
|
||||||
);
|
|
||||||
// Set the return value (0).
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.op)
|
|
||||||
.r(XRegister.a0, Funct3.and, XRegister.zero, XRegister.zero)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Epilogue.
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.load)
|
|
||||||
.i(XRegister.s0, Funct3.lw, XRegister.sp, 28)
|
|
||||||
);
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.load)
|
|
||||||
.i(XRegister.ra, Funct3.lw, XRegister.sp, 24)
|
|
||||||
);
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.opImm)
|
|
||||||
.i(XRegister.sp, Funct3.addi, XRegister.sp, 32)
|
|
||||||
);
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.jalr)
|
|
||||||
.i(XRegister.zero, Funct3.jalr, XRegister.ra, 0)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
override void visit(Expression) @nogc
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
override void visit(Statement statement) @nogc
|
|
||||||
{
|
|
||||||
statement.expression.accept(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
override void visit(Variable variable) @nogc
|
|
||||||
{
|
|
||||||
const freeRegister = this.registerInUse ? XRegister.a0 : XRegister.t0;
|
|
||||||
|
|
||||||
// movl -x(%rbp), %eax; where x is a number.
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.load)
|
|
||||||
.i(freeRegister, Funct3.lw, XRegister.sp,
|
|
||||||
cast(byte) (variable.counter * 4))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
override void visit(VariableDeclaration) @nogc
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
override void visit(Number number) @nogc
|
|
||||||
{
|
|
||||||
const freeRegister = this.registerInUse ? XRegister.a0 : XRegister.t0;
|
|
||||||
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.opImm) // movl $x, %eax; where $x is a number.
|
|
||||||
.i(freeRegister, Funct3.addi, XRegister.zero, number.value)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
override void visit(BinaryExpression expression) @nogc
|
|
||||||
{
|
|
||||||
this.registerInUse = true;
|
|
||||||
expression.lhs.accept(this);
|
|
||||||
this.registerInUse = false;
|
|
||||||
expression.rhs.accept(this);
|
|
||||||
|
|
||||||
// Calculate the result and assign it to a variable on the stack.
|
|
||||||
final switch (expression.operator)
|
|
||||||
{
|
|
||||||
case BinaryOperator.sum:
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.op)
|
|
||||||
.r(XRegister.a0, Funct3.add, XRegister.a0, XRegister.t0)
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
case BinaryOperator.subtraction:
|
|
||||||
this.instructions.insertBack(
|
|
||||||
Instruction(BaseOpcode.op)
|
|
||||||
.r(XRegister.a0, Funct3.sub, XRegister.a0, XRegister.t0, Funct7.sub)
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
this.instructions.insertBack( // movl %eax, -x(%rbp); where x is a number.
|
|
||||||
Instruction(BaseOpcode.store)
|
|
||||||
.s(cast(uint) (this.variableCounter * 4), Funct3.sw, XRegister.sp, XRegister.a0)
|
|
||||||
);
|
|
||||||
|
|
||||||
++this.variableCounter;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Symbol writeNext(Definition ast) @nogc
|
|
||||||
{
|
|
||||||
Array!Instruction instructions;
|
|
||||||
Array!Reference references;
|
|
||||||
auto visitor = defaultAllocator.make!RiscVVisitor;
|
|
||||||
scope (exit)
|
|
||||||
{
|
|
||||||
defaultAllocator.dispose(visitor);
|
|
||||||
}
|
|
||||||
visitor.visit(ast);
|
|
||||||
|
|
||||||
auto program = Symbol(String("main"));
|
|
||||||
|
|
||||||
program.symbols = move(visitor.references);
|
|
||||||
foreach (ref instruction; visitor.instructions)
|
|
||||||
{
|
|
||||||
program.text.insertBack(instruction.encode);
|
|
||||||
}
|
|
||||||
return program;
|
|
||||||
}
|
|
@ -1,33 +1,72 @@
|
|||||||
import elna.backend;
|
import core.stdc.stdio;
|
||||||
|
import core.stdc.string;
|
||||||
|
import core.stdc.stdlib;
|
||||||
|
import elna.lexer;
|
||||||
|
import elna.parser;
|
||||||
|
import elna.generator;
|
||||||
import elna.ir;
|
import elna.ir;
|
||||||
import elna.arguments;
|
|
||||||
import std.path;
|
|
||||||
import std.sumtype;
|
|
||||||
import tanya.container.string;
|
import tanya.container.string;
|
||||||
import tanya.memory.allocator;
|
import tanya.memory.allocator;
|
||||||
import tanya.memory.mmappool;
|
import tanya.memory.mmappool;
|
||||||
|
|
||||||
|
private char[] readSource(size_t N)(string source, out char[N] buffer) @nogc
|
||||||
|
{
|
||||||
|
memcpy(buffer.ptr, source.ptr, source.length + 1);
|
||||||
|
buffer[source.length] = '\0';
|
||||||
|
auto handle = fopen(buffer.ptr, "r");
|
||||||
|
if (handle is null)
|
||||||
|
{
|
||||||
|
perror(buffer.ptr);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
fseek(handle, 0, SEEK_END);
|
||||||
|
size_t fsize = ftell(handle);
|
||||||
|
rewind(handle);
|
||||||
|
|
||||||
|
fread(buffer.ptr, fsize, 1, handle);
|
||||||
|
fclose(handle);
|
||||||
|
buffer[fsize] = '\0';
|
||||||
|
|
||||||
|
return buffer[0 .. fsize];
|
||||||
|
}
|
||||||
|
|
||||||
int main(string[] args)
|
int main(string[] args)
|
||||||
{
|
{
|
||||||
|
char[255] buffer;
|
||||||
|
|
||||||
defaultAllocator = MmapPool.instance;
|
defaultAllocator = MmapPool.instance;
|
||||||
|
|
||||||
return Arguments.parse(args).match!(
|
if (args.length < 2)
|
||||||
(ArgumentError argumentError) => 4,
|
|
||||||
(Arguments arguments) {
|
|
||||||
String outputFilename;
|
|
||||||
if (arguments.output is null)
|
|
||||||
{
|
{
|
||||||
outputFilename = arguments
|
return 4;
|
||||||
.inFile
|
|
||||||
.baseName
|
|
||||||
.withExtension("o");
|
|
||||||
}
|
}
|
||||||
else
|
auto sourceText = readSource(args[1], buffer);
|
||||||
|
if (sourceText is null)
|
||||||
{
|
{
|
||||||
outputFilename = String(arguments.output);
|
return 3;
|
||||||
}
|
}
|
||||||
|
auto tokens = lex(sourceText);
|
||||||
|
if (tokens.length == 0)
|
||||||
|
{
|
||||||
|
printf("Lexical analysis failed.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
auto ast = parse(tokens);
|
||||||
|
if (!ast.valid)
|
||||||
|
{
|
||||||
|
auto compileError = ast.error.get;
|
||||||
|
printf("%lu:%lu: %s\n", compileError.line, compileError.column, compileError.message.ptr);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
auto ir = transform(ast.result);
|
||||||
|
|
||||||
return generate(arguments.inFile, outputFilename);
|
String outputFilename = String("build/");
|
||||||
}
|
outputFilename.insertBack(args[1][0 .. $ - 4]);
|
||||||
);
|
outputFilename.insertBack("o");
|
||||||
|
writeObject(ir, outputFilename);
|
||||||
|
|
||||||
|
auto code = generate(ir);
|
||||||
|
printf("%s", code.toStringz());
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
const a = 1, b = 2;
|
const a = 1, b = 2;
|
||||||
! a + b
|
! + a b
|
||||||
.
|
.
|
@ -1 +0,0 @@
|
|||||||
8
|
|
@ -1 +0,0 @@
|
|||||||
1
|
|
@ -1,2 +0,0 @@
|
|||||||
! (3 + 4) + 1
|
|
||||||
.
|
|
@ -1,2 +0,0 @@
|
|||||||
! 5 - 4
|
|
||||||
.
|
|
@ -1,2 +0,0 @@
|
|||||||
! 1 + 7
|
|
||||||
.
|
|
2
tests/sum.elna
Normal file
2
tests/sum.elna
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
! + 1 7
|
||||||
|
.
|
@ -1,2 +0,0 @@
|
|||||||
! 1 + (3 + 4)
|
|
||||||
.
|
|
2
tests/sums.elna
Normal file
2
tests/sums.elna
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
! + 1 (+ 3 4)
|
||||||
|
.
|
Loading…
x
Reference in New Issue
Block a user