as_restore.cpp 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126
  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_restore.cpp
  25. //
  26. // Functions for saving and restoring module bytecode
  27. // asCRestore was originally written by Dennis Bollyn, [email protected]
  28. #include "as_config.h"
  29. #include "as_restore.h"
  30. #include "as_bytecode.h"
  31. #include "as_scriptobject.h"
  32. #include "as_texts.h"
  33. BEGIN_AS_NAMESPACE
  34. asCReader::asCReader(asCModule* _module, asIBinaryStream* _stream, asCScriptEngine* _engine)
  35. : module(_module), stream(_stream), engine(_engine)
  36. {
  37. error = false;
  38. }
  39. void asCReader::ReadData(void *data, asUINT size)
  40. {
  41. asASSERT(size == 1 || size == 2 || size == 4 || size == 8);
  42. #if defined(AS_BIG_ENDIAN)
  43. for( asUINT n = 0; n < size; n++ )
  44. stream->Read(((asBYTE*)data)+n, 1);
  45. #else
  46. for( int n = size-1; n >= 0; n-- )
  47. stream->Read(((asBYTE*)data)+n, 1);
  48. #endif
  49. }
  50. int asCReader::Read(bool *wasDebugInfoStripped)
  51. {
  52. // Before starting the load, make sure that
  53. // any existing resources have been freed
  54. module->InternalReset();
  55. // Call the inner method to do the actual loading
  56. int r = ReadInner();
  57. if( r < 0 )
  58. {
  59. // Something went wrong while loading the bytecode, so we need
  60. // to clean-up whatever has been created during the process.
  61. // Make sure none of the loaded functions attempt to release
  62. // references that have not yet been increased
  63. asUINT i;
  64. for( i = 0; i < module->scriptFunctions.GetLength(); i++ )
  65. if( !dontTranslate.MoveTo(0, module->scriptFunctions[i]) )
  66. module->scriptFunctions[i]->byteCode.SetLength(0);
  67. asCSymbolTable<asCGlobalProperty>::iterator it = module->scriptGlobals.List();
  68. for( ; it; it++ )
  69. if( (*it)->GetInitFunc() )
  70. (*it)->GetInitFunc()->byteCode.SetLength(0);
  71. module->InternalReset();
  72. }
  73. else
  74. {
  75. // Init system functions properly
  76. engine->PrepareEngine();
  77. // Initialize the global variables (unless requested not to)
  78. if( engine->ep.initGlobalVarsAfterBuild )
  79. r = module->ResetGlobalVars(0);
  80. if( wasDebugInfoStripped )
  81. *wasDebugInfoStripped = noDebugInfo;
  82. }
  83. return r;
  84. }
  85. int asCReader::ReadInner()
  86. {
  87. // This function will load each entity one by one from the stream.
  88. // If any error occurs, it will return to the caller who is
  89. // responsible for cleaning up the partially loaded entities.
  90. engine->deferValidationOfTemplateTypes = true;
  91. unsigned long i, count;
  92. asCScriptFunction* func;
  93. ReadData(&noDebugInfo, 1);
  94. // Read enums
  95. count = ReadEncodedUInt();
  96. module->enumTypes.Allocate(count, 0);
  97. for( i = 0; i < count && !error; i++ )
  98. {
  99. asCObjectType *ot = asNEW(asCObjectType)(engine);
  100. if( ot == 0 )
  101. return asOUT_OF_MEMORY;
  102. ReadObjectTypeDeclaration(ot, 1);
  103. // If the type is shared then we should use the original if it exists
  104. bool sharedExists = false;
  105. if( ot->IsShared() )
  106. {
  107. for( asUINT n = 0; n < engine->classTypes.GetLength(); n++ )
  108. {
  109. asCObjectType *t = engine->classTypes[n];
  110. if( t &&
  111. t->IsShared() &&
  112. t->name == ot->name &&
  113. t->nameSpace == ot->nameSpace &&
  114. (t->flags & asOBJ_ENUM) )
  115. {
  116. asDELETE(ot, asCObjectType);
  117. ot = t;
  118. sharedExists = true;
  119. break;
  120. }
  121. }
  122. }
  123. if( sharedExists )
  124. existingShared.Insert(ot, true);
  125. else
  126. engine->classTypes.PushLast(ot);
  127. module->enumTypes.PushLast(ot);
  128. ot->AddRef();
  129. ReadObjectTypeDeclaration(ot, 2);
  130. }
  131. if( error ) return asERROR;
  132. // classTypes[]
  133. // First restore the structure names, then the properties
  134. count = ReadEncodedUInt();
  135. module->classTypes.Allocate(count, 0);
  136. for( i = 0; i < count && !error; ++i )
  137. {
  138. asCObjectType *ot = asNEW(asCObjectType)(engine);
  139. if( ot == 0 )
  140. return asOUT_OF_MEMORY;
  141. ReadObjectTypeDeclaration(ot, 1);
  142. // If the type is shared, then we should use the original if it exists
  143. bool sharedExists = false;
  144. if( ot->IsShared() )
  145. {
  146. for( asUINT n = 0; n < engine->classTypes.GetLength(); n++ )
  147. {
  148. asCObjectType *t = engine->classTypes[n];
  149. if( t &&
  150. t->IsShared() &&
  151. t->name == ot->name &&
  152. t->nameSpace == ot->nameSpace &&
  153. t->IsInterface() == ot->IsInterface() )
  154. {
  155. asDELETE(ot, asCObjectType);
  156. ot = t;
  157. sharedExists = true;
  158. break;
  159. }
  160. }
  161. }
  162. if( sharedExists )
  163. existingShared.Insert(ot, true);
  164. else
  165. {
  166. engine->classTypes.PushLast(ot);
  167. // Set this module as the owner
  168. ot->module = module;
  169. }
  170. module->classTypes.PushLast(ot);
  171. ot->AddRef();
  172. }
  173. if( error ) return asERROR;
  174. // Read func defs
  175. count = ReadEncodedUInt();
  176. module->funcDefs.Allocate(count, 0);
  177. for( i = 0; i < count && !error; i++ )
  178. {
  179. bool isNew;
  180. asCScriptFunction *func = ReadFunction(isNew, false, true);
  181. if( func )
  182. module->funcDefs.PushLast(func);
  183. else
  184. error = true;
  185. }
  186. // Read interface methods
  187. for( i = 0; i < module->classTypes.GetLength() && !error; i++ )
  188. {
  189. if( module->classTypes[i]->IsInterface() )
  190. ReadObjectTypeDeclaration(module->classTypes[i], 2);
  191. }
  192. // Read class methods and behaviours
  193. for( i = 0; i < module->classTypes.GetLength() && !error; ++i )
  194. {
  195. if( !module->classTypes[i]->IsInterface() )
  196. ReadObjectTypeDeclaration(module->classTypes[i], 2);
  197. }
  198. // Read class properties
  199. for( i = 0; i < module->classTypes.GetLength() && !error; ++i )
  200. {
  201. if( !module->classTypes[i]->IsInterface() )
  202. ReadObjectTypeDeclaration(module->classTypes[i], 3);
  203. }
  204. if( error ) return asERROR;
  205. // Read typedefs
  206. count = ReadEncodedUInt();
  207. module->typeDefs.Allocate(count, 0);
  208. for( i = 0; i < count && !error; i++ )
  209. {
  210. asCObjectType *ot = asNEW(asCObjectType)(engine);
  211. if( ot == 0 )
  212. return asOUT_OF_MEMORY;
  213. ReadObjectTypeDeclaration(ot, 1);
  214. engine->classTypes.PushLast(ot);
  215. module->typeDefs.PushLast(ot);
  216. ot->AddRef();
  217. ReadObjectTypeDeclaration(ot, 2);
  218. }
  219. if( error ) return asERROR;
  220. // scriptGlobals[]
  221. count = ReadEncodedUInt();
  222. if( count && engine->ep.disallowGlobalVars )
  223. {
  224. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_GLOBAL_VARS_NOT_ALLOWED);
  225. error = true;
  226. }
  227. module->scriptGlobals.Allocate(count, 0);
  228. for( i = 0; i < count && !error; ++i )
  229. {
  230. ReadGlobalProperty();
  231. }
  232. // scriptFunctions[]
  233. count = ReadEncodedUInt();
  234. for( i = 0; i < count && !error; ++i )
  235. {
  236. size_t len = module->scriptFunctions.GetLength();
  237. bool isNew;
  238. func = ReadFunction(isNew);
  239. if( func == 0 )
  240. {
  241. error = true;
  242. break;
  243. }
  244. // Is the function shared and was it created now?
  245. if( func->isShared && len != module->scriptFunctions.GetLength() )
  246. {
  247. // If the function already existed in another module, then
  248. // we need to replace it with previously existing one
  249. for( asUINT n = 0; n < engine->scriptFunctions.GetLength() && !error; n++ )
  250. {
  251. asCScriptFunction *realFunc = engine->scriptFunctions[n];
  252. if( realFunc &&
  253. realFunc != func &&
  254. realFunc->IsShared() &&
  255. realFunc->IsSignatureEqual(func) )
  256. {
  257. // Replace the recently created function with the pre-existing function
  258. module->scriptFunctions[module->scriptFunctions.GetLength()-1] = realFunc;
  259. realFunc->AddRef();
  260. savedFunctions[savedFunctions.GetLength()-1] = realFunc;
  261. engine->FreeScriptFunctionId(func->id);
  262. // Insert the function in the dontTranslate array
  263. dontTranslate.Insert(realFunc, true);
  264. // Release the function, but make sure nothing else is released
  265. func->id = 0;
  266. func->byteCode.SetLength(0);
  267. func->Release();
  268. break;
  269. }
  270. }
  271. }
  272. }
  273. // globalFunctions[]
  274. count = ReadEncodedUInt();
  275. for( i = 0; i < count && !error; ++i )
  276. {
  277. bool isNew;
  278. func = ReadFunction(isNew, false, false);
  279. if( func )
  280. {
  281. module->globalFunctions.Put(func);
  282. func->AddRef();
  283. }
  284. else
  285. error = true;
  286. }
  287. if( error ) return asERROR;
  288. // bindInformations[]
  289. count = ReadEncodedUInt();
  290. module->bindInformations.Allocate(count, 0);
  291. for( i = 0; i < count && !error; ++i )
  292. {
  293. sBindInfo *info = asNEW(sBindInfo);
  294. if( info == 0 )
  295. return asOUT_OF_MEMORY;
  296. bool isNew;
  297. info->importedFunctionSignature = ReadFunction(isNew, false, false);
  298. if( info->importedFunctionSignature == 0 )
  299. {
  300. error = true;
  301. break;
  302. }
  303. if( engine->freeImportedFunctionIdxs.GetLength() )
  304. {
  305. int id = engine->freeImportedFunctionIdxs.PopLast();
  306. info->importedFunctionSignature->id = int(FUNC_IMPORTED + id);
  307. engine->importedFunctions[id] = info;
  308. }
  309. else
  310. {
  311. info->importedFunctionSignature->id = int(FUNC_IMPORTED + engine->importedFunctions.GetLength());
  312. engine->importedFunctions.PushLast(info);
  313. }
  314. ReadString(&info->importFromModule);
  315. info->boundFunctionId = -1;
  316. module->bindInformations.PushLast(info);
  317. }
  318. if( error ) return asERROR;
  319. // usedTypes[]
  320. count = ReadEncodedUInt();
  321. usedTypes.Allocate(count, 0);
  322. for( i = 0; i < count && !error; ++i )
  323. {
  324. asCObjectType *ot = ReadObjectType();
  325. usedTypes.PushLast(ot);
  326. }
  327. // usedTypeIds[]
  328. if( !error )
  329. ReadUsedTypeIds();
  330. // usedFunctions[]
  331. if( !error )
  332. ReadUsedFunctions();
  333. // usedGlobalProperties[]
  334. if( !error )
  335. ReadUsedGlobalProps();
  336. // usedStringConstants[]
  337. if( !error )
  338. ReadUsedStringConstants();
  339. // usedObjectProperties
  340. if( !error )
  341. ReadUsedObjectProps();
  342. // Validate the template types
  343. if( !error )
  344. {
  345. for( i = 0; i < usedTypes.GetLength() && !error; i++ )
  346. {
  347. if( !(usedTypes[i]->flags & asOBJ_TEMPLATE) ||
  348. !usedTypes[i]->beh.templateCallback )
  349. continue;
  350. bool dontGarbageCollect = false;
  351. asCScriptFunction *callback = engine->scriptFunctions[usedTypes[i]->beh.templateCallback];
  352. if( !engine->CallGlobalFunctionRetBool(usedTypes[i], &dontGarbageCollect, callback->sysFuncIntf, callback) )
  353. {
  354. asCString sub = usedTypes[i]->templateSubTypes[0].Format();
  355. for( asUINT n = 1; n < usedTypes[i]->templateSubTypes.GetLength(); n++ )
  356. {
  357. sub += ",";
  358. sub += usedTypes[i]->templateSubTypes[n].Format();
  359. }
  360. asCString str;
  361. str.Format(TXT_INSTANCING_INVLD_TMPL_TYPE_s_s, usedTypes[i]->name.AddressOf(), sub.AddressOf());
  362. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  363. error = true;
  364. }
  365. }
  366. }
  367. engine->deferValidationOfTemplateTypes = false;
  368. if( error ) return asERROR;
  369. // Update the loaded bytecode to point to the correct types, property offsets,
  370. // function ids, etc. This is basically a linking stage.
  371. for( i = 0; i < module->scriptFunctions.GetLength() && !error; i++ )
  372. if( module->scriptFunctions[i]->funcType == asFUNC_SCRIPT )
  373. TranslateFunction(module->scriptFunctions[i]);
  374. asCSymbolTable<asCGlobalProperty>::iterator globIt = module->scriptGlobals.List();
  375. while( globIt && !error )
  376. {
  377. asCScriptFunction *initFunc = (*globIt)->GetInitFunc();
  378. if( initFunc )
  379. TranslateFunction(initFunc);
  380. globIt++;
  381. }
  382. if( error ) return asERROR;
  383. // Add references for all functions (except for the pre-existing shared code)
  384. for( i = 0; i < module->scriptFunctions.GetLength(); i++ )
  385. if( !dontTranslate.MoveTo(0, module->scriptFunctions[i]) )
  386. module->scriptFunctions[i]->AddReferences();
  387. globIt = module->scriptGlobals.List();
  388. while( globIt )
  389. {
  390. asCScriptFunction *initFunc = (*globIt)->GetInitFunc();
  391. if( initFunc )
  392. initFunc->AddReferences();
  393. globIt++;
  394. }
  395. return error ? asERROR : asSUCCESS;
  396. }
  397. void asCReader::ReadUsedStringConstants()
  398. {
  399. asCString str;
  400. asUINT count;
  401. count = ReadEncodedUInt();
  402. usedStringConstants.Allocate(count, 0);
  403. for( asUINT i = 0; i < count; ++i )
  404. {
  405. ReadString(&str);
  406. usedStringConstants.PushLast(engine->AddConstantString(str.AddressOf(), str.GetLength()));
  407. }
  408. }
  409. void asCReader::ReadUsedFunctions()
  410. {
  411. asUINT count;
  412. count = ReadEncodedUInt();
  413. usedFunctions.SetLength(count);
  414. memset(usedFunctions.AddressOf(), 0, sizeof(asCScriptFunction *)*count);
  415. for( asUINT n = 0; n < usedFunctions.GetLength(); n++ )
  416. {
  417. char c;
  418. // Read the data to be able to uniquely identify the function
  419. // Is the function from the module or the application?
  420. ReadData(&c, 1);
  421. if( c == 'n' )
  422. {
  423. // Null function pointer
  424. usedFunctions[n] = 0;
  425. }
  426. else
  427. {
  428. asCScriptFunction func(engine, c == 'm' ? module : 0, asFUNC_DUMMY);
  429. ReadFunctionSignature(&func);
  430. // Find the correct function
  431. if( c == 'm' )
  432. {
  433. for( asUINT i = 0; i < module->scriptFunctions.GetLength(); i++ )
  434. {
  435. asCScriptFunction *f = module->scriptFunctions[i];
  436. if( !func.IsSignatureEqual(f) ||
  437. func.objectType != f->objectType ||
  438. func.funcType != f->funcType ||
  439. func.nameSpace != f->nameSpace )
  440. continue;
  441. usedFunctions[n] = f;
  442. break;
  443. }
  444. }
  445. else
  446. {
  447. for( asUINT i = 0; i < engine->scriptFunctions.GetLength(); i++ )
  448. {
  449. asCScriptFunction *f = engine->scriptFunctions[i];
  450. if( f == 0 ||
  451. !func.IsSignatureEqual(f) ||
  452. func.objectType != f->objectType ||
  453. func.nameSpace != f->nameSpace )
  454. continue;
  455. usedFunctions[n] = f;
  456. break;
  457. }
  458. }
  459. // Set the type to dummy so it won't try to release the id
  460. func.funcType = asFUNC_DUMMY;
  461. }
  462. }
  463. }
  464. void asCReader::ReadFunctionSignature(asCScriptFunction *func)
  465. {
  466. int i, count;
  467. asCDataType dt;
  468. int num;
  469. ReadString(&func->name);
  470. ReadDataType(&func->returnType);
  471. count = ReadEncodedUInt();
  472. func->parameterTypes.Allocate(count, 0);
  473. for( i = 0; i < count; ++i )
  474. {
  475. ReadDataType(&dt);
  476. func->parameterTypes.PushLast(dt);
  477. }
  478. count = ReadEncodedUInt();
  479. func->inOutFlags.Allocate(count, 0);
  480. for( i = 0; i < count; ++i )
  481. {
  482. num = ReadEncodedUInt();
  483. func->inOutFlags.PushLast(static_cast<asETypeModifiers>(num));
  484. }
  485. func->funcType = (asEFuncType)ReadEncodedUInt();
  486. // Read the default args, from last to first
  487. count = ReadEncodedUInt();
  488. if( count )
  489. {
  490. func->defaultArgs.SetLength(func->parameterTypes.GetLength());
  491. memset(func->defaultArgs.AddressOf(), 0, sizeof(asCString*)*func->parameterTypes.GetLength());
  492. for( i = 0; i < count; i++ )
  493. {
  494. asCString *str = asNEW(asCString);
  495. if( str == 0 )
  496. {
  497. // Out of memory
  498. error = true;
  499. return;
  500. }
  501. func->defaultArgs[func->defaultArgs.GetLength()-1-i] = str;
  502. ReadString(str);
  503. }
  504. }
  505. func->objectType = ReadObjectType();
  506. if( func->objectType )
  507. {
  508. asBYTE b;
  509. ReadData(&b, 1);
  510. func->isReadOnly = (b & 1) ? true : false;
  511. func->isPrivate = (b & 2) ? true : false;
  512. func->nameSpace = engine->nameSpaces[0];
  513. }
  514. else
  515. {
  516. asCString ns;
  517. ReadString(&ns);
  518. func->nameSpace = engine->AddNameSpace(ns.AddressOf());
  519. }
  520. }
  521. asCScriptFunction *asCReader::ReadFunction(bool &isNew, bool addToModule, bool addToEngine, bool addToGC)
  522. {
  523. isNew = false;
  524. if( error ) return 0;
  525. char c;
  526. ReadData(&c, 1);
  527. if( c == '\0' )
  528. {
  529. // There is no function, so return a null pointer
  530. return 0;
  531. }
  532. if( c == 'r' )
  533. {
  534. // This is a reference to a previously saved function
  535. asUINT index = ReadEncodedUInt();
  536. if( index < savedFunctions.GetLength() )
  537. return savedFunctions[index];
  538. else
  539. {
  540. error = true;
  541. return 0;
  542. }
  543. }
  544. // Load the new function
  545. isNew = true;
  546. asCScriptFunction *func = asNEW(asCScriptFunction)(engine,module,asFUNC_DUMMY);
  547. if( func == 0 )
  548. {
  549. // Out of memory
  550. error = true;
  551. return 0;
  552. }
  553. savedFunctions.PushLast(func);
  554. int i, count;
  555. asCDataType dt;
  556. int num;
  557. ReadFunctionSignature(func);
  558. if( func->funcType == asFUNC_SCRIPT )
  559. {
  560. if( addToGC && !addToModule )
  561. engine->gc.AddScriptObjectToGC(func, &engine->functionBehaviours);
  562. ReadByteCode(func);
  563. func->variableSpace = ReadEncodedUInt();
  564. count = ReadEncodedUInt();
  565. func->objVariablePos.Allocate(count, 0);
  566. func->objVariableTypes.Allocate(count, 0);
  567. func->funcVariableTypes.Allocate(count, 0);
  568. for( i = 0; i < count; ++i )
  569. {
  570. func->objVariableTypes.PushLast(ReadObjectType());
  571. asUINT idx = ReadEncodedUInt();
  572. func->funcVariableTypes.PushLast((asCScriptFunction*)(asPWORD)idx);
  573. num = ReadEncodedUInt();
  574. func->objVariablePos.PushLast(num);
  575. }
  576. if( count > 0 )
  577. func->objVariablesOnHeap = ReadEncodedUInt();
  578. else
  579. func->objVariablesOnHeap = 0;
  580. int length = ReadEncodedUInt();
  581. func->objVariableInfo.SetLength(length);
  582. for( i = 0; i < length; ++i )
  583. {
  584. func->objVariableInfo[i].programPos = ReadEncodedUInt();
  585. func->objVariableInfo[i].variableOffset = ReadEncodedUInt();
  586. func->objVariableInfo[i].option = ReadEncodedUInt();
  587. }
  588. if( !noDebugInfo )
  589. {
  590. length = ReadEncodedUInt();
  591. func->lineNumbers.SetLength(length);
  592. for( i = 0; i < length; ++i )
  593. func->lineNumbers[i] = ReadEncodedUInt();
  594. // Read the array of script sections
  595. length = ReadEncodedUInt();
  596. func->sectionIdxs.SetLength(length);
  597. for( i = 0; i < length; ++i )
  598. func->sectionIdxs[i] = ReadEncodedUInt();
  599. }
  600. ReadData(&func->isShared, 1);
  601. // Read the variable information
  602. if( !noDebugInfo )
  603. {
  604. length = ReadEncodedUInt();
  605. func->variables.Allocate(length, 0);
  606. for( i = 0; i < length; i++ )
  607. {
  608. asSScriptVariable *var = asNEW(asSScriptVariable);
  609. if( var == 0 )
  610. {
  611. // Out of memory
  612. error = true;
  613. return 0;
  614. }
  615. func->variables.PushLast(var);
  616. var->declaredAtProgramPos = ReadEncodedUInt();
  617. var->stackOffset = ReadEncodedUInt();
  618. ReadString(&var->name);
  619. ReadDataType(&var->type);
  620. }
  621. }
  622. ReadData(&func->dontCleanUpOnException, 1);
  623. }
  624. else if( func->funcType == asFUNC_VIRTUAL )
  625. {
  626. func->vfTableIdx = ReadEncodedUInt();
  627. }
  628. // Read script section name
  629. if( !noDebugInfo )
  630. {
  631. asCString name;
  632. ReadString(&name);
  633. func->scriptSectionIdx = engine->GetScriptSectionNameIndex(name.AddressOf());
  634. }
  635. if( addToModule )
  636. {
  637. // The refCount is already 1
  638. module->scriptFunctions.PushLast(func);
  639. }
  640. if( addToEngine )
  641. {
  642. func->id = engine->GetNextScriptFunctionId();
  643. engine->SetScriptFunction(func);
  644. }
  645. if( func->objectType )
  646. func->ComputeSignatureId();
  647. return func;
  648. }
  649. void asCReader::ReadObjectTypeDeclaration(asCObjectType *ot, int phase)
  650. {
  651. if( phase == 1 )
  652. {
  653. // Read the initial attributes
  654. ReadString(&ot->name);
  655. ReadData(&ot->flags, 4);
  656. ot->size = ReadEncodedUInt();
  657. asCString ns;
  658. ReadString(&ns);
  659. ot->nameSpace = engine->AddNameSpace(ns.AddressOf());
  660. // Reset the size of script classes, since it will be recalculated as properties are added
  661. if( (ot->flags & asOBJ_SCRIPT_OBJECT) && ot->size != 0 )
  662. ot->size = sizeof(asCScriptObject);
  663. // Use the default script class behaviours
  664. ot->beh = engine->scriptTypeBehaviours.beh;
  665. ot->beh.construct = 0;
  666. ot->beh.factory = 0;
  667. ot->beh.constructors.PopLast(); // These will be read from the file
  668. ot->beh.factories.PopLast(); // These will be read from the file
  669. engine->scriptFunctions[ot->beh.addref]->AddRef();
  670. engine->scriptFunctions[ot->beh.release]->AddRef();
  671. engine->scriptFunctions[ot->beh.gcEnumReferences]->AddRef();
  672. engine->scriptFunctions[ot->beh.gcGetFlag]->AddRef();
  673. engine->scriptFunctions[ot->beh.gcGetRefCount]->AddRef();
  674. engine->scriptFunctions[ot->beh.gcReleaseAllReferences]->AddRef();
  675. engine->scriptFunctions[ot->beh.gcSetFlag]->AddRef();
  676. engine->scriptFunctions[ot->beh.copy]->AddRef();
  677. for( asUINT i = 1; i < ot->beh.operators.GetLength(); i += 2 )
  678. engine->scriptFunctions[ot->beh.operators[i]]->AddRef();
  679. }
  680. else if( phase == 2 )
  681. {
  682. if( ot->flags & asOBJ_ENUM )
  683. {
  684. int count = ReadEncodedUInt();
  685. bool sharedExists = existingShared.MoveTo(0, ot);
  686. if( !sharedExists )
  687. {
  688. ot->enumValues.Allocate(count, 0);
  689. for( int n = 0; n < count; n++ )
  690. {
  691. asSEnumValue *e = asNEW(asSEnumValue);
  692. if( e == 0 )
  693. {
  694. // Out of memory
  695. error = true;
  696. return;
  697. }
  698. ReadString(&e->name);
  699. ReadData(&e->value, 4); // TODO: Should be encoded
  700. ot->enumValues.PushLast(e);
  701. }
  702. }
  703. else
  704. {
  705. // Verify that the enum values exists in the original
  706. asCString name;
  707. int value;
  708. for( int n = 0; n < count; n++ )
  709. {
  710. ReadString(&name);
  711. ReadData(&value, 4); // TODO: Should be encoded
  712. bool found = false;
  713. for( asUINT e = 0; e < ot->enumValues.GetLength(); e++ )
  714. {
  715. if( ot->enumValues[e]->name == name &&
  716. ot->enumValues[e]->value == value )
  717. {
  718. found = true;
  719. break;
  720. }
  721. }
  722. if( !found )
  723. {
  724. asCString str;
  725. str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, ot->GetName());
  726. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  727. error = true;
  728. }
  729. }
  730. }
  731. }
  732. else if( ot->flags & asOBJ_TYPEDEF )
  733. {
  734. eTokenType t = (eTokenType)ReadEncodedUInt();
  735. ot->templateSubTypes.PushLast(asCDataType::CreatePrimitive(t, false));
  736. }
  737. else
  738. {
  739. // If the type is shared and pre-existing, we should just
  740. // validate that the loaded methods match the original
  741. bool sharedExists = existingShared.MoveTo(0, ot);
  742. if( sharedExists )
  743. {
  744. asCObjectType *dt = ReadObjectType();
  745. if( ot->derivedFrom != dt )
  746. {
  747. asCString str;
  748. str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, ot->GetName());
  749. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  750. error = true;
  751. }
  752. }
  753. else
  754. {
  755. ot->derivedFrom = ReadObjectType();
  756. if( ot->derivedFrom )
  757. ot->derivedFrom->AddRef();
  758. }
  759. // interfaces[]
  760. int size = ReadEncodedUInt();
  761. if( sharedExists )
  762. {
  763. for( int n = 0; n < size; n++ )
  764. {
  765. asCObjectType *intf = ReadObjectType();
  766. if( !ot->Implements(intf) )
  767. {
  768. asCString str;
  769. str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, ot->GetName());
  770. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  771. error = true;
  772. }
  773. }
  774. }
  775. else
  776. {
  777. ot->interfaces.Allocate(size,0);
  778. for( int n = 0; n < size; n++ )
  779. {
  780. asCObjectType *intf = ReadObjectType();
  781. ot->interfaces.PushLast(intf);
  782. }
  783. }
  784. // behaviours
  785. if( !ot->IsInterface() && ot->flags != asOBJ_TYPEDEF && ot->flags != asOBJ_ENUM )
  786. {
  787. bool isNew;
  788. asCScriptFunction *func = ReadFunction(isNew, !sharedExists, !sharedExists, !sharedExists);
  789. if( sharedExists )
  790. {
  791. // Find the real function in the object, and update the savedFunctions array
  792. asCScriptFunction *realFunc = engine->GetScriptFunction(ot->beh.destruct);
  793. if( (realFunc == 0 && func == 0) || realFunc->IsSignatureEqual(func) )
  794. {
  795. // If the function is not the last, then the substitution has already occurred before
  796. if( func && savedFunctions[savedFunctions.GetLength()-1] == func )
  797. savedFunctions[savedFunctions.GetLength()-1] = realFunc;
  798. }
  799. else
  800. {
  801. asCString str;
  802. str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, ot->GetName());
  803. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  804. error = true;
  805. }
  806. if( func )
  807. {
  808. if( isNew )
  809. {
  810. // Destroy the function without releasing any references
  811. func->id = 0;
  812. func->byteCode.SetLength(0);
  813. func->Release();
  814. }
  815. module->scriptFunctions.PushLast(realFunc);
  816. realFunc->AddRef();
  817. dontTranslate.Insert(realFunc, true);
  818. }
  819. }
  820. else
  821. {
  822. if( func )
  823. {
  824. ot->beh.destruct = func->id;
  825. func->AddRef();
  826. }
  827. else
  828. ot->beh.destruct = 0;
  829. }
  830. size = ReadEncodedUInt();
  831. for( int n = 0; n < size; n++ )
  832. {
  833. bool isNew;
  834. asCScriptFunction *func = ReadFunction(isNew, !sharedExists, !sharedExists, !sharedExists);
  835. if( func )
  836. {
  837. if( sharedExists )
  838. {
  839. // Find the real function in the object, and update the savedFunctions array
  840. bool found = false;
  841. for( asUINT n = 0; n < ot->beh.constructors.GetLength(); n++ )
  842. {
  843. asCScriptFunction *realFunc = engine->GetScriptFunction(ot->beh.constructors[n]);
  844. if( realFunc->IsSignatureEqual(func) )
  845. {
  846. // If the function is not the last, then the substitution has already occurred before
  847. if( savedFunctions[savedFunctions.GetLength()-1] == func )
  848. savedFunctions[savedFunctions.GetLength()-1] = realFunc;
  849. found = true;
  850. module->scriptFunctions.PushLast(realFunc);
  851. realFunc->AddRef();
  852. dontTranslate.Insert(realFunc, true);
  853. break;
  854. }
  855. }
  856. if( !found )
  857. {
  858. asCString str;
  859. str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, ot->GetName());
  860. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  861. error = true;
  862. }
  863. if( isNew )
  864. {
  865. // Destroy the function without releasing any references
  866. func->id = 0;
  867. func->byteCode.SetLength(0);
  868. func->Release();
  869. }
  870. }
  871. else
  872. {
  873. ot->beh.constructors.PushLast(func->id);
  874. func->AddRef();
  875. if( func->parameterTypes.GetLength() == 0 )
  876. ot->beh.construct = func->id;
  877. }
  878. }
  879. else
  880. {
  881. // TODO: Write message
  882. error = true;
  883. }
  884. func = ReadFunction(isNew, !sharedExists, !sharedExists, !sharedExists);
  885. if( func )
  886. {
  887. if( sharedExists )
  888. {
  889. // Find the real function in the object, and update the savedFunctions array
  890. bool found = false;
  891. for( asUINT n = 0; n < ot->beh.factories.GetLength(); n++ )
  892. {
  893. asCScriptFunction *realFunc = engine->GetScriptFunction(ot->beh.factories[n]);
  894. if( realFunc->IsSignatureEqual(func) )
  895. {
  896. // If the function is not the last, then the substitution has already occurred before
  897. if( savedFunctions[savedFunctions.GetLength()-1] == func )
  898. savedFunctions[savedFunctions.GetLength()-1] = realFunc;
  899. found = true;
  900. module->scriptFunctions.PushLast(realFunc);
  901. realFunc->AddRef();
  902. dontTranslate.Insert(realFunc, true);
  903. break;
  904. }
  905. }
  906. if( !found )
  907. {
  908. asCString str;
  909. str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, ot->GetName());
  910. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  911. error = true;
  912. }
  913. if( isNew )
  914. {
  915. // Destroy the function without releasing any references
  916. func->id = 0;
  917. func->byteCode.SetLength(0);
  918. func->Release();
  919. }
  920. }
  921. else
  922. {
  923. ot->beh.factories.PushLast(func->id);
  924. func->AddRef();
  925. if( func->parameterTypes.GetLength() == 0 )
  926. ot->beh.factory = func->id;
  927. }
  928. }
  929. else
  930. {
  931. // TODO: Write message
  932. error = true;
  933. }
  934. }
  935. }
  936. // methods[]
  937. size = ReadEncodedUInt();
  938. int n;
  939. for( n = 0; n < size; n++ )
  940. {
  941. bool isNew;
  942. asCScriptFunction *func = ReadFunction(isNew, !sharedExists, !sharedExists, !sharedExists);
  943. if( func )
  944. {
  945. if( sharedExists )
  946. {
  947. // Find the real function in the object, and update the savedFunctions array
  948. bool found = false;
  949. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  950. {
  951. asCScriptFunction *realFunc = engine->GetScriptFunction(ot->methods[n]);
  952. if( realFunc->IsSignatureEqual(func) )
  953. {
  954. // If the function is not the last, then the substitution has already occurred before
  955. if( savedFunctions[savedFunctions.GetLength()-1] == func )
  956. savedFunctions[savedFunctions.GetLength()-1] = realFunc;
  957. found = true;
  958. module->scriptFunctions.PushLast(realFunc);
  959. realFunc->AddRef();
  960. dontTranslate.Insert(realFunc, true);
  961. break;
  962. }
  963. }
  964. if( !found )
  965. {
  966. asCString str;
  967. str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, ot->GetName());
  968. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  969. error = true;
  970. }
  971. if( isNew )
  972. {
  973. // Destroy the function without releasing any references
  974. func->id = 0;
  975. func->byteCode.SetLength(0);
  976. func->Release();
  977. }
  978. }
  979. else
  980. {
  981. // If the method is the assignment operator we need to replace the default implementation
  982. if( func->name == "opAssign" && func->parameterTypes.GetLength() == 1 &&
  983. func->parameterTypes[0].GetObjectType() == func->objectType &&
  984. (func->inOutFlags[0] & asTM_INREF) )
  985. {
  986. engine->scriptFunctions[ot->beh.copy]->Release();
  987. ot->beh.copy = func->id;
  988. func->AddRef();
  989. }
  990. ot->methods.PushLast(func->id);
  991. func->AddRef();
  992. }
  993. }
  994. else
  995. {
  996. // TODO: Write message
  997. error = true;
  998. }
  999. }
  1000. // virtualFunctionTable[]
  1001. size = ReadEncodedUInt();
  1002. for( n = 0; n < size; n++ )
  1003. {
  1004. bool isNew;
  1005. asCScriptFunction *func = ReadFunction(isNew, !sharedExists, !sharedExists, !sharedExists);
  1006. if( func )
  1007. {
  1008. if( sharedExists )
  1009. {
  1010. // Find the real function in the object, and update the savedFunctions array
  1011. bool found = false;
  1012. for( asUINT n = 0; n < ot->virtualFunctionTable.GetLength(); n++ )
  1013. {
  1014. asCScriptFunction *realFunc = ot->virtualFunctionTable[n];
  1015. if( realFunc->IsSignatureEqual(func) )
  1016. {
  1017. // If the function is not the last, then the substitution has already occurred before
  1018. if( savedFunctions[savedFunctions.GetLength()-1] == func )
  1019. savedFunctions[savedFunctions.GetLength()-1] = realFunc;
  1020. found = true;
  1021. module->scriptFunctions.PushLast(realFunc);
  1022. realFunc->AddRef();
  1023. dontTranslate.Insert(realFunc, true);
  1024. break;
  1025. }
  1026. }
  1027. if( !found )
  1028. {
  1029. asCString str;
  1030. str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, ot->GetName());
  1031. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  1032. error = true;
  1033. }
  1034. if( isNew )
  1035. {
  1036. // Destroy the function without releasing any references
  1037. func->id = 0;
  1038. func->byteCode.SetLength(0);
  1039. func->Release();
  1040. }
  1041. }
  1042. else
  1043. {
  1044. ot->virtualFunctionTable.PushLast(func);
  1045. func->AddRef();
  1046. }
  1047. }
  1048. else
  1049. {
  1050. // TODO: Write message
  1051. error = true;
  1052. }
  1053. }
  1054. }
  1055. }
  1056. else if( phase == 3 )
  1057. {
  1058. // properties[]
  1059. asUINT size = ReadEncodedUInt();
  1060. for( asUINT n = 0; n < size; n++ )
  1061. ReadObjectProperty(ot);
  1062. }
  1063. }
  1064. asWORD asCReader::ReadEncodedUInt16()
  1065. {
  1066. asDWORD dw = ReadEncodedUInt();
  1067. if( (dw>>16) != 0 && (dw>>16) != 0xFFFF )
  1068. {
  1069. // TODO: Write message
  1070. error = true;
  1071. }
  1072. return asWORD(dw & 0xFFFF);
  1073. }
  1074. asUINT asCReader::ReadEncodedUInt()
  1075. {
  1076. asQWORD qw = ReadEncodedUInt64();
  1077. if( (qw>>32) != 0 && (qw>>32) != 0xFFFFFFFF )
  1078. {
  1079. // TODO: Write message
  1080. error = true;
  1081. }
  1082. return asUINT(qw & 0xFFFFFFFFu);
  1083. }
  1084. asQWORD asCReader::ReadEncodedUInt64()
  1085. {
  1086. asQWORD i = 0;
  1087. asBYTE b;
  1088. ReadData(&b, 1);
  1089. bool isNegative = ( b & 0x80 ) ? true : false;
  1090. b &= 0x7F;
  1091. if( (b & 0x7F) == 0x7F )
  1092. {
  1093. ReadData(&b, 1); i = asQWORD(b) << 56;
  1094. ReadData(&b, 1); i += asQWORD(b) << 48;
  1095. ReadData(&b, 1); i += asQWORD(b) << 40;
  1096. ReadData(&b, 1); i += asQWORD(b) << 32;
  1097. ReadData(&b, 1); i += asUINT(b) << 24;
  1098. ReadData(&b, 1); i += asUINT(b) << 16;
  1099. ReadData(&b, 1); i += asUINT(b) << 8;
  1100. ReadData(&b, 1); i += b;
  1101. }
  1102. else if( (b & 0x7E) == 0x7E )
  1103. {
  1104. i = asQWORD(b & 0x01) << 48;
  1105. ReadData(&b, 1); i += asQWORD(b) << 40;
  1106. ReadData(&b, 1); i += asQWORD(b) << 32;
  1107. ReadData(&b, 1); i += asUINT(b) << 24;
  1108. ReadData(&b, 1); i += asUINT(b) << 16;
  1109. ReadData(&b, 1); i += asUINT(b) << 8;
  1110. ReadData(&b, 1); i += b;
  1111. }
  1112. else if( (b & 0x7C) == 0x7C )
  1113. {
  1114. i = asQWORD(b & 0x03) << 40;
  1115. ReadData(&b, 1); i += asQWORD(b) << 32;
  1116. ReadData(&b, 1); i += asUINT(b) << 24;
  1117. ReadData(&b, 1); i += asUINT(b) << 16;
  1118. ReadData(&b, 1); i += asUINT(b) << 8;
  1119. ReadData(&b, 1); i += b;
  1120. }
  1121. else if( (b & 0x78) == 0x78 )
  1122. {
  1123. i = asQWORD(b & 0x07) << 32;
  1124. ReadData(&b, 1); i += asUINT(b) << 24;
  1125. ReadData(&b, 1); i += asUINT(b) << 16;
  1126. ReadData(&b, 1); i += asUINT(b) << 8;
  1127. ReadData(&b, 1); i += b;
  1128. }
  1129. else if( (b & 0x70) == 0x70 )
  1130. {
  1131. i = asUINT(b & 0x0F) << 24;
  1132. ReadData(&b, 1); i += asUINT(b) << 16;
  1133. ReadData(&b, 1); i += asUINT(b) << 8;
  1134. ReadData(&b, 1); i += b;
  1135. }
  1136. else if( (b & 0x60) == 0x60 )
  1137. {
  1138. i = asUINT(b & 0x1F) << 16;
  1139. ReadData(&b, 1); i += asUINT(b) << 8;
  1140. ReadData(&b, 1); i += b;
  1141. }
  1142. else if( (b & 0x40) == 0x40 )
  1143. {
  1144. i = asUINT(b & 0x3F) << 8;
  1145. ReadData(&b, 1); i += b;
  1146. }
  1147. else
  1148. {
  1149. i = b;
  1150. }
  1151. if( isNegative )
  1152. i = (asQWORD)(-asINT64(i));
  1153. return i;
  1154. }
  1155. void asCReader::ReadString(asCString* str)
  1156. {
  1157. char b;
  1158. ReadData(&b, 1);
  1159. if( b == '\0' )
  1160. {
  1161. str->SetLength(0);
  1162. }
  1163. else if( b == 'n' )
  1164. {
  1165. asUINT len = ReadEncodedUInt();
  1166. str->SetLength(len);
  1167. stream->Read(str->AddressOf(), len);
  1168. savedStrings.PushLast(*str);
  1169. }
  1170. else
  1171. {
  1172. asUINT n = ReadEncodedUInt();
  1173. if( n < savedStrings.GetLength() )
  1174. *str = savedStrings[n];
  1175. else
  1176. error = true;
  1177. }
  1178. }
  1179. void asCReader::ReadGlobalProperty()
  1180. {
  1181. asCString name;
  1182. asCDataType type;
  1183. ReadString(&name);
  1184. asCString ns;
  1185. ReadString(&ns);
  1186. asSNameSpace *nameSpace = engine->AddNameSpace(ns.AddressOf());
  1187. ReadDataType(&type);
  1188. asCGlobalProperty *prop = module->AllocateGlobalProperty(name.AddressOf(), type, nameSpace);
  1189. // Read the initialization function
  1190. bool f;
  1191. ReadData(&f, 1);
  1192. if( f )
  1193. {
  1194. bool isNew;
  1195. // Do not add the function to the GC at this time. It will
  1196. // only be added to the GC when the module releases the property
  1197. asCScriptFunction *func = ReadFunction(isNew, false, true, false);
  1198. if( func )
  1199. {
  1200. prop->SetInitFunc(func);
  1201. func->Release();
  1202. }
  1203. else
  1204. error = true;
  1205. }
  1206. }
  1207. void asCReader::ReadObjectProperty(asCObjectType *ot)
  1208. {
  1209. asCString name;
  1210. ReadString(&name);
  1211. asCDataType dt;
  1212. ReadDataType(&dt);
  1213. bool isPrivate;
  1214. ReadData(&isPrivate, 1);
  1215. // TODO: shared: If the type is shared and pre-existing, we should just
  1216. // validate that the loaded methods match the original
  1217. if( !existingShared.MoveTo(0, ot) )
  1218. ot->AddPropertyToClass(name, dt, isPrivate);
  1219. }
  1220. void asCReader::ReadDataType(asCDataType *dt)
  1221. {
  1222. eTokenType tokenType;
  1223. tokenType = (eTokenType)ReadEncodedUInt();
  1224. if( tokenType == 0 )
  1225. {
  1226. // Get the datatype from the cache
  1227. asUINT n = ReadEncodedUInt();
  1228. *dt = savedDataTypes[n];
  1229. return;
  1230. }
  1231. // Reserve a spot in the savedDataTypes
  1232. size_t saveSlot = savedDataTypes.GetLength();
  1233. savedDataTypes.PushLast(asCDataType());
  1234. // Read the datatype for the first time
  1235. asCObjectType *objType = 0;
  1236. bool isObjectHandle = false;
  1237. bool isReadOnly = false;
  1238. bool isHandleToConst = false;
  1239. bool isReference = false;
  1240. if( tokenType == ttIdentifier )
  1241. {
  1242. objType = ReadObjectType();
  1243. ReadData(&isObjectHandle, 1);
  1244. ReadData(&isHandleToConst, 1);
  1245. }
  1246. ReadData(&isReference, 1);
  1247. ReadData(&isReadOnly, 1);
  1248. asCScriptFunction *funcDef = 0;
  1249. if( tokenType == ttIdentifier && objType && objType->name == "_builtin_function_" )
  1250. {
  1251. asCScriptFunction func(engine, module, asFUNC_DUMMY);
  1252. ReadFunctionSignature(&func);
  1253. for( asUINT n = 0; n < engine->registeredFuncDefs.GetLength(); n++ )
  1254. {
  1255. // TODO: access: Only return the definitions that the module has access to
  1256. if( engine->registeredFuncDefs[n]->name == func.name &&
  1257. engine->registeredFuncDefs[n]->nameSpace == func.nameSpace )
  1258. {
  1259. funcDef = engine->registeredFuncDefs[n];
  1260. break;
  1261. }
  1262. }
  1263. if( !funcDef && module )
  1264. {
  1265. for( asUINT n = 0; n < module->funcDefs.GetLength(); n++ )
  1266. {
  1267. if( module->funcDefs[n]->name == func.name &&
  1268. module->funcDefs[n]->nameSpace == func.nameSpace )
  1269. {
  1270. funcDef = module->funcDefs[n];
  1271. break;
  1272. }
  1273. }
  1274. }
  1275. // Set to dummy to avoid unwanted release of resources
  1276. func.funcType = asFUNC_DUMMY;
  1277. }
  1278. if( funcDef )
  1279. *dt = asCDataType::CreateFuncDef(funcDef);
  1280. else if( tokenType == ttIdentifier )
  1281. *dt = asCDataType::CreateObject(objType, false);
  1282. else
  1283. *dt = asCDataType::CreatePrimitive(tokenType, false);
  1284. if( isObjectHandle )
  1285. {
  1286. dt->MakeReadOnly(isHandleToConst);
  1287. // Here we must allow a scoped type to be a handle
  1288. // e.g. if the datatype is for a system function
  1289. dt->MakeHandle(true, true);
  1290. }
  1291. dt->MakeReadOnly(isReadOnly);
  1292. dt->MakeReference(isReference);
  1293. // Update the previously saved slot
  1294. savedDataTypes[saveSlot] = *dt;
  1295. }
  1296. asCObjectType* asCReader::ReadObjectType()
  1297. {
  1298. asCObjectType *ot = 0;
  1299. char ch;
  1300. ReadData(&ch, 1);
  1301. if( ch == 'a' )
  1302. {
  1303. // Read the name of the template type
  1304. asCString typeName;
  1305. ReadString(&typeName);
  1306. asCObjectType *tmpl = engine->GetObjectType(typeName.AddressOf(), engine->nameSpaces[0]);
  1307. if( tmpl == 0 )
  1308. {
  1309. asCString str;
  1310. str.Format(TXT_TEMPLATE_TYPE_s_DOESNT_EXIST, typeName.AddressOf());
  1311. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  1312. error = true;
  1313. return 0;
  1314. }
  1315. asUINT numSubTypes = ReadEncodedUInt();
  1316. asCArray<asCDataType> subTypes;
  1317. for( asUINT n = 0; n < numSubTypes; n++ )
  1318. {
  1319. ReadData(&ch, 1);
  1320. if( ch == 's' )
  1321. {
  1322. asCDataType dt;
  1323. ReadDataType(&dt);
  1324. subTypes.PushLast(dt);
  1325. }
  1326. else
  1327. {
  1328. eTokenType tokenType = (eTokenType)ReadEncodedUInt();
  1329. asCDataType dt = asCDataType::CreatePrimitive(tokenType, false);
  1330. subTypes.PushLast(dt);
  1331. }
  1332. }
  1333. // Return the actual template if the subtypes are the template's dummy types
  1334. if( tmpl->templateSubTypes == subTypes )
  1335. ot = tmpl;
  1336. else
  1337. {
  1338. // Get the template instance type based on the loaded subtypes
  1339. ot = engine->GetTemplateInstanceType(tmpl, subTypes);
  1340. }
  1341. if( ot == 0 )
  1342. {
  1343. // Show all subtypes in error message
  1344. asCString sub = subTypes[0].Format();
  1345. for( asUINT n = 1; n < subTypes.GetLength(); n++ )
  1346. {
  1347. sub += ",";
  1348. sub += subTypes[n].Format();
  1349. }
  1350. asCString str;
  1351. str.Format(TXT_INSTANCING_INVLD_TMPL_TYPE_s_s, typeName.AddressOf(), sub.AddressOf());
  1352. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  1353. error = true;
  1354. return 0;
  1355. }
  1356. }
  1357. else if( ch == 's' )
  1358. {
  1359. // Read the name of the template subtype
  1360. asCString typeName;
  1361. ReadString(&typeName);
  1362. // Find the template subtype
  1363. ot = 0;
  1364. for( asUINT n = 0; n < engine->templateSubTypes.GetLength(); n++ )
  1365. {
  1366. if( engine->templateSubTypes[n] && engine->templateSubTypes[n]->name == typeName )
  1367. {
  1368. ot = engine->templateSubTypes[n];
  1369. break;
  1370. }
  1371. }
  1372. if( ot == 0 )
  1373. {
  1374. asCString str;
  1375. str.Format(TXT_TEMPLATE_SUBTYPE_s_DOESNT_EXIST, typeName.AddressOf());
  1376. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  1377. error = true;
  1378. return 0;
  1379. }
  1380. }
  1381. else if( ch == 'o' )
  1382. {
  1383. // Read the object type name
  1384. asCString typeName, ns;
  1385. ReadString(&typeName);
  1386. ReadString(&ns);
  1387. asSNameSpace *nameSpace = engine->AddNameSpace(ns.AddressOf());
  1388. if( typeName.GetLength() && typeName != "_builtin_object_" && typeName != "_builtin_function_" )
  1389. {
  1390. // Find the object type
  1391. ot = module->GetObjectType(typeName.AddressOf(), nameSpace);
  1392. if( !ot )
  1393. ot = engine->GetObjectType(typeName.AddressOf(), nameSpace);
  1394. if( ot == 0 )
  1395. {
  1396. asCString str;
  1397. str.Format(TXT_OBJECT_TYPE_s_DOESNT_EXIST, typeName.AddressOf());
  1398. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  1399. error = true;
  1400. return 0;
  1401. }
  1402. }
  1403. else if( typeName == "_builtin_object_" )
  1404. {
  1405. ot = &engine->scriptTypeBehaviours;
  1406. }
  1407. else if( typeName == "_builtin_function_" )
  1408. {
  1409. ot = &engine->functionBehaviours;
  1410. }
  1411. else
  1412. asASSERT( false );
  1413. }
  1414. else
  1415. {
  1416. // No object type
  1417. asASSERT( ch == '\0' );
  1418. ot = 0;
  1419. }
  1420. return ot;
  1421. }
  1422. void asCReader::ReadByteCode(asCScriptFunction *func)
  1423. {
  1424. // Read number of instructions
  1425. asUINT total, numInstructions;
  1426. total = numInstructions = ReadEncodedUInt();
  1427. // Reserve some space for the instructions
  1428. func->byteCode.AllocateNoConstruct(numInstructions, false);
  1429. asUINT pos = 0;
  1430. while( numInstructions )
  1431. {
  1432. asBYTE b;
  1433. ReadData(&b, 1);
  1434. // Allocate the space for the instruction
  1435. asUINT len = asBCTypeSize[asBCInfo[b].type];
  1436. asUINT newSize = asUINT(func->byteCode.GetLength()) + len;
  1437. if( func->byteCode.GetCapacity() < newSize )
  1438. {
  1439. // Determine the average size of the loaded instructions and re-estimate the final size
  1440. asUINT size = asUINT(float(newSize) / (total - numInstructions) * total) + 1;
  1441. func->byteCode.AllocateNoConstruct(size, true);
  1442. }
  1443. if( !func->byteCode.SetLengthNoConstruct(newSize) )
  1444. {
  1445. // Out of memory
  1446. error = true;
  1447. return;
  1448. }
  1449. asDWORD *bc = func->byteCode.AddressOf() + pos;
  1450. pos += len;
  1451. switch( asBCInfo[b].type )
  1452. {
  1453. case asBCTYPE_NO_ARG:
  1454. {
  1455. *(asBYTE*)(bc) = b;
  1456. bc++;
  1457. }
  1458. break;
  1459. case asBCTYPE_W_ARG:
  1460. case asBCTYPE_wW_ARG:
  1461. case asBCTYPE_rW_ARG:
  1462. {
  1463. *(asBYTE*)(bc) = b;
  1464. // Read the argument
  1465. asWORD w = ReadEncodedUInt16();
  1466. *(((asWORD*)bc)+1) = w;
  1467. bc++;
  1468. }
  1469. break;
  1470. case asBCTYPE_rW_DW_ARG:
  1471. case asBCTYPE_wW_DW_ARG:
  1472. case asBCTYPE_W_DW_ARG:
  1473. {
  1474. *(asBYTE*)(bc) = b;
  1475. // Read the word argument
  1476. asWORD w = ReadEncodedUInt16();
  1477. *(((asWORD*)bc)+1) = w;
  1478. bc++;
  1479. // Read the dword argument
  1480. *bc++ = ReadEncodedUInt();
  1481. }
  1482. break;
  1483. case asBCTYPE_DW_ARG:
  1484. {
  1485. *(asBYTE*)(bc) = b;
  1486. bc++;
  1487. // Read the argument
  1488. *bc++ = ReadEncodedUInt();
  1489. }
  1490. break;
  1491. case asBCTYPE_DW_DW_ARG:
  1492. {
  1493. *(asBYTE*)(bc) = b;
  1494. bc++;
  1495. // Read the first argument
  1496. *bc++ = ReadEncodedUInt();
  1497. // Read the second argument
  1498. *bc++ = ReadEncodedUInt();
  1499. }
  1500. break;
  1501. case asBCTYPE_wW_rW_rW_ARG:
  1502. {
  1503. *(asBYTE*)(bc) = b;
  1504. // Read the first argument
  1505. asWORD w = ReadEncodedUInt16();
  1506. *(((asWORD*)bc)+1) = w;
  1507. bc++;
  1508. // Read the second argument
  1509. w = ReadEncodedUInt16();
  1510. *(asWORD*)bc = w;
  1511. // Read the third argument
  1512. w = ReadEncodedUInt16();
  1513. *(((asWORD*)bc)+1) = w;
  1514. bc++;
  1515. }
  1516. break;
  1517. case asBCTYPE_wW_rW_ARG:
  1518. case asBCTYPE_rW_rW_ARG:
  1519. case asBCTYPE_wW_W_ARG:
  1520. {
  1521. *(asBYTE*)(bc) = b;
  1522. // Read the first argument
  1523. asWORD w = ReadEncodedUInt16();
  1524. *(((asWORD*)bc)+1) = w;
  1525. bc++;
  1526. // Read the second argument
  1527. w = ReadEncodedUInt16();
  1528. *(asWORD*)bc = w;
  1529. bc++;
  1530. }
  1531. break;
  1532. case asBCTYPE_wW_rW_DW_ARG:
  1533. case asBCTYPE_rW_W_DW_ARG:
  1534. {
  1535. *(asBYTE*)(bc) = b;
  1536. // Read the first argument
  1537. asWORD w = ReadEncodedUInt16();
  1538. *(((asWORD*)bc)+1) = w;
  1539. bc++;
  1540. // Read the second argument
  1541. w = ReadEncodedUInt16();
  1542. *(asWORD*)bc = w;
  1543. bc++;
  1544. // Read the third argument
  1545. asDWORD dw = ReadEncodedUInt();
  1546. *bc++ = dw;
  1547. }
  1548. break;
  1549. case asBCTYPE_QW_ARG:
  1550. {
  1551. *(asBYTE*)(bc) = b;
  1552. bc++;
  1553. // Read the argument
  1554. asQWORD qw = ReadEncodedUInt64();
  1555. *(asQWORD*)bc = qw;
  1556. bc += 2;
  1557. }
  1558. break;
  1559. case asBCTYPE_QW_DW_ARG:
  1560. {
  1561. *(asBYTE*)(bc) = b;
  1562. bc++;
  1563. // Read the first argument
  1564. asQWORD qw = ReadEncodedUInt64();
  1565. *(asQWORD*)bc = qw;
  1566. bc += 2;
  1567. // Read the second argument
  1568. asDWORD dw = ReadEncodedUInt();
  1569. *bc++ = dw;
  1570. }
  1571. break;
  1572. case asBCTYPE_rW_QW_ARG:
  1573. case asBCTYPE_wW_QW_ARG:
  1574. {
  1575. *(asBYTE*)(bc) = b;
  1576. // Read the first argument
  1577. asWORD w = ReadEncodedUInt16();
  1578. *(((asWORD*)bc)+1) = w;
  1579. bc++;
  1580. // Read the argument
  1581. asQWORD qw = ReadEncodedUInt64();
  1582. *(asQWORD*)bc = qw;
  1583. bc += 2;
  1584. }
  1585. break;
  1586. default:
  1587. {
  1588. // This should never happen
  1589. asASSERT(false);
  1590. // Read the next 3 bytes
  1591. asDWORD c; asBYTE t;
  1592. #if defined(AS_BIG_ENDIAN)
  1593. c = b << 24;
  1594. ReadData(&t, 1); c += t << 16;
  1595. ReadData(&t, 1); c += t << 8;
  1596. ReadData(&t, 1); c += t;
  1597. #else
  1598. c = b;
  1599. ReadData(&t, 1); c += t << 8;
  1600. ReadData(&t, 1); c += t << 16;
  1601. ReadData(&t, 1); c += t << 24;
  1602. #endif
  1603. *bc++ = c;
  1604. c = *(asBYTE*)&c;
  1605. // Read the bc as is
  1606. for( int n = 1; n < asBCTypeSize[asBCInfo[c].type]; n++ )
  1607. ReadData(&*bc++, 4);
  1608. }
  1609. }
  1610. numInstructions--;
  1611. }
  1612. // Correct the final size in case we over-estimated it
  1613. func->byteCode.SetLengthNoConstruct(pos);
  1614. }
  1615. void asCReader::ReadUsedTypeIds()
  1616. {
  1617. asUINT count = ReadEncodedUInt();
  1618. usedTypeIds.Allocate(count, 0);
  1619. for( asUINT n = 0; n < count; n++ )
  1620. {
  1621. asCDataType dt;
  1622. ReadDataType(&dt);
  1623. usedTypeIds.PushLast(engine->GetTypeIdFromDataType(dt));
  1624. }
  1625. }
  1626. void asCReader::ReadUsedGlobalProps()
  1627. {
  1628. int c = ReadEncodedUInt();
  1629. usedGlobalProperties.Allocate(c, 0);
  1630. for( int n = 0; n < c; n++ )
  1631. {
  1632. asCString name, ns;
  1633. asCDataType type;
  1634. char moduleProp;
  1635. ReadString(&name);
  1636. ReadString(&ns);
  1637. ReadDataType(&type);
  1638. ReadData(&moduleProp, 1);
  1639. asSNameSpace *nameSpace = engine->AddNameSpace(ns.AddressOf());
  1640. // Find the real property
  1641. asCGlobalProperty *globProp = 0;
  1642. if( moduleProp )
  1643. globProp = module->scriptGlobals.GetFirst(nameSpace, name);
  1644. else
  1645. globProp = engine->registeredGlobalProps.GetFirst(nameSpace, name);
  1646. void *prop = 0;
  1647. if( globProp && globProp->type == type )
  1648. prop = globProp->GetAddressOfValue();
  1649. usedGlobalProperties.PushLast(prop);
  1650. if( prop == 0 )
  1651. {
  1652. // TODO: Write error message to the callback
  1653. error = true;
  1654. }
  1655. }
  1656. }
  1657. void asCReader::ReadUsedObjectProps()
  1658. {
  1659. asUINT c = ReadEncodedUInt();
  1660. usedObjectProperties.SetLength(c);
  1661. for( asUINT n = 0; n < c; n++ )
  1662. {
  1663. asCObjectType *objType = ReadObjectType();
  1664. if( objType == 0 )
  1665. {
  1666. // TODO: Write error message to callback
  1667. error = true;
  1668. break;
  1669. }
  1670. asCString name;
  1671. ReadString(&name);
  1672. // Find the property offset
  1673. bool found = false;
  1674. for( asUINT p = 0; p < objType->properties.GetLength(); p++ )
  1675. {
  1676. if( objType->properties[p]->name == name )
  1677. {
  1678. usedObjectProperties[n].objType = objType;
  1679. usedObjectProperties[n].offset = objType->properties[p]->byteOffset;
  1680. found = true;
  1681. break;
  1682. }
  1683. }
  1684. if( !found )
  1685. {
  1686. // TODO: Write error message to callback
  1687. error = true;
  1688. return;
  1689. }
  1690. }
  1691. }
  1692. short asCReader::FindObjectPropOffset(asWORD index)
  1693. {
  1694. if( index >= usedObjectProperties.GetLength() )
  1695. {
  1696. // TODO: Write to message callback
  1697. asASSERT(false);
  1698. error = true;
  1699. return 0;
  1700. }
  1701. return (short)usedObjectProperties[index].offset;
  1702. }
  1703. asCScriptFunction *asCReader::FindFunction(int idx)
  1704. {
  1705. if( idx >= 0 && idx < (int)usedFunctions.GetLength() )
  1706. return usedFunctions[idx];
  1707. else
  1708. {
  1709. // TODO: Write to message callback
  1710. error = true;
  1711. return 0;
  1712. }
  1713. }
  1714. void asCReader::TranslateFunction(asCScriptFunction *func)
  1715. {
  1716. // Skip this if the function is part of an pre-existing shared object
  1717. if( dontTranslate.MoveTo(0, func) ) return;
  1718. // Pre-compute the size of each instruction in order to translate jump offsets
  1719. asUINT n;
  1720. asDWORD *bc = func->byteCode.AddressOf();
  1721. asCArray<asUINT> bcSizes(func->byteCode.GetLength());
  1722. asCArray<asUINT> instructionNbrToPos(func->byteCode.GetLength());
  1723. for( n = 0; n < func->byteCode.GetLength(); )
  1724. {
  1725. int c = *(asBYTE*)&bc[n];
  1726. asUINT size = asBCTypeSize[asBCInfo[c].type];
  1727. if( size == 0 )
  1728. {
  1729. error = true;
  1730. return;
  1731. }
  1732. bcSizes.PushLast(size);
  1733. instructionNbrToPos.PushLast(n);
  1734. n += size;
  1735. }
  1736. asUINT bcNum = 0;
  1737. for( n = 0; n < func->byteCode.GetLength(); bcNum++ )
  1738. {
  1739. int c = *(asBYTE*)&bc[n];
  1740. if( c == asBC_FREE ||
  1741. c == asBC_REFCPY ||
  1742. c == asBC_RefCpyV ||
  1743. c == asBC_OBJTYPE )
  1744. {
  1745. // Translate the index to the true object type
  1746. asPWORD *ot = (asPWORD*)&bc[n+1];
  1747. *(asCObjectType**)ot = FindObjectType(*(int*)ot);
  1748. }
  1749. else if( c == asBC_TYPEID ||
  1750. c == asBC_Cast )
  1751. {
  1752. // Translate the index to the type id
  1753. int *tid = (int*)&bc[n+1];
  1754. *tid = FindTypeId(*tid);
  1755. }
  1756. else if( c == asBC_ADDSi ||
  1757. c == asBC_LoadThisR )
  1758. {
  1759. // Translate the index to the type id
  1760. int *tid = (int*)&bc[n+1];
  1761. *tid = FindTypeId(*tid);
  1762. // Translate the prop index into the property offset
  1763. *(((short*)&bc[n])+1) = FindObjectPropOffset(*(((short*)&bc[n])+1));
  1764. }
  1765. else if( c == asBC_LoadRObjR ||
  1766. c == asBC_LoadVObjR )
  1767. {
  1768. // Translate the index to the type id
  1769. int *tid = (int*)&bc[n+2];
  1770. *tid = FindTypeId(*tid);
  1771. // Translate the prop index into the property offset
  1772. *(((short*)&bc[n])+2) = FindObjectPropOffset(*(((short*)&bc[n])+2));
  1773. }
  1774. else if( c == asBC_COPY )
  1775. {
  1776. // Translate the index to the type id
  1777. int *tid = (int*)&bc[n+1];
  1778. *tid = FindTypeId(*tid);
  1779. // COPY is used to copy POD types that don't have the opAssign method. It is
  1780. // also used to copy references to scoped types during variable initializations.
  1781. // Update the number of dwords to copy as it may be different on the target platform
  1782. if( (*tid) & asTYPEID_OBJHANDLE )
  1783. {
  1784. // It is the actual reference that is being copied, not the object itself
  1785. asBC_SWORDARG0(&bc[n]) = AS_PTR_SIZE;
  1786. }
  1787. else
  1788. {
  1789. asCDataType dt = engine->GetDataTypeFromTypeId(*tid);
  1790. if( !dt.IsValid() )
  1791. {
  1792. // TODO: Write error to message
  1793. error = true;
  1794. }
  1795. else
  1796. asBC_SWORDARG0(&bc[n]) = (short)dt.GetSizeInMemoryDWords();
  1797. }
  1798. }
  1799. else if( c == asBC_RET )
  1800. {
  1801. // Determine the correct amount of DWORDs to pop
  1802. asWORD dw = (asWORD)func->GetSpaceNeededForArguments();
  1803. if( func->DoesReturnOnStack() ) dw += AS_PTR_SIZE;
  1804. if( func->objectType ) dw += AS_PTR_SIZE;
  1805. asBC_WORDARG0(&bc[n]) = dw;
  1806. }
  1807. else if( c == asBC_CALL ||
  1808. c == asBC_CALLINTF ||
  1809. c == asBC_CALLSYS )
  1810. {
  1811. // Translate the index to the func id
  1812. int *fid = (int*)&bc[n+1];
  1813. asCScriptFunction *f = FindFunction(*fid);
  1814. if( f )
  1815. *fid = f->id;
  1816. else
  1817. {
  1818. // TODO: Write to message callback
  1819. error = true;
  1820. return;
  1821. }
  1822. }
  1823. else if( c == asBC_FuncPtr )
  1824. {
  1825. // Translate the index to the func pointer
  1826. asPWORD *fid = (asPWORD*)&bc[n+1];
  1827. *fid = (asPWORD)FindFunction((int)*fid);
  1828. }
  1829. else if( c == asBC_ALLOC )
  1830. {
  1831. // Translate the index to the true object type
  1832. asPWORD *arg = (asPWORD*)&bc[n+1];
  1833. *(asCObjectType**)arg = FindObjectType(*(int*)arg);
  1834. // The constructor function id must be translated, unless it is zero
  1835. int *fid = (int*)&bc[n+1+AS_PTR_SIZE];
  1836. if( *fid != 0 )
  1837. {
  1838. // Subtract 1 from the id, as it was incremented during the writing
  1839. asCScriptFunction *f = FindFunction(*fid-1);
  1840. if( f )
  1841. *fid = f->id;
  1842. else
  1843. {
  1844. // TODO: Write to message callback
  1845. error = true;
  1846. return;
  1847. }
  1848. }
  1849. }
  1850. else if( c == asBC_STR )
  1851. {
  1852. // Translate the index to the true string id
  1853. asWORD *arg = ((asWORD*)&bc[n])+1;
  1854. if( *arg < usedStringConstants.GetLength() )
  1855. *arg = (asWORD)usedStringConstants[*arg];
  1856. else
  1857. {
  1858. // TODO: Write to message callback
  1859. error = true;
  1860. return;
  1861. }
  1862. }
  1863. else if( c == asBC_CALLBND )
  1864. {
  1865. // Translate the function id
  1866. asUINT *fid = (asUINT*)&bc[n+1];
  1867. if( *fid < module->bindInformations.GetLength() )
  1868. {
  1869. sBindInfo *bi = module->bindInformations[*fid];
  1870. if( bi )
  1871. *fid = bi->importedFunctionSignature->id;
  1872. else
  1873. {
  1874. // TODO: Write to message callback
  1875. error = true;
  1876. return;
  1877. }
  1878. }
  1879. else
  1880. {
  1881. // TODO: Write to message callback
  1882. error = true;
  1883. return;
  1884. }
  1885. }
  1886. else if( c == asBC_PGA ||
  1887. c == asBC_PshGPtr ||
  1888. c == asBC_LDG ||
  1889. c == asBC_PshG4 ||
  1890. c == asBC_LdGRdR4 ||
  1891. c == asBC_CpyGtoV4 ||
  1892. c == asBC_CpyVtoG4 ||
  1893. c == asBC_SetG4 )
  1894. {
  1895. // Translate the global var index to pointer
  1896. asPWORD *index = (asPWORD*)&bc[n+1];
  1897. if( *(asUINT*)index < usedGlobalProperties.GetLength() )
  1898. *(void**)index = usedGlobalProperties[*(asUINT*)index];
  1899. else
  1900. {
  1901. // TODO: Write to message callback
  1902. error = true;
  1903. return;
  1904. }
  1905. }
  1906. else if( c == asBC_JMP ||
  1907. c == asBC_JZ ||
  1908. c == asBC_JNZ ||
  1909. c == asBC_JLowZ ||
  1910. c == asBC_JLowNZ ||
  1911. c == asBC_JS ||
  1912. c == asBC_JNS ||
  1913. c == asBC_JP ||
  1914. c == asBC_JNP ) // The JMPP instruction doesn't need modification
  1915. {
  1916. // Get the offset
  1917. int offset = int(bc[n+1]);
  1918. // Count the instruction sizes to the destination instruction
  1919. int size = 0;
  1920. if( offset >= 0 )
  1921. // If moving ahead, then start from next instruction
  1922. for( asUINT num = bcNum+1; offset-- > 0; num++ )
  1923. size += bcSizes[num];
  1924. else
  1925. // If moving backwards, then start at current instruction
  1926. for( asUINT num = bcNum; offset++ < 0; num-- )
  1927. size -= bcSizes[num];
  1928. // The size is dword offset
  1929. bc[n+1] = size;
  1930. }
  1931. n += asBCTypeSize[asBCInfo[c].type];
  1932. }
  1933. // Calculate the stack adjustments
  1934. CalculateAdjustmentByPos(func);
  1935. // Adjust all variable positions in the bytecode
  1936. bc = func->byteCode.AddressOf();
  1937. for( n = 0; n < func->byteCode.GetLength(); )
  1938. {
  1939. int c = *(asBYTE*)&bc[n];
  1940. switch( asBCInfo[c].type )
  1941. {
  1942. case asBCTYPE_wW_ARG:
  1943. case asBCTYPE_rW_DW_ARG:
  1944. case asBCTYPE_wW_QW_ARG:
  1945. case asBCTYPE_rW_ARG:
  1946. case asBCTYPE_wW_DW_ARG:
  1947. case asBCTYPE_wW_W_ARG:
  1948. case asBCTYPE_rW_QW_ARG:
  1949. case asBCTYPE_rW_W_DW_ARG:
  1950. {
  1951. asBC_SWORDARG0(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG0(&bc[n]));
  1952. }
  1953. break;
  1954. case asBCTYPE_wW_rW_ARG:
  1955. case asBCTYPE_wW_rW_DW_ARG:
  1956. case asBCTYPE_rW_rW_ARG:
  1957. {
  1958. asBC_SWORDARG0(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG0(&bc[n]));
  1959. asBC_SWORDARG1(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG1(&bc[n]));
  1960. }
  1961. break;
  1962. case asBCTYPE_wW_rW_rW_ARG:
  1963. {
  1964. asBC_SWORDARG0(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG0(&bc[n]));
  1965. asBC_SWORDARG1(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG1(&bc[n]));
  1966. asBC_SWORDARG2(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG2(&bc[n]));
  1967. }
  1968. break;
  1969. default:
  1970. // The other types don't treat variables so won't be modified
  1971. break;
  1972. }
  1973. n += asBCTypeSize[asBCInfo[c].type];
  1974. }
  1975. // Adjust the space needed for local variables
  1976. func->variableSpace = AdjustStackPosition(func->variableSpace);
  1977. // Adjust the variable information. This will be used during the adjustment below
  1978. for( n = 0; n < func->variables.GetLength(); n++ )
  1979. {
  1980. func->variables[n]->declaredAtProgramPos = instructionNbrToPos[func->variables[n]->declaredAtProgramPos];
  1981. func->variables[n]->stackOffset = AdjustStackPosition(func->variables[n]->stackOffset);
  1982. }
  1983. // objVariablePos
  1984. for( n = 0; n < func->objVariablePos.GetLength(); n++ )
  1985. {
  1986. func->objVariablePos[n] = AdjustStackPosition(func->objVariablePos[n]);
  1987. func->funcVariableTypes[n] = FindFunction((int)(asPWORD)func->funcVariableTypes[n]);
  1988. }
  1989. // Adjust the get offsets. This must be done in the second iteration because
  1990. // it relies on the function ids and variable position already being correct in the
  1991. // bytecodes that come after the GET instructions.
  1992. // TODO: optimize: Instead of doing a full extra loop. We can push the GET instructions
  1993. // on a stack, and then when a call instruction is found update all of them.
  1994. // This will also make the AdjustGetOffset() function quicker as it can
  1995. // receive the called function directly instead of having to search for it.
  1996. bc = func->byteCode.AddressOf();
  1997. for( n = 0; n < func->byteCode.GetLength(); )
  1998. {
  1999. int c = *(asBYTE*)&bc[n];
  2000. if( c == asBC_GETREF ||
  2001. c == asBC_GETOBJ ||
  2002. c == asBC_GETOBJREF )
  2003. {
  2004. asBC_WORDARG0(&bc[n]) = (asWORD)AdjustGetOffset(asBC_WORDARG0(&bc[n]), func, n);
  2005. }
  2006. n += asBCTypeSize[asBCInfo[c].type];
  2007. }
  2008. for( n = 0; n < func->objVariableInfo.GetLength(); n++ )
  2009. {
  2010. // The program position must be adjusted as it is stored in number of instructions
  2011. func->objVariableInfo[n].programPos = instructionNbrToPos[func->objVariableInfo[n].programPos];
  2012. func->objVariableInfo[n].variableOffset = AdjustStackPosition(func->objVariableInfo[n].variableOffset);
  2013. }
  2014. // The program position (every even number) needs to be adjusted
  2015. // for the line numbers to be in number of dwords instead of number of instructions
  2016. for( n = 0; n < func->lineNumbers.GetLength(); n += 2 )
  2017. func->lineNumbers[n] = instructionNbrToPos[func->lineNumbers[n]];
  2018. for( n = 0; n < func->sectionIdxs.GetLength(); n += 2 )
  2019. func->sectionIdxs[n] = instructionNbrToPos[func->sectionIdxs[n]];
  2020. CalculateStackNeeded(func);
  2021. }
  2022. void asCReader::CalculateStackNeeded(asCScriptFunction *func)
  2023. {
  2024. int largestStackUsed = 0;
  2025. // Clear the known stack size for each bytecode
  2026. asCArray<int> stackSize;
  2027. stackSize.SetLength(func->byteCode.GetLength());
  2028. memset(&stackSize[0], -1, stackSize.GetLength()*4);
  2029. // Add the first instruction to the list of unchecked code
  2030. // paths and set the stack size at that instruction to variableSpace
  2031. asCArray<asUINT> paths;
  2032. paths.PushLast(0);
  2033. stackSize[0] = func->variableSpace;
  2034. // Go through each of the code paths
  2035. for( asUINT p = 0; p < paths.GetLength(); ++p )
  2036. {
  2037. asUINT pos = paths[p];
  2038. int currStackSize = stackSize[pos];
  2039. asBYTE bc = *(asBYTE*)&func->byteCode[pos];
  2040. if( bc == asBC_RET )
  2041. continue;
  2042. // Determine the change in stack size for this instruction
  2043. int stackInc = asBCInfo[bc].stackInc;
  2044. if( stackInc == 0xFFFF )
  2045. {
  2046. // Determine the true delta from the instruction arguments
  2047. if( bc == asBC_CALL ||
  2048. bc == asBC_CALLSYS ||
  2049. bc == asBC_CALLBND ||
  2050. bc == asBC_ALLOC ||
  2051. bc == asBC_CALLINTF ||
  2052. bc == asBC_CallPtr )
  2053. {
  2054. asCScriptFunction *called = GetCalledFunction(func, pos);
  2055. if( called )
  2056. {
  2057. stackInc = -called->GetSpaceNeededForArguments();
  2058. if( called->objectType )
  2059. stackInc -= AS_PTR_SIZE;
  2060. if( called->DoesReturnOnStack() )
  2061. stackInc -= AS_PTR_SIZE;
  2062. }
  2063. else
  2064. {
  2065. // It is an allocation for an object without a constructor
  2066. asASSERT( bc == asBC_ALLOC );
  2067. stackInc = -AS_PTR_SIZE;
  2068. }
  2069. }
  2070. }
  2071. currStackSize += stackInc;
  2072. asASSERT( currStackSize >= 0 );
  2073. if( currStackSize > largestStackUsed )
  2074. largestStackUsed = currStackSize;
  2075. if( bc == asBC_JMP )
  2076. {
  2077. // Find the label that we should jump to
  2078. int offset = asBC_INTARG(&func->byteCode[pos]);
  2079. pos += 2 + offset;
  2080. // Add the destination as a new path
  2081. if( stackSize[pos] == -1 )
  2082. {
  2083. stackSize[pos] = currStackSize;
  2084. paths.PushLast(pos);
  2085. }
  2086. else
  2087. asASSERT(stackSize[pos] == currStackSize);
  2088. continue;
  2089. }
  2090. else if( bc == asBC_JZ || bc == asBC_JNZ ||
  2091. bc == asBC_JLowZ || bc == asBC_JLowNZ ||
  2092. bc == asBC_JS || bc == asBC_JNS ||
  2093. bc == asBC_JP || bc == asBC_JNP )
  2094. {
  2095. // Find the label that is being jumped to
  2096. int offset = asBC_INTARG(&func->byteCode[pos]);
  2097. // Add both paths to the code paths
  2098. pos += 2;
  2099. if( stackSize[pos] == -1 )
  2100. {
  2101. stackSize[pos] = currStackSize;
  2102. paths.PushLast(pos);
  2103. }
  2104. else
  2105. asASSERT(stackSize[pos] == currStackSize);
  2106. pos += offset;
  2107. if( stackSize[pos] == -1 )
  2108. {
  2109. stackSize[pos] = currStackSize;
  2110. paths.PushLast(pos);
  2111. }
  2112. else
  2113. asASSERT(stackSize[pos] == currStackSize);
  2114. continue;
  2115. }
  2116. else if( bc == asBC_JMPP )
  2117. {
  2118. pos++;
  2119. // Add all subsequent JMP instructions to the path
  2120. while( *(asBYTE*)&func->byteCode[pos] == asBC_JMP )
  2121. {
  2122. if( stackSize[pos] == -1 )
  2123. {
  2124. stackSize[pos] = currStackSize;
  2125. paths.PushLast(pos);
  2126. }
  2127. else
  2128. asASSERT(stackSize[pos] == currStackSize);
  2129. pos += 2;
  2130. }
  2131. continue;
  2132. }
  2133. else
  2134. {
  2135. // Add next instruction to the paths
  2136. pos += asBCTypeSize[asBCInfo[bc].type];
  2137. if( stackSize[pos] == -1 )
  2138. {
  2139. stackSize[pos] = currStackSize;
  2140. paths.PushLast(pos);
  2141. }
  2142. else
  2143. asASSERT(stackSize[pos] == currStackSize);
  2144. continue;
  2145. }
  2146. }
  2147. func->stackNeeded = largestStackUsed;
  2148. }
  2149. void asCReader::CalculateAdjustmentByPos(asCScriptFunction *func)
  2150. {
  2151. // Adjust the offset of all negative variables (parameters) as
  2152. // all pointers have been stored as having a size of 1 dword
  2153. asUINT n;
  2154. asCArray<int> adjustments;
  2155. asUINT offset = 0;
  2156. if( func->objectType )
  2157. {
  2158. adjustments.PushLast(offset);
  2159. adjustments.PushLast(1-AS_PTR_SIZE);
  2160. offset += 1;
  2161. }
  2162. if( func->DoesReturnOnStack() )
  2163. {
  2164. adjustments.PushLast(offset);
  2165. adjustments.PushLast(1-AS_PTR_SIZE);
  2166. offset += 1;
  2167. }
  2168. for( n = 0; n < func->parameterTypes.GetLength(); n++ )
  2169. {
  2170. if( !func->parameterTypes[n].IsPrimitive() ||
  2171. func->parameterTypes[n].IsReference() )
  2172. {
  2173. adjustments.PushLast(offset);
  2174. adjustments.PushLast(1-AS_PTR_SIZE);
  2175. offset += 1;
  2176. }
  2177. else
  2178. {
  2179. asASSERT( func->parameterTypes[n].IsPrimitive() );
  2180. offset += func->parameterTypes[n].GetSizeOnStackDWords();
  2181. }
  2182. }
  2183. // Build look-up table with the adjustments for each stack position
  2184. adjustNegativeStackByPos.SetLength(offset);
  2185. memset(adjustNegativeStackByPos.AddressOf(), 0, adjustNegativeStackByPos.GetLength()*sizeof(int));
  2186. for( n = 0; n < adjustments.GetLength(); n+=2 )
  2187. {
  2188. int pos = adjustments[n];
  2189. int adjust = adjustments[n+1];
  2190. for( asUINT i = pos+1; i < adjustNegativeStackByPos.GetLength(); i++ )
  2191. adjustNegativeStackByPos[i] += adjust;
  2192. }
  2193. // The bytecode has been stored as if all object variables take up only 1 dword.
  2194. // It is necessary to adjust to the size according to the current platform.
  2195. adjustments.SetLength(0);
  2196. int highestPos = 0;
  2197. for( n = 0; n < func->objVariableTypes.GetLength(); n++ )
  2198. {
  2199. if( func->objVariableTypes[n] )
  2200. {
  2201. // Determine the size the variable currently occupies on the stack
  2202. int size = AS_PTR_SIZE;
  2203. if( (func->objVariableTypes[n]->GetFlags() & asOBJ_VALUE) &&
  2204. n >= func->objVariablesOnHeap )
  2205. {
  2206. size = func->objVariableTypes[n]->GetSize();
  2207. if( size < 4 )
  2208. size = 1;
  2209. else
  2210. size /= 4;
  2211. }
  2212. // Check if type has a different size than stored
  2213. if( size > 1 )
  2214. {
  2215. if( func->objVariablePos[n] > highestPos )
  2216. highestPos = func->objVariablePos[n];
  2217. adjustments.PushLast(func->objVariablePos[n]);
  2218. adjustments.PushLast(size-1);
  2219. }
  2220. }
  2221. }
  2222. // Count position 0 too
  2223. adjustByPos.SetLength(highestPos+1);
  2224. memset(adjustByPos.AddressOf(), 0, adjustByPos.GetLength()*sizeof(int));
  2225. // Build look-up table with the adjustments for each stack position
  2226. for( n = 0; n < adjustments.GetLength(); n+=2 )
  2227. {
  2228. int pos = adjustments[n];
  2229. int adjust = adjustments[n+1];
  2230. for( asUINT i = pos; i < adjustByPos.GetLength(); i++ )
  2231. adjustByPos[i] += adjust;
  2232. }
  2233. }
  2234. int asCReader::AdjustStackPosition(int pos)
  2235. {
  2236. if( pos >= (int)adjustByPos.GetLength() )
  2237. {
  2238. // It can be higher for primitives allocated on top of highest object variable
  2239. if( adjustByPos.GetLength() )
  2240. pos += (short)adjustByPos[adjustByPos.GetLength()-1];
  2241. }
  2242. else if( pos >= 0 )
  2243. pos += (short)adjustByPos[pos];
  2244. else if( -pos >= (int)adjustNegativeStackByPos.GetLength() )
  2245. error = true;
  2246. else
  2247. pos += (short)adjustNegativeStackByPos[-pos];
  2248. return pos;
  2249. }
  2250. asCScriptFunction *asCReader::GetCalledFunction(asCScriptFunction *func, asDWORD programPos)
  2251. {
  2252. asBYTE bc = *(asBYTE*)&func->byteCode[programPos];
  2253. if( bc == asBC_CALL ||
  2254. bc == asBC_CALLSYS ||
  2255. bc == asBC_CALLINTF )
  2256. {
  2257. // Find the function from the function id in bytecode
  2258. int funcId = asBC_INTARG(&func->byteCode[programPos]);
  2259. return engine->scriptFunctions[funcId];
  2260. }
  2261. else if( bc == asBC_ALLOC )
  2262. {
  2263. // Find the function from the function id in the bytecode
  2264. int funcId = asBC_INTARG(&func->byteCode[programPos+AS_PTR_SIZE]);
  2265. return engine->scriptFunctions[funcId];
  2266. }
  2267. else if( bc == asBC_CALLBND )
  2268. {
  2269. // Find the function from the engine's bind array
  2270. int funcId = asBC_INTARG(&func->byteCode[programPos]);
  2271. return engine->importedFunctions[funcId&0xFFFF]->importedFunctionSignature;
  2272. }
  2273. else if( bc == asBC_CallPtr )
  2274. {
  2275. asUINT v;
  2276. int var = asBC_SWORDARG0(&func->byteCode[programPos]);
  2277. // Find the funcdef from the local variable
  2278. for( v = 0; v < func->objVariablePos.GetLength(); v++ )
  2279. if( func->objVariablePos[v] == var )
  2280. return func->funcVariableTypes[v];
  2281. // Look in parameters
  2282. int paramPos = 0;
  2283. if( func->objectType )
  2284. paramPos -= AS_PTR_SIZE;
  2285. if( func->DoesReturnOnStack() )
  2286. paramPos -= AS_PTR_SIZE;
  2287. for( v = 0; v < func->parameterTypes.GetLength(); v++ )
  2288. {
  2289. if( var == paramPos )
  2290. return func->parameterTypes[v].GetFuncDef();
  2291. paramPos -= func->parameterTypes[v].GetSizeOnStackDWords();
  2292. }
  2293. }
  2294. return 0;
  2295. }
  2296. int asCReader::AdjustGetOffset(int offset, asCScriptFunction *func, asDWORD programPos)
  2297. {
  2298. // TODO: optimize: multiple instructions for the same function doesn't need to look for the function everytime
  2299. // the function can remember where it found the function and check if the programPos is still valid
  2300. // Get offset 0 doesn't need adjustment
  2301. if( offset == 0 ) return 0;
  2302. // Find out which function that will be called
  2303. asCScriptFunction *calledFunc = 0;
  2304. for( asUINT n = programPos; func->byteCode.GetLength(); )
  2305. {
  2306. asBYTE bc = *(asBYTE*)&func->byteCode[n];
  2307. if( bc == asBC_CALL ||
  2308. bc == asBC_CALLSYS ||
  2309. bc == asBC_CALLINTF ||
  2310. bc == asBC_ALLOC ||
  2311. bc == asBC_CALLBND ||
  2312. bc == asBC_CallPtr )
  2313. {
  2314. calledFunc = GetCalledFunction(func, n);
  2315. break;
  2316. }
  2317. else if( bc == asBC_REFCPY ||
  2318. bc == asBC_COPY )
  2319. {
  2320. // In this case we know there is only 1 pointer on the stack above
  2321. asASSERT( offset == 1 );
  2322. return offset - (1 - AS_PTR_SIZE);
  2323. }
  2324. n += asBCTypeSize[asBCInfo[bc].type];
  2325. }
  2326. if( calledFunc == 0 )
  2327. {
  2328. // TODO: Report error
  2329. error = true;
  2330. return offset;
  2331. }
  2332. // Count the number of pointers pushed on the stack above the
  2333. // current offset, and then adjust the offset accordingly
  2334. asUINT numPtrs = 0;
  2335. int currOffset = 0;
  2336. if( offset > currOffset && calledFunc->GetObjectType() )
  2337. {
  2338. numPtrs++;
  2339. currOffset++;
  2340. }
  2341. if( offset > currOffset && calledFunc->DoesReturnOnStack() )
  2342. {
  2343. numPtrs++;
  2344. currOffset++;
  2345. }
  2346. for( asUINT p = 0; p < calledFunc->parameterTypes.GetLength(); p++ )
  2347. {
  2348. if( offset <= currOffset ) break;
  2349. if( !calledFunc->parameterTypes[p].IsPrimitive() ||
  2350. calledFunc->parameterTypes[p].IsReference() )
  2351. {
  2352. numPtrs++;
  2353. currOffset++;
  2354. // The variable arg ? has an additiona 32bit integer with the typeid
  2355. if( calledFunc->parameterTypes[p].IsAnyType() )
  2356. currOffset += 1;
  2357. }
  2358. else
  2359. {
  2360. // Enums or built-in primitives are passed by value
  2361. asASSERT( calledFunc->parameterTypes[p].IsPrimitive() );
  2362. currOffset += calledFunc->parameterTypes[p].GetSizeOnStackDWords();
  2363. }
  2364. }
  2365. return offset - numPtrs * (1 - AS_PTR_SIZE);
  2366. }
  2367. int asCReader::FindTypeId(int idx)
  2368. {
  2369. if( idx >= 0 && idx < (int)usedTypeIds.GetLength() )
  2370. return usedTypeIds[idx];
  2371. else
  2372. {
  2373. // TODO: Write to message callback
  2374. error = true;
  2375. return 0;
  2376. }
  2377. }
  2378. asCObjectType *asCReader::FindObjectType(int idx)
  2379. {
  2380. if( idx < 0 || idx >= (int)usedTypes.GetLength() )
  2381. {
  2382. // TODO: Write to message callback
  2383. error = true;
  2384. return 0;
  2385. }
  2386. return usedTypes[idx];
  2387. }
  2388. #ifndef AS_NO_COMPILER
  2389. asCWriter::asCWriter(asCModule* _module, asIBinaryStream* _stream, asCScriptEngine* _engine, bool _stripDebug)
  2390. : module(_module), stream(_stream), engine(_engine), stripDebugInfo(_stripDebug)
  2391. {
  2392. }
  2393. void asCWriter::WriteData(const void *data, asUINT size)
  2394. {
  2395. asASSERT(size == 1 || size == 2 || size == 4 || size == 8);
  2396. #if defined(AS_BIG_ENDIAN)
  2397. for( asUINT n = 0; n < size; n++ )
  2398. stream->Write(((asBYTE*)data)+n, 1);
  2399. #else
  2400. for( int n = size-1; n >= 0; n-- )
  2401. stream->Write(((asBYTE*)data)+n, 1);
  2402. #endif
  2403. }
  2404. int asCWriter::Write()
  2405. {
  2406. unsigned long i, count;
  2407. // Store everything in the same order that the builder parses scripts
  2408. // TODO: Should be possible to skip saving the enum values. They are usually not needed after the script is compiled anyway
  2409. // TODO: Should be possible to skip saving the typedefs. They are usually not needed after the script is compiled anyway
  2410. // TODO: Should be possible to skip saving constants. They are usually not needed after the script is compiled anyway
  2411. WriteData(&stripDebugInfo, sizeof(stripDebugInfo));
  2412. // Store enums
  2413. count = (asUINT)module->enumTypes.GetLength();
  2414. WriteEncodedInt64(count);
  2415. for( i = 0; i < count; i++ )
  2416. {
  2417. WriteObjectTypeDeclaration(module->enumTypes[i], 1);
  2418. WriteObjectTypeDeclaration(module->enumTypes[i], 2);
  2419. }
  2420. // Store type declarations first
  2421. count = (asUINT)module->classTypes.GetLength();
  2422. WriteEncodedInt64(count);
  2423. for( i = 0; i < count; i++ )
  2424. {
  2425. // Store only the name of the class/interface types
  2426. WriteObjectTypeDeclaration(module->classTypes[i], 1);
  2427. }
  2428. // Store func defs
  2429. count = (asUINT)module->funcDefs.GetLength();
  2430. WriteEncodedInt64(count);
  2431. for( i = 0; i < count; i++ )
  2432. WriteFunction(module->funcDefs[i]);
  2433. // Now store all interface methods
  2434. count = (asUINT)module->classTypes.GetLength();
  2435. for( i = 0; i < count; i++ )
  2436. {
  2437. if( module->classTypes[i]->IsInterface() )
  2438. WriteObjectTypeDeclaration(module->classTypes[i], 2);
  2439. }
  2440. // Then store the class methods and behaviours
  2441. for( i = 0; i < count; ++i )
  2442. {
  2443. if( !module->classTypes[i]->IsInterface() )
  2444. WriteObjectTypeDeclaration(module->classTypes[i], 2);
  2445. }
  2446. // Then store the class properties
  2447. for( i = 0; i < count; ++i )
  2448. {
  2449. if( !module->classTypes[i]->IsInterface() )
  2450. WriteObjectTypeDeclaration(module->classTypes[i], 3);
  2451. }
  2452. // Store typedefs
  2453. count = (asUINT)module->typeDefs.GetLength();
  2454. WriteEncodedInt64(count);
  2455. for( i = 0; i < count; i++ )
  2456. {
  2457. WriteObjectTypeDeclaration(module->typeDefs[i], 1);
  2458. WriteObjectTypeDeclaration(module->typeDefs[i], 2);
  2459. }
  2460. // scriptGlobals[]
  2461. count = (asUINT)module->scriptGlobals.GetSize();
  2462. WriteEncodedInt64(count);
  2463. asCSymbolTable<asCGlobalProperty>::iterator it = module->scriptGlobals.List();
  2464. for( ; it; it++ )
  2465. WriteGlobalProperty(*it);
  2466. // scriptFunctions[]
  2467. count = 0;
  2468. for( i = 0; i < module->scriptFunctions.GetLength(); i++ )
  2469. if( module->scriptFunctions[i]->objectType == 0 )
  2470. count++;
  2471. WriteEncodedInt64(count);
  2472. for( i = 0; i < module->scriptFunctions.GetLength(); ++i )
  2473. if( module->scriptFunctions[i]->objectType == 0 )
  2474. WriteFunction(module->scriptFunctions[i]);
  2475. // globalFunctions[]
  2476. count = (int)module->globalFunctions.GetSize();
  2477. asCSymbolTable<asCScriptFunction>::iterator funcIt = module->globalFunctions.List();
  2478. WriteEncodedInt64(count);
  2479. while( funcIt )
  2480. {
  2481. WriteFunction(*funcIt);
  2482. funcIt++;
  2483. }
  2484. // bindInformations[]
  2485. count = (asUINT)module->bindInformations.GetLength();
  2486. WriteEncodedInt64(count);
  2487. for( i = 0; i < count; ++i )
  2488. {
  2489. WriteFunction(module->bindInformations[i]->importedFunctionSignature);
  2490. WriteString(&module->bindInformations[i]->importFromModule);
  2491. }
  2492. // usedTypes[]
  2493. count = (asUINT)usedTypes.GetLength();
  2494. WriteEncodedInt64(count);
  2495. for( i = 0; i < count; ++i )
  2496. WriteObjectType(usedTypes[i]);
  2497. // usedTypeIds[]
  2498. WriteUsedTypeIds();
  2499. // usedFunctions[]
  2500. WriteUsedFunctions();
  2501. // usedGlobalProperties[]
  2502. WriteUsedGlobalProps();
  2503. // usedStringConstants[]
  2504. WriteUsedStringConstants();
  2505. // usedObjectProperties[]
  2506. WriteUsedObjectProps();
  2507. return asSUCCESS;
  2508. }
  2509. int asCWriter::FindStringConstantIndex(int id)
  2510. {
  2511. asSMapNode<int,int> *cursor = 0;
  2512. if (stringIdToIndexMap.MoveTo(&cursor, id))
  2513. return cursor->value;
  2514. usedStringConstants.PushLast(id);
  2515. int index = int(usedStringConstants.GetLength() - 1);
  2516. stringIdToIndexMap.Insert(id, index);
  2517. return index;
  2518. }
  2519. void asCWriter::WriteUsedStringConstants()
  2520. {
  2521. asUINT count = (asUINT)usedStringConstants.GetLength();
  2522. WriteEncodedInt64(count);
  2523. for( asUINT i = 0; i < count; ++i )
  2524. WriteString(engine->stringConstants[usedStringConstants[i]]);
  2525. }
  2526. void asCWriter::WriteUsedFunctions()
  2527. {
  2528. asUINT count = (asUINT)usedFunctions.GetLength();
  2529. WriteEncodedInt64(count);
  2530. for( asUINT n = 0; n < usedFunctions.GetLength(); n++ )
  2531. {
  2532. char c;
  2533. // Write enough data to be able to uniquely identify the function upon load
  2534. if( usedFunctions[n] )
  2535. {
  2536. // Is the function from the module or the application?
  2537. c = usedFunctions[n]->module ? 'm' : 'a';
  2538. WriteData(&c, 1);
  2539. WriteFunctionSignature(usedFunctions[n]);
  2540. }
  2541. else
  2542. {
  2543. // null function pointer
  2544. c = 'n';
  2545. WriteData(&c, 1);
  2546. }
  2547. }
  2548. }
  2549. void asCWriter::WriteFunctionSignature(asCScriptFunction *func)
  2550. {
  2551. asUINT i, count;
  2552. WriteString(&func->name);
  2553. WriteDataType(&func->returnType);
  2554. count = (asUINT)func->parameterTypes.GetLength();
  2555. WriteEncodedInt64(count);
  2556. for( i = 0; i < count; ++i )
  2557. WriteDataType(&func->parameterTypes[i]);
  2558. count = (asUINT)func->inOutFlags.GetLength();
  2559. WriteEncodedInt64(count);
  2560. for( i = 0; i < count; ++i )
  2561. WriteEncodedInt64(func->inOutFlags[i]);
  2562. WriteEncodedInt64(func->funcType);
  2563. // Write the default args, from last to first
  2564. count = 0;
  2565. for( i = (asUINT)func->defaultArgs.GetLength(); i-- > 0; )
  2566. if( func->defaultArgs[i] )
  2567. count++;
  2568. WriteEncodedInt64(count);
  2569. for( i = (asUINT)func->defaultArgs.GetLength(); i-- > 0; )
  2570. if( func->defaultArgs[i] )
  2571. WriteString(func->defaultArgs[i]);
  2572. WriteObjectType(func->objectType);
  2573. if( func->objectType )
  2574. {
  2575. asBYTE b = 0;
  2576. b += func->isReadOnly ? 1 : 0;
  2577. b += func->isPrivate ? 2 : 0;
  2578. WriteData(&b, 1);
  2579. }
  2580. else
  2581. {
  2582. WriteString(&func->nameSpace->name);
  2583. }
  2584. }
  2585. void asCWriter::WriteFunction(asCScriptFunction* func)
  2586. {
  2587. char c;
  2588. // If there is no function, then store a null char
  2589. if( func == 0 )
  2590. {
  2591. c = '\0';
  2592. WriteData(&c, 1);
  2593. return;
  2594. }
  2595. // First check if the function has been saved already
  2596. for( asUINT f = 0; f < savedFunctions.GetLength(); f++ )
  2597. {
  2598. if( savedFunctions[f] == func )
  2599. {
  2600. c = 'r';
  2601. WriteData(&c, 1);
  2602. WriteEncodedInt64(f);
  2603. return;
  2604. }
  2605. }
  2606. // Keep a reference to the function in the list
  2607. savedFunctions.PushLast(func);
  2608. c = 'f';
  2609. WriteData(&c, 1);
  2610. asUINT i, count;
  2611. WriteFunctionSignature(func);
  2612. if( func->funcType == asFUNC_SCRIPT )
  2613. {
  2614. // Calculate the adjustment by position lookup table
  2615. CalculateAdjustmentByPos(func);
  2616. WriteByteCode(func);
  2617. asDWORD varSpace = AdjustStackPosition(func->variableSpace);
  2618. WriteEncodedInt64(varSpace);
  2619. count = (asUINT)func->objVariablePos.GetLength();
  2620. WriteEncodedInt64(count);
  2621. for( i = 0; i < count; ++i )
  2622. {
  2623. WriteObjectType(func->objVariableTypes[i]);
  2624. // TODO: Only write this if the object type is the builtin function type
  2625. WriteEncodedInt64(FindFunctionIndex(func->funcVariableTypes[i]));
  2626. WriteEncodedInt64(AdjustStackPosition(func->objVariablePos[i]));
  2627. }
  2628. if( count > 0 )
  2629. WriteEncodedInt64(func->objVariablesOnHeap);
  2630. WriteEncodedInt64((asUINT)func->objVariableInfo.GetLength());
  2631. for( i = 0; i < func->objVariableInfo.GetLength(); ++i )
  2632. {
  2633. // The program position must be adjusted to be in number of instructions
  2634. WriteEncodedInt64(bytecodeNbrByPos[func->objVariableInfo[i].programPos]);
  2635. WriteEncodedInt64(AdjustStackPosition(func->objVariableInfo[i].variableOffset));
  2636. WriteEncodedInt64(func->objVariableInfo[i].option);
  2637. }
  2638. // The program position (every even number) needs to be adjusted
  2639. // to be in number of instructions instead of DWORD offset
  2640. if( !stripDebugInfo )
  2641. {
  2642. asUINT length = (asUINT)func->lineNumbers.GetLength();
  2643. WriteEncodedInt64(length);
  2644. for( i = 0; i < length; ++i )
  2645. {
  2646. if( (i & 1) == 0 )
  2647. WriteEncodedInt64(bytecodeNbrByPos[func->lineNumbers[i]]);
  2648. else
  2649. WriteEncodedInt64(func->lineNumbers[i]);
  2650. }
  2651. // Write the array of script sections
  2652. length = (asUINT)func->sectionIdxs.GetLength();
  2653. WriteEncodedInt64(length);
  2654. for( i = 0; i < length; ++i )
  2655. {
  2656. if( (i & 1) == 0 )
  2657. WriteEncodedInt64(bytecodeNbrByPos[func->sectionIdxs[i]]);
  2658. else
  2659. WriteEncodedInt64(func->sectionIdxs[i]);
  2660. }
  2661. }
  2662. WriteData(&func->isShared, 1);
  2663. // Write the variable information
  2664. if( !stripDebugInfo )
  2665. {
  2666. WriteEncodedInt64((asUINT)func->variables.GetLength());
  2667. for( i = 0; i < func->variables.GetLength(); i++ )
  2668. {
  2669. // The program position must be adjusted to be in number of instructions
  2670. WriteEncodedInt64(bytecodeNbrByPos[func->variables[i]->declaredAtProgramPos]);
  2671. // The stack position must be adjusted according to the pointer sizes
  2672. WriteEncodedInt64(AdjustStackPosition(func->variables[i]->stackOffset));
  2673. WriteString(&func->variables[i]->name);
  2674. WriteDataType(&func->variables[i]->type);
  2675. }
  2676. }
  2677. WriteData(&func->dontCleanUpOnException, 1);
  2678. }
  2679. else if( func->funcType == asFUNC_VIRTUAL )
  2680. {
  2681. WriteEncodedInt64(func->vfTableIdx);
  2682. }
  2683. // Store script section name
  2684. if( !stripDebugInfo )
  2685. {
  2686. if( func->scriptSectionIdx >= 0 )
  2687. WriteString(engine->scriptSectionNames[func->scriptSectionIdx]);
  2688. else
  2689. {
  2690. char c = 0;
  2691. WriteData(&c, 1);
  2692. }
  2693. }
  2694. }
  2695. void asCWriter::WriteObjectTypeDeclaration(asCObjectType *ot, int phase)
  2696. {
  2697. if( phase == 1 )
  2698. {
  2699. // name
  2700. WriteString(&ot->name);
  2701. // flags
  2702. WriteData(&ot->flags, 4);
  2703. // size
  2704. if( (ot->flags & asOBJ_SCRIPT_OBJECT) && ot->size > 0 )
  2705. {
  2706. // The size for script objects may vary from platform to platform so
  2707. // only store 1 to diferentiate from interfaces that have size 0.
  2708. WriteEncodedInt64(1);
  2709. }
  2710. else
  2711. {
  2712. // Enums, typedefs, and interfaces have fixed sizes independently
  2713. // of platform so it is safe to serialize the size directly.
  2714. WriteEncodedInt64(ot->size);
  2715. }
  2716. // namespace
  2717. WriteString(&ot->nameSpace->name);
  2718. }
  2719. else if( phase == 2 )
  2720. {
  2721. if( ot->flags & asOBJ_ENUM )
  2722. {
  2723. // enumValues[]
  2724. int size = (int)ot->enumValues.GetLength();
  2725. WriteEncodedInt64(size);
  2726. for( int n = 0; n < size; n++ )
  2727. {
  2728. WriteString(&ot->enumValues[n]->name);
  2729. WriteData(&ot->enumValues[n]->value, 4);
  2730. }
  2731. }
  2732. else if( ot->flags & asOBJ_TYPEDEF )
  2733. {
  2734. eTokenType t = ot->templateSubTypes[0].GetTokenType();
  2735. WriteEncodedInt64(t);
  2736. }
  2737. else
  2738. {
  2739. WriteObjectType(ot->derivedFrom);
  2740. // interfaces[]
  2741. int size = (asUINT)ot->interfaces.GetLength();
  2742. WriteEncodedInt64(size);
  2743. asUINT n;
  2744. for( n = 0; n < ot->interfaces.GetLength(); n++ )
  2745. {
  2746. WriteObjectType(ot->interfaces[n]);
  2747. }
  2748. // behaviours
  2749. if( !ot->IsInterface() && ot->flags != asOBJ_TYPEDEF && ot->flags != asOBJ_ENUM )
  2750. {
  2751. WriteFunction(engine->scriptFunctions[ot->beh.destruct]);
  2752. size = (int)ot->beh.constructors.GetLength();
  2753. WriteEncodedInt64(size);
  2754. for( n = 0; n < ot->beh.constructors.GetLength(); n++ )
  2755. {
  2756. WriteFunction(engine->scriptFunctions[ot->beh.constructors[n]]);
  2757. WriteFunction(engine->scriptFunctions[ot->beh.factories[n]]);
  2758. }
  2759. }
  2760. // methods[]
  2761. size = (int)ot->methods.GetLength();
  2762. WriteEncodedInt64(size);
  2763. for( n = 0; n < ot->methods.GetLength(); n++ )
  2764. {
  2765. WriteFunction(engine->scriptFunctions[ot->methods[n]]);
  2766. }
  2767. // virtualFunctionTable[]
  2768. size = (int)ot->virtualFunctionTable.GetLength();
  2769. WriteEncodedInt64(size);
  2770. for( n = 0; n < (asUINT)size; n++ )
  2771. {
  2772. WriteFunction(ot->virtualFunctionTable[n]);
  2773. }
  2774. }
  2775. }
  2776. else if( phase == 3 )
  2777. {
  2778. // properties[]
  2779. asUINT size = (asUINT)ot->properties.GetLength();
  2780. WriteEncodedInt64(size);
  2781. for( asUINT n = 0; n < ot->properties.GetLength(); n++ )
  2782. {
  2783. WriteObjectProperty(ot->properties[n]);
  2784. }
  2785. }
  2786. }
  2787. void asCWriter::WriteEncodedInt64(asINT64 i)
  2788. {
  2789. asBYTE signBit = ( i & asINT64(1)<<63 ) ? 0x80 : 0;
  2790. if( signBit ) i = -i;
  2791. asBYTE b;
  2792. if( i < (1<<6) )
  2793. {
  2794. b = (asBYTE)(signBit + i); WriteData(&b, 1);
  2795. }
  2796. else if( i < (1<<13) )
  2797. {
  2798. b = asBYTE(0x40 + signBit + (i >> 8)); WriteData(&b, 1);
  2799. b = asBYTE(i & 0xFF); WriteData(&b, 1);
  2800. }
  2801. else if( i < (1<<20) )
  2802. {
  2803. b = asBYTE(0x60 + signBit + (i >> 16)); WriteData(&b, 1);
  2804. b = asBYTE((i >> 8) & 0xFF); WriteData(&b, 1);
  2805. b = asBYTE(i & 0xFF); WriteData(&b, 1);
  2806. }
  2807. else if( i < (1<<27) )
  2808. {
  2809. b = asBYTE(0x70 + signBit + (i >> 24)); WriteData(&b, 1);
  2810. b = asBYTE((i >> 16) & 0xFF); WriteData(&b, 1);
  2811. b = asBYTE((i >> 8) & 0xFF); WriteData(&b, 1);
  2812. b = asBYTE(i & 0xFF); WriteData(&b, 1);
  2813. }
  2814. else if( i < (asINT64(1)<<34) )
  2815. {
  2816. b = asBYTE(0x78 + signBit + (i >> 32)); WriteData(&b, 1);
  2817. b = asBYTE((i >> 24) & 0xFF); WriteData(&b, 1);
  2818. b = asBYTE((i >> 16) & 0xFF); WriteData(&b, 1);
  2819. b = asBYTE((i >> 8) & 0xFF); WriteData(&b, 1);
  2820. b = asBYTE(i & 0xFF); WriteData(&b, 1);
  2821. }
  2822. else if( i < (asINT64(1)<<41) )
  2823. {
  2824. b = asBYTE(0x7C + signBit + (i >> 40)); WriteData(&b, 1);
  2825. b = asBYTE((i >> 32) & 0xFF); WriteData(&b, 1);
  2826. b = asBYTE((i >> 24) & 0xFF); WriteData(&b, 1);
  2827. b = asBYTE((i >> 16) & 0xFF); WriteData(&b, 1);
  2828. b = asBYTE((i >> 8) & 0xFF); WriteData(&b, 1);
  2829. b = asBYTE(i & 0xFF); WriteData(&b, 1);
  2830. }
  2831. else if( i < (asINT64(1)<<48) )
  2832. {
  2833. b = asBYTE(0x7E + signBit + (i >> 48)); WriteData(&b, 1);
  2834. b = asBYTE((i >> 40) & 0xFF); WriteData(&b, 1);
  2835. b = asBYTE((i >> 32) & 0xFF); WriteData(&b, 1);
  2836. b = asBYTE((i >> 24) & 0xFF); WriteData(&b, 1);
  2837. b = asBYTE((i >> 16) & 0xFF); WriteData(&b, 1);
  2838. b = asBYTE((i >> 8) & 0xFF); WriteData(&b, 1);
  2839. b = asBYTE(i & 0xFF); WriteData(&b, 1);
  2840. }
  2841. else
  2842. {
  2843. b = asBYTE(0x7F + signBit); WriteData(&b, 1);
  2844. b = asBYTE((i >> 56) & 0xFF); WriteData(&b, 1);
  2845. b = asBYTE((i >> 48) & 0xFF); WriteData(&b, 1);
  2846. b = asBYTE((i >> 40) & 0xFF); WriteData(&b, 1);
  2847. b = asBYTE((i >> 32) & 0xFF); WriteData(&b, 1);
  2848. b = asBYTE((i >> 24) & 0xFF); WriteData(&b, 1);
  2849. b = asBYTE((i >> 16) & 0xFF); WriteData(&b, 1);
  2850. b = asBYTE((i >> 8) & 0xFF); WriteData(&b, 1);
  2851. b = asBYTE(i & 0xFF); WriteData(&b, 1);
  2852. }
  2853. }
  2854. void asCWriter::WriteString(asCString* str)
  2855. {
  2856. // TODO: All strings should be stored in a separate section, and when
  2857. // they are used an offset into that section should be stored.
  2858. // This will make it unnecessary to store the extra byte to
  2859. // identify new versus old strings.
  2860. if( str->GetLength() == 0 )
  2861. {
  2862. char z = '\0';
  2863. WriteData(&z, 1);
  2864. return;
  2865. }
  2866. // First check if the string hasn't been saved already
  2867. asSMapNode<asCStringPointer, int> *cursor = 0;
  2868. if (stringToIdMap.MoveTo(&cursor, asCStringPointer(str)))
  2869. {
  2870. // Save a reference to the existing string
  2871. char b = 'r';
  2872. WriteData(&b, 1);
  2873. WriteEncodedInt64(cursor->value);
  2874. return;
  2875. }
  2876. // Save a new string
  2877. char b = 'n';
  2878. WriteData(&b, 1);
  2879. asUINT len = (asUINT)str->GetLength();
  2880. WriteEncodedInt64(len);
  2881. stream->Write(str->AddressOf(), (asUINT)len);
  2882. savedStrings.PushLast(*str);
  2883. stringToIdMap.Insert(asCStringPointer(str), int(savedStrings.GetLength()) - 1);
  2884. }
  2885. void asCWriter::WriteGlobalProperty(asCGlobalProperty* prop)
  2886. {
  2887. // TODO: We might be able to avoid storing the name and type of the global
  2888. // properties twice if we merge this with the WriteUsedGlobalProperties.
  2889. WriteString(&prop->name);
  2890. WriteString(&prop->nameSpace->name);
  2891. WriteDataType(&prop->type);
  2892. // Store the initialization function
  2893. if( prop->GetInitFunc() )
  2894. {
  2895. bool f = true;
  2896. WriteData(&f, 1);
  2897. WriteFunction(prop->GetInitFunc());
  2898. }
  2899. else
  2900. {
  2901. bool f = false;
  2902. WriteData(&f, 1);
  2903. }
  2904. }
  2905. void asCWriter::WriteObjectProperty(asCObjectProperty* prop)
  2906. {
  2907. WriteString(&prop->name);
  2908. WriteDataType(&prop->type);
  2909. WriteData(&prop->isPrivate, 1);
  2910. }
  2911. void asCWriter::WriteDataType(const asCDataType *dt)
  2912. {
  2913. // First check if the datatype has already been saved
  2914. for( asUINT n = 0; n < savedDataTypes.GetLength(); n++ )
  2915. {
  2916. if( *dt == savedDataTypes[n] )
  2917. {
  2918. asUINT c = 0;
  2919. WriteEncodedInt64(c);
  2920. WriteEncodedInt64(n);
  2921. return;
  2922. }
  2923. }
  2924. // Save the new datatype
  2925. savedDataTypes.PushLast(*dt);
  2926. bool b;
  2927. int t = dt->GetTokenType();
  2928. WriteEncodedInt64(t);
  2929. if( t == ttIdentifier )
  2930. {
  2931. WriteObjectType(dt->GetObjectType());
  2932. b = dt->IsObjectHandle();
  2933. WriteData(&b, 1);
  2934. b = dt->IsHandleToConst();
  2935. WriteData(&b, 1);
  2936. }
  2937. b = dt->IsReference();
  2938. WriteData(&b, 1);
  2939. b = dt->IsReadOnly();
  2940. WriteData(&b, 1);
  2941. if( t == ttIdentifier && dt->GetObjectType()->name == "_builtin_function_" )
  2942. {
  2943. WriteFunctionSignature(dt->GetFuncDef());
  2944. }
  2945. }
  2946. void asCWriter::WriteObjectType(asCObjectType* ot)
  2947. {
  2948. char ch;
  2949. if( ot )
  2950. {
  2951. // Check for template instances/specializations
  2952. if( ot->templateSubTypes.GetLength() )
  2953. {
  2954. ch = 'a';
  2955. WriteData(&ch, 1);
  2956. WriteString(&ot->name);
  2957. WriteEncodedInt64(ot->templateSubTypes.GetLength());
  2958. for( asUINT n = 0; n < ot->templateSubTypes.GetLength(); n++ )
  2959. {
  2960. if( ot->templateSubTypes[0].IsObject() || ot->templateSubTypes[0].IsEnumType() )
  2961. {
  2962. ch = 's';
  2963. WriteData(&ch, 1);
  2964. WriteDataType(&ot->templateSubTypes[0]);
  2965. }
  2966. else
  2967. {
  2968. ch = 't';
  2969. WriteData(&ch, 1);
  2970. eTokenType t = ot->templateSubTypes[0].GetTokenType();
  2971. WriteEncodedInt64(t);
  2972. }
  2973. }
  2974. }
  2975. else if( ot->flags & asOBJ_TEMPLATE_SUBTYPE )
  2976. {
  2977. ch = 's';
  2978. WriteData(&ch, 1);
  2979. WriteString(&ot->name);
  2980. }
  2981. else
  2982. {
  2983. ch = 'o';
  2984. WriteData(&ch, 1);
  2985. WriteString(&ot->name);
  2986. WriteString(&ot->nameSpace->name);
  2987. }
  2988. }
  2989. else
  2990. {
  2991. ch = '\0';
  2992. WriteData(&ch, 1);
  2993. }
  2994. }
  2995. void asCWriter::CalculateAdjustmentByPos(asCScriptFunction *func)
  2996. {
  2997. // Adjust the offset of all negative variables (parameters) so all pointers will have a size of 1 dword
  2998. asUINT n;
  2999. asCArray<int> adjustments;
  3000. asUINT offset = 0;
  3001. if( func->objectType )
  3002. {
  3003. adjustments.PushLast(offset);
  3004. adjustments.PushLast(1-AS_PTR_SIZE);
  3005. offset += AS_PTR_SIZE;
  3006. }
  3007. if( func->DoesReturnOnStack() )
  3008. {
  3009. adjustments.PushLast(offset);
  3010. adjustments.PushLast(1-AS_PTR_SIZE);
  3011. offset += AS_PTR_SIZE;
  3012. }
  3013. for( n = 0; n < func->parameterTypes.GetLength(); n++ )
  3014. {
  3015. if( !func->parameterTypes[n].IsPrimitive() ||
  3016. func->parameterTypes[n].IsReference() )
  3017. {
  3018. adjustments.PushLast(offset);
  3019. adjustments.PushLast(1-AS_PTR_SIZE);
  3020. offset += AS_PTR_SIZE;
  3021. }
  3022. else
  3023. {
  3024. asASSERT( func->parameterTypes[n].IsPrimitive() );
  3025. offset += func->parameterTypes[n].GetSizeOnStackDWords();
  3026. }
  3027. }
  3028. // Build look-up table with the adjustments for each stack position
  3029. adjustNegativeStackByPos.SetLength(offset);
  3030. memset(adjustNegativeStackByPos.AddressOf(), 0, adjustNegativeStackByPos.GetLength()*sizeof(int));
  3031. for( n = 0; n < adjustments.GetLength(); n+=2 )
  3032. {
  3033. int pos = adjustments[n];
  3034. int adjust = adjustments[n+1];
  3035. for( asUINT i = pos+1; i < adjustNegativeStackByPos.GetLength(); i++ )
  3036. adjustNegativeStackByPos[i] += adjust;
  3037. }
  3038. // Adjust the offset of all positive variables so that all object types and handles have a size of 1 dword
  3039. // This is similar to how the adjustment is done in the asCReader::TranslateFunction, only the reverse
  3040. adjustments.SetLength(0);
  3041. for( n = 0; n < func->objVariableTypes.GetLength(); n++ )
  3042. {
  3043. if( func->objVariableTypes[n] )
  3044. {
  3045. // Determine the size the variable currently occupies on the stack
  3046. int size = AS_PTR_SIZE;
  3047. if( (func->objVariableTypes[n]->GetFlags() & asOBJ_VALUE) &&
  3048. n >= func->objVariablesOnHeap )
  3049. {
  3050. size = func->objVariableTypes[n]->GetSize();
  3051. if( size < 4 )
  3052. size = 1;
  3053. else
  3054. size /= 4;
  3055. }
  3056. // If larger than 1 dword, adjust the offsets accordingly
  3057. if( size > 1 )
  3058. {
  3059. // How much needs to be adjusted?
  3060. adjustments.PushLast(func->objVariablePos[n]);
  3061. adjustments.PushLast(-(size-1));
  3062. }
  3063. }
  3064. }
  3065. // Build look-up table with the adjustments for each stack position
  3066. adjustStackByPos.SetLength(func->stackNeeded);
  3067. memset(adjustStackByPos.AddressOf(), 0, adjustStackByPos.GetLength()*sizeof(int));
  3068. for( n = 0; n < adjustments.GetLength(); n+=2 )
  3069. {
  3070. int pos = adjustments[n];
  3071. int adjust = adjustments[n+1];
  3072. for( asUINT i = pos; i < adjustStackByPos.GetLength(); i++ )
  3073. adjustStackByPos[i] += adjust;
  3074. }
  3075. // Compute the sequence number of each bytecode instruction in order to update the jump offsets
  3076. size_t length = func->byteCode.GetLength();
  3077. asDWORD *bc = func->byteCode.AddressOf();
  3078. bytecodeNbrByPos.SetLength(length);
  3079. asUINT num;
  3080. for( offset = 0, num = 0; offset < length; )
  3081. {
  3082. bytecodeNbrByPos[offset] = num;
  3083. offset += asBCTypeSize[asBCInfo[*(asBYTE*)(bc+offset)].type];
  3084. num++;
  3085. }
  3086. // The last instruction is always a BC_RET. This make it possible to query
  3087. // the number of instructions by checking the last entry in bytecodeNbrByPos
  3088. asASSERT(*(asBYTE*)(bc+length-1) == asBC_RET);
  3089. }
  3090. int asCWriter::AdjustStackPosition(int pos)
  3091. {
  3092. if( pos >= (int)adjustStackByPos.GetLength() )
  3093. {
  3094. // This happens for example if the function only have temporary variables
  3095. // The adjustByPos can also be empty if the function doesn't have any variables at all, but receive a handle by parameter
  3096. if( adjustStackByPos.GetLength() > 0 )
  3097. pos += adjustStackByPos[adjustStackByPos.GetLength()-1];
  3098. }
  3099. else if( pos >= 0 )
  3100. pos += adjustStackByPos[pos];
  3101. else
  3102. {
  3103. asASSERT( -pos < (int)adjustNegativeStackByPos.GetLength() );
  3104. pos -= (short)adjustNegativeStackByPos[-pos];
  3105. }
  3106. return pos;
  3107. }
  3108. int asCWriter::AdjustGetOffset(int offset, asCScriptFunction *func, asDWORD programPos)
  3109. {
  3110. // TODO: optimize: multiple instructions for the same function doesn't need to look for the function everytime
  3111. // the function can remember where it found the function and check if the programPos is still valid
  3112. // Get offset 0 doesn't need adjustment
  3113. if( offset == 0 ) return 0;
  3114. // Find out which function that will be called
  3115. asCScriptFunction *calledFunc = 0;
  3116. for( asUINT n = programPos; n < func->byteCode.GetLength(); )
  3117. {
  3118. asBYTE bc = *(asBYTE*)&func->byteCode[n];
  3119. if( bc == asBC_CALL ||
  3120. bc == asBC_CALLSYS ||
  3121. bc == asBC_CALLINTF )
  3122. {
  3123. // Find the function from the function id in bytecode
  3124. int funcId = asBC_INTARG(&func->byteCode[n]);
  3125. calledFunc = engine->scriptFunctions[funcId];
  3126. break;
  3127. }
  3128. else if( bc == asBC_ALLOC )
  3129. {
  3130. // Find the function from the function id in the bytecode
  3131. int funcId = asBC_INTARG(&func->byteCode[n+AS_PTR_SIZE]);
  3132. calledFunc = engine->scriptFunctions[funcId];
  3133. break;
  3134. }
  3135. else if( bc == asBC_CALLBND )
  3136. {
  3137. // Find the function from the engine's bind array
  3138. int funcId = asBC_INTARG(&func->byteCode[n]);
  3139. calledFunc = engine->importedFunctions[funcId&0xFFFF]->importedFunctionSignature;
  3140. break;
  3141. }
  3142. else if( bc == asBC_CallPtr )
  3143. {
  3144. int var = asBC_SWORDARG0(&func->byteCode[n]);
  3145. asUINT v;
  3146. // Find the funcdef from the local variable
  3147. for( v = 0; v < func->objVariablePos.GetLength(); v++ )
  3148. {
  3149. if( func->objVariablePos[v] == var )
  3150. {
  3151. calledFunc = func->funcVariableTypes[v];
  3152. break;
  3153. }
  3154. }
  3155. if( !calledFunc )
  3156. {
  3157. // Look in parameters
  3158. int paramPos = 0;
  3159. if( func->objectType )
  3160. paramPos -= AS_PTR_SIZE;
  3161. if( func->DoesReturnOnStack() )
  3162. paramPos -= AS_PTR_SIZE;
  3163. for( v = 0; v < func->parameterTypes.GetLength(); v++ )
  3164. {
  3165. if( var == paramPos )
  3166. {
  3167. calledFunc = func->parameterTypes[v].GetFuncDef();
  3168. break;
  3169. }
  3170. paramPos -= func->parameterTypes[v].GetSizeOnStackDWords();
  3171. }
  3172. }
  3173. break;
  3174. }
  3175. else if( bc == asBC_REFCPY ||
  3176. bc == asBC_COPY )
  3177. {
  3178. // In this case we know there is only 1 pointer on the stack above
  3179. asASSERT( offset == AS_PTR_SIZE );
  3180. return offset + (1 - AS_PTR_SIZE);
  3181. }
  3182. n += asBCTypeSize[asBCInfo[bc].type];
  3183. }
  3184. asASSERT( calledFunc );
  3185. // Count the number of pointers pushed on the stack above the
  3186. // current offset, and then adjust the offset accordingly
  3187. asUINT numPtrs = 0;
  3188. int currOffset = 0;
  3189. if( offset > currOffset && calledFunc->GetObjectType() )
  3190. {
  3191. numPtrs++;
  3192. currOffset += AS_PTR_SIZE;
  3193. }
  3194. if( offset > currOffset && calledFunc->DoesReturnOnStack() )
  3195. {
  3196. numPtrs++;
  3197. currOffset += AS_PTR_SIZE;
  3198. }
  3199. for( asUINT p = 0; p < calledFunc->parameterTypes.GetLength(); p++ )
  3200. {
  3201. if( offset <= currOffset ) break;
  3202. if( !calledFunc->parameterTypes[p].IsPrimitive() ||
  3203. calledFunc->parameterTypes[p].IsReference() )
  3204. {
  3205. // objects and references are passed by pointer
  3206. numPtrs++;
  3207. currOffset += AS_PTR_SIZE;
  3208. // The variable arg ? has an additional 32bit int with the typeid
  3209. if( calledFunc->parameterTypes[p].IsAnyType() )
  3210. currOffset += 1;
  3211. }
  3212. else
  3213. {
  3214. // built-in primitives or enums are passed by value
  3215. asASSERT( calledFunc->parameterTypes[p].IsPrimitive() );
  3216. currOffset += calledFunc->parameterTypes[p].GetSizeOnStackDWords();
  3217. }
  3218. }
  3219. // The get offset must match one of the parameter offsets
  3220. asASSERT( offset == currOffset );
  3221. return offset + numPtrs * (1 - AS_PTR_SIZE);
  3222. }
  3223. void asCWriter::WriteByteCode(asCScriptFunction *func)
  3224. {
  3225. asDWORD *bc = func->byteCode.AddressOf();
  3226. size_t length = func->byteCode.GetLength();
  3227. // The length cannot be stored, because it is platform dependent,
  3228. // instead we store the number of instructions
  3229. asUINT count = bytecodeNbrByPos[bytecodeNbrByPos.GetLength()-1] + 1;
  3230. WriteEncodedInt64(count);
  3231. asDWORD *startBC = bc;
  3232. while( length )
  3233. {
  3234. asDWORD tmp[4]; // The biggest instructions take up 4 DWORDs
  3235. asDWORD c = *(asBYTE*)bc;
  3236. // Copy the instruction to a temp buffer so we can work on it before saving
  3237. memcpy(tmp, bc, asBCTypeSize[asBCInfo[c].type]*sizeof(asDWORD));
  3238. if( c == asBC_ALLOC ) // PTR_DW_ARG
  3239. {
  3240. // Translate the object type
  3241. asCObjectType *ot = *(asCObjectType**)(tmp+1);
  3242. *(asPWORD*)(tmp+1) = FindObjectTypeIdx(ot);
  3243. // Translate the constructor func id, unless it is 0
  3244. if( *(int*)&tmp[1+AS_PTR_SIZE] != 0 )
  3245. {
  3246. // Increment 1 to the translated function id, as 0 will be reserved for no function
  3247. *(int*)&tmp[1+AS_PTR_SIZE] = 1+FindFunctionIndex(engine->scriptFunctions[*(int*)&tmp[1+AS_PTR_SIZE]]);
  3248. }
  3249. }
  3250. else if( c == asBC_FREE || // wW_PTR_ARG
  3251. c == asBC_REFCPY || // PTR_ARG
  3252. c == asBC_RefCpyV || // wW_PTR_ARG
  3253. c == asBC_OBJTYPE ) // PTR_ARG
  3254. {
  3255. // Translate object type pointers into indices
  3256. *(asPWORD*)(tmp+1) = FindObjectTypeIdx(*(asCObjectType**)(tmp+1));
  3257. }
  3258. else if( c == asBC_JitEntry ) // PTR_ARG
  3259. {
  3260. // We don't store the JIT argument
  3261. *(asPWORD*)(tmp+1) = 0;
  3262. }
  3263. else if( c == asBC_TYPEID || // DW_ARG
  3264. c == asBC_Cast ) // DW_ARG
  3265. {
  3266. // Translate type ids into indices
  3267. *(int*)(tmp+1) = FindTypeIdIdx(*(int*)(tmp+1));
  3268. }
  3269. else if( c == asBC_ADDSi || // W_DW_ARG
  3270. c == asBC_LoadThisR ) // W_DW_ARG
  3271. {
  3272. // Translate property offsets into indices
  3273. *(((short*)tmp)+1) = (short)FindObjectPropIndex(*(((short*)tmp)+1), *(int*)(tmp+1));
  3274. // Translate type ids into indices
  3275. *(int*)(tmp+1) = FindTypeIdIdx(*(int*)(tmp+1));
  3276. }
  3277. else if( c == asBC_LoadRObjR || // rW_W_DW_ARG
  3278. c == asBC_LoadVObjR ) // rW_W_DW_ARG
  3279. {
  3280. // Translate property offsets into indices
  3281. *(((short*)tmp)+2) = (short)FindObjectPropIndex(*(((short*)tmp)+2), *(int*)(tmp+2));
  3282. // Translate type ids into indices
  3283. *(int*)(tmp+2) = FindTypeIdIdx(*(int*)(tmp+2));
  3284. }
  3285. else if( c == asBC_COPY ) // W_DW_ARG
  3286. {
  3287. // Translate type ids into indices
  3288. *(int*)(tmp+1) = FindTypeIdIdx(*(int*)(tmp+1));
  3289. // Update the WORDARG0 to 0, as this will be recalculated on the target platform
  3290. asBC_WORDARG0(tmp) = 0;
  3291. }
  3292. else if( c == asBC_RET ) // W_ARG
  3293. {
  3294. // Save with arg 0, as this will be recalculated on the target platform
  3295. asBC_WORDARG0(tmp) = 0;
  3296. }
  3297. else if( c == asBC_CALL || // DW_ARG
  3298. c == asBC_CALLINTF || // DW_ARG
  3299. c == asBC_CALLSYS ) // DW_ARG
  3300. {
  3301. // Translate the function id
  3302. *(int*)(tmp+1) = FindFunctionIndex(engine->scriptFunctions[*(int*)(tmp+1)]);
  3303. }
  3304. else if( c == asBC_FuncPtr ) // PTR_ARG
  3305. {
  3306. // Translate the function pointer
  3307. *(asPWORD*)(tmp+1) = FindFunctionIndex(*(asCScriptFunction**)(tmp+1));
  3308. }
  3309. else if( c == asBC_STR ) // W_ARG
  3310. {
  3311. // Translate the string constant id
  3312. asWORD *arg = ((asWORD*)tmp)+1;
  3313. *arg = (asWORD)FindStringConstantIndex(*arg);
  3314. }
  3315. else if( c == asBC_CALLBND ) // DW_ARG
  3316. {
  3317. // Translate the function id
  3318. int funcId = tmp[1];
  3319. for( asUINT n = 0; n < module->bindInformations.GetLength(); n++ )
  3320. if( module->bindInformations[n]->importedFunctionSignature->id == funcId )
  3321. {
  3322. funcId = n;
  3323. break;
  3324. }
  3325. tmp[1] = funcId;
  3326. }
  3327. else if( c == asBC_PGA || // PTR_ARG
  3328. c == asBC_PshGPtr || // PTR_ARG
  3329. c == asBC_LDG || // PTR_ARG
  3330. c == asBC_PshG4 || // PTR_ARG
  3331. c == asBC_LdGRdR4 || // wW_PTR_ARG
  3332. c == asBC_CpyGtoV4 || // wW_PTR_ARG
  3333. c == asBC_CpyVtoG4 || // rW_PTR_ARG
  3334. c == asBC_SetG4 ) // PTR_DW_ARG
  3335. {
  3336. // Translate global variable pointers into indices
  3337. *(asPWORD*)(tmp+1) = FindGlobalPropPtrIndex(*(void**)(tmp+1));
  3338. }
  3339. else if( c == asBC_JMP || // DW_ARG
  3340. c == asBC_JZ ||
  3341. c == asBC_JNZ ||
  3342. c == asBC_JLowZ ||
  3343. c == asBC_JLowNZ ||
  3344. c == asBC_JS ||
  3345. c == asBC_JNS ||
  3346. c == asBC_JP ||
  3347. c == asBC_JNP ) // The JMPP instruction doesn't need modification
  3348. {
  3349. // Get the DWORD offset from arg
  3350. int offset = *(int*)(tmp+1);
  3351. // Determine instruction number for next instruction and destination
  3352. int bcSeqNum = bytecodeNbrByPos[bc - startBC] + 1;
  3353. asDWORD *targetBC = bc + 2 + offset;
  3354. int targetBcSeqNum = bytecodeNbrByPos[targetBC - startBC];
  3355. // Set the offset in number of instructions
  3356. *(int*)(tmp+1) = targetBcSeqNum - bcSeqNum;
  3357. }
  3358. else if( c == asBC_GETOBJ || // W_ARG
  3359. c == asBC_GETOBJREF ||
  3360. c == asBC_GETREF )
  3361. {
  3362. // Adjust the offset according to the function call that comes after
  3363. asBC_WORDARG0(tmp) = (asWORD)AdjustGetOffset(asBC_WORDARG0(tmp), func, asDWORD(bc - startBC));
  3364. }
  3365. // Adjust the variable offsets
  3366. switch( asBCInfo[c].type )
  3367. {
  3368. case asBCTYPE_wW_ARG:
  3369. case asBCTYPE_rW_DW_ARG:
  3370. case asBCTYPE_wW_QW_ARG:
  3371. case asBCTYPE_rW_ARG:
  3372. case asBCTYPE_wW_DW_ARG:
  3373. case asBCTYPE_wW_W_ARG:
  3374. case asBCTYPE_rW_QW_ARG:
  3375. case asBCTYPE_rW_W_DW_ARG:
  3376. {
  3377. asBC_SWORDARG0(tmp) = (short)AdjustStackPosition(asBC_SWORDARG0(tmp));
  3378. }
  3379. break;
  3380. case asBCTYPE_wW_rW_ARG:
  3381. case asBCTYPE_wW_rW_DW_ARG:
  3382. case asBCTYPE_rW_rW_ARG:
  3383. {
  3384. asBC_SWORDARG0(tmp) = (short)AdjustStackPosition(asBC_SWORDARG0(tmp));
  3385. asBC_SWORDARG1(tmp) = (short)AdjustStackPosition(asBC_SWORDARG1(tmp));
  3386. }
  3387. break;
  3388. case asBCTYPE_wW_rW_rW_ARG:
  3389. {
  3390. asBC_SWORDARG0(tmp) = (short)AdjustStackPosition(asBC_SWORDARG0(tmp));
  3391. asBC_SWORDARG1(tmp) = (short)AdjustStackPosition(asBC_SWORDARG1(tmp));
  3392. asBC_SWORDARG2(tmp) = (short)AdjustStackPosition(asBC_SWORDARG2(tmp));
  3393. }
  3394. break;
  3395. default:
  3396. // The other types don't treat variables so won't be modified
  3397. break;
  3398. }
  3399. // TODO: bytecode: Must make sure that floats and doubles are always stored the same way regardless of platform.
  3400. // Some platforms may not use the IEEE 754 standard, in which case it is necessary to encode the values
  3401. // Now store the instruction in the smallest possible way
  3402. switch( asBCInfo[c].type )
  3403. {
  3404. case asBCTYPE_NO_ARG:
  3405. {
  3406. // Just write 1 byte
  3407. asBYTE b = (asBYTE)c;
  3408. WriteData(&b, 1);
  3409. }
  3410. break;
  3411. case asBCTYPE_W_ARG:
  3412. case asBCTYPE_wW_ARG:
  3413. case asBCTYPE_rW_ARG:
  3414. {
  3415. // Write the instruction code
  3416. asBYTE b = (asBYTE)c;
  3417. WriteData(&b, 1);
  3418. // Write the argument
  3419. short w = *(((short*)tmp)+1);
  3420. WriteEncodedInt64(w);
  3421. }
  3422. break;
  3423. case asBCTYPE_rW_DW_ARG:
  3424. case asBCTYPE_wW_DW_ARG:
  3425. case asBCTYPE_W_DW_ARG:
  3426. {
  3427. // Write the instruction code
  3428. asBYTE b = (asBYTE)c;
  3429. WriteData(&b, 1);
  3430. // Write the word argument
  3431. short w = *(((short*)tmp)+1);
  3432. WriteEncodedInt64(w);
  3433. // Write the dword argument
  3434. WriteEncodedInt64((int)tmp[1]);
  3435. }
  3436. break;
  3437. case asBCTYPE_DW_ARG:
  3438. {
  3439. // Write the instruction code
  3440. asBYTE b = (asBYTE)c;
  3441. WriteData(&b, 1);
  3442. // Write the argument
  3443. WriteEncodedInt64((int)tmp[1]);
  3444. }
  3445. break;
  3446. case asBCTYPE_DW_DW_ARG:
  3447. {
  3448. // Write the instruction code
  3449. asBYTE b = (asBYTE)c;
  3450. WriteData(&b, 1);
  3451. // Write the dword argument
  3452. WriteEncodedInt64((int)tmp[1]);
  3453. // Write the dword argument
  3454. WriteEncodedInt64((int)tmp[2]);
  3455. }
  3456. break;
  3457. case asBCTYPE_wW_rW_rW_ARG:
  3458. {
  3459. // Write the instruction code
  3460. asBYTE b = (asBYTE)c;
  3461. WriteData(&b, 1);
  3462. // Write the first argument
  3463. short w = *(((short*)tmp)+1);
  3464. WriteEncodedInt64(w);
  3465. // Write the second argument
  3466. w = *(((short*)tmp)+2);
  3467. WriteEncodedInt64(w);
  3468. // Write the third argument
  3469. w = *(((short*)tmp)+3);
  3470. WriteEncodedInt64(w);
  3471. }
  3472. break;
  3473. case asBCTYPE_wW_rW_ARG:
  3474. case asBCTYPE_rW_rW_ARG:
  3475. case asBCTYPE_wW_W_ARG:
  3476. {
  3477. // Write the instruction code
  3478. asBYTE b = (asBYTE)c;
  3479. WriteData(&b, 1);
  3480. // Write the first argument
  3481. short w = *(((short*)tmp)+1);
  3482. WriteEncodedInt64(w);
  3483. // Write the second argument
  3484. w = *(((short*)tmp)+2);
  3485. WriteEncodedInt64(w);
  3486. }
  3487. break;
  3488. case asBCTYPE_wW_rW_DW_ARG:
  3489. case asBCTYPE_rW_W_DW_ARG:
  3490. {
  3491. // Write the instruction code
  3492. asBYTE b = (asBYTE)c;
  3493. WriteData(&b, 1);
  3494. // Write the first argument
  3495. short w = *(((short*)tmp)+1);
  3496. WriteEncodedInt64(w);
  3497. // Write the second argument
  3498. w = *(((short*)tmp)+2);
  3499. WriteEncodedInt64(w);
  3500. // Write the third argument
  3501. int dw = tmp[2];
  3502. WriteEncodedInt64(dw);
  3503. }
  3504. break;
  3505. case asBCTYPE_QW_ARG:
  3506. {
  3507. // Write the instruction code
  3508. asBYTE b = (asBYTE)c;
  3509. WriteData(&b, 1);
  3510. // Write the argument
  3511. asQWORD qw = *(asQWORD*)&tmp[1];
  3512. WriteEncodedInt64(qw);
  3513. }
  3514. break;
  3515. case asBCTYPE_QW_DW_ARG:
  3516. {
  3517. // Write the instruction code
  3518. asBYTE b = (asBYTE)c;
  3519. WriteData(&b, 1);
  3520. // Write the argument
  3521. asQWORD qw = *(asQWORD*)&tmp[1];
  3522. WriteEncodedInt64(qw);
  3523. // Write the second argument
  3524. int dw = tmp[3];
  3525. WriteEncodedInt64(dw);
  3526. }
  3527. break;
  3528. case asBCTYPE_rW_QW_ARG:
  3529. case asBCTYPE_wW_QW_ARG:
  3530. {
  3531. // Write the instruction code
  3532. asBYTE b = (asBYTE)c;
  3533. WriteData(&b, 1);
  3534. // Write the first argument
  3535. short w = *(((short*)tmp)+1);
  3536. WriteEncodedInt64(w);
  3537. // Write the argument
  3538. asQWORD qw = *(asQWORD*)&tmp[1];
  3539. WriteEncodedInt64(qw);
  3540. }
  3541. break;
  3542. default:
  3543. {
  3544. // This should never happen
  3545. asASSERT(false);
  3546. // Store the bc as is
  3547. for( int n = 0; n < asBCTypeSize[asBCInfo[c].type]; n++ )
  3548. WriteData(&tmp[n], 4);
  3549. }
  3550. }
  3551. // Move to the next instruction
  3552. bc += asBCTypeSize[asBCInfo[c].type];
  3553. length -= asBCTypeSize[asBCInfo[c].type];
  3554. }
  3555. }
  3556. void asCWriter::WriteUsedTypeIds()
  3557. {
  3558. asUINT count = (asUINT)usedTypeIds.GetLength();
  3559. WriteEncodedInt64(count);
  3560. for( asUINT n = 0; n < count; n++ )
  3561. {
  3562. asCDataType dt = engine->GetDataTypeFromTypeId(usedTypeIds[n]);
  3563. WriteDataType(&dt);
  3564. }
  3565. }
  3566. int asCWriter::FindGlobalPropPtrIndex(void *ptr)
  3567. {
  3568. int i = usedGlobalProperties.IndexOf(ptr);
  3569. if( i >= 0 ) return i;
  3570. usedGlobalProperties.PushLast(ptr);
  3571. return (int)usedGlobalProperties.GetLength()-1;
  3572. }
  3573. void asCWriter::WriteUsedGlobalProps()
  3574. {
  3575. int c = (int)usedGlobalProperties.GetLength();
  3576. WriteEncodedInt64(c);
  3577. for( int n = 0; n < c; n++ )
  3578. {
  3579. asPWORD *p = (asPWORD*)usedGlobalProperties[n];
  3580. // First search for the global in the module
  3581. char moduleProp = 0;
  3582. asCGlobalProperty *prop = 0;
  3583. asCSymbolTable<asCGlobalProperty>::iterator it = module->scriptGlobals.List();
  3584. for( ; it; it++ )
  3585. {
  3586. if( p == (*it)->GetAddressOfValue() )
  3587. {
  3588. prop = (*it);
  3589. moduleProp = 1;
  3590. break;
  3591. }
  3592. }
  3593. // If it is not in the module, it must be an application registered property
  3594. if( !prop )
  3595. {
  3596. asCSymbolTable<asCGlobalProperty>::iterator it = engine->registeredGlobalProps.List();
  3597. for( ; it; it++ )
  3598. {
  3599. if( it->GetAddressOfValue() == p )
  3600. {
  3601. prop = *it;
  3602. break;
  3603. }
  3604. }
  3605. }
  3606. asASSERT(prop);
  3607. // Store the name and type of the property so we can find it again on loading
  3608. WriteString(&prop->name);
  3609. WriteString(&prop->nameSpace->name);
  3610. WriteDataType(&prop->type);
  3611. // Also store whether the property is a module property or a registered property
  3612. WriteData(&moduleProp, 1);
  3613. }
  3614. }
  3615. void asCWriter::WriteUsedObjectProps()
  3616. {
  3617. int c = (int)usedObjectProperties.GetLength();
  3618. WriteEncodedInt64(c);
  3619. for( asUINT n = 0; n < usedObjectProperties.GetLength(); n++ )
  3620. {
  3621. asCObjectType *objType = usedObjectProperties[n].objType;
  3622. WriteObjectType(objType);
  3623. // Find the property name
  3624. for( asUINT p = 0; p < objType->properties.GetLength(); p++ )
  3625. {
  3626. if( objType->properties[p]->byteOffset == usedObjectProperties[n].offset )
  3627. {
  3628. WriteString(&objType->properties[p]->name);
  3629. break;
  3630. }
  3631. }
  3632. }
  3633. }
  3634. int asCWriter::FindObjectPropIndex(short offset, int typeId)
  3635. {
  3636. asCObjectType *objType = engine->GetObjectTypeFromTypeId(typeId);
  3637. for( asUINT n = 0; n < usedObjectProperties.GetLength(); n++ )
  3638. {
  3639. if( usedObjectProperties[n].objType == objType &&
  3640. usedObjectProperties[n].offset == offset )
  3641. return n;
  3642. }
  3643. SObjProp prop = {objType, offset};
  3644. usedObjectProperties.PushLast(prop);
  3645. return (int)usedObjectProperties.GetLength() - 1;
  3646. }
  3647. int asCWriter::FindFunctionIndex(asCScriptFunction *func)
  3648. {
  3649. for( asUINT n = 0; n < usedFunctions.GetLength(); n++ )
  3650. {
  3651. if( usedFunctions[n] == func )
  3652. return n;
  3653. }
  3654. usedFunctions.PushLast(func);
  3655. return (int)usedFunctions.GetLength() - 1;
  3656. }
  3657. int asCWriter::FindTypeIdIdx(int typeId)
  3658. {
  3659. asUINT n;
  3660. for( n = 0; n < usedTypeIds.GetLength(); n++ )
  3661. {
  3662. if( usedTypeIds[n] == typeId )
  3663. return n;
  3664. }
  3665. usedTypeIds.PushLast(typeId);
  3666. return (int)usedTypeIds.GetLength() - 1;
  3667. }
  3668. int asCWriter::FindObjectTypeIdx(asCObjectType *obj)
  3669. {
  3670. asUINT n;
  3671. for( n = 0; n < usedTypes.GetLength(); n++ )
  3672. {
  3673. if( usedTypes[n] == obj )
  3674. return n;
  3675. }
  3676. usedTypes.PushLast(obj);
  3677. return (int)usedTypes.GetLength() - 1;
  3678. }
  3679. #endif // AS_NO_COMPILER
  3680. END_AS_NAMESPACE