gmThread.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. /*
  2. _____ __ ___ __ ____ _ __
  3. / ___/__ ___ _ ___ / |/ /__ ___ / /_____ __ __/ __/_______(_)__ / /_
  4. / (_ / _ `/ ' \/ -_) /|_/ / _ \/ _ \/ '_/ -_) // /\ \/ __/ __/ / _ \/ __/
  5. \___/\_,_/_/_/_/\__/_/ /_/\___/_//_/_/\_\\__/\_, /___/\__/_/ /_/ .__/\__/
  6. /___/ /_/
  7. See Copyright Notice in gmMachine.h
  8. */
  9. #include "gmConfig.h"
  10. #include "gmThread.h"
  11. #include "gmByteCode.h"
  12. #include "gmMachine.h"
  13. #include "gmFunctionObject.h"
  14. #include "gmOperators.h"
  15. #include "gmMachineLib.h"
  16. // helper macros
  17. #define OPCODE_INT(I) *((gmint *) (I)); (I) += sizeof(gmint);
  18. #define OPCODE_PTR(I) *((gmptr *) (I)); (I) += sizeof(gmptr);
  19. #define OPCODE_PTR_NI(I) *((gmptr *) I);
  20. #define OPCODE_FLOAT(I) *((gmfloat *) I); I += sizeof(gmfloat);
  21. #define OPERATOR(TYPE, OPERATOR) (m_machine->GetTypeNativeOperator((TYPE), (OPERATOR)))
  22. #define CALLOPERATOR(TYPE, OPERATOR) (m_machine->GetTypeOperator((TYPE), (OPERATOR)))
  23. #define GMTHREAD_LOG m_machine->GetLog().LogEntry
  24. #define PUSHNULL top->m_type = GM_NULL; top->m_value.m_int = 0; ++top;
  25. // helper functions
  26. void gmGetLineFromString(const char * a_string, int a_line, char * a_buffer, int a_len)
  27. {
  28. const char * cp = a_string, * eol;
  29. int line = 1;
  30. while(a_line > line)
  31. {
  32. while(*cp && *cp != '\n' && *cp != '\r') ++cp;
  33. if(*cp == '\n') { ++cp; while(*cp == '\r') ++cp; ++line; }
  34. else if(*cp == '\r') { ++cp; if(*cp == '\n') ++cp; ++line; }
  35. if(*cp == '\0') { a_buffer[0] = '\0'; return; }
  36. }
  37. eol = cp;
  38. while(*eol && *eol != '\n' && *eol != '\r') ++eol;
  39. int len = (int)(eol - cp);
  40. len = ((a_len - 1) < len) ? (a_len - 1) : len;
  41. memcpy(a_buffer, cp, len);
  42. a_buffer[len] = '\0';
  43. }
  44. //
  45. //
  46. // Implementation of gmThread
  47. //
  48. //
  49. gmThread::gmThread(gmMachine * a_machine, int a_initialByteSize)
  50. {
  51. m_frame = NULL;
  52. m_machine = a_machine;
  53. m_size = a_initialByteSize / sizeof(gmVariable);
  54. m_stack = GM_NEW( gmVariable[m_size] );
  55. m_top = 0;
  56. m_base = 0;
  57. m_numParameters = 0;
  58. #if GMDEBUG_SUPPORT
  59. m_debugUser = 0;
  60. m_debugFlags = 0;
  61. #endif // GMDEBUG_SUPPORT
  62. m_timeStamp = 0;
  63. m_startTime = 0;
  64. m_instruction = NULL;
  65. m_state = KILLED;
  66. m_id = GM_INVALID_THREAD;
  67. m_blocks = NULL;
  68. m_signals = NULL;
  69. m_user = 0;
  70. }
  71. gmThread::~gmThread()
  72. {
  73. Sys_Reset(0);
  74. if(m_stack)
  75. {
  76. delete [] m_stack;
  77. }
  78. }
  79. #if GM_USE_INCGC
  80. void gmThread::GCScanRoots(gmMachine* a_machine, gmGarbageCollector* a_gc)
  81. {
  82. // mark stack
  83. int i;
  84. for(i = 0; i < m_top; ++i)
  85. {
  86. if(m_stack[i].IsReference())
  87. {
  88. gmObject * object = GM_MOBJECT(m_machine, m_stack[i].m_value.m_ref);
  89. a_gc->GetNextObject(object);
  90. }
  91. }
  92. // mark signals
  93. gmSignal * signal = m_signals;
  94. while(signal)
  95. {
  96. if(signal->m_signal.IsReference())
  97. {
  98. gmObject * object = GM_MOBJECT(m_machine, signal->m_signal.m_value.m_ref);
  99. a_gc->GetNextObject(object);
  100. }
  101. signal = signal->m_nextSignal;
  102. }
  103. // mark blocks
  104. gmBlock * block = m_blocks;
  105. while(block)
  106. {
  107. if(block->m_block.IsReference())
  108. {
  109. gmObject * object = GM_MOBJECT(m_machine, block->m_block.m_value.m_ref);
  110. a_gc->GetNextObject(object);
  111. }
  112. block = block->m_nextBlock;
  113. }
  114. }
  115. #else //GM_USE_INCGC
  116. void gmThread::Mark(gmuint32 a_mark)
  117. {
  118. // mark stack
  119. int i;
  120. for(i = 0; i < m_top; ++i)
  121. {
  122. if(m_stack[i].IsReference())
  123. {
  124. gmObject * object = GM_MOBJECT(m_machine, m_stack[i].m_value.m_ref);
  125. if(object->NeedsMark(a_mark)) object->Mark(m_machine, a_mark);
  126. }
  127. }
  128. // mark signals
  129. gmSignal * signal = m_signals;
  130. while(signal)
  131. {
  132. if(signal->m_signal.IsReference())
  133. {
  134. gmObject * object = GM_MOBJECT(m_machine, signal->m_signal.m_value.m_ref);
  135. if(object->NeedsMark(a_mark)) object->Mark(m_machine, a_mark);
  136. }
  137. signal = signal->m_nextSignal;
  138. }
  139. // mark blocks
  140. gmBlock * block = m_blocks;
  141. while(block)
  142. {
  143. if(block->m_block.IsReference())
  144. {
  145. gmObject * object = GM_MOBJECT(m_machine, block->m_block.m_value.m_ref);
  146. if(object->NeedsMark(a_mark)) object->Mark(m_machine, a_mark);
  147. }
  148. block = block->m_nextBlock;
  149. }
  150. }
  151. #endif //GM_USE_INCGC
  152. // RAGE AGAINST THE VIRTUAL MACHINE =)
  153. gmThread::State gmThread::Sys_Execute(gmVariable * a_return)
  154. {
  155. register union
  156. {
  157. const gmuint8 * instruction;
  158. const gmuint32 * instruction32;
  159. };
  160. register gmVariable * top;
  161. gmVariable * base;
  162. gmVariable * operand;
  163. const gmuint8 * code;
  164. if(m_state != RUNNING) return m_state;
  165. #if GMDEBUG_SUPPORT
  166. if(m_debugFlags && m_machine->GetDebugMode() && m_machine->m_isBroken)
  167. {
  168. if(m_machine->m_isBroken(this))
  169. return RUNNING;
  170. }
  171. #endif // GMDEBUG_SUPPORT
  172. // make sure we have a stack frame
  173. GM_ASSERT(m_frame);
  174. GM_ASSERT(GetFunction()->m_type == GM_FUNCTION);
  175. // cache our "registers"
  176. gmFunctionObject * fn = (gmFunctionObject *) GM_MOBJECT(m_machine, GetFunction()->m_value.m_ref);
  177. code = (const gmuint8 *) fn->GetByteCode();
  178. if(m_instruction == NULL) instruction = code;
  179. else instruction = m_instruction;
  180. top = GetTop();
  181. base = GetBase();
  182. //
  183. // start byte code execution
  184. //
  185. for(;;)
  186. {
  187. #ifdef GM_CHECK_USER_BREAK_CALLBACK // This may be defined in gmConfig_p.h
  188. // Check external source to break execution with exception eg. Check for CTRL-BREAK
  189. // Endless loop protection could be implemented with this, or in a similar manner.
  190. if( gmMachine::s_userBreakCallback && gmMachine::s_userBreakCallback(this) )
  191. {
  192. GMTHREAD_LOG("User break. Execution halted.");
  193. goto LabelException;
  194. }
  195. #endif //GM_CHECK_USER_BREAK_CALLBACK
  196. switch(*(instruction32++))
  197. {
  198. //
  199. // unary operator
  200. //
  201. case BC_BIT_INV :
  202. case BC_OP_NEG :
  203. case BC_OP_POS :
  204. case BC_OP_NOT :
  205. {
  206. operand = top - 1;
  207. gmOperatorFunction op = OPERATOR(operand->m_type, (gmOperator) instruction32[-1]);
  208. if(op)
  209. {
  210. op(this, operand);
  211. }
  212. else if((fn = CALLOPERATOR(operand->m_type, (gmOperator) instruction32[-1])))
  213. {
  214. operand[2] = operand[0];
  215. operand[0] = gmVariable(GM_NULL, 0);
  216. operand[1] = gmVariable(GM_FUNCTION, fn->GetRef());
  217. SetTop(operand + 3);
  218. State res = PushStackFrame(1, &instruction, &code);
  219. top = GetTop();
  220. base = GetBase();
  221. if(res == RUNNING) break;
  222. if(res == SYS_YIELD) return RUNNING;
  223. if(res == SYS_EXCEPTION) goto LabelException;
  224. if(res == KILLED) { m_machine->Sys_SwitchState(this, KILLED); GM_ASSERT(0); } // operator should not kill a thread
  225. return res;
  226. }
  227. else
  228. {
  229. GMTHREAD_LOG("unary operator %s undefined for type %s", gmGetOperatorName((gmOperator) instruction32[-1]), m_machine->GetTypeName(operand->m_type));
  230. goto LabelException;
  231. }
  232. break;
  233. }
  234. //
  235. // operator
  236. //
  237. case BC_OP_ADD :
  238. case BC_OP_SUB :
  239. case BC_OP_MUL :
  240. case BC_OP_DIV :
  241. case BC_OP_REM :
  242. case BC_BIT_OR :
  243. case BC_BIT_XOR :
  244. case BC_BIT_AND :
  245. case BC_BIT_SHL :
  246. case BC_BIT_SHR :
  247. case BC_OP_LT :
  248. case BC_OP_GT :
  249. case BC_OP_LTE :
  250. case BC_OP_GTE :
  251. case BC_OP_EQ :
  252. case BC_OP_NEQ :
  253. {
  254. operand = top - 2;
  255. --top;
  256. // NOTE: Classic logic for operators. Higher type processes the operation.
  257. register gmType t1 = operand[1].m_type;
  258. if(operand->m_type > t1) t1 = operand->m_type;
  259. gmOperatorFunction op = OPERATOR(t1, (gmOperator) instruction32[-1]);
  260. if(op)
  261. {
  262. op(this, operand);
  263. }
  264. else if((fn = CALLOPERATOR(t1, (gmOperator) instruction32[-1])))
  265. {
  266. operand[2] = operand[0];
  267. operand[3] = operand[1];
  268. operand[0] = gmVariable(GM_NULL, 0);
  269. operand[1] = gmVariable(GM_FUNCTION, fn->GetRef());
  270. SetTop(operand + 4);
  271. State res = PushStackFrame(2, &instruction, &code);
  272. top = GetTop();
  273. base = GetBase();
  274. if(res == RUNNING) break;
  275. if(res == SYS_YIELD) return RUNNING;
  276. if(res == SYS_EXCEPTION) goto LabelException;
  277. if(res == KILLED) { m_machine->Sys_SwitchState(this, KILLED); GM_ASSERT(0); } // operator should not kill a thread
  278. return res;
  279. }
  280. else
  281. {
  282. GMTHREAD_LOG("operator %s undefined for type %s and %s", gmGetOperatorName((gmOperator) instruction32[-1]), m_machine->GetTypeName(operand->m_type), m_machine->GetTypeName((operand + 1)->m_type));
  283. goto LabelException;
  284. }
  285. break;
  286. }
  287. case BC_GETIND :
  288. {
  289. operand = top - 2;
  290. --top;
  291. gmOperatorFunction op = OPERATOR(operand->m_type, (gmOperator) instruction32[-1]);
  292. if(op)
  293. {
  294. op(this, operand);
  295. }
  296. else if((fn = CALLOPERATOR(operand->m_type, (gmOperator) instruction32[-1])))
  297. {
  298. operand[2] = operand[0];
  299. operand[3] = operand[1];
  300. operand[0] = gmVariable(GM_NULL, 0);
  301. operand[1] = gmVariable(GM_FUNCTION, fn->GetRef());
  302. SetTop(operand + 4);
  303. State res = PushStackFrame(2, &instruction, &code);
  304. top = GetTop();
  305. base = GetBase();
  306. if(res == RUNNING) break;
  307. if(res == SYS_YIELD) return RUNNING;
  308. if(res == SYS_EXCEPTION) goto LabelException;
  309. if(res == KILLED) { m_machine->Sys_SwitchState(this, KILLED); GM_ASSERT(0); } // operator should not kill a thread
  310. return res;
  311. }
  312. else
  313. {
  314. GMTHREAD_LOG("operator %s undefined for type %s and %s", gmGetOperatorName((gmOperator) instruction32[-1]), m_machine->GetTypeName(operand->m_type), m_machine->GetTypeName((operand + 1)->m_type));
  315. goto LabelException;
  316. }
  317. break;
  318. }
  319. case BC_SETIND :
  320. {
  321. operand = top - 3;
  322. top -= 3;
  323. gmOperatorFunction op = OPERATOR(operand->m_type, O_SETIND);
  324. if(op)
  325. {
  326. op(this, operand);
  327. }
  328. else if((fn = CALLOPERATOR(operand->m_type, O_SETIND)))
  329. {
  330. operand[4] = operand[2];
  331. operand[3] = operand[1];
  332. operand[2] = operand[0];
  333. operand[0] = gmVariable(GM_NULL, 0);
  334. operand[1] = gmVariable(GM_FUNCTION, fn->GetRef());
  335. SetTop(operand + 5);
  336. State res = PushStackFrame(3, &instruction, &code);
  337. top = GetTop();
  338. base = GetBase();
  339. if(res == RUNNING) break;
  340. if(res == SYS_YIELD) return RUNNING;
  341. if(res == SYS_EXCEPTION) goto LabelException;
  342. if(res == KILLED) { m_machine->Sys_SwitchState(this, KILLED); GM_ASSERT(0); } // operator should not kill a thread
  343. return res;
  344. }
  345. else
  346. {
  347. GMTHREAD_LOG("setind failed.");
  348. goto LabelException;
  349. }
  350. break;
  351. }
  352. case BC_NOP :
  353. {
  354. break;
  355. }
  356. case BC_LINE :
  357. {
  358. #if GMDEBUG_SUPPORT
  359. if(m_machine->GetDebugMode() && m_machine->m_line)
  360. {
  361. SetTop(top);
  362. m_instruction = instruction;
  363. if(m_machine->m_line(this)) return RUNNING;
  364. }
  365. #endif // GMDEBUG_SUPPORT
  366. break;
  367. }
  368. case BC_GETDOT :
  369. {
  370. operand = top - 1;
  371. gmptr member = OPCODE_PTR(instruction);
  372. top->m_type = GM_STRING;
  373. top->m_value.m_ref = member;
  374. gmType t1 = operand->m_type;
  375. gmOperatorFunction op = OPERATOR(t1, O_GETDOT);
  376. if(op)
  377. {
  378. op(this, operand);
  379. if(operand->m_type) break;
  380. }
  381. if(t1 == GM_NULL)
  382. {
  383. GMTHREAD_LOG("getdot failed.");
  384. goto LabelException;
  385. }
  386. *operand = m_machine->GetTypeVariable(t1, gmVariable(GM_STRING, member));
  387. break;
  388. }
  389. case BC_SETDOT :
  390. {
  391. operand = top - 2;
  392. gmptr member = OPCODE_PTR(instruction);
  393. top->m_type = GM_STRING;
  394. top->m_value.m_ref = member;
  395. top -= 2;
  396. gmOperatorFunction op = OPERATOR(operand->m_type, O_SETDOT);
  397. if(op)
  398. {
  399. op(this, operand);
  400. }
  401. else
  402. {
  403. GMTHREAD_LOG("setdot failed.");
  404. goto LabelException;
  405. }
  406. break;
  407. }
  408. case BC_BRA :
  409. {
  410. instruction = code + OPCODE_PTR_NI(instruction);
  411. break;
  412. }
  413. case BC_BRZ :
  414. {
  415. #if GM_BOOL_OP
  416. operand = top - 1;
  417. --top;
  418. if (operand->m_type > GM_USER)
  419. {
  420. // Look for overridden operator.
  421. gmOperatorFunction op = OPERATOR(operand->m_type, O_BOOL);
  422. if (op)
  423. {
  424. op(this, operand);
  425. }
  426. else if ((fn = CALLOPERATOR(operand->m_type, O_BOOL)))
  427. {
  428. operand[2] = operand[0];
  429. operand[0] = gmVariable(GM_NULL, 0);
  430. operand[1] = gmVariable(GM_FUNCTION, fn->GetRef());
  431. SetTop(operand + 3);
  432. // Return to the same instruction after making the call but it will be testing the results of the call.
  433. --instruction32;
  434. State res = PushStackFrame(1, &instruction, &code);
  435. top = GetTop();
  436. base = GetBase();
  437. if(res == RUNNING) break;
  438. if(res == SYS_YIELD) return RUNNING;
  439. if(res == SYS_EXCEPTION) goto LabelException;
  440. if(res == KILLED) { m_machine->Sys_SwitchState(this, KILLED); GM_ASSERT(0); } // operator should not kill a thread
  441. return res;
  442. }
  443. }
  444. if(operand->m_value.m_int == 0)
  445. {
  446. instruction = code + OPCODE_PTR_NI(instruction);
  447. }
  448. else instruction += sizeof(gmptr);
  449. #else // !GM_BOOL_OP
  450. --top;
  451. if(top->m_value.m_int == 0)
  452. {
  453. instruction = code + OPCODE_PTR_NI(instruction);
  454. }
  455. else instruction += sizeof(gmptr);
  456. #endif // !GM_BOOL_OP
  457. break;
  458. }
  459. case BC_BRNZ :
  460. {
  461. #if GM_BOOL_OP
  462. operand = top - 1;
  463. --top;
  464. if (operand->m_type > GM_USER)
  465. {
  466. // Look for overridden operator.
  467. gmOperatorFunction op = OPERATOR(operand->m_type, O_BOOL);
  468. if (op)
  469. {
  470. op(this, operand);
  471. }
  472. else if ((fn = CALLOPERATOR(operand->m_type, O_BOOL)))
  473. {
  474. operand[2] = operand[0];
  475. operand[0] = gmVariable(GM_NULL, 0);
  476. operand[1] = gmVariable(GM_FUNCTION, fn->GetRef());
  477. SetTop(operand + 3);
  478. // Return to the same instruction after making the call but it will be testing the results of the call.
  479. --instruction32;
  480. State res = PushStackFrame(1, &instruction, &code);
  481. top = GetTop();
  482. base = GetBase();
  483. if(res == RUNNING) break;
  484. if(res == SYS_YIELD) return RUNNING;
  485. if(res == SYS_EXCEPTION) goto LabelException;
  486. if(res == KILLED) { m_machine->Sys_SwitchState(this, KILLED); GM_ASSERT(0); } // operator should not kill a thread
  487. return res;
  488. }
  489. }
  490. if(operand->m_value.m_int != 0)
  491. {
  492. instruction = code + OPCODE_PTR_NI(instruction);
  493. }
  494. else instruction += sizeof(gmptr);
  495. #else // !GM_BOOL_OP
  496. --top;
  497. if(top->m_value.m_int != 0)
  498. {
  499. instruction = code + OPCODE_PTR_NI(instruction);
  500. }
  501. else instruction += sizeof(gmptr);
  502. #endif // !GM_BOOL_OP
  503. break;
  504. }
  505. case BC_BRZK :
  506. {
  507. #if GM_BOOL_OP
  508. operand = top - 1;
  509. if (operand->m_type > GM_USER)
  510. {
  511. // Look for overridden operator.
  512. gmOperatorFunction op = OPERATOR(operand->m_type, O_BOOL);
  513. if (op)
  514. {
  515. op(this, operand);
  516. }
  517. else if ((fn = CALLOPERATOR(operand->m_type, O_BOOL)))
  518. {
  519. operand[2] = operand[0];
  520. operand[0] = gmVariable(GM_NULL, 0);
  521. operand[1] = gmVariable(GM_FUNCTION, fn->GetRef());
  522. SetTop(operand + 3);
  523. // Return to the same instruction after making the call but it will be testing the results of the call.
  524. --instruction32;
  525. State res = PushStackFrame(1, &instruction, &code);
  526. top = GetTop();
  527. base = GetBase();
  528. if(res == RUNNING) break;
  529. if(res == SYS_YIELD) return RUNNING;
  530. if(res == SYS_EXCEPTION) goto LabelException;
  531. if(res == KILLED) { m_machine->Sys_SwitchState(this, KILLED); GM_ASSERT(0); } // operator should not kill a thread
  532. return res;
  533. }
  534. }
  535. if(operand->m_value.m_int == 0)
  536. {
  537. instruction = code + OPCODE_PTR_NI(instruction);
  538. }
  539. else instruction += sizeof(gmptr);
  540. #else // !GM_BOOL_OP
  541. if(top[-1].m_value.m_int == 0)
  542. {
  543. instruction = code + OPCODE_PTR_NI(instruction);
  544. }
  545. else instruction += sizeof(gmptr);
  546. #endif // !GM_BOOL_OP
  547. break;
  548. }
  549. case BC_BRNZK :
  550. {
  551. #if GM_BOOL_OP
  552. operand = top - 1;
  553. if (operand->m_type > GM_USER)
  554. {
  555. // Look for overridden operator.
  556. gmOperatorFunction op = OPERATOR(operand->m_type, O_BOOL);
  557. if (op)
  558. {
  559. op(this, operand);
  560. }
  561. else if ((fn = CALLOPERATOR(operand->m_type, O_BOOL)))
  562. {
  563. operand[2] = operand[0];
  564. operand[0] = gmVariable(GM_NULL, 0);
  565. operand[1] = gmVariable(GM_FUNCTION, fn->GetRef());
  566. SetTop(operand + 3);
  567. // Return to the same instruction after making the call but it will be testing the results of the call.
  568. --instruction32;
  569. State res = PushStackFrame(1, &instruction, &code);
  570. top = GetTop();
  571. base = GetBase();
  572. if(res == RUNNING) break;
  573. if(res == SYS_YIELD) return RUNNING;
  574. if(res == SYS_EXCEPTION) goto LabelException;
  575. if(res == KILLED) { m_machine->Sys_SwitchState(this, KILLED); GM_ASSERT(0); } // operator should not kill a thread
  576. return res;
  577. }
  578. }
  579. if(operand->m_value.m_int != 0)
  580. {
  581. instruction = code + OPCODE_PTR_NI(instruction);
  582. }
  583. else instruction += sizeof(gmptr);
  584. #else // !GM_BOOL_OP
  585. if(top[-1].m_value.m_int != 0)
  586. {
  587. instruction = code + OPCODE_PTR_NI(instruction);
  588. }
  589. else instruction += sizeof(gmptr);
  590. #endif // !GM_BOOL_OP
  591. break;
  592. }
  593. case BC_CALL :
  594. {
  595. SetTop(top);
  596. int numParams = (int) OPCODE_INT(instruction);
  597. State res = PushStackFrame(numParams, &instruction, &code);
  598. top = GetTop();
  599. base = GetBase();
  600. if(res == RUNNING)
  601. {
  602. #if GMDEBUG_SUPPORT
  603. if(m_debugFlags && m_machine->GetDebugMode() && m_machine->m_call)
  604. {
  605. m_instruction = instruction;
  606. if(m_machine->m_call(this)) return RUNNING;
  607. }
  608. #endif // GMDEBUG_SUPPORT
  609. break;
  610. }
  611. if(res == SYS_YIELD) return RUNNING;
  612. if(res == SYS_EXCEPTION) goto LabelException;
  613. if(res == KILLED)
  614. {
  615. if(a_return) *a_return = m_stack[m_top - 1];
  616. m_machine->Sys_SwitchState(this, KILLED);
  617. }
  618. return res;
  619. }
  620. case BC_RET :
  621. {
  622. PUSHNULL;
  623. }
  624. case BC_RETV :
  625. {
  626. SetTop(top);
  627. int res = Sys_PopStackFrame(instruction, code);
  628. top = GetTop();
  629. base = GetBase();
  630. if(res == RUNNING)
  631. {
  632. #if GMDEBUG_SUPPORT
  633. if(m_debugFlags && m_machine->GetDebugMode() && m_machine->m_return)
  634. {
  635. m_instruction = instruction;
  636. if(m_machine->m_return(this)) return RUNNING;
  637. }
  638. #endif // GMDEBUG_SUPPORT
  639. break;
  640. }
  641. if(res == KILLED)
  642. {
  643. if(a_return) *a_return = *(top - 1);
  644. m_machine->Sys_SwitchState(this, KILLED);
  645. return KILLED;
  646. }
  647. if(res == SYS_EXCEPTION) goto LabelException;
  648. break;
  649. }
  650. #if GM_USE_FORK
  651. // duplicates the current thread and just the local stack frame
  652. // and branches around the forked section of code
  653. case BC_FORK :
  654. {
  655. int id;
  656. gmThread* newthr = GetMachine()->CreateThread(&id);
  657. GM_ASSERT( newthr );
  658. // make sure there is enough room
  659. newthr->Touch( m_size - m_base + 2 );
  660. // copy stack and vars
  661. memcpy( newthr->m_stack, &m_stack[ m_base - 2 ], sizeof( gmVariable ) * (m_top - m_base + 2 ) );
  662. newthr->m_top = m_top - m_base + 2;
  663. newthr->m_frame = m_machine->Sys_AllocStackFrame();
  664. newthr->m_frame->m_prev = 0;
  665. newthr->m_frame->m_returnAddress = 0;
  666. newthr->m_frame->m_returnBase = 0;
  667. newthr->m_base = 2;
  668. newthr->m_instruction = instruction + sizeof(gmptr); // skip branch on other thread
  669. newthr->PushInt( GetId() );
  670. instruction = code + OPCODE_PTR_NI( instruction ); // branch
  671. top->m_type = GM_INT;
  672. top->m_value.m_int = newthr->GetId();
  673. ++top;
  674. break;
  675. }
  676. #endif //GM_USE_FORK
  677. case BC_FOREACH :
  678. {
  679. gmuint32 localvalue = OPCODE_INT(instruction);
  680. gmuint32 localkey = localvalue >> 16;
  681. localvalue &= 0xffff;
  682. // iterator is at tos-1, table is at tos -2, push int 1 if continuing loop. write key and value into localkey and localvalue
  683. if(top[-2].m_type != GM_TABLE)
  684. {
  685. #if GM_USER_FOREACH
  686. gmTypeIteratorCallback itrfunc = m_machine->GetUserTypeIteratorCallback(top[-2].m_type);
  687. if (!itrfunc)
  688. {
  689. GMTHREAD_LOG("foreach expression has no iterator function");
  690. goto LabelException;
  691. }
  692. gmTypeIterator it = (gmTypeIterator) top[-1].m_value.m_int;
  693. gmUserObject *obj = (gmUserObject*)GM_MOBJECT(m_machine, top[-2].m_value.m_ref);
  694. // Do callback for getnext
  695. gmVariable localvar;
  696. gmVariable localkeyvar;
  697. itrfunc(this, obj, it, &localkeyvar, &localvar);
  698. if (it != GM_TYPE_ITR_NULL)
  699. {
  700. base[localkey] = localkeyvar;
  701. base[localvalue] = localvar;
  702. top->m_type = GM_INT; top->m_value.m_int = 1;
  703. }
  704. else
  705. {
  706. top->m_type = GM_INT; top->m_value.m_int = 0;
  707. }
  708. top[-1].m_value.m_int = it;
  709. ++top;
  710. #else //GM_USER_FOREACH (original)
  711. GMTHREAD_LOG("foreach expression is not table type");
  712. goto LabelException;
  713. #endif //GM_USER_FOREACH
  714. }
  715. else
  716. {
  717. GM_ASSERT(top[-1].m_type == GM_INT);
  718. gmTableIterator it = (gmTableIterator) top[-1].m_value.m_int;
  719. gmTableObject * table = (gmTableObject *) GM_MOBJECT(m_machine, top[-2].m_value.m_ref);
  720. gmTableNode * node = table->GetNext(it);
  721. top[-1].m_value.m_int = it;
  722. if(node)
  723. {
  724. base[localkey] = node->m_key;
  725. base[localvalue] = node->m_value;
  726. top->m_type = GM_INT; top->m_value.m_int = 1;
  727. }
  728. else
  729. {
  730. top->m_type = GM_INT; top->m_value.m_int = 0;
  731. }
  732. ++top;
  733. }
  734. break;
  735. }
  736. case BC_POP :
  737. {
  738. --top;
  739. break;
  740. }
  741. case BC_POP2 :
  742. {
  743. top -= 2;
  744. break;
  745. }
  746. case BC_DUP :
  747. {
  748. top[0] = top[-1];
  749. ++top;
  750. break;
  751. }
  752. case BC_DUP2 :
  753. {
  754. top[0] = top[-2];
  755. top[1] = top[-1];
  756. top += 2;
  757. break;
  758. }
  759. case BC_SWAP :
  760. {
  761. top[0] = top[-1];
  762. top[-1] = top[-2];
  763. top[-2] = top[0];
  764. break;
  765. }
  766. case BC_PUSHNULL :
  767. {
  768. PUSHNULL;
  769. break;
  770. }
  771. case BC_PUSHINT :
  772. {
  773. top->m_type = GM_INT;
  774. top->m_value.m_int = OPCODE_INT(instruction);
  775. ++top;
  776. break;
  777. }
  778. case BC_PUSHINT0 :
  779. {
  780. top->m_type = GM_INT;
  781. top->m_value.m_int = 0;
  782. ++top;
  783. break;
  784. }
  785. case BC_PUSHINT1 :
  786. {
  787. top->m_type = GM_INT;
  788. top->m_value.m_int = 1;
  789. ++top;
  790. break;
  791. }
  792. case BC_PUSHFP :
  793. {
  794. top->m_type = GM_FLOAT;
  795. top->m_value.m_float = OPCODE_FLOAT(instruction);
  796. ++top;
  797. break;
  798. }
  799. case BC_PUSHSTR :
  800. {
  801. top->m_type = GM_STRING;
  802. top->m_value.m_ref = OPCODE_PTR(instruction);
  803. ++top;
  804. break;
  805. }
  806. case BC_PUSHTBL :
  807. {
  808. SetTop(top);
  809. top->m_type = GM_TABLE;
  810. top->m_value.m_ref = m_machine->AllocTableObject()->GetRef();
  811. ++top;
  812. break;
  813. }
  814. case BC_PUSHFN :
  815. {
  816. top->m_type = GM_FUNCTION;
  817. top->m_value.m_ref = OPCODE_PTR(instruction);
  818. ++top;
  819. break;
  820. }
  821. case BC_PUSHTHIS :
  822. {
  823. *top = *GetThis();
  824. ++top;
  825. break;
  826. }
  827. case BC_GETLOCAL :
  828. {
  829. gmuint32 offset = OPCODE_INT(instruction);
  830. *(top++) = base[offset];
  831. break;
  832. }
  833. case BC_SETLOCAL :
  834. {
  835. gmuint32 offset = OPCODE_INT(instruction);
  836. // Write barrier old local objects
  837. {
  838. gmGarbageCollector* gc = m_machine->GetGC();
  839. if( !gc->IsOff() && base[offset].IsReference() )
  840. {
  841. gmObject * object = GM_MOBJECT(m_machine, base[offset].m_value.m_ref);
  842. gc->WriteBarrier(object);
  843. }
  844. }
  845. base[offset] = *(--top);
  846. break;
  847. }
  848. case BC_GETGLOBAL :
  849. {
  850. top->m_type = GM_STRING;
  851. top->m_value.m_ref = OPCODE_PTR(instruction);
  852. *top = m_machine->GetGlobals()->Get(*top); ++top;
  853. break;
  854. }
  855. case BC_SETGLOBAL :
  856. {
  857. top->m_type = GM_STRING;
  858. top->m_value.m_ref = OPCODE_PTR(instruction);
  859. m_machine->GetGlobals()->Set(m_machine, *top, *(top-1)); --top;
  860. break;
  861. }
  862. case BC_GETTHIS :
  863. {
  864. gmptr member = OPCODE_PTR(instruction);
  865. const gmVariable * thisVar = GetThis();
  866. *top = *thisVar;
  867. top[1].m_type = GM_STRING;
  868. top[1].m_value.m_ref = member;
  869. gmOperatorFunction op = OPERATOR(thisVar->m_type, O_GETDOT);
  870. if(op)
  871. {
  872. op(this, top);
  873. if(top->m_type) { ++top; break; }
  874. }
  875. if(thisVar->m_type == GM_NULL)
  876. {
  877. GMTHREAD_LOG("getthis failed. this is null");
  878. goto LabelException;
  879. }
  880. *top = m_machine->GetTypeVariable(thisVar->m_type, top[1]);
  881. ++top;
  882. break;
  883. }
  884. case BC_SETTHIS :
  885. {
  886. gmptr member = OPCODE_PTR(instruction);
  887. const gmVariable * thisVar = GetThis();
  888. operand = top - 1;
  889. *top = *operand;
  890. *operand = *thisVar;
  891. top[1].m_type = GM_STRING;
  892. top[1].m_value.m_ref = member;
  893. --top;
  894. gmOperatorFunction op = OPERATOR(thisVar->m_type, O_SETDOT);
  895. if(op)
  896. {
  897. op(this, operand);
  898. }
  899. else
  900. {
  901. GMTHREAD_LOG("setthis failed.");
  902. goto LabelException;
  903. }
  904. break;
  905. }
  906. default :
  907. {
  908. break;
  909. }
  910. }
  911. }
  912. LabelException:
  913. //
  914. // exception handler
  915. //
  916. m_instruction = instruction;
  917. // spit out error info
  918. LogLineFile();
  919. LogCallStack();
  920. // call machine exception handler
  921. if(gmMachine::s_machineCallback)
  922. {
  923. if(gmMachine::s_machineCallback(m_machine, MC_THREAD_EXCEPTION, this))
  924. {
  925. #if GMDEBUG_SUPPORT
  926. // if we are being debugged, put this thread into a limbo state, waiting for delete.
  927. if(m_machine->GetDebugMode() && m_machine->m_debugUser)
  928. {
  929. m_machine->Sys_SwitchState(this, EXCEPTION);
  930. return EXCEPTION;
  931. }
  932. #endif
  933. }
  934. }
  935. // kill the thread
  936. m_machine->Sys_SwitchState(this, KILLED);
  937. return KILLED;
  938. }
  939. void gmThread::Sys_Reset(int a_id)
  940. {
  941. m_machine->Sys_RemoveBlocks(this);
  942. m_machine->Sys_RemoveSignals(this);
  943. gmStackFrame * frame;
  944. while(m_frame)
  945. {
  946. frame = m_frame->m_prev;
  947. m_machine->Sys_FreeStackFrame(m_frame);
  948. m_frame = frame;
  949. }
  950. m_top = 0;
  951. m_base = 0;
  952. m_instruction = NULL;
  953. m_timeStamp = 0;
  954. m_startTime = 0;
  955. m_id = a_id;
  956. m_numParameters = 0;
  957. m_user = 0;
  958. }
  959. gmThread::State gmThread::PushStackFrame(int a_numParameters, const gmuint8 ** a_ip, const gmuint8 ** a_cp)
  960. {
  961. // calculate new stack base
  962. int base = m_top - a_numParameters;
  963. if( base == 2 ) // When initial thread function is ready, signal thread creation
  964. {
  965. // This may not be the best place to signal, but we at least want a valid 'this'
  966. m_base = base; // Init so some thread queries work
  967. m_machine->Sys_SignalCreateThread(this);
  968. }
  969. gmVariable * fnVar = &m_stack[base - 1];
  970. if(fnVar->m_type != GM_FUNCTION)
  971. {
  972. m_machine->GetLog().LogEntry("attempt to call non function type");
  973. return SYS_EXCEPTION;
  974. }
  975. gmFunctionObject * fn = (gmFunctionObject *) GM_MOBJECT(m_machine, fnVar->m_value.m_ref);
  976. if(fn->m_cFunction)
  977. {
  978. //
  979. // Its a native function call, call it now as we cannot stack wind natives. this avoids
  980. // pushing a gmStackFrame.
  981. //
  982. m_numParameters = (short) a_numParameters;
  983. int lastBase = m_base;
  984. int lastTop = m_top;
  985. m_base = base;
  986. int result = fn->m_cFunction(this);
  987. // Write barrier old local objects at native pop time
  988. {
  989. gmGarbageCollector* gc = m_machine->GetGC();
  990. if( !gc->IsOff() )
  991. {
  992. for(int index = m_base; index < m_top; ++index)
  993. {
  994. if(m_stack[index].IsReference())
  995. {
  996. gmObject * object = GM_MOBJECT(m_machine, m_stack[index].m_value.m_ref);
  997. gc->WriteBarrier(object);
  998. }
  999. }
  1000. }
  1001. }
  1002. // handle state
  1003. if(result == GM_SYS_STATE)
  1004. {
  1005. // this is special case, a bit messy.
  1006. return PushStackFrame(a_numParameters - GM_STATE_NUM_PARAMS, a_ip, a_cp);
  1007. }
  1008. // NOTE: It is not currently safe for a C binding to kill this thread.
  1009. // Since we cant unwind mixed script and native functions anyway,
  1010. // perhaps the safest thing would be for ALWAYS delay killed threads
  1011. // from deletion.
  1012. // push a null if the function did not return anything
  1013. if(lastTop == m_top)
  1014. {
  1015. m_stack[m_base - 2] = gmVariable(GM_NULL, 0);
  1016. }
  1017. else
  1018. {
  1019. m_stack[m_base - 2] = m_stack[m_top - 1];
  1020. }
  1021. // Restore the stack
  1022. m_top = m_base - 1;
  1023. m_base = lastBase;
  1024. // check the call result
  1025. if(result != GM_OK)
  1026. {
  1027. const gmuint8 * returnAddress = (a_ip) ? *a_ip : NULL;
  1028. if(result == GM_SYS_YIELD)
  1029. {
  1030. m_machine->Sys_RemoveSignals(this);
  1031. m_instruction = returnAddress;
  1032. return SYS_YIELD;
  1033. }
  1034. else if(result == GM_SYS_BLOCK)
  1035. {
  1036. m_instruction = returnAddress;
  1037. m_machine->Sys_SwitchState(this, BLOCKED);
  1038. return BLOCKED;
  1039. }
  1040. else if(result == GM_SYS_SLEEP)
  1041. {
  1042. m_instruction = returnAddress;
  1043. m_machine->Sys_SwitchState(this, SLEEPING);
  1044. return SLEEPING;
  1045. }
  1046. else if(result == GM_SYS_KILL)
  1047. {
  1048. return KILLED;
  1049. }
  1050. return SYS_EXCEPTION;
  1051. }
  1052. if(!m_frame) // C called C function, no stack frame, so signal killed.
  1053. {
  1054. return KILLED;
  1055. }
  1056. // return result
  1057. return RUNNING;
  1058. }
  1059. //
  1060. // Its a script function call, push a stack frame
  1061. //
  1062. int clearSize = fn->GetNumParamsLocals() - a_numParameters;
  1063. if(!Touch(clearSize + fn->GetMaxStackSize()))
  1064. {
  1065. m_machine->GetLog().LogEntry("stack overflow");
  1066. return SYS_EXCEPTION;
  1067. }
  1068. // zero missing params and locals.
  1069. if(a_numParameters <= fn->GetNumParams())
  1070. {
  1071. memset(GetTop(), 0, sizeof(gmVariable) * clearSize);
  1072. }
  1073. else
  1074. {
  1075. memset(&m_stack[base + fn->GetNumParams()], 0, sizeof(gmVariable) * fn->GetNumLocals());
  1076. }
  1077. // push a new stack frame
  1078. gmStackFrame * frame = m_machine->Sys_AllocStackFrame();
  1079. frame->m_prev = m_frame;
  1080. m_frame = frame;
  1081. // cache new frame variables
  1082. m_frame->m_returnBase = m_base;
  1083. if(a_ip)
  1084. {
  1085. m_frame->m_returnAddress = *a_ip;
  1086. *a_ip = (const gmuint8 *) fn->GetByteCode();
  1087. *a_cp = *a_ip;
  1088. }
  1089. else
  1090. {
  1091. m_frame->m_returnAddress = NULL;
  1092. }
  1093. m_base = base;
  1094. m_top = base + fn->GetNumParamsLocals();
  1095. return RUNNING;
  1096. }
  1097. gmThread::State gmThread::Sys_PopStackFrame(const gmuint8 * &a_ip, const gmuint8 * &a_cp)
  1098. {
  1099. if(m_frame == NULL)
  1100. {
  1101. m_machine->GetLog().LogEntry("stack underflow");
  1102. return SYS_EXCEPTION;
  1103. }
  1104. // Write barrier old local objects
  1105. {
  1106. gmGarbageCollector* gc = m_machine->GetGC();
  1107. if( !gc->IsOff() )
  1108. {
  1109. for(int index = m_base-2; index < m_top; ++index)
  1110. {
  1111. if(m_stack[index].IsReference())
  1112. {
  1113. gmObject * object = GM_MOBJECT(m_machine, m_stack[index].m_value.m_ref);
  1114. gc->WriteBarrier(object);
  1115. }
  1116. }
  1117. }
  1118. }
  1119. gmStackFrame * frame = m_frame->m_prev;
  1120. if( frame == NULL ) // Final frame, we will exit now
  1121. {
  1122. return KILLED; // Don't clean up stack, let the machine reset it as state changes to killed (so Exit callback can examine valid thread contents)
  1123. }
  1124. a_ip = m_frame->m_returnAddress;
  1125. // Copy old tos to new tos
  1126. m_stack[m_base - 2] = m_stack[m_top - 1];
  1127. m_top = m_base - 1;
  1128. m_base = m_frame->m_returnBase;
  1129. m_machine->Sys_FreeStackFrame(m_frame);
  1130. m_frame = frame;
  1131. // Update instruction and code pointers
  1132. GM_ASSERT(GetFunction()->m_type == GM_FUNCTION);
  1133. gmFunctionObject * fn = (gmFunctionObject *) GM_MOBJECT(m_machine, GetFunction()->m_value.m_ref);
  1134. a_cp = (const gmuint8 *) fn->GetByteCode();
  1135. return RUNNING;
  1136. }
  1137. void gmThread::LogLineFile()
  1138. {
  1139. // spit out the source and line info for the exception.
  1140. if(m_base >= 2 && GetFunction()->m_type == GM_FUNCTION)
  1141. {
  1142. gmFunctionObject * fn = (gmFunctionObject *) GM_MOBJECT(m_machine, GetFunction()->m_value.m_ref);
  1143. if(fn)
  1144. {
  1145. int line = fn->GetLine(m_instruction);
  1146. gmuint32 id = fn->GetSourceId();
  1147. const char * source, * filename;
  1148. if(m_machine->GetSourceCode(id, source, filename))
  1149. {
  1150. char buffer[80];
  1151. gmGetLineFromString(source, line, buffer, 80);
  1152. m_machine->GetLog().LogEntry(GM_NL"%s(%d) : %s", filename, line, buffer);
  1153. }
  1154. else
  1155. {
  1156. m_machine->GetLog().LogEntry(GM_NL"unknown(%d) : ", line);
  1157. }
  1158. }
  1159. }
  1160. }
  1161. bool gmThread::Touch(int a_extra)
  1162. {
  1163. // Grow stack if necessary. NOTE: Use better growth metric if needed.
  1164. bool reAlloc = false;
  1165. while((m_top + a_extra + GMTHREAD_SLACKSPACE) >= m_size)
  1166. {
  1167. if(sizeof(gmVariable) * m_size > GMTHREAD_MAXBYTESIZE)
  1168. {
  1169. GM_ASSERT(!"GMTHREAD_MAXBYTESIZE exceeded");
  1170. return false;
  1171. }
  1172. m_size *= 2;
  1173. reAlloc = true;
  1174. }
  1175. if(reAlloc)
  1176. {
  1177. gmVariable * stack = new gmVariable[m_size];
  1178. //memset(stack, 0, sizeof(gmVariable) * m_size);
  1179. memcpy(stack, m_stack, m_top * sizeof(gmVariable));
  1180. if(m_stack)
  1181. delete[] m_stack;
  1182. m_stack = stack;
  1183. }
  1184. return true;
  1185. }
  1186. void gmThread::LogCallStack()
  1187. {
  1188. m_machine->GetLog().LogEntry(GM_NL"callstack..");
  1189. gmStackFrame * frame = m_frame;
  1190. int base = m_base;
  1191. const gmuint8 * ip = m_instruction;
  1192. while(frame)
  1193. {
  1194. // get the function object
  1195. gmVariable * fnVar = &m_stack[base - 1];
  1196. if(fnVar->m_type == GM_FUNCTION)
  1197. {
  1198. gmFunctionObject * fn = (gmFunctionObject *) GM_MOBJECT(m_machine, fnVar->m_value.m_ref);
  1199. m_machine->GetLog().LogEntry("%3d: %s", fn->GetLine(ip), fn->GetDebugName());
  1200. }
  1201. base = frame->m_returnBase;
  1202. ip = frame->m_returnAddress;
  1203. frame = frame->m_prev;
  1204. }
  1205. m_machine->GetLog().LogEntry("");
  1206. }