vm/kite_vm.h File Reference

#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <pthread.h>
#include <signal.h>
#include <assert.h>
#include "kite_opcodes.h"

Go to the source code of this file.

Data Structures

struct  kite_stack_t
struct  kite_stack_entry_t
struct  kite_funccall_info_t
struct  kite_thread_t
struct  kite_list_t
struct  kite_vm_t
struct  kite_compiler_t

Defines

#define kite_vm_creator_thread(vm)   ((vm)->thread_list[0])
#define kite_vm_push(thd, obj)   kite_push_stack(&(thd)->running_stack, FALSE, kite_reference_object(obj))
#define kite_vm_pop(thd)   (struct kite_object_t*)kite_pop_stack(&(thd)->running_stack)
#define kite_vm_return(thd, obj)
#define STACK_MAX   1024
#define PUSH_FUNC_STACK(t)
#define PUSH_FUNC_STACK_WITH_RET_VALUE(t, nxt)
#define POP_FUNC_STACK
#define NSIG   65

Functions

KITE_EXPORT void kite_app_init ()
KITE_EXPORT kite_vm_tkite_new_vm (char **args)
KITE_EXPORT void kite_free_vm (kite_vm_t **vm)
KITE_EXPORT kite_thread_tkite_new_thread_without_start (kite_vm_t *vm)
KITE_EXPORT kite_thread_tkite_new_thread_bytecode (kite_vm_t *vm, void *ptr)
KITE_EXPORT kite_thread_tkite_new_thread_compiled (kite_vm_t *vm, void *ptr)
KITE_EXPORT void kite_start_bytecode (kite_thread_t *thd, void *ptr)
KITE_EXPORT void kite_start_compiled (kite_thread_t *thd, void *ptr)
KITE_EXPORT void kite_join_thread (kite_vm_t *vm, kite_thread_t *thd)
KITE_EXPORT void kite_exit_thread (kite_vm_t *vm, kite_thread_t *thd)
KITE_EXPORT void kite_push_stack (kite_stack_t **stack, int ref, void *obj)
KITE_EXPORT void kite_destroy_stack (kite_stack_t **stack)
KITE_EXPORT void * kite_pop_stack (kite_stack_t **stack)
KITE_EXPORT void * kite_top_stack (kite_stack_t *stack)
KITE_EXPORT void kite_list_remove (kite_list_t **begin, kite_list_t **end, kite_list_t *item)
KITE_EXPORT void kite_list_add_end (kite_list_t **begin, kite_list_t **end, kite_list_t *item)
KITE_EXPORT void kite_list_add_begin (kite_list_t **begin, kite_list_t **end, kite_list_t *item)
KITE_EXPORT void kite_vm_call_method (kite_thread_t *thd, struct kite_object_t *this, char *name, struct kite_object_t *args, int err)
KITE_EXPORT void kite_vm_call_object (kite_thread_t *thd, struct kite_object_t *this, struct kite_object_t *method, struct kite_object_t *args)
KITE_EXPORT void kite_vm_call_constructor (kite_thread_t *thd, struct kite_object_t *this, struct kite_object_t *args)
KITE_EXPORT int kite_vm_call_operator (kite_thread_t *thd, struct kite_object_t *this, int op, struct kite_object_t *args, int err)
KITE_EXPORT struct kite_object_tkite_vm_compile_from_file (kite_thread_t *thd, char *file)
KITE_EXPORT struct kite_object_tkite_vm_compile_from_fp (kite_thread_t *thd, FILE *fp, char *file, struct kite_object_t *created_obj)
KITE_EXPORT struct kite_object_tkite_vm_compile_from_string (kite_thread_t *thd, char *code, int ln)
KITE_EXPORT struct kite_object_tkite_vm_compile_from_string_without_obj (kite_thread_t *thd, char *code, int ln, struct kite_object_t *obj)
KITE_EXPORT struct kite_object_tkite_vm_compile_from_file_without_create (kite_thread_t *thd, char *file, struct kite_object_t *created_obj)

Variables

