Skip to content

Commit

Permalink
add raw functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MESYETI committed Jun 6, 2024
1 parent 00cb242 commit 5484cec
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 13 deletions.
2 changes: 1 addition & 1 deletion editors/micro_callisto.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ detect:
rules:
- statement: "\\b(func|end|begin|asm|include|inline|if|then|elseif|else|while|do)\\b"
- statement: "\\b(let|enable|requires|struct|version|return|const|enum|restrict)\\b"
- statement: "\\b(continue|break|union|alias|overwrite|error)\\b"
- statement: "\\b(continue|break|union|alias|overwrite|error|extern|call|raw)\\b"
- type: "\\b(addr|void|u8|i8|u16|i16|u32|i32|u64|i64|size|usize|cell|array)\\b"

- constant.string:
Expand Down
5 changes: 5 additions & 0 deletions examples/rawFunction.cal
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
func raw test begin

end

test
14 changes: 10 additions & 4 deletions source/backends/linux86.d
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,16 @@ class BackendLinux86 : CompilerBackend {
assert(!inScope);
inScope = true;

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

string symbol =
node.raw? node.name : format("__func__%s", node.name.Sanitise());

if (exportSymbols) {
output ~= format("global __func__%s\n", node.name.Sanitise());
output ~= format("global %s\n", symbol);
}

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

foreach (ref inode ; node.nodes) {
compiler.CompileNode(inode);
Expand Down Expand Up @@ -755,7 +758,10 @@ class BackendLinux86 : CompilerBackend {
Error(node.error, "Function '%s' doesn't exist");
}

output ~= format("mov rax, __func__%s\n", node.func.Sanitise());
auto word = words[node.func];
string symbol = word.raw? node.func : format("__func__%s", node.func.Sanitise());

output ~= format("mov rax, %s\n", symbol);
output ~= "mov [r15], rax\n";
output ~= "add r15, 8\n";
}
Expand Down
13 changes: 10 additions & 3 deletions source/backends/rm86.d
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,12 @@ class BackendRM86 : CompilerBackend {
assert(!inScope);
inScope = true;

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

output ~= format("__func__%s:\n", node.name.Sanitise());
string symbol =
node.raw? node.name : format("__func__%s", node.name.Sanitise());

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

foreach (ref inode ; node.nodes) {
compiler.CompileNode(inode);
Expand Down Expand Up @@ -664,7 +667,11 @@ class BackendRM86 : CompilerBackend {
Error(node.error, "Function '%s' doesn't exist");
}

output ~= format("mov ax, __func__%s\n", node.func.Sanitise());
auto word = words[node.func];
string symbol =
word.raw? node.func : format("__func__%s", node.func.Sanitise());

output ~= format("mov ax, %s\n", symbol);
output ~= "mov [si], ax\n";
output ~= "add si, 2\n";
}
Expand Down
14 changes: 11 additions & 3 deletions source/backends/uxn.d
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class BackendUXN : CompilerBackend {

override void Init() {
output ~= "|0 @vsp $2 @arraySrc $2 @arrayDest $2\n";
output ~= "|00 @System &vector $2 &pad $6 &r $2 &g $2 &b $2\n";
output ~= "|10 @Console &vector $2 &read $1 &pad $5 &write $1 &error $1\n";
output ~= "|100\n";
output ~= "@on-reset\n";
Expand Down Expand Up @@ -250,9 +251,12 @@ class BackendUXN : CompilerBackend {
assert(!inScope);
inScope = true;

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

output ~= format("@func__%s\n", node.name.Sanitise());
string symbol =
node.raw? node.name : format("func__%s", node.name.Sanitise());

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

foreach (ref inode ; node.nodes) {
compiler.CompileNode(inode);
Expand Down Expand Up @@ -651,6 +655,10 @@ class BackendUXN : CompilerBackend {
Error(node.error, "Function '%s' doesn't exist");
}

output ~= format(";func__%s\n", node.func.Sanitise());
auto word = words[node.func];
string symbol =
word.raw? node.func : format("func__%s", node.func.Sanitise());

output ~= format(";%s\n", symbol);
}
}
8 changes: 6 additions & 2 deletions source/backends/y32.d
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,12 @@ class BackendY32 : CompilerBackend {
assert(!inScope);
inScope = true;

words[node.name] = Word(false, false, []);
output ~= format("__func__%s:\n", node.name.Sanitise());
words[node.name] = Word(node.raw, false, []);

string symbol =
node.raw? node.name : format("__func__%s", node.name.Sanitise());

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

foreach (ref inode ; node.nodes) {
compiler.CompileNode(inode);
Expand Down
8 changes: 8 additions & 0 deletions source/parser.d
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class FuncDefNode : Node {
string name;
Node[] nodes;
bool inline;
bool raw;

this(ErrorInfo perror) {
type = NodeType.FuncDef;
Expand Down Expand Up @@ -470,6 +471,13 @@ class Parser {

Next();
Expect(TokenType.Identifier);

if (tokens[i].contents == "raw") {
ret.raw = true;
Next();
Expect(TokenType.Identifier);
}

ret.name = tokens[i].contents;

Next();
Expand Down

0 comments on commit 5484cec

Please sign in to comment.