Skip to content

Commit

Permalink
add vector example
Browse files Browse the repository at this point in the history
  • Loading branch information
MESYETI committed Dec 11, 2024
1 parent 1ddd6a7 commit 75fd283
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 11 deletions.
40 changes: 40 additions & 0 deletions examples/vector.cal
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
include "cores/select.cal"
include "std/io.cal"
include "std/vector.cal"

let Vector vec
cell.sizeOf &vec init_vec

func vec_info begin
"Capacity: " printstr vec.capacity printdec new_line
"Length: " printstr vec.length printdec new_line

"Contents: " printstr

0
while dup vec.length < do
dup &vec a@ printdec ' ' printch
1 +
end
drop

new_line new_line
end

"=== vec_push 1" printstr new_line
1 &vec vec_push
&vec vec_top printdec new_line
vec_info

"=== vec_push 2, 3" printstr new_line
2 &vec vec_push
3 &vec vec_push
vec_info

"=== vec_remove 1" printstr new_line
1 &vec vec_remove
vec_info

"=== vec_insert 1, 5" printstr new_line
5 1 &vec vec_insert
vec_info
10 changes: 6 additions & 4 deletions source/backends/arm64.d
Original file line number Diff line number Diff line change
Expand Up @@ -527,15 +527,17 @@ class BackendARM64 : CompilerBackend {
crash = true;
}

