as_bytecode.cpp 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2013 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. }
  905. }
  906. // Optimize unnecessary refcpy for return handle. This scenario only happens for return statements
  907. // and LOADOBJ can only be the last instruction before the RET, so doing this check after the rest of
  908. // the optimizations have taken place saves us time.
  909. if( last && last->op == asBC_LOADOBJ && IsTemporary(last->wArg[0]) )
  910. {
  911. // A temporary handle is being loaded into the object register.
  912. // Let's look for a trivial RefCpyV to that temporary variable, and a Free of the original
  913. // variable. If this is found, then we can simply load the original value into the register
  914. // and avoid both the RefCpy and the Free.
  915. short tempVar = last->wArg[0];
  916. asCArray<short> freedVars;
  917. asCByteInstruction *instr = last->prev;
  918. asASSERT( instr && instr->op == asBC_Block );
  919. instr = instr->prev;
  920. while( instr && instr->op == asBC_FREE )
  921. {
  922. freedVars.PushLast(instr->wArg[0]);
  923. instr = instr->prev;
  924. }
  925. // If there is any non-trivial cleanups, e.g. call to destructors, then we skip this optimizations
  926. // TODO: runtime optimize: Do we need to skip it? Is there really a chance the local variable
  927. // will be invalidated while the destructor, or any other function for
  928. // that matter, is being called?
  929. if( instr && instr->op == asBC_Block )
  930. {
  931. // We expect a sequence PshVPtr, RefCpyV, PopPtr just before the clean up block
  932. instr = instr->prev;
  933. if( instr && instr->op == asBC_PopPtr ) instr = instr->prev;
  934. if( instr && instr->op == asBC_RefCpyV && instr->wArg[0] == tempVar ) instr = instr->prev;
  935. if( instr && instr->op == asBC_PshVPtr && freedVars.Exists(instr->wArg[0]) )
  936. {
  937. // Update the LOADOBJ to load the local variable directly
  938. tempVar = instr->wArg[0];
  939. last->wArg[0] = tempVar;
  940. // Remove the copy of the local variable into the temp
  941. DeleteInstruction(instr->next); // deletes RefCpyV
  942. DeleteInstruction(instr->next); // deletes PopPtr
  943. DeleteInstruction(instr); // deletes PshVPtr
  944. // Find and remove the FREE instruction for the local variable too
  945. instr = last->prev->prev;
  946. while( instr )
  947. {
  948. asASSERT( instr->op == asBC_FREE );
  949. if( instr->wArg[0] == tempVar )
  950. {
  951. DeleteInstruction(instr);
  952. break;
  953. }
  954. instr = instr->prev;
  955. }
  956. }
  957. }
  958. }
  959. }
  960. void asCByteCode::Optimize()
  961. {
  962. // This function performs the optimizations that require global knowledge of the entire function
  963. TimeIt("asCByteCode::Optimize");
  964. if( !engine->ep.optimizeByteCode )
  965. return;
  966. // TODO: runtime optimize: The optimizer should be able to inline function calls.
  967. // If the called function has only a few instructions, the function call should be inlined.
  968. // This is especially useful with the factory stubs used for template types and script classes.
  969. asCByteInstruction *instr = first;
  970. while( instr )
  971. {
  972. asCByteInstruction *curr = instr;
  973. instr = instr->next;
  974. const asEBCInstr currOp = curr->op;
  975. // Delete JitEntry if the JIT instructions are not supposed to be included
  976. if( currOp == asBC_JitEntry && !engine->ep.includeJitInstructions )
  977. {
  978. instr = GoBack(DeleteInstruction(curr));
  979. continue;
  980. }
  981. if( instr )
  982. {
  983. const asEBCInstr instrOp = instr->op;
  984. // PopPtr, RET b -> RET b
  985. if( currOp == asBC_PopPtr && instrOp == asBC_RET )
  986. {
  987. // We don't combine the PopPtr+RET because RET first restores
  988. // the previous stack pointer and then pops the arguments
  989. // Delete PopPtr
  990. instr = GoBack(DeleteInstruction(curr));
  991. }
  992. else if( currOp == asBC_SUSPEND )
  993. {
  994. // SUSPEND, JitEntry, SUSPEND -> SUSPEND
  995. if( instrOp == asBC_JitEntry && instr->next && instr->next->op == asBC_SUSPEND )
  996. {
  997. // Delete the two first instructions
  998. DeleteInstruction(instr);
  999. instr = GoBack(DeleteInstruction(curr));
  1000. }
  1001. // SUSPEND, SUSPEND -> SUSPEND
  1002. else if( instrOp == asBC_SUSPEND )
  1003. {
  1004. // Delete the first instruction
  1005. instr = GoBack(DeleteInstruction(curr));
  1006. }
  1007. // SUSPEND, Block, SUSPEND -> Block, SUSPEND
  1008. else if( instrOp == asBC_Block && instr->next && instr->next->op == asBC_SUSPEND )
  1009. {
  1010. // Delete the first instruction
  1011. instr = GoBack(DeleteInstruction(curr));
  1012. }
  1013. }
  1014. else if( currOp == asBC_LINE )
  1015. {
  1016. // LINE, JitEntry, LINE -> LINE
  1017. if( instrOp == asBC_JitEntry && instr->next && instr->next->op == asBC_LINE )
  1018. {
  1019. // Delete the two first instructions
  1020. DeleteInstruction(instr);
  1021. instr = GoBack(DeleteInstruction(curr));
  1022. }
  1023. // LINE, LINE -> LINE
  1024. else if( instrOp == asBC_LINE )
  1025. {
  1026. // Delete the first instruction
  1027. instr = GoBack(DeleteInstruction(curr));
  1028. }
  1029. // LINE, Block, LINE -> Block, LINE
  1030. else if( instrOp == asBC_Block && instr->next && instr->next->op == asBC_LINE )
  1031. {
  1032. // Delete the first instruction
  1033. instr = GoBack(DeleteInstruction(curr));
  1034. }
  1035. }
  1036. // JMP +0 -> remove
  1037. else if( currOp == asBC_JMP && instrOp == asBC_LABEL && *(int*)&curr->arg == instr->wArg[0] )
  1038. instr = GoBack(DeleteInstruction(curr));
  1039. }
  1040. }
  1041. }
  1042. bool asCByteCode::IsTempVarReadByInstr(asCByteInstruction *curr, int offset)
  1043. {
  1044. // Which instructions read from variables?
  1045. if( asBCInfo[curr->op].type == asBCTYPE_wW_rW_rW_ARG &&
  1046. (int(curr->wArg[1]) == offset || int(curr->wArg[2]) == offset) )
  1047. return true;
  1048. else if( (asBCInfo[curr->op].type == asBCTYPE_rW_ARG ||
  1049. asBCInfo[curr->op].type == asBCTYPE_rW_DW_ARG ||
  1050. asBCInfo[curr->op].type == asBCTYPE_rW_QW_ARG ||
  1051. asBCInfo[curr->op].type == asBCTYPE_rW_W_DW_ARG ||
  1052. asBCInfo[curr->op].type == asBCTYPE_rW_DW_DW_ARG ||
  1053. curr->op == asBC_FREE) && // FREE both read and write to the variable
  1054. int(curr->wArg[0]) == offset )
  1055. return true;
  1056. else if( (asBCInfo[curr->op].type == asBCTYPE_wW_rW_ARG ||
  1057. asBCInfo[curr->op].type == asBCTYPE_wW_rW_DW_ARG) &&
  1058. int(curr->wArg[1]) == offset )
  1059. return true;
  1060. else if( asBCInfo[curr->op].type == asBCTYPE_rW_rW_ARG &&
  1061. (int(curr->wArg[0]) == offset || int(curr->wArg[1]) == offset) )
  1062. return true;
  1063. else if( curr->op == asBC_LoadThisR && offset == 0 )
  1064. return true;
  1065. return false;
  1066. }
  1067. bool asCByteCode::IsInstrJmpOrLabel(asCByteInstruction *curr)
  1068. {
  1069. if( curr->op == asBC_JS ||
  1070. curr->op == asBC_JNS ||
  1071. curr->op == asBC_JP ||
  1072. curr->op == asBC_JNP ||
  1073. curr->op == asBC_JMPP ||
  1074. curr->op == asBC_JMP ||
  1075. curr->op == asBC_JZ ||
  1076. curr->op == asBC_JNZ ||
  1077. curr->op == asBC_JLowZ ||
  1078. curr->op == asBC_JLowNZ ||
  1079. curr->op == asBC_LABEL )
  1080. return true;
  1081. return false;
  1082. }
  1083. bool asCByteCode::IsTempVarOverwrittenByInstr(asCByteInstruction *curr, int offset)
  1084. {
  1085. // Which instructions overwrite the variable or discard it?
  1086. if( curr->op == asBC_RET ||
  1087. curr->op == asBC_SUSPEND )
  1088. return true;
  1089. else if( (asBCInfo[curr->op].type == asBCTYPE_wW_rW_rW_ARG ||
  1090. asBCInfo[curr->op].type == asBCTYPE_wW_rW_ARG ||
  1091. asBCInfo[curr->op].type == asBCTYPE_wW_rW_DW_ARG ||
  1092. asBCInfo[curr->op].type == asBCTYPE_wW_ARG ||
  1093. asBCInfo[curr->op].type == asBCTYPE_wW_W_ARG ||
  1094. asBCInfo[curr->op].type == asBCTYPE_wW_DW_ARG ||
  1095. asBCInfo[curr->op].type == asBCTYPE_wW_QW_ARG) &&
  1096. int(curr->wArg[0]) == offset )
  1097. return true;
  1098. return false;
  1099. }
  1100. bool asCByteCode::IsTempVarRead(asCByteInstruction *curr, int offset)
  1101. {
  1102. TimeIt("asCByteCode::IsTempVarRead");
  1103. asCArray<asCByteInstruction *> openPaths;
  1104. asCArray<asCByteInstruction *> closedPaths;
  1105. // We're not interested in the first instruction, since it is the one that sets the variable
  1106. openPaths.PushLast(curr->next);
  1107. while( openPaths.GetLength() )
  1108. {
  1109. curr = openPaths.PopLast();
  1110. // Add the instruction to the closed paths so that we don't verify it again
  1111. closedPaths.PushLast(curr);
  1112. while( curr )
  1113. {
  1114. if( IsTempVarReadByInstr(curr, offset) )
  1115. return true;
  1116. if( IsTempVarOverwrittenByInstr(curr, offset) ) break;
  1117. // In case of jumps, we must follow the each of the paths
  1118. if( curr->op == asBC_JMP )
  1119. {
  1120. // Find the destination. If it cannot be found it is because we're doing a localized
  1121. // optimization and the label hasn't been added to the final bytecode yet
  1122. int label = *((int*)ARG_DW(curr->arg));
  1123. int r = FindLabel(label, curr, &curr, 0);
  1124. if( r >= 0 &&
  1125. !closedPaths.Exists(curr) &&
  1126. !openPaths.Exists(curr) )
  1127. openPaths.PushLast(curr);
  1128. break;
  1129. }
  1130. else if( curr->op == asBC_JZ || curr->op == asBC_JNZ ||
  1131. curr->op == asBC_JS || curr->op == asBC_JNS ||
  1132. curr->op == asBC_JP || curr->op == asBC_JNP ||
  1133. curr->op == asBC_JLowZ || curr->op == asBC_JLowNZ )
  1134. {
  1135. // Find the destination. If it cannot be found it is because we're doing a localized
  1136. // optimization and the label hasn't been added to the final bytecode yet
  1137. asCByteInstruction *dest = 0;
  1138. int label = *((int*)ARG_DW(curr->arg));
  1139. int r = FindLabel(label, curr, &dest, 0);
  1140. if( r >= 0 &&
  1141. !closedPaths.Exists(dest) &&
  1142. !openPaths.Exists(dest) )
  1143. openPaths.PushLast(dest);
  1144. }
  1145. else if( curr->op == asBC_JMPP )
  1146. {
  1147. // A JMPP instruction is always followed by a series of JMP instructions
  1148. // that give the real destination (like a look-up table). We need add all
  1149. // of these as open paths.
  1150. curr = curr->next;
  1151. while( curr->op == asBC_JMP )
  1152. {
  1153. // Find the destination. If it cannot be found it is because we're doing a localized
  1154. // optimization and the label hasn't been added to the final bytecode yet
  1155. asCByteInstruction *dest = 0;
  1156. int label = *((int*)ARG_DW(curr->arg));
  1157. int r = FindLabel(label, curr, &dest, 0);
  1158. if( r >= 0 &&
  1159. !closedPaths.Exists(dest) &&
  1160. !openPaths.Exists(dest) )
  1161. openPaths.PushLast(dest);
  1162. curr = curr->next;
  1163. }
  1164. // We should now be on a label which is the destination of the
  1165. // first JMP in the sequence and is already added in the open paths
  1166. asASSERT(curr->op == asBC_LABEL);
  1167. break;
  1168. }
  1169. curr = curr->next;
  1170. }
  1171. }
  1172. return false;
  1173. }
  1174. bool asCByteCode::IsTempRegUsed(asCByteInstruction *curr)
  1175. {
  1176. TimeIt("asCByteCode::IsTempRegUsed");
  1177. // We're not interested in the first instruction, since it is the one that sets the register
  1178. while( curr->next )
  1179. {
  1180. curr = curr->next;
  1181. // Which instructions read from the register?
  1182. if( curr->op == asBC_INCi ||
  1183. curr->op == asBC_INCi16 ||
  1184. curr->op == asBC_INCi8 ||
  1185. curr->op == asBC_INCf ||
  1186. curr->op == asBC_INCd ||
  1187. curr->op == asBC_DECi ||
  1188. curr->op == asBC_DECi16 ||
  1189. curr->op == asBC_DECi8 ||
  1190. curr->op == asBC_DECf ||
  1191. curr->op == asBC_DECd ||
  1192. curr->op == asBC_WRTV1 ||
  1193. curr->op == asBC_WRTV2 ||
  1194. curr->op == asBC_WRTV4 ||
  1195. curr->op == asBC_WRTV8 ||
  1196. curr->op == asBC_RDR1 ||
  1197. curr->op == asBC_RDR2 ||
  1198. curr->op == asBC_RDR4 ||
  1199. curr->op == asBC_RDR8 ||
  1200. curr->op == asBC_PshRPtr ||
  1201. curr->op == asBC_CpyRtoV4 ||
  1202. curr->op == asBC_CpyRtoV8 ||
  1203. curr->op == asBC_TZ ||
  1204. curr->op == asBC_TNZ ||
  1205. curr->op == asBC_TS ||
  1206. curr->op == asBC_TNS ||
  1207. curr->op == asBC_TP ||
  1208. curr->op == asBC_TNP ||
  1209. curr->op == asBC_JZ ||
  1210. curr->op == asBC_JNZ ||
  1211. curr->op == asBC_JLowZ ||
  1212. curr->op == asBC_JLowNZ ||
  1213. curr->op == asBC_JS ||
  1214. curr->op == asBC_JNS ||
  1215. curr->op == asBC_JP ||
  1216. curr->op == asBC_JNP )
  1217. return true;
  1218. // Which instructions overwrite the register or discard the value?
  1219. if( curr->op == asBC_CALL ||
  1220. curr->op == asBC_PopRPtr ||
  1221. curr->op == asBC_CALLSYS ||
  1222. curr->op == asBC_CALLBND ||
  1223. curr->op == asBC_SUSPEND ||
  1224. curr->op == asBC_ALLOC ||
  1225. curr->op == asBC_CpyVtoR4 ||
  1226. curr->op == asBC_LdGRdR4 ||
  1227. curr->op == asBC_LDG ||
  1228. curr->op == asBC_LDV ||
  1229. curr->op == asBC_TZ ||
  1230. curr->op == asBC_TNZ ||
  1231. curr->op == asBC_TS ||
  1232. curr->op == asBC_TNS ||
  1233. curr->op == asBC_TP ||
  1234. curr->op == asBC_TNP ||
  1235. curr->op == asBC_JS ||
  1236. curr->op == asBC_JNS ||
  1237. curr->op == asBC_JP ||
  1238. curr->op == asBC_JNP ||
  1239. curr->op == asBC_JMPP ||
  1240. curr->op == asBC_JMP ||
  1241. curr->op == asBC_JZ ||
  1242. curr->op == asBC_JNZ ||
  1243. curr->op == asBC_JLowZ ||
  1244. curr->op == asBC_JLowNZ ||
  1245. curr->op == asBC_CMPi ||
  1246. curr->op == asBC_CMPu ||
  1247. curr->op == asBC_CMPf ||
  1248. curr->op == asBC_CMPd ||
  1249. curr->op == asBC_CMPIi ||
  1250. curr->op == asBC_CMPIu ||
  1251. curr->op == asBC_CMPIf ||
  1252. curr->op == asBC_LABEL ||
  1253. curr->op == asBC_LoadThisR ||
  1254. curr->op == asBC_LoadRObjR ||
  1255. curr->op == asBC_LoadVObjR )
  1256. return false;
  1257. }
  1258. return false;
  1259. }
  1260. bool asCByteCode::IsSimpleExpression()
  1261. {
  1262. // A simple expression is one that cannot be suspended at any time, i.e.
  1263. // it doesn't have any calls to other routines, and doesn't have any suspend instructions
  1264. asCByteInstruction *instr = first;
  1265. while( instr )
  1266. {
  1267. if( instr->op == asBC_ALLOC ||
  1268. instr->op == asBC_CALL ||
  1269. instr->op == asBC_CALLSYS ||
  1270. instr->op == asBC_SUSPEND ||
  1271. instr->op == asBC_LINE ||
  1272. instr->op == asBC_FREE ||
  1273. instr->op == asBC_CallPtr ||
  1274. instr->op == asBC_CALLINTF ||
  1275. instr->op == asBC_CALLBND )
  1276. return false;
  1277. instr = instr->next;
  1278. }
  1279. return true;
  1280. }
  1281. void asCByteCode::ExtractLineNumbers()
  1282. {
  1283. // This function will extract the line number and source file for each statement by looking for LINE instructions.
  1284. // The LINE instructions will be converted to SUSPEND instructions, or removed depending on the configuration.
  1285. TimeIt("asCByteCode::ExtractLineNumbers");
  1286. int lastLinePos = -1;
  1287. int pos = 0;
  1288. asCByteInstruction *instr = first;
  1289. while( instr )
  1290. {
  1291. asCByteInstruction *curr = instr;
  1292. instr = instr->next;
  1293. if( curr->op == asBC_LINE )
  1294. {
  1295. if( lastLinePos == pos )
  1296. {
  1297. lineNumbers.PopLast(); // pop position
  1298. lineNumbers.PopLast(); // pop line number
  1299. sectionIdxs.PopLast(); // pop section index
  1300. }
  1301. lastLinePos = pos;
  1302. lineNumbers.PushLast(pos);
  1303. lineNumbers.PushLast(*(int*)ARG_DW(curr->arg));
  1304. sectionIdxs.PushLast(*((int*)ARG_DW(curr->arg)+1));
  1305. if( !engine->ep.buildWithoutLineCues )
  1306. {
  1307. // Transform BC_LINE into BC_SUSPEND
  1308. curr->op = asBC_SUSPEND;
  1309. curr->size = asBCTypeSize[asBCInfo[asBC_SUSPEND].type];
  1310. pos += curr->size;
  1311. }
  1312. else
  1313. {
  1314. // Delete the instruction
  1315. DeleteInstruction(curr);
  1316. }
  1317. }
  1318. else
  1319. pos += curr->size;
  1320. }
  1321. }
  1322. void asCByteCode::ExtractObjectVariableInfo(asCScriptFunction *outFunc)
  1323. {
  1324. asASSERT( outFunc->scriptData );
  1325. unsigned int pos = 0;
  1326. asCByteInstruction *instr = first;
  1327. int blockLevel = 0;
  1328. while( instr )
  1329. {
  1330. if( instr->op == asBC_Block )
  1331. {
  1332. asSObjectVariableInfo info;
  1333. info.programPos = pos;
  1334. info.variableOffset = 0;
  1335. info.option = instr->wArg[0] ? asBLOCK_BEGIN : asBLOCK_END;
  1336. if( info.option == asBLOCK_BEGIN )
  1337. {
  1338. blockLevel++;
  1339. outFunc->scriptData->objVariableInfo.PushLast(info);
  1340. }
  1341. else
  1342. {
  1343. blockLevel--;
  1344. asASSERT( blockLevel >= 0 );
  1345. if( outFunc->scriptData->objVariableInfo[outFunc->scriptData->objVariableInfo.GetLength()-1].option == asBLOCK_BEGIN &&
  1346. outFunc->scriptData->objVariableInfo[outFunc->scriptData->objVariableInfo.GetLength()-1].programPos == pos )
  1347. outFunc->scriptData->objVariableInfo.PopLast();
  1348. else
  1349. outFunc->scriptData->objVariableInfo.PushLast(info);
  1350. }
  1351. }
  1352. else if( instr->op == asBC_ObjInfo )
  1353. {
  1354. asSObjectVariableInfo info;
  1355. info.programPos = pos;
  1356. info.variableOffset = (short)instr->wArg[0];
  1357. info.option = *(int*)ARG_DW(instr->arg);
  1358. outFunc->scriptData->objVariableInfo.PushLast(info);
  1359. }
  1360. else if( instr->op == asBC_VarDecl )
  1361. {
  1362. outFunc->scriptData->variables[instr->wArg[0]]->declaredAtProgramPos = pos;
  1363. }
  1364. else
  1365. pos += instr->size;
  1366. instr = instr->next;
  1367. }
  1368. asASSERT( blockLevel == 0 );
  1369. }
  1370. int asCByteCode::GetSize()
  1371. {
  1372. int size = 0;
  1373. asCByteInstruction *instr = first;
  1374. while( instr )
  1375. {
  1376. size += instr->GetSize();
  1377. instr = instr->next;
  1378. }
  1379. return size;
  1380. }
  1381. void asCByteCode::AddCode(asCByteCode *bc)
  1382. {
  1383. if( bc->first )
  1384. {
  1385. if( first == 0 )
  1386. {
  1387. first = bc->first;
  1388. last = bc->last;
  1389. bc->first = 0;
  1390. bc->last = 0;
  1391. }
  1392. else
  1393. {
  1394. last->next = bc->first;
  1395. bc->first->prev = last;
  1396. last = bc->last;
  1397. bc->first = 0;
  1398. bc->last = 0;
  1399. }
  1400. }
  1401. }
  1402. int asCByteCode::AddInstruction()
  1403. {
  1404. void *ptr = engine->memoryMgr.AllocByteInstruction();
  1405. if( ptr == 0 )
  1406. {
  1407. // Out of memory
  1408. return 0;
  1409. }
  1410. asCByteInstruction *instr = new(ptr) asCByteInstruction();
  1411. if( first == 0 )
  1412. {
  1413. first = last = instr;
  1414. }
  1415. else
  1416. {
  1417. last->AddAfter(instr);
  1418. last = instr;
  1419. }
  1420. return 0;
  1421. }
  1422. int asCByteCode::AddInstructionFirst()
  1423. {
  1424. void *ptr = engine->memoryMgr.AllocByteInstruction();
  1425. if( ptr == 0 )
  1426. {
  1427. // Out of memory
  1428. return 0;
  1429. }
  1430. asCByteInstruction *instr = new(ptr) asCByteInstruction();
  1431. if( first == 0 )
  1432. {
  1433. first = last = instr;
  1434. }
  1435. else
  1436. {
  1437. first->AddBefore(instr);
  1438. first = instr;
  1439. }
  1440. return 0;
  1441. }
  1442. void asCByteCode::Call(asEBCInstr instr, int funcID, int pop)
  1443. {
  1444. if( AddInstruction() < 0 )
  1445. return;
  1446. asASSERT(asBCInfo[instr].type == asBCTYPE_DW_ARG);
  1447. last->op = instr;
  1448. last->size = asBCTypeSize[asBCInfo[instr].type];
  1449. last->stackInc = -pop; // BC_CALL and BC_CALLBND doesn't pop the argument but when the callee returns the arguments are already popped
  1450. *((int*)ARG_DW(last->arg)) = funcID;
  1451. // Add a JitEntry instruction after function calls so that JIT's can resume execution
  1452. InstrPTR(asBC_JitEntry, 0);
  1453. }
  1454. void asCByteCode::CallPtr(asEBCInstr instr, int funcPtrVar, int pop)
  1455. {
  1456. if( AddInstruction() < 0 )
  1457. return;
  1458. asASSERT(asBCInfo[instr].type == asBCTYPE_rW_ARG);
  1459. last->op = instr;
  1460. last->size = asBCTypeSize[asBCInfo[instr].type];
  1461. last->stackInc = -pop;
  1462. last->wArg[0] = (short)funcPtrVar;
  1463. // Add a JitEntry instruction after function calls so that JIT's can resume execution
  1464. InstrPTR(asBC_JitEntry, 0);
  1465. }
  1466. void asCByteCode::Alloc(asEBCInstr instr, void *objID, int funcID, int pop)
  1467. {
  1468. if( AddInstruction() < 0 )
  1469. return;
  1470. last->op = instr;
  1471. last->size = asBCTypeSize[asBCInfo[instr].type];
  1472. last->stackInc = -pop; // BC_ALLOC
  1473. asASSERT(asBCInfo[instr].type == asBCTYPE_PTR_DW_ARG);
  1474. *ARG_PTR(last->arg) = (asPWORD)objID;
  1475. *((int*)(ARG_DW(last->arg)+AS_PTR_SIZE)) = funcID;
  1476. // Add a JitEntry instruction after function calls so that JIT's can resume execution
  1477. InstrPTR(asBC_JitEntry, 0);
  1478. }
  1479. void asCByteCode::Ret(int pop)
  1480. {
  1481. if( AddInstruction() < 0 )
  1482. return;
  1483. asASSERT(asBCInfo[asBC_RET].type == asBCTYPE_W_ARG);
  1484. last->op = asBC_RET;
  1485. last->size = asBCTypeSize[asBCInfo[asBC_RET].type];
  1486. last->stackInc = 0; // The instruction pops the argument, but it doesn't affect current function
  1487. last->wArg[0] = (short)pop;
  1488. }
  1489. void asCByteCode::JmpP(int var, asDWORD max)
  1490. {
  1491. if( AddInstruction() < 0 )
  1492. return;
  1493. asASSERT(asBCInfo[asBC_JMPP].type == asBCTYPE_rW_ARG);
  1494. last->op = asBC_JMPP;
  1495. last->size = asBCTypeSize[asBCInfo[asBC_JMPP].type];
  1496. last->stackInc = asBCInfo[asBC_JMPP].stackInc;
  1497. last->wArg[0] = (short)var;
  1498. // Store the largest jump that is made for PostProcess()
  1499. *ARG_DW(last->arg) = max;
  1500. }
  1501. void asCByteCode::Label(short label)
  1502. {
  1503. if( AddInstruction() < 0 )
  1504. return;
  1505. last->op = asBC_LABEL;
  1506. last->size = 0;
  1507. last->stackInc = 0;
  1508. last->wArg[0] = label;
  1509. }
  1510. void asCByteCode::Line(int line, int column, int scriptIdx)
  1511. {
  1512. if( AddInstruction() < 0 )
  1513. return;
  1514. last->op = asBC_LINE;
  1515. // If the build is without line cues these instructions will be removed
  1516. // otherwise they will be transformed into SUSPEND instructions.
  1517. if( engine->ep.buildWithoutLineCues )
  1518. last->size = 0;
  1519. else
  1520. last->size = asBCTypeSize[asBCInfo[asBC_SUSPEND].type];
  1521. last->stackInc = 0;
  1522. *((int*)ARG_DW(last->arg)) = (line & 0xFFFFF)|((column & 0xFFF)<<20);
  1523. *((int*)ARG_DW(last->arg)+1) = scriptIdx;
  1524. // Add a JitEntry after the line instruction to allow the JIT function to resume after a suspend
  1525. InstrPTR(asBC_JitEntry, 0);
  1526. }
  1527. void asCByteCode::ObjInfo(int offset, int info)
  1528. {
  1529. if( AddInstruction() < 0 )
  1530. return;
  1531. // Add the special instruction that will be used to tell the exception
  1532. // handler when an object is initialized and deinitialized.
  1533. last->op = asBC_ObjInfo;
  1534. last->size = 0;
  1535. last->stackInc = 0;
  1536. last->wArg[0] = (short)offset;
  1537. *((int*)ARG_DW(last->arg)) = info;
  1538. }
  1539. void asCByteCode::Block(bool start)
  1540. {
  1541. if( AddInstruction() < 0 )
  1542. return;
  1543. last->op = asBC_Block;
  1544. last->size = 0;
  1545. last->stackInc = 0;
  1546. last->wArg[0] = start ? 1 : 0;
  1547. }
  1548. void asCByteCode::VarDecl(int varDeclIdx)
  1549. {
  1550. if( AddInstruction() < 0 )
  1551. return;
  1552. last->op = asBC_VarDecl;
  1553. last->size = 0;
  1554. last->stackInc = 0;
  1555. last->wArg[0] = asWORD(varDeclIdx);
  1556. }
  1557. int asCByteCode::FindLabel(int label, asCByteInstruction *from, asCByteInstruction **dest, int *positionDelta)
  1558. {
  1559. TimeIt("asCByteCode::FindLabel");
  1560. // Search forward
  1561. int labelPos = -from->GetSize();
  1562. asCByteInstruction *labelInstr = from;
  1563. while( labelInstr )
  1564. {
  1565. labelPos += labelInstr->GetSize();
  1566. labelInstr = labelInstr->next;
  1567. if( labelInstr && labelInstr->op == asBC_LABEL )
  1568. {
  1569. if( labelInstr->wArg[0] == label )
  1570. break;
  1571. }
  1572. }
  1573. if( labelInstr == 0 )
  1574. {
  1575. // Search backwards
  1576. labelPos = -from->GetSize();
  1577. labelInstr = from;
  1578. while( labelInstr )
  1579. {
  1580. labelInstr = labelInstr->prev;
  1581. if( labelInstr )
  1582. {
  1583. labelPos -= labelInstr->GetSize();
  1584. if( labelInstr->op == asBC_LABEL )
  1585. {
  1586. if( labelInstr->wArg[0] == label )
  1587. break;
  1588. }
  1589. }
  1590. }
  1591. }
  1592. if( labelInstr != 0 )
  1593. {
  1594. if( dest ) *dest = labelInstr;
  1595. if( positionDelta ) *positionDelta = labelPos;
  1596. return 0;
  1597. }
  1598. return -1;
  1599. }
  1600. int asCByteCode::ResolveJumpAddresses()
  1601. {
  1602. TimeIt("asCByteCode::ResolveJumpAddresses");
  1603. asCByteInstruction *instr = first;
  1604. while( instr )
  1605. {
  1606. if( instr->op == asBC_JMP ||
  1607. instr->op == asBC_JZ || instr->op == asBC_JNZ ||
  1608. instr->op == asBC_JLowZ || instr->op == asBC_JLowNZ ||
  1609. instr->op == asBC_JS || instr->op == asBC_JNS ||
  1610. instr->op == asBC_JP || instr->op == asBC_JNP )
  1611. {
  1612. int label = *((int*) ARG_DW(instr->arg));
  1613. int labelPosOffset;
  1614. int r = FindLabel(label, instr, 0, &labelPosOffset);
  1615. if( r == 0 )
  1616. *((int*) ARG_DW(instr->arg)) = labelPosOffset;
  1617. else
  1618. return -1;
  1619. }
  1620. instr = instr->next;
  1621. }
  1622. return 0;
  1623. }
  1624. asCByteInstruction *asCByteCode::DeleteInstruction(asCByteInstruction *instr)
  1625. {
  1626. if( instr == 0 ) return 0;
  1627. asCByteInstruction *ret = instr->prev ? instr->prev : instr->next;
  1628. RemoveInstruction(instr);
  1629. engine->memoryMgr.FreeByteInstruction(instr);
  1630. return ret;
  1631. }
  1632. void asCByteCode::Output(asDWORD *array)
  1633. {
  1634. TimeIt("asCByteCode::Output");
  1635. // TODO: Receive a script function pointer instead of the bytecode array
  1636. asDWORD *ap = array;
  1637. asCByteInstruction *instr = first;
  1638. while( instr )
  1639. {
  1640. if( instr->GetSize() > 0 )
  1641. {
  1642. *(asBYTE*)ap = asBYTE(instr->op);
  1643. *(((asBYTE*)ap)+1) = 0; // Second byte is always zero
  1644. switch( asBCInfo[instr->op].type )
  1645. {
  1646. case asBCTYPE_NO_ARG:
  1647. *(((asWORD*)ap)+1) = 0; // Clear upper bytes
  1648. break;
  1649. case asBCTYPE_wW_rW_rW_ARG:
  1650. *(((asWORD*)ap)+1) = instr->wArg[0];
  1651. *(((asWORD*)ap)+2) = instr->wArg[1];
  1652. *(((asWORD*)ap)+3) = instr->wArg[2];
  1653. break;
  1654. case asBCTYPE_wW_DW_ARG:
  1655. case asBCTYPE_rW_DW_ARG:
  1656. case asBCTYPE_W_DW_ARG:
  1657. *(((asWORD*)ap)+1) = instr->wArg[0];
  1658. *(ap+1) = *(asDWORD*)&instr->arg;
  1659. break;
  1660. case asBCTYPE_wW_rW_DW_ARG:
  1661. case asBCTYPE_rW_W_DW_ARG:
  1662. *(((asWORD*)ap)+1) = instr->wArg[0];
  1663. *(((asWORD*)ap)+2) = instr->wArg[1];
  1664. *(ap+2) = *(asDWORD*)&instr->arg;
  1665. break;
  1666. case asBCTYPE_wW_QW_ARG:
  1667. case asBCTYPE_rW_QW_ARG:
  1668. *(((asWORD*)ap)+1) = instr->wArg[0];
  1669. *(asQWORD*)(ap+1) = asQWORD(instr->arg);
  1670. break;
  1671. case asBCTYPE_W_ARG:
  1672. case asBCTYPE_rW_ARG:
  1673. case asBCTYPE_wW_ARG:
  1674. *(((asWORD*)ap)+1) = instr->wArg[0];
  1675. break;
  1676. case asBCTYPE_wW_rW_ARG:
  1677. case asBCTYPE_rW_rW_ARG:
  1678. case asBCTYPE_wW_W_ARG:
  1679. *(((asWORD *)ap)+1) = instr->wArg[0];
  1680. *(((asWORD *)ap)+2) = instr->wArg[1];
  1681. break;
  1682. case asBCTYPE_QW_DW_ARG:
  1683. case asBCTYPE_DW_DW_ARG:
  1684. case asBCTYPE_QW_ARG:
  1685. case asBCTYPE_DW_ARG:
  1686. *(((asWORD*)ap)+1) = 0; // Clear upper bytes
  1687. memcpy(ap+1, &instr->arg, instr->GetSize()*4-4);
  1688. break;
  1689. case asBCTYPE_rW_DW_DW_ARG:
  1690. *(((asWORD*)ap)+1) = instr->wArg[0];
  1691. memcpy(ap+1, &instr->arg, instr->GetSize()*4-4);
  1692. break;
  1693. default:
  1694. // How did we get here?
  1695. asASSERT(false);
  1696. break;
  1697. }
  1698. }
  1699. ap += instr->GetSize();
  1700. instr = instr->next;
  1701. }
  1702. }
  1703. void asCByteCode::PostProcess()
  1704. {
  1705. TimeIt("asCByteCode::PostProcess");
  1706. if( first == 0 ) return;
  1707. // This function will do the following
  1708. // - Verify if there is any code that never gets executed and remove it
  1709. // - Calculate the stack size at the position of each byte code
  1710. // - Calculate the largest stack needed
  1711. largestStackUsed = 0;
  1712. asCByteInstruction *instr = first;
  1713. while( instr )
  1714. {
  1715. instr->marked = false;
  1716. instr->stackSize = -1;
  1717. instr = instr->next;
  1718. }
  1719. // Add the first instruction to the list of unchecked code paths
  1720. asCArray<asCByteInstruction *> paths;
  1721. AddPath(paths, first, 0);
  1722. // Go through each of the code paths
  1723. for( asUINT p = 0; p < paths.GetLength(); ++p )
  1724. {
  1725. instr = paths[p];
  1726. int stackSize = instr->stackSize;
  1727. while( instr )
  1728. {
  1729. instr->marked = true;
  1730. instr->stackSize = stackSize;
  1731. stackSize += instr->stackInc;
  1732. if( stackSize > largestStackUsed )
  1733. largestStackUsed = stackSize;
  1734. if( instr->op == asBC_JMP )
  1735. {
  1736. // Find the label that we should jump to
  1737. int label = *((int*) ARG_DW(instr->arg));
  1738. asCByteInstruction *dest = 0;
  1739. int r = FindLabel(label, instr, &dest, 0); asASSERT( r == 0 ); UNUSED_VAR(r);
  1740. AddPath(paths, dest, stackSize);
  1741. break;
  1742. }
  1743. else if( instr->op == asBC_JZ || instr->op == asBC_JNZ ||
  1744. instr->op == asBC_JLowZ || instr->op == asBC_JLowNZ ||
  1745. instr->op == asBC_JS || instr->op == asBC_JNS ||
  1746. instr->op == asBC_JP || instr->op == asBC_JNP )
  1747. {
  1748. // Find the label that is being jumped to
  1749. int label = *((int*) ARG_DW(instr->arg));
  1750. asCByteInstruction *dest = 0;
  1751. int r = FindLabel(label, instr, &dest, 0); asASSERT( r == 0 ); UNUSED_VAR(r);
  1752. AddPath(paths, dest, stackSize);
  1753. // Add both paths to the code paths
  1754. AddPath(paths, instr->next, stackSize);
  1755. break;
  1756. }
  1757. else if( instr->op == asBC_JMPP )
  1758. {
  1759. // I need to know the largest value possible
  1760. asDWORD max = *ARG_DW(instr->arg);
  1761. // Add all destinations to the code paths
  1762. asCByteInstruction *dest = instr->next;
  1763. for( asDWORD n = 0; n <= max && dest != 0; ++n )
  1764. {
  1765. AddPath(paths, dest, stackSize);
  1766. dest = dest->next;
  1767. }
  1768. break;
  1769. }
  1770. else
  1771. {
  1772. instr = instr->next;
  1773. if( instr == 0 || instr->marked )
  1774. break;
  1775. }
  1776. }
  1777. }
  1778. // Are there any instructions that didn't get visited?
  1779. instr = first;
  1780. while( instr )
  1781. {
  1782. // Don't remove asBC_Block instructions as then the start and end of blocks may become mismatched
  1783. if( instr->marked == false && instr->op != asBC_Block )
  1784. {
  1785. // Remove it
  1786. asCByteInstruction *curr = instr;
  1787. instr = instr->next;
  1788. DeleteInstruction(curr);
  1789. }
  1790. else
  1791. instr = instr->next;
  1792. }
  1793. }
  1794. #ifdef AS_DEBUG
  1795. void asCByteCode::DebugOutput(const char *name, asCScriptEngine *engine, asCScriptFunction *func)
  1796. {
  1797. _mkdir("AS_DEBUG");
  1798. asCString str = "AS_DEBUG/";
  1799. str += name;
  1800. #if _MSC_VER >= 1500 && !defined(AS_MARMALADE)
  1801. FILE *file;
  1802. fopen_s(&file, str.AddressOf(), "w");
  1803. #else
  1804. FILE *file = fopen(str.AddressOf(), "w");
  1805. #endif
  1806. #if !defined(AS_XENON) // XBox 360: When running in DVD Emu, no write is allowed
  1807. asASSERT( file );
  1808. #endif
  1809. if( file == 0 )
  1810. return;
  1811. asUINT n;
  1812. fprintf(file, "%s\n\n", func->GetDeclaration());
  1813. fprintf(file, "Temps: ");
  1814. for( n = 0; n < temporaryVariables->GetLength(); n++ )
  1815. {
  1816. fprintf(file, "%d", (*temporaryVariables)[n]);
  1817. if( n < temporaryVariables->GetLength()-1 )
  1818. fprintf(file, ", ");
  1819. }
  1820. fprintf(file, "\n\n");
  1821. fprintf(file, "Variables: \n");
  1822. for( n = 0; n < func->scriptData->variables.GetLength(); n++ )
  1823. {
  1824. fprintf(file, " %.3d: %s %s\n", func->scriptData->variables[n]->stackOffset, func->scriptData->variables[n]->type.Format().AddressOf(), func->scriptData->variables[n]->name.AddressOf());
  1825. }
  1826. asUINT offset = 0;
  1827. if( func->objectType )
  1828. {
  1829. fprintf(file, " %.3d: %s this\n", 0, func->objectType->name.AddressOf());
  1830. offset -= AS_PTR_SIZE;
  1831. }
  1832. for( n = 0; n < func->parameterTypes.GetLength(); n++ )
  1833. {
  1834. bool found = false;
  1835. for( asUINT v = 0; v < func->scriptData->variables.GetLength(); v++ )
  1836. {
  1837. if( func->scriptData->variables[v]->stackOffset == (int)offset )
  1838. {
  1839. found = true;
  1840. break;
  1841. }
  1842. }
  1843. if( !found )
  1844. fprintf(file, " %.3d: %s {noname param}\n", offset, func->parameterTypes[n].Format().AddressOf());
  1845. offset -= func->parameterTypes[n].GetSizeOnStackDWords();
  1846. }
  1847. for( n = 0; n < func->scriptData->objVariablePos.GetLength(); n++ )
  1848. {
  1849. bool found = false;
  1850. for( asUINT v = 0; v < func->scriptData->variables.GetLength(); v++ )
  1851. {
  1852. if( func->scriptData->variables[v]->stackOffset == func->scriptData->objVariablePos[n] )
  1853. {
  1854. found = true;
  1855. break;
  1856. }
  1857. }
  1858. if( !found )
  1859. fprintf(file, " %.3d: %s {noname}\n", func->scriptData->objVariablePos[n], func->scriptData->objVariableTypes[n]->name.AddressOf());
  1860. }
  1861. fprintf(file, "\n\n");
  1862. int pos = 0;
  1863. asUINT lineIndex = 0;
  1864. asCByteInstruction *instr = first;
  1865. while( instr )
  1866. {
  1867. if( lineIndex < lineNumbers.GetLength() && lineNumbers[lineIndex] == pos )
  1868. {
  1869. asDWORD line = lineNumbers[lineIndex+1];
  1870. fprintf(file, "- %d,%d -\n", (int)(line&0xFFFFF), (int)(line>>20));
  1871. lineIndex += 2;
  1872. }
  1873. fprintf(file, "%5d ", pos);
  1874. pos += instr->GetSize();
  1875. fprintf(file, "%3d %c ", int(instr->stackSize + func->scriptData->variableSpace), instr->marked ? '*' : ' ');
  1876. switch( asBCInfo[instr->op].type )
  1877. {
  1878. case asBCTYPE_W_ARG:
  1879. if( instr->op == asBC_STR )
  1880. {
  1881. int id = asWORD(instr->wArg[0]);
  1882. const asCString &str = engine->GetConstantString(id);
  1883. fprintf(file, " %-8s %d (l:%ld s:\"%.10s\")\n", asBCInfo[instr->op].name, asWORD(instr->wArg[0]), (long int)str.GetLength(), str.AddressOf());
  1884. }
  1885. else
  1886. fprintf(file, " %-8s %d\n", asBCInfo[instr->op].name, instr->wArg[0]);
  1887. break;
  1888. case asBCTYPE_wW_ARG:
  1889. case asBCTYPE_rW_ARG:
  1890. fprintf(file, " %-8s v%d\n", asBCInfo[instr->op].name, instr->wArg[0]);
  1891. break;
  1892. case asBCTYPE_wW_rW_ARG:
  1893. case asBCTYPE_rW_rW_ARG:
  1894. fprintf(file, " %-8s v%d, v%d\n", asBCInfo[instr->op].name, instr->wArg[0], instr->wArg[1]);
  1895. break;
  1896. case asBCTYPE_wW_W_ARG:
  1897. fprintf(file, " %-8s v%d, %d\n", asBCInfo[instr->op].name, instr->wArg[0], instr->wArg[1]);
  1898. break;
  1899. case asBCTYPE_wW_rW_DW_ARG:
  1900. case asBCTYPE_rW_W_DW_ARG:
  1901. switch( instr->op )
  1902. {
  1903. case asBC_ADDIf:
  1904. case asBC_SUBIf:
  1905. case asBC_MULIf:
  1906. fprintf(file, " %-8s v%d, v%d, %f\n", asBCInfo[instr->op].name, instr->wArg[0], instr->wArg[1], *((float*) ARG_DW(instr->arg)));
  1907. break;
  1908. default:
  1909. fprintf(file, " %-8s v%d, v%d, %d\n", asBCInfo[instr->op].name, instr->wArg[0], instr->wArg[1], *((int*) ARG_DW(instr->arg)));
  1910. break;
  1911. }
  1912. break;
  1913. case asBCTYPE_DW_ARG:
  1914. switch( instr->op )
  1915. {
  1916. case asBC_OBJTYPE:
  1917. fprintf(file, " %-8s 0x%x\n", asBCInfo[instr->op].name, (asUINT)*ARG_DW(instr->arg));
  1918. break;
  1919. case asBC_PshC4:
  1920. case asBC_Cast:
  1921. 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)));
  1922. break;
  1923. case asBC_TYPEID:
  1924. fprintf(file, " %-8s 0x%x '%s'\n", asBCInfo[instr->op].name, (asUINT)*ARG_DW(instr->arg), engine->GetTypeDeclaration((int)*ARG_DW(instr->arg)));
  1925. break;
  1926. case asBC_CALL:
  1927. case asBC_CALLSYS:
  1928. case asBC_CALLBND:
  1929. case asBC_CALLINTF:
  1930. {
  1931. int funcID = *(int*)ARG_DW(instr->arg);
  1932. asCString decl = engine->GetFunctionDeclaration(funcID);
  1933. fprintf(file, " %-8s %d (%s)\n", asBCInfo[instr->op].name, *((int*) ARG_DW(instr->arg)), decl.AddressOf());
  1934. }
  1935. break;
  1936. case asBC_REFCPY:
  1937. fprintf(file, " %-8s 0x%x\n", asBCInfo[instr->op].name, *((int*) ARG_DW(instr->arg)));
  1938. break;
  1939. case asBC_JMP:
  1940. case asBC_JZ:
  1941. case asBC_JLowZ:
  1942. case asBC_JS:
  1943. case asBC_JP:
  1944. case asBC_JNZ:
  1945. case asBC_JLowNZ:
  1946. case asBC_JNS:
  1947. case asBC_JNP:
  1948. fprintf(file, " %-8s %+d (d:%d)\n", asBCInfo[instr->op].name, *((int*) ARG_DW(instr->arg)), pos+*((int*) ARG_DW(instr->arg)));
  1949. break;
  1950. default:
  1951. fprintf(file, " %-8s %d\n", asBCInfo[instr->op].name, *((int*) ARG_DW(instr->arg)));
  1952. break;
  1953. }
  1954. break;
  1955. case asBCTYPE_QW_ARG:
  1956. #ifdef __GNUC__
  1957. #ifdef _LP64
  1958. 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)));
  1959. #else
  1960. 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)));
  1961. #endif
  1962. #else
  1963. 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)));
  1964. #endif
  1965. break;
  1966. case asBCTYPE_wW_QW_ARG:
  1967. case asBCTYPE_rW_QW_ARG:
  1968. #ifdef __GNUC__
  1969. #ifdef _LP64
  1970. 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)));
  1971. #else
  1972. 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)));
  1973. #endif
  1974. #else
  1975. 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)));
  1976. #endif
  1977. break;
  1978. case asBCTYPE_DW_DW_ARG:
  1979. if( instr->op == asBC_ALLOC )
  1980. {
  1981. asCObjectType *ot = *(asCObjectType**)ARG_DW(instr->arg);
  1982. fprintf(file, " %-8s 0x%x, %d (type:%s)\n", asBCInfo[instr->op].name, *(int*)ARG_DW(instr->arg), *(int*)(ARG_DW(instr->arg)+1), ot->GetName());
  1983. }
  1984. else
  1985. fprintf(file, " %-8s %u, %d\n", asBCInfo[instr->op].name, *(int*)ARG_DW(instr->arg), *(int*)(ARG_DW(instr->arg)+1));
  1986. break;
  1987. case asBCTYPE_rW_DW_DW_ARG:
  1988. 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));
  1989. break;
  1990. case asBCTYPE_QW_DW_ARG:
  1991. if( instr->op == asBC_ALLOC )
  1992. {
  1993. asCObjectType *ot = *(asCObjectType**)ARG_QW(instr->arg);
  1994. #ifdef __GNUC__
  1995. #ifdef AS_64BIT_PTR
  1996. fprintf(file, " %-8s 0x%lx, %d (type:%s)\n", asBCInfo[instr->op].name, *(asINT64*)ARG_QW(instr->arg), *(int*)(ARG_DW(instr->arg)+2), ot->GetName());
  1997. #else
  1998. fprintf(file, " %-8s 0x%llx, %d (type:%s)\n", asBCInfo[instr->op].name, *(asINT64*)ARG_QW(instr->arg), *(int*)(ARG_DW(instr->arg)+2), ot->GetName());
  1999. #endif
  2000. #else
  2001. fprintf(file, " %-8s 0x%I64x, %d (type:%s)\n", asBCInfo[instr->op].name, *(asINT64*)ARG_QW(instr->arg), *(int*)(ARG_DW(instr->arg)+2), ot->GetName());
  2002. #endif
  2003. }
  2004. else
  2005. #ifdef __GNUC__
  2006. #ifdef AS_64BIT_PTR
  2007. fprintf(file, " %-8s %lu, %d\n", asBCInfo[instr->op].name, *(asINT64*)ARG_QW(instr->arg), *(int*)(ARG_DW(instr->arg)+2));
  2008. #else
  2009. fprintf(file, " %-8s %llu, %d\n", asBCInfo[instr->op].name, *(asINT64*)ARG_QW(instr->arg), *(int*)(ARG_DW(instr->arg)+2));
  2010. #endif
  2011. #else
  2012. fprintf(file, " %-8s %I64u, %d\n", asBCInfo[instr->op].name, *(asINT64*)ARG_QW(instr->arg), *(int*)(ARG_DW(instr->arg)+2));
  2013. #endif
  2014. break;
  2015. case asBCTYPE_INFO:
  2016. if( instr->op == asBC_LABEL )
  2017. fprintf(file, "%d:\n", instr->wArg[0]);
  2018. else if( instr->op == asBC_LINE )
  2019. fprintf(file, " %s\n", asBCInfo[instr->op].name);
  2020. else if( instr->op == asBC_Block )
  2021. fprintf(file, "%c\n", instr->wArg[0] ? '{' : '}');
  2022. break;
  2023. case asBCTYPE_rW_DW_ARG:
  2024. case asBCTYPE_wW_DW_ARG:
  2025. case asBCTYPE_W_DW_ARG:
  2026. if( instr->op == asBC_SetV1 )
  2027. fprintf(file, " %-8s v%d, 0x%x\n", asBCInfo[instr->op].name, instr->wArg[0], *(asBYTE*)ARG_DW(instr->arg));
  2028. else if( instr->op == asBC_SetV2 )
  2029. fprintf(file, " %-8s v%d, 0x%x\n", asBCInfo[instr->op].name, instr->wArg[0], *(asWORD*)ARG_DW(instr->arg));
  2030. else if( instr->op == asBC_SetV4 )
  2031. 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)));
  2032. else if( instr->op == asBC_CMPIf )
  2033. fprintf(file, " %-8s v%d, %f\n", asBCInfo[instr->op].name, instr->wArg[0], *(float*)ARG_DW(instr->arg));
  2034. else
  2035. fprintf(file, " %-8s v%d, %d\n", asBCInfo[instr->op].name, instr->wArg[0], (asUINT)*ARG_DW(instr->arg));
  2036. break;
  2037. case asBCTYPE_wW_rW_rW_ARG:
  2038. fprintf(file, " %-8s v%d, v%d, v%d\n", asBCInfo[instr->op].name, instr->wArg[0], instr->wArg[1], instr->wArg[2]);
  2039. break;
  2040. case asBCTYPE_NO_ARG:
  2041. fprintf(file, " %s\n", asBCInfo[instr->op].name);
  2042. break;
  2043. default:
  2044. asASSERT(false);
  2045. }
  2046. instr = instr->next;
  2047. }
  2048. fclose(file);
  2049. }
  2050. #endif
  2051. //=============================================================================
  2052. int asCByteCode::InsertFirstInstrDWORD(asEBCInstr bc, asDWORD param)
  2053. {
  2054. asASSERT(asBCInfo[bc].type == asBCTYPE_DW_ARG);
  2055. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2056. if( AddInstructionFirst() < 0 )
  2057. return 0;
  2058. first->op = bc;
  2059. *ARG_DW(first->arg) = param;
  2060. first->size = asBCTypeSize[asBCInfo[bc].type];
  2061. first->stackInc = asBCInfo[bc].stackInc;
  2062. return first->stackInc;
  2063. }
  2064. int asCByteCode::InsertFirstInstrQWORD(asEBCInstr bc, asQWORD param)
  2065. {
  2066. asASSERT(asBCInfo[bc].type == asBCTYPE_QW_ARG);
  2067. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2068. if( AddInstructionFirst() < 0 )
  2069. return 0;
  2070. first->op = bc;
  2071. *ARG_QW(first->arg) = param;
  2072. first->size = asBCTypeSize[asBCInfo[bc].type];
  2073. first->stackInc = asBCInfo[bc].stackInc;
  2074. return first->stackInc;
  2075. }
  2076. int asCByteCode::Instr(asEBCInstr bc)
  2077. {
  2078. asASSERT(asBCInfo[bc].type == asBCTYPE_NO_ARG);
  2079. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2080. if( AddInstruction() < 0 )
  2081. return 0;
  2082. last->op = bc;
  2083. last->size = asBCTypeSize[asBCInfo[bc].type];
  2084. last->stackInc = asBCInfo[bc].stackInc;
  2085. return last->stackInc;
  2086. }
  2087. int asCByteCode::InstrW_W_W(asEBCInstr bc, int a, int b, int c)
  2088. {
  2089. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_rW_rW_ARG);
  2090. asASSERT(asBCInfo[bc].stackInc == 0);
  2091. if( AddInstruction() < 0 )
  2092. return 0;
  2093. last->op = bc;
  2094. last->wArg[0] = (short)a;
  2095. last->wArg[1] = (short)b;
  2096. last->wArg[2] = (short)c;
  2097. last->size = asBCTypeSize[asBCInfo[bc].type];
  2098. last->stackInc = asBCInfo[bc].stackInc;
  2099. return last->stackInc;
  2100. }
  2101. int asCByteCode::InstrW_W(asEBCInstr bc, int a, int b)
  2102. {
  2103. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_rW_ARG ||
  2104. asBCInfo[bc].type == asBCTYPE_rW_rW_ARG);
  2105. asASSERT(asBCInfo[bc].stackInc == 0);
  2106. if( AddInstruction() < 0 )
  2107. return 0;
  2108. last->op = bc;
  2109. last->wArg[0] = (short)a;
  2110. last->wArg[1] = (short)b;
  2111. last->size = asBCTypeSize[asBCInfo[bc].type];
  2112. last->stackInc = asBCInfo[bc].stackInc;
  2113. return last->stackInc;
  2114. }
  2115. int asCByteCode::InstrW_PTR(asEBCInstr bc, short a, void *param)
  2116. {
  2117. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_PTR_ARG);
  2118. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2119. if( AddInstruction() < 0 )
  2120. return 0;
  2121. last->op = bc;
  2122. last->wArg[0] = a;
  2123. *ARG_PTR(last->arg) = (asPWORD)param;
  2124. last->size = asBCTypeSize[asBCInfo[bc].type];
  2125. last->stackInc = asBCInfo[bc].stackInc;
  2126. return last->stackInc;
  2127. }
  2128. int asCByteCode::InstrW_DW(asEBCInstr bc, asWORD a, asDWORD b)
  2129. {
  2130. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_DW_ARG ||
  2131. asBCInfo[bc].type == asBCTYPE_rW_DW_ARG ||
  2132. asBCInfo[bc].type == asBCTYPE_W_DW_ARG);
  2133. asASSERT(asBCInfo[bc].stackInc == 0);
  2134. if( AddInstruction() < 0 )
  2135. return 0;
  2136. last->op = bc;
  2137. last->wArg[0] = a;
  2138. *((int*) ARG_DW(last->arg)) = b;
  2139. last->size = asBCTypeSize[asBCInfo[bc].type];
  2140. last->stackInc = asBCInfo[bc].stackInc;
  2141. return last->stackInc;
  2142. }
  2143. int asCByteCode::InstrSHORT_DW_DW(asEBCInstr bc, short a, asDWORD b, asDWORD c)
  2144. {
  2145. asASSERT(asBCInfo[bc].type == asBCTYPE_rW_DW_DW_ARG);
  2146. asASSERT(asBCInfo[bc].stackInc == 0);
  2147. if( AddInstruction() < 0 )
  2148. return 0;
  2149. last->op = bc;
  2150. last->wArg[0] = a;
  2151. *(int*)ARG_DW(last->arg) = b;
  2152. *(int*)(ARG_DW(last->arg)+1) = c;
  2153. last->size = asBCTypeSize[asBCInfo[bc].type];
  2154. last->stackInc = asBCInfo[bc].stackInc;
  2155. return last->stackInc;
  2156. }
  2157. int asCByteCode::InstrSHORT_B(asEBCInstr bc, short a, asBYTE b)
  2158. {
  2159. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_DW_ARG ||
  2160. asBCInfo[bc].type == asBCTYPE_rW_DW_ARG ||
  2161. asBCInfo[bc].type == asBCTYPE_W_DW_ARG);
  2162. asASSERT(asBCInfo[bc].stackInc == 0);
  2163. if( AddInstruction() < 0 )
  2164. return 0;
  2165. last->op = bc;
  2166. last->wArg[0] = a;
  2167. // We'll have to be careful to store the byte correctly, independent of endianess.
  2168. // Some optimizing compilers may change the order of operations, so we make sure
  2169. // the value is not overwritten even if that happens.
  2170. asBYTE *argPtr = (asBYTE*)ARG_DW(last->arg);
  2171. argPtr[0] = b; // The value is always stored in the lower byte
  2172. argPtr[1] = 0; // and clear the rest of the DWORD
  2173. argPtr[2] = 0;
  2174. argPtr[3] = 0;
  2175. last->size = asBCTypeSize[asBCInfo[bc].type];
  2176. last->stackInc = asBCInfo[bc].stackInc;
  2177. return last->stackInc;
  2178. }
  2179. int asCByteCode::InstrSHORT_W(asEBCInstr bc, short a, asWORD b)
  2180. {
  2181. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_DW_ARG ||
  2182. asBCInfo[bc].type == asBCTYPE_rW_DW_ARG ||
  2183. asBCInfo[bc].type == asBCTYPE_W_DW_ARG);
  2184. asASSERT(asBCInfo[bc].stackInc == 0);
  2185. if( AddInstruction() < 0 )
  2186. return 0;
  2187. last->op = bc;
  2188. last->wArg[0] = a;
  2189. // We'll have to be careful to store the word correctly, independent of endianess.
  2190. // Some optimizing compilers may change the order of operations, so we make sure
  2191. // the value is not overwritten even if that happens.
  2192. asWORD *argPtr = (asWORD*)ARG_DW(last->arg);
  2193. argPtr[0] = b; // The value is always stored in the lower word
  2194. argPtr[1] = 0; // and clear the rest of the DWORD
  2195. last->size = asBCTypeSize[asBCInfo[bc].type];
  2196. last->stackInc = asBCInfo[bc].stackInc;
  2197. return last->stackInc;
  2198. }
  2199. int asCByteCode::InstrSHORT_DW(asEBCInstr bc, short a, asDWORD b)
  2200. {
  2201. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_DW_ARG ||
  2202. asBCInfo[bc].type == asBCTYPE_rW_DW_ARG ||
  2203. asBCInfo[bc].type == asBCTYPE_W_DW_ARG);
  2204. if( AddInstruction() < 0 )
  2205. return 0;
  2206. last->op = bc;
  2207. last->wArg[0] = a;
  2208. *((int*) ARG_DW(last->arg)) = b;
  2209. last->size = asBCTypeSize[asBCInfo[bc].type];
  2210. last->stackInc = asBCInfo[bc].stackInc;
  2211. return last->stackInc;
  2212. }
  2213. int asCByteCode::InstrW_QW(asEBCInstr bc, asWORD a, asQWORD b)
  2214. {
  2215. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_QW_ARG);
  2216. asASSERT(asBCInfo[bc].stackInc == 0);
  2217. if( AddInstruction() < 0 )
  2218. return 0;
  2219. last->op = bc;
  2220. last->wArg[0] = a;
  2221. *ARG_QW(last->arg) = b;
  2222. last->size = asBCTypeSize[asBCInfo[bc].type];
  2223. last->stackInc = asBCInfo[bc].stackInc;
  2224. return last->stackInc;
  2225. }
  2226. int asCByteCode::InstrSHORT_QW(asEBCInstr bc, short a, asQWORD b)
  2227. {
  2228. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_QW_ARG);
  2229. asASSERT(asBCInfo[bc].stackInc == 0);
  2230. if( AddInstruction() < 0 )
  2231. return 0;
  2232. last->op = bc;
  2233. last->wArg[0] = a;
  2234. *ARG_QW(last->arg) = b;
  2235. last->size = asBCTypeSize[asBCInfo[bc].type];
  2236. last->stackInc = asBCInfo[bc].stackInc;
  2237. return last->stackInc;
  2238. }
  2239. int asCByteCode::InstrW_FLOAT(asEBCInstr bc, asWORD a, float b)
  2240. {
  2241. asASSERT(asBCInfo[bc].type == asBCTYPE_wW_DW_ARG);
  2242. asASSERT(asBCInfo[bc].stackInc == 0);
  2243. if( AddInstruction() < 0 )
  2244. return 0;
  2245. last->op = bc;
  2246. last->wArg[0] = a;
  2247. *((float*) ARG_DW(last->arg)) = b;
  2248. last->size = asBCTypeSize[asBCInfo[bc].type];
  2249. last->stackInc = asBCInfo[bc].stackInc;
  2250. return last->stackInc;
  2251. }
  2252. int asCByteCode::InstrSHORT(asEBCInstr bc, short param)
  2253. {
  2254. asASSERT(asBCInfo[bc].type == asBCTYPE_rW_ARG ||
  2255. asBCInfo[bc].type == asBCTYPE_wW_ARG ||
  2256. asBCInfo[bc].type == asBCTYPE_W_ARG);
  2257. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2258. if( AddInstruction() < 0 )
  2259. return 0;
  2260. last->op = bc;
  2261. last->wArg[0] = param;
  2262. last->size = asBCTypeSize[asBCInfo[bc].type];
  2263. last->stackInc = asBCInfo[bc].stackInc;
  2264. return last->stackInc;
  2265. }
  2266. int asCByteCode::InstrINT(asEBCInstr bc, int param)
  2267. {
  2268. asASSERT(asBCInfo[bc].type == asBCTYPE_DW_ARG);
  2269. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2270. if( AddInstruction() < 0 )
  2271. return 0;
  2272. last->op = bc;
  2273. *((int*) ARG_DW(last->arg)) = param;
  2274. last->size = asBCTypeSize[asBCInfo[bc].type];
  2275. last->stackInc = asBCInfo[bc].stackInc;
  2276. return last->stackInc;
  2277. }
  2278. int asCByteCode::InstrDWORD(asEBCInstr bc, asDWORD param)
  2279. {
  2280. asASSERT(asBCInfo[bc].type == asBCTYPE_DW_ARG);
  2281. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2282. if( AddInstruction() < 0 )
  2283. return 0;
  2284. last->op = bc;
  2285. *ARG_DW(last->arg) = param;
  2286. last->size = asBCTypeSize[asBCInfo[bc].type];
  2287. last->stackInc = asBCInfo[bc].stackInc;
  2288. return last->stackInc;
  2289. }
  2290. int asCByteCode::InstrPTR(asEBCInstr bc, void *param)
  2291. {
  2292. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2293. if( AddInstruction() < 0 )
  2294. return 0;
  2295. last->op = bc;
  2296. asASSERT(asBCInfo[bc].type == asBCTYPE_PTR_ARG);
  2297. *ARG_PTR(last->arg) = (asPWORD)param;
  2298. last->size = asBCTypeSize[asBCInfo[bc].type];
  2299. last->stackInc = asBCInfo[bc].stackInc;
  2300. return last->stackInc;
  2301. }
  2302. int asCByteCode::InstrQWORD(asEBCInstr bc, asQWORD param)
  2303. {
  2304. asASSERT(asBCInfo[bc].type == asBCTYPE_QW_ARG);
  2305. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2306. if( AddInstruction() < 0 )
  2307. return 0;
  2308. last->op = bc;
  2309. *ARG_QW(last->arg) = param;
  2310. last->size = asBCTypeSize[asBCInfo[bc].type];
  2311. last->stackInc = asBCInfo[bc].stackInc;
  2312. return last->stackInc;
  2313. }
  2314. int asCByteCode::InstrWORD(asEBCInstr bc, asWORD param)
  2315. {
  2316. asASSERT(asBCInfo[bc].type == asBCTYPE_W_ARG ||
  2317. asBCInfo[bc].type == asBCTYPE_rW_ARG ||
  2318. asBCInfo[bc].type == asBCTYPE_wW_ARG);
  2319. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2320. if( AddInstruction() < 0 )
  2321. return 0;
  2322. last->op = bc;
  2323. last->wArg[0] = param;
  2324. last->size = asBCTypeSize[asBCInfo[bc].type];
  2325. last->stackInc = asBCInfo[bc].stackInc;
  2326. return last->stackInc;
  2327. }
  2328. int asCByteCode::InstrFLOAT(asEBCInstr bc, float param)
  2329. {
  2330. asASSERT(asBCInfo[bc].type == asBCTYPE_DW_ARG);
  2331. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2332. if( AddInstruction() < 0 )
  2333. return 0;
  2334. last->op = bc;
  2335. *((float*) ARG_DW(last->arg)) = param;
  2336. last->size = asBCTypeSize[asBCInfo[bc].type];
  2337. last->stackInc = asBCInfo[bc].stackInc;
  2338. return last->stackInc;
  2339. }
  2340. int asCByteCode::InstrDOUBLE(asEBCInstr bc, double param)
  2341. {
  2342. asASSERT(asBCInfo[bc].type == asBCTYPE_QW_ARG);
  2343. asASSERT(asBCInfo[bc].stackInc != 0xFFFF);
  2344. if( AddInstruction() < 0 )
  2345. return 0;
  2346. last->op = bc;
  2347. *((double*) ARG_QW(last->arg)) = param;
  2348. last->size = asBCTypeSize[asBCInfo[bc].type];
  2349. last->stackInc = asBCInfo[bc].stackInc;
  2350. return last->stackInc;
  2351. }
  2352. int asCByteCode::GetLastInstr()
  2353. {
  2354. if( last == 0 ) return -1;
  2355. return last->op;
  2356. }
  2357. int asCByteCode::RemoveLastInstr()
  2358. {
  2359. if( last == 0 ) return -1;
  2360. if( first == last )
  2361. {
  2362. engine->memoryMgr.FreeByteInstruction(last);
  2363. first = 0;
  2364. last = 0;
  2365. }
  2366. else
  2367. {
  2368. asCByteInstruction *bc = last;
  2369. last = bc->prev;
  2370. bc->Remove();
  2371. engine->memoryMgr.FreeByteInstruction(bc);
  2372. }
  2373. return 0;
  2374. }
  2375. asDWORD asCByteCode::GetLastInstrValueDW()
  2376. {
  2377. if( last == 0 ) return 0;
  2378. return *ARG_DW(last->arg);
  2379. }
  2380. //===================================================================
  2381. asCByteInstruction::asCByteInstruction()
  2382. {
  2383. next = 0;
  2384. prev = 0;
  2385. op = asBC_LABEL;
  2386. arg = 0;
  2387. wArg[0] = 0;
  2388. wArg[1] = 0;
  2389. wArg[2] = 0;
  2390. size = 0;
  2391. stackInc = 0;
  2392. marked = false;
  2393. stackSize = 0;
  2394. }
  2395. void asCByteInstruction::AddAfter(asCByteInstruction *nextCode)
  2396. {
  2397. if( next )
  2398. next->prev = nextCode;
  2399. nextCode->next = next;
  2400. nextCode->prev = this;
  2401. next = nextCode;
  2402. }
  2403. void asCByteInstruction::AddBefore(asCByteInstruction *prevCode)
  2404. {
  2405. if( prev )
  2406. prev->next = prevCode;
  2407. prevCode->prev = prev;
  2408. prevCode->next = this;
  2409. prev = prevCode;
  2410. }
  2411. int asCByteInstruction::GetSize()
  2412. {
  2413. return size;
  2414. }
  2415. int asCByteInstruction::GetStackIncrease()
  2416. {
  2417. return stackInc;
  2418. }
  2419. void asCByteInstruction::Remove()
  2420. {
  2421. if( prev ) prev->next = next;
  2422. if( next ) next->prev = prev;
  2423. prev = 0;
  2424. next = 0;
  2425. }
  2426. END_AS_NAMESPACE
  2427. #endif // AS_NO_COMPILER