/*
  Copyright (c) 2008-2012  John Lee (j.y.lee@yeah.net)
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

  * Neither the name of the copyright holders nor the names of
    contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __LIGHTWEIGHT_OBJECT_ORENTED_KERNEL_H
#error "Include <look.h> instead of this file."
#else	// __LIGHTWEIGHT_OBJECT_ORENTED_KERNEL_H

namespace instantiate {
	template<typename TASK, uintptr_t STACK_SIZE = LOOK_STACK_SIZE>
	class task : public TASK {
		static_assert(__is_base_of(look::idle_task_t, TASK), "the type argument for <TASK> must be derived from idle_task_t");

		template<bool>
		class app_task {};

	public:
		template<typename... R>
		__INLINE task(R... r)
		: TASK(r...)
		{
			this->init(stack[(STACK_SIZE + 1) & ~1], reinterpret_cast<void*>(&task::routine));
			create(app_task<__is_base_of(look::base::task_t, task)>());
#if 1
			__asm__ __volatile__(
				".global .LOOK.STACK.%c[t]"		"\n\t"
				".weak .LOOK.STACK.%c[t]"		"\n\t"
				".set .LOOK.STACK.%c[t],%c[s]"
				: : [t] "i" (this), [s] "i" (sizeof(stack) / sizeof(uintptr_t))
			);
#endif
		}

	private:
		__INLINE void create(app_task<true>)
		{
			look::scheduler.create(*this);
		}

		__INLINE void create(app_task<false>)
		{
			__asm__ __volatile__(
				".global %c[idle]"		"\n\t"
				".weak %c[idle]"		"\n\t"
				".set %c[idle],%c[t]"
				: : [idle] "i" (&look::idle_task), [t] "i" (this)
			);
		}

	private:
		uintptr_t stack[((STACK_SIZE + 1) & ~1) + TASK::MIN_CONTEXT_SIZE] __attribute__((aligned(8)));
	};

	template<typename SCHED>
	class scheduler : public SCHED {
		static_assert(__is_base_of(look::base::sched_t, SCHED), "the type argument for <SCHED> must be derived from look::base::sched_t");
	public:
		template<typename... R>
		__INLINE scheduler(R... r)
		: SCHED(r...)
		{
			__asm__ __volatile__(
				".global %c[sched]"		"\n\t"
				".weak %c[sched]"		"\n\t"
				".set %c[sched],%c[s]"
				: : [sched] "i" (&look::scheduler), [s] "i" (this)
			);
		}
	};

	template<typename SYSTICK>
	class systick : public SYSTICK {
		static_assert(__is_base_of(look::systick_t, SYSTICK), "the type argument for <SYSTICK> must be derived from look::systick_t");
	public:
		template<typename... R>
		__INLINE systick(R... r)
		: SYSTICK(r...)
		{
			__asm__ __volatile__(
				".global %c[systick]"	"\n\t"
				".weak %c[systick]"		"\n\t"
				".set %c[systick],%c[s]"
				: : [systick] "i" (&look::systick), [s] "i" (this)
			);
		}
	};
}

#endif	// __LIGHTWEIGHT_OBJECT_ORENTED_KERNEL_H
