Skip to content

Commit 608c841

Browse files
committed
Nested bootstrap, continuation test, cosmetic
1 parent d01702a commit 608c841

File tree

9 files changed

+254
-212
lines changed

9 files changed

+254
-212
lines changed

bootstrap.edict

+37-35
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,56 @@
1-
[
2-
]@newline
1+
ROOT <
2+
[
3+
]@newline
34

4-
[LTV_concat!]@concat
5+
[LTV_concat!]@concat
56

6-
[compile_ltv(@code jit_edict code)]@compile
7-
[[a b c] compile compile!!! stack!]@test_compile
7+
[compile_ltv(@code jit_edict code)]@compile
8+
[[a b c] compile compile!!! stack!]@test.compile
89

9-
[vm_try(cif_preview_module!)]@preview
10-
[vm_try(cif_import_module())]@import
11-
[[preview(lib) import(lib) lib](@lib)]@loadlib
10+
[vm_try(cif_preview_module!)]@preview
11+
[vm_try(cif_import_module())]@import
12+
[[preview(lib) import(lib) lib](@lib)]@loadlib
1213

13-
[loadlib([../test/build/libtestlib.so]) @test test.y.1 stack!]@testimport
14-
[loadlib([../../htm/build/libhtmlib.so]) @htm htm.htm_main(int! '((char)*)*'!)]@testimport2
15-
[loadlib([../../htm/build/libhtmlib.so]) <htm_main(int! '((char)*)*'!)>]@testimport3
14+
[loadlib([../test/build/libtestlib.so]) @test test.y.1 stack!]@test.import1
15+
[loadlib([../../htm/build/libhtmlib.so]) @htm htm.htm_main(int! '((char)*)*'!)]@test.import2
16+
[loadlib([../../htm/build/libhtmlib.so]) <htm_main(int! '((char)*)*'!)>]@test.import3
1617

17-
[bench encaps! vm_while! | locals!]@fastbench
18-
[bench [dup! ! loop!]@loop loop! | locals!]@midbench
18+
[bench encaps! vm_while! | locals!]@fastbench
19+
[bench [dup! ! loop!]@loop loop! | locals!]@midbench
1920

20-
[a b int_add! a@ limit int_nequal! slowloop!]@slowloop
21-
[int! @limit limit@ int! 0@ @a int! 1@ @b slowloop! | locals!]@slowbench
21+
[a b int_add! a@ limit int_nequal! slowloop!]@slowloop
22+
[int! @limit limit@ int! 0@ @a int! 1@ @b slowloop! | locals!]@slowbench
2223

23-
[encaps! <RETURN>@]@return_tos
24+
[encaps! <RETURN>@]@return_tos
25+
[ROOT<ARG0 decaps! stack! ! return_tos!>]@std.thunk
2426

25-
[square(3)]@square3
26-
[vm_eval(ROOT [ROOT<ARG0 decaps! stack! ! encaps! <RETURN>@] [square(3)]) stack!]@async_test1
27+
[square(3)]@square3
28+
[minus(1)]@decrement
2729