KITE_EXPORT struct kite_object_tkite_signal_handlers [NSIG+1]


Define Documentation

#define kite_vm_creator_thread ( vm   )     ((vm)->thread_list[0])

Return first thread in application.

Parameters:
vm The VM object to operate on.

#define kite_vm_pop ( thd   )     (struct kite_object_t*)kite_pop_stack(&(thd)->running_stack)

Pop item from running stack.

Parameters:
thd The current thread.
Returns:
The item popped from the running stack.

Referenced by kite_boolean_object(), kite_float_object(), kite_int_object(), kite_string_object(), and kite_vm_execute().

#define kite_vm_push ( thd,
obj   )     kite_push_stack(&(thd)->running_stack, FALSE, kite_reference_object(obj))

Push item onto running stack.

Parameters:
thd The current thread.
obj The object to push.

Referenced by kite_vm_call_object().

#define kite_vm_return ( thd,
obj   ) 

Value:

{ \
        kite_object_t *tmp = (obj); \
        kite_vm_push((thd), tmp); \
        kite_dereference_object(tmp); \
        return; \
    }
Push item onto running stack and return from method.
Parameters:
thd The current thread.
obj The object to push.

#define NSIG   65

Number of possible signals.

Referenced by kite_free_vm(), and kite_new_vm().

#define POP_FUNC_STACK

Value:

kite_pop_stack(&thd->func_stack); \
    if (fc_entry->locals) kite_destruct_symtab(thd, &fc_entry->locals, TRUE); \
    free(fc_entry);

#define PUSH_FUNC_STACK (  ) 

Value:

fc_entry = malloc(sizeof(kite_funccall_info_t)); \
     oldentry = ((kite_funccall_info_t*)kite_top_stack(thd->func_stack)); \
     old_sys = oldentry ? oldentry->sys_or_user : 0; \
     fc_entry->this = t; \
     fc_entry->locals = NULL; \
     fc_entry->file = ins->common.file; \
     fc_entry->line = ins->common.line; \
     fc_entry->last_deref = NULL; \
     fc_entry->last_deref_obj = NULL; \
     fc_entry->sys_or_user = 0; \
     kite_push_stack(&thd->func_stack, FALSE, fc_entry); \
     if (thd->func_stack->length >= STACK_MAX && !old_sys) { \
         kite_object_t *exc; \
         fc_entry->sys_or_user = 1; \
         exc = kite_new_exception(thd, "System.exceptions.StackOverflow", \
                                  "The function call stack has overflowed."); \
         kite_vm_call_method(thd, exc, "throw", kite_new_list(thd), TRUE); \
         fc_entry->sys_or_user = 0; \
         return; \
     }

#define PUSH_FUNC_STACK_WITH_RET_VALUE ( t,
nxt   ) 

Value:

fc_entry = malloc(sizeof(kite_funccall_info_t)); \
     oldentry = ((kite_funccall_info_t*)kite_top_stack(thd->func_stack)); \
     old_sys = oldentry ? oldentry->sys_or_user : 0; \
     fc_entry->this = t; \
     fc_entry->locals = NULL; \
     fc_entry->file = ins->common.file; \
     fc_entry->line = ins->common.line; \
     fc_entry->last_deref = NULL; \
     fc_entry->last_deref_obj = NULL; \
     fc_entry->sys_or_user = 0; \
     kite_push_stack(&thd->func_stack, FALSE, fc_entry); \
     if (thd->func_stack->length >= STACK_MAX && !old_sys) { \
         kite_object_t *exc; \
         fc_entry->sys_or_user = 1; \
         exc = kite_new_exception(thd, "System.exceptions.StackOverflow", \
                                  "The function call stack has overflowed."); \
         kite_vm_call_method(thd, exc, "throw", kite_new_list(thd), TRUE); \
         fc_entry->sys_or_user = 0; \
         return nxt; \
     }

#define STACK_MAX   1024


Function Documentation

KITE_EXPORT void kite_app_init (  ) 

Initialize Kite.

