Skip to content

Commit

Permalink
refactor compiler to improve binary sizes and make it possible to com…
Browse files Browse the repository at this point in the history
…pile to languages other than assembly
  • Loading branch information
MESYETI committed Apr 16, 2024
1 parent 50b55db commit 60ff4f5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/helloWorld.cal
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ include "cores/select.cal"
include "std/io.cal"
include "std/array.cal"

"Hello, world!" printlstr
"Hello, world!\n" printlstr
4 changes: 4 additions & 0 deletions source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ int main(string[] args) {
auto optimiser = new Optimiser();
optimiser.Run(nodes);
nodes = optimiser.res;

foreach (ref func ; optimiser.usedFunctions) {
writeln(func);
}
}

try {
Expand Down
12 changes: 9 additions & 3 deletions source/backends/linux86.d
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ class BackendLinux86 : CompilerBackend {
format("rm %s.asm %s.o", compiler.outFile, compiler.outFile)
];

override void BeginMain() {
output ~= "__calmain:\n";
}

override void Init() {
output ~= "global _start\n";
output ~= "section .text\n";
Expand All @@ -144,6 +148,9 @@ class BackendLinux86 : CompilerBackend {

// copy static array constants
output ~= "call __copy_arrays\n";

// jump to main
output ~= "jmp __calmain\n";
}

override void End() {
Expand Down Expand Up @@ -256,14 +263,13 @@ class BackendLinux86 : CompilerBackend {

words[node.name] = Word(false, []);

output ~= format("jmp __func_end__%s\n", node.name.Sanitise());
output ~= format("__func__%s:\n", node.name.Sanitise());

foreach (ref inode ; node.nodes) {
compiler.CompileNode(inode);
}

output ~= format("__func_return__%s:\n", node.name.Sanitise());
//output ~= format("__func_return__%s:\n", node.name.Sanitise());

size_t scopeSize;
foreach (ref var ; variables) {
Expand All @@ -272,7 +278,7 @@ class BackendLinux86 : CompilerBackend {
output ~= format("add rsp, %d\n", scopeSize);

output ~= "ret\n";
output ~= format("__func_end__%s:\n", node.name.Sanitise());
//output ~= format("__func_end__%s:\n", node.name.Sanitise());
inScope = false;
variables = [];
}
Expand Down
10 changes: 7 additions & 3 deletions source/backends/rm86.d
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,14 @@ class BackendRM86 : CompilerBackend {
format("rm %s.asm", compiler.outFile)
];

override void BeginMain() {
output ~= "__calmain:\n";
}

override void Init() {
output ~= format("org 0x%.4X\n", org);
output ~= "mov si, __stack\n";
output ~= "jmp __calmain\n";
}

override void End() {
Expand Down Expand Up @@ -213,14 +218,13 @@ class BackendRM86 : CompilerBackend {

words[node.name] = Word(false, []);

output ~= format("jmp __func_end__%s\n", node.name.Sanitise());
output ~= format("__func__%s:\n", node.name.Sanitise());

foreach (ref inode ; node.nodes) {
compiler.CompileNode(inode);
}

output ~= format("__func_return__%s:\n", node.name.Sanitise());
//output ~= format("__func_return__%s:\n", node.name.Sanitise());

size_t scopeSize;
foreach (ref var ; variables) {
Expand All @@ -229,7 +233,7 @@ class BackendRM86 : CompilerBackend {
output ~= format("add sp, %d\n", scopeSize);

output ~= "ret\n";
output ~= format("__func_end__%s:\n", node.name.Sanitise());
//output ~= format("__func_end__%s:\n", node.name.Sanitise());
variables = [];
inScope = false;
}
Expand Down
11 changes: 8 additions & 3 deletions source/backends/y16.d
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,15 @@ class BackendY16 : CompilerBackend {
return size;
}

override void BeginMain() {
output ~= "__calmain:\n";
}

override void Init() {
output ~= "cpp sr bs\n";
output ~= "ldi a __stack\n";
output ~= "addp sr a\n";
output ~= "jmpb __calmain\n";
}

override void End() {
Expand Down Expand Up @@ -186,17 +191,17 @@ class BackendY16 : CompilerBackend {

words[node.name] = Word(false, []);

output ~= format("jmpb __func_end__%s\n", node.name.Sanitise());
//output ~= format("jmpb __func_end__%s\n", node.name.Sanitise());
output ~= format("__func__%s:\n", node.name.Sanitise());

foreach (ref inode ; node.nodes) {
compiler.CompileNode(inode);
}

output ~= format("__func_return__%s:\n", node.name.Sanitise());
//output ~= format("__func_return__%s:\n", node.name.Sanitise());

output ~= "ret\n";
output ~= format("__func_end__%s:\n", node.name.Sanitise());
//output ~= format("__func_end__%s:\n", node.name.Sanitise());
inScope = false;
}
}
Expand Down
28 changes: 28 additions & 0 deletions source/compiler.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class CompilerBackend {
abstract string[] FinalCommands();
abstract void NewConst(string name, long value, ErrorInfo error);

abstract void BeginMain();

abstract void Init();
abstract void End();
abstract void CompileWord(WordNode node);
Expand Down Expand Up @@ -202,7 +204,33 @@ class Compiler {
backend.compiler = this;
backend.Init();

Node[] header;
Node[] main;

foreach (ref node ; nodes) {
switch (node.type) {
case NodeType.FuncDef:
case NodeType.Include:
case NodeType.Let:
case NodeType.Implements:
case NodeType.Feature:
case NodeType.Requires:
case NodeType.Struct:
case NodeType.Const: {
header ~= node;
break;
}
default: main ~= node;
}
}

foreach (ref node ; header) {
CompileNode(node);
}

backend.BeginMain();

foreach (ref node ; main) {
CompileNode(node);
}

Expand Down

0 comments on commit 60ff4f5

Please sign in to comment.