as_bytecode.cpp 81 KB

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