as_bytecode.cpp 86 KB

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