can run fibonacci sequence, nqueen

This commit is contained in:
yoyo 2024-11-04 02:11:36 +09:00
parent 1bff7d30e7
commit 00f87d4e55
17 changed files with 113 additions and 85 deletions

12
asm.c
View File

@ -21,6 +21,7 @@ char* instname[] = {
[ILTW] = "lt", [ILTW] = "lt",
[ILEQW] = "leq", [ILEQW] = "leq",
[IEQW] = "eq", [IEQW] = "eq",
[INEQW] = "neq",
}; };
void void
@ -33,18 +34,17 @@ asminst(FILE *f, Inst *in)
void void
asmexport(FILE *f, Sym *pkg, Decl **arr, int n) asmexport(FILE *f, Sym *pkg, Decl **arr, int n)
{ {
fprintf(f, "\tpackage\t"); fprintf(f, "\n\tpackage\t");
fprintf(f, "%s\n", pkg->name); fprintf(f, "%s\n", pkg->name);
fprintf(f, "\texported %d\n", n); fprintf(f, "\tfn %d\n", n);
for(int i = 0; i < n; ++i){ for(int i = 0; i < n; ++i){
Decl *d = arr[i]; Decl *d = arr[i];
switch(d->store){ switch(d->store){
default: default:
break; break;
// case Dfn: case Dfn:
// fprintf(f, "\tlink\t%u,%u,\"",d->desc->id, d->pc->pc); fprintf(f, "\tfn\t%s,%d,\n",d->sym->name, d->pc->pc);
// fprintf(f, "%s\"\n", d->sym->name); break;
// break;
} }
} }
} }

View File

@ -212,18 +212,16 @@ assertexpr(Node *n)
break; break;
case Oslice: case Oslice:
t = usetype(l->ty); t = usetype(l->ty);
assert(t->kind == Tarray || t->kind == Tslice);
if(t->kind == Tslice) if(t->kind == Tslice)
t = typeofslice(t); t = t->tof;
assert(t->kind == Tarray); n->ty = mktype(Tslice, ArrHead, t->tof, nil);
n->ty = mktype(Tslice, ArrHead, t, nil);
break; break;
case Oindex: case Oindex:
t = l->ty; t = l->ty;
if(t->kind == Tslice) assert(t->kind == Tarray || t->kind == Tslice);
t = typeofslice(t->tof);
assert(t->kind == Tarray);
assertexpr(r); assertexpr(r);
if(r->op == Oconst) if(r->op == Oconst && t->kind == Tarray)
assert(r->val < t->len); assert(r->val < t->len);
n->ty = t->tof; n->ty = t->tof;
break; break;
@ -280,16 +278,17 @@ assertstmt(Type *ret, Node *n)
assert(l->ty == tnone); assert(l->ty == tnone);
else else
assert(eqtype(ret, l->ty)); assert(eqtype(ret, l->ty));
n->ty = ret;
return top; return top;
case Oif: case Oif:
n->l = l = assertstmt(ret, l); n->l = l = assertstmt(ret, l);
assert(l->ty == tbool); assert(l->ty->kind == Tbool);
r->l = assertstmt(ret, r->l); r->l = assertstmt(ret, r->l);
n = r; n = r;
break; break;
case Ofor: case Ofor:
assertexpr(l); assertexpr(l);
assert(l->ty == tbool); assert(l->ty->kind == Tbool);
pushloop(); pushloop();
r->r = assertstmt(ret, r->r); r->r = assertstmt(ret, r->r);
r->l = assertstmt(ret, r->l); r->l = assertstmt(ret, r->l);

27
com.c
View File

