Skip to content

Commit 7ed9b76

Browse files
committed
finish modulesgit add -A!
1 parent 42f0e65 commit 7ed9b76

File tree

9 files changed

+133
-14
lines changed

9 files changed

+133
-14
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ Lua, while keeping its low level features (like direct access to memory).
1414

1515
## Build
1616
```
17-
dub build
17+
dub build --compiler=ldc
1818
```
1919

20+
Compiling with LDC is required because DMD freezes
21+
2022
The compiler executable will be called `cac`
2123

2224
> [!WARNING]

source/app.d

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import callisto.backends.rm86;
2727
import callisto.backends.arm64;
2828
import callisto.backends.x86_64;
2929

30-
const static string appVersion = "Beta 0.12.7";
30+
const static string appVersion = "Beta 0.13.0";
3131

3232
const static string usage = "
3333
Callisto Compiler
@@ -241,9 +241,8 @@ int main(string[] args) {
241241
break;
242242
}
243243
case "lua": {
244-
writeln("Language subset 'CallistoScript' in use");
245244
backend = new BackendLua();
246-
modCPU = ModCPU.None;
245+
modCPU = ModCPU.Lua;
247246
break;
248247
}
249248
default: {

source/backends/lua.d

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ class BackendLua : CompilerBackend {
9797
output ~= "regA = 0;\n";
9898
output ~= "regB = 0;\n";
9999
output ~= "dspRestore = 0;\n";
100-
output ~= format("exception = %d;\n", globalStack);
101-
globalStack += 4;
102100

103101
output ~= "
104102
function cal_pop()
@@ -124,9 +122,15 @@ class BackendLua : CompilerBackend {
124122
);
125123
}
126124

127-
output ~= "end\n";
125+
if (output.mode != OutputMode.Module) {
126+
output ~= "end\n";
127+
}
128+
129+
// end top level code
130+
output.FinishSection();
128131

129132
// create arrays
133+
output.StartSection(SectionType.Data);
130134
foreach (ref array ; arrays) {
131135
// create metadata
132136
auto arrayExtra = cast(ArrayExtra*) array.extra;
@@ -153,13 +157,19 @@ class BackendLua : CompilerBackend {
153157
end
154158
", array.values.length, arrayExtra.elements);
155159
}
160+
output.FinishSection();
156161

157-
output ~= "regA = 0\n";
158-
output ~= "calmain();\n";
162+
if (output.mode != OutputMode.Module) {
163+
output ~= "regA = 0\n";
164+
output ~= "calmain();\n";
165+
}
159166
}
160167

161168
override void BeginMain() {
162-
output ~= "function calmain()\n";
169+
output.StartSection(SectionType.TopLevel);
170+
if (output.mode != OutputMode.Module) {
171+
output ~= "function calmain()\n";
172+
}
163173

164174
// call constructors
165175
foreach (global ; globals) {
@@ -376,7 +386,8 @@ class BackendLua : CompilerBackend {
376386
}
377387

378388
if (node.inline) {
379-
output ~= format("mem[%d] = 0\n", exception);
389+
// why was this here??
390+
//output ~= format("mem[%d] = 0\n", exception);
380391

381392
words ~= Word(
382393
output.GetModName(), node.name, WordType.Callisto, false, true,
@@ -688,6 +699,8 @@ class BackendLua : CompilerBackend {
688699
"Anonymous variables can only be created inside a function"
689700
);
690701
}
702+
703+
output.AddGlobal(global);
691704
}
692705
}
693706

@@ -963,6 +976,13 @@ class BackendLua : CompilerBackend {
963976
assert(!inScope);
964977
inScope = true;
965978

979+
output.StartSection(SectionType.Implement);
980+
if (output.mode == OutputMode.Module) {
981+
auto sect = cast(ImplementSection) output.sect;
982+
sect.type = type.name;
983+
sect.method = node.method;
984+
}
985+
966986
output ~= format("function %s()\n", labelName);
967987

968988
foreach (ref inode ; node.nodes) {
@@ -989,6 +1009,8 @@ class BackendLua : CompilerBackend {
9891009

9901010
inScope = false;
9911011
variables = [];
1012+
1013+
output.FinishSection();
9921014
}
9931015

9941016
void SetGlobal(

source/compiler.d

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,8 @@ class CompilerBackend {
425425
}
426426

427427
string ExtLabel(string mod, string prefix, string label) {
428-
auto modPrefix = output.mode == OutputMode.Module? format("%s__sep__", mod) : "";
428+
auto modPrefix = output.mode == OutputMode.Module?
429+
format("%s__sep__", Sanitise(mod)) : "";
429430
return format("%s%s%s", prefix, modPrefix, label);
430431
}
431432

source/linker.d

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import callisto.error;
99
import callisto.mod.mod;
1010
import callisto.mod.sections;
1111
import callisto.linkers.uxn;
12+
import callisto.linkers.lua;
1213
import callisto.linkers.rm86;
1314
import callisto.linkers.arm64;
1415
import callisto.linkers.x86_64;
@@ -243,6 +244,7 @@ int LinkerProgram(string[] args) {
243244
case ModCPU.RM86: linker = new LinkerRM86(); break;
244245
case ModCPU.ARM64: linker = new LinkerARM64(); break;
245246
case ModCPU.Uxn: linker = new LinkerUXN(); break;
247+
case ModCPU.Lua: linker = new LinkerLua(); break;
246248
default: {
247249
stderr.writefln("Unsupported architecture '%s'", cpu);
248250
return 1;

source/linkers/lua.d

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
module callisto.linkers.lua;
2+
3+
import std.file;
4+
import std.stdio;
5+
import std.format;
6+
import std.string;
7+
import std.process;
8+
import std.algorithm;
9+
import callisto.util;
10+
import callisto.error;
11+
import callisto.linker;
12+
import callisto.output;
13+
import callisto.mod.mod;
14+
import callisto.compiler;
15+
import callisto.mod.sections;
16+
import callisto.backends.lua;
17+
18+
// TODO: link time optimisation
19+
class LinkerLua : Linker {
20+
override bool HandleOption(string opt) => false;
21+
22+
override void Link() {
23+
auto file = File(outFile, "w");
24+
auto backend = new BackendLua();
25+
26+
file.writeln("mem = {};");
27+
file.writeln("for i = 1, 1048576 do");
28+
file.writeln(" table.insert(mem, 0);");
29+
file.writeln("end");
30+
file.writeln("dsp = 1;");
31+
file.writeln("vsp = 1048576;");
32+
file.writeln("gsp = 524288;");
33+
file.writeln("regA = 0;");
34+
file.writeln("regB = 0;");
35+
file.writeln("dspRestore = 0;");
36+
37+
file.writeln("
38+
function cal_pop()
39+
dsp = dsp - 1;
40+
return mem[dsp];
41+
end
42+
");
43+
44+
// run top level code
45+
file.writeln("function calmain()");
46+
file.write(tlcAsm);
47+
file.writeln("end");
48+
49+
// write function assembly
50+
file.write(funcAsm);
51+
52+
// bss, even though it doesn't exist on this backend!
53+
file.write(bssAsm);
54+
55+
file.write(dataAsm);
56+
57+
file.writeln("regA = 0\n");
58+
file.writeln("calmain();");
59+
file.flush();
60+
file.close();
61+
62+
// run final commands
63+
backend.compiler = new Compiler();
64+
backend.compiler.outFile = outFile;
65+
backend.output = new Output("");
66+
67+
final switch (os) {
68+
case ModOS.None: backend.os = "bare-metal"; break;
69+
case ModOS.Linux: backend.os = "linux"; break;
70+
case ModOS.macOS: backend.os = "osx"; break;
71+
case ModOS.Windows: backend.os = "windows"; break;
72+
case ModOS.DOS: backend.os = "dos"; break;
73+
case ModOS.FreeBSD: backend.os = "freebsd"; break;
74+
case ModOS.Varvara: backend.os = "varvara"; break;
75+
case ModOS.Fox32OS: backend.os = "fox32os"; break;
76+
}
77+
78+
auto commands = backend.FinalCommands();
79+
80+
foreach (cmd ; commands) {
81+
writeln(cmd);
82+
auto res = executeShell(cmd);
83+
84+
if (res.status != 0) {
85+
stderr.writefln("Error running '%s': %s", cmd, res.output);
86+
exit(1);
87+
}
88+
}
89+
}
90+
}

source/mod/sections.d

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ enum ModCPU : SectionInt {
1919
Uxn = 0x0004,
2020
Fox32 = 0x0005,
2121
M68k = 0x0006,
22-
RISCV = 0x0007
22+
RISCV = 0x0007,
23+
Lua = 0x0008
2324
}
2425

2526
enum ModOS : SectionInt {

source/output.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Output {
6161
string GetModName() => mode == OutputMode.Module? mod.name : "";
6262

6363
string GetModPrefix() {
64-
return mode == OutputMode.Module? format("%s__sep__", mod.name) : "";
64+
return mode == OutputMode.Module? format("%s__sep__", Sanitise(mod.name)) : "";
6565
}
6666

6767
void StartSection(SectionType type) {

source/parser.d

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ class ArrayNode : Node {
336336
override string toString() {
337337
string ret = constant? "c[" : "[";
338338

339+
ret ~= format("%s%s ", arrayType.ptr? "ptr " : "", arrayType.name);
340+
339341
foreach (ref node ; elements) {
340342
ret ~= node.toString() ~ ' ';
341343
}

0 commit comments

Comments
 (0)