as_restore.cpp 124 KB

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