@ -397,14 +397,6 @@ rewrite(Node *n)
n->l->ty = tint; n->l->ty = tint;
n->l = rewrite(n->l); n->l = rewrite(n->l);
return n; return n;
case Oslice:
if(r->l == nil)
r->l = mkconst(0);
if(r->r == nil)
r->r = mkconst(n->ty->len);
n->l = rewrite(n->l);
n->r = rewrite(n->r);
break;
case Oindex: case Oindex:
t = n->ty; t = n->ty;
n->r = mkn(Omul, mkconst(IBY2WD), n->r); n->r = mkn(Omul, mkconst(IBY2WD), n->r);
@ -462,7 +454,7 @@ swaprelop(int op)
case Ogt: return Olt; case Ogt: return Olt;
case Ogeq: return Oleq; case Ogeq: return Oleq;
case Oleq: return Ogeq; case Oleq: return Ogeq;
default: assert(0); default: return op;
} }
} }
@ -504,17 +496,13 @@ static Inst*
brcom(Node *n, int ift, Inst *b) brcom(Node *n, int ift, Inst *b)
{ {
Inst *bb = nil; Inst *bb = nil;
int op = n->op;
Node nto={0}; Node nto={0};
Node f = (Node){.op=Oconst,.val=!ift,.addable=Rconst,.ty=tbool}; Node f = (Node){.op=Oconst,.addable=Rconst,.ty=tbool,.val=1};
sumark(n); if(ift)
if(op != Oconst){ f.val = 0;
cmpcom(talloc(&nto, tbool, nil), n); ecom(talloc(&nto, tbool, nil), n);
bb = genrawop(IBEQW, &nto, &f, nil); bb = genrawop(IBEQW, &nto, &f, nil);
}else{
bb = genrawop(IBEQW, &f, n, nil);
}
bb->branch = b; bb->branch = b;
tfree(&nto); tfree(&nto);
return bb; return bb;
@ -668,6 +656,8 @@ scom(Node *n)
case Oif: case Oif:
pushblock(); pushblock();
pushblock(); pushblock();
n->l = simplifiy(n->l);
sumark(n->l);
p = brcom(l, 1, nil); p = brcom(l, 1, nil);
tfreenow(); tfreenow();
popblock(); popblock();
@ -760,5 +750,4 @@ fncom(Decl *d)
Decl *locs = concatdecl(d->locals, tdecls()); Decl *locs = concatdecl(d->locals, tdecls());
d->offset += idoffsets(locs, d->offset, IBY2WD); d->offset += idoffsets(locs, d->offset, IBY2WD);
d->locals = locs; d->locals = locs;
printf("%d\n", d->offset);
} }

6
dis.c
View File

