static	kbool_t exception_initPackage(KonohaContext *kctx, kNameSpace *ns, int argc, const char**args, kfileline_t pline)
{
	KonohaExceptionModule *mod = (KonohaExceptionModule*)KCALLOC(sizeof(KonohaExceptionModule), 1);
	mod->h.name     = "exception";
	mod->h.setup    = kModuleException_setup;
	mod->h.reftrace = kModuleException_reftrace;
	mod->h.free     = kModuleException_free;
	KLIB Konoha_setModule(kctx, MOD_exception, &mod->h, pline);
	KDEFINE_CLASS defException = {
		STRUCTNAME(Exception),
		.cflag = CFLAG_Exception,
		.init = Exception_init,
		.reftrace = Exception_reftrace,
		.p     = Exception_p,
	};
	mod->cException = KLIB Konoha_defineClass(kctx, ns->packageId, PN_konoha, NULL, &defException, pline);

	KDEFINE_METHOD MethodData[] = {
		_Public|_Hidden, _F(System_throw), TY_void,  TY_System, MN_("throw"), 1, TY_Exception, FN_("e"),
		_Public|_Hidden, _F(System_getThrownException), TY_Exception, TY_System, MN_("getThrownException"), 0,
		DEND,
	};
	KLIB kNameSpace_loadMethodData(kctx, ns, MethodData);
	return true;
}
Beispiel #2
0
static kcontext_t* new_context(const kcontext_t *_ctx, const kplatform_t *plat)
{
	kcontext_t *newctx;
	static volatile size_t ctxid_counter = 0;
	ctxid_counter++;
	if(_ctx == NULL) {  // NULL means first one
		struct _klib2 *klib2 = (struct _klib2*)calloc(sizeof(klib2_t) + sizeof(kcontext_t), 1);
		klib2_init(klib2);
		newctx = (kcontext_t*)(klib2 + 1);
		newctx->lib2 = (klib2_t*)klib2;
		newctx->plat = plat;
		_ctx = (CTX_t)newctx;
		newctx->modshare = (kmodshare_t**)calloc(sizeof(kmodshare_t*), MOD_MAX);
		newctx->modlocal = (kmodlocal_t**)calloc(sizeof(kmodlocal_t*), MOD_MAX);

		MODLOGGER_init(_ctx, newctx);
		MODGC_init(_ctx, newctx);
		KCLASSTABLE_init(_ctx, newctx);
	}
	else {   // others take ctx as its parent
		newctx = (kcontext_t*)KCALLOC(sizeof(kcontext_t), 1);
		newctx->lib2 = _ctx->lib2;
		newctx->plat = _ctx->plat;
		newctx->share = _ctx->share;
		newctx->modshare = _ctx->modshare;
		newctx->modlocal = (kmodlocal_t**)KCALLOC(sizeof(kmodlocal_t*), MOD_MAX);
		MODGC_init(_ctx, newctx);
//		MODLOGGER_init(_ctx, newctx);
	}
	KRUNTIME_init(_ctx, newctx, plat->stacksize);
	if(IS_ROOTCTX(newctx)) {
		MODCODE_init(_ctx, newctx);
		MODSUGAR_init(_ctx, newctx);
		KCLASSTABLE_loadMethod(_ctx);
		MODSUGAR_loadMethod(_ctx);
	}
	else {
//		for(i = 0; i < MOD_MAX; i++) {
//			if(newctx->modshare[i] != NULL && newctx->modshare[i]->new_local != NULL) {
//				newctx->mod[i] = newctx->modshare[i]->new_local((CTX_t)newctx, newctx->modshare[i]);
//			}
//		}
	}
	return newctx;
}
Beispiel #3
0
static void KRUNTIME_init(CTX, kcontext_t *ctx, size_t stacksize)
{
	size_t i;
	kstack_t *base = (kstack_t*)KCALLOC(sizeof(kstack_t), 1);
	base->stacksize = stacksize;
	base->stack = (ksfp_t*)KCALLOC(sizeof(ksfp_t), stacksize);
	assert(stacksize>64);
	base->stack_uplimit = base->stack + (stacksize - 64);
	for(i = 0; i < stacksize; i++) {
		KINITv(base->stack[i].o, K_NULL);
	}
	KINITv(base->gcstack, new_(Array, K_PAGESIZE/sizeof(void*)));
	KARRAY_INIT(&base->cwb, K_PAGESIZE * 4);
	KARRAY_INIT(&base->ref, K_PAGESIZE);
	base->reftail = base->ref.refhead;
	ctx->esp = base->stack;
	ctx->stack = base;
}
Beispiel #4
0
void knh_EventTBL_expand(CTX)
{
	size_t s = ctx->share->sizeEventTBL, max = ctx->share->capacityEventTBL * 2;
	knh_EventTBL_t *newt = (knh_EventTBL_t*)KCALLOC(SIZEOF_TEXPT(max));
	knh_bzero(newt, SIZEOF_TEXPT(max));
	knh_memcpy(newt, ctx->share->EventTBL, SIZEOF_TEXPT(s));
	((kshare_t*)ctx->share)->EventTBL = newt;
	((kshare_t*)ctx->share)->capacityEventTBL = max;
}
Beispiel #5
0
void ksampleshare_init(CTX, kcontext_t *ctx)
{
	ksampleshare_t *mod = (ksampleshare_t *) KCALLOC(sizeof(ksampleshare_t));
	mod->h.name     = "sample";
	mod->h.setup    = ksampleshare_setup;
	mod->h.reftrace = ksampleshare_reftrace;
	mod->h.free     = ksampleshare_free;
	Konoha_setModule(MOD_SAMPLE, (kmodshare_t*)mod, 0);
}
Beispiel #6
0
static kbool_t float_initPackage(KonohaContext *kctx, kNameSpace *ns, int argc, const char**args, kfileline_t pline)
{
	KonohaFloatModule *base = (KonohaFloatModule*)KCALLOC(sizeof(KonohaFloatModule), 1);
	base->h.name     = "float";
	base->h.setup    = kmodfloat_setup;
	base->h.reftrace = kmodfloat_reftrace;
	base->h.free     = kmodfloat_free;
	KLIB Konoha_setModule(kctx, MOD_float, &base->h, pline);

	KDEFINE_CLASS defFloat = {
		STRUCTNAME(Float),
		.cflag = CFLAG_Int,
		.init = Float_init,
		.p     = Float_p,
	};
	base->cFloat = KLIB Konoha_defineClass(kctx, ns->packageId, PN_konoha, NULL, &defFloat, pline);
	int FN_x = FN_("x");
	KDEFINE_METHOD MethodData[] = {
		_Public|_Const|_Im, _F(Float_opPlus), TY_Float, TY_Float, MN_("+"), 0,
		_Public|_Const|_Im, _F(Float_opADD), TY_Float, TY_Float, MN_("+"), 1, TY_Float, FN_x,
		_Public|_Const|_Im, _F(Int_opADD), TY_Float, TY_Int, MN_("+"), 1, TY_Float, FN_x,
		_Public|_Const|_Im, _F(Float_opSUB), TY_Float, TY_Float, MN_("-"), 1, TY_Float, FN_x,
		_Public|_Const|_Im, _F(Int_opSUB), TY_Float, TY_Int, MN_("-"), 1, TY_Float, FN_x,
		_Public|_Const|_Im, _F(Float_opMUL), TY_Float, TY_Float, MN_("*"), 1, TY_Float, FN_x,
		_Public|_Im, _F(Float_opDIV), TY_Float, TY_Float, MN_("/"), 1, TY_Float, FN_x,
		_Public|_Const|_Im, _F(Float_opEQ),  TY_Boolean, TY_Float, MN_("=="), 1, TY_Float, FN_x,
		_Public|_Const|_Im, _F(Float_opNEQ), TY_Boolean, TY_Float, MN_("!="), 1, TY_Float, FN_x,
		_Public|_Const|_Im, _F(Float_opLT),  TY_Boolean, TY_Float, MN_("<"), 1, TY_Float, FN_x,
		_Public|_Const|_Im, _F(Float_opLTE), TY_Boolean, TY_Float, MN_("<="), 1, TY_Float, FN_x,
		_Public|_Const|_Im, _F(Float_opGT),  TY_Boolean, TY_Float, MN_(">"), 1, TY_Float, FN_x,
		_Public|_Const|_Im, _F(Float_opGTE), TY_Boolean, TY_Float, MN_(">="), 1, TY_Float, FN_x,
		_Public|_Const|_Im, _F(Int_opMUL), TY_Float, TY_Int, MN_("*"), 1, TY_Float, FN_x,
		_Public|_Im, _F(Int_opDIV), TY_Float, TY_Int, MN_("/"), 1, TY_Float, FN_x,
		_Public|_Const|_Im, _F(Int_opEQ),  TY_Boolean, TY_Int, MN_("=="), 1, TY_Float, FN_x,
		_Public|_Const|_Im, _F(Int_opNEQ), TY_Boolean, TY_Int, MN_("!="), 1, TY_Float, FN_x,
		_Public|_Const|_Im, _F(Int_opLT),  TY_Boolean, TY_Int, MN_("<"), 1, TY_Float, FN_x,
		_Public|_Const|_Im, _F(Int_opLTE), TY_Boolean, TY_Int, MN_("<="), 1, TY_Float, FN_x,
		_Public|_Const|_Im, _F(Int_opGT),  TY_Boolean, TY_Int, MN_(">"), 1, TY_Float, FN_x,
		_Public|_Const|_Im, _F(Int_opGTE), TY_Boolean, TY_Int, MN_(">="), 1, TY_Float, FN_x,

		_Public|_Const|_Im|_Coercion, _F(Float_toInt), TY_Int, TY_Float, MN_to(TY_Int), 0,
		_Public|_Const|_Im|_Coercion, _F(Int_toFloat), TY_Float, TY_Int, MN_to(TY_Float), 0,
		_Public|_Const|_Im, _F(Float_toString), TY_String, TY_Float, MN_to(TY_String), 0,
		_Public|_Const|_Im, _F(String_toFloat), TY_Float, TY_String, MN_to(TY_Float), 0,
		_Public|_Const|_Im, _F(Float_opMINUS), TY_Float, TY_Float, MN_("-"), 0,
		DEND,
	};
	KLIB kNameSpace_loadMethodData(kctx, ns, MethodData);
	KDEFINE_FLOAT_CONST FloatData[] = {
		{"FLOAT_EPSILON", TY_Float, DBL_EPSILON},
		{"Infinity", TY_Float, INFINITY},
		{"NaN", TY_Float, NAN},
		{}
	};
	KLIB kNameSpace_loadConstData(kctx, ns, KonohaConst_(FloatData), pline);
	return true;
}
Beispiel #7
0
void ksampleshare_setup(CTX, struct kmodshare_t *def)
{
	if(_ctx->mod[MOD_SAMPLE] == NULL) {
		ksamplemod_t *mod = (ksamplemod_t *) KCALLOC(sizeof(ksamplemod_t));
		mod->h.reftrace = ksamplemod_reftrace;
		mod->h.free     = ksamplemod_free;
		KINITv(mod->array, new_(Array, 8));
		_ctx->mod[MOD_SAMPLE] = (kmodlocal_t*)mod;
	}
}
Beispiel #8
0
static void kmodcode_setup(KonohaContext *kctx, struct KonohaModule *def, int newctx)
{
	if(!newctx) { // lazy setup
		assert(kctx->modlocal[MOD_code] == NULL);
		ctxcode_t *base = (ctxcode_t*)KCALLOC(sizeof(ctxcode_t), 1);
		base->h.reftrace = ctxcode_reftrace;
		base->h.free     = ctxcode_free;
		KINITv(base->codeList, new_(Array, K_PAGESIZE/sizeof(void*)));
		KINITv(base->constPools, new_(Array, 64));
		kctx->modlocal[MOD_code] = (KonohaModuleContext*)base;
	}
}
Beispiel #9
0
void MODCODE_init(KonohaContext *kctx, KonohaContextVar *ctx)
{
	KModuleByteCode *base = (KModuleByteCode*)KCALLOC(sizeof(KModuleByteCode), 1);
	opcode_check();
	base->h.name     = "minivm";
	base->h.setup    = kmodcode_setup;
	base->h.reftrace = kmodcode_reftrace;
	base->h.free     = kmodcode_free;
	KLIB Konoha_setModule(kctx, MOD_code, &base->h, 0);

	KDEFINE_CLASS defBasicBlock = {
		STRUCTNAME(BasicBlock),
		.init = BasicBlock_init,
		.free = BasicBlock_free,
	};

	KDEFINE_CLASS defByteCode = {
		STRUCTNAME(ByteCode),
		.init = ByteCode_init,
		.reftrace = ByteCode_reftrace,
		.free = ByteCode_free,
	};

	base->cBasicBlock = KLIB Konoha_defineClass(kctx, PackageId_sugar, PackageId_sugar, NULL, &defBasicBlock, 0);
	base->cByteCode = KLIB Konoha_defineClass(kctx, PackageId_sugar, PackageId_sugar, NULL, &defByteCode, 0);
	kmodcode_setup(kctx, &base->h, 0/*lazy*/);
	{
		INIT_GCSTACK();
		kBasicBlock* ia = (kBasicBlock*)new_(BasicBlock, 0);
		kBasicBlock* ib = (kBasicBlock*)new_(BasicBlock, 0);
		PUSH_GCSTACK(ia);
		PUSH_GCSTACK(ib);
		kBasicBlock_add(ia, THCODE, _THCODE);
		kBasicBlock_add(ia, NCALL); // FUNCCALL
		kBasicBlock_add(ia, ENTER);
		kBasicBlock_add(ia, EXIT);
		kBasicBlock_add(ib, RET);   // NEED TERMINATION
		ia->nextBlock = ib;
		kByteCode *kcode = new_ByteCode(kctx, ia, ib);
		KINITv(kmodcode->codeNull, kcode);
		VirtualMachineInstruction *pc = KonohaVirtualMachine_run(kctx, kctx->esp, kcode->code);
		CODE_ENTER = pc;
		CODE_ENTER = pc+1;
		KLIB kArray_clear(kctx, ctxcode->codeList, 0);
		RESET_GCSTACK();
	}
	KonohaLibVar *l = (KonohaLibVar*)kctx->klib;
	l->kMethod_setFunc = kMethod_setFunc;
	l->kMethod_genCode = kMethod_genCode;
}
Beispiel #10
0
knh_cond_t *kthread_cond_init(CTX)
{
	knh_cond_t *c = (knh_cond_t *)KCALLOC(sizeof(knh_cond_t));
	// Create an auto-reset event.
	c->events_[E_SIGNAL] = CreateEvent(NULL,  // no secfileidty
			FALSE, // auto-reset event
			FALSE, // non-signaled initially
			NULL); // unnamed
	// Create a manual-reset event.
	c->events_[E_BROADCAST] = CreateEvent(NULL,  // no secfileidty
			TRUE,  // manual-reset
			FALSE, // non-signaled initially
			NULL); // unnamed
	return c;
}
Beispiel #11
0
static kByteCode* new_ByteCode(KonohaContext *kctx, kBasicBlock *beginBlock, kBasicBlock *endBlock)
{
#define CT_ByteCodeVar CT_ByteCode
	kByteCodeVar *kcode = GCSAFE_new(ByteCodeVar, NULL);
	kBasicBlock *prev[1] = {};
	kcode->fileid = ctxcode->uline; //TODO
	kcode->codesize = BasicBlock_size(kctx, beginBlock, 0) * sizeof(VirtualMachineInstruction);
	kcode->code = (VirtualMachineInstruction*)KCALLOC(kcode->codesize, 1);
	endBlock->code = kcode->code; // dummy
	{
		VirtualMachineInstruction *op = BasicBlock_copy(kctx, kcode->code, beginBlock, prev);
		DBG_ASSERT(op - kcode->code > 0);
		endBlock->code = NULL;
		BasicBlock_copy(kctx, op, endBlock, prev);
		BasicBlock_setjump(beginBlock);
	}
	return kcode;
}
Beispiel #12
0
/*
 * Allocate a new label.
 */