28-
[vm_eval(ROOT [ROOT<ARG0 decaps! stack! ! encaps! <RETURN>@] [square3!]) stack!]@async_test2 [fails, can't inherit parent dict]/
30+
[vm_eval(ROOT std.thunk [square(3)]) stack!]@test.async1
31+
[vm_eval(ROOT std.thunk [square3!]) stack!]@test.async2
2932

30-
[ROOT<ARG0 decaps! stack! ! encaps! <RETURN>@]@std_thunk
31-
[vm_eval(ROOT std_thunk [square3!]) stack!]@async_test3 [fails, can't inherit parent dict]/
33+
[[]<@thunk @root @sig encaps(vm_continuation(encaps(sig) root thunk))>/]@continuation
34+
[[]<@continuation @arg vm_async(continuation arg)@thread>]@async
35+
[<vm_await(thread)>/]@await
3236

33-
[[]<@thunk @root @sig encaps(vm_continuation(encaps(sig) root thunk))>/]@continuation
34-
[[]<@continuation @arg vm_async(continuation arg)@thread>]@async
35-
[<vm_await(thread)>/]@await
37+
[await(async([square(3)] continuation('(LTV)*(*)((LTV)*)' ROOT std.thunk))) stack!]@test.async6
3638

37-
[await(async([square(3)] continuation('(LTV)*(*)((LTV)*)' ROOT [ROOT<ARG0 decaps! stack! ! encaps! <RETURN>@>]))) stack!]@async_test6
39+
[async(continuation('(LTV)*(*)((LTV)*)' ROOT std.thunk))]@std.thread
3840

39-
[continuation('(LTV)*(*)((LTV)*)' ROOT [ROOT<ARG0 decaps! stack! ! encaps! <RETURN>@>])@C
40-
[square(2)] async(C) @T1
41-
[square(3)] async(C) @T2
42-
minus(await(T2) await(T1))
43-
stack!
44-
]@async_test7
41+
[ std.thread([square(2)]) @T1
42+
std.thread([square(3)]) @T2
43+
minus(await(T2) await(T1))
44+
stack!
45+
]@test.async7
4546

46-
[vm_eval(ROOT [ROOT<ARG0 decaps! stack! ! encaps! <RETURN>@>] [testimport3!]) stack!]@async_test8
47+
[vm_eval(ROOT std.thunk [testimport3!]) stack!]@test.async8
4748

48-
[minus(1)]@decrement
49-
50-
[[[repl(get_stdin!)|]! repeat!]@repeat repeat!]/
49+
continuation('int(*)(int,int)' ROOT [ROOT <string(ARG0) string(ARG1) minus(ARG0 ARG1) return_tos!])@callback
50+
>
5151

52+
[[[repl(get_stdin!)|]! repeat!]@repeat repeat!]/ [persistent]/
5253
repl(get_stdin!)
5354

5455
[goodbye!] return_tos!
56+

compile.c

+48-46
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ int jit_edict(EMITTER emit,void *data,int len)
135135
return status;
136136
}
137137

138+
int jit_edict2(EMITTER emit, void *data, int len) { return jit_edict(emit, data, len); }
139+
138140
int jit_xml(EMITTER emit, void *data, int len) { printf("jit_xml not implemented\n"); }
139141

140142
/*
@@ -254,76 +256,76 @@ int jit_json(EMITTER emit, void *data, int len)
254256
}
255257
*/
256258

257-
LTV *compile(COMPILER compiler,void *data,int len)
258-
{
259-
TSTART(0,"compile");
260-
char *buf=NULL;
261-
size_t flen=0;
262-
FILE *stream=open_memstream(&buf,&flen);
259+
LTV *compile(COMPILER compiler, void *data, int len) {
260+
TSTART(0, "compile");
261+
char *buf = NULL;
262+
size_t flen = 0;
263+
FILE *stream = open_memstream(&buf, &flen);
263264

264-
int emit(VM_CMD *cmd) {
265+
int emit(VM_CMD * cmd) {
265266
unsigned unsigned_val;
266-
fwrite(&cmd->op,1,1,stream);
267-
if (cmd->op==VMOP_EXT) {
267+
fwrite(&cmd->op, 1, 1, stream);
268+
if (cmd->op == VMOP_EXT) {
268269
switch (cmd->len) {
269270
case -1:
270-
cmd->len=strlen(cmd->data); // rewrite len and...
271+
cmd->len = strlen(cmd->data); // rewrite len and...
271272
// ...fall thru!
272273
default:
273-
unsigned_val=htonl(cmd->len);
274-
fwrite(&unsigned_val,sizeof(unsigned),1,stream);
275-
unsigned_val=htonl(cmd->flags);
276-
fwrite(&unsigned_val,sizeof(unsigned),1,stream);
277-
fwrite(cmd->data,1,cmd->len,stream);
274+
unsigned_val = htonl(cmd->len);
275+
fwrite(&unsigned_val, sizeof(unsigned), 1, stream);
276+
unsigned_val = htonl(cmd->flags);
277+
fwrite(&unsigned_val, sizeof(unsigned), 1, stream);
278+
fwrite(cmd->data, 1, cmd->len, stream);
278279
break;
279280
}
280281
}
281282
}
282283

283-
if (len==-1)
284-
len=strlen((char *) data);
285-
compiler(emit,data,len);
284+
if (len == -1)
285+
len = strlen((char *) data);
286+
compiler(emit, data, len);
286287
fclose(stream);
287-
TFINISH(0,"compile");
288-
return LTV_init(NEW(LTV),buf,flen,LT_BC|LT_OWN|LT_BIN|LT_LIST);
288+
TFINISH(0, "compile");
289+
return LTV_init(NEW(LTV), buf, flen, LT_BC | LT_OWN | LT_BIN | LT_LIST);
289290
}
290291

291-
LTV *compile_ltv(COMPILER compiler,LTV *ltv)
292-
{
292+
LTV *compile_ltv(COMPILER compiler, LTV *ltv) {
293293
// print_ltv(stdout,CODE_RED "compile: ",ltv,CODE_RESET "\n",0);
294-
if (ltv->flags&(LT_CVAR|LT_BC))
295-
return ltv; // FFI or pre-compiled
296-
LTV *bc=compile(compiler,ltv->data,ltv->len);
294+
if (ltv->flags & (LT_CVAR | LT_BC))
295+
return ltv; // FFI or pre-compiled
296+
LTV *bc = compile(compiler, ltv->data, ltv->len);
297297
return bc;
298298
}
299299

300-
char *opcode_name[] = { "RESET","YIELD","EXT","THROW","CATCH","PUSHEXT","EVAL","REF","DEREF","ASSIGN","REMOVE","CTX_PUSH","CTX_POP","FUN_PUSH","FUN_EVAL","FUN_POP",
301-
"S2S","D2S","E2S","F2S","S2D","S2E","S2F" };
302-
303-
void disassemble(FILE *ofile,LTV *ltv)
304-
{
305-
TSTART(0,"disassemble");
306-
unsigned char *data,*code=(unsigned char *) ltv->data;
307-
int i=0,length=0,flags=0;
308-
fprintf(ofile,"BYTECODE: ");
309-
while (i<ltv->len) {
310-
unsigned char opcode=code[i++];
311-
switch(opcode) {
300+
char *opcode_name[] = {"RESET", "YIELD", "EXT", "THROW", "CATCH", "PUSHEXT", "EVAL", "REF", "DEREF", "ASSIGN", "REMOVE", "CTX_PUSH", "CTX_POP", "FUN_PUSH", "FUN_EVAL", "FUN_POP",
301+
"S2S", "D2S", "E2S", "F2S", "S2D", "S2E", "S2F"};
302+
303+
void disassemble(FILE *ofile, LTV *ltv) {
304+
TSTART(0, "disassemble");
305+
unsigned char *data, *code = (unsigned char *) ltv->data;
306+
int i = 0, length = 0, flags = 0;
307+
fprintf(ofile, "BYTECODE: ");
308+
while (i < ltv->len) {
309+
unsigned char opcode = code[i++];
310+
switch (opcode) {
312311
case VMOP_EXT:
313-
length=ntohl(*(unsigned *) (code+i)); i+=sizeof(unsigned);
314-
flags=ntohl(*(unsigned *) (code+i)); i+=sizeof(unsigned);
315-
data=code+i; i+=length;
316-
//fprintf(ofile, "\n" CODE_BLUE);
312+
length = ntohl(*(unsigned *) (code + i));
313+
i += sizeof(unsigned);
314+
flags = ntohl(*(unsigned *) (code + i));
315+
i += sizeof(unsigned);
316+
data = code + i;
317+
i += length;
318+
// fprintf(ofile, "\n" CODE_BLUE);
317319
fprintf(ofile, CODE_BLUE);
318-
fstrnprint(ofile,data,length);
320+
fstrnprint(ofile, data, length);
319321
fprintf(ofile, ":%x " CODE_RESET, flags);
320322
break;
321323
case VMOP_RESET:
322-
//fprintf(ofile,"\n");
324+
// fprintf(ofile,"\n");
323325
default:
324-
fprintf(ofile,"%s ",opcode_name[opcode]);
326+
fprintf(ofile, "%s ", opcode_name[opcode]);
325327
}
326328
}
327-
fprintf(ofile,"\n");
328-
TFINISH(0,"disassemble");
329+
fprintf(ofile, "\n");
330+
TFINISH(0, "disassemble");
329331
}

compile.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ extern void disassemble(FILE *ofile,LTV *ltv);
8282
extern int jit_asm(EMITTER emit,void *data,int len);
8383
extern int jit_edict(EMITTER emit,void *data,int len);
8484
extern int jit_xml(EMITTER emit,void *data,int len);
85-
extern int jit_json(EMITTER emit,void *data,int len);
86-
extern int jit_yaml(EMITTER emit,void *data,int len);
87-
extern int jit_swagger(EMITTER emit,void *data,int len);
88-
extern int jit_lisp(EMITTER emit,void *data,int len);
89-
extern int jit_massoc(EMITTER emit,void *data,int len); // mathematica association
85+
extern int jit_edict2(EMITTER emit, void *data, int len);
86+
87+
//extern int jit_json(EMITTER emit,void *data,int len);
88+
//extern int jit_yaml(EMITTER emit,void *data,int len);
89+
//extern int jit_swagger(EMITTER emit,void *data,int len);
90+
//extern int jit_lisp(EMITTER emit,void *data,int len);
91+
//extern int jit_massoc(EMITTER emit,void *data,int len); // mathematica association
9092

9193
#endif // COMPILE_H

extensions.c

+3
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,6 @@ extern void bench() {
168168
}
169169
return;
170170
}
171+
172+
test_callback_sig callback_example=NULL;
173+
extern int test_callback(int a,int b) { return callback_example? callback_example(a,b):0; }

extensions.h

+2
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@
3737
#define EXTENSIONS_H
3838

3939
extern LTV *get_separated_debug_filename(char *filename);
40+
typedef int (*test_callback_sig)(int a,int b);
41+
extern test_callback_sig example_callback;
4042

4143
#endif

listree.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ typedef enum {
5555
LT_BIN =0x00000008, // data is binary/unprintable
5656
LT_REFS =0x00000010, // LTV holds a list of REFs (implies LT_LIST)
5757
LT_CVAR =0x00000020, // LTV data is a C variable
58-
LT_TYPE =0x00000040, // CVAR of type TYPE_INFO (for reflection)
58+
LT_TYPE =0x00000040, // CVAR of type TYPE_INFO_LTV (for reflection)
5959
LT_FFI =0x00000080, // CVAR of type ffi_type (for reflection)
6060
LT_CIF =0x00000100, // CVAR of type ffi_cif (for reflection)
6161
LT_NULL =0x00000200, // empty

makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ cmake: build; cd build && cmake ..
1111
compile: cmake; make -C build
1212
clean: cmake; make -C build clean
1313
distclean: ; rm -rf build
14-
run_lib: compile; MALLOC_CHECK=1 rlwrap -S "[]/ " build/libreflect.so
15-
run_bin: compile; MALLOC_CHECK=1 rlwrap -S "[]/ " build/jj
16-
run: run_lib
14+
runlib: compile; MALLOC_CHECK=1 rlwrap -S "[]/ " build/libreflect.so
15+
runbin: compile; MALLOC_CHECK=1 rlwrap -S "[]/ " build/jj
16+
run: runbin
1717
compilerun: compile run
1818
install: cmake; sudo make -C build install
1919
fastbench: cmake; rm callgrind.out.*; echo "fastbench!" | (valgrind --tool=callgrind build/jj)

0 commit comments

Comments
 (0)