@ -41,15 +41,15 @@ wr2(FILE *f, u16 v)
fwrite(&v, 1, 2, f); fwrite(&v, 1, 2, f);
} }
void void
wr4(FILE *f, u32 v) wr4(FILE *f, i32 v)
{ {
fwrite(&v, 1, 4, f); fwrite(&v, 1, 4, f);
} }
void void
disaddr(FILE* f, u32 m, Addr *a) disaddr(FILE* f, i32 m, Addr *a)
{ {
u32 val = 0; i32 val = 0;
switch(m){ switch(m){
case Anone: case Anone:
return; return;

13
fib.yo
View File

@ -1,13 +0,0 @@
package a
fn main() int {
x := fib(10);
return x;
};
fn fib(n int) int{
if n <= 1 {
return n;
};
return fib(n-1) + fib(n-2);
};

6
file.c
View File

@ -34,7 +34,7 @@ getfiles(char *path, char *sufix)
int ll = strlen(path); int ll = strlen(path);
int j = 1; int j = 1;
s = new(sizeof(char*)); s = new(sizeof(char*));
d = opendir(path); assert(d = opendir(path));
while((p=readdir(d)) != NULL){ while((p=readdir(d)) != NULL){
if(issufix(p->d_name, sufix)){ if(issufix(p->d_name, sufix)){
s = realloc(s, (j+1)*sizeof(void*)); s = realloc(s, (j+1)*sizeof(void*));
@ -42,7 +42,7 @@ getfiles(char *path, char *sufix)
char *n = new(l+ll+2); char *n = new(l+ll+2);
memcpy(n, path, ll); memcpy(n, path, ll);
n[ll] = '/'; n[ll] = '/';
memcpy(n+ll, p->d_name, l+1); memcpy(n+ll+1, p->d_name, l+1);
s[j-1] = n; s[j-1] = n;
s[j++] = nil; s[j++] = nil;
} }
@ -50,4 +50,4 @@ getfiles(char *path, char *sufix)
nsrc = j - 1; nsrc = j - 1;
closedir(d); closedir(d);
return s; return s;
} }

4
fn.h
View File

@ -83,8 +83,8 @@ int sharelocal(Decl *d);
/* dis.c */ /* dis.c */
int disinst(FILE *out, Inst *in); int disinst(FILE *out, Inst *in);
void wr1(FILE *f, u8 v); void wr1(FILE *f, u8 v);
void wr4(FILE *f, u32 v); void wr4(FILE *f, i32 v);
void disaddr(FILE* f, u32 m, Addr *a); void disaddr(FILE* f, i32 m, Addr *a);
/* gen.c */ /* gen.c */
void genstart(void); void genstart(void);

15
gen.c
View File

@ -238,6 +238,7 @@ genop(int op, Node *s, Node *m, Node *d)
{ {
[Olt] = {[Tint]=ILTW,[Tbool]=ILTW}, [Olt] = {[Tint]=ILTW,[Tbool]=ILTW},
[Oeq] = {[Tint]=IEQW,[Tbool]=IEQW}, [Oeq] = {[Tint]=IEQW,[Tbool]=IEQW},
[Oneq] = {[Tint]=INEQW,[Tbool]=INEQW},
[Oleq] ={[Tint]=ILEQW,[Tbool]=ILEQW}, [Oleq] ={[Tint]=ILEQW,[Tbool]=ILEQW},
[Oandand] = {[Tint]=IEQW,[Tbool]=IEQW}, [Oandand] = {[Tint]=IEQW,[Tbool]=IEQW},
[Ooror] = {[Tint]=IADDW,[Tbool]=IADDW}, [Ooror] = {[Tint]=IADDW,[Tbool]=IADDW},
@ -273,25 +274,25 @@ addprint(FILE *f, int am, Addr *a)
case Anone: case Anone:
return; return;
case Afp: case Afp:
fprintf(f, "%u(fp)", a->reg); fprintf(f, "%d(fp)", a->reg);
break; break;
case Aimm: case Aimm:
fprintf(f, "$%u", a->offset); fprintf(f, "$%d", a->offset);
break; break;
case Afpind: case Afpind:
fprintf(f, "%u(%u(fp))", a->offset, a->reg); fprintf(f, "%d(%d(fp))", a->offset, a->reg);
break; break;
case Adesc: case Adesc:
fprintf(f, "$%u", a->offset+a->reg); fprintf(f, "$%d", a->offset+a->reg);
break; break;
case Apc: case Apc:
fprintf(f, "$%u", a->reg+a->offset); fprintf(f, "$%d", a->reg+a->offset);
break; break;
case Amp: case Amp:
fprintf(f, "%u(mp)",a->reg); fprintf(f, "%d(mp)",a->reg);
break; break;
case Aldt: case Aldt:
fprintf(f, "$%u", a->reg); fprintf(f, "$%d", a->reg);
break; break;
default: default:
assert(0); assert(0);

1
isa.h
View File

@ -32,6 +32,7 @@ enum
IMOVM, IMOVM,
ILTW, ILTW,
IEQW, IEQW,
INEQW,
ILEQW, ILEQW,
IBEQW, IBEQW,
IBNEQW, IBNEQW,

2
lex.c
View File

@ -330,4 +330,4 @@ importdef(Sym *file)
assert(b != nil); assert(b != nil);
assert(bstack + 1 < sizeof(bins)); assert(bstack + 1 < sizeof(bins));
bin = bins[++bstack] = b; bin = bins[++bstack] = b;
} }

1
main.c
View File

@ -102,4 +102,5 @@ main(int argc, char **argv)
genbin("obj"); genbin("obj");
asminst(stdout, firstinst); asminst(stdout, firstinst);
asmexport(stdout, pkgname, fns, nfn);
} }

View File

@ -6,13 +6,12 @@ OBJS=$(SRCS:.c=.o)
com: $(OBJS) com: $(OBJS)
$(CC) $(CFLAGS) -o $@ $^ $(CC) $(CFLAGS) -o $@ $^
make -C ./vm make -C ./vm
cp ./vm/run .
$(OBJS): yo.h isa.h fn.h dat.h $(OBJS): yo.h isa.h fn.h dat.h
tests: tests:
./com fib.yo ./com ./test
./run obj ./vm/run obj
clean: clean:
rm -f com $(OBJS) obj exported run rm -f com $(OBJS) obj exported run

19
parse.c
View File

@ -246,8 +246,8 @@ index(Node *id)
assert(0); assert(0);
case ':': case ':':
if(try((int[]){']',0}).kind) if(try((int[]){']',0}).kind)
return mkn(Oslice, id, mkn(Oseq, nil, nil)); return mkn(Oslice, id, mkn(Oseq, mkconst(0), mkconst(-1)));
n = mkn(Oslice, id, mkn(Oseq, nil, expr())); n = mkn(Oslice, id, mkn(Oseq, mkconst(0), expr()));
break; break;
default: default:
l = expr(); l = expr();
@ -257,7 +257,7 @@ index(Node *id)
} }
want((int[]){':',0}); want((int[]){':',0});
if(peek((int[]){']',0}).kind){ if(peek((int[]){']',0}).kind){
n = mkn(Oslice, id, mkn(Oseq, l, nil)); n = mkn(Oslice, id, mkn(Oseq, l, mkconst(-1)));
break; break;
} }
r = expr(); r = expr();
@ -293,10 +293,10 @@ unrayexpr(void)
switch(try((int[]){'+','-','*','/','&',0}).kind){ switch(try((int[]){'+','-','*','/','&',0}).kind){
default: return primaryexprs(); default: return primaryexprs();
case '+': case '+':
case '-':
case '!': case '!':
case '/': case '/':
case '^': assert(0); case '^': assert(0);
case '-': return mkn(Omul, mkconst(-1), unrayexpr());
case '*': return mkn(Oxref, unrayexpr(), nil); case '*': return mkn(Oxref, unrayexpr(), nil);
case '&': return mkn(Oref,unrayexpr(), nil); case '&': return mkn(Oref,unrayexpr(), nil);
} }
@ -587,12 +587,11 @@ paramdecl(void)
static Decl* static Decl*
paramlist(void) paramlist(void)
{ {
Decl h={0}, *p = &h; Decl *p = paramdecl();
for(;try((int[]){',',0}).kind==',';){
p = p->next = paramdecl(); p =concatdecl(p, paramdecl());
for(;try((int[]){',',0}).kind==',';) }
p = p->next = paramdecl(); return p;
return h.next;
} }
// "(" [parameterlist [ ',' ] ] ")" // "(" [parameterlist [ ',' ] ] ")"

