as_bytecode.cpp 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2012 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_bytecode.cpp
  25. //
  26. // A class for constructing the final byte code
  27. //
  28. #include <stdio.h> // fopen(), fprintf(), fclose()
  29. #include "as_config.h"
  30. #ifndef AS_NO_COMPILER
  31. #include "as_bytecode.h"
  32. #include "as_debug.h" // mkdir()
  33. #include "as_array.h"
  34. #include "as_string.h"
  35. #include "as_scriptengine.h"
  36. BEGIN_AS_NAMESPACE
  37. asCByteCode::asCByteCode(asCScriptEngine *engine)
  38. {
  39. first = 0;
  40. last = 0;
  41. largestStackUsed = -1;
  42. this->engine = engine;
  43. }
  44. asCByteCode::~asCByteCode()
  45. {
  46. ClearAll();
  47. }
  48. void asCByteCode::Finalize()
  49. {
  50. // verify the bytecode
  51. PostProcess();
  52. // Optimize the code (optionally)
  53. if( engine->ep.optimizeByteCode )
  54. Optimize();
  55. // Resolve jumps
  56. ResolveJumpAddresses();
  57. // Build line numbers buffer
  58. ExtractLineNumbers();
  59. }
  60. void asCByteCode::ClearAll()
  61. {
  62. cByteInstruction *del = first;
  63. while( del )
  64. {
  65. first = del->next;
  66. engine->memoryMgr.FreeByteInstruction(del);
  67. del = first;
  68. }
  69. first = 0;
  70. last = 0;
  71. lineNumbers.SetLength(0);
  72. largestStackUsed = -1;
  73. temporaryVariables.SetLength(0);
  74. }
  75. void asCByteCode::InsertIfNotExists(asCArray<int> &vars, int var)
  76. {
  77. if( !vars.Exists(var) )
  78. vars.PushLast(var);
  79. }
  80. void asCByteCode::GetVarsUsed(asCArray<int> &vars)
  81. {
  82. cByteInstruction *curr = first;
  83. while( curr )
  84. {
  85. if( asBCInfo[curr->op].type == asBCTYPE_wW_rW_rW_ARG )
  86. {
  87. InsertIfNotExists(vars, curr->wArg[0]);
  88. InsertIfNotExists(vars, curr->wArg[1]);
  89. InsertIfNotExists(vars, curr->wArg[2]);
  90. }
  91. else if( asBCInfo[curr->op].type == asBCTYPE_rW_ARG ||
  92. asBCInfo[curr->op].type == asBCTYPE_wW_ARG ||
  93. asBCInfo[curr->op].type == asBCTYPE_wW_W_ARG ||
  94. asBCInfo[curr->op].type == asBCTYPE_rW_DW_ARG ||
  95. asBCInfo[curr->op].type == asBCTYPE_wW_DW_ARG ||
  96. asBCInfo[curr->op].type == asBCTYPE_wW_QW_ARG ||
  97. asBCInfo[curr->op].type == asBCTYPE_rW_W_DW_ARG )
  98. {
  99. InsertIfNotExists(vars, curr->wArg[0]);
  100. }
  101. else if( asBCInfo[curr->op].type == asBCTYPE_wW_rW_ARG ||
  102. asBCInfo[curr->op].type == asBCTYPE_rW_rW_ARG ||
  103. asBCInfo[curr->op].type == asBCTYPE_wW_rW_DW_ARG )
  104. {
  105. InsertIfNotExists(vars, curr->wArg[0]);
  106. InsertIfNotExists(vars, curr->wArg[1]);
  107. }
  108. else if( curr->op == asBC_LoadThisR )
  109. {
  110. InsertIfNotExists(vars, 0);
  111. }
  112. curr = curr->next;
  113. }
  114. }
  115. bool asCByteCode::IsVarUsed(int offset)
  116. {
  117. cByteInstruction *curr = first;
  118. while( curr )
  119. {
  120. // Verify all ops that use variables
  121. if( asBCInfo[curr->op].type == asBCTYPE_wW_rW_rW_ARG )
  122. {
  123. if( curr->wArg[0] == offset || curr->wArg[1] == offset || curr->wArg[2] == offset )
  124. return true;
  125. }
  126. else if( asBCInfo[curr->op].type == asBCTYPE_rW_ARG ||
  127. asBCInfo[curr->op].type == asBCTYPE_wW_ARG ||
  128. asBCInfo[curr->op].type == asBCTYPE_wW_W_ARG ||
  129. asBCInfo[curr->op].type == asBCTYPE_rW_DW_ARG ||
  130. asBCInfo[curr->op].type == asBCTYPE_wW_DW_ARG ||
  131. asBCInfo[curr->op].type == asBCTYPE_wW_QW_ARG ||
  132. asBCInfo[curr->op].type == asBCTYPE_rW_W_DW_ARG )
  133. {
  134. if( curr->wArg[0] == offset )
  135. return true;
  136. }
  137. else if( asBCInfo[curr->op].type == asBCTYPE_wW_rW_ARG ||
  138. asBCInfo[curr->op].type == asBCTYPE_rW_rW_ARG ||
  139. asBCInfo[curr->op].type == asBCTYPE_wW_rW_DW_ARG )
  140. {
  141. if( curr->wArg[0] == offset || curr->wArg[1] == offset )
  142. return true;
  143. }
  144. else if( curr->op == asBC_LoadThisR )
  145. {
  146. if( offset == 0 )
  147. return true;
  148. }
  149. curr = curr->next;
  150. }
  151. return false;
  152. }
  153. void asCByteCode::ExchangeVar(int oldOffset, int newOffset)
  154. {
  155. asASSERT(oldOffset != 0);
  156. cByteInstruction *curr = first;
  157. while( curr )
  158. {
  159. // Verify all ops that use variables
  160. if( asBCInfo[curr->op].type == asBCTYPE_wW_rW_rW_ARG )
  161. {
  162. if( curr->wArg[0] == oldOffset )
  163. curr->wArg[0] = (short)newOffset;
  164. if( curr->wArg[1] == oldOffset )
  165. curr->wArg[1] = (short)newOffset;
  166. if( curr->wArg[2] == oldOffset )
  167. curr->wArg[2] = (short)newOffset;
  168. }
  169. else if( asBCInfo[curr->op].type == asBCTYPE_rW_ARG ||
  170. asBCInfo[curr->op].type == asBCTYPE_wW_ARG ||
  171. asBCInfo[curr->op].type == asBCTYPE_wW_W_ARG ||
  172. asBCInfo[curr->op].type == asBCTYPE_rW_DW_ARG ||
  173. asBCInfo[curr->op].type == asBCTYPE_wW_DW_ARG ||
  174. asBCInfo[curr->op].type == asBCTYPE_wW_QW_ARG )
  175. {
  176. if( curr->wArg[0] == oldOffset )
  177. curr->wArg[0] = (short)newOffset;
  178. }
  179. else if( asBCInfo[curr->op].type == asBCTYPE_wW_rW_ARG ||
  180. asBCInfo[curr->op].type == asBCTYPE_rW_rW_ARG )
  181. {
  182. if( curr->wArg[0] == oldOffset )
  183. curr->wArg[0] = (short)newOffset;
  184. if( curr->wArg[1] == oldOffset )
  185. curr->wArg[1] = (short)newOffset;
  186. }
  187. curr = curr->next;
  188. }
  189. }
  190. void asCByteCode::AddPath(asCArray<cByteInstruction *> &paths, cByteInstruction *instr, int stackSize)
  191. {
  192. if( instr->marked )
  193. {
  194. // Verify the size of the stack
  195. asASSERT(instr->stackSize == stackSize);
  196. }
  197. else
  198. {
  199. // Add the destination to the code paths
  200. instr->marked = true;
  201. instr->stackSize = stackSize;
  202. paths.PushLast(instr);
  203. }
  204. }
  205. bool asCByteCode::IsCombination(cByteInstruction *curr, asEBCInstr bc1, asEBCInstr bc2)
  206. {
  207. if( curr->op == bc1 && curr->next && curr->next->op == bc2 )
  208. return true;
  209. return false;
  210. }
  211. bool asCByteCode::IsCombination(cByteInstruction *curr, asEBCInstr bc1, asEBCInstr bc2, asEBCInstr bc3)
  212. {
  213. if( curr->op == bc1 &&
  214. curr->next && curr->next->op == bc2 &&
  215. curr->next->next && curr->next->next->op == bc3 )
  216. return true;
  217. return false;
  218. }
  219. cByteInstruction *asCByteCode::ChangeFirstDeleteNext(cByteInstruction *curr, asEBCInstr bc)
  220. {
  221. curr->op = bc;
  222. if( curr->next ) DeleteInstruction(curr->next);
  223. // Continue optimization with the instruction before the altered one
  224. if( curr->prev )
  225. return curr->prev;
  226. else
  227. return curr;
  228. }
  229. cByteInstruction *asCByteCode::DeleteFirstChangeNext(cByteInstruction *curr, asEBCInstr bc)
  230. {
  231. asASSERT( curr->next );
  232. cByteInstruction *instr = curr->next;
  233. instr->op = bc;
  234. DeleteInstruction(curr);
  235. // Continue optimization with the instruction before the altered one
  236. if( instr->prev )
  237. return instr->prev;
  238. else
  239. return instr;
  240. }
  241. void asCByteCode::InsertBefore(cByteInstruction *before, cByteInstruction *instr)
  242. {
  243. asASSERT(instr->next == 0);
  244. asASSERT(instr->prev == 0);
  245. if( before->prev ) before->prev->next = instr;
  246. instr->prev = before->prev;
  247. before->prev = instr;
  248. instr->next = before;
  249. if( first == before ) first = instr;
  250. }
  251. void asCByteCode::RemoveInstruction(cByteInstruction *instr)
  252. {
  253. if( instr == first ) first = first->next;
  254. if( instr == last ) last = last->prev;
  255. if( instr->prev ) instr->prev->next = instr->next;
  256. if( instr->next ) instr->next->prev = instr->prev;
  257. instr->next = 0;
  258. instr->prev = 0;
  259. }
  260. bool asCByteCode::CanBeSwapped(cByteInstruction *curr)
  261. {
  262. if( !curr || !curr->next || !curr->next->next ) return false;
  263. if( curr->next->next->op != asBC_SwapPtr ) return false;
  264. cByteInstruction *next = curr->next;
  265. if( curr->op != asBC_PshNull &&
  266. curr->op != asBC_PshVPtr &&
  267. curr->op != asBC_PSF )
  268. return false;
  269. if( next->op != asBC_PshNull &&
  270. next->op != asBC_PshVPtr &&
  271. next->op != asBC_PSF )
  272. return false;
  273. return true;
  274. }
  275. cByteInstruction *asCByteCode::GoBack(cByteInstruction *curr)
  276. {
  277. // Go back 2 instructions
  278. if( !curr ) return 0;
  279. if( curr->prev ) curr = curr->prev;
  280. if( curr->prev ) curr = curr->prev;
  281. return curr;
  282. }
  283. bool asCByteCode::PostponeInitOfTemp(cByteInstruction *curr, cByteInstruction **next)
  284. {
  285. // This is not done for pointers
  286. if( (curr->op != asBC_SetV4 && curr->op != asBC_SetV8) ||
  287. !IsTemporary(curr->wArg[0]) ) return false;
  288. // Move the initialization to just before it's use.
  289. // Don't move it beyond any labels or jumps.
  290. cByteInstruction *use = curr->next;
  291. while( use )
  292. {
  293. if( IsTempVarReadByInstr(use, curr->wArg[0]) )
  294. break;
  295. if( IsTempVarOverwrittenByInstr(use, curr->wArg[0]) )
  296. return false;
  297. if( IsInstrJmpOrLabel(use) )
  298. return false;
  299. use = use->next;
  300. }
  301. if( use && use->prev != curr )
  302. {
  303. *next = curr->next;
  304. // Move the instruction
  305. RemoveInstruction(curr);
  306. InsertBefore(use, curr);
  307. // Try a RemoveUnusedValue to see if it can be combined with the other
  308. cByteInstruction *temp;
  309. if( RemoveUnusedValue(curr, &temp) )
  310. {
  311. *next = GoBack(*next);
  312. return true;
  313. }
  314. // Return the instructions to its original position as it wasn't useful
  315. RemoveInstruction(curr);
  316. InsertBefore(*next, curr);
  317. }
  318. return false;
  319. }
  320. bool asCByteCode::RemoveUnusedValue(cByteInstruction *curr, cByteInstruction **next)
  321. {
  322. // TODO: runtime optimize: Should work for 64bit types as well
  323. // The value isn't used for anything
  324. if( (asBCInfo[curr->op].type == asBCTYPE_wW_rW_rW_ARG ||
  325. asBCInfo[curr->op].type == asBCTYPE_wW_rW_ARG ||
  326. asBCInfo[curr->op].type == asBCTYPE_wW_rW_DW_ARG ||
  327. asBCInfo[curr->op].type == asBCTYPE_wW_ARG ||
  328. asBCInfo[curr->op].type == asBCTYPE_wW_DW_ARG ||
  329. asBCInfo[curr->op].type == asBCTYPE_wW_QW_ARG) &&
  330. IsTemporary(curr->wArg[0]) &&
  331. !IsTempVarRead(curr, curr->wArg[0]) &&
  332. curr->op != asBC_FREE ) // Can't remove the FREE instruction
  333. {
  334. if( curr->op == asBC_LdGRdR4 && IsTempRegUsed(curr) )
  335. {
  336. curr->op = asBC_LDG;
  337. *next = GoBack(curr);
  338. return true;
  339. }
  340. *next = GoBack(DeleteInstruction(curr));
  341. return true;
  342. }
  343. // TODO: runtime optimize: There should be one for doubles as well
  344. // The value is immediately used and then never again
  345. if( curr->op == asBC_SetV4 &&
  346. curr->next &&
  347. (curr->next->op == asBC_CMPi ||
  348. curr->next->op == asBC_CMPf ||
  349. curr->next->op == asBC_CMPu) &&
  350. curr->wArg[0] == curr->next->wArg[1] &&
  351. (IsTemporary(curr->wArg[0]) && // The variable is temporary and never used again
  352. !IsTempVarRead(curr->next, curr->wArg[0])) )
  353. {
  354. if( curr->next->op == asBC_CMPi ) curr->next->op = asBC_CMPIi;
  355. else if( curr->next->op == asBC_CMPf ) curr->next->op = asBC_CMPIf;
  356. else if( curr->next->op == asBC_CMPu ) curr->next->op = asBC_CMPIu;
  357. curr->next->size = asBCTypeSize[asBCInfo[asBC_CMPIi].type];
  358. curr->next->arg = curr->arg;
  359. *next = GoBack(DeleteInstruction(curr));
  360. return true;
  361. }
  362. // The value is immediately used and then never again
  363. if( curr->op == asBC_SetV4 &&
  364. curr->next &&
  365. (curr->next->op == asBC_ADDi ||
  366. curr->next->op == asBC_SUBi ||
  367. curr->next->op == asBC_MULi ||
  368. curr->next->op == asBC_ADDf ||
  369. curr->next->op == asBC_SUBf ||
  370. curr->next->op == asBC_MULf) &&
  371. curr->wArg[0] == curr->next->wArg[2] &&
  372. (curr->next->wArg[0] == curr->wArg[0] || // The variable is overwritten
  373. (IsTemporary(curr->wArg[0]) && // The variable is temporary and never used again
  374. !IsTempVarRead(curr->next, curr->wArg[0]))) )
  375. {
  376. if( curr->next->op == asBC_ADDi ) curr->next->op = asBC_ADDIi;
  377. else if( curr->next->op == asBC_SUBi ) curr->next->op = asBC_SUBIi;
  378. else if( curr->next->op == asBC_MULi ) curr->next->op = asBC_MULIi;
  379. else if( curr->next->op == asBC_ADDf ) curr->next->op = asBC_ADDIf;
  380. else if( curr->next->op == asBC_SUBf ) curr->next->op = asBC_SUBIf;
  381. else if( curr->next->op == asBC_MULf ) curr->next->op = asBC_MULIf;
  382. curr->next->size = asBCTypeSize[asBCInfo[asBC_ADDIi].type];
  383. curr->next->arg = curr->arg;
  384. *next = GoBack(DeleteInstruction(curr));
  385. return true;
  386. }
  387. if( curr->op == asBC_SetV4 &&
  388. curr->next &&
  389. (curr->next->op == asBC_ADDi ||
  390. curr->next->op == asBC_MULi ||
  391. curr->next->op == asBC_ADDf ||
  392. curr->next->op == asBC_MULf) &&
  393. curr->wArg[0] == curr->next->wArg[1] &&
  394. (curr->next->wArg[0] == curr->wArg[0] || // The variable is overwritten
  395. (IsTemporary(curr->wArg[0]) && // The variable is temporary and never used again
  396. !IsTempVarRead(curr->next, curr->wArg[0]))) )
  397. {
  398. if( curr->next->op == asBC_ADDi ) curr->next->op = asBC_ADDIi;
  399. else if( curr->next->op == asBC_MULi ) curr->next->op = asBC_MULIi;
  400. else if( curr->next->op == asBC_ADDf ) curr->next->op = asBC_ADDIf;
  401. else if( curr->next->op == asBC_MULf ) curr->next->op = asBC_MULIf;
  402. curr->next->size = asBCTypeSize[asBCInfo[asBC_ADDIi].type];
  403. curr->next->arg = curr->arg;
  404. // The order of the operands are changed
  405. curr->next->wArg[1] = curr->next->wArg[2];
  406. *next = GoBack(DeleteInstruction(curr));
  407. return true;
  408. }
  409. // The values is immediately moved to another variable and then not used again
  410. if( (asBCInfo[curr->op].type == asBCTYPE_wW_rW_rW_ARG ||
  411. asBCInfo[curr->op].type == asBCTYPE_wW_rW_DW_ARG) &&
  412. curr->next && curr->next->op == asBC_CpyVtoV4 &&
  413. curr->wArg[0] == curr->next->wArg[1] &&
  414. IsTemporary(curr->wArg[0]) &&
  415. !IsTempVarRead(curr->next, curr->wArg[0]) )
  416. {
  417. curr->wArg[0] = curr->next->wArg[0];
  418. DeleteInstruction(curr->next);
  419. *next = GoBack(curr);
  420. return true;
  421. }
  422. // The constant value is immediately moved to another variable and then not used again
  423. if( curr->op == asBC_SetV4 && curr->next && curr->next->op == asBC_CpyVtoV4 &&
  424. curr->wArg[0] == curr->next->wArg[1] &&
  425. IsTemporary(curr->wArg[0]) &&
  426. !IsTempVarRead(curr->next, curr->wArg[0]) )
  427. {
  428. curr->wArg[0] = curr->next->wArg[0];
  429. DeleteInstruction(curr->next);
  430. *next = GoBack(curr);
  431. return true;
  432. }
  433. // The register is copied to a temp variable and then back to the register again without being used afterwards
  434. if( curr->op == asBC_CpyRtoV4 && curr->next && curr->next->op == asBC_CpyVtoR4 &&
  435. curr->wArg[0] == curr->next->wArg[0] &&
  436. IsTemporary(curr->wArg[0]) &&
  437. !IsTempVarRead(curr->next, curr->wArg[0]) )
  438. {
  439. // Delete both instructions
  440. DeleteInstruction(curr->next);
  441. *next = GoBack(DeleteInstruction(curr));
  442. return true;
  443. }
  444. // The global value is copied to a temp and then immediately pushed on the stack
  445. if( curr->op == asBC_CpyGtoV4 && curr->next && curr->next->op == asBC_PshV4 &&
  446. curr->wArg[0] == curr->next->wArg[0] &&
  447. IsTemporary(curr->wArg[0]) &&
  448. !IsTempVarRead(curr->next, curr->wArg[0]) )
  449. {
  450. curr->op = asBC_PshG4;
  451. curr->size = asBCTypeSize[asBCInfo[asBC_PshG4].type];
  452. curr->stackInc = asBCInfo[asBC_PshG4].stackInc;
  453. DeleteInstruction(curr->next);
  454. *next = GoBack(curr);
  455. return true;
  456. }
  457. // The constant is copied to a temp and then immediately pushed on the stack
  458. if( curr->op == asBC_SetV4 && curr->next && curr->next->op == asBC_PshV4 &&
  459. curr->wArg[0] == curr->next->wArg[0] &&
  460. IsTemporary(curr->wArg[0]) &&
  461. !IsTempVarRead(curr->next, curr->wArg[0]) )
  462. {
  463. curr->op = asBC_PshC4;
  464. curr->stackInc = asBCInfo[asBC_PshC4].stackInc;
  465. DeleteInstruction(curr->next);
  466. *next = GoBack(curr);
  467. return true;
  468. }
  469. if( curr->op == asBC_SetV8 && curr->next && curr->next->op == asBC_PshV8 &&
  470. curr->wArg[0] == curr->next->wArg[0] &&
  471. IsTemporary(curr->wArg[0]) &&
  472. !IsTempVarRead(curr->next, curr->wArg[0]) )
  473. {
  474. curr->op = asBC_PshC8;
  475. curr->stackInc = asBCInfo[asBC_PshC8].stackInc;
  476. DeleteInstruction(curr->next);
  477. *next = GoBack(curr);
  478. return true;
  479. }
  480. // The constant is copied to a global variable and then never used again
  481. if( curr->op == asBC_SetV4 && curr->next && curr->next->op == asBC_CpyVtoG4 &&
  482. curr->wArg[0] == curr->next->wArg[0] &&
  483. IsTemporary(curr->wArg[0]) &&
  484. !IsTempVarRead(curr->next, curr->wArg[0]) )
  485. {
  486. curr->op = asBC_SetG4;
  487. curr->size = asBCTypeSize[asBCInfo[asBC_SetG4].type];
  488. *(((asDWORD*)&curr->arg)+AS_PTR_SIZE) = *ARG_DW(curr->arg);
  489. *ARG_PTR(curr->arg) = *ARG_PTR(curr->next->arg);
  490. DeleteInstruction(curr->next);
  491. *next = GoBack(curr);
  492. return true;
  493. }
  494. return false;
  495. }
  496. bool asCByteCode::IsTemporary(short offset)
  497. {
  498. for( asUINT n = 0; n < temporaryVariables.GetLength(); n++ )
  499. if( temporaryVariables[n] == offset )
  500. return true;
  501. return false;
  502. }
  503. int asCByteCode::Optimize()
  504. {
  505. // TODO: runtime optimize: The optimizer should be able to inline function calls.
  506. // If the called function has only a few instructions, the function call should be inlined.
  507. // This is especially useful with the factory stubs used for template types and script classes.
  508. // TODO: runtime optimize: Need a bytecode BC_AddRef so that BC_CALLSYS doesn't have to be used for this trivial call
  509. // TODO: runtime optimize: A single bytecode for incrementing a variable, comparing, and jumping can probably improve
  510. // loops a lot. How often do these loops really occur?
  511. // TODO: runtime optimize: VAR + GET... should be optimized if the only instructions between them are trivial, i.e. no
  512. // function calls that can suspend the execution.
  513. cByteInstruction *instr = first;
  514. while( instr )
  515. {
  516. cByteInstruction *curr = instr;
  517. instr = instr->next;
  518. // Remove or combine instructions
  519. if( RemoveUnusedValue(curr, &instr) ) continue;
  520. // Postpone initializations so that they may be combined in the second pass
  521. if( PostponeInitOfTemp(curr, &instr) ) continue;
  522. // XXX x, YYY y, SwapPtr -> YYY y, XXX x
  523. if( CanBeSwapped(curr) )
  524. {
  525. // Delete SwapPtr
  526. DeleteInstruction(instr->next);
  527. // Swap instructions
  528. RemoveInstruction(instr);
  529. InsertBefore(curr, instr);
  530. instr = GoBack(instr);
  531. }
  532. // T??, ClrHi -> T??
  533. else if( IsCombination(curr, asBC_TZ , asBC_ClrHi) ||
  534. IsCombination(curr, asBC_TNZ, asBC_ClrHi) ||
  535. IsCombination(curr, asBC_TS , asBC_ClrHi) ||
  536. IsCombination(curr, asBC_TNS, asBC_ClrHi) ||
  537. IsCombination(curr, asBC_TP , asBC_ClrHi) ||
  538. IsCombination(curr, asBC_TNP, asBC_ClrHi) )
  539. {
  540. // Remove the ClrHi instruction, since the test instructions always clear the top bytes anyway
  541. DeleteInstruction(instr);
  542. instr = GoBack(curr);
  543. }
  544. // ClrHi, JZ -> JLowZ
  545. else if( IsCombination(curr, asBC_ClrHi, asBC_JZ) )
  546. {
  547. DeleteInstruction(curr);
  548. instr->op = asBC_JLowZ;
  549. instr = GoBack(instr);
  550. }
  551. // ClrHi, JNZ -> JLowNZ
  552. else if( IsCombination(curr, asBC_ClrHi, asBC_JNZ) )
  553. {
  554. DeleteInstruction(curr);
  555. instr->op = asBC_JLowNZ;
  556. instr = GoBack(instr);
  557. }
  558. // PGA, RDSPtr -> PshGPtr
  559. else if( IsCombination(curr, asBC_PGA, asBC_RDSPtr) )
  560. {
  561. curr->op = asBC_PshGPtr;
  562. DeleteInstruction(instr);
  563. instr = GoBack(curr);
  564. }
  565. // ChkRefS, RDSPtr -> RDSPtr, CHKREF
  566. else if( IsCombination(curr, asBC_ChkRefS, asBC_RDSPtr) )
  567. {
  568. // This exchange removes one pointer dereference, and also
  569. // makes it easier to completely remove the CHKREF instruction
  570. curr->op = asBC_RDSPtr;
  571. instr->op = asBC_CHKREF;
  572. instr = GoBack(curr);
  573. }
  574. // CHKREF, ADDSi -> ADDSi
  575. // CHKREF, RDSPtr -> RDSPtr
  576. else if( IsCombination(curr, asBC_CHKREF, asBC_ADDSi) ||
  577. IsCombination(curr, asBC_CHKREF, asBC_RDSPtr) )
  578. {
  579. // As ADDSi & RDSPtr already checks the pointer, the CHKREF instruction is unnecessary
  580. DeleteInstruction(curr);
  581. instr = GoBack(instr);
  582. }
  583. // ADDSi, CHKREF -> ADDSi
  584. else if( IsCombination(curr, asBC_ADDSi, asBC_CHKREF) )
  585. {
  586. // As ADDSi is guaranteed to work on valid pointers, then CHKREF is not necessary
  587. DeleteInstruction(instr);
  588. instr = GoBack(curr);
  589. }
  590. // PshVPtr 0, ADDSi, PopRPtr -> LoadThisR
  591. else if( IsCombination(curr, asBC_PshVPtr, asBC_ADDSi) &&
  592. IsCombination(instr, asBC_ADDSi, asBC_PopRPtr) &&
  593. curr->wArg[0] == 0 )
  594. {
  595. DeleteInstruction(curr);
  596. instr = GoBack(ChangeFirstDeleteNext(instr, asBC_LoadThisR));
  597. }
  598. // TODO: runtime optimize: PshVPtr x, PopRPtr -> LoadRObjR x, 0
  599. // PshVPtr x, ADDSi, PopRPtr -> LoadRObjR
  600. else if( IsCombination(curr, asBC_PshVPtr, asBC_ADDSi) &&
  601. IsCombination(instr, asBC_ADDSi, asBC_PopRPtr) &&
  602. curr->wArg[0] != 0 )
  603. {
  604. curr->op = asBC_LoadRObjR;
  605. curr->size = asBCTypeSize[asBCInfo[asBC_LoadRObjR].type];
  606. curr->stackInc = asBCInfo[asBC_LoadRObjR].stackInc;
  607. curr->wArg[1] = instr->wArg[0];
  608. *(asDWORD*)&curr->arg = *(asDWORD*)&instr->arg;
  609. DeleteInstruction(instr->next);
  610. DeleteInstruction(instr);
  611. instr = GoBack(curr);
  612. }
  613. // PSF x, REFCPY -> RefCpyV x
  614. else if( IsCombination(curr, asBC_PSF, asBC_REFCPY) )
  615. {
  616. instr->op = asBC_RefCpyV;
  617. instr->wArg[0] = curr->wArg[0];
  618. instr->stackInc = asBCInfo[asBC_LoadVObjR].stackInc;
  619. DeleteInstruction(curr);
  620. instr = GoBack(instr);
  621. }
  622. // PshNull, RefCpyV, PopPtr -> FREE
  623. else if( IsCombination(curr, asBC_PshNull, asBC_RefCpyV) &&
  624. IsCombination(instr, asBC_RefCpyV, asBC_PopPtr) )
  625. {
  626. DeleteInstruction(curr);
  627. instr->op = asBC_FREE;
  628. DeleteInstruction(instr->next);
  629. instr = GoBack(instr);
  630. }
  631. // PSF x, ADDSi, PopRPtr -> LoadVObjR
  632. else if( IsCombination(curr, asBC_PSF, asBC_ADDSi) &&
  633. IsCombination(instr, asBC_ADDSi, asBC_PopRPtr) )
  634. {
  635. curr->op = asBC_LoadVObjR;
  636. curr->size = asBCTypeSize[asBCInfo[asBC_LoadVObjR].type];
  637. curr->stackInc = asBCInfo[asBC_LoadVObjR].stackInc;
  638. curr->wArg[1] = instr->wArg[0];
  639. *(asDWORD*)&curr->arg = *(asDWORD*)&instr->arg;
  640. DeleteInstruction(instr->next);
  641. DeleteInstruction(instr);
  642. instr = GoBack(curr);
  643. }
  644. // LDG x, WRTV4 y -> CpyVtoG4 y, x
  645. else if( IsCombination(curr, asBC_LDG, asBC_WRTV4) && !IsTempRegUsed(instr) )
  646. {
  647. curr->op = asBC_CpyVtoG4;
  648. curr->size = asBCTypeSize[asBCInfo[asBC_CpyVtoG4].type];
  649. curr->wArg[0] = instr->wArg[0];
  650. DeleteInstruction(instr);
  651. instr = GoBack(curr);
  652. }
  653. // LDG x, RDR4 y -> CpyGtoV4 y, x
  654. else if( IsCombination(curr, asBC_LDG, asBC_RDR4) )
  655. {
  656. if( !IsTempRegUsed(instr) )
  657. curr->op = asBC_CpyGtoV4;
  658. else
  659. curr->op = asBC_LdGRdR4;
  660. curr->size = asBCTypeSize[asBCInfo[asBC_CpyGtoV4].type];
  661. curr->wArg[0] = instr->wArg[0];
  662. DeleteInstruction(instr);
  663. instr = GoBack(curr);
  664. }
  665. // LDV x, INCi -> IncVi x
  666. else if( IsCombination(curr, asBC_LDV, asBC_INCi) && !IsTempRegUsed(instr) )
  667. {
  668. curr->op = asBC_IncVi;
  669. DeleteInstruction(instr);
  670. instr = GoBack(curr);
  671. }
  672. // LDV x, DECi -> DecVi x
  673. else if( IsCombination(curr, asBC_LDV, asBC_DECi) && !IsTempRegUsed(instr) )
  674. {
  675. curr->op = asBC_DecVi;
  676. DeleteInstruction(instr);
  677. instr = GoBack(curr);
  678. }
  679. // PopPtr, RET b -> RET b
  680. else if( IsCombination(curr, asBC_PopPtr, asBC_RET) )
  681. {
  682. // We don't combine the PopPtr+RET because RET first restores
  683. // the previous stack pointer and then pops the arguments
  684. // Delete PopPtr
  685. instr = GoBack(DeleteInstruction(curr));
  686. }
  687. // Delete JitEntry if the JIT instructions are not supposed to be included
  688. else if( curr->op == asBC_JitEntry && !engine->ep.includeJitInstructions )
  689. {
  690. instr = GoBack(DeleteInstruction(curr));
  691. }
  692. // SUSPEND, JitEntry, SUSPEND -> SUSPEND
  693. // LINE, JitEntry, LINE -> LINE
  694. else if( (IsCombination(curr, asBC_SUSPEND, asBC_JitEntry) && IsCombination(instr, asBC_JitEntry, asBC_SUSPEND)) ||
  695. (IsCombination(curr, asBC_LINE, asBC_JitEntry) && IsCombination(instr, asBC_JitEntry, asBC_LINE)) )
  696. {
  697. // Delete the two first instructions
  698. DeleteInstruction(instr);
  699. instr = GoBack(DeleteInstruction(curr));
  700. }
  701. // SUSPEND, SUSPEND -> SUSPEND
  702. // LINE, LINE -> LINE
  703. else if( IsCombination(curr, asBC_SUSPEND, asBC_SUSPEND) ||
  704. IsCombination(curr, asBC_LINE, asBC_LINE) )
  705. {
  706. // Delete the first instruction
  707. instr = GoBack(DeleteInstruction(curr));
  708. }
  709. // SUSPEND, Block, SUSPEND -> Block, SUSPEND
  710. else if( (IsCombination(curr, asBC_SUSPEND, asBC_Block) && IsCombination(instr, asBC_Block, asBC_SUSPEND)) ||
  711. (IsCombination(curr, asBC_LINE, asBC_Block) && IsCombination(instr, asBC_Block, asBC_LINE)) )
  712. {
  713. // Delete the first instruction
  714. instr = GoBack(DeleteInstruction(curr));
  715. }
  716. // VAR a, GETREF 0 -> PSF a
  717. else if( IsCombination(curr, asBC_VAR, asBC_GETREF) && instr->wArg[0] == 0 )
  718. {
  719. instr = GoBack(ChangeFirstDeleteNext(curr, asBC_PSF));
  720. }
  721. // PGA, CHKREF -> PGA
  722. // PSF, CHKREF -> PSF
  723. else if( IsCombination(curr, asBC_PGA, asBC_CHKREF) ||
  724. IsCombination(curr, asBC_PSF, asBC_CHKREF) )
  725. {
  726. // Delete CHKREF since PGA and PSF always pushes a valid address on the stack
  727. DeleteInstruction(instr);
  728. instr = GoBack(curr);
  729. }
  730. // PGA, ChkRefS, CHKREF -> PGA, ChkRefS
  731. else if( IsCombination(curr, asBC_PGA, asBC_ChkRefS) &&
  732. IsCombination(instr, asBC_ChkRefS, asBC_CHKREF) )
  733. {
  734. // Delete CHKREF since PGA always pushes a valid address on the stack
  735. DeleteInstruction(instr->next);
  736. instr = GoBack(curr);
  737. }
  738. // PSF, FREE -> FREE, PSF
  739. else if( IsCombination(curr, asBC_PSF, asBC_FREE) )
  740. {
  741. // This pattern usually happens when a function returns an object, or handle
  742. // and then releases a temporary variable, possibly used in one of the arguments.
  743. // By swapping the order of these instructions, the code can be further optimized
  744. // to combine the PSF with the following instructions
  745. RemoveInstruction(instr);
  746. InsertBefore(curr, instr);
  747. instr = GoBack(instr);
  748. }
  749. // PshVPtr y, PopPtr -> nothing
  750. // PSF y , PopPtr -> nothing
  751. // VAR y , PopPtr -> nothing
  752. // PshNull , PopPtr -> nothing
  753. // PshRPtr , PopPtr -> nothing
  754. else if( IsCombination(curr, asBC_PshRPtr, asBC_PopPtr) ||
  755. IsCombination(curr, asBC_PSF , asBC_PopPtr) ||
  756. IsCombination(curr, asBC_VAR , asBC_PopPtr) ||
  757. IsCombination(curr, asBC_PshVPtr, asBC_PopPtr) ||
  758. IsCombination(curr, asBC_PshNull, asBC_PopPtr) )
  759. {
  760. // A pointer is pushed on the stack then immediately removed
  761. // Remove both instructions as they cancel each other
  762. cByteInstruction *instr2 = instr->next;
  763. DeleteInstruction(curr);
  764. DeleteInstruction(instr);
  765. instr = GoBack(instr2);
  766. }
  767. // Begin PATTERN
  768. // T**; J** +x -> J** +x
  769. else if( IsCombination(curr, asBC_TZ , asBC_JZ ) ||
  770. IsCombination(curr, asBC_TNZ, asBC_JNZ) )
  771. instr = GoBack(DeleteFirstChangeNext(curr, asBC_JNZ));
  772. else if( IsCombination(curr, asBC_TNZ, asBC_JZ ) ||
  773. IsCombination(curr, asBC_TZ , asBC_JNZ) )
  774. instr = GoBack(DeleteFirstChangeNext(curr, asBC_JZ));
  775. else if( IsCombination(curr, asBC_TS , asBC_JZ ) ||
  776. IsCombination(curr, asBC_TNS, asBC_JNZ) )
  777. instr = GoBack(DeleteFirstChangeNext(curr, asBC_JNS));
  778. else if( IsCombination(curr, asBC_TNS, asBC_JZ ) ||
  779. IsCombination(curr, asBC_TS , asBC_JNZ) )
  780. instr = GoBack(DeleteFirstChangeNext(curr, asBC_JS));
  781. else if( IsCombination(curr, asBC_TP , asBC_JZ ) ||
  782. IsCombination(curr, asBC_TNP, asBC_JNZ) )
  783. instr = GoBack(DeleteFirstChangeNext(curr, asBC_JNP));
  784. else if( IsCombination(curr, asBC_TNP, asBC_JZ ) ||
  785. IsCombination(curr, asBC_TP , asBC_JNZ) )
  786. instr = GoBack(DeleteFirstChangeNext(curr, asBC_JP));
  787. // End PATTERN
  788. // JMP +0 -> remove
  789. else if( IsCombination(curr, asBC_JMP, asBC_LABEL) && *(int*)&curr->arg == instr->wArg[0] )
  790. instr = GoBack(DeleteInstruction(curr));
  791. // PSF, RDSPtr -> PshVPtr
  792. else if( IsCombination(curr, asBC_PSF, asBC_RDSPtr) )
  793. {
  794. curr->op = asBC_PshVPtr;
  795. DeleteInstruction(instr);
  796. instr = GoBack(curr);
  797. }
  798. // PSF, ChkRefS, RDSPtr -> PshVPtr, CHKREF
  799. else if( IsCombination(curr, asBC_PSF, asBC_ChkRefS) &&
  800. IsCombination(instr, asBC_ChkRefS, asBC_RDSPtr) )
  801. {
  802. curr->op = asBC_PshVPtr;
  803. instr->op = asBC_CHKREF;
  804. DeleteInstruction(instr->next);
  805. instr = GoBack(curr);
  806. }
  807. // PSF, ChkRefS, PopPtr -> ChkNullV
  808. else if( IsCombination(curr, asBC_PSF, asBC_ChkRefS) &&
  809. IsCombination(instr, asBC_ChkRefS, asBC_PopPtr) )
  810. {
  811. curr->op = asBC_ChkNullV;
  812. curr->stackInc = 0;
  813. // Delete the PopPtr instruction
  814. DeleteInstruction(instr->next);
  815. // Delete the ChkRefS instruction
  816. DeleteInstruction(instr);
  817. instr = GoBack(curr);
  818. }
  819. // PshVPtr, CHKREF, PopPtr -> ChkNullV
  820. else if( IsCombination(curr, asBC_PshVPtr, asBC_CHKREF) &&
  821. IsCombination(instr, asBC_CHKREF, asBC_PopPtr) )
  822. {
  823. curr->op = asBC_ChkNullV;
  824. curr->stackInc = 0;
  825. DeleteInstruction(instr->next);
  826. DeleteInstruction(instr);
  827. instr = GoBack(curr);
  828. }
  829. }
  830. return 0;
  831. }
  832. bool asCByteCode::IsTempVarReadByInstr(cByteInstruction *curr, int offset)
  833. {
  834. // Which instructions read from variables?
  835. if( asBCInfo[curr->op].type == asBCTYPE_wW_rW_rW_ARG &&
  836. (curr->wArg[1] == offset || curr->wArg[2] == offset) )
  837. return true;
  838. else if( (asBCInfo[curr->op].type == asBCTYPE_rW_ARG ||
  839. asBCInfo[curr->op].type == asBCTYPE_rW_DW_ARG ||
  840. asBCInfo[curr->op].type == asBCTYPE_rW_QW_ARG ||
  841. asBCInfo[curr->op].type == asBCTYPE_rW_W_DW_ARG ||
  842. curr->op == asBC_FREE) && // FREE both read and write to the variable
  843. curr->wArg[0] == offset )
  844. return true;
  845. else if( (asBCInfo[curr->op].type == asBCTYPE_wW_rW_ARG ||
  846. asBCInfo[curr->op].type == asBCTYPE_wW_rW_DW_ARG) &&
  847. curr->wArg[1] == offset )
  848. return true;
  849. else if( asBCInfo[curr->op].type == asBCTYPE_rW_rW_ARG &&
  850. ((signed)curr->wArg[0] == offset || (signed)curr->wArg[1] == offset) )
  851. return true;
  852. else if( curr->op == asBC_LoadThisR && offset == 0 )
  853. return true;
  854. return false;
  855. }
  856. bool asCByteCode::IsInstrJmpOrLabel(cByteInstruction *curr)
  857. {
  858. if( curr->op == asBC_JS ||
  859. curr->op == asBC_JNS ||
  860. curr->op == asBC_JP ||
  861. curr->op == asBC_JNP ||
  862. curr->op == asBC_JMPP ||
  863. curr->op == asBC_JMP ||
  864. curr->op == asBC_JZ ||
  865. curr->op == asBC_JNZ ||
  866. curr->op == asBC_JLowZ ||
  867. curr->op == asBC_JLowNZ ||
  868. curr->op == asBC_LABEL )
  869. return true;
  870. return false;
  871. }
  872. bool asCByteCode::IsTempVarOverwrittenByInstr(cByteInstruction *curr, int offset)
  873. {
  874. // Which instructions overwrite the variable or discard it?
  875. if( curr->op == asBC_RET ||
  876. curr->op == asBC_SUSPEND )
  877. return true;
  878. else if( (asBCInfo[curr->op].type == asBCTYPE_wW_rW_rW_ARG ||
  879. asBCInfo[curr->op].type == asBCTYPE_wW_rW_ARG ||
  880. asBCInfo[curr->op].type == asBCTYPE_wW_rW_DW_ARG ||
  881. asBCInfo[curr->op].type == asBCTYPE_wW_ARG ||
  882. asBCInfo[curr->op].type == asBCTYPE_wW_W_ARG ||
  883. asBCInfo[curr->op].type == asBCTYPE_wW_DW_ARG ||
  884. asBCInfo[curr->op].type == asBCTYPE_wW_QW_ARG) &&
  885. curr->wArg[0] == offset )
  886. return true;
  887. return false;
  888. }
  889. bool asCByteCode::IsTempVarRead(cByteInstruction *curr, int offset)
  890. {
  891. asCArray<cByteInstruction *> openPaths;
  892. asCArray<cByteInstruction *> closedPaths;
  893. // We're not interested in the first instruction, since it is the one that sets the variable
  894. openPaths.PushLast(curr->next);
  895. while( openPaths.GetLength() )
  896. {
  897. curr = openPaths.PopLast();
  898. // Add the instruction to the closed paths so that we don't verify it again
  899. closedPaths.PushLast(curr);
  900. while( curr )
  901. {
  902. if( IsTempVarReadByInstr(curr, offset) ) return true;
  903. if( IsTempVarOverwrittenByInstr(curr, offset) ) break;
  904. // In case of jumps, we must follow the each of the paths
  905. if( curr->op == asBC_JMP )
  906. {
  907. int label = *((int*)ARG_DW(curr->arg));
  908. int r = FindLabel(label, curr, &curr, 0); asASSERT( r == 0 ); UNUSED_VAR(r);
  909. if( !closedPaths.Exists(curr) &&
  910. !openPaths.Exists(curr) )
  911. openPaths.PushLast(curr);
  912. break;
  913. }
  914. else if( curr->op == asBC_JZ || curr->op == asBC_JNZ ||
  915. curr->op == asBC_JS || curr->op == asBC_JNS ||
  916. curr->op == asBC_JP || curr->op == asBC_JNP ||
  917. curr->op == asBC_JLowZ || curr->op == asBC_JLowNZ )
  918. {
  919. cByteInstruction *dest = 0;
  920. int label = *((int*)ARG_DW(curr->arg));
  921. int r = FindLabel(label, curr, &dest, 0); asASSERT( r == 0 ); UNUSED_VAR(r);
  922. if( !closedPaths.Exists(dest) &&
  923. !openPaths.Exists(dest) )
  924. openPaths.PushLast(dest);
  925. }
  926. else if( curr->op == asBC_JMPP )
  927. {
  928. // A JMPP instruction is always followed by a series of JMP instructions
  929. // that give the real destination (like a look-up table). We need add all
  930. // of these as open paths.
  931. curr = curr->next;
  932. while( curr->op == asBC_JMP )
  933. {
  934. cByteInstruction *dest = 0;
  935. int label = *((int*)ARG_DW(curr->arg));
  936. int r = FindLabel(label, curr, &dest, 0); asASSERT( r == 0 ); UNUSED_VAR(r);
  937. if( !closedPaths.Exists(dest) &&
  938. !openPaths.Exists(dest) )
  939. openPaths.PushLast(dest);
  940. curr = curr->next;
  941. }
  942. // We should now be on a label which is the destination of the
  943. // first JMP in the sequence and is already added in the open paths
  944. asASSERT(curr->op == asBC_LABEL);
  945. break;
  946. }
  947. curr = curr->next;
  948. }
  949. }
  950. return false;
  951. }
  952. bool asCByteCode::IsTempRegUsed(cByteInstruction *curr)
  953. {
  954. // We're not interested in the first instruction, since it is the one that sets the register
  955. while( curr->next )
  956. {
  957. curr = curr->next;
  958. // Which instructions read from the register?
  959. if( curr->op == asBC_INCi ||
  960. curr->op == asBC_INCi16 ||
  961. curr->op == asBC_INCi8 ||
  962. curr->op == asBC_INCf ||
  963. curr->op == asBC_INCd ||
  964. curr->op == asBC_DECi ||
  965. curr->op == asBC_DECi16 ||
  966. curr->op == asBC_DECi8 ||
  967. curr->op == asBC_DECf ||
  968. curr->op == asBC_DECd ||
  969. curr->op == asBC_WRTV1 ||
  970. curr->op == asBC_WRTV2 ||
  971. curr->op == asBC_WRTV4 ||
  972. curr->op == asBC_WRTV8 ||
  973. curr->op == asBC_RDR1 ||
  974. curr->op == asBC_RDR2 ||
  975. curr->op == asBC_RDR4 ||
  976. curr->op == asBC_RDR8 ||
  977. curr->op == asBC_PshRPtr ||
  978. curr->op == asBC_CpyRtoV4 ||
  979. curr->op == asBC_CpyRtoV8 ||
  980. curr->op == asBC_TZ ||
  981. curr->op == asBC_TNZ ||
  982. curr->op == asBC_TS ||
  983. curr->op == asBC_TNS ||
  984. curr->op == asBC_TP ||
  985. curr->op == asBC_TNP ||
  986. curr->op == asBC_JZ ||
  987. curr->op == asBC_JNZ ||
  988. curr->op == asBC_JLowZ ||
  989. curr->op == asBC_JLowNZ ||
  990. curr->op == asBC_JS ||
  991. curr->op == asBC_JNS ||
  992. curr->op == asBC_JP ||
  993. curr->op == asBC_JNP )
  994. return true;
  995. // Which instructions overwrite the register or discard the value?
  996. if( curr->op == asBC_CALL ||
  997. curr->op == asBC_PopRPtr ||
  998. curr->op == asBC_CALLSYS ||
  999. curr->op == asBC_CALLBND ||
  1000. curr->op == asBC_SUSPEND ||
  1001. curr->op == asBC_ALLOC ||
  1002. curr->op == asBC_CpyVtoR4 ||
  1003. curr->op == asBC_LdGRdR4 ||
  1004. curr->op == asBC_LDG ||
  1005. curr->op == asBC_LDV ||
  1006. curr->op == asBC_TZ ||
  1007. curr->op == asBC_TNZ ||
  1008. curr->op == asBC_TS ||
  1009. curr->op == asBC_TNS ||
  1010. curr->op == asBC_TP ||
  1011. curr->op == asBC_TNP ||
  1012. curr->op == asBC_JS ||
  1013. curr->op == asBC_JNS ||
  1014. curr->op == asBC_JP ||
  1015. curr->op == asBC_JNP ||
  1016. curr->op == asBC_JMPP ||
  1017. curr->op == asBC_JMP ||
  1018. curr->op == asBC_JZ ||
  1019. curr->op == asBC_JNZ ||
  1020. curr->op == asBC_JLowZ ||
  1021. curr->op == asBC_JLowNZ ||
  1022. curr->op == asBC_CMPi ||
  1023. curr->op == asBC_CMPu ||
  1024. curr->op == asBC_CMPf ||
  1025. curr->op == asBC_CMPd ||
  1026. curr->op == asBC_CMPIi ||
  1027. curr->op == asBC_CMPIu ||
  1028. curr->op == asBC_CMPIf ||
  1029. curr->op == asBC_LABEL ||
  1030. curr->op == asBC_LoadThisR ||
  1031. curr->op == asBC_LoadRObjR ||
  1032. curr->op == asBC_LoadVObjR )
  1033. return false;
  1034. }
  1035. return false;
  1036. }
  1037. bool asCByteCode::IsSimpleExpression()
  1038. {
  1039. // A simple expression is one that cannot be suspended at any time, i.e.
  1040. // it doesn't have any calls to other routines, and doesn't have any suspend instructions
  1041. cByteInstruction *instr = first;
  1042. while( instr )
  1043. {
  1044. if( instr->op == asBC_ALLOC ||
  1045. instr->op == asBC_CALL ||
  1046. instr->op == asBC_CALLSYS ||
  1047. instr->op == asBC_SUSPEND ||
  1048. instr->op == asBC_LINE ||
  1049. instr->op == asBC_FREE ||
  1050. instr->op == asBC_CallPtr ||
  1051. instr->op == asBC_CALLINTF ||
  1052. instr->op == asBC_CALLBND )
  1053. return false;
  1054. instr = instr->next;
  1055. }
  1056. return true;
  1057. }
  1058. void asCByteCode::ExtractLineNumbers()
  1059. {
  1060. int lastLinePos = -1;
  1061. int pos = 0;
  1062. cByteInstruction *instr = first;
  1063. while( instr )
  1064. {
  1065. cByteInstruction *curr = instr;
  1066. instr = instr->next;
  1067. if( curr->op == asBC_LINE )
  1068. {
  1069. if( lastLinePos == pos )
  1070. {
  1071. lineNumbers.PopLast();
  1072. lineNumbers.PopLast();
  1073. }
  1074. lastLinePos = pos;
  1075. lineNumbers.PushLast(pos);
  1076. lineNumbers.PushLast(*(int*)ARG_DW(curr->arg));
  1077. if( !engine->ep.buildWithoutLineCues )
  1078. {
  1079. // Transform BC_LINE into BC_SUSPEND
  1080. curr->op = asBC_SUSPEND;
  1081. curr->size = asBCTypeSize[asBCInfo[asBC_SUSPEND].type];
  1082. pos += curr->size;
  1083. }
  1084. else
  1085. {
  1086. // Delete the instruction
  1087. DeleteInstruction(curr);
  1088. }
  1089. }
  1090. else
  1091. pos += curr->size;
  1092. }
  1093. }
  1094. void asCByteCode::ExtractObjectVariableInfo(asCScriptFunction *outFunc)
  1095. {
  1096. int pos = 0;
  1097. cByteInstruction *instr = first;
  1098. while( instr )
  1099. {
  1100. if( instr->op == asBC_Block )
  1101. {
  1102. asSObjectVariableInfo info;
  1103. info.programPos = pos;
  1104. info.variableOffset = 0;
  1105. info.option = instr->wArg[0] ? asBLOCK_BEGIN : asBLOCK_END;
  1106. outFunc->objVariableInfo.PushLast(info);
  1107. }
  1108. else if( instr->op == asBC_ObjInfo )
  1109. {
  1110. asSObjectVariableInfo info;
  1111. info.programPos = pos;
  1112. info.variableOffset = (short)instr->wArg[0];
  1113. info.option = *(int*)ARG_DW(instr->arg);
  1114. outFunc->objVariableInfo.PushLast(info);
  1115. }
  1116. else if( instr->op == asBC_VarDecl )
  1117. {
  1118. outFunc->variables[instr->wArg[0]]->declaredAtProgramPos = pos;
  1119. }
  1120. else
  1121. pos += instr->size;
  1122. instr = instr->next;
  1123. }
  1124. }
  1125. int asCByteCode::GetSize()
  1126. {
  1127. int size = 0;
  1128. cByteInstruction *instr = first;
  1129. while( instr )
  1130. {
  1131. size += instr->GetSize();
  1132. instr = instr->next;
  1133. }
  1134. return size;
  1135. }
  1136. void asCByteCode::AddCode(asCByteCode *bc)
  1137. {
  1138. if( bc->first )
  1139. {
  1140. if( first == 0 )
  1141. {
  1142. first = bc->first;
  1143. last = bc->last;
  1144. bc->first = 0;
  1145. bc->last = 0;
  1146. }
  1147. else
  1148. {
  1149. last->next = bc->first;
  1150. bc->first->prev = last;
  1151. last = bc->last;
  1152. bc->first = 0;
  1153. bc->last = 0;
  1154. }
  1155. }
  1156. }
  1157. int asCByteCode::AddInstruction()
  1158. {
  1159. cByteInstruction *instr = new(engine->memoryMgr.AllocByteInstruction()) cByteInstruction();
  1160. if( first == 0 )
  1161. {
  1162. first = last = instr;
  1163. }
  1164. else
  1165. {
  1166. last->AddAfter(instr);
  1167. last = instr;
  1168. }
  1169. return 0;
  1170. }
  1171. int asCByteCode::AddInstructionFirst()
  1172. {
  1173. cByteInstruction *instr = new(engine->memoryMgr.AllocByteInstruction()) cByteInstruction();
  1174. if( first == 0 )
  1175. {
  1176. first = last = instr;
  1177. }
  1178. else
  1179. {
  1180. first->AddBefore(instr);
  1181. first = instr;
  1182. }
  1183. return 0;
  1184. }
  1185. void asCByteCode::Call(asEBCInstr instr, int funcID, int pop)
  1186. {
  1187. if( AddInstruction() < 0 )
  1188. return;
  1189. asASSERT(asBCInfo[instr].type == asBCTYPE_DW_ARG);
  1190. last->op = instr;
  1191. last->size = asBCTypeSize[asBCInfo[instr].type];
  1192. last->stackInc = -pop; // BC_CALL and BC_CALLBND doesn't pop the argument but when the callee returns the arguments are already popped
  1193. *((int*)ARG_DW(last->arg)) = funcID;
  1194. // Add a JitEntry instruction after function calls so that JIT's can resume execution
  1195. InstrPTR(asBC_JitEntry, 0);
  1196. }
  1197. void asCByteCode::CallPtr(asEBCInstr instr, int funcPtrVar, int pop)
  1198. {
  1199. if( AddInstruction() < 0 )
  1200. return;
  1201. asASSERT(asBCInfo[instr].type == asBCTYPE_rW_ARG);
  1202. last->op = instr;
  1203. last->size = asBCTypeSize[asBCInfo[instr].type];
  1204. last->stackInc = -pop;
  1205. last->wArg[0] = (short)funcPtrVar;
  1206. // Add a JitEntry instruction after function calls so that JIT's can resume execution
  1207. InstrPTR(asBC_JitEntry, 0);
  1208. }
  1209. void asCByteCode::Alloc(asEBCInstr instr, void *objID, int funcID, int pop)
  1210. {
  1211. if( AddInstruction() < 0 )
  1212. return;
  1213. last->op = instr;
  1214. last->size = asBCTypeSize[asBCInfo[instr].type];
  1215. last->stackInc = -pop; // BC_ALLOC
  1216. asASSERT(asBCInfo[instr].type == asBCTYPE_PTR_DW_ARG);
  1217. *ARG_PTR(last->arg) = (asPWORD)objID;
  1218. *((int*)(ARG_DW(last->arg)+AS_PTR_SIZE)) = funcID;
  1219. // Add a JitEntry instruction after function calls so that JIT's can resume execution
  1220. InstrPTR(asBC_JitEntry, 0);
  1221. }
  1222. void asCByteCode::Ret(int pop)
  1223. {
  1224. if( AddInstruction() < 0 )
  1225. return;
  1226. asASSERT(asBCInfo[asBC_RET].type == asBCTYPE_W_ARG);
  1227. last->op = asBC_RET;
  1228. last->size = asBCTypeSize[asBCInfo[asBC_RET].type];
  1229. last->stackInc = 0; // The instruction pops the argument, but it doesn't affect current function
  1230. last->wArg[0] = (short)pop;
  1231. }
  1232. void asCByteCode::JmpP(int var, asDWORD max)
  1233. {
  1234. if( AddInstruction() < 0 )
  1235. return;
  1236. asASSERT(asBCInfo[asBC_JMPP].type == asBCTYPE_rW_ARG);
  1237. last->op = asBC_JMPP;
  1238. last->size = asBCTypeSize[asBCInfo[asBC_JMPP].type];
  1239. last->stackInc = asBCInfo[asBC_JMPP].stackInc;
  1240. last->wArg[0] = (short)var;
  1241. // Store the largest jump that is made for PostProcess()
  1242. *ARG_DW(last->arg) = max;
  1243. }
  1244. void asCByteCode::Label(short label)
  1245. {
  1246. if( AddInstruction() < 0 )
  1247. return;
  1248. last->op = asBC_LABEL;
  1249. last->size = 0;
  1250. last->stackInc = 0;
  1251. last->wArg[0] = label;
  1252. }
  1253. void asCByteCode::Line(int line, int column)
  1254. {
  1255. if( AddInstruction() < 0 )
  1256. return;
  1257. last->op = asBC_LINE;
  1258. // If the build is without line cues these instructions will be removed
  1259. // otherwise they will be transformed into SUSPEND instructions.
  1260. if( engine->ep.buildWithoutLineCues )
  1261. last->size = 0;
  1262. else
  1263. last->size = asBCTypeSize[asBCInfo[asBC_SUSPEND].type];
  1264. last->stackInc = 0;
  1265. *((int*)ARG_DW(last->arg)) = (line & 0xFFFFF)|((column & 0xFFF)<<20);
  1266. // Add a JitEntry after the line instruction to allow the JIT function to resume after a suspend
  1267. InstrPTR(asBC_JitEntry, 0);
  1268. }
  1269. void asCByteCode::ObjInfo(int offset, int info)
  1270. {
  1271. if( AddInstruction() < 0 )
  1272. return;
  1273. // Add the special instruction that will be used to tell the exception
  1274. // handler when an object is initialized and deinitialized.
  1275. last->op = asBC_ObjInfo;
  1276. last->size = 0;
  1277. last->stackInc = 0;
  1278. last->wArg[0] = (short)offset;
  1279. *((int*)ARG_DW(last->arg)) = info;
  1280. }
  1281. void asCByteCode::Block(bool start)
  1282. {
  1283. if( AddInstruction() < 0 )
  1284. return;
  1285. last->op = asBC_Block;
  1286. last->size = 0;
  1287. last->stackInc = 0;
  1288. last->wArg[0] = start ? 1 : 0;
  1289. }
  1290. void asCByteCode::VarDecl(int varDeclIdx)
  1291. {
  1292. if( AddInstruction() < 0 )
  1293. return;
  1294. last->op = asBC_VarDecl;
  1295. last->size = 0;
  1296. last->stackInc = 0;
  1297. last->wArg[0] = (asWORD)varDeclIdx;
  1298. }
  1299. int asCByteCode::FindLabel(int label, cByteInstruction *from, cByteInstruction **dest, int *positionDelta)
  1300. {
  1301. // Search forward
  1302. int labelPos = -from->GetSize();
  1303. cByteInstruction *labelInstr = from;
  1304. while( labelInstr )
  1305. {
  1306. labelPos += labelInstr->GetSize();
  1307. labelInstr = labelInstr->next;
  1308. if( labelInstr && labelInstr->op == asBC_LABEL )
  1309. {
  1310. if( labelInstr->wArg[0] == label )
  1311. break;
  1312. }
  1313. }
  1314. if( labelInstr == 0 )
  1315. {
  1316. // Search backwards
  1317. labelPos = -from->GetSize();
  1318. labelInstr = from;
  1319. while( labelInstr )
  1320. {
  1321. labelInstr = labelInstr->prev;
  1322. if( labelInstr )
  1323. {
  1324. labelPos -= labelInstr->GetSize();
  1325. if( labelInstr->op == asBC_LABEL )
  1326. {
  1327. if( labelInstr->wArg[0] == label )
  1328. break;
  1329. }
  1330. }
  1331. }
  1332. }
  1333. if( labelInstr != 0 )
  1334. {
  1335. if( dest ) *dest = labelInstr;
  1336. if( positionDelta ) *positionDelta = labelPos;
  1337. return 0;
  1338. }
  1339. return -1;
  1340. }
  1341. int asCByteCode::ResolveJumpAddresses()
  1342. {
  1343. cByteInstruction *instr = first;
  1344. while( instr )
  1345. {
  1346. if( instr->op == asBC_JMP ||
  1347. instr->op == asBC_JZ || instr->op == asBC_JNZ ||
  1348. instr->op == asBC_JLowZ || instr->op == asBC_JLowNZ ||
  1349. instr->op == asBC_JS || instr->op == asBC_JNS ||
  1350. instr->op == asBC_JP || instr->op == asBC_JNP )
  1351. {
  1352. int label = *((int*) ARG_DW(instr->arg));
  1353. int labelPosOffset;
  1354. int r = FindLabel(label, instr, 0, &labelPosOffset);
  1355. if( r == 0 )
  1356. *((int*) ARG_DW(instr->arg)) = labelPosOffset;
  1357. else
  1358. return -1;
  1359. }
  1360. instr = instr->next;
  1361. }
  1362. return 0;
  1363. }
  1364. cByteInstruction *asCByteCode::DeleteInstruction(cByteInstruction *instr)
  1365. {
  1366. if( instr == 0 ) return 0;
  1367. cByteInstruction *ret = instr->prev ? instr->prev : instr->next;
  1368. RemoveInstruction(instr);
  1369. engine->memoryMgr.FreeByteInstruction(instr);
  1370. return ret;
  1371. }
  1372. void asCByteCode::Output(asDWORD *array)
  1373. {
  1374. // TODO: Receive a script function pointer instead of the bytecode array
  1375. asDWORD *ap = array;
  1376. cByteInstruction *instr = first;
  1377. while( instr )
  1378. {
  1379. if( instr->GetSize() > 0 )
  1380. {
  1381. *(asBYTE*)ap = asBYTE(instr->op);
  1382. *(((asBYTE*)ap)+1) = 0; // Second byte is always zero
  1383. switch( asBCInfo[instr->op].type )
  1384. {
  1385. case asBCTYPE_NO_ARG:
  1386. *(((asWORD*)ap)+1) = 0; // Clear upper bytes
  1387. break;
  1388. case asBCTYPE_wW_rW_rW_ARG:
  1389. *(((asWORD*)ap)+1) = instr->wArg[0];
  1390. *(((asWORD*)ap)+2) = instr->wArg[1];
  1391. *(((asWORD*)ap)+3) = instr->wArg[2];
  1392. break;
  1393. case asBCTYPE_wW_DW_ARG:
  1394. case asBCTYPE_rW_DW_ARG:
  1395. case asBCTYPE_W_DW_ARG:
  1396. *(((asWORD*)ap)+1) = instr->wArg[0];
  1397. *(ap+1) = *(asDWORD*)&instr->arg;
  1398. break;
  1399. case asBCTYPE_wW_rW_DW_ARG:
  1400. case asBCTYPE_rW_W_DW_ARG:
  1401. *(((asWORD*)ap)+1) = instr->wArg[0];
  1402. *(((asWORD*)ap)+2) = instr->wArg[1];
  1403. *(ap+2) = *(asDWORD*)&instr->arg;
  1404. break;
  1405. case asBCTYPE_wW_QW_ARG:
  1406. case asBCTYPE_rW_QW_ARG:
  1407. *(((asWORD*)ap)+1) = instr->wArg[0];
  1408. *(asQWORD*)(ap+1) = asQWORD(instr->arg);
  1409. break;
  1410. case asBCTYPE_W_ARG:
  1411. case asBCTYPE_rW_ARG:
  1412. case asBCTYPE_wW_ARG:
  1413. *(((asWORD*)ap)+1) = instr->wArg[0];
  1414. break;
  1415. case asBCTYPE_wW_rW_ARG:
  1416. case asBCTYPE_rW_rW_ARG:
  1417. case asBCTYPE_wW_W_ARG:
  1418. *(((asWORD *)ap)+1) = instr->wArg[0];
  1419. *(((asWORD *)ap)+2) = instr->wArg[1];
  1420. break;
  1421. case asBCTYPE_QW_DW_ARG:
  1422. case asBCTYPE_DW_DW_ARG:
  1423. case asBCTYPE_QW_ARG:
  1424. case asBCTYPE_DW_ARG:
  1425. *(((asWORD*)ap)+1) = 0; // Clear upper bytes
  1426. memcpy(ap+1, &instr->arg, instr->GetSize()*4-4);
  1427. break;
  1428. default:
  1429. // How did we get here?
  1430. asASSERT(false);
  1431. break;
  1432. }
  1433. }
  1434. ap += instr->GetSize();
  1435. instr = instr->next;
  1436. }
  1437. }
  1438. void asCByteCode::PostProcess()
  1439. {
  1440. if( first == 0 ) return;
  1441. // This function will do the following
  1442. // - Verify if there is any code that never gets executed and remove it
  1443. // - Calculate the stack size at the position of each byte code
  1444. // - Calculate the largest stack needed
  1445. largestStackUsed = 0;
  1446. cByteInstruction *instr = first;
  1447. while( instr )
  1448. {
  1449. instr->marked = false;
  1450. instr->stackSize = -1;
  1451. instr = instr->next;
  1452. }
  1453. // Add the first instruction to the list of unchecked code paths
  1454. asCArray<cByteInstruction *> paths;
  1455. AddPath(paths, first, 0);
  1456. // Go through each of the code paths
  1457. for( asUINT p = 0; p < paths.GetLength(); ++p )
  1458. {
  1459. instr = paths[p];
  1460. int stackSize = instr->stackSize;
  1461. while( instr )
  1462. {
  1463. instr->marked = true;
  1464. instr->stackSize = stackSize;
  1465. stackSize += instr->stackInc;
  1466. if( stackSize > largestStackUsed )
  1467. largestStackUsed = stackSize;
  1468. if( instr->op == asBC_JMP )
  1469. {
  1470. // Find the label that we should jump to
  1471. int label = *((int*) ARG_DW(instr->arg));
  1472. cByteInstruction *dest = 0;
  1473. int r = FindLabel(label, instr, &dest, 0); asASSERT( r == 0 ); UNUSED_VAR(r);
  1474. AddPath(paths, dest, stackSize);
  1475. break;
  1476. }
  1477. else if( instr->op == asBC_JZ || instr->op == asBC_JNZ ||
  1478. instr->op == asBC_JLowZ || instr->op == asBC_JLowNZ ||
  1479. instr->op == asBC_JS || instr->op == asBC_JNS ||
  1480. instr->op == asBC_JP || instr->op == asBC_JNP )
  1481. {
  1482. // Find the label that is being jumped to
  1483. int label = *((int*) ARG_DW(instr->arg));
  1484. cByteInstruction *dest = 0;
  1485. int r = FindLabel(label, instr, &dest, 0); asASSERT( r == 0 ); UNUSED_VAR(r);
  1486. AddPath(paths, dest, stackSize);
  1487. // Add both paths to the code paths
  1488. AddPath(paths, instr->next, stackSize);
  1489. break;
  1490. }
  1491. else if( instr->op == asBC_JMPP )
  1492. {
  1493. // I need to know the largest value possible
  1494. asDWORD max = *ARG_DW(instr->arg);
  1495. // Add all destinations to the code paths
  1496. cByteInstruction *dest = instr->next;
  1497. for( asDWORD n = 0; n <= max && dest != 0; ++n )
  1498. {
  1499. AddPath(paths, dest, stackSize);
  1500. dest = dest->next;
  1501. }
  1502. break;
  1503. }
  1504. else
  1505. {
  1506. instr = instr->next;
  1507. if( instr == 0 || instr->marked )
  1508. break;
  1509. }
  1510. }
  1511. }
  1512. // Are there any instructions that didn't get visited?
  1513. instr = first;
  1514. while( instr )
  1515. {
  1516. if( instr->marked == false )
  1517. {
  1518. // TODO: Give warning of unvisited code
  1519. // Remove it
  1520. cByteInstruction *curr = instr;
  1521. instr = instr->next;
  1522. DeleteInstruction(curr);
  1523. }
  1524. else
  1525. instr = instr->next;
  1526. }
  1527. }
  1528. #ifdef AS_DEBUG
  1529. void asCByteCode::DebugOutput(const char *name, asCScriptEngine *engine, asCScriptFunction *func)
  1530. {
  1531. _mkdir("AS_DEBUG");
  1532. asCString str = "AS_DEBUG/";
  1533. str += name;
  1534. #if _MSC_VER >= 1500
  1535. FILE *file;
  1536. fopen_s(&file, str.AddressOf(), "w");
  1537. #else
  1538. FILE *file = fopen(str.AddressOf(), "w");
  1539. #endif
  1540. #if !defined(AS_XENON) // XBox 360: When running in DVD Emu, no write is allowed
  1541. asASSERT( file );
  1542. #endif
  1543. if( file == 0 )
  1544. return;
  1545. asUINT n;
  1546. fprintf(file, "%s\n\n", func->GetDeclaration());
  1547. fprintf(file, "Temps: ");
  1548. for( n = 0; n < temporaryVariables.GetLength(); n++ )
  1549. {
  1550. fprintf(file, "%d", temporaryVariables[n]);
  1551. if( n < temporaryVariables.GetLength()-1 )
  1552. fprintf(file, ", ");
  1553. }
  1554. fprintf(file, "\n\n");
  1555. fprintf(file, "Variables: \n");
  1556. for( n = 0; n < func->variables.GetLength(); n++ )
  1557. {
  1558. fprintf(file, " %.3d: %s %s\n", func->variables[n]->stackOffset, func->variables[n]->type.Format().AddressOf(), func->variables[n]->name.AddressOf());
  1559. }
  1560. asUINT offset = 0;
  1561. if( func->objectType )
  1562. {
  1563. fprintf(file, " %.3d: %s this\n", 0, func->objectType->name.AddressOf());
  1564. offset -= AS_PTR_SIZE;
  1565. }
  1566. for( n = 0; n < func->parameterTypes.GetLength(); n++ )
  1567. {
  1568. bool found = false;
  1569. for( asUINT v = 0; v < func->variables.GetLength(); v++ )
  1570. {
  1571. if( func->variables[v]->stackOffset == (int)offset )
  1572. {
  1573. found = true;
  1574. break;
  1575. }
  1576. }
  1577. if( !found )
  1578. fprintf(file, " %.3d: %s {noname param}\n", offset, func->parameterTypes[n].Format().AddressOf());
  1579. offset -= func->parameterTypes[n].GetSizeOnStackDWords();
  1580. }
  1581. for( n = 0; n < func->objVariablePos.GetLength(); n++ )
  1582. {
  1583. bool found = false;
  1584. for( asUINT v = 0; v < func->variables.GetLength(); v++ )
  1585. {
  1586. if( func->variables[v]->stackOffset == func->objVariablePos[n] )
  1587. {
  1588. found = true;
  1589. break;
  1590. }
  1591. }
  1592. if( !found )
  1593. fprintf(file, " %.3d: %s {noname}\n", func->objVariablePos[n], func->objVariableTypes[n]->name.AddressOf());
  1594. }
  1595. fprintf(file, "\n\n");
  1596. int pos = 0;
  1597. asUINT lineIndex = 0;
  1598. cByteInstruction *instr = first;
  1599. while( instr )
  1600. {
  1601. if( lineIndex < lineNumbers.GetLength() && lineNumbers[lineIndex] == pos )
  1602. {
  1603. asDWORD line = lineNumbers[lineIndex+1];
  1604. fprintf(file, "- %d,%d -\n", (int)(line&0xFFFFF), (int)(line>>20));
  1605. lineIndex += 2;
  1606. }
  1607. fprintf(file, "%5d ", pos);
  1608. pos += instr->GetSize();
  1609. fprintf(file, "%3d %c ", instr->stackSize + func->variableSpace, instr->marked ? '*' : ' ');
  1610. switch( asBCInfo[instr->op].type )
  1611. {
  1612. case asBCTYPE_W_ARG:
  1613. if( instr->op == asBC_STR )
  1614. {
  1615. int id = instr->wArg[0];
  1616. const asCString &str = engine->GetConstantString(id);
  1617. fprintf(file, " %-8s %d (l:%ld s:\"%.10s\")\n", asBCInfo[instr->op].name, instr->wArg[0], (long int)str.GetLength(), str.AddressOf());
  1618. }
  1619. else
  1620. fprintf(file, " %-8s %d\n", asBCInfo[instr->op].name, instr->wArg[0]);
  1621. break;
  1622. case asBCTYPE_wW_ARG:
  1623. case asBCTYPE_rW_ARG:
  1624. fprintf(file, " %-8s v%d\n", asBCInfo[instr->op].name, instr->wArg[0]);
  1625. break;
  1626. case asBCTYPE_wW_rW_ARG:
  1627. case asBCTYPE_rW_rW_ARG:
  1628. fprintf(file, " %-8s v%d, v%d\n", asBCInfo[instr->op].name, instr->wArg[0], instr->wArg[1]);
  1629. break;
  1630. case asBCTYPE_wW_W_ARG:
  1631. fprintf(file, " %-8s v%d, %d\n", asBCInfo[instr->op].name, instr->wArg[0], instr->wArg[1]);
  1632. break;
  1633. case asBCTYPE_wW_rW_DW_ARG:
  1634. case asBCTYPE_rW_W_DW_ARG:
  1635. switch( instr->op )
  1636. {
  1637. case asBC_ADDIf:
  1638. case asBC_SUBIf:
  1639. case asBC_MULIf:
  1640. fprintf(file, " %-8s v%d, v%d, %f\n", asBCInfo[instr->op].name, instr->wArg[0], instr->wArg[1], *((float*) ARG_DW(instr->arg)));
  1641. break;
  1642. default:
  1643. fprintf(file, " %-8s v%d, v%d, %d\n", asBCInfo[instr->op].name, instr->wArg[0], instr->wArg[1], *((int*) ARG_DW(instr->arg)));
  1644. break;
  1645. }
  1646. break;
  1647. case asBCTYPE_DW_ARG:
  1648. switch( instr->op )
  1649. {
  1650. case asBC_OBJTYPE:
  1651. fprintf(file, " %-8s 0x%x\n", asBCInfo[instr->op].name, (asUINT)*ARG_DW(instr->arg));
  1652. break;
  1653. case asBC_PshC4:
  1654. case asBC_Cast:
  1655. fprintf(file, " %-8s 0x%x (i:%d, f:%g)\n", asBCInfo[instr->op].name, (asUINT)*ARG_DW(instr->arg), *((int*) ARG_DW(instr->arg)), *((float*) ARG_DW(instr->arg)));
  1656. break;
  1657. case asBC_TYPEID:
  1658. fprintf(file, " %-8s 0x%x '%s'\n", asBCInfo[instr->op].name, (asUINT)*ARG_DW(instr->arg), engine->GetTypeDeclaration((int)*ARG_DW(instr->arg)));
  1659. break;
  1660. case asBC_CALL:
  1661. case asBC_CALLSYS:
  1662. case asBC_CALLBND:
  1663. case asBC_CALLINTF:
  1664. {
  1665. int funcID = *(int*)ARG_DW(instr->arg);
  1666. asCString decl = engine->GetFunctionDeclaration(funcID);
  1667. fprintf(file, " %-8s %d (%s)\n", asBCInfo[instr->op].name, *((int*) ARG_DW(instr->arg)), decl.AddressOf());
  1668. }
  1669. break;
  1670. case asBC_REFCPY:
  1671. fprintf(file, " %-8s 0x%x\n", asBCInfo[instr->op].name, *((int*) ARG_DW(instr->arg)));
  1672. break;
  1673. case asBC_JMP:
  1674. case asBC_JZ:
  1675. case asBC_JLowZ:
  1676. case asBC_JS:
  1677. case asBC_JP:
  1678. case asBC_JNZ:
  1679. case asBC_JLowNZ:
  1680. case asBC_JNS:
  1681. case asBC_JNP:
  1682. fprintf(file, " %-8s %+d (d:%d)\n", asBCInfo[instr->op].name, *((int*) ARG_DW(instr->arg)), pos+*((int*) ARG_DW(instr->arg)));
  1683. break;
  1684. default:
  1685. fprintf(file, " %-8s %d\n", asBCInfo[instr->op].name, *((int*) ARG_DW(instr->arg)));
  1686. break;
  1687. }
  1688. break;
  1689. case asBCTYPE_QW_ARG:
  1690. #ifdef __GNUC__
  1691. #ifdef _LP64
  1692. fprintf(file, " %-8s 0x%lx (i:%ld, f:%g)\n", asBCInfo[instr->op].name, *ARG_QW(instr->arg), *((asINT64*) ARG_QW(instr->arg)), *((double*) ARG_QW(instr->arg)));
  1693. #else
  1694. fprintf(file, " %-8s 0x%llx (i:%lld, f:%g)\n", asBCInfo[instr->op].name, *ARG_QW(instr->arg), *((asINT64*) ARG_QW(instr->arg)), *((double*) ARG_QW(instr->arg)));
  1695. #endif
  1696. #else
  1697. fprintf(file, " %-8s 0x%I64x (i:%I64d, f:%g)\n", asBCInfo[instr->op].name, *ARG_QW(instr->arg), *((asINT64*) ARG_QW(instr->arg)), *((double*) ARG_QW(instr->arg)));
  1698. #endif
  1699. break;
  1700. case asBCTYPE_wW_QW_ARG:
  1701. case asBCTYPE_rW_QW_ARG:
  1702. #ifdef __GNUC__
  1703. #ifdef _LP64
  1704. fprintf(file, " %-8s v%d, 0x%lx (i:%ld, f:%g)\n", asBCInfo[instr->op].name, instr->wArg[0], *ARG_QW(instr->arg), *((asINT64*) ARG_QW(instr->arg)), *((double*) ARG_QW(instr->arg)));
  1705. #else
  1706. fprintf(file, " %-8s v%d, 0x%llx (i:%lld, f:%g)\n", asBCInfo[instr->op].name, instr->wArg[0], *ARG_QW(instr->arg), *((asINT64*) ARG_QW(instr->arg)), *((double*) ARG_QW(instr->arg)));
  1707. #endif
  1708. #else
  1709. fprintf(file, " %-8s v%d, 0x%I64x (i:%I64d, f:%g)\n", asBCInfo[instr->op].name, instr->wArg[0], *ARG_QW(instr->arg), *((asINT64*) ARG_QW(instr->arg)), *((double*) ARG_QW(instr->arg)));
  1710. #endif
  1711. break;
  1712. case asBCTYPE_DW_DW_ARG:
  1713. if( instr->op == asBC_ALLOC )
  1714. {
  1715. asCObjectType *ot = *(asCObjectType**)ARG_DW(instr->arg);
  1716. fprintf(file, " %-8s 0x%x, %d (type:%s)\n", asBCInfo[instr->op].name, *(int*)ARG_DW(instr->arg), *(int*)(ARG_DW(instr->arg)+1), ot->GetName());
  1717. }
  1718. else
  1719. fprintf(file, " %-8s %u, %d\n", asBCInfo[instr->op].name, *(int*)ARG_DW(instr->arg), *(int*)(ARG_DW(instr->arg)+1));
  1720. break;
  1721. case asBCTYPE_QW_DW_ARG:
  1722. if( instr->op == asBC_ALLOC )
  1723. {
  1724. asCObjectType *ot = *(asCObjectType**)ARG_QW(instr->arg);
  1725. #ifdef __GNUC__
  1726. #ifdef AS_64BIT_PTR
  1727. fprintf(file, " %-8s 0x%lx, %d (type:%s)\n", asBCInfo[instr->op].name, *(asINT64*)ARG_QW(instr->arg), *(int*)(ARG_DW(instr->arg)+2), ot->GetName());
  1728. #else
  1729. fprintf(file, " %-8s 0x%llx, %d (type:%s)\n", asBCInfo[instr->op].name, *(asINT64*)ARG_QW(instr->arg), *(int*)(ARG_DW(instr->arg)+2), ot->GetName());
  1730. #endif
  1731. #else
  1732. fprintf(file, " %-8s 0x%I64x, %d (type:%s)\n", asBCInfo[instr->op].name, *(asINT64*)ARG_QW(instr->arg), *(int*)(ARG_DW(instr->arg)+2), ot->GetName());
  1733. #endif
  1734. }
  1735. else
  1736. #ifdef __GNUC__
  1737. #ifdef AS_64BIT_PTR
  1738. fprintf(file, " %-8s %lu, %d\n", asBCInfo[instr->op].name, *(asINT64*)ARG_QW(instr->arg), *(int*)(ARG_DW(instr->arg)+2));
  1739. #else
  1740. fprintf(file, " %-8s %llu, %d\n", asBCInfo[instr->op].name, *(asINT64*)ARG_QW(instr->arg), *(int*)(ARG_DW(instr->arg)+2));
  1741. #endif
  1742. #else
  1743. fprintf(file, " %-8s %I64u, %d\n", asBCInfo[instr->op].name, *(asINT64*)ARG_QW(instr->arg), *(int*)(ARG_DW(instr->arg)+2));
  1744. #endif
  1745. break;
  1746. case asBCTYPE_INFO:
  1747. if( instr->op == asBC_LABEL )
  1748. fprintf(file, "%d:\n", instr->wArg[0]);
  1749. else if( instr->op == asBC_LINE )
  1750. fprintf(file, " %s\n", asBCInfo[instr->op].name);
  1751. else if( instr->op == asBC_Block )
  1752. fprintf(file, "%c\n", instr->wArg[0] ? '{' : '}');
  1753. break;
  1754. case asBCTYPE_rW_DW_ARG:
  1755. case asBCTYPE_wW_DW_ARG:
  1756. case asBCTYPE_W_DW_ARG:
  1757. if( instr->op == asBC_SetV1 )
  1758. fprintf(file, " %-8s v%d, 0x%x\n", asBCInfo[instr->op].name, instr->wArg[0], *(asBYTE*)ARG_DW(instr->arg));
  1759. else if( instr->op == asBC_SetV2 )
  1760. fprintf(file, " %-8s v%d, 0x%x\n", asBCInfo[instr->op].name, instr->wArg[0], *(asWORD*)ARG_DW(instr->arg));
  1761. else if( instr->op == asBC_SetV4 )
  1762. fprintf(file, " %-8s v%d, 0x%x (i:%d, f:%g)\n", asBCInfo[instr->op].name, instr->wArg[0], (asUINT)*ARG_DW(instr->arg), *((int*) ARG_DW(instr->arg)), *((float*) ARG_DW(instr->arg)));
  1763. else if( instr->op == asBC_CMPIf )
  1764. fprintf(file, " %-8s v%d, %f\n", asBCInfo[instr->op].name, instr->wArg[0], *(float*)ARG_DW(instr->arg));
  1765. else
  1766. fprintf(file, " %-8s v%d, %d\n", asBCInfo[instr->op].name, instr->wArg[0], (asUINT)*ARG_DW(instr->arg));
  1767. break;
  1768. case asBCTYPE_wW_rW_rW_ARG:
  1769. fprintf(file, " %-8s v%d, v%d, v%d\n", asBCInfo[instr->op].name, instr->wArg[0], instr->wArg[1], instr->wArg[2]);
  1770. break;
  1771. case asBCTYPE_NO_ARG:
  1772. fprintf(file, " %s\n", asBCInfo[instr->op].name);
  1773. break;
  1774. default:
  1775. asASSERT(false);
  1776. }
  1777. instr = instr->next;
  1778. }
  1779. fclose(file);
  1780. }
  1781. #endif
  1782. //=============================================================================
  1783. int asCByteCode::InsertFirstInstrDWORD(asEBCInstr bc, asDWORD param)
  1784. {
  1785. asASSERT(asBCInfo[bc].type == asBCTYPE_DW_ARG);
  1786. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  1787. if( AddInstructionFirst() < 0 )
  1788. return 0;
  1789. first->op = bc;
  1790. *ARG_DW(first->arg) = param;
  1791. first->size = asBCTypeSize[asBCInfo[bc].type];
  1792. first->stackInc = asBCInfo[bc].stackInc;
  1793. return first->stackInc;
  1794. }
  1795. int asCByteCode::InsertFirstInstrQWORD(asEBCInstr bc, asQWORD param)
  1796. {
  1797. asASSERT(asBCInfo[bc].type == asBCTYPE_QW_ARG);
  1798. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  1799. if( AddInstructionFirst() < 0 )
  1800. return 0;
  1801. first->op = bc;
  1802. *ARG_QW(first->arg) = param;
  1803. first->size = asBCTypeSize[asBCInfo[bc].type];
  1804. first->stackInc = asBCInfo[bc].stackInc;
  1805. return first->stackInc;
  1806. }
  1807. int asCByteCode::Instr(asEBCInstr bc)
  1808. {
  1809. asASSERT(asBCInfo[bc].type == asBCTYPE_NO_ARG);
  1810. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  1811. if( AddInstruction() < 0 )
  1812. return 0;
  1813. last->op = bc;
  1814. last->size = asBCTypeSize[asBCInfo[bc].type];
  1815. last->stackInc = asBCInfo[bc].stackInc;
  1816. return last->stackInc;
  1817. }
  1818. int asCByteCode::InstrW_W_W(asEBCInstr bc, int a, int b, int c)
  1819. {
  1820. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_rW_rW_ARG);
  1821. asASSERT(asBCInfo[bc].stackInc == 0);
  1822. if( AddInstruction() < 0 )
  1823. return 0;
  1824. last->op = bc;
  1825. last->wArg[0] = (short)a;
  1826. last->wArg[1] = (short)b;
  1827. last->wArg[2] = (short)c;
  1828. last->size = asBCTypeSize[asBCInfo[bc].type];
  1829. last->stackInc = asBCInfo[bc].stackInc;
  1830. return last->stackInc;
  1831. }
  1832. int asCByteCode::InstrW_W(asEBCInstr bc, int a, int b)
  1833. {
  1834. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_rW_ARG ||
  1835. asBCInfo[bc].type == asBCTYPE_rW_rW_ARG);
  1836. asASSERT(asBCInfo[bc].stackInc == 0);
  1837. if( AddInstruction() < 0 )
  1838. return 0;
  1839. last->op = bc;
  1840. last->wArg[0] = (short)a;
  1841. last->wArg[1] = (short)b;
  1842. last->size = asBCTypeSize[asBCInfo[bc].type];
  1843. last->stackInc = asBCInfo[bc].stackInc;
  1844. return last->stackInc;
  1845. }
  1846. int asCByteCode::InstrW_PTR(asEBCInstr bc, short a, void *param)
  1847. {
  1848. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_PTR_ARG);
  1849. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  1850. if( AddInstruction() < 0 )
  1851. return 0;
  1852. last->op = bc;
  1853. last->wArg[0] = a;
  1854. *ARG_PTR(last->arg) = (asPWORD)param;
  1855. last->size = asBCTypeSize[asBCInfo[bc].type];
  1856. last->stackInc = asBCInfo[bc].stackInc;
  1857. return last->stackInc;
  1858. }
  1859. int asCByteCode::InstrW_DW(asEBCInstr bc, asWORD a, asDWORD b)
  1860. {
  1861. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_DW_ARG ||
  1862. asBCInfo[bc].type == asBCTYPE_rW_DW_ARG ||
  1863. asBCInfo[bc].type == asBCTYPE_W_DW_ARG);
  1864. asASSERT(asBCInfo[bc].stackInc == 0);
  1865. if( AddInstruction() < 0 )
  1866. return 0;
  1867. last->op = bc;
  1868. last->wArg[0] = a;
  1869. *((int*) ARG_DW(last->arg)) = b;
  1870. last->size = asBCTypeSize[asBCInfo[bc].type];
  1871. last->stackInc = asBCInfo[bc].stackInc;
  1872. return last->stackInc;
  1873. }
  1874. int asCByteCode::InstrSHORT_B(asEBCInstr bc, short a, asBYTE b)
  1875. {
  1876. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_DW_ARG ||
  1877. asBCInfo[bc].type == asBCTYPE_rW_DW_ARG ||
  1878. asBCInfo[bc].type == asBCTYPE_W_DW_ARG);
  1879. asASSERT(asBCInfo[bc].stackInc == 0);
  1880. if( AddInstruction() < 0 )
  1881. return 0;
  1882. last->op = bc;
  1883. last->wArg[0] = a;
  1884. // We'll have to be careful to store the byte correctly, independent of endianess.
  1885. // Some optimizing compilers may change the order of operations, so we make sure
  1886. // the value is not overwritten even if that happens.
  1887. asBYTE *argPtr = (asBYTE*)ARG_DW(last->arg);
  1888. argPtr[0] = b; // The value is always stored in the lower byte
  1889. argPtr[1] = 0; // and clear the rest of the DWORD
  1890. argPtr[2] = 0;
  1891. argPtr[3] = 0;
  1892. last->size = asBCTypeSize[asBCInfo[bc].type];
  1893. last->stackInc = asBCInfo[bc].stackInc;
  1894. return last->stackInc;
  1895. }
  1896. int asCByteCode::InstrSHORT_W(asEBCInstr bc, short a, asWORD b)
  1897. {
  1898. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_DW_ARG ||
  1899. asBCInfo[bc].type == asBCTYPE_rW_DW_ARG ||
  1900. asBCInfo[bc].type == asBCTYPE_W_DW_ARG);
  1901. asASSERT(asBCInfo[bc].stackInc == 0);
  1902. if( AddInstruction() < 0 )
  1903. return 0;
  1904. last->op = bc;
  1905. last->wArg[0] = a;
  1906. // We'll have to be careful to store the word correctly, independent of endianess.
  1907. // Some optimizing compilers may change the order of operations, so we make sure
  1908. // the value is not overwritten even if that happens.
  1909. asWORD *argPtr = (asWORD*)ARG_DW(last->arg);
  1910. argPtr[0] = b; // The value is always stored in the lower word
  1911. argPtr[1] = 0; // and clear the rest of the DWORD
  1912. last->size = asBCTypeSize[asBCInfo[bc].type];
  1913. last->stackInc = asBCInfo[bc].stackInc;
  1914. return last->stackInc;
  1915. }
  1916. int asCByteCode::InstrSHORT_DW(asEBCInstr bc, short a, asDWORD b)
  1917. {
  1918. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_DW_ARG ||
  1919. asBCInfo[bc].type == asBCTYPE_rW_DW_ARG ||
  1920. asBCInfo[bc].type == asBCTYPE_W_DW_ARG);
  1921. if( AddInstruction() < 0 )
  1922. return 0;
  1923. last->op = bc;
  1924. last->wArg[0] = a;
  1925. *((int*) ARG_DW(last->arg)) = b;
  1926. last->size = asBCTypeSize[asBCInfo[bc].type];
  1927. last->stackInc = asBCInfo[bc].stackInc;
  1928. return last->stackInc;
  1929. }
  1930. int asCByteCode::InstrW_QW(asEBCInstr bc, asWORD a, asQWORD b)
  1931. {
  1932. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_QW_ARG);
  1933. asASSERT(asBCInfo[bc].stackInc == 0);
  1934. if( AddInstruction() < 0 )
  1935. return 0;
  1936. last->op = bc;
  1937. last->wArg[0] = a;
  1938. *ARG_QW(last->arg) = b;
  1939. last->size = asBCTypeSize[asBCInfo[bc].type];
  1940. last->stackInc = asBCInfo[bc].stackInc;
  1941. return last->stackInc;
  1942. }
  1943. int asCByteCode::InstrSHORT_QW(asEBCInstr bc, short a, asQWORD b)
  1944. {
  1945. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_QW_ARG);
  1946. asASSERT(asBCInfo[bc].stackInc == 0);
  1947. if( AddInstruction() < 0 )
  1948. return 0;
  1949. last->op = bc;
  1950. last->wArg[0] = a;
  1951. *ARG_QW(last->arg) = b;
  1952. last->size = asBCTypeSize[asBCInfo[bc].type];
  1953. last->stackInc = asBCInfo[bc].stackInc;
  1954. return last->stackInc;
  1955. }
  1956. int asCByteCode::InstrW_FLOAT(asEBCInstr bc, asWORD a, float b)
  1957. {
  1958. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_DW_ARG);
  1959. asASSERT(asBCInfo[bc].stackInc == 0);
  1960. if( AddInstruction() < 0 )
  1961. return 0;
  1962. last->op = bc;
  1963. last->wArg[0] = a;
  1964. *((float*) ARG_DW(last->arg)) = b;
  1965. last->size = asBCTypeSize[asBCInfo[bc].type];
  1966. last->stackInc = asBCInfo[bc].stackInc;
  1967. return last->stackInc;
  1968. }
  1969. int asCByteCode::InstrSHORT(asEBCInstr bc, short param)
  1970. {
  1971. asASSERT(asBCInfo[bc].type == asBCTYPE_rW_ARG ||
  1972. asBCInfo[bc].type == asBCTYPE_wW_ARG ||
  1973. asBCInfo[bc].type == asBCTYPE_W_ARG);
  1974. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  1975. if( AddInstruction() < 0 )
  1976. return 0;
  1977. last->op = bc;
  1978. last->wArg[0] = param;
  1979. last->size = asBCTypeSize[asBCInfo[bc].type];
  1980. last->stackInc = asBCInfo[bc].stackInc;
  1981. return last->stackInc;
  1982. }
  1983. int asCByteCode::InstrINT(asEBCInstr bc, int param)
  1984. {
  1985. asASSERT(asBCInfo[bc].type == asBCTYPE_DW_ARG);
  1986. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  1987. if( AddInstruction() < 0 )
  1988. return 0;
  1989. last->op = bc;
  1990. *((int*) ARG_DW(last->arg)) = param;
  1991. last->size = asBCTypeSize[asBCInfo[bc].type];
  1992. last->stackInc = asBCInfo[bc].stackInc;
  1993. return last->stackInc;
  1994. }
  1995. int asCByteCode::InstrDWORD(asEBCInstr bc, asDWORD param)
  1996. {
  1997. asASSERT(asBCInfo[bc].type == asBCTYPE_DW_ARG);
  1998. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  1999. if( AddInstruction() < 0 )
  2000. return 0;
  2001. last->op = bc;
  2002. *ARG_DW(last->arg) = param;
  2003. last->size = asBCTypeSize[asBCInfo[bc].type];
  2004. last->stackInc = asBCInfo[bc].stackInc;
  2005. return last->stackInc;
  2006. }
  2007. int asCByteCode::InstrPTR(asEBCInstr bc, void *param)
  2008. {
  2009. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2010. if( AddInstruction() < 0 )
  2011. return 0;
  2012. last->op = bc;
  2013. asASSERT(asBCInfo[bc].type == asBCTYPE_PTR_ARG);
  2014. *ARG_PTR(last->arg) = (asPWORD)param;
  2015. last->size = asBCTypeSize[asBCInfo[bc].type];
  2016. last->stackInc = asBCInfo[bc].stackInc;
  2017. return last->stackInc;
  2018. }
  2019. int asCByteCode::InstrQWORD(asEBCInstr bc, asQWORD param)
  2020. {
  2021. asASSERT(asBCInfo[bc].type == asBCTYPE_QW_ARG);
  2022. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2023. if( AddInstruction() < 0 )
  2024. return 0;
  2025. last->op = bc;
  2026. *ARG_QW(last->arg) = param;
  2027. last->size = asBCTypeSize[asBCInfo[bc].type];
  2028. last->stackInc = asBCInfo[bc].stackInc;
  2029. return last->stackInc;
  2030. }
  2031. int asCByteCode::InstrWORD(asEBCInstr bc, asWORD param)
  2032. {
  2033. asASSERT(asBCInfo[bc].type == asBCTYPE_W_ARG ||
  2034. asBCInfo[bc].type == asBCTYPE_rW_ARG ||
  2035. asBCInfo[bc].type == asBCTYPE_wW_ARG);
  2036. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2037. if( AddInstruction() < 0 )
  2038. return 0;
  2039. last->op = bc;
  2040. last->wArg[0] = param;
  2041. last->size = asBCTypeSize[asBCInfo[bc].type];
  2042. last->stackInc = asBCInfo[bc].stackInc;
  2043. return last->stackInc;
  2044. }
  2045. int asCByteCode::InstrFLOAT(asEBCInstr bc, float param)
  2046. {
  2047. asASSERT(asBCInfo[bc].type == asBCTYPE_DW_ARG);
  2048. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2049. if( AddInstruction() < 0 )
  2050. return 0;
  2051. last->op = bc;
  2052. *((float*) ARG_DW(last->arg)) = param;
  2053. last->size = asBCTypeSize[asBCInfo[bc].type];
  2054. last->stackInc = asBCInfo[bc].stackInc;
  2055. return last->stackInc;
  2056. }
  2057. int asCByteCode::InstrDOUBLE(asEBCInstr bc, double param)
  2058. {
  2059. asASSERT(asBCInfo[bc].type == asBCTYPE_QW_ARG);
  2060. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2061. if( AddInstruction() < 0 )
  2062. return 0;
  2063. last->op = bc;
  2064. *((double*) ARG_QW(last->arg)) = param;
  2065. last->size = asBCTypeSize[asBCInfo[bc].type];
  2066. last->stackInc = asBCInfo[bc].stackInc;
  2067. return last->stackInc;
  2068. }
  2069. int asCByteCode::GetLastInstr()
  2070. {
  2071. if( last == 0 ) return -1;
  2072. return last->op;
  2073. }
  2074. int asCByteCode::RemoveLastInstr()
  2075. {
  2076. if( last == 0 ) return -1;
  2077. if( first == last )
  2078. {
  2079. engine->memoryMgr.FreeByteInstruction(last);
  2080. first = 0;
  2081. last = 0;
  2082. }
  2083. else
  2084. {
  2085. cByteInstruction *bc = last;
  2086. last = bc->prev;
  2087. bc->Remove();
  2088. engine->memoryMgr.FreeByteInstruction(bc);
  2089. }
  2090. return 0;
  2091. }
  2092. asDWORD asCByteCode::GetLastInstrValueDW()
  2093. {
  2094. if( last == 0 ) return 0;
  2095. return *ARG_DW(last->arg);
  2096. }
  2097. void asCByteCode::DefineTemporaryVariable(int varOffset)
  2098. {
  2099. temporaryVariables.PushLast(varOffset);
  2100. }
  2101. //===================================================================
  2102. cByteInstruction::cByteInstruction()
  2103. {
  2104. next = 0;
  2105. prev = 0;
  2106. op = asBC_LABEL;
  2107. arg = 0;
  2108. wArg[0] = 0;
  2109. wArg[1] = 0;
  2110. wArg[2] = 0;
  2111. size = 0;
  2112. stackInc = 0;
  2113. marked = false;
  2114. stackSize = 0;
  2115. }
  2116. void cByteInstruction::AddAfter(cByteInstruction *nextCode)
  2117. {
  2118. if( next )
  2119. next->prev = nextCode;
  2120. nextCode->next = next;
  2121. nextCode->prev = this;
  2122. next = nextCode;
  2123. }
  2124. void cByteInstruction::AddBefore(cByteInstruction *prevCode)
  2125. {
  2126. if( prev )
  2127. prev->next = prevCode;
  2128. prevCode->prev = prev;
  2129. prevCode->next = this;
  2130. prev = prevCode;
  2131. }
  2132. int cByteInstruction::GetSize()
  2133. {
  2134. return size;
  2135. }
  2136. int cByteInstruction::GetStackIncrease()
  2137. {
  2138. return stackInc;
  2139. }
  2140. void cByteInstruction::Remove()
  2141. {
  2142. if( prev ) prev->next = next;
  2143. if( next ) next->prev = prev;
  2144. prev = 0;
  2145. next = 0;
  2146. }
  2147. END_AS_NAMESPACE
  2148. #endif // AS_NO_COMPILER