add import feature

This commit is contained in:
Hojun-Cho 2026-01-13 10:32:47 +09:00
parent eb1a977adb
commit dd1fbfd5da
12 changed files with 109 additions and 37 deletions

View File

@ -232,10 +232,10 @@ assertexpr(Node *n)
case Ocall: case Ocall:
assertexpr(r); assertexpr(r);
t = l->ty; t = l->ty;
n->ty = t->tof;
if(l->op==Opkg) if(l->op==Opkg)
return; return;
usetype(t); usetype(t);
n->ty = t->tof;
break; break;
default: default:
assert(0); assert(0);

View File

@ -93,12 +93,14 @@ void
genbin(char *name) genbin(char *name)
{ {
f = fopen(name, "w+"); f = fopen(name, "w+");
// fprintf(f, "%s\n", pkgname->name); wr4(f, nimport);
// fprintf(f, "%d\n", nimport); for(int i = 0; i < nimport; ++i){
// for(int i = 0; i < nimport; ++i){ char buf[1024];
// fprintf(f, "%d %s %s\n", i, imports[i].path->name, imports[i].sym->name); sprintf(buf, "%s/obj", imports[i].path->name);
// } int len = strlen(buf);
wr4(f, len);
fwrite(buf, 1, len, f);
}
wr4(f, ninst); wr4(f, ninst);
disinst(f, firstinst); disinst(f, firstinst);
fclose(f); fclose(f);

View File

@ -827,7 +827,6 @@ imret(int n)
if(n == 0) if(n == 0)
return tnone; return tnone;
if(n == 1){ if(n == 1){
want((int[]){Lconst,0});
want((int[]){Lconst,0}); want((int[]){Lconst,0});
return imtype(); return imtype();
} }

7
test/fib/exported Normal file
View File

@ -0,0 +1,7 @@
fib
fn .fib 160 0 1 1
0
int 1 8 56
8
int 1 8 0

8
test/fib/fib.yo Normal file
View File

@ -0,0 +1,8 @@
package fib
export fn fib(n int) int {
if n <= 1 {
return n;
};
return fib(n-1) + fib(n-2);
};

BIN
test/fib/obj Normal file

Binary file not shown.

10
test/main.yo Normal file
View File

@ -0,0 +1,10 @@
package main
import "./test/fib"
import "./test/nqueen"
fn main() int {
f := fib.fib(10);
n := nqueen.solve(8);
return f + n;
};

7
test/nqueen/exported Normal file
View File

@ -0,0 +1,7 @@
nqueen
fn .solve 296 0 1 1
0
int 1 8 56
8
int 1 8 0

View File

@ -1,27 +1,15 @@
package main package nqueen
type Any struct { export fn solve(n int) int {
x,y int; arr := [16]int{};
arr [8]int; return nqueen(0, 0, arr[:n]);
}; };
fn main() int { fn nqueen(r,ans int, arr []int) int {
arr := [8]int{};
return nqueen(0, 0, arr[:]);
};
fn fib(n int) int{
if n <= 1 {
return n;
};
return fib(n-1) + fib(n-2);
};
fn nqueen(r,ans int, arr []int) int{
if r == len(arr) { if r == len(arr) {
return ans + 1; return ans + 1;
}; };
for c:=0; c < len(arr); c=c+1{ for c:=0; c < len(arr); c=c+1 {
arr[r] = c; arr[r] = c;
if canput(r, arr) { if canput(r, arr) {
ans = nqueen(r+1, ans, arr); ans = nqueen(r+1, ans, arr);

BIN
test/nqueen/obj Normal file

Binary file not shown.

BIN
vm/run Executable file

Binary file not shown.

65
vm/vm.c
View File

@ -5,6 +5,10 @@ u32 ninst;
Inst *inst; Inst *inst;
REG R; REG R;
Inst **modules;
u32 *modsizes;
int nmodules;
#define OP(fn) void fn(void) #define OP(fn) void fn(void)
#define B(r) *((i8*)(R.r)) #define B(r) *((i8*)(R.r))
#define F(r) ((WORD*)(R.r)) #define F(r) ((WORD*)(R.r))
@ -112,6 +116,18 @@ OP(call){
R.FP = (u8*)f; R.FP = (u8*)f;
JMP(d); JMP(d);
} }
OP(pcall){
Frame *f;
int mod, pc;
f = T(s);
f->lr = R.PC;
f->fp = R.FP;
R.FP = (u8*)f;
mod = W(m);
pc = W(d);
R.PC = &modules[mod][pc];
}
OP(ret) { OP(ret) {
Frame *f = (Frame*)R.FP; Frame *f = (Frame*)R.FP;
R.FP = f->fp; R.FP = f->fp;
@ -165,6 +181,7 @@ static void (*optab[])(void) = {
[ILEA] = lea, [ILEA] = lea,
[IFRAME] = frame, [IFRAME] = frame,
[ICALL] = call, [ICALL] = call,
[PCALL] = pcall,
[IJMP] = jmp, [IJMP] = jmp,
[IRET] = ret, [IRET] = ret,
[ILEN] = len, [ILEN] = len,
@ -204,15 +221,15 @@ xec(void)
} }
void void
bpatch(Inst *i) bpatch(Inst *i, Inst *base, u32 n)
{ {
static int tab[IEND] = { static int tab[IEND] = {
[ICALL]=1,[IBEQW]=1,[IBNEQW]=1,[IJMP]=1, [ICALL]=1,[IBEQW]=1,[IBNEQW]=1,[IJMP]=1,
}; };
if(tab[i->op] == 0) if(tab[i->op] == 0)
return; return;
assert(i->d.imm >= 0 && i->d.imm < ninst); assert(i->d.imm >= 0 && i->d.imm < n);
i->d.imm = (WORD)&inst[i->d.imm]; i->d.imm = (WORD)&base[i->d.imm];
return; return;
} }
@ -248,7 +265,6 @@ rdinst(FILE *f, Inst *in)
break; break;
case DST(AIMM): case DST(AIMM):
in->d.ind = rd4(f); in->d.ind = rd4(f);
bpatch(in);
break; break;
case DST(AIND|AFP): case DST(AIND|AFP):
case DST(AIND|AMP): case DST(AIND|AMP):
@ -258,18 +274,53 @@ rdinst(FILE *f, Inst *in)
} }
} }
Inst*
loadmod(char *fname, u32 *outn)
{
FILE *f = fopen(fname, "r");
assert(f != 0);
u32 nim = rd4(f);
for(u32 i = 0; i < nim; i++){
u32 len = rd4(f);
fseek(f, len, SEEK_CUR);
}
u32 n = rd4(f);
Inst *ins = calloc(sizeof(Inst), n);
for(u32 i = 0; i < n; i++)
rdinst(f, ins+i);
for(u32 i = 0; i < n; i++)
bpatch(ins+i, ins, n);
fclose(f);
*outn = n;
return ins;
}
void void
load(char *fname) load(char *fname)
{ {
FILE *f = fopen(fname, "r"); FILE *f = fopen(fname, "r");
assert(f != 0); assert(f != 0);
initprog(1024); initprog(1024);
nmodules = rd4(f);
if(nmodules > 0){
modules = calloc(sizeof(Inst*), nmodules);
modsizes = calloc(sizeof(u32), nmodules);
for(int i = 0; i < nmodules; i++){
u32 len = rd4(f);
char *path = calloc(1, len+1);
fread(path, 1, len, f);
modules[i] = loadmod(path, &modsizes[i]);
free(path);
}
}
ninst = rd4(f); ninst = rd4(f);
inst = calloc(sizeof(Inst), ninst); inst = calloc(sizeof(Inst), ninst);
for(u32 i = 0; i < ninst; i++)
for(u32 i = 0; i < ninst; ++i){
rdinst(f, inst+i); rdinst(f, inst+i);
} for(u32 i = 0; i < ninst; i++)
bpatch(inst+i, inst, ninst);
R.PC = inst; R.PC = inst;
fclose(f); fclose(f);
} }