tail calls #1

Open
msyds wants to merge 5 commits from tail into main
4 changed files with 19 additions and 0 deletions
Showing only changes of commit 4f12bcc476 - Show all commits

2
.dir-locals.el Normal file
View File

@@ -0,0 +1,2 @@
((c-mode . ((tab-width . 4)
(indent-tabs-mode . t))))

1
all.h
View File

@@ -160,6 +160,7 @@ enum J {
X(retw) X(retl) X(rets) X(retd) \
X(retsb) X(retub) X(retsh) X(retuh) \
X(retc) X(ret0) X(jmp) X(jnz) \
X(tail) \
X(jfieq) X(jfine) X(jfisge) X(jfisgt) \
X(jfisle) X(jfislt) X(jfiuge) X(jfiugt) \
X(jfiule) X(jfiult) X(jffeq) X(jffge) \

View File

@@ -53,6 +53,7 @@ enum Token {
Tphi,
Tjmp,
Tjnz,
Ttail,
Tret,
Thlt,
Texport,
@@ -113,6 +114,7 @@ static char *kwmap[Ntok] = {
[Tphi] = "phi",
[Tjmp] = "jmp",
[Tjnz] = "jnz",
[Ttail] = "tail",
[Tret] = "ret",
[Thlt] = "hlt",
[Texport] = "export",
@@ -664,6 +666,9 @@ parseline(PState ps)
curb->jmp.arg = r;
}
goto Close;
case Ttail:
curb->jmp.type = Jtail;
goto Close;
case Tjmp:
curb->jmp.type = Jjmp;
goto Jump;
@@ -1434,6 +1439,9 @@ printfn(Fn *fn, FILE *f)
if (b->s1 != b->link)
fprintf(f, "\tjmp @%s\n", b->s1->name);
break;
case Jtail:
fprintf (f, "\ttail\n");
break;
default:
fprintf(f, "\t%s ", jtoa[b->jmp.type]);
if (b->jmp.type == Jjnz) {

8
test/tail.ssa Normal file
View File

@@ -0,0 +1,8 @@
function w $factorial (w %n, w %acc) {
@start
jnz %n, @z, @nz
@z
ret %acc
@nz
tail
}