mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-31 15:43:01 +00:00 
			
		
		
		
	Add quick asm for adding apply and error to the stl.
This commit is contained in:
		| @@ -243,3 +243,17 @@ DstFunction *dst_function(DstFuncDef *def, DstFunction *parent) { | |||||||
|     } |     } | ||||||
|     return func; |     return func; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | /* Utility for inline assembly */ | ||||||
|  | DstFunction *dst_quick_asm(int32_t arity, int varargs, int32_t slots, const uint32_t *bytecode, size_t bytecode_size) { | ||||||
|  |     DstFuncDef *def = dst_funcdef_alloc(); | ||||||
|  |     def->arity = arity; | ||||||
|  |     def->flags = varargs ? DST_FUNCDEF_FLAG_VARARG : 0; | ||||||
|  |     def->slotcount = slots; | ||||||
|  |     def->bytecode = malloc(bytecode_size); | ||||||
|  |     if (!def->bytecode) { | ||||||
|  |         DST_OUT_OF_MEMORY; | ||||||
|  |     } | ||||||
|  |     memcpy(def->bytecode, bytecode, bytecode_size); | ||||||
|  |     return dst_function(def, NULL); | ||||||
|  | } | ||||||
|   | |||||||
| @@ -23,6 +23,7 @@ | |||||||
| #include <dst/dst.h> | #include <dst/dst.h> | ||||||
| #include <dst/dststl.h> | #include <dst/dststl.h> | ||||||
| #include <dst/dstasm.h> | #include <dst/dstasm.h> | ||||||
|  | #include <dst/dstopcodes.h> | ||||||
|  |  | ||||||
| int dst_stl_exit(DstArgs args) { | int dst_stl_exit(DstArgs args) { | ||||||
|     int32_t exitcode = 0; |     int32_t exitcode = 0; | ||||||
| @@ -274,12 +275,24 @@ static const DstReg cfuns[] = { | |||||||
| }; | }; | ||||||
|  |  | ||||||
| DstTable *dst_stl_env() { | DstTable *dst_stl_env() { | ||||||
|  |     static uint32_t error_asm[] = { | ||||||
|  |         DOP_ERROR | ||||||
|  |     }; | ||||||
|  |  | ||||||
|  |     static uint32_t apply_asm[] = { | ||||||
|  |        DOP_PUSH_ARRAY | (1 << 8), | ||||||
|  |        DOP_TAILCALL | ||||||
|  |     }; | ||||||
|  |  | ||||||
|     DstTable *env = dst_table(0); |     DstTable *env = dst_table(0); | ||||||
|     Dst ret = dst_wrap_table(env); |     Dst ret = dst_wrap_table(env); | ||||||
|  |  | ||||||
|     /* Load main functions */ |     /* Load main functions */ | ||||||
|     dst_env_cfuns(env, cfuns); |     dst_env_cfuns(env, cfuns); | ||||||
|  |  | ||||||
|  |     dst_env_def(env, "error", dst_wrap_function(dst_quick_asm(1, 0, 1, error_asm, sizeof(uint32_t)))); | ||||||
|  |     dst_env_def(env, "apply", dst_wrap_function(dst_quick_asm(2, 0, 2, apply_asm, 2 * sizeof(uint32_t)))); | ||||||
|  |  | ||||||
|     /* Allow references to the environment */ |     /* Allow references to the environment */ | ||||||
|     dst_env_def(env, "_env", ret); |     dst_env_def(env, "_env", ret); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -165,6 +165,7 @@ int dst_gcunrootall(Dst root); | |||||||
| DstFuncDef *dst_funcdef_alloc(); | DstFuncDef *dst_funcdef_alloc(); | ||||||
| DstFunction *dst_function(DstFuncDef *def, DstFunction *parent); | DstFunction *dst_function(DstFuncDef *def, DstFunction *parent); | ||||||
| int dst_verify(DstFuncDef *def); | int dst_verify(DstFuncDef *def); | ||||||
|  | DstFunction *dst_quick_asm(int32_t arity, int varargs, int32_t slots, const uint32_t *bytecode, size_t bytecode_size); | ||||||
|  |  | ||||||
| /* Misc */ | /* Misc */ | ||||||
| int dst_equals(Dst x, Dst y); | int dst_equals(Dst x, Dst y); | ||||||
|   | |||||||
| @@ -1,9 +0,0 @@ | |||||||
| (def outerfun (fn [x y] |  | ||||||
|     (def c (do |  | ||||||
| #(+ 1 2 3 4) |  | ||||||
|       (def someval (+ x y)) |  | ||||||
|       (def ctemp (if x (fn [] someval) (fn [] y))) |  | ||||||
|       ctemp |  | ||||||
|     )) |  | ||||||
| #(+ 1 2 3 4 5 6 7 8 9 10) |  | ||||||
|     c)) |  | ||||||
| @@ -141,17 +141,18 @@ | |||||||
| 	(varset! count (+ 1 count))) | 	(varset! count (+ 1 count))) | ||||||
| (assert (= accum 65536) "loop globally") | (assert (= accum 65536) "loop globally") | ||||||
|  |  | ||||||
| (assert (= (struct 1 2 3 4 5 6 7 8) (struct 7 8 5 6 3 4 1 2)) "struct order does not matter") | (assert (= (struct 1 2 3 4 5 6 7 8) (struct 7 8 5 6 3 4 1 2)) "struct order does not matter 1") | ||||||
|  | (assert (= (struct  | ||||||
|  |             :apple 1  | ||||||
|  |             6 :bork | ||||||
|  |             '(1 2 3) 5) | ||||||
|  |          (struct  | ||||||
|  |             6 :bork | ||||||
|  |             '(1 2 3) 5 | ||||||
|  |             :apple 1)) "struct order does not matter 2") | ||||||
|  |  | ||||||
| # Fiber tests | # Fiber tests | ||||||
|  |  | ||||||
| (def error (asm '{ |  | ||||||
|   arity 1 |  | ||||||
|   bytecode [ |  | ||||||
|     (error 0) |  | ||||||
|   ] |  | ||||||
| })) |  | ||||||
|  |  | ||||||
| (def afiber (fiber (fn [x] | (def afiber (fiber (fn [x] | ||||||
| 	(error (string "hello, " x))))) | 	(error (string "hello, " x))))) | ||||||
|  |  | ||||||
| @@ -171,14 +172,6 @@ | |||||||
|  |  | ||||||
| # Var arg tests | # Var arg tests | ||||||
|  |  | ||||||
| (def apply (asm '{ |  | ||||||
|   arity 2 |  | ||||||
|   bytecode [ |  | ||||||
|     (push-array 1) |  | ||||||
|     (tailcall 0)  |  | ||||||
|   ]  |  | ||||||
| })) |  | ||||||
|  |  | ||||||
| (def vargf (fn [more] (apply + more))) | (def vargf (fn [more] (apply + more))) | ||||||
|  |  | ||||||
| (assert (= 0 (vargf [])) "var arg no arguments") | (assert (= 0 (vargf [])) "var arg no arguments") | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 bakpakin
					bakpakin