view runtime/builtin.c @ 186:ba35ab624ec2

Add support for raw C function output from C backend as well as an option to use Boehm-GC instead of reference counting
author Mike Pavone <pavone@retrodev.com>
date Fri, 07 Oct 2011 00:10:02 -0700
parents f2cb85c53ced
children
line wrap: on
line source

#include "builtin.h"
#include "object.h"
#include "integer.h"
#include "bool.h"
#include "context.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>

void register_builtin_type(uint32_t type)
{
	blueprint * bp;
	switch(type)
	{
	case TYPE_BLUEPRINT:
		bp = register_type_byid(TYPE_BLUEPRINT, sizeof(blueprint *), NULL, NULL, NULL);
		break;
	case TYPE_CONTEXT:
		register_type_byid(TYPE_CONTEXT, sizeof(context *), NULL, NULL, NULL);
	}
}

void register_builtin_types()
{
	uint32_t i;
	for(i = 0; i < TYPE_FIRST_USER; ++i)
		register_builtin_type(i);
}
object * make_Int64(int64_t val)
{
	t_Int64 * obj;
	object * ret = new_object(TYPE_INT64);
	obj = (t_Int64 *)ret;
	obj->Num = val;
	return ret;
}


object * make_Int32(int32_t val)
{
	t_Int32 * obj;
	object * ret = new_object(TYPE_INT32);
	obj = (t_Int32 *)ret;
	obj->Num = val;
	return ret;
}

object * make_Int16(int16_t val)
{
	t_Int16 * obj;
	object * ret = new_object(TYPE_INT16);
	obj = (t_Int16 *)ret;
	obj->Num = val;
	return ret;
}

object * make_Int8(int8_t val)
{
	t_Int8 * obj;
	object * ret = new_object(TYPE_INT8);
	obj = (t_Int8 *)ret;
	obj->Num = val;
	return ret;
}

object * make_UInt64(uint64_t val)
{
	t_UInt64 * obj;
	object * ret = new_object(TYPE_UINT64);
	obj = (t_UInt64 *)ret;
	obj->Num = val;
	return ret;
}

object * make_UInt32(uint32_t val)
{
	t_UInt32 * obj;
	object * ret = new_object(TYPE_UINT32);
	obj = (t_UInt32 *)ret;
	obj->Num = val;
	return ret;
}

object * make_UInt16(uint16_t val)
{
	t_UInt16 * obj;
	object * ret = new_object(TYPE_UINT16);
	obj = (t_UInt16 *)ret;
	obj->Num = val;
	return ret;
}

object * make_UInt8(uint8_t val)
{
	t_UInt8 * obj;
	object * ret = new_object(TYPE_UINT8);
	obj = (t_UInt8 *)ret;
	obj->Num = val;
	return ret;
}

object * make_String(char * text)
{
#ifdef RAW_FUNC
	returntype ret;
#endif
	object * params[1];
	t_Array * arr = (t_Array *)_internal_array_allocnaked(strlen(text), make_Blueprint(TYPE_UINT8));
	arr->payload.Length = arr->payload.Storage;
	memcpy(((char *)arr) + sizeof(t_Array), text, arr->payload.Length);
	
	params[0] = (object *)arr;
#ifdef RAW_FUNC
	ret = f_String(1, params);
	return ret.retvals[0];
#else
	rhope(FUNC_String, params, 1, 1);
	return params[0];
#endif
}

object * make_Bool(int32_t val)
{
	t_Boolean * b = (t_Boolean *)new_object(TYPE_BOOLEAN);
	b->Val = val != 0;
	return (object*)b;
}

object * make_Context(context * ct)
{
	t_Context * c = (t_Context *)new_object(TYPE_CONTEXT);
	c->ct = ct;
	return (object *)c;
}

object * make_Worker(int32_t index, int16_t initialsize, int16_t initialcount)
{
	t_Worker * worker = (t_Worker *)_internal_worker_alloc(initialsize);
	worker->payload.Index = index;
	worker->payload.Count = initialcount;
	return (object *)worker;
}

object * make_List(int32_t numels,...)
{
#ifdef RAW_FUNC
	returntype ret;
#endif
	int32_t idx, elidx;
	object * inout[3];
	va_list args;

	va_start(args, numels);
#ifdef RAW_FUNC
	ret = f_List(1, inout);
	inout[0] = ret.retvals[0];
#else
	rhope(FUNC_List, inout, 0, 1);
#endif
	
	for (idx = 0; idx < numels; ++idx)
	{
		elidx = va_arg(args, int32_t);
		inout[1] = make_Int32(elidx);
		inout[2] = va_arg(args, object *);
#ifdef RAW_FUNC
		ret = f_Append(3, inout);
		inout[0] = ret.retvals[0];
#else
		rhope(FUNC_Append, inout, 3, 3);
#endif
	}
	return inout[0];
}