as_bytecode.cpp 83 KB

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