Warning:
This is mandatory before using kite_new_vm() in order to initialize the garbage collector!

KITE_EXPORT void kite_destroy_stack ( kite_stack_t **  stack  ) 

Destroy stack.

Parameters:
[in,out] stack The stack to destroy.

Referenced by kite_vm_compile_from_fp().

KITE_EXPORT void kite_exit_thread ( kite_vm_t vm,
kite_thread_t thd 
)

Force thread to terminate.

Parameters:
vm The VM to operate on.
thd The thread to wait for.

References kite_join_thread(), kite_thread_t::start, kite_thread_t::thread, and kite_thread_t::vm.

Referenced by kite_free_vm().

KITE_EXPORT void kite_free_vm ( kite_vm_t **  vm  ) 

KITE_EXPORT void kite_join_thread ( kite_vm_t vm,
kite_thread_t thd 
)

Wait for thread to terminate.

Parameters:
vm The VM to operate on.
thd The thread to wait for.

References kite_thread_t::thread, kite_vm_t::thread_list, and kite_thread_t::vm.

Referenced by kite_exit_thread().

KITE_EXPORT void kite_list_add_begin ( kite_list_t **  begin,
kite_list_t **  end,
kite_list_t item 
)

Add item to the beginning of a linked list.

Parameters:
[in,out] begin The beginning of the list.
[in,out] end The end of the list.
item The item to add.

References kite_list_t::next, and kite_list_t::prev.

Referenced by kite_dereference_object().

KITE_EXPORT void kite_list_add_end ( kite_list_t **  begin,
kite_list_t **  end,
kite_list_t item 
)

Add item to the end of a linked list.

Parameters:
[in,out] begin The beginning of the list.
[in,out] end The end of the list.
item The item to add.

Referenced by kite_new_class(), kite_new_instance(), and kite_reference_object().

KITE_EXPORT void kite_list_remove ( kite_list_t **  begin,
kite_list_t **  end,
kite_list_t item 
)

Remove item from a linked list.

Parameters:
[in,out] begin The beginning of the list.
[in,out] end The end of the list.
item The item to remove.

References kite_list_t::next, and kite_list_t::prev.

Referenced by kite_dereference_object(), kite_destruct_object_nofree(), and kite_reference_object().

KITE_EXPORT kite_thread_t* kite_new_thread_bytecode ( kite_vm_t vm,
void *  ptr 
)

Create a new bytecode thread (and start it).

Parameters:
vm Virtual machine to create thread on.
ptr Pointer to bytecode to start.
Returns:
A new thread object.

References kite_new_thread_without_start(), and kite_start_bytecode().

KITE_EXPORT kite_thread_t* kite_new_thread_compiled ( kite_vm_t vm,
void *  ptr 
)

Create a new compiled thread (and start it).

Parameters:
vm Virtual machine to create thread on.
ptr Pointer to function to start.
Returns:
A new thread object.

References kite_new_thread_without_start(), and kite_start_compiled().

KITE_EXPORT kite_thread_t* kite_new_thread_without_start ( kite_vm_t vm  ) 

Create a new thread without starting it.

Parameters:
vm The virtual machine to create the thread on.
Returns:
A new thread object.

References kite_thread_t::entry, kite_thread_t::func_stack, kite_vm_t::numthreads, kite_thread_t::running_stack, kite_thread_t::start, kite_vm_t::thread_list, and kite_thread_t::vm.

Referenced by kite_free_vm(), kite_new_thread_bytecode(), kite_new_thread_compiled(), and kite_new_vm().

KITE_EXPORT kite_vm_t* kite_new_vm ( char **  args  ) 

KITE_EXPORT void* kite_pop_stack ( kite_stack_t **  stack  ) 

Pop item off of the stack.

Parameters:
[in,out] stack The stack to operate on.
Returns:
The item at the top of the stack.

References kite_top_stack(), and kite_stack_t::length.

Referenced by kite_vm_call_object(), kite_vm_compile_from_fp(), and kite_vm_compile_from_string_without_obj().