label*
KaffeJIT_newLabel(void)
{
    int i;
    label* ret = currLabel;

    if (ret == 0) {
        /* Allocate chunk of label elements */
        ret = KCALLOC(ALLOCLABELNR, sizeof(label));
        addToCounter(&jitmem, "jitmem-temp", 1,
                     ALLOCLABELNR * sizeof(label));

        /* Attach to current chain */
        if (lastLabel == 0) {
            firstLabel = ret;
        }
        else {
            lastLabel->next = ret;
        }
        lastLabel = &ret[ALLOCLABELNR-1];

        /* Link elements into list */
        for (i = 0; i < ALLOCLABELNR-1; i++) {
#if defined(KAFFE_VMDEBUG)
            sprintf(ret[i].name, "L%d", labelCount + i);
#endif

            ret[i].next = &ret[i+1];
        }
        ret[ALLOCLABELNR-1].next = NULL;
    }
    currLabel = ret->next;
#if defined(KAFFE_VMDEBUG)
    labelCount += 1;
#endif
    return (ret);
}
Beispiel #13
0
static kcontext_t* new_RootContext(void)
{
	kcontext_t *ctx = (kcontext_t*)new_hcontext(NULL);
	const knh_LoaderAPI_t *kapi = knh_getLoaderAPI();
	kshare_t *share = (kshare_t*)malloc(sizeof(kshare_t) + sizeof(kstatinfo_t) + sizeof(knh_ServiceSPI_t));
	ctx->share = share;
	knh_bzero(share, sizeof(kshare_t) + sizeof(kstatinfo_t) + sizeof(knh_ServiceSPI_t));
	share->syslock = knh_mutex_malloc(_ctx);
	ctx->stat = (kstatinfo_t*)((share+1));
	ctx->spi = (const knh_ServiceSPI_t*)(_ctx->stat + 1);
	initServiceSPI((knh_ServiceSPI_t*)ctx->spi);

	kmemshare_init(_ctx);
	share->ClassTBL = (kclass_t**)KMALLOC((CTX)ctx, sizeof(struct _kclass*)*(K_CLASSTABLE_INIT));
	knh_bzero(share->ClassTBL, sizeof(struct _kclass*)*(K_CLASSTABLE_INIT));
	share->sizeClassTBL = 0;
	share->capacityClassTBL  = K_CLASSTABLE_INIT;

	share->EventTBL = (knh_EventTBL_t*)KCALLOC(SIZEOF_TEXPT(K_EVENTTBL_INIT));
	knh_bzero((void*)share->EventTBL, SIZEOF_TEXPT(K_EVENTTBL_INIT));
	share->sizeEventTBL = 0;
	share->capacityEventTBL  = K_EVENTTBL_INIT;
	kshare_loadStructData(_ctx, kapi);

	KINITv(share->funcDictSet, new_DictSet0(_ctx, 0, 0, "funcDictSet"));
	KINITv(share->constPtrMap, new_PtrMap(_ctx, 0));
	KINITv(share->inferPtrMap, new_PtrMap(_ctx, 0));
	KINITv(share->xdataPtrMap, new_PtrMap(_ctx, 0));
	KINITv(share->constPools, new_Array0(_ctx, 0));
	knh_loadSystemTypeMapRule(_ctx);
	knh_ClassTBL_setConstPool(_ctx, ClassTBL(CLASS_Int));
	knh_ClassTBL_setConstPool(_ctx, ClassTBL(CLASS_Float));
#ifdef K_USING_STRINGPOOL
	knh_ClassTBL_setConstPool(_ctx, ClassTBL(CLASS_String));
#endif
	{
		kObject *p = (kObject*)new_hObject_(_ctx, ClassTBL(CLASS_Object));
		Object_setNullObject(p, 1);
		KINITv(share->constNull, p);
	}
	{
		kBoolean *o = new_H(Boolean);
		o->n.bvalue = 1;
		KINITv(share->constTrue, o);
		o = new_H(Boolean);
		o->n.bvalue = 0;
		KINITv(share->constFalse, o);
	}
	{
		static const kdim_t dimINIT = {};
		kArray *a = new_H(Array);
		(a)->size = 0;
		(a)->dim = &dimINIT;
		KINITv(share->emptyArray, a);
	}
	share->tString = (kString**)KCALLOC(SIZEOF_TSTRING);
	knh_bzero(share->tString, SIZEOF_TSTRING);
	knh_loadScriptSystemString(_ctx);
	KINITv(share->cwdPath, new_CurrentPath(_ctx));

	/* These are not shared, but needed to initialize System*/
	knh_stack_initexpand(_ctx, NULL, K_STACKSIZE);
	KINITv(share->packageDictMap, new_DictMap0(_ctx, 0, 1/*isCaseMap*/, "packageDictMap"));
	KINITv(share->secfileidtyDictMap, new_DictMap0(_ctx, 0, 1/*isCaseMap*/, "secfileidtyDictMap")); // added by Wakamori
	KINITv(share->classNameDictSet, new_DictSet0(_ctx, 128, 1/*isCaseMap*/, "classNameDictSet"));
	KINITv(share->eventDictSet, new_DictSet0(_ctx, 32, 1/*isCaseMap*/, "eventDictSet"));
	KINITv(share->streamDpiDictSet, new_DictSet0(_ctx, 0, 1/*isCaseMap*/, "streamDpiDictSet"));
	KINITv(share->mapDpiDictSet, new_DictSet0(_ctx, 0, 1/*isCaseMap*/, "mapDpiDictSet"));
	KINITv(share->convDpiDictSet, new_DictSet0(_ctx, 0, 1/*isCaseMap*/, "convDpiDictSet"));
	KINITv(share->rconvDpiDictSet, new_DictSet0(_ctx, 0, 1/*isCaseMap*/, "convDpiDictSet"));

	KINITv(share->enc,   new_T(knh_getSystemEncoding()));
	KINITv(share->in,    new_InputStreamStdIn(_ctx, share->enc));
	KINITv(share->out,   new_OutputStreamStdOut(_ctx, share->enc));
	KINITv(share->err,   new_OutputStreamStdErr(_ctx, share->enc));

	KINITv(share->props, new_DictMap0(_ctx, 20, 1/*isCaseMap*/, "System.props"));
	KINITv(share->symbolDictCaseSet, new_DictSet0(_ctx, K_TFIELD_SIZE + 10, 1/*isCaseMap*/, "System.symbolDictSet"));
	KINITv(share->symbolList, new_Array0(_ctx, K_TFIELD_SIZE + 10));
	KINITv(share->urnDictSet, new_DictSet0(_ctx, 0, 0/*isCaseMap*/, "System.urnDictSet"));
	KINITv(share->urns, new_Array0(_ctx, 1));
	KINITv(share->corelang, new_(Lang));
	knh_initSugarData(_ctx);
	KINITv(share->tokenDictSet, new_DictSet0(_ctx, (TT_MAX - STT_MAX), 0/*isCaseMap*/, "System.tokenDictSet"));
//	KINITv(share->URNAliasDictMap, new_DictMap0(_ctx, 0, 0/*isCaseMap*/, "System.URNAliasDictMap"));

	KINITv(share->rootks, new_(KonohaSpace));
	knh_loadScriptSystemData(_ctx, share->rootks, kapi);
	knh_System_initPath(_ctx);                 // require rootks
	KINITv(_ctx->script, new_(Script));     // require rootks
	KINITv(_ctx->gma, new_(GammaBuilder));         // require script
	knh_loadScriptSystemKonohaCode(_ctx);      // require gamma
	loadPolicy(_ctx); // added by Wakamori
	knh_loadScriptSystemMethod(_ctx, kapi);
	CommonContext_init(_ctx, ctx);
	knh_loadScriptTermData(_ctx);
	knh_loadScriptAliasTermData(_ctx);
	share->ctx0 = ctx;
	knh_GammaBuilder_init(_ctx);  // initalize gamma->gf, reported by uh
	knh_initBuiltInPackage(_ctx, knh_getLoaderAPI());

	/* CompilerAPI */
	KINITv(share->konoha_compiler, K_NULL);
	share->compilerAPI = NULL;

	share->contextCounter = 1;
	share->threadCounter = 1;
	share->stopCounter = 0;
	share->gcStopCounter = 0;
	KINITv(share->contextListNULL, new_Array0(_ctx, 4));
	kArray_add(ctx->share->contextListNULL, knh_toContext(_ctx));
#if defined(K_USING_THREAD)
	share->stop_cond = kthread_cond_init(_ctx);
	share->start_cond = kthread_cond_init(_ctx);
	share->close_cond = kthread_cond_init(_ctx);
#endif
	return ctx;
}
Beispiel #14
0
knh_cond_t *kthread_cond_init(CTX)
{
	pthread_cond_t *c = (pthread_cond_t *)KCALLOC(sizeof(pthread_cond_t));
	pthread_cond_init(c, NULL);
	return (knh_cond_t *)c;
}