Skip to content

Commit

Permalink
Merge pull request #16 from callisto-lang/FunctionParameters2
Browse files Browse the repository at this point in the history
Function parameters
  • Loading branch information
MESYETI authored Aug 15, 2024
2 parents 2c0f4b0 + 0aa36d6 commit 0b7bb24
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 9 deletions.
2 changes: 1 addition & 1 deletion source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ int main(string[] args) {
break;
}
case "--help": {
writeln(usage.strip());
writefln(usage.strip(), args[0]);
return 0;
}
default: {
Expand Down
38 changes: 38 additions & 0 deletions source/backends/arm64.d
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,44 @@ class BackendARM64 : CompilerBackend {
output ~= format("%s:\n", symbol);
output ~= "str lr, [x20, #-8]!\n";

// allocate parameters
size_t paramSize;
foreach (ref type ; node.paramTypes) {
if (!TypeExists(type)) {
Error(node.error, "Type '%s' doesn't exist", type);
}

paramSize += GetType(type).size;
}
if (paramSize > 0) {
output ~= format("sub x20, x20, #%d\n", paramSize);
foreach (ref var ; variables) {
var.offset += paramSize;
}

size_t offset;
foreach (i, ref type ; node.paramTypes) {
auto param = node.params[i];
Variable var;

var.name = param;
var.type = GetType(type);
var.offset = cast(uint) offset;
offset += var.Size();
variables ~= var;
}

// copy data to parameters
output ~= format("sub x9, x19, #%d\n", paramSize);
output ~= "mov x10, x20\n";
output ~= format("mov x11, #%d\n", paramSize);
output ~= "1:\n";
output ~= "ldrb w12, [x9], #1\n";
output ~= "strb w12, [x10], #1\n";
output ~= "subs x11, x11, #1\n";
output ~= "bne 1b\n";
}

foreach (ref inode ; node.nodes) {
compiler.CompileNode(inode);
}
Expand Down
36 changes: 36 additions & 0 deletions source/backends/lua.d
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,42 @@ class BackendLua : CompilerBackend {
words[node.name] = Word(WordType.Callisto, false);

output ~= format("function func__%s()\n", node.name.Sanitise());

// allocate parameters
size_t paramSize;
foreach (ref type ; node.paramTypes) {
if (!TypeExists(type)) {
Error(node.error, "Type '%s' doesn't exist", type);
}

paramSize += GetType(type).size;
}
if (paramSize > 0) {
output ~= format("vsp = vsp - %d\n", paramSize);
foreach (ref var ; variables) {
var.offset += paramSize;
}

size_t offset;
foreach (i, ref type ; node.paramTypes) {
auto param = node.params[i];
Variable var;

var.name = param;
var.type = GetType(type);
var.offset = cast(uint) offset;
offset += var.Size();
variables ~= var;
}

// copy data to parameters
output ~= format("
for i = 1, %d do
mem[vsp + (i - 1)] = mem[(dsp - %d) + (i - 1)]
end
", paramSize, paramSize);
}

foreach (ref inode ; node.nodes) {
compiler.CompileNode(inode);
}
Expand Down
36 changes: 36 additions & 0 deletions source/backends/rm86.d
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,42 @@ class BackendRM86 : CompilerBackend {

output ~= format("%s:\n", symbol);

// allocate parameters
size_t paramSize;
foreach (ref type ; node.paramTypes) {
if (!TypeExists(type)) {
Error(node.error, "Type '%s' doesn't exist", type);
}

paramSize += GetType(type).size;
}
if (paramSize > 0) {
output ~= format("sub sp, %d\n", paramSize);
foreach (ref var ; variables) {
var.offset += paramSize;
}

size_t offset;
foreach (i, ref type ; node.paramTypes) {
auto param = node.params[i];
Variable var;

var.name = param;
var.type = GetType(type);
var.offset = cast(uint) offset;
offset += var.Size();
variables ~= var;
}

// copy data to parameters
output ~= "mov ax, si\n";
output ~= format("sub si, %d\n", paramSize);
output ~= "mov di, sp\n";
output ~= format("mov cx, %d\n", paramSize);
output ~= "rep movsb\n";
output ~= "mov si, ax\n";
}

foreach (ref inode ; node.nodes) {
compiler.CompileNode(inode);
}
Expand Down
35 changes: 35 additions & 0 deletions source/backends/uxn.d
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,41 @@ class BackendUXN : CompilerBackend {

output ~= format("@%s\n", symbol);

// allocate parameters
size_t paramSize;
foreach (ref type ; node.paramTypes) {
if (!TypeExists(type)) {
Error(node.error, "Type '%s' doesn't exist", type);
}

paramSize += GetType(type).size;
}
if (paramSize > 0) {
output ~= format(".vsp LDZ2 #%.4x SUB2 .vsp STZ2\n", paramSize);
foreach (ref var ; variables) {
var.offset += paramSize;
}

size_t offset;
foreach (i, ref type ; node.paramTypes) {
auto param = node.params[i];
Variable var;

var.name = param;
var.type = GetType(type);
var.offset = cast(uint) offset;
offset += var.Size();
variables ~= var;
}

// copy all parameters
foreach_reverse (ref param ; node.params) {
auto setNode = new SetNode(node.error);
setNode.var = param;
CompileSet(setNode);
}
}

foreach (ref inode ; node.nodes) {
compiler.CompileNode(inode);
}
Expand Down
35 changes: 35 additions & 0 deletions source/backends/x86_64.d
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,41 @@ class BackendX86_64 : CompilerBackend {

output ~= format("%s:\n", symbol);

// allocate parameters
size_t paramSize;
foreach (ref type ; node.paramTypes) {
if (!TypeExists(type)) {
Error(node.error, "Type '%s' doesn't exist", type);
}

paramSize += GetType(type).size;
}
if (paramSize > 0) {
output ~= format("sub rsp, %d\n", paramSize);
foreach (ref var ; variables) {
var.offset += paramSize;
}

size_t offset;
foreach (i, ref type ; node.paramTypes) {
auto param = node.params[i];
Variable var;

var.name = param;
var.type = GetType(type);
var.offset = cast(uint) offset;
offset += var.Size();
variables ~= var;
}

// copy data to parameters
output ~= "mov rsi, r15\n";
output ~= format("sub rsi, %d\n", paramSize);
output ~= "mov rdi, rsp\n";
output ~= format("mov rcx, %d\n", paramSize);
output ~= "rep movsb\n";
}

foreach (ref inode ; node.nodes) {
compiler.CompileNode(inode);
}
Expand Down
35 changes: 27 additions & 8 deletions source/parser.d
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,25 @@ class IntegerNode : Node {
}

class FuncDefNode : Node {
string name;
Node[] nodes;
bool inline;
bool raw;
string name;
Node[] nodes;
bool inline;
bool raw;
string[] paramTypes;
string[] params;

this(ErrorInfo perror) {
type = NodeType.FuncDef;
error = perror;
}

override string toString() {
string ret = format("func %s\n", name);
string ret = format("func %s", name);

foreach (i, ref param ; params) {
ret ~= format(" %s %s", paramTypes[i], param);
}
ret ~= " begin\n";

foreach (ref node ; nodes) {
ret ~= " " ~ node.toString() ~ '\n';
Expand Down Expand Up @@ -526,6 +533,12 @@ class Parser {
}
}

bool IsIdentifier(string identifier) {
return
(tokens[i].type == TokenType.Identifier) &&
(tokens[i].contents == identifier);
}

Node ParseFuncDef(bool inline) {
auto ret = new FuncDefNode(GetError());
ret.inline = inline;
Expand All @@ -543,10 +556,16 @@ class Parser {
ret.name = tokens[i].contents;

Next();
Expect(TokenType.Identifier);
if (tokens[i].contents != "begin") {
Error("Expected begin keyword"); // TODO: add parameters

while (!IsIdentifier("begin")) {
Expect(TokenType.Identifier);
ret.paramTypes ~= tokens[i].contents;
Next();
Expect(TokenType.Identifier);
ret.params ~= tokens[i].contents;
Next();
}

Next();

while (true) {
Expand Down

0 comments on commit 0b7bb24

Please sign in to comment.