From 60ff4f586b9a209d1a460bbb496ace3ae13971b0 Mon Sep 17 00:00:00 2001 From: MESYETI Date: Tue, 16 Apr 2024 15:29:47 +0100 Subject: [PATCH] refactor compiler to improve binary sizes and make it possible to compile to languages other than assembly --- examples/helloWorld.cal | 2 +- source/app.d | 4 ++++ source/backends/linux86.d | 12 +++++++++--- source/backends/rm86.d | 10 +++++++--- source/backends/y16.d | 11 ++++++++--- source/compiler.d | 28 ++++++++++++++++++++++++++++ 6 files changed, 57 insertions(+), 10 deletions(-) diff --git a/examples/helloWorld.cal b/examples/helloWorld.cal index 75d07b5..a58427f 100644 --- a/examples/helloWorld.cal +++ b/examples/helloWorld.cal @@ -2,4 +2,4 @@ include "cores/select.cal" include "std/io.cal" include "std/array.cal" -"Hello, world!" printlstr +"Hello, world!\n" printlstr diff --git a/source/app.d b/source/app.d index 916f263..d2da4b5 100644 --- a/source/app.d +++ b/source/app.d @@ -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 { diff --git a/source/backends/linux86.d b/source/backends/linux86.d index 4faa00e..d2ea862 100644 --- a/source/backends/linux86.d +++ b/source/backends/linux86.d @@ -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"; @@ -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() { @@ -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) { @@ -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 = []; } diff --git a/source/backends/rm86.d b/source/backends/rm86.d index 8f4206c..3d4b897 100644 --- a/source/backends/rm86.d +++ b/source/backends/rm86.d @@ -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() { @@ -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) { @@ -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; } diff --git a/source/backends/y16.d b/source/backends/y16.d index 30b6926..283fe6f 100644 --- a/source/backends/y16.d +++ b/source/backends/y16.d @@ -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() { @@ -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; } } diff --git a/source/compiler.d b/source/compiler.d index d133970..bc503b5 100644 --- a/source/compiler.d +++ b/source/compiler.d @@ -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); @@ -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); }