LoadAddress("x9", "__global_" ~ Sanitise("_cal_exception"));
output ~= "ldr x9, [x9]\n";
output ~= "cmp x9, #0\n";
if (crash) {
LoadAddress("x9", "__global_" ~ Sanitise("_cal_exception"));
output ~= "ldr x9, [x9]\n";
output ~= "cmp x9, #0\n";
output ~= format("bne __func__%s\n", Sanitise("__arm64_exception"));
}
else {
output ~= "beq 1f\n";
output ~= "mov x19, x15\n";
CompileReturn(node);
output ~= "1:\n";
}
}
else {
Expand Down Expand Up @@ -868,7 +870,7 @@ class BackendARM64 : CompilerBackend {
default: OffsetLocalsStack(var.Size(), true);
}

if (var.type.hasInit) { // call constructor
if (var.type.hasInit && !var.type.ptr) { // call constructor
output ~= "str x20, [x19], #8\n";
output ~= format("bl __type_init_%s\n", var.type.name.Sanitise());
}
Expand Down
25 changes: 21 additions & 4 deletions source/backends/lua.d
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class BackendLua : CompilerBackend {
output ~= "gsp = 524288;\n";
output ~= "regA = 0;\n";
output ~= "regB = 0;\n";
output ~= "dspRestore = 0;\n";

output ~= "
function cal_pop()
Expand Down Expand Up @@ -230,19 +231,35 @@ class BackendLua : CompilerBackend {
output ~= format("func__%s();\n", node.name.Sanitise());

if (word.error && words[thisFunc].error) {
output ~= "dsp = mem[vsp]\n";
output ~= "dspRestore = mem[vsp]\n";
output ~= "vsp = vsp + 1\n";
}
}

if (word.error) {
if ("__lua_exception" in words) {
bool crash;
auto exception = GetGlobal("_cal_exception");
auto extra = cast(GlobalExtra*) exception.extra;

output ~= format("if mem[%d] ~= 0 then\n", extra.addr);
output ~= format("func__%s()\n", Sanitise("__lua_exception"));
output ~= "end\n";
if (inScope) {
crash = !words[thisFunc].error;
}
else {
crash = true;
}

if (crash) {
output ~= format("if mem[%d] ~= 0 then\n", extra.addr);
output ~= format("func__%s()\n", Sanitise("__lua_exception"));
output ~= "end\n";
}
else {
output ~= format("if mem[%d] ~= 0 then\n", extra.addr);
output ~= "dsp = dspRestore\n";
CompileReturn(node);
output ~= "end\n";
}
}
else {
Warn(node.error, "No exception handler");
Expand Down
11 changes: 11 additions & 0 deletions source/backends/rm86.d
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class BackendRM86 : CompilerBackend {
bool inWhile;
uint currentLoop;
RM86Opts opts;
uint tempLabelNum;

this() {
addrSize = 2;
Expand Down Expand Up @@ -74,6 +75,11 @@ class BackendRM86 : CompilerBackend {
);
}

string TempLabel() {
++ tempLabelNum;
return format("__temp_%d", tempLabelNum);
}

override void NewConst(string name, long value, ErrorInfo error = ErrorInfo.init) {
consts[name] = Constant(new IntegerNode(error, value));
}
Expand Down Expand Up @@ -340,8 +346,13 @@ class BackendRM86 : CompilerBackend {
output ~= format("jne __func__%s\n", Sanitise("__rm86_exception"));
}
else {
string temp = TempLabel();

output ~= format("cmp word [__global_%s], 0\n", Sanitise("_cal_exception"));
output ~= format("je %s\n", temp);
output ~= "mov si, di\n";
CompileReturn(node);
output ~= format("%s:\n", temp);
}
}
else {
Expand Down
13 changes: 12 additions & 1 deletion source/backends/uxn.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class BackendUXN : CompilerBackend {
string thisFunc;
bool inWhile;
uint currentLoop;
uint tempLabelNum;

this() {
addrSize = 2;
Expand Down Expand Up @@ -68,6 +69,11 @@ class BackendUXN : CompilerBackend {
);
}

string TempLabel() {
++ tempLabelNum;
return format("__temp_%d", tempLabelNum);
}

override string[] GetVersions() => [
// platform
"UXN", "BigEndian", "16Bit",
Expand Down Expand Up @@ -295,11 +301,16 @@ class BackendUXN : CompilerBackend {
if (crash) {
output ~= format(";global_%s LDA2\n", Sanitise("_cal_exception"));
output ~= "#0000 NEQ2\n";
output ~= format(";func__%s JCN2\n", Sanitise("__uxn_exception"));
output ~= format("?func__%s\n", Sanitise("__uxn_exception"));
}
else {
string temp = TempLabel();

output ~= format(";global_%s LDA2\n", Sanitise("_cal_exception"));
output ~= format("#0000 EQU2 ?%s\n", temp);
output ~= ".temp LDZ .System/wst DEO\n";
CompileReturn(node);
output ~= format("@%s\n", temp);
}
}
else {
Expand Down
11 changes: 11 additions & 0 deletions source/backends/x86_64.d
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class BackendX86_64 : CompilerBackend {
bool inWhile;
uint currentLoop;
bool useLibc;
uint tempLabelNum;

this() {
addrSize = 8;
Expand Down Expand Up @@ -101,6 +102,11 @@ class BackendX86_64 : CompilerBackend {
);
}

string TempLabel() {
++ tempLabelNum;
return format("__temp_%d", tempLabelNum);
}

override void NewConst(string name, long value, ErrorInfo error = ErrorInfo.init) {
consts[name] = Constant(new IntegerNode(error, value));
}
Expand Down Expand Up @@ -600,8 +606,13 @@ class BackendX86_64 : CompilerBackend {
output ~= format("jne __func__%s\n", Sanitise("__x86_64_exception"));
}
else {
string temp = TempLabel();

output ~= format("cmp qword [__global_%s], 0\n", Sanitise("_cal_exception"));
output ~= format("je %s\n", temp);
output ~= "mov r15, r14\n";
CompileReturn(node);
output ~= format("%s:\n", temp);
}
}
else {
Expand Down
4 changes: 3 additions & 1 deletion source/stackCheck.d
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class StackChecker {
string[][string] structs;
size_t[] whileStacks;
string[] types;
string thisFunc;

this() {
structs["Array"] = ["length", "memberSize", "elements"];
Expand Down Expand Up @@ -161,6 +162,7 @@ class StackChecker {
}
}

thisFunc = node.name;
Evaluate(node.nodes);

if (stack.length > node.returnTypes.length) {
Expand Down Expand Up @@ -407,7 +409,7 @@ class StackChecker {
auto wnode = cast(WordNode) node;

switch (wnode.name) {
case "return": Pop(node, 1); break;
case "return": Pop(node, words[thisFunc].effect.push); break;
case "continue": EvaluateBreakContinue(wnode); break;
case "break": EvaluateBreakContinue(wnode); break;
case "call": Error(node.error, "Call is unsafe"); break;
Expand Down
2 changes: 1 addition & 1 deletion std

0 comments on commit 75fd283

Please sign in to comment.