Skip to content

Commit

Permalink
temporarily disable yeti-16 backend
Browse files Browse the repository at this point in the history
  • Loading branch information
MESYETI committed Apr 2, 2024
1 parent 7dcf816 commit 2de8778
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 6 deletions.
10 changes: 7 additions & 3 deletions source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Flags:
Backends:
rm86 - Real mode x86
y16 - YETI-16 (work in progress)
";

int main(string[] args) {
Expand All @@ -46,6 +45,8 @@ int main(string[] args) {
string[] versions;
CompilerBackend backend = new BackendRM86();

writeln(backend.GetVersions());

for (size_t i = 1; i < args.length; ++ i) {
if (args[i][0] == '-') {
switch (args[i]) {
Expand Down Expand Up @@ -120,8 +121,7 @@ int main(string[] args) {
break;
}
case "y16": {
backend = new BackendY16();
break;
goto default;
}
default: {
stderr.writefln("Unknown backend '%s'", args[i]);
Expand All @@ -145,6 +145,8 @@ int main(string[] args) {
}
}

writeln(backend.GetVersions());

if (file == "") {
stderr.writeln("No source files");
return 1;
Expand All @@ -159,6 +161,8 @@ int main(string[] args) {
compiler.backend.orgSet = orgSet;

versions ~= compiler.backend.GetVersions();

writeln(backend.GetVersions());

auto preproc = new Preprocessor();
preproc.includeDirs = includeDirs;
Expand Down
113 changes: 110 additions & 3 deletions source/backends/y16.d
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module callisto.backends.y16;

import std.range;
import std.format;
import std.algorithm;
import callisto.util;
import callisto.error;
import callisto.parser;
import callisto.compiler;
import callisto.language;

private struct Word {
bool inline;
Expand All @@ -19,12 +22,32 @@ private struct Constant {
Node value;
}

private struct Variable {
string name;
Type type;
uint offset; // SP + offset to access
bool array;
ulong arraySize;

size_t Size() => array? arraySize * type.size : type.size;
}

private struct Global {
Type type;
bool array;
ulong arraySize;

size_t Size() => array? arraySize * type.size : type.size;
}

class BackendY16 : CompilerBackend {
Word[string] words;
Type[string] types;
bool inScope;
Constant[string] consts;
uint blockCounter;
Variable[] variables;
Global[string] globals;

override string[] GetVersions() => ["Y16"];

Expand All @@ -33,11 +56,11 @@ class BackendY16 : CompilerBackend {
types["i8"] = Type(1);
types["u16"] = Type(2);
types["i16"] = Type(2);
types["addr"] = Type(2);
types["addr"] = Type(3);
types["size"] = Type(2);
types["usize"] = Type(2);
types["cell"] = Type(2);
types["Array"] = Type(6);
types["Array"] = Type(7);

foreach (name, ref type ; types) {
NewConst(format("%s.sizeof", name), cast(long) type.size);
Expand All @@ -56,6 +79,22 @@ class BackendY16 : CompilerBackend {
void NewConst(string name, long value, ErrorInfo error = ErrorInfo.init) {
consts[name] = Constant(new IntegerNode(error, value));
}

bool VariableExists(string name) => variables.any!(v => v.name == name);

Variable GetVariable(string name) {
foreach (ref var ; variables) {
if (var.name == name) {
return var;
}
}

assert(0);
}

size_t GetStackSize() {
return variables.empty()? 0 : variables[0].offset + variables[0].type.size;
}

override void Init() {
output ~= "cpp sr bs\n";
Expand All @@ -65,6 +104,11 @@ class BackendY16 : CompilerBackend {

override void End() {
output ~= "hlt\n";

foreach (name, var ; globals) {
output ~= format("__global_%s: fill %d 0\n", name, var.Size());
}

output ~= "__stack: fill 1024 0\n"; // 512 cell stack
}

Expand All @@ -81,6 +125,29 @@ class BackendY16 : CompilerBackend {
output ~= format("callb __func__%s\n", node.name.Sanitise());
}
}
else if (VariableExists(node.name)) {
auto var = GetVariable(node.name);

output ~= "cpp gh sp\n";
output ~= format("ldi a %d\n", var.offset);
output ~= "wrw sr g\n";
output ~= "ldsi b 2\n";
output ~= "addp sr b\n";
output ~= "wrw sr h\n";
output ~= "addp sr b\n";
}
else if (node.name in globals) {
auto var = globals[node.name];

output ~= "cpp gh bs\n";
output ~= format("ldi a __global_%s\n", node.name.Sanitise());
output ~= "addp gh a\n";
output ~= "wrw sr g\n";
output ~= "ldsi b 2\n";
output ~= "addp sr b\n";
output ~= "wrw sr h\n";
output ~= "addp sr b\n";
}
else if (node.name in consts) {
compiler.CompileNode(consts[node.name].value);
}
Expand Down Expand Up @@ -184,7 +251,47 @@ class BackendY16 : CompilerBackend {
}

override void CompileLet(LetNode node) {
assert(0);
if (node.varType !in types) {
Error(node.error, "Undefined type '%s'", node.varType);
}
if (VariableExists(node.name) || (node.name in words)) {
Error(node.error, "Variable name '%s' already used", node.name);
}
if (Language.bannedNames.canFind(node.name)) {
Error(node.error, "Name '%s' can't be used", node.name);
}

if (inScope) {
foreach (ref var ; variables) {
var.offset += types[node.varType].size;
// idk but RM86 had a comment here saying TODO: fix this
// i have no memory of what needs to be fixed
// but RM86 works fine so i guess its ok
}

Variable var;
var.name = node.name;
var.type = types[node.varType];
var.offset = 0;
var.array = node.array;
var.arraySize = node.arraySize;

variables ~= var;

if (var.Size() == 2) {
output ~= "ldsi a 0\n";
output ~= "push a\n";
}
else {
output ~= format("ldi a %d\n", var.Size());
output ~= "subp sp a\n";
}
}
else {
Global global;
global.type = types[node.varType];
globals[node.name] = global;
}
}

override void CompileArray(ArrayNode node) {
Expand Down

0 comments on commit 2de8778

Please sign in to comment.