KITE_EXPORT void kite_push_stack ( kite_stack_t **  stack,
int  ref,
void *  obj 
)

Push item onto the stack.

Parameters:
[in,out] stack The stack to push onto.
ref If 1, the object is not a kite_object_t.
obj The object to push.

References kite_stack_t::allocated, kite_stack_t::length, kite_stack_entry_t::obj, kite_stack_entry_t::reference, and kite_stack_t::stack.

Referenced by kite_vm_call_object(), kite_vm_compile_from_fp(), and kite_vm_compile_from_string_without_obj().

KITE_EXPORT void kite_start_bytecode ( kite_thread_t thd,
void *  ptr 
)

Start preexisting thread with bytecode.

Parameters:
thd The thread to start.
ptr Pointer to bytecode to run.

References kite_vm_execute(), PTHREAD_STACK_SIZE, kite_thread_t::start, kite_thread_t::thread, kite_vm_t::thread_list, and kite_thread_t::vm.

Referenced by kite_new_thread_bytecode().

KITE_EXPORT void kite_start_compiled ( kite_thread_t thd,
void *  ptr 
)

Start preexisting thread with compiled C function.

Parameters:
thd The thread to start.
ptr Pointer to C function to run.

References PTHREAD_STACK_SIZE, kite_thread_t::start, kite_thread_t::thread, kite_vm_t::thread_list, and kite_thread_t::vm.

Referenced by kite_new_thread_compiled().

KITE_EXPORT void* kite_top_stack ( kite_stack_t stack  ) 

Return the item at the top of the stack without popping it.

Parameters:
stack The stack to operate on.
Returns:
The item at the top of the stack.

References kite_stack_t::length, kite_stack_entry_t::obj, kite_stack_entry_t::reference, kite_stack_t::stack, and kite_symtab_t::value.

Referenced by kite_pop_stack(), kite_vm_call_object(), and kite_vm_execute_user_method().

KITE_EXPORT void kite_vm_call_constructor ( kite_thread_t thd,
struct kite_object_t this,
struct kite_object_t args 
)

Call constructor on given object.

Parameters:
thd The current thread.
this The object to operate on.
args A list containing the method arguments to pass in.

References KITE_FIND_ANY_IN_SYMTAB, kite_list_count(), kite_vm_call_method(), kite_object_t::object_data, kite_object_t::properties, and TRUE.

Referenced by kite_new_instance_with_constructor().

KITE_EXPORT void kite_vm_call_method ( kite_thread_t thd,
struct kite_object_t this,
char *  name,
struct kite_object_t args,
int  err 
)

Call method on given object.

Parameters:
thd The current thread.
this The object to operate on.
name The name of the method to call.
args A list containing the method arguments to pass in.
err If 1, throw an exception if the method does not exist.
Note:
Method will push its return value to the top of the running stack.

References kite_object_t::builtin_data, kite_object_t::inherit_from, kite_dereference_object(), KITE_FIND_METHOD, kite_list_count(), kite_new_exception(), kite_new_list(), kite_new_string(), kite_reference_object(), kite_vm_call_method(), kite_vm_call_object(), kite_vm_call_operator(), kite_object_t::listvalue, kite_object_t::object_data, OP_METHOD_CALL, TRUE, kite_object_t::type, and kite_symtab_t::value.

Referenced by kite_boolean_object(), kite_dereference_and_load(), kite_float_object(), kite_gc_destroy_all(), kite_gc_incremental(), kite_int_object(), kite_string_object(), kite_vm_call_constructor(), kite_vm_call_method(), and kite_vm_call_operator().

KITE_EXPORT void kite_vm_call_object ( kite_thread_t thd,
struct kite_object_t this,
struct kite_object_t method,
struct kite_object_t args 
)

KITE_EXPORT int kite_vm_call_operator ( kite_thread_t thd,
struct kite_object_t this,
int  op,
struct kite_object_t args,
int  err 
)

Call operator on given object.

