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: 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: 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: 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: optimize: Need a bytecode BC_AddRef so that BC_CALLSYS doesn't have to be used for this trivial call
  509. // TODO: optimize: A bytecode BC_RefCpyV that copies a handle from a local variable to another local variable
  510. // can easily substitute the frequently appearing pattern BC_PshV4, BC_PSF, BC_REFCPY, BC_POP
  511. // TODO: optimize: Script class methods are currently implemented to increase the ref count of the object upon
  512. // entry, and then release it upon exit. When the method isn't doing anything at all, this is
  513. // not necessary, as the function could simply do a RET immediately. This optimization is only
  514. // possible if the code has been built without the line cues, as if the SUSPEND is within the
  515. // function, then we can't do this optimization. Of course, this optimization may not be all
  516. // that useful, since in a real world app, it is probably not very common that empty class
  517. // methods are called.
  518. cByteInstruction *instr = first;
  519. while( instr )
  520. {
  521. cByteInstruction *curr = instr;
  522. instr = instr->next;
  523. // Remove or combine instructions
  524. if( RemoveUnusedValue(curr, &instr) ) continue;
  525. // Postpone initializations so that they may be combined in the second pass
  526. if( PostponeInitOfTemp(curr, &instr) ) continue;
  527. // XXX x, YYY y, SwapPtr -> YYY y, XXX x
  528. if( CanBeSwapped(curr) )
  529. {
  530. // Delete SwapPtr
  531. DeleteInstruction(instr->next);
  532. // Swap instructions
  533. RemoveInstruction(instr);
  534. InsertBefore(curr, instr);
  535. instr = GoBack(instr);
  536. }
  537. // T??, ClrHi -> T??
  538. else if( IsCombination(curr, asBC_TZ, asBC_ClrHi) ||
  539. IsCombination(curr, asBC_TNZ, asBC_ClrHi) ||
  540. IsCombination(curr, asBC_TS, asBC_ClrHi) ||
  541. IsCombination(curr, asBC_TNS, asBC_ClrHi) ||
  542. IsCombination(curr, asBC_TP, asBC_ClrHi) ||
  543. IsCombination(curr, asBC_TNP, asBC_ClrHi) )
  544. {
  545. // Remove the ClrHi instruction, since the test instructions always clear the top bytes anyway
  546. DeleteInstruction(instr);
  547. instr = GoBack(curr);
  548. }
  549. // PshVPtr 0, ADDSi, PopRPtr -> LoadThisR
  550. else if( IsCombination(curr, asBC_PshVPtr, asBC_ADDSi) &&
  551. IsCombination(instr, asBC_ADDSi, asBC_PopRPtr) &&
  552. curr->wArg[0] == 0 )
  553. {
  554. DeleteInstruction(curr);
  555. instr = GoBack(ChangeFirstDeleteNext(instr, asBC_LoadThisR));
  556. }
  557. // TODO: Optimize: PshVPtr x, PopRPtr -> LoadRObjR x, 0
  558. // PshVPtr x, ADDSi, PopRPtr -> LoadRObjR
  559. else if( IsCombination(curr, asBC_PshVPtr, asBC_ADDSi) &&
  560. IsCombination(instr, asBC_ADDSi, asBC_PopRPtr) &&
  561. curr->wArg[0] != 0 )
  562. {
  563. curr->op = asBC_LoadRObjR;
  564. curr->size = asBCTypeSize[asBCInfo[asBC_LoadRObjR].type];
  565. curr->stackInc = asBCInfo[asBC_LoadRObjR].stackInc;
  566. curr->wArg[1] = instr->wArg[0];
  567. *(asDWORD*)&curr->arg = *(asDWORD*)&instr->arg;
  568. DeleteInstruction(instr->next);
  569. DeleteInstruction(instr);
  570. instr = GoBack(curr);
  571. }
  572. // PSF x, ADDSi, PopRPtr -> LoadVObjR
  573. else if( IsCombination(curr, asBC_PSF, asBC_ADDSi) &&
  574. IsCombination(instr, asBC_ADDSi, asBC_PopRPtr) )
  575. {
  576. curr->op = asBC_LoadVObjR;
  577. curr->size = asBCTypeSize[asBCInfo[asBC_LoadVObjR].type];
  578. curr->stackInc = asBCInfo[asBC_LoadVObjR].stackInc;
  579. curr->wArg[1] = instr->wArg[0];
  580. *(asDWORD*)&curr->arg = *(asDWORD*)&instr->arg;
  581. DeleteInstruction(instr->next);
  582. DeleteInstruction(instr);
  583. instr = GoBack(curr);
  584. }
  585. // LDG x, WRTV4 y -> CpyVtoG4 y, x
  586. else if( IsCombination(curr, asBC_LDG, asBC_WRTV4) && !IsTempRegUsed(instr) )
  587. {
  588. curr->op = asBC_CpyVtoG4;
  589. curr->size = asBCTypeSize[asBCInfo[asBC_CpyVtoG4].type];
  590. curr->wArg[0] = instr->wArg[0];
  591. DeleteInstruction(instr);
  592. instr = GoBack(curr);
  593. }
  594. // LDG x, RDR4 y -> CpyGtoV4 y, x
  595. else if( IsCombination(curr, asBC_LDG, asBC_RDR4) )
  596. {
  597. if( !IsTempRegUsed(instr) )
  598. curr->op = asBC_CpyGtoV4;
  599. else
  600. curr->op = asBC_LdGRdR4;
  601. curr->size = asBCTypeSize[asBCInfo[asBC_CpyGtoV4].type];
  602. curr->wArg[0] = instr->wArg[0];
  603. DeleteInstruction(instr);
  604. instr = GoBack(curr);
  605. }
  606. // LDV x, INCi -> IncVi x
  607. else if( IsCombination(curr, asBC_LDV, asBC_INCi) && !IsTempRegUsed(instr) )
  608. {
  609. curr->op = asBC_IncVi;
  610. DeleteInstruction(instr);
  611. instr = GoBack(curr);
  612. }
  613. // LDV x, DECi -> DecVi x
  614. else if( IsCombination(curr, asBC_LDV, asBC_DECi) && !IsTempRegUsed(instr) )
  615. {
  616. curr->op = asBC_DecVi;
  617. DeleteInstruction(instr);
  618. instr = GoBack(curr);
  619. }
  620. // POP a, RET b -> RET b
  621. else if( IsCombination(curr, asBC_POP, asBC_RET) )
  622. {
  623. // We don't combine the POP+RET because RET first restores
  624. // the previous stack pointer and then pops the arguments
  625. // Delete POP
  626. instr = GoBack(DeleteInstruction(curr));
  627. }
  628. // Delete JitEntry if the JIT instructions are not supposed to be included
  629. else if( curr->op == asBC_JitEntry && !engine->ep.includeJitInstructions )
  630. {
  631. instr = GoBack(DeleteInstruction(curr));
  632. }
  633. // SUSPEND, JitEntry, SUSPEND -> SUSPEND
  634. // LINE, JitEntry, LINE -> LINE
  635. else if( (IsCombination(curr, asBC_SUSPEND, asBC_JitEntry) && IsCombination(instr, asBC_JitEntry, asBC_SUSPEND)) ||
  636. (IsCombination(curr, asBC_LINE, asBC_JitEntry) && IsCombination(instr, asBC_JitEntry, asBC_LINE)) )
  637. {
  638. // Delete the two first instructions
  639. DeleteInstruction(instr);
  640. instr = GoBack(DeleteInstruction(curr));
  641. }
  642. // SUSPEND, SUSPEND -> SUSPEND
  643. // LINE, LINE -> LINE
  644. else if( IsCombination(curr, asBC_SUSPEND, asBC_SUSPEND) ||
  645. IsCombination(curr, asBC_LINE, asBC_LINE) )
  646. {
  647. // Delete the first instruction
  648. instr = GoBack(DeleteInstruction(curr));
  649. }
  650. // SUSPEND, Block, SUSPEND -> Block, SUSPEND
  651. else if( (IsCombination(curr, asBC_SUSPEND, asBC_Block) && IsCombination(instr, asBC_Block, asBC_SUSPEND)) ||
  652. (IsCombination(curr, asBC_LINE, asBC_Block) && IsCombination(instr, asBC_Block, asBC_LINE)) )
  653. {
  654. // Delete the first instruction
  655. instr = GoBack(DeleteInstruction(curr));
  656. }
  657. // PUSH a, PUSH b -> PUSH a+b
  658. else if( IsCombination(curr, asBC_PUSH, asBC_PUSH) )
  659. {
  660. // Combine the two PUSH
  661. instr->wArg[0] = curr->wArg[0] + instr->wArg[0];
  662. // Delete current
  663. DeleteInstruction(curr);
  664. // Continue with the instruction before the one removed
  665. instr = GoBack(instr);
  666. }
  667. // VAR a, GETREF 0 -> PSF a
  668. else if( IsCombination(curr, asBC_VAR, asBC_GETREF) && instr->wArg[0] == 0 )
  669. {
  670. instr = GoBack(ChangeFirstDeleteNext(curr, asBC_PSF));
  671. }
  672. // PGA, CHKREF -> PGA
  673. // PSF, CHKREF -> PSF
  674. else if( IsCombination(curr, asBC_PGA, asBC_CHKREF) ||
  675. IsCombination(curr, asBC_PSF, asBC_CHKREF) )
  676. {
  677. // Delete CHKREF since PGA and PSF always pushes a valid address on the stack
  678. DeleteInstruction(instr);
  679. instr = GoBack(curr);
  680. }
  681. // PGA, ChkRefS, CHKREF -> PGA, ChkRefS
  682. else if( IsCombination(curr, asBC_PGA, asBC_ChkRefS) &&
  683. IsCombination(instr, asBC_ChkRefS, asBC_CHKREF) )
  684. {
  685. // Delete CHKREF since PGA always pushes a valid address on the stack
  686. DeleteInstruction(instr->next);
  687. instr = GoBack(curr);
  688. }
  689. // PSF, FREE -> FREE, PSF
  690. else if( IsCombination(curr, asBC_PSF, asBC_FREE) )
  691. {
  692. // This pattern usually happens when a function returns an object, or handle
  693. // and then releases a temporary variable, possibly used in one of the arguments.
  694. // By swapping the order of these instructions, the code can be further optimized
  695. // to combine the PSF with the following instructions
  696. RemoveInstruction(instr);
  697. InsertBefore(curr, instr);
  698. instr = GoBack(instr);
  699. }
  700. // PshV4 y, POP x -> POP x-1
  701. // PshC4 y, POP x -> POP x-1
  702. else if( (IsCombination(curr, asBC_PshV4, asBC_POP) ||
  703. IsCombination(curr, asBC_PshC4, asBC_POP)) && instr->wArg[0] >= 1 )
  704. {
  705. DeleteInstruction(curr);
  706. instr->wArg[0]--;
  707. instr = GoBack(instr);
  708. }
  709. // PshV8 y, POP x -> POP x-2
  710. // PshC8 y, POP x -> POP x-2
  711. else if( (IsCombination(curr, asBC_PshV8, asBC_POP) ||
  712. IsCombination(curr, asBC_PshC8, asBC_POP)) && instr->wArg[0] >= 2 )
  713. {
  714. DeleteInstruction(curr);
  715. instr->wArg[0] -= 2;
  716. instr = GoBack(instr);
  717. }
  718. // PshVPtr y, POP x -> POP x-AS_PTR_SIZE
  719. // PSF y , POP x -> POP x-AS_PTR_SIZE
  720. // VAR y , POP x -> POP x-AS_PTR_SIZE
  721. // PshNull , POP x -> POP x-AS_PTR_SIZE
  722. // PshRPtr , POP x -> POP x-AS_PTR_SIZE
  723. else if( (IsCombination(curr, asBC_PshRPtr, asBC_POP) ||
  724. IsCombination(curr, asBC_PSF , asBC_POP) ||
  725. IsCombination(curr, asBC_VAR , asBC_POP) ||
  726. IsCombination(curr, asBC_PshVPtr, asBC_POP) ||
  727. IsCombination(curr, asBC_PshNull, asBC_POP)) &&
  728. instr->wArg[0] >= AS_PTR_SIZE )
  729. {
  730. // A pointer is pushed on the stack then immediately removed
  731. DeleteInstruction(curr);
  732. instr->wArg[0] -= AS_PTR_SIZE;
  733. instr = GoBack(instr);
  734. }
  735. // POP 0 -> remove
  736. // PUSH 0 -> remove
  737. else if( (curr->op == asBC_POP || curr->op == asBC_PUSH ) && curr->wArg[0] == 0 )
  738. instr = GoBack(DeleteInstruction(curr));
  739. // Begin PATTERN
  740. // T**; J** +x -> J** +x
  741. else if( IsCombination(curr, asBC_TZ , asBC_JZ ) ||
  742. IsCombination(curr, asBC_TNZ, asBC_JNZ) )
  743. instr = GoBack(DeleteFirstChangeNext(curr, asBC_JNZ));
  744. else if( IsCombination(curr, asBC_TNZ, asBC_JZ ) ||
  745. IsCombination(curr, asBC_TZ , asBC_JNZ) )
  746. instr = GoBack(DeleteFirstChangeNext(curr, asBC_JZ));
  747. else if( IsCombination(curr, asBC_TS , asBC_JZ ) ||
  748. IsCombination(curr, asBC_TNS, asBC_JNZ) )
  749. instr = GoBack(DeleteFirstChangeNext(curr, asBC_JNS));
  750. else if( IsCombination(curr, asBC_TNS, asBC_JZ ) ||
  751. IsCombination(curr, asBC_TS , asBC_JNZ) )
  752. instr = GoBack(DeleteFirstChangeNext(curr, asBC_JS));
  753. else if( IsCombination(curr, asBC_TP , asBC_JZ ) ||
  754. IsCombination(curr, asBC_TNP, asBC_JNZ) )
  755. instr = GoBack(DeleteFirstChangeNext(curr, asBC_JNP));
  756. else if( IsCombination(curr, asBC_TNP, asBC_JZ ) ||
  757. IsCombination(curr, asBC_TP , asBC_JNZ) )
  758. instr = GoBack(DeleteFirstChangeNext(curr, asBC_JP));
  759. // End PATTERN
  760. // JMP +0 -> remove
  761. else if( IsCombination(curr, asBC_JMP, asBC_LABEL) && *(int*)&curr->arg == instr->wArg[0] )
  762. instr = GoBack(DeleteInstruction(curr));
  763. // PSF, RDSPtr -> PshVPtr
  764. else if( IsCombination(curr, asBC_PSF, asBC_RDSPtr) )
  765. {
  766. curr->op = asBC_PshVPtr;
  767. DeleteInstruction(instr);
  768. instr = GoBack(curr);
  769. }
  770. // PSF, ChkRefS, RDSPtr -> PshVPtr, CHKREF
  771. else if( IsCombination(curr, asBC_PSF, asBC_ChkRefS) &&
  772. IsCombination(instr, asBC_ChkRefS, asBC_RDSPtr) )
  773. {
  774. curr->op = asBC_PshVPtr;
  775. instr->op = asBC_CHKREF;
  776. DeleteInstruction(instr->next);
  777. instr = GoBack(curr);
  778. }
  779. // PSF, ChkRefS, POP -> ChkNullV
  780. else if( (IsCombination(curr, asBC_PSF, asBC_ChkRefS) &&
  781. IsCombination(instr, asBC_ChkRefS, asBC_POP) &&
  782. instr->next->wArg[0] >= AS_PTR_SIZE) )
  783. {
  784. curr->op = asBC_ChkNullV;
  785. curr->stackInc = 0;
  786. // Decrease the number of DWORDs popped
  787. instr->next->wArg[0] -= AS_PTR_SIZE;
  788. // Delete the ChkRefS instruction
  789. DeleteInstruction(instr);
  790. instr = GoBack(curr);
  791. }
  792. // PshVPtr, CHKREF, POP -> ChkNullV
  793. else if( (IsCombination(curr, asBC_PshVPtr, asBC_CHKREF) &&
  794. IsCombination(instr, asBC_CHKREF, asBC_POP) &&
  795. instr->next->wArg[0] == AS_PTR_SIZE) )
  796. {
  797. curr->op = asBC_ChkNullV;
  798. curr->stackInc = 0;
  799. DeleteInstruction(instr->next);
  800. DeleteInstruction(instr);
  801. instr = GoBack(curr);
  802. }
  803. }
  804. return 0;
  805. }
  806. bool asCByteCode::IsTempVarReadByInstr(cByteInstruction *curr, int offset)
  807. {
  808. // Which instructions read from variables?
  809. if( asBCInfo[curr->op].type == asBCTYPE_wW_rW_rW_ARG &&
  810. (curr->wArg[1] == offset || curr->wArg[2] == offset) )
  811. return true;
  812. else if( (asBCInfo[curr->op].type == asBCTYPE_rW_ARG ||
  813. asBCInfo[curr->op].type == asBCTYPE_rW_DW_ARG ||
  814. asBCInfo[curr->op].type == asBCTYPE_rW_QW_ARG ||
  815. asBCInfo[curr->op].type == asBCTYPE_rW_W_DW_ARG ||
  816. curr->op == asBC_FREE) && // FREE both read and write to the variable
  817. curr->wArg[0] == offset )
  818. return true;
  819. else if( (asBCInfo[curr->op].type == asBCTYPE_wW_rW_ARG ||
  820. asBCInfo[curr->op].type == asBCTYPE_wW_rW_DW_ARG) &&
  821. curr->wArg[1] == offset )
  822. return true;
  823. else if( asBCInfo[curr->op].type == asBCTYPE_rW_rW_ARG &&
  824. ((signed)curr->wArg[0] == offset || (signed)curr->wArg[1] == offset) )
  825. return true;
  826. else if( curr->op == asBC_LoadThisR && offset == 0 )
  827. return true;
  828. return false;
  829. }
  830. bool asCByteCode::IsInstrJmpOrLabel(cByteInstruction *curr)
  831. {
  832. if( curr->op == asBC_JS ||
  833. curr->op == asBC_JNS ||
  834. curr->op == asBC_JP ||
  835. curr->op == asBC_JNP ||
  836. curr->op == asBC_JMPP ||
  837. curr->op == asBC_JMP ||
  838. curr->op == asBC_JZ ||
  839. curr->op == asBC_JNZ ||
  840. curr->op == asBC_LABEL )
  841. return true;
  842. return false;
  843. }
  844. bool asCByteCode::IsTempVarOverwrittenByInstr(cByteInstruction *curr, int offset)
  845. {
  846. // Which instructions overwrite the variable or discard it?
  847. if( curr->op == asBC_RET ||
  848. curr->op == asBC_SUSPEND )
  849. return true;
  850. else if( (asBCInfo[curr->op].type == asBCTYPE_wW_rW_rW_ARG ||
  851. asBCInfo[curr->op].type == asBCTYPE_wW_rW_ARG ||
  852. asBCInfo[curr->op].type == asBCTYPE_wW_rW_DW_ARG ||
  853. asBCInfo[curr->op].type == asBCTYPE_wW_ARG ||
  854. asBCInfo[curr->op].type == asBCTYPE_wW_W_ARG ||
  855. asBCInfo[curr->op].type == asBCTYPE_wW_DW_ARG ||
  856. asBCInfo[curr->op].type == asBCTYPE_wW_QW_ARG) &&
  857. curr->wArg[0] == offset )
  858. return true;
  859. return false;
  860. }
  861. bool asCByteCode::IsTempVarRead(cByteInstruction *curr, int offset)
  862. {
  863. asCArray<cByteInstruction *> openPaths;
  864. asCArray<cByteInstruction *> closedPaths;
  865. // We're not interested in the first instruction, since it is the one that sets the variable
  866. openPaths.PushLast(curr->next);
  867. while( openPaths.GetLength() )
  868. {
  869. curr = openPaths.PopLast();
  870. // Add the instruction to the closed paths so that we don't verify it again
  871. closedPaths.PushLast(curr);
  872. while( curr )
  873. {
  874. if( IsTempVarReadByInstr(curr, offset) ) return true;
  875. if( IsTempVarOverwrittenByInstr(curr, offset) ) break;
  876. // In case of jumps, we must follow the each of the paths
  877. if( curr->op == asBC_JMP )
  878. {
  879. int label = *((int*)ARG_DW(curr->arg));
  880. int r = FindLabel(label, curr, &curr, 0); asASSERT( r == 0 ); UNUSED_VAR(r);
  881. if( !closedPaths.Exists(curr) &&
  882. !openPaths.Exists(curr) )
  883. openPaths.PushLast(curr);
  884. break;
  885. }
  886. else if( curr->op == asBC_JZ || curr->op == asBC_JNZ ||
  887. curr->op == asBC_JS || curr->op == asBC_JNS ||
  888. curr->op == asBC_JP || curr->op == asBC_JNP )
  889. {
  890. cByteInstruction *dest = 0;
  891. int label = *((int*)ARG_DW(curr->arg));
  892. int r = FindLabel(label, curr, &dest, 0); asASSERT( r == 0 ); UNUSED_VAR(r);
  893. if( !closedPaths.Exists(dest) &&
  894. !openPaths.Exists(dest) )
  895. openPaths.PushLast(dest);
  896. }
  897. else if( curr->op == asBC_JMPP )
  898. {
  899. // A JMPP instruction is always followed by a series of JMP instructions
  900. // that give the real destination (like a look-up table). We need add all
  901. // of these as open paths.
  902. curr = curr->next;
  903. while( curr->op == asBC_JMP )
  904. {
  905. cByteInstruction *dest = 0;
  906. int label = *((int*)ARG_DW(curr->arg));
  907. int r = FindLabel(label, curr, &dest, 0); asASSERT( r == 0 ); UNUSED_VAR(r);
  908. if( !closedPaths.Exists(dest) &&
  909. !openPaths.Exists(dest) )
  910. openPaths.PushLast(dest);
  911. curr = curr->next;
  912. }
  913. // We should now be on a label which is the destination of the
  914. // first JMP in the sequence and is already added in the open paths
  915. asASSERT(curr->op == asBC_LABEL);
  916. break;
  917. }
  918. curr = curr->next;
  919. }
  920. }
  921. return false;
  922. }
  923. bool asCByteCode::IsTempRegUsed(cByteInstruction *curr)
  924. {
  925. // We're not interested in the first instruction, since it is the one that sets the register
  926. while( curr->next )
  927. {
  928. curr = curr->next;
  929. // Which instructions read from the register?
  930. if( curr->op == asBC_INCi ||
  931. curr->op == asBC_INCi16 ||
  932. curr->op == asBC_INCi8 ||
  933. curr->op == asBC_INCf ||
  934. curr->op == asBC_INCd ||
  935. curr->op == asBC_DECi ||
  936. curr->op == asBC_DECi16 ||
  937. curr->op == asBC_DECi8 ||
  938. curr->op == asBC_DECf ||
  939. curr->op == asBC_DECd ||
  940. curr->op == asBC_WRTV1 ||
  941. curr->op == asBC_WRTV2 ||
  942. curr->op == asBC_WRTV4 ||
  943. curr->op == asBC_WRTV8 ||
  944. curr->op == asBC_RDR1 ||
  945. curr->op == asBC_RDR2 ||
  946. curr->op == asBC_RDR4 ||
  947. curr->op == asBC_RDR8 ||
  948. curr->op == asBC_PshRPtr ||
  949. curr->op == asBC_CpyRtoV4 ||
  950. curr->op == asBC_CpyRtoV8 ||
  951. curr->op == asBC_TZ ||
  952. curr->op == asBC_TNZ ||
  953. curr->op == asBC_TS ||
  954. curr->op == asBC_TNS ||
  955. curr->op == asBC_TP ||
  956. curr->op == asBC_TNP ||
  957. curr->op == asBC_JZ ||
  958. curr->op == asBC_JNZ ||
  959. curr->op == asBC_JS ||
  960. curr->op == asBC_JNS ||
  961. curr->op == asBC_JP ||
  962. curr->op == asBC_JNP )
  963. return true;
  964. // Which instructions overwrite the register or discard the value?
  965. if( curr->op == asBC_CALL ||
  966. curr->op == asBC_PopRPtr ||
  967. curr->op == asBC_CALLSYS ||
  968. curr->op == asBC_CALLBND ||
  969. curr->op == asBC_SUSPEND ||
  970. curr->op == asBC_ALLOC ||
  971. curr->op == asBC_CpyVtoR4 ||
  972. curr->op == asBC_LdGRdR4 ||
  973. curr->op == asBC_LDG ||
  974. curr->op == asBC_LDV ||
  975. curr->op == asBC_TZ ||
  976. curr->op == asBC_TNZ ||
  977. curr->op == asBC_TS ||
  978. curr->op == asBC_TNS ||
  979. curr->op == asBC_TP ||
  980. curr->op == asBC_TNP ||
  981. curr->op == asBC_JS ||
  982. curr->op == asBC_JNS ||
  983. curr->op == asBC_JP ||
  984. curr->op == asBC_JNP ||
  985. curr->op == asBC_JMPP ||
  986. curr->op == asBC_JMP ||
  987. curr->op == asBC_JZ ||
  988. curr->op == asBC_JNZ ||
  989. curr->op == asBC_CMPi ||
  990. curr->op == asBC_CMPu ||
  991. curr->op == asBC_CMPf ||
  992. curr->op == asBC_CMPd ||
  993. curr->op == asBC_CMPIi ||
  994. curr->op == asBC_CMPIu ||
  995. curr->op == asBC_CMPIf ||
  996. curr->op == asBC_LABEL ||
  997. curr->op == asBC_LoadThisR ||
  998. curr->op == asBC_LoadRObjR ||
  999. curr->op == asBC_LoadVObjR )
  1000. return false;
  1001. }
  1002. return false;
  1003. }
  1004. bool asCByteCode::IsSimpleExpression()
  1005. {
  1006. // A simple expression is one that cannot be suspended at any time, i.e.
  1007. // it doesn't have any calls to other routines, and doesn't have any suspend instructions
  1008. cByteInstruction *instr = first;
  1009. while( instr )
  1010. {
  1011. if( instr->op == asBC_ALLOC ||
  1012. instr->op == asBC_CALL ||
  1013. instr->op == asBC_CALLSYS ||
  1014. instr->op == asBC_SUSPEND ||
  1015. instr->op == asBC_LINE ||
  1016. instr->op == asBC_FREE ||
  1017. instr->op == asBC_CallPtr ||
  1018. instr->op == asBC_CALLINTF ||
  1019. instr->op == asBC_CALLBND )
  1020. return false;
  1021. instr = instr->next;
  1022. }
  1023. return true;
  1024. }
  1025. void asCByteCode::ExtractLineNumbers()
  1026. {
  1027. int lastLinePos = -1;
  1028. int pos = 0;
  1029. cByteInstruction *instr = first;
  1030. while( instr )
  1031. {
  1032. cByteInstruction *curr = instr;
  1033. instr = instr->next;
  1034. if( curr->op == asBC_LINE )
  1035. {
  1036. if( lastLinePos == pos )
  1037. {
  1038. lineNumbers.PopLast();
  1039. lineNumbers.PopLast();
  1040. }
  1041. lastLinePos = pos;
  1042. lineNumbers.PushLast(pos);
  1043. lineNumbers.PushLast(*(int*)ARG_DW(curr->arg));
  1044. if( !engine->ep.buildWithoutLineCues )
  1045. {
  1046. // Transform BC_LINE into BC_SUSPEND
  1047. curr->op = asBC_SUSPEND;
  1048. curr->size = asBCTypeSize[asBCInfo[asBC_SUSPEND].type];
  1049. pos += curr->size;
  1050. }
  1051. else
  1052. {
  1053. // Delete the instruction
  1054. DeleteInstruction(curr);
  1055. }
  1056. }
  1057. else
  1058. pos += curr->size;
  1059. }
  1060. }
  1061. void asCByteCode::ExtractObjectVariableInfo(asCScriptFunction *outFunc)
  1062. {
  1063. int pos = 0;
  1064. cByteInstruction *instr = first;
  1065. while( instr )
  1066. {
  1067. if( instr->op == asBC_Block )
  1068. {
  1069. asSObjectVariableInfo info;
  1070. info.programPos = pos;
  1071. info.variableOffset = 0;
  1072. info.option = instr->wArg[0] ? asBLOCK_BEGIN : asBLOCK_END;
  1073. outFunc->objVariableInfo.PushLast(info);
  1074. }
  1075. else if( instr->op == asBC_ObjInfo )
  1076. {
  1077. asSObjectVariableInfo info;
  1078. info.programPos = pos;
  1079. info.variableOffset = (short)instr->wArg[0];
  1080. info.option = *(int*)ARG_DW(instr->arg);
  1081. outFunc->objVariableInfo.PushLast(info);
  1082. }
  1083. else if( instr->op == asBC_VarDecl )
  1084. {
  1085. outFunc->variables[instr->wArg[0]]->declaredAtProgramPos = pos;
  1086. }
  1087. else
  1088. pos += instr->size;
  1089. instr = instr->next;
  1090. }
  1091. }
  1092. int asCByteCode::GetSize()
  1093. {
  1094. int size = 0;
  1095. cByteInstruction *instr = first;
  1096. while( instr )
  1097. {
  1098. size += instr->GetSize();
  1099. instr = instr->next;
  1100. }
  1101. return size;
  1102. }
  1103. void asCByteCode::AddCode(asCByteCode *bc)
  1104. {
  1105. if( bc->first )
  1106. {
  1107. if( first == 0 )
  1108. {
  1109. first = bc->first;
  1110. last = bc->last;
  1111. bc->first = 0;
  1112. bc->last = 0;
  1113. }
  1114. else
  1115. {
  1116. last->next = bc->first;
  1117. bc->first->prev = last;
  1118. last = bc->last;
  1119. bc->first = 0;
  1120. bc->last = 0;
  1121. }
  1122. }
  1123. }
  1124. int asCByteCode::AddInstruction()
  1125. {
  1126. cByteInstruction *instr = new(engine->memoryMgr.AllocByteInstruction()) cByteInstruction();
  1127. if( first == 0 )
  1128. {
  1129. first = last = instr;
  1130. }
  1131. else
  1132. {
  1133. last->AddAfter(instr);
  1134. last = instr;
  1135. }
  1136. return 0;
  1137. }
  1138. int asCByteCode::AddInstructionFirst()
  1139. {
  1140. cByteInstruction *instr = new(engine->memoryMgr.AllocByteInstruction()) cByteInstruction();
  1141. if( first == 0 )
  1142. {
  1143. first = last = instr;
  1144. }
  1145. else
  1146. {
  1147. first->AddBefore(instr);
  1148. first = instr;
  1149. }
  1150. return 0;
  1151. }
  1152. void asCByteCode::Call(asEBCInstr instr, int funcID, int pop)
  1153. {
  1154. if( AddInstruction() < 0 )
  1155. return;
  1156. asASSERT(asBCInfo[instr].type == asBCTYPE_DW_ARG);
  1157. last->op = instr;
  1158. last->size = asBCTypeSize[asBCInfo[instr].type];
  1159. last->stackInc = -pop; // BC_CALL and BC_CALLBND doesn't pop the argument but when the callee returns the arguments are already popped
  1160. *((int*)ARG_DW(last->arg)) = funcID;
  1161. // Add a JitEntry instruction after function calls so that JIT's can resume execution
  1162. InstrPTR(asBC_JitEntry, 0);
  1163. }
  1164. void asCByteCode::CallPtr(asEBCInstr instr, int funcPtrVar, int pop)
  1165. {
  1166. if( AddInstruction() < 0 )
  1167. return;
  1168. asASSERT(asBCInfo[instr].type == asBCTYPE_rW_ARG);
  1169. last->op = instr;
  1170. last->size = asBCTypeSize[asBCInfo[instr].type];
  1171. last->stackInc = -pop;
  1172. last->wArg[0] = (short)funcPtrVar;
  1173. // Add a JitEntry instruction after function calls so that JIT's can resume execution
  1174. InstrPTR(asBC_JitEntry, 0);
  1175. }
  1176. void asCByteCode::Alloc(asEBCInstr instr, void *objID, int funcID, int pop)
  1177. {
  1178. if( AddInstruction() < 0 )
  1179. return;
  1180. last->op = instr;
  1181. last->size = asBCTypeSize[asBCInfo[instr].type];
  1182. last->stackInc = -pop; // BC_ALLOC
  1183. asASSERT(asBCInfo[instr].type == asBCTYPE_PTR_DW_ARG);
  1184. *ARG_PTR(last->arg) = (asPWORD)objID;
  1185. *((int*)(ARG_DW(last->arg)+AS_PTR_SIZE)) = funcID;
  1186. // Add a JitEntry instruction after function calls so that JIT's can resume execution
  1187. InstrPTR(asBC_JitEntry, 0);
  1188. }
  1189. void asCByteCode::Ret(int pop)
  1190. {
  1191. if( AddInstruction() < 0 )
  1192. return;
  1193. asASSERT(asBCInfo[asBC_RET].type == asBCTYPE_W_ARG);
  1194. last->op = asBC_RET;
  1195. last->size = asBCTypeSize[asBCInfo[asBC_RET].type];
  1196. last->stackInc = 0; // The instruction pops the argument, but it doesn't affect current function
  1197. last->wArg[0] = (short)pop;
  1198. }
  1199. void asCByteCode::JmpP(int var, asDWORD max)
  1200. {
  1201. if( AddInstruction() < 0 )
  1202. return;
  1203. asASSERT(asBCInfo[asBC_JMPP].type == asBCTYPE_rW_ARG);
  1204. last->op = asBC_JMPP;
  1205. last->size = asBCTypeSize[asBCInfo[asBC_JMPP].type];
  1206. last->stackInc = asBCInfo[asBC_JMPP].stackInc;
  1207. last->wArg[0] = (short)var;
  1208. // Store the largest jump that is made for PostProcess()
  1209. *ARG_DW(last->arg) = max;
  1210. }
  1211. void asCByteCode::Label(short label)
  1212. {
  1213. if( AddInstruction() < 0 )
  1214. return;
  1215. last->op = asBC_LABEL;
  1216. last->size = 0;
  1217. last->stackInc = 0;
  1218. last->wArg[0] = label;
  1219. }
  1220. void asCByteCode::Line(int line, int column)
  1221. {
  1222. if( AddInstruction() < 0 )
  1223. return;
  1224. last->op = asBC_LINE;
  1225. // If the build is without line cues these instructions will be removed
  1226. // otherwise they will be transformed into SUSPEND instructions.
  1227. if( engine->ep.buildWithoutLineCues )
  1228. last->size = 0;
  1229. else
  1230. last->size = asBCTypeSize[asBCInfo[asBC_SUSPEND].type];
  1231. last->stackInc = 0;
  1232. *((int*)ARG_DW(last->arg)) = (line & 0xFFFFF)|((column & 0xFFF)<<20);
  1233. // Add a JitEntry after the line instruction to allow the JIT function to resume after a suspend
  1234. InstrPTR(asBC_JitEntry, 0);
  1235. }
  1236. void asCByteCode::ObjInfo(int offset, int info)
  1237. {
  1238. if( AddInstruction() < 0 )
  1239. return;
  1240. // Add the special instruction that will be used to tell the exception
  1241. // handler when an object is initialized and deinitialized.
  1242. last->op = asBC_ObjInfo;
  1243. last->size = 0;
  1244. last->stackInc = 0;
  1245. last->wArg[0] = (short)offset;
  1246. *((int*)ARG_DW(last->arg)) = info;
  1247. }
  1248. void asCByteCode::Block(bool start)
  1249. {
  1250. if( AddInstruction() < 0 )
  1251. return;
  1252. last->op = asBC_Block;
  1253. last->size = 0;
  1254. last->stackInc = 0;
  1255. last->wArg[0] = start ? 1 : 0;
  1256. }
  1257. void asCByteCode::VarDecl(int varDeclIdx)
  1258. {
  1259. if( AddInstruction() < 0 )
  1260. return;
  1261. last->op = asBC_VarDecl;
  1262. last->size = 0;
  1263. last->stackInc = 0;
  1264. last->wArg[0] = (asWORD)varDeclIdx;
  1265. }
  1266. int asCByteCode::FindLabel(int label, cByteInstruction *from, cByteInstruction **dest, int *positionDelta)
  1267. {
  1268. // Search forward
  1269. int labelPos = -from->GetSize();
  1270. cByteInstruction *labelInstr = from;
  1271. while( labelInstr )
  1272. {
  1273. labelPos += labelInstr->GetSize();
  1274. labelInstr = labelInstr->next;
  1275. if( labelInstr && labelInstr->op == asBC_LABEL )
  1276. {
  1277. if( labelInstr->wArg[0] == label )
  1278. break;
  1279. }
  1280. }
  1281. if( labelInstr == 0 )
  1282. {
  1283. // Search backwards
  1284. labelPos = -from->GetSize();
  1285. labelInstr = from;
  1286. while( labelInstr )
  1287. {
  1288. labelInstr = labelInstr->prev;
  1289. if( labelInstr )
  1290. {
  1291. labelPos -= labelInstr->GetSize();
  1292. if( labelInstr->op == asBC_LABEL )
  1293. {
  1294. if( labelInstr->wArg[0] == label )
  1295. break;
  1296. }
  1297. }
  1298. }
  1299. }
  1300. if( labelInstr != 0 )
  1301. {
  1302. if( dest ) *dest = labelInstr;
  1303. if( positionDelta ) *positionDelta = labelPos;
  1304. return 0;
  1305. }
  1306. return -1;
  1307. }
  1308. int asCByteCode::ResolveJumpAddresses()
  1309. {
  1310. int pos = 0;
  1311. cByteInstruction *instr = first;
  1312. while( instr )
  1313. {
  1314. // The program pointer is updated as the instruction is read
  1315. pos += instr->GetSize();
  1316. if( instr->op == asBC_JMP ||
  1317. instr->op == asBC_JZ || instr->op == asBC_JNZ ||
  1318. instr->op == asBC_JS || instr->op == asBC_JNS ||
  1319. instr->op == asBC_JP || instr->op == asBC_JNP )
  1320. {
  1321. int label = *((int*) ARG_DW(instr->arg));
  1322. int labelPosOffset;
  1323. int r = FindLabel(label, instr, 0, &labelPosOffset);
  1324. if( r == 0 )
  1325. *((int*) ARG_DW(instr->arg)) = labelPosOffset;
  1326. else
  1327. return -1;
  1328. }
  1329. instr = instr->next;
  1330. }
  1331. return 0;
  1332. }
  1333. cByteInstruction *asCByteCode::DeleteInstruction(cByteInstruction *instr)
  1334. {
  1335. if( instr == 0 ) return 0;
  1336. cByteInstruction *ret = instr->prev ? instr->prev : instr->next;
  1337. RemoveInstruction(instr);
  1338. engine->memoryMgr.FreeByteInstruction(instr);
  1339. return ret;
  1340. }
  1341. void asCByteCode::Output(asDWORD *array)
  1342. {
  1343. // TODO: Receive a script function pointer instead of the bytecode array
  1344. asDWORD *ap = array;
  1345. cByteInstruction *instr = first;
  1346. while( instr )
  1347. {
  1348. if( instr->GetSize() > 0 )
  1349. {
  1350. *(asBYTE*)ap = asBYTE(instr->op);
  1351. *(((asBYTE*)ap)+1) = 0; // Second byte is always zero
  1352. switch( asBCInfo[instr->op].type )
  1353. {
  1354. case asBCTYPE_NO_ARG:
  1355. *(((asWORD*)ap)+1) = 0; // Clear upper bytes
  1356. break;
  1357. case asBCTYPE_wW_rW_rW_ARG:
  1358. *(((asWORD*)ap)+1) = instr->wArg[0];
  1359. *(((asWORD*)ap)+2) = instr->wArg[1];
  1360. *(((asWORD*)ap)+3) = instr->wArg[2];
  1361. break;
  1362. case asBCTYPE_wW_DW_ARG:
  1363. case asBCTYPE_rW_DW_ARG:
  1364. case asBCTYPE_W_DW_ARG:
  1365. *(((asWORD*)ap)+1) = instr->wArg[0];
  1366. *(ap+1) = *(asDWORD*)&instr->arg;
  1367. break;
  1368. case asBCTYPE_wW_rW_DW_ARG:
  1369. case asBCTYPE_rW_W_DW_ARG:
  1370. *(((asWORD*)ap)+1) = instr->wArg[0];
  1371. *(((asWORD*)ap)+2) = instr->wArg[1];
  1372. *(ap+2) = *(asDWORD*)&instr->arg;
  1373. break;
  1374. case asBCTYPE_wW_QW_ARG:
  1375. case asBCTYPE_rW_QW_ARG:
  1376. *(((asWORD*)ap)+1) = instr->wArg[0];
  1377. *(asQWORD*)(ap+1) = asQWORD(instr->arg);
  1378. break;
  1379. case asBCTYPE_W_ARG:
  1380. case asBCTYPE_rW_ARG:
  1381. case asBCTYPE_wW_ARG:
  1382. *(((asWORD*)ap)+1) = instr->wArg[0];
  1383. break;
  1384. case asBCTYPE_wW_rW_ARG:
  1385. case asBCTYPE_rW_rW_ARG:
  1386. case asBCTYPE_wW_W_ARG:
  1387. *(((asWORD *)ap)+1) = instr->wArg[0];
  1388. *(((asWORD *)ap)+2) = instr->wArg[1];
  1389. break;
  1390. case asBCTYPE_QW_DW_ARG:
  1391. case asBCTYPE_DW_DW_ARG:
  1392. case asBCTYPE_QW_ARG:
  1393. case asBCTYPE_DW_ARG:
  1394. *(((asWORD*)ap)+1) = 0; // Clear upper bytes
  1395. memcpy(ap+1, &instr->arg, instr->GetSize()*4-4);
  1396. break;
  1397. default:
  1398. // How did we get here?
  1399. asASSERT(false);
  1400. break;
  1401. }
  1402. }
  1403. ap += instr->GetSize();
  1404. instr = instr->next;
  1405. }
  1406. }
  1407. void asCByteCode::PostProcess()
  1408. {
  1409. if( first == 0 ) return;
  1410. // This function will do the following
  1411. // - Verify if there is any code that never gets executed and remove it
  1412. // - Calculate the stack size at the position of each byte code
  1413. // - Calculate the largest stack needed
  1414. largestStackUsed = 0;
  1415. cByteInstruction *instr = first;
  1416. while( instr )
  1417. {
  1418. instr->marked = false;
  1419. instr->stackSize = -1;
  1420. instr = instr->next;
  1421. }
  1422. // Add the first instruction to the list of unchecked code paths
  1423. asCArray<cByteInstruction *> paths;
  1424. AddPath(paths, first, 0);
  1425. // Go through each of the code paths
  1426. for( asUINT p = 0; p < paths.GetLength(); ++p )
  1427. {
  1428. instr = paths[p];
  1429. int stackSize = instr->stackSize;
  1430. while( instr )
  1431. {
  1432. instr->marked = true;
  1433. instr->stackSize = stackSize;
  1434. stackSize += instr->stackInc;
  1435. if( stackSize > largestStackUsed )
  1436. largestStackUsed = stackSize;
  1437. if( instr->op == asBC_JMP )
  1438. {
  1439. // Find the label that we should jump to
  1440. int label = *((int*) ARG_DW(instr->arg));
  1441. cByteInstruction *dest = 0;
  1442. int r = FindLabel(label, instr, &dest, 0); asASSERT( r == 0 ); UNUSED_VAR(r);
  1443. AddPath(paths, dest, stackSize);
  1444. break;
  1445. }
  1446. else if( instr->op == asBC_JZ || instr->op == asBC_JNZ ||
  1447. instr->op == asBC_JS || instr->op == asBC_JNS ||
  1448. instr->op == asBC_JP || instr->op == asBC_JNP )
  1449. {
  1450. // Find the label that is being jumped to
  1451. int label = *((int*) ARG_DW(instr->arg));
  1452. cByteInstruction *dest = 0;
  1453. int r = FindLabel(label, instr, &dest, 0); asASSERT( r == 0 ); UNUSED_VAR(r);
  1454. AddPath(paths, dest, stackSize);
  1455. // Add both paths to the code paths
  1456. AddPath(paths, instr->next, stackSize);
  1457. break;
  1458. }
  1459. else if( instr->op == asBC_JMPP )
  1460. {
  1461. // I need to know the largest value possible
  1462. asDWORD max = *ARG_DW(instr->arg);
  1463. // Add all destinations to the code paths
  1464. cByteInstruction *dest = instr->next;
  1465. for( asDWORD n = 0; n <= max && dest != 0; ++n )
  1466. {
  1467. AddPath(paths, dest, stackSize);
  1468. dest = dest->next;
  1469. }
  1470. break;
  1471. }
  1472. else
  1473. {
  1474. instr = instr->next;
  1475. if( instr == 0 || instr->marked )
  1476. break;
  1477. }
  1478. }
  1479. }
  1480. // Are there any instructions that didn't get visited?
  1481. instr = first;
  1482. while( instr )
  1483. {
  1484. if( instr->marked == false )
  1485. {
  1486. // TODO: Give warning of unvisited code
  1487. // Remove it
  1488. cByteInstruction *curr = instr;
  1489. instr = instr->next;
  1490. DeleteInstruction(curr);
  1491. }
  1492. else
  1493. instr = instr->next;
  1494. }
  1495. }
  1496. #ifdef AS_DEBUG
  1497. void asCByteCode::DebugOutput(const char *name, asCScriptEngine *engine, asCScriptFunction *func)
  1498. {
  1499. _mkdir("AS_DEBUG");
  1500. asCString str = "AS_DEBUG/";
  1501. str += name;
  1502. #if _MSC_VER >= 1500
  1503. FILE *file;
  1504. fopen_s(&file, str.AddressOf(), "w");
  1505. #else
  1506. FILE *file = fopen(str.AddressOf(), "w");
  1507. #endif
  1508. #if !defined(AS_XENON) // XBox 360: When running in DVD Emu, no write is allowed
  1509. asASSERT( file );
  1510. #endif
  1511. if( file == 0 )
  1512. return;
  1513. asUINT n;
  1514. fprintf(file, "%s\n\n", func->GetDeclaration());
  1515. fprintf(file, "Temps: ");
  1516. for( n = 0; n < temporaryVariables.GetLength(); n++ )
  1517. {
  1518. fprintf(file, "%d", temporaryVariables[n]);
  1519. if( n < temporaryVariables.GetLength()-1 )
  1520. fprintf(file, ", ");
  1521. }
  1522. fprintf(file, "\n\n");
  1523. fprintf(file, "Variables: \n");
  1524. for( n = 0; n < func->variables.GetLength(); n++ )
  1525. {
  1526. fprintf(file, " %.3d: %s %s\n", func->variables[n]->stackOffset, func->variables[n]->type.Format().AddressOf(), func->variables[n]->name.AddressOf());
  1527. }
  1528. asUINT offset = 0;
  1529. if( func->objectType )
  1530. {
  1531. fprintf(file, " %.3d: %s this\n", 0, func->objectType->name.AddressOf());
  1532. offset -= AS_PTR_SIZE;
  1533. }
  1534. for( n = 0; n < func->parameterTypes.GetLength(); n++ )
  1535. {
  1536. bool found = false;
  1537. for( asUINT v = 0; v < func->variables.GetLength(); v++ )
  1538. {
  1539. if( func->variables[v]->stackOffset == (int)offset )
  1540. {
  1541. found = true;
  1542. break;
  1543. }
  1544. }
  1545. if( !found )
  1546. fprintf(file, " %.3d: %s {noname param}\n", offset, func->parameterTypes[n].Format().AddressOf());
  1547. offset -= func->parameterTypes[n].GetSizeOnStackDWords();
  1548. }
  1549. for( n = 0; n < func->objVariablePos.GetLength(); n++ )
  1550. {
  1551. bool found = false;
  1552. for( asUINT v = 0; v < func->variables.GetLength(); v++ )
  1553. {
  1554. if( func->variables[v]->stackOffset == func->objVariablePos[n] )
  1555. {
  1556. found = true;
  1557. break;
  1558. }
  1559. }
  1560. if( !found )
  1561. fprintf(file, " %.3d: %s {noname}\n", func->objVariablePos[n], func->objVariableTypes[n]->name.AddressOf());
  1562. }
  1563. fprintf(file, "\n\n");
  1564. int pos = 0;
  1565. asUINT lineIndex = 0;
  1566. cByteInstruction *instr = first;
  1567. while( instr )
  1568. {
  1569. if( lineIndex < lineNumbers.GetLength() && lineNumbers[lineIndex] == pos )
  1570. {
  1571. asDWORD line = lineNumbers[lineIndex+1];
  1572. fprintf(file, "- %d,%d -\n", (int)(line&0xFFFFF), (int)(line>>20));
  1573. lineIndex += 2;
  1574. }
  1575. fprintf(file, "%5d ", pos);
  1576. pos += instr->GetSize();
  1577. fprintf(file, "%3d %c ", instr->stackSize, instr->marked ? '*' : ' ');
  1578. switch( asBCInfo[instr->op].type )
  1579. {
  1580. case asBCTYPE_W_ARG:
  1581. if( instr->op == asBC_STR )
  1582. {
  1583. int id = instr->wArg[0];
  1584. const asCString &str = engine->GetConstantString(id);
  1585. fprintf(file, " %-8s %d (l:%ld s:\"%.10s\")\n", asBCInfo[instr->op].name, instr->wArg[0], (long int)str.GetLength(), str.AddressOf());
  1586. }
  1587. else
  1588. fprintf(file, " %-8s %d\n", asBCInfo[instr->op].name, instr->wArg[0]);
  1589. break;
  1590. case asBCTYPE_wW_ARG:
  1591. case asBCTYPE_rW_ARG:
  1592. fprintf(file, " %-8s v%d\n", asBCInfo[instr->op].name, instr->wArg[0]);
  1593. break;
  1594. case asBCTYPE_wW_rW_ARG:
  1595. case asBCTYPE_rW_rW_ARG:
  1596. fprintf(file, " %-8s v%d, v%d\n", asBCInfo[instr->op].name, instr->wArg[0], instr->wArg[1]);
  1597. break;
  1598. case asBCTYPE_wW_W_ARG:
  1599. fprintf(file, " %-8s v%d, %d\n", asBCInfo[instr->op].name, instr->wArg[0], instr->wArg[1]);
  1600. break;
  1601. case asBCTYPE_wW_rW_DW_ARG:
  1602. case asBCTYPE_rW_W_DW_ARG:
  1603. switch( instr->op )
  1604. {
  1605. case asBC_ADDIf:
  1606. case asBC_SUBIf:
  1607. case asBC_MULIf:
  1608. fprintf(file, " %-8s v%d, v%d, %f\n", asBCInfo[instr->op].name, instr->wArg[0], instr->wArg[1], *((float*) ARG_DW(instr->arg)));
  1609. break;
  1610. default:
  1611. fprintf(file, " %-8s v%d, v%d, %d\n", asBCInfo[instr->op].name, instr->wArg[0], instr->wArg[1], *((int*) ARG_DW(instr->arg)));
  1612. break;
  1613. }
  1614. break;
  1615. case asBCTYPE_DW_ARG:
  1616. switch( instr->op )
  1617. {
  1618. case asBC_OBJTYPE:
  1619. fprintf(file, " %-8s 0x%x\n", asBCInfo[instr->op].name, (asUINT)*ARG_DW(instr->arg));
  1620. break;
  1621. case asBC_PshC4:
  1622. case asBC_Cast:
  1623. 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)));
  1624. break;
  1625. case asBC_TYPEID:
  1626. fprintf(file, " %-8s 0x%x '%s'\n", asBCInfo[instr->op].name, (asUINT)*ARG_DW(instr->arg), engine->GetTypeDeclaration((int)*ARG_DW(instr->arg)));
  1627. break;
  1628. case asBC_CALL:
  1629. case asBC_CALLSYS:
  1630. case asBC_CALLBND:
  1631. case asBC_CALLINTF:
  1632. {
  1633. int funcID = *(int*)ARG_DW(instr->arg);
  1634. asCString decl = engine->GetFunctionDeclaration(funcID);
  1635. fprintf(file, " %-8s %d (%s)\n", asBCInfo[instr->op].name, *((int*) ARG_DW(instr->arg)), decl.AddressOf());
  1636. }
  1637. break;
  1638. case asBC_REFCPY:
  1639. fprintf(file, " %-8s 0x%x\n", asBCInfo[instr->op].name, *((int*) ARG_DW(instr->arg)));
  1640. break;
  1641. case asBC_JMP:
  1642. case asBC_JZ:
  1643. case asBC_JS:
  1644. case asBC_JP:
  1645. case asBC_JNZ:
  1646. case asBC_JNS:
  1647. case asBC_JNP:
  1648. fprintf(file, " %-8s %+d (d:%d)\n", asBCInfo[instr->op].name, *((int*) ARG_DW(instr->arg)), pos+*((int*) ARG_DW(instr->arg)));
  1649. break;
  1650. default:
  1651. fprintf(file, " %-8s %d\n", asBCInfo[instr->op].name, *((int*) ARG_DW(instr->arg)));
  1652. break;
  1653. }
  1654. break;
  1655. case asBCTYPE_QW_ARG:
  1656. #ifdef __GNUC__
  1657. #ifdef _LP64
  1658. 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)));
  1659. #else
  1660. 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)));
  1661. #endif
  1662. #else
  1663. 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)));
  1664. #endif
  1665. break;
  1666. case asBCTYPE_wW_QW_ARG:
  1667. case asBCTYPE_rW_QW_ARG:
  1668. #ifdef __GNUC__
  1669. #ifdef _LP64
  1670. 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)));
  1671. #else
  1672. 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)));
  1673. #endif
  1674. #else
  1675. 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)));
  1676. #endif
  1677. break;
  1678. case asBCTYPE_DW_DW_ARG:
  1679. if( instr->op == asBC_ALLOC )
  1680. {
  1681. asCObjectType *ot = *(asCObjectType**)ARG_DW(instr->arg);
  1682. 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());
  1683. }
  1684. else
  1685. fprintf(file, " %-8s %u, %d\n", asBCInfo[instr->op].name, *(int*)ARG_DW(instr->arg), *(int*)(ARG_DW(instr->arg)+1));
  1686. break;
  1687. case asBCTYPE_QW_DW_ARG:
  1688. if( instr->op == asBC_ALLOC )
  1689. {
  1690. asCObjectType *ot = *(asCObjectType**)ARG_QW(instr->arg);
  1691. #ifdef __GNUC__
  1692. #ifdef AS_64BIT_PTR
  1693. 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());
  1694. #else
  1695. 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());
  1696. #endif
  1697. #else
  1698. 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());
  1699. #endif
  1700. }
  1701. else
  1702. #ifdef __GNUC__
  1703. #ifdef AS_64BIT_PTR
  1704. fprintf(file, " %-8s %lu, %d\n", asBCInfo[instr->op].name, *(asINT64*)ARG_QW(instr->arg), *(int*)(ARG_DW(instr->arg)+2));
  1705. #else
  1706. fprintf(file, " %-8s %llu, %d\n", asBCInfo[instr->op].name, *(asINT64*)ARG_QW(instr->arg), *(int*)(ARG_DW(instr->arg)+2));
  1707. #endif
  1708. #else
  1709. fprintf(file, " %-8s %I64u, %d\n", asBCInfo[instr->op].name, *(asINT64*)ARG_QW(instr->arg), *(int*)(ARG_DW(instr->arg)+2));
  1710. #endif
  1711. break;
  1712. case asBCTYPE_INFO:
  1713. if( instr->op == asBC_LABEL )
  1714. fprintf(file, "%d:\n", instr->wArg[0]);
  1715. else if( instr->op == asBC_LINE )
  1716. fprintf(file, " %s\n", asBCInfo[instr->op].name);
  1717. else if( instr->op == asBC_Block )
  1718. fprintf(file, "%c\n", instr->wArg[0] ? '{' : '}');
  1719. break;
  1720. case asBCTYPE_rW_DW_ARG:
  1721. case asBCTYPE_wW_DW_ARG:
  1722. case asBCTYPE_W_DW_ARG:
  1723. if( instr->op == asBC_SetV1 )
  1724. fprintf(file, " %-8s v%d, 0x%x\n", asBCInfo[instr->op].name, instr->wArg[0], *(asBYTE*)ARG_DW(instr->arg));
  1725. else if( instr->op == asBC_SetV2 )
  1726. fprintf(file, " %-8s v%d, 0x%x\n", asBCInfo[instr->op].name, instr->wArg[0], *(asWORD*)ARG_DW(instr->arg));
  1727. else if( instr->op == asBC_SetV4 )
  1728. 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)));
  1729. else if( instr->op == asBC_CMPIf )
  1730. fprintf(file, " %-8s v%d, %f\n", asBCInfo[instr->op].name, instr->wArg[0], *(float*)ARG_DW(instr->arg));
  1731. else
  1732. fprintf(file, " %-8s v%d, %d\n", asBCInfo[instr->op].name, instr->wArg[0], (asUINT)*ARG_DW(instr->arg));
  1733. break;
  1734. case asBCTYPE_wW_rW_rW_ARG:
  1735. fprintf(file, " %-8s v%d, v%d, v%d\n", asBCInfo[instr->op].name, instr->wArg[0], instr->wArg[1], instr->wArg[2]);
  1736. break;
  1737. case asBCTYPE_NO_ARG:
  1738. fprintf(file, " %s\n", asBCInfo[instr->op].name);
  1739. break;
  1740. default:
  1741. asASSERT(false);
  1742. }
  1743. instr = instr->next;
  1744. }
  1745. fclose(file);
  1746. }
  1747. #endif
  1748. //=============================================================================
  1749. // Decrease stack with "numDwords"
  1750. int asCByteCode::Pop(int numDwords)
  1751. {
  1752. asASSERT(asBCInfo[asBC_POP].type == asBCTYPE_W_ARG);
  1753. if( AddInstruction() < 0 )
  1754. return 0;
  1755. last->op = asBC_POP;
  1756. last->wArg[0] = (short)numDwords;
  1757. last->size = asBCTypeSize[asBCInfo[asBC_POP].type];
  1758. last->stackInc = -numDwords;
  1759. return last->stackInc;
  1760. }
  1761. // Increase stack with "numDwords"
  1762. int asCByteCode::Push(int numDwords)
  1763. {
  1764. asASSERT(asBCInfo[asBC_PUSH].type == asBCTYPE_W_ARG);
  1765. if( AddInstruction() < 0 )
  1766. return 0;
  1767. last->op = asBC_PUSH;
  1768. last->wArg[0] = (short)numDwords;
  1769. last->size = asBCTypeSize[asBCInfo[asBC_PUSH].type];
  1770. last->stackInc = numDwords;
  1771. return last->stackInc;
  1772. }
  1773. int asCByteCode::InsertFirstInstrDWORD(asEBCInstr bc, asDWORD param)
  1774. {
  1775. asASSERT(asBCInfo[bc].type == asBCTYPE_DW_ARG);
  1776. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  1777. if( AddInstructionFirst() < 0 )
  1778. return 0;
  1779. first->op = bc;
  1780. *ARG_DW(first->arg) = param;
  1781. first->size = asBCTypeSize[asBCInfo[bc].type];
  1782. first->stackInc = asBCInfo[bc].stackInc;
  1783. return first->stackInc;
  1784. }
  1785. int asCByteCode::InsertFirstInstrQWORD(asEBCInstr bc, asQWORD param)
  1786. {
  1787. asASSERT(asBCInfo[bc].type == asBCTYPE_QW_ARG);
  1788. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  1789. if( AddInstructionFirst() < 0 )
  1790. return 0;
  1791. first->op = bc;
  1792. *ARG_QW(first->arg) = param;
  1793. first->size = asBCTypeSize[asBCInfo[bc].type];
  1794. first->stackInc = asBCInfo[bc].stackInc;
  1795. return first->stackInc;
  1796. }
  1797. int asCByteCode::Instr(asEBCInstr bc)
  1798. {
  1799. asASSERT(asBCInfo[bc].type == asBCTYPE_NO_ARG);
  1800. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  1801. if( AddInstruction() < 0 )
  1802. return 0;
  1803. last->op = bc;
  1804. last->size = asBCTypeSize[asBCInfo[bc].type];
  1805. last->stackInc = asBCInfo[bc].stackInc;
  1806. return last->stackInc;
  1807. }
  1808. int asCByteCode::InstrW_W_W(asEBCInstr bc, int a, int b, int c)
  1809. {
  1810. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_rW_rW_ARG);
  1811. asASSERT(asBCInfo[bc].stackInc == 0);
  1812. if( AddInstruction() < 0 )
  1813. return 0;
  1814. last->op = bc;
  1815. last->wArg[0] = (short)a;
  1816. last->wArg[1] = (short)b;
  1817. last->wArg[2] = (short)c;
  1818. last->size = asBCTypeSize[asBCInfo[bc].type];
  1819. last->stackInc = asBCInfo[bc].stackInc;
  1820. return last->stackInc;
  1821. }
  1822. int asCByteCode::InstrW_W(asEBCInstr bc, int a, int b)
  1823. {
  1824. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_rW_ARG ||
  1825. asBCInfo[bc].type == asBCTYPE_rW_rW_ARG);
  1826. asASSERT(asBCInfo[bc].stackInc == 0);
  1827. if( AddInstruction() < 0 )
  1828. return 0;
  1829. last->op = bc;
  1830. last->wArg[0] = (short)a;
  1831. last->wArg[1] = (short)b;
  1832. last->size = asBCTypeSize[asBCInfo[bc].type];
  1833. last->stackInc = asBCInfo[bc].stackInc;
  1834. return last->stackInc;
  1835. }
  1836. int asCByteCode::InstrW_PTR(asEBCInstr bc, short a, void *param)
  1837. {
  1838. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_PTR_ARG);
  1839. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  1840. if( AddInstruction() < 0 )
  1841. return 0;
  1842. last->op = bc;
  1843. last->wArg[0] = a;
  1844. *ARG_PTR(last->arg) = (asPWORD)param;
  1845. last->size = asBCTypeSize[asBCInfo[bc].type];
  1846. last->stackInc = asBCInfo[bc].stackInc;
  1847. return last->stackInc;
  1848. }
  1849. int asCByteCode::InstrW_DW(asEBCInstr bc, asWORD a, asDWORD b)
  1850. {
  1851. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_DW_ARG ||
  1852. asBCInfo[bc].type == asBCTYPE_rW_DW_ARG ||
  1853. asBCInfo[bc].type == asBCTYPE_W_DW_ARG);
  1854. asASSERT(asBCInfo[bc].stackInc == 0);
  1855. if( AddInstruction() < 0 )
  1856. return 0;
  1857. last->op = bc;
  1858. last->wArg[0] = a;
  1859. *((int*) ARG_DW(last->arg)) = b;
  1860. last->size = asBCTypeSize[asBCInfo[bc].type];
  1861. last->stackInc = asBCInfo[bc].stackInc;
  1862. return last->stackInc;
  1863. }
  1864. int asCByteCode::InstrSHORT_B(asEBCInstr bc, short a, asBYTE b)
  1865. {
  1866. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_DW_ARG ||
  1867. asBCInfo[bc].type == asBCTYPE_rW_DW_ARG ||
  1868. asBCInfo[bc].type == asBCTYPE_W_DW_ARG);
  1869. asASSERT(asBCInfo[bc].stackInc == 0);
  1870. if( AddInstruction() < 0 )
  1871. return 0;
  1872. last->op = bc;
  1873. last->wArg[0] = a;
  1874. // We'll have to be careful to store the byte correctly, independent of endianess.
  1875. // Some optimizing compilers may change the order of operations, so we make sure
  1876. // the value is not overwritten even if that happens.
  1877. asBYTE *argPtr = (asBYTE*)ARG_DW(last->arg);
  1878. argPtr[0] = b; // The value is always stored in the lower byte
  1879. argPtr[1] = 0; // and clear the rest of the DWORD
  1880. argPtr[2] = 0;
  1881. argPtr[3] = 0;
  1882. last->size = asBCTypeSize[asBCInfo[bc].type];
  1883. last->stackInc = asBCInfo[bc].stackInc;
  1884. return last->stackInc;
  1885. }
  1886. int asCByteCode::InstrSHORT_W(asEBCInstr bc, short a, asWORD b)
  1887. {
  1888. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_DW_ARG ||
  1889. asBCInfo[bc].type == asBCTYPE_rW_DW_ARG ||
  1890. asBCInfo[bc].type == asBCTYPE_W_DW_ARG);
  1891. asASSERT(asBCInfo[bc].stackInc == 0);
  1892. if( AddInstruction() < 0 )
  1893. return 0;
  1894. last->op = bc;
  1895. last->wArg[0] = a;
  1896. // We'll have to be careful to store the word correctly, independent of endianess.
  1897. // Some optimizing compilers may change the order of operations, so we make sure
  1898. // the value is not overwritten even if that happens.
  1899. asWORD *argPtr = (asWORD*)ARG_DW(last->arg);
  1900. argPtr[0] = b; // The value is always stored in the lower word
  1901. argPtr[1] = 0; // and clear the rest of the DWORD
  1902. last->size = asBCTypeSize[asBCInfo[bc].type];
  1903. last->stackInc = asBCInfo[bc].stackInc;
  1904. return last->stackInc;
  1905. }
  1906. int asCByteCode::InstrSHORT_DW(asEBCInstr bc, short a, asDWORD b)
  1907. {
  1908. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_DW_ARG ||
  1909. asBCInfo[bc].type == asBCTYPE_rW_DW_ARG ||
  1910. asBCInfo[bc].type == asBCTYPE_W_DW_ARG);
  1911. if( AddInstruction() < 0 )
  1912. return 0;
  1913. last->op = bc;
  1914. last->wArg[0] = a;
  1915. *((int*) ARG_DW(last->arg)) = b;
  1916. last->size = asBCTypeSize[asBCInfo[bc].type];
  1917. last->stackInc = asBCInfo[bc].stackInc;
  1918. return last->stackInc;
  1919. }
  1920. int asCByteCode::InstrW_QW(asEBCInstr bc, asWORD a, asQWORD b)
  1921. {
  1922. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_QW_ARG);
  1923. asASSERT(asBCInfo[bc].stackInc == 0);
  1924. if( AddInstruction() < 0 )
  1925. return 0;
  1926. last->op = bc;
  1927. last->wArg[0] = a;
  1928. *ARG_QW(last->arg) = b;
  1929. last->size = asBCTypeSize[asBCInfo[bc].type];
  1930. last->stackInc = asBCInfo[bc].stackInc;
  1931. return last->stackInc;
  1932. }
  1933. int asCByteCode::InstrSHORT_QW(asEBCInstr bc, short a, asQWORD b)
  1934. {
  1935. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_QW_ARG);
  1936. asASSERT(asBCInfo[bc].stackInc == 0);
  1937. if( AddInstruction() < 0 )
  1938. return 0;
  1939. last->op = bc;
  1940. last->wArg[0] = a;
  1941. *ARG_QW(last->arg) = b;
  1942. last->size = asBCTypeSize[asBCInfo[bc].type];
  1943. last->stackInc = asBCInfo[bc].stackInc;
  1944. return last->stackInc;
  1945. }
  1946. int asCByteCode::InstrW_FLOAT(asEBCInstr bc, asWORD a, float b)
  1947. {
  1948. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_DW_ARG);
  1949. asASSERT(asBCInfo[bc].stackInc == 0);
  1950. if( AddInstruction() < 0 )
  1951. return 0;
  1952. last->op = bc;
  1953. last->wArg[0] = a;
  1954. *((float*) ARG_DW(last->arg)) = b;
  1955. last->size = asBCTypeSize[asBCInfo[bc].type];
  1956. last->stackInc = asBCInfo[bc].stackInc;
  1957. return last->stackInc;
  1958. }
  1959. int asCByteCode::InstrSHORT(asEBCInstr bc, short param)
  1960. {
  1961. asASSERT(asBCInfo[bc].type == asBCTYPE_rW_ARG ||
  1962. asBCInfo[bc].type == asBCTYPE_wW_ARG ||
  1963. asBCInfo[bc].type == asBCTYPE_W_ARG);
  1964. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  1965. if( AddInstruction() < 0 )
  1966. return 0;
  1967. last->op = bc;
  1968. last->wArg[0] = param;
  1969. last->size = asBCTypeSize[asBCInfo[bc].type];
  1970. last->stackInc = asBCInfo[bc].stackInc;
  1971. return last->stackInc;
  1972. }
  1973. int asCByteCode::InstrINT(asEBCInstr bc, int param)
  1974. {
  1975. asASSERT(asBCInfo[bc].type == asBCTYPE_DW_ARG);
  1976. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  1977. if( AddInstruction() < 0 )
  1978. return 0;
  1979. last->op = bc;
  1980. *((int*) ARG_DW(last->arg)) = param;
  1981. last->size = asBCTypeSize[asBCInfo[bc].type];
  1982. last->stackInc = asBCInfo[bc].stackInc;
  1983. return last->stackInc;
  1984. }
  1985. int asCByteCode::InstrDWORD(asEBCInstr bc, asDWORD param)
  1986. {
  1987. asASSERT(asBCInfo[bc].type == asBCTYPE_DW_ARG);
  1988. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  1989. if( AddInstruction() < 0 )
  1990. return 0;
  1991. last->op = bc;
  1992. *ARG_DW(last->arg) = param;
  1993. last->size = asBCTypeSize[asBCInfo[bc].type];
  1994. last->stackInc = asBCInfo[bc].stackInc;
  1995. return last->stackInc;
  1996. }
  1997. int asCByteCode::InstrPTR(asEBCInstr bc, void *param)
  1998. {
  1999. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2000. if( AddInstruction() < 0 )
  2001. return 0;
  2002. last->op = bc;
  2003. asASSERT(asBCInfo[bc].type == asBCTYPE_PTR_ARG);
  2004. *ARG_PTR(last->arg) = (asPWORD)param;
  2005. last->size = asBCTypeSize[asBCInfo[bc].type];
  2006. last->stackInc = asBCInfo[bc].stackInc;
  2007. return last->stackInc;
  2008. }
  2009. int asCByteCode::InstrQWORD(asEBCInstr bc, asQWORD param)
  2010. {
  2011. asASSERT(asBCInfo[bc].type == asBCTYPE_QW_ARG);
  2012. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2013. if( AddInstruction() < 0 )
  2014. return 0;
  2015. last->op = bc;
  2016. *ARG_QW(last->arg) = param;
  2017. last->size = asBCTypeSize[asBCInfo[bc].type];
  2018. last->stackInc = asBCInfo[bc].stackInc;
  2019. return last->stackInc;
  2020. }
  2021. int asCByteCode::InstrWORD(asEBCInstr bc, asWORD param)
  2022. {
  2023. asASSERT(asBCInfo[bc].type == asBCTYPE_W_ARG ||
  2024. asBCInfo[bc].type == asBCTYPE_rW_ARG ||
  2025. asBCInfo[bc].type == asBCTYPE_wW_ARG);
  2026. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2027. if( AddInstruction() < 0 )
  2028. return 0;
  2029. last->op = bc;
  2030. last->wArg[0] = param;
  2031. last->size = asBCTypeSize[asBCInfo[bc].type];
  2032. last->stackInc = asBCInfo[bc].stackInc;
  2033. return last->stackInc;
  2034. }
  2035. int asCByteCode::InstrFLOAT(asEBCInstr bc, float param)
  2036. {
  2037. asASSERT(asBCInfo[bc].type == asBCTYPE_DW_ARG);
  2038. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2039. if( AddInstruction() < 0 )
  2040. return 0;
  2041. last->op = bc;
  2042. *((float*) ARG_DW(last->arg)) = param;
  2043. last->size = asBCTypeSize[asBCInfo[bc].type];
  2044. last->stackInc = asBCInfo[bc].stackInc;
  2045. return last->stackInc;
  2046. }
  2047. int asCByteCode::InstrDOUBLE(asEBCInstr bc, double param)
  2048. {
  2049. asASSERT(asBCInfo[bc].type == asBCTYPE_QW_ARG);
  2050. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2051. if( AddInstruction() < 0 )
  2052. return 0;
  2053. last->op = bc;
  2054. *((double*) ARG_QW(last->arg)) = param;
  2055. last->size = asBCTypeSize[asBCInfo[bc].type];
  2056. last->stackInc = asBCInfo[bc].stackInc;
  2057. return last->stackInc;
  2058. }
  2059. int asCByteCode::GetLastInstr()
  2060. {
  2061. if( last == 0 ) return -1;
  2062. return last->op;
  2063. }
  2064. int asCByteCode::RemoveLastInstr()
  2065. {
  2066. if( last == 0 ) return -1;
  2067. if( first == last )
  2068. {
  2069. engine->memoryMgr.FreeByteInstruction(last);
  2070. first = 0;
  2071. last = 0;
  2072. }
  2073. else
  2074. {
  2075. cByteInstruction *bc = last;
  2076. last = bc->prev;
  2077. bc->Remove();
  2078. engine->memoryMgr.FreeByteInstruction(bc);
  2079. }
  2080. return 0;
  2081. }
  2082. asDWORD asCByteCode::GetLastInstrValueDW()
  2083. {
  2084. if( last == 0 ) return 0;
  2085. return *ARG_DW(last->arg);
  2086. }
  2087. void asCByteCode::DefineTemporaryVariable(int varOffset)
  2088. {
  2089. temporaryVariables.PushLast(varOffset);
  2090. }
  2091. //===================================================================
  2092. cByteInstruction::cByteInstruction()
  2093. {
  2094. next = 0;
  2095. prev = 0;
  2096. op = asBC_LABEL;
  2097. arg = 0;
  2098. wArg[0] = 0;
  2099. wArg[1] = 0;
  2100. wArg[2] = 0;
  2101. size = 0;
  2102. stackInc = 0;
  2103. marked = false;
  2104. stackSize = 0;
  2105. }
  2106. void cByteInstruction::AddAfter(cByteInstruction *nextCode)
  2107. {
  2108. if( next )
  2109. next->prev = nextCode;
  2110. nextCode->next = next;
  2111. nextCode->prev = this;
  2112. next = nextCode;
  2113. }
  2114. void cByteInstruction::AddBefore(cByteInstruction *prevCode)
  2115. {
  2116. if( prev )
  2117. prev->next = prevCode;
  2118. prevCode->prev = prev;
  2119. prevCode->next = this;
  2120. prev = prevCode;
  2121. }
  2122. int cByteInstruction::GetSize()
  2123. {
  2124. return size;
  2125. }
  2126. int cByteInstruction::GetStackIncrease()
  2127. {
  2128. return stackInc;
  2129. }
  2130. void cByteInstruction::Remove()
  2131. {
  2132. if( prev ) prev->next = next;
  2133. if( next ) next->prev = prev;
  2134. prev = 0;
  2135. next = 0;
  2136. }
  2137. END_AS_NAMESPACE
  2138. #endif // AS_NO_COMPILER