View File

@ -1,13 +1,47 @@
package a package main
type Any struct {
x,y int;
arr [8]int;
};
fn main() int { fn main() int {
x := fib(10); arr := [8]int{};
return x; return nqueen(0, 0, arr[:]);
}; };
fn fib(n int) int{ fn fib(n int) int{
if n <= 1 { if n <= 1 {
return n; return n;
}; };
return fib(n-1) + return fib(n-2); return fib(n-1) + fib(n-2);
};
fn nqueen(r,ans int, arr []int) int{
if r == len(arr) {
return ans + 1;
};
for c:=0; c < len(arr); c=c+1{
arr[r] = c;
if canput(r, arr) {
ans = nqueen(r+1, ans, arr);
};
};
return ans;
};
fn canput(r int, arr []int) bool {
for i:=0; i < r; i=i+1 {
if arr[r] == arr[i] || abs(arr[r] - arr[i]) == abs(r - i) {
return false;
};
};
return true;
};
fn abs(x int) int {
if x < 0 {
return x * -1;
};
return x;
}; };

View File

@ -170,6 +170,18 @@ D91(void)
R.m = R.FP+R.PC->reg; R.m = R.FP+R.PC->reg;
} }
static void static void
D92(void)
{
R.s = (u8*)&R.PC->s.imm;
R.d = (u8*)&R.PC->d.imm;
R.m = R.FP+R.PC->reg;
}
static void
D93(void)
{
R.s = (u8*)&R.PC->s.imm;
}
static void
D95(void) D95(void)
{ {
R.s = (u8*)&R.PC->s.imm; R.s = (u8*)&R.PC->s.imm;
@ -216,6 +228,8 @@ void (*dec[])(void) =
[0x8A] = D8A, [0x8A] = D8A,
[0x8D] = D8D, [0x8D] = D8D,
[0x91] = D91, [0x91] = D91,
[0x92] = D92,
[0x93] = D93,
[0x95] = D95, [0x95] = D95,
[0xA9] = DA9, [0xA9] = DA9,
[0xAD] = DAD, [0xAD] = DAD,

12
vm/vm.c
View File

@ -1,6 +1,6 @@
#include "vm.h" #include "vm.h"
static u8 end[1]; static u8 end[1024];
u32 ninst; u32 ninst;
Inst *inst; Inst *inst;
REG R; REG R;
@ -47,10 +47,10 @@ rd2(FILE *f)
assert(fread(&v, 1, 2, f) == 2); assert(fread(&v, 1, 2, f) == 2);
return v; return v;
} }
u32 WORD
rd4(FILE *f) rd4(FILE *f)
{ {
u32 v = 0; i32 v = 0;
assert(fread(&v, 1, 4, f) == 4); assert(fread(&v, 1, 4, f) == 4);
return v; return v;
} }
@ -86,6 +86,7 @@ OP(bneqw) {if(W(s) != W(m)) JMP(d);}
OP(ltw) {W(d) = (W(s) < W(m));} OP(ltw) {W(d) = (W(s) < W(m));}
OP(leqw) {W(d) = (W(s) <= W(m));} OP(leqw) {W(d) = (W(s) <= W(m));}
OP(eqw) {W(d) = (W(s) == W(m));} OP(eqw) {W(d) = (W(s) == W(m));}
OP(neqw) {W(d) = (W(s) != W(m));}
OP(frame){ OP(frame){
Stack *s; Stack *s;
Frame *f; Frame *f;
@ -116,6 +117,7 @@ OP(ret) {
R.FP = f->fp; R.FP = f->fp;
if(R.FP == NULL){ if(R.FP == NULL){
printf("result %ld\n", W(d)); printf("result %ld\n", W(d));
WORD *p = end;
exit(0); exit(0);
} }
R.SP = (u8*)f; R.SP = (u8*)f;
@ -150,7 +152,8 @@ OP(slice){
if(s2 == -1) if(s2 == -1)
s2 = a->len; s2 = a->len;
assert(s1 >= 0 && s1 < a->len); assert(s1 >= 0 && s1 < a->len);
assert(s2 >= 0 && s2 < a->len); assert(s2 >= 0 && s2 <= a->len);
assert(s1 < s2);
Array d = *a; Array d = *a;
d.len = s2 - s1; d.len = s2 - s1;
d.cap = s2 - s1; d.cap = s2 - s1;
@ -175,6 +178,7 @@ static void (*optab[])(void) = {
[IBEQW] = beqw, [IBEQW] = beqw,
[IBNEQW] = bneqw, [IBNEQW] = bneqw,
[IEQW] = eqw, [IEQW] = eqw,
[INEQW] = neqw,
[ILEQW] = leqw, [ILEQW] = leqw,
[ILTW] = ltw, [ILTW] = ltw,
}; };

View File

@ -61,7 +61,7 @@ struct REG
u8 *TS; u8 *TS;
u8 *EX; u8 *EX;
void *s, *d, *m; void *s, *d, *m;
int t, dt ,mt; WORD t, dt ,mt;
}; };
struct Array struct Array