Parameters:
thd The current thread.
this The object to operate on.
op The operator to call (see enum kite_operators).
args A list containing the method arguments to pass in.
err If 1, throw an exception if the operator does not exist.
Note:
Method will push its return value to the top of the running stack.

References kite_object_t::inherit_from, KITE_FIND_ANY_IN_SYMTAB, kite_new_exception(), kite_new_list(), kite_vm_call_method(), kite_vm_call_object(), kite_object_t::object_data, OP_TO_METHOD, OP_TO_STRING, kite_object_t::properties, TRUE, and kite_symtab_t::value.

Referenced by kite_vm_call_method().

KITE_EXPORT struct kite_object_t* kite_vm_compile_from_file ( kite_thread_t thd,
char *  file 
) [read]

Compile Kite code from a file.

Parameters:
thd The current thread.
file The path to the file to compile.
Returns:
The object that has been compiled.
Note:
thd->exception will be non-NULL if there was a problem during compile.

References kite_dereference_and_load(), kite_new_class(), and kite_vm_compile_from_fp().

KITE_EXPORT struct kite_object_t* kite_vm_compile_from_file_without_create ( kite_thread_t thd,
char *  file,
struct kite_object_t created_obj 
) [read]

Compile Kite code from a file using pre-created object.

Parameters:
thd The current thread.
file The file to compile.
created_obj The pre-created object to use.
Returns:
The object that has been compiled.
Note:
thd->exception will be non-NULL if there was a problem during compile.

References kite_vm_compile_from_fp().

KITE_EXPORT struct kite_object_t* kite_vm_compile_from_fp ( kite_thread_t thd,
FILE *  fp,
char *  file,
struct kite_object_t created_obj 
) [read]

Compile Kite code from an already open file.

Parameters:
thd The current thread.
fp The file to compile.
file The file name to associate with the object.
created_obj The pre-created object to use.
Returns:
The object that has been compiled.
Note:
thd->exception will be non-NULL if there was a problem during compile.

References kite_object_t::builtin_data, kite_compiler_t::currentLine, kite_compiler_t::decide_exits, FALSE, kite_compiler_t::file, kite_function_t::functype, kite_object_t::funcvalue, kite_destroy_stack(), kite_pop_stack(), kite_push_stack(), kite_compiler_t::obj_created, kite_compiler_t::thd, yylex_destroy(), yylex_init(), yyparse(), yyset_extra(), and yyset_in().

Referenced by kite_vm_compile_from_file(), and kite_vm_compile_from_file_without_create().

KITE_EXPORT struct kite_object_t* kite_vm_compile_from_string ( kite_thread_t thd,
char *  code,
int  ln 
) [read]

Compile Kite code from a string.

Parameters:
thd The current thread.
code The code to compile.
ln The starting line number.
Returns:
The object that has been compiled.
Note:
thd->exception will be non-NULL if there was a problem during compile.

References kite_dereference_and_load(), kite_new_class(), and kite_vm_compile_from_string_without_obj().

KITE_EXPORT struct kite_object_t* kite_vm_compile_from_string_without_obj ( kite_thread_t thd,
char *  code,
int  ln,
struct kite_object_t obj 
) [read]

Compile Kite code from a string using pre-made object.

Parameters:
thd The current thread.
code The code to compile.
ln The starting line number.
obj The object that has been pre-made.
Returns:
The object that has been compiled.
Note:
thd->exception will be non-NULL if there was a problem during compile.

References kite_object_t::builtin_data, kite_compiler_t::currentLine, FALSE, kite_compiler_t::file, kite_function_t::functype, kite_object_t::funcvalue, kite_pop_stack(), kite_push_stack(), kite_compiler_t::obj_created, kite_compiler_t::thd, yy_delete_buffer(), yy_scan_string(), yy_switch_to_buffer(), yylex_destroy(), yylex_init(), yyparse(), and yyset_extra().

Referenced by kite_vm_compile_from_string().


Variable Documentation

KITE_EXPORT struct kite_object_t* kite_signal_handlers[NSIG+1]


Generated on Mon Apr 13 23:02:01 2009 for Kite by  doxygen 1.5.6