00001 /* 00002 FALCON - The Falcon Programming Language. 00003 FILE: flc_vmcontext.h 00004 00005 Virtual Machine coroutine execution context. 00006 ------------------------------------------------------------------- 00007 Author: Giancarlo Niccolai 00008 Begin: mar nov 9 2004 00009 00010 ------------------------------------------------------------------- 00011 (C) Copyright 2004: the FALCON developers (see list in AUTHORS file) 00012 00013 See LICENSE file for licensing details. 00014 */ 00015 00020 #ifndef flc_vmcontext_H 00021 #define flc_vmcontext_H 00022 00023 #include <falcon/setup.h> 00024 #include <falcon/types.h> 00025 #include <falcon/genericvector.h> 00026 #include <falcon/genericlist.h> 00027 #include <falcon/basealloc.h> 00028 #include <falcon/livemodule.h> 00029 #include <falcon/stackframe.h> 00030 00031 namespace Falcon { 00032 00033 class Symbol; 00034 class Item; 00035 class VMSemaphore; 00036 00038 class FALCON_DYN_CLASS VMContext: public BaseAlloc 00039 { 00040 Item m_regA; 00041 Item m_regB; 00042 00043 //Item m_regS1; 00044 Item m_regL1; 00045 Item m_regL2; 00046 Item m_regBind; 00047 Item m_regBindP; 00048 00049 ItemArray m_stack; 00050 VMSemaphore *m_sleepingOn; 00051 00057 const Symbol* m_symbol; 00058 00060 LiveModule *m_lmodule; 00061 00065 numeric m_schedule; 00066 00067 int32 m_priority; 00068 00072 uint32 m_pc; 00073 00080 uint32 m_pc_next; 00081 00086 uint32 m_stackBase; 00087 00089 uint32 m_tryFrame; 00090 00092 bool m_atomicMode; 00093 00094 friend class VMSemaphore; 00095 00096 public: 00097 VMContext(); 00098 VMContext( const VMContext& other ); 00099 ~VMContext(); 00100 00102 void wakeup( bool signaled = false ); 00103 00104 void priority( int32 value ) { m_priority = value; } 00105 int32 priority() const { return m_priority; } 00106 00107 void schedule( numeric value ) { m_schedule = value; } 00108 numeric schedule() const { return m_schedule; } 00109 00117 void scheduleAfter( numeric secs ); 00118 00120 bool isWaitingForever() const { return m_sleepingOn != 0 && m_schedule < 0; } 00121 00122 VMSemaphore* waitingOn() const { return m_sleepingOn; } 00123 00124 void waitOn( VMSemaphore* sem, numeric value=-1 ); 00125 void signaled(); 00126 00127 //=========================== 00128 uint32& pc() { return m_pc; } 00129 const uint32& pc() const { return m_pc; } 00130 00131 uint32& pc_next() { return m_pc_next; } 00132 const uint32& pc_next() const { return m_pc_next; } 00133 00134 uint32& stackBase() { return m_stackBase; } 00135 const uint32& stackBase() const { return m_stackBase; } 00136 00137 uint32& tryFrame() { return m_tryFrame; } 00138 const uint32& tryFrame() const { return m_tryFrame; } 00139 00140 Item ®A() { return m_regA; } 00141 const Item ®A() const { return m_regA; } 00142 Item ®B() { return m_regB; } 00143 const Item ®B() const { return m_regB; } 00144 00145 Item ®Bind() { return m_regBind; } 00146 const Item ®Bind() const { return m_regBind; } 00147 /* 00148 Item ®Bind() { return currentFrame()->m_binding; } 00149 const Item ®Bind() const { return currentFrame()->m_binding; } 00150 */ 00151 Item ®BindP() { return m_regBindP; } 00152 const Item ®BindP() const { return m_regBindP; } 00153 00154 Item &self() { return currentFrame()->m_self; } 00155 const Item &self() const { return currentFrame()->m_self; } 00156 00157 /* 00158 Item &self() { return m_regS1; } 00159 const Item &self() const { return m_regS1; } 00160 */ 00164 const Item &latch() const { return m_regL1; } 00168 Item &latch() { return m_regL1; } 00169 00173 const Item &latcher() const { return m_regL2; } 00177 Item &latcher() { return m_regL2; } 00178 00179 ItemArray &stack() { return m_stack; } 00180 const ItemArray &stack() const { return m_stack; } 00181 00182 VMSemaphore *sleepingOn() const { return m_sleepingOn; } 00183 void sleepOn( VMSemaphore *sl ) { m_sleepingOn = sl; } 00184 00186 ItemArray &globals() { return m_lmodule->globals(); } 00187 00189 const ItemArray &globals() const { return m_lmodule->globals(); } 00190 00192 LiveModule *lmodule() const { return m_lmodule; } 00193 00195 void lmodule(LiveModule *lm) { m_lmodule = lm; } 00196 00198 const Symbol *symbol() const { return m_symbol; } 00199 00201 void symbol( const Symbol* s ) { m_symbol = s; } 00202 00204 byte* code() const { 00205 fassert( symbol()->isFunction() ); 00206 return symbol()->getFuncDef()->code(); 00207 } 00208 00210 StackFrame* currentFrame() const 00211 { 00212 return (StackFrame *) &m_stack[ stackBase() - VM_FRAME_SPACE ]; 00213 } 00214 00220 void createFrame( uint32 pcount, ext_func_frame_t frameEndFunc = 0 ); 00221 00222 bool atomicMode() const { return m_atomicMode; } 00223 void atomicMode( bool b ) { m_atomicMode = b; } 00224 00226 void addLocals( uint32 space ) 00227 { 00228 if ( stack().length() < stackBase() + space ) 00229 stack().resize( stackBase() + space ); 00230 } 00231 00243 const Item *local( uint32 itemId ) const 00244 { 00245 return stack()[ stackBase() + itemId ].dereference(); 00246 } 00247 00254 Item *local( uint32 itemId ) 00255 { 00256 return stack()[ stackBase() + itemId ].dereference(); 00257 } 00258 00275 void returnHandler( ext_func_frame_t callbackFunc ) 00276 { 00277 currentFrame()->m_endFrameFunc = callbackFunc; 00278 } 00279 00280 00281 ext_func_frame_t returnHandler() const 00282 { 00283 if ( stackBase() > VM_FRAME_SPACE ) 00284 { 00285 return currentFrame()->m_endFrameFunc; 00286 } 00287 return 0; 00288 } 00289 00295 void pushParameter( const Item &item ) { stack().append(item); } 00296 00297 }; 00298 00299 } 00300 00301 #endif 00302 00303 /* end of flc_vmcontext.h */