hlcgobj.pas 188 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467
  1. {
  2. Copyright (c) 1998-2010 by Florian Klaempfl and Jonas Maebe
  3. Member of the Free Pascal development team
  4. This unit implements the basic high level code generator object
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {# @abstract(Abstract code generator unit)
  19. Abstract high level code generator unit. This contains the base class
  20. that either lowers most code to the regular code generator, or that
  21. has to be implemented/overridden for higher level targets (such as LLVM).
  22. }
  23. unit hlcgobj;
  24. {$i fpcdefs.inc}
  25. { define hlcginline}
  26. interface
  27. uses
  28. cclasses,globtype,constexp,
  29. cpubase,cgbase,cgutils,parabase,
  30. aasmbase,aasmtai,aasmdata,aasmcpu,
  31. symconst,symtype,symdef,
  32. node
  33. ;
  34. type
  35. tsubsetloadopt = (SL_REG,SL_REGNOSRCMASK,SL_SETZERO,SL_SETMAX);
  36. {# @abstract(Abstract high level code generator)
  37. This class implements an abstract instruction generator. All
  38. methods of this class are generic and are mapped to low level code
  39. generator methods by default. They have to be overridden for higher
  40. level targets
  41. }
  42. { thlcgobj }
  43. thlcgobj = class
  44. public
  45. {************************************************}
  46. { basic routines }
  47. constructor create;
  48. {# Initialize the register allocators needed for the codegenerator.}
  49. procedure init_register_allocators;virtual;
  50. {# Clean up the register allocators needed for the codegenerator.}
  51. procedure done_register_allocators;virtual;
  52. {# Set whether live_start or live_end should be updated when allocating registers, needed when e.g. generating initcode after the rest of the code. }
  53. procedure set_regalloc_live_range_direction(dir: TRADirection);virtual;
  54. {# Gets a register suitable to do integer operations on.}
  55. function getintregister(list:TAsmList;size:tdef):Tregister;virtual;
  56. {# Gets a register suitable to do integer operations on.}
  57. function getaddressregister(list:TAsmList;size:tdef):Tregister;virtual;
  58. function getfpuregister(list:TAsmList;size:tdef):Tregister;virtual;
  59. // we don't have high level defs yet that translate into all mm cgsizes
  60. // function getmmregister(list:TAsmList;size:tdef):Tregister;virtual;
  61. function getflagregister(list:TAsmList;size:tdef):Tregister;virtual;
  62. function getregisterfordef(list: TAsmList;size:tdef):Tregister;virtual;
  63. {Does the generic cg need SIMD registers, like getmmxregister? Or should
  64. the cpu specific child cg object have such a method?}
  65. function uses_registers(rt:Tregistertype):boolean; inline;
  66. {# Get a specific register.}
  67. procedure getcpuregister(list:TAsmList;r:Tregister);virtual;
  68. procedure ungetcpuregister(list:TAsmList;r:Tregister);virtual;
  69. {# Get multiple registers specified.}
  70. procedure alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  71. {# Free multiple registers specified.}
  72. procedure dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  73. procedure allocallcpuregisters(list:TAsmList);virtual;
  74. procedure deallocallcpuregisters(list:TAsmList);virtual;
  75. procedure do_register_allocation(list:TAsmList;headertai:tai); inline;
  76. procedure translate_register(var reg : tregister); inline;
  77. {# Returns the kind of register this type should be loaded in (it does not
  78. check whether this is actually possible, but if it's loaded in a register
  79. by the compiler for any purpose other than parameter passing/function
  80. result loading, this is the register type used }
  81. function def2regtyp(def: tdef): tregistertype; virtual;
  82. { # Returns orddef corresponding to size }
  83. class function tcgsize2orddef(size: tcgsize): torddef; static;
  84. {# Emit a label to the instruction stream. }
  85. procedure a_label(list : TAsmList;l : tasmlabel); inline;
  86. {# Allocates register r by inserting a pai_realloc record }
  87. procedure a_reg_alloc(list : TAsmList;r : tregister); inline;
  88. {# Deallocates register r by inserting a pa_regdealloc record}
  89. procedure a_reg_dealloc(list : TAsmList;r : tregister); inline;
  90. { Synchronize register, make sure it is still valid }
  91. procedure a_reg_sync(list : TAsmList;r : tregister); inline;
  92. {# Pass a parameter, which is located in a register, to a routine.
  93. This routine should push/send the parameter to the routine, as
  94. required by the specific processor ABI and routine modifiers.
  95. It must generate register allocation information for the cgpara in
  96. case it consists of cpuregisters.
  97. @param(size size of the operand in the register)
  98. @param(r register source of the operand)
  99. @param(cgpara where the parameter will be stored)
  100. }
  101. procedure a_load_reg_cgpara(list : TAsmList;size : tdef;r : tregister;const cgpara : TCGPara);virtual;
  102. {# Pass a parameter, which is a constant, to a routine.
  103. A generic version is provided. This routine should
  104. be overridden for optimization purposes if the cpu
  105. permits directly sending this type of parameter.
  106. It must generate register allocation information for the cgpara in
  107. case it consists of cpuregisters.
  108. @param(size size of the operand in constant)
  109. @param(a value of constant to send)
  110. @param(cgpara where the parameter will be stored)
  111. }
  112. procedure a_load_const_cgpara(list : TAsmList;tosize : tdef;a : aint;const cgpara : TCGPara);virtual;
  113. {# Pass the value of a parameter, which is located in memory, to a routine.
  114. A generic version is provided. This routine should
  115. be overridden for optimization purposes if the cpu
  116. permits directly sending this type of parameter.
  117. It must generate register allocation information for the cgpara in
  118. case it consists of cpuregisters.
  119. @param(size size of the operand in constant)
  120. @param(r Memory reference of value to send)
  121. @param(cgpara where the parameter will be stored)
  122. }
  123. procedure a_load_ref_cgpara(list : TAsmList;size : tdef;const r : treference;const cgpara : TCGPara);virtual;
  124. {# Pass the value of a parameter, which can be located either in a register or memory location,
  125. to a routine.
  126. A generic version is provided.
  127. @param(l location of the operand to send)
  128. @param(nr parameter number (starting from one) of routine (from left to right))
  129. @param(cgpara where the parameter will be stored)
  130. }
  131. procedure a_load_loc_cgpara(list : TAsmList;size : tdef; const l : tlocation;const cgpara : TCGPara);virtual;
  132. {# Pass the address of a reference to a routine. This routine
  133. will calculate the address of the reference, and pass this
  134. calculated address as a parameter.
  135. It must generate register allocation information for the cgpara in
  136. case it consists of cpuregisters.
  137. A generic version is provided. This routine should
  138. be overridden for optimization purposes if the cpu
  139. permits directly sending this type of parameter.
  140. @param(fromsize type of the reference we are taking the address of)
  141. @param(tosize type of the pointer that we get as a result)
  142. @param(r reference to get address from)
  143. }
  144. procedure a_loadaddr_ref_cgpara(list : TAsmList;fromsize : tdef;const r : treference;const cgpara : TCGPara);virtual;
  145. { Remarks:
  146. * If a method specifies a size you have only to take care
  147. of that number of bits, i.e. load_const_reg with OP_8 must
  148. only load the lower 8 bit of the specified register
  149. the rest of the register can be undefined
  150. if necessary the compiler will call a method
  151. to zero or sign extend the register
  152. * The a_load_XX_XX with OP_64 needn't to be
  153. implemented for 32 bit
  154. processors, the code generator takes care of that
  155. * the addr size is for work with the natural pointer
  156. size
  157. * the procedures without fpu/mm are only for integer usage
  158. * normally the first location is the source and the
  159. second the destination
  160. }
  161. {# Emits instruction to call the method specified by symbol name.
  162. Returns the function result location.
  163. This routine must be overridden for each new target cpu.
  164. }
  165. function a_call_name(list : TAsmList;pd : tprocdef;const s : TSymStr; forceresdef: tdef; weak: boolean): tcgpara;virtual;abstract;
  166. procedure a_call_reg(list : TAsmList;pd : tabstractprocdef;reg : tregister);virtual;abstract;
  167. procedure a_call_ref(list : TAsmList;pd : tabstractprocdef;const ref : treference);virtual;
  168. { same as a_call_name, might be overridden on certain architectures to emit
  169. static calls without usage of a got trampoline }
  170. function a_call_name_static(list : TAsmList;pd : tprocdef;const s : TSymStr; forceresdef: tdef): tcgpara;virtual;
  171. { same as a_call_name, might be overridden on certain architectures to emit
  172. special static calls for inherited methods }
  173. procedure a_call_name_inherited(list : TAsmList;pd : tprocdef;const s : TSymStr);virtual;
  174. { move instructions }
  175. procedure a_load_const_reg(list : TAsmList;tosize : tdef;a : aint;register : tregister);virtual;abstract;
  176. procedure a_load_const_ref(list : TAsmList;tosize : tdef;a : aint;const ref : treference);virtual;
  177. procedure a_load_const_loc(list : TAsmList;tosize : tdef;a : aint;const loc : tlocation);virtual;
  178. procedure a_load_reg_ref(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);virtual;abstract;
  179. procedure a_load_reg_ref_unaligned(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);virtual;
  180. procedure a_load_reg_reg(list : TAsmList;fromsize, tosize : tdef;reg1,reg2 : tregister);virtual;abstract;
  181. procedure a_load_reg_loc(list : TAsmList;fromsize, tosize : tdef;reg : tregister;const loc: tlocation);virtual;
  182. procedure a_load_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);virtual;abstract;
  183. procedure a_load_ref_reg_unaligned(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);virtual;
  184. procedure a_load_ref_ref(list : TAsmList;fromsize, tosize : tdef;const sref : treference;const dref : treference);virtual;
  185. procedure a_load_loc_reg(list : TAsmList;fromsize, tosize : tdef; const loc: tlocation; reg : tregister);virtual;
  186. procedure a_load_loc_ref(list : TAsmList;fromsize, tosize: tdef; const loc: tlocation; const ref : treference);virtual;
  187. procedure a_load_loc_subsetreg(list : TAsmList;fromsize, tosubsetsize: tdef; const loc: tlocation; const sreg : tsubsetregister);virtual;
  188. procedure a_load_loc_subsetref(list : TAsmList;fromsize, tosubsetsize: tdef; const loc: tlocation; const sref : tsubsetreference);virtual;
  189. procedure a_loadaddr_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;r : tregister);virtual;abstract;
  190. { For subsetreg/ref, the def is the size of the packed element. The
  191. size of the register that holds the data is a tcgsize, and hence
  192. always must be an orddef of the corresponding size in practice }
  193. procedure a_load_subsetreg_reg(list : TAsmList; subsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister); virtual;
  194. procedure a_load_reg_subsetreg(list : TAsmList; fromsize, tosubsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister); virtual;
  195. procedure a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tdef; const fromsreg, tosreg: tsubsetregister); virtual;
  196. procedure a_load_subsetreg_ref(list : TAsmList; fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const destref: treference); virtual;
  197. procedure a_load_ref_subsetreg(list : TAsmList; fromsize, tosubsetsize: tdef; const fromref: treference; const sreg: tsubsetregister); virtual;
  198. procedure a_load_const_subsetreg(list: TAsmlist; tosubsetsize: tdef; a: aint; const sreg: tsubsetregister); virtual;
  199. procedure a_load_subsetreg_loc(list: TAsmlist; fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const loc: tlocation); virtual;
  200. procedure a_load_subsetref_reg(list : TAsmList; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; destreg: tregister); virtual;
  201. procedure a_load_reg_subsetref(list : TAsmList; fromsize, tosubsetsize: tdef; fromreg: tregister; const sref: tsubsetreference); virtual;
  202. procedure a_load_subsetref_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tdef; const fromsref, tosref: tsubsetreference); virtual;
  203. procedure a_load_subsetref_ref(list : TAsmList; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const destref: treference); virtual;
  204. procedure a_load_ref_subsetref(list : TAsmList; fromsize, tosubsetsize: tdef; const fromref: treference; const sref: tsubsetreference); virtual;
  205. procedure a_load_const_subsetref(list: TAsmlist; tosubsetsize: tdef; a: tcgint; const sref: tsubsetreference); virtual;
  206. procedure a_load_subsetref_loc(list: TAsmlist; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const loc: tlocation); virtual;
  207. procedure a_load_subsetref_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tdef; const fromsref: tsubsetreference; const tosreg: tsubsetregister); virtual;
  208. procedure a_load_subsetreg_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tdef; const fromsreg: tsubsetregister; const tosref: tsubsetreference); virtual;
  209. { bit test instructions }
  210. procedure a_bit_test_reg_reg_reg(list : TAsmList; bitnumbersize,valuesize,destsize: tdef;bitnumber,value,destreg: tregister); virtual;
  211. procedure a_bit_test_const_ref_reg(list: TAsmList; fromsize, destsize: tdef; bitnumber: aint; const ref: treference; destreg: tregister); virtual;
  212. procedure a_bit_test_const_reg_reg(list: TAsmList; setregsize, destsize: tdef; bitnumber: aint; setreg, destreg: tregister); virtual;
  213. procedure a_bit_test_const_subsetreg_reg(list: TAsmList; fromsubsetsize, destsize: tdef; bitnumber: aint; const setreg: tsubsetregister; destreg: tregister); virtual;
  214. procedure a_bit_test_reg_ref_reg(list: TAsmList; bitnumbersize, refsize, destsize: tdef; bitnumber: tregister; const ref: treference; destreg: tregister); virtual;
  215. procedure a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, locsize, destsize: tdef; bitnumber: tregister; const loc: tlocation; destreg: tregister);virtual;
  216. procedure a_bit_test_const_loc_reg(list: TAsmList; locsize, destsize: tdef; bitnumber: aint; const loc: tlocation; destreg: tregister);virtual;
  217. { bit set/clear instructions }
  218. procedure a_bit_set_reg_reg(list : TAsmList; doset: boolean; bitnumbersize, destsize: tdef; bitnumber,dest: tregister); virtual;
  219. procedure a_bit_set_const_ref(list: TAsmList; doset: boolean;destsize: tdef; bitnumber: aint; const ref: treference); virtual;
  220. procedure a_bit_set_const_reg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; destreg: tregister); virtual;
  221. procedure a_bit_set_const_subsetreg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; const destreg: tsubsetregister); virtual;
  222. procedure a_bit_set_reg_ref(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const ref: treference); virtual;
  223. procedure a_bit_set_reg_loc(list: TAsmList; doset: boolean; regsize, tosize: tdef; bitnumber: tregister; const loc: tlocation);virtual;
  224. procedure a_bit_set_const_loc(list: TAsmList; doset: boolean; tosize: tdef; bitnumber: aint; const loc: tlocation);virtual;
  225. protected
  226. function get_call_result_cgpara(pd: tprocdef; forceresdef: tdef): tcgpara;
  227. procedure get_subsetref_load_info(const sref: tsubsetreference; out loadsize: torddef; out extra_load: boolean);
  228. procedure a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister); virtual;
  229. procedure a_load_subsetref_regs_index(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg: tregister); virtual;
  230. procedure a_load_regconst_subsetref_intern(list : TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sref: tsubsetreference; slopt: tsubsetloadopt); virtual;
  231. procedure a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt); virtual;
  232. { return a subsetref that represents bit "bitnumber" in ref, if ref
  233. has the type "refdef". The subsetref must be addressable via
  234. (unsigned) 8 bit access, unless all the *_bit_* methods are
  235. overloaded and use something else. }
  236. function get_bit_const_ref_sref(bitnumber: tcgint; refdef: tdef; const ref: treference): tsubsetreference;
  237. function get_bit_const_reg_sreg(setregsize: tdef; bitnumber: tcgint; setreg: tregister): tsubsetregister;
  238. function get_bit_reg_ref_sref(list: TAsmList; bitnumbersize, refsize: tdef; bitnumber: tregister; const ref: treference): tsubsetreference;
  239. public
  240. { bit scan instructions (still need transformation to thlcgobj) }
  241. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: tdef; src, dst: tregister); virtual; abstract;
  242. { fpu move instructions }
  243. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister); virtual; abstract;
  244. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister); virtual; abstract;
  245. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference); virtual; abstract;
  246. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1,ref2: treference);virtual;
  247. procedure a_loadfpu_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister);virtual;
  248. procedure a_loadfpu_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation);virtual;
  249. procedure a_loadfpu_reg_cgpara(list : TAsmList;fromsize: tdef;const r : tregister;const cgpara : TCGPara);virtual;
  250. procedure a_loadfpu_ref_cgpara(list : TAsmList;fromsize : tdef;const ref : treference;const cgpara : TCGPara);virtual;
  251. { vector register move instructions }
  252. // we don't have high level defs yet that translate into all mm cgsizes
  253. {
  254. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef;reg1, reg2: tregister;shuffle : pmmshuffle); virtual;
  255. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  256. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual;
  257. }
  258. { required for subsetreg/ref; still tcgsize rather than tdef because of reason mentioned above }
  259. procedure a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);virtual; abstract;
  260. {
  261. procedure a_loadmm_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);virtual;
  262. procedure a_loadmm_reg_cgpara(list: TAsmList; fromsize: tdef; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  263. procedure a_loadmm_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  264. procedure a_loadmm_loc_cgpara(list: TAsmList; fromsize: tdef; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  265. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tdef;src,dst: tregister;shuffle : pmmshuffle); virtual;
  266. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tdef;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  267. procedure a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tdef;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  268. procedure a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tdef;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  269. }
  270. // we don't have high level defs yet that translate into all mm cgsizes
  271. // procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tdef; intreg, mmreg: tregister; shuffle: pmmshuffle); virtual;
  272. // procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tdef; mmreg, intreg: tregister; shuffle : pmmshuffle); virtual;
  273. { basic arithmetic operations }
  274. { note: for operators which require only one argument (not, neg), use }
  275. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  276. { that in this case the *second* operand is used as both source and }
  277. { destination (JM) }
  278. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; reg: TRegister); virtual; abstract;
  279. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference); virtual;
  280. procedure a_op_const_subsetreg(list : TAsmList; Op : TOpCG; size, subsetsize : tdef; a : aint; const sreg: tsubsetregister); virtual;
  281. procedure a_op_const_subsetref(list : TAsmList; Op : TOpCG; size, subsetsize : tdef; a : aint; const sref: tsubsetreference); virtual;
  282. procedure a_op_const_loc(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; const loc: tlocation);virtual;
  283. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister); virtual; abstract;
  284. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: tdef; reg: TRegister; const ref: TReference); virtual;
  285. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister); virtual;
  286. procedure a_op_reg_subsetreg(list: TAsmList; Op: TOpCG; opsize, destsubsetsize: tdef; reg: TRegister; const sreg: tsubsetregister); virtual;
  287. procedure a_op_reg_subsetref(list: TAsmList; Op: TOpCG; opsize, destsubsetsize: tdef; reg: TRegister; const sref: tsubsetreference); virtual;
  288. procedure a_op_reg_loc(list : TAsmList; Op: TOpCG; size: tdef; reg: tregister; const loc: tlocation);virtual;
  289. procedure a_op_ref_loc(list : TAsmList; Op: TOpCG; size: tdef; const ref: TReference; const loc: tlocation);virtual;
  290. { trinary operations for processors that support them, 'emulated' }
  291. { on others. None with "ref" arguments since I don't think there }
  292. { are any processors that support it (JM) }
  293. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister); virtual;
  294. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister); virtual;
  295. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  296. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  297. { comparison operations }
  298. procedure a_cmp_const_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp;a : aint;reg : tregister;
  299. l : tasmlabel);virtual;
  300. procedure a_cmp_const_ref_label(list : TAsmList;size : tdef;cmp_op : topcmp;a : aint;const ref : treference;
  301. l : tasmlabel); virtual;
  302. procedure a_cmp_const_loc_label(list: TAsmList; size: tdef;cmp_op: topcmp; a: aint; const loc: tlocation;
  303. l : tasmlabel);virtual;
  304. procedure a_cmp_reg_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  305. procedure a_cmp_ref_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  306. procedure a_cmp_reg_ref_label(list : TAsmList;size : tdef;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  307. procedure a_cmp_subsetreg_reg_label(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; l: tasmlabel); virtual;
  308. procedure a_cmp_subsetref_reg_label(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; l: tasmlabel); virtual;
  309. procedure a_cmp_loc_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);virtual;
  310. procedure a_cmp_reg_loc_label(list : TAsmList;size : tdef;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  311. procedure a_cmp_ref_loc_label(list: TAsmList; size: tdef;cmp_op: topcmp; const ref: treference; const loc: tlocation; l : tasmlabel);virtual;
  312. procedure a_jmp_always(list : TAsmList;l: tasmlabel); virtual;abstract;
  313. {$ifdef cpuflags}
  314. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); virtual; abstract;
  315. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  316. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  317. }
  318. procedure g_flags2reg(list: TAsmList; size: tdef; const f: tresflags; reg: TRegister); virtual; abstract;
  319. procedure g_flags2ref(list: TAsmList; size: tdef; const f: tresflags; const ref:TReference); virtual; abstract;
  320. {$endif cpuflags}
  321. procedure g_maybe_testself(list : TAsmList; selftype: tdef; reg:tregister);
  322. // procedure g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  323. {# This should emit the opcode to copy len bytes from the source
  324. to destination.
  325. It must be overridden for each new target processor.
  326. @param(source Source reference of copy)
  327. @param(dest Destination reference of copy)
  328. }
  329. procedure g_concatcopy(list : TAsmList;size: tdef; const source,dest : treference);virtual;
  330. {# This should emit the opcode to copy len bytes from the an unaligned source
  331. to destination.
  332. It must be overridden for each new target processor.
  333. @param(source Source reference of copy)
  334. @param(dest Destination reference of copy)
  335. }
  336. procedure g_concatcopy_unaligned(list : TAsmList;size: tdef; const source,dest : treference);virtual;
  337. {# This should emit the opcode to a shortrstring from the source
  338. to destination.
  339. @param(source Source reference of copy)
  340. @param(dest Destination reference of copy)
  341. }
  342. procedure g_copyshortstring(list : TAsmList;const source,dest : treference;strdef:tstringdef);virtual;
  343. procedure g_copyvariant(list : TAsmList;const source,dest : treference;vardef:tvariantdef);virtual;
  344. procedure g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);virtual;
  345. procedure g_initialize(list : TAsmList;t : tdef;const ref : treference);virtual;
  346. procedure g_finalize(list : TAsmList;t : tdef;const ref : treference);virtual;
  347. procedure g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation;
  348. const name: string);virtual;
  349. {# Generates range checking code. It is to note
  350. that this routine does not need to be overridden,
  351. as it takes care of everything.
  352. @param(p Node which contains the value to check)
  353. @param(todef Type definition of node to range check)
  354. }
  355. procedure g_rangecheck(list: TAsmList; const l:tlocation; fromdef,todef: tdef); virtual;
  356. {# Generates overflow checking code for a node }
  357. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef); virtual; abstract;
  358. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;var ovloc : tlocation);virtual; abstract;
  359. procedure g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;arrdef: tarraydef;destreg:tregister);virtual;
  360. procedure g_releasevaluepara_openarray(list : TAsmList;arrdef: tarraydef;const l:tlocation);virtual;
  361. {# Emits instructions when compilation is done in profile
  362. mode (this is set as a command line option). The default
  363. behavior does nothing, should be overridden as required.
  364. }
  365. procedure g_profilecode(list : TAsmList);virtual;
  366. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  367. @param(size Number of bytes to allocate)
  368. }
  369. procedure g_stackpointer_alloc(list : TAsmList;size : longint);virtual; abstract;
  370. {# Emits instruction for allocating the locals in entry
  371. code of a routine. This is one of the first
  372. routine called in @var(genentrycode).
  373. @param(localsize Number of bytes to allocate as locals)
  374. }
  375. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);virtual; abstract;
  376. {# Emits instructions for returning from a subroutine.
  377. Should also restore the framepointer and stack.
  378. @param(parasize Number of bytes of parameters to deallocate from stack)
  379. }
  380. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);virtual; abstract;
  381. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);virtual; abstract;
  382. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);virtual; abstract;
  383. { generate a stub which only purpose is to pass control the given external method,
  384. setting up any additional environment before doing so (if required).
  385. The default implementation issues a jump instruction to the external name. }
  386. // procedure g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string); virtual;
  387. protected
  388. procedure g_allocload_reg_reg(list: TAsmList; regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  389. public
  390. { create "safe copy" of a tlocation that can be used later: all
  391. registers used in the tlocation are copied to new ones, so that
  392. even if the original ones change, things stay the same (except if
  393. the original location was already a register, then the register is
  394. kept). Must only be used on lvalue locations.
  395. It's intended as some kind of replacement for a_loadaddr_ref_reg()
  396. for targets without pointers. }
  397. procedure g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation); virtual;
  398. { routines migrated from ncgutil }
  399. procedure location_force_reg(list:TAsmList;var l:tlocation;src_size,dst_size:tdef;maybeconst:boolean);virtual;
  400. procedure location_force_fpureg(list:TAsmList;var l: tlocation;size: tdef;maybeconst:boolean);virtual;
  401. procedure location_force_mem(list:TAsmList;var l:tlocation;size:tdef);virtual;
  402. // procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;abstract;
  403. // procedure location_force_mmreg(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;abstract;
  404. { Retrieve the location of the data pointed to in location l, when the location is
  405. a register it is expected to contain the address of the data }
  406. procedure location_get_data_ref(list:TAsmList;def: tdef; const l:tlocation;var ref:treference;loadref:boolean; alignment: longint);virtual;
  407. procedure maketojumpbool(list:TAsmList; p : tnode);virtual;
  408. procedure gen_proc_symbol(list:TAsmList);virtual;
  409. procedure gen_proc_symbol_end(list:TAsmList);virtual;
  410. procedure gen_initialize_code(list:TAsmList);virtual;
  411. procedure gen_finalize_code(list:TAsmList);virtual;
  412. procedure gen_entry_code(list:TAsmList);virtual;
  413. procedure gen_exit_code(list:TAsmList);virtual;
  414. protected
  415. { helpers called by gen_initialize_code/gen_finalize_code }
  416. procedure inittempvariables(list:TAsmList);virtual;
  417. procedure initialize_data(p:TObject;arg:pointer);virtual;
  418. procedure finalizetempvariables(list:TAsmList);virtual;
  419. procedure initialize_regvars(p:TObject;arg:pointer);virtual;
  420. procedure finalize_sym(asmlist:TAsmList;sym:tsym);virtual;
  421. { generates the code for finalisation of local variables }
  422. procedure finalize_local_vars(p:TObject;arg:pointer);virtual;
  423. { generates the code for finalization of static symtable and
  424. all local (static) typed consts }
  425. procedure finalize_static_data(p:TObject;arg:pointer);virtual;
  426. { generates the code for decrementing the reference count of parameters }
  427. procedure final_paras(p:TObject;arg:pointer);
  428. public
  429. procedure gen_load_para_value(list:TAsmList);virtual;
  430. protected
  431. { helpers called by gen_load_para_value }
  432. procedure g_copyvalueparas(p:TObject;arg:pointer);virtual;
  433. procedure gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation;const cgpara: tcgpara;locintsize: longint);virtual;
  434. procedure init_paras(p:TObject;arg:pointer);
  435. protected
  436. { Some targets have to put "something" in the function result
  437. location if it's not initialised by the Pascal code, e.g.
  438. stack-based architectures. By default it does nothing }
  439. procedure gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);virtual;
  440. public
  441. { load a tlocation into a cgpara }
  442. procedure gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);virtual;
  443. { load a cgpara into a tlocation }
  444. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);virtual;
  445. { load the function return value into the ABI-defined function return location }
  446. procedure gen_load_return_value(list:TAsmList);virtual;
  447. { extras refactored from other units }
  448. { queue the code/data generated for a procedure for writing out to
  449. the assembler/object file }
  450. procedure record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList); virtual;
  451. { generate a call to a routine in the system unit }
  452. function g_call_system_proc(list: TAsmList; const procname: string; forceresdef: tdef): tcgpara;
  453. function g_call_system_proc(list: TAsmList; pd: tprocdef; forceresdef: tdef): tcgpara;
  454. protected
  455. function g_call_system_proc_intern(list: TAsmList; pd: tprocdef; forceresdef: tdef): tcgpara; virtual;
  456. public
  457. { Generate code to exit an unwind-protected region. The default implementation
  458. produces a simple jump to destination label. }
  459. procedure g_local_unwind(list: TAsmList; l: TAsmLabel);virtual;abstract;
  460. end;
  461. var
  462. {# Main high level code generator class }
  463. hlcg : thlcgobj;
  464. procedure destroy_hlcodegen;
  465. implementation
  466. uses
  467. globals,systems,
  468. fmodule,export,
  469. verbose,defutil,paramgr,
  470. symbase,symsym,symtable,
  471. ncon,nld,ncgrtti,pass_1,pass_2,
  472. cpuinfo,cgobj,tgobj,cutils,procinfo,
  473. ncgutil,ngenutil;
  474. procedure destroy_hlcodegen;
  475. begin
  476. hlcg.free;
  477. hlcg:=nil;
  478. destroy_codegen;
  479. end;
  480. { thlcgobj }
  481. constructor thlcgobj.create;
  482. begin
  483. end;
  484. procedure thlcgobj.init_register_allocators;
  485. begin
  486. cg.init_register_allocators;
  487. end;
  488. procedure thlcgobj.done_register_allocators;
  489. begin
  490. cg.done_register_allocators;
  491. end;
  492. procedure thlcgobj.set_regalloc_live_range_direction(dir: TRADirection);
  493. begin
  494. cg.set_regalloc_live_range_direction(dir);
  495. end;
  496. function thlcgobj.getintregister(list: TAsmList; size: tdef): Tregister;
  497. begin
  498. result:=cg.getintregister(list,def_cgsize(size));
  499. end;
  500. function thlcgobj.getaddressregister(list: TAsmList; size: tdef): Tregister;
  501. begin
  502. result:=cg.getaddressregister(list);
  503. end;
  504. function thlcgobj.getfpuregister(list: TAsmList; size: tdef): Tregister;
  505. begin
  506. result:=cg.getfpuregister(list,def_cgsize(size));
  507. end;
  508. (*
  509. function thlcgobj.getmmregister(list: TAsmList; size: tdef): Tregister;
  510. begin
  511. result:=cg.getmmregister(list,def_cgsize(size));
  512. end;
  513. *)
  514. function thlcgobj.getflagregister(list: TAsmList; size: tdef): Tregister;
  515. begin
  516. result:=cg.getflagregister(list,def_cgsize(size));
  517. end;
  518. function thlcgobj.getregisterfordef(list: TAsmList; size: tdef): Tregister;
  519. begin
  520. case def2regtyp(size) of
  521. R_INTREGISTER:
  522. result:=getintregister(list,size);
  523. R_ADDRESSREGISTER:
  524. result:=getaddressregister(list,size);
  525. R_FPUREGISTER:
  526. result:=getfpuregister(list,size);
  527. (*
  528. R_MMREGISTER:
  529. result:=getmmregister(list,size);
  530. *)
  531. else
  532. internalerror(2010122901);
  533. end;
  534. end;
  535. function thlcgobj.uses_registers(rt: Tregistertype): boolean;
  536. begin
  537. result:=cg.uses_registers(rt);
  538. end;
  539. procedure thlcgobj.getcpuregister(list: TAsmList; r: Tregister);
  540. begin
  541. cg.getcpuregister(list,r);
  542. end;
  543. procedure thlcgobj.ungetcpuregister(list: TAsmList; r: Tregister);
  544. begin
  545. cg.ungetcpuregister(list,r);
  546. end;
  547. procedure thlcgobj.alloccpuregisters(list: TAsmList; rt: Tregistertype; const r: Tcpuregisterset);
  548. begin
  549. cg.alloccpuregisters(list,rt,r);
  550. end;
  551. procedure thlcgobj.dealloccpuregisters(list: TAsmList; rt: Tregistertype; const r: Tcpuregisterset);
  552. begin
  553. cg.dealloccpuregisters(list,rt,r);
  554. end;
  555. procedure thlcgobj.allocallcpuregisters(list: TAsmList);
  556. begin
  557. cg.allocallcpuregisters(list);
  558. end;
  559. procedure thlcgobj.deallocallcpuregisters(list: TAsmList);
  560. begin
  561. cg.deallocallcpuregisters(list);
  562. end;
  563. procedure thlcgobj.do_register_allocation(list: TAsmList; headertai: tai);
  564. begin
  565. cg.do_register_allocation(list,headertai);
  566. end;
  567. procedure thlcgobj.translate_register(var reg: tregister);
  568. begin
  569. cg.translate_register(reg);
  570. end;
  571. function thlcgobj.def2regtyp(def: tdef): tregistertype;
  572. begin
  573. case def.typ of
  574. enumdef,
  575. orddef,
  576. recorddef,
  577. setdef:
  578. result:=R_INTREGISTER;
  579. stringdef,
  580. pointerdef,
  581. classrefdef,
  582. objectdef,
  583. procvardef,
  584. procdef,
  585. arraydef,
  586. formaldef:
  587. result:=R_ADDRESSREGISTER;
  588. floatdef:
  589. if use_vectorfpu(def) then
  590. result:=R_MMREGISTER
  591. else if cs_fp_emulation in current_settings.moduleswitches then
  592. result:=R_INTREGISTER
  593. else
  594. result:=R_FPUREGISTER;
  595. filedef,
  596. variantdef:
  597. internalerror(2010120507);
  598. else
  599. internalerror(2010120506);
  600. end;
  601. end;
  602. class function thlcgobj.tcgsize2orddef(size: tcgsize): torddef;
  603. begin
  604. case size of
  605. OS_8:
  606. result:=torddef(u8inttype);
  607. OS_S8:
  608. result:=torddef(s8inttype);
  609. OS_16:
  610. result:=torddef(u16inttype);
  611. OS_S16:
  612. result:=torddef(s16inttype);
  613. OS_32:
  614. result:=torddef(u32inttype);
  615. OS_S32:
  616. result:=torddef(s32inttype);
  617. OS_64:
  618. result:=torddef(u64inttype);
  619. OS_S64:
  620. result:=torddef(s64inttype);
  621. else
  622. internalerror(2012050401);
  623. end;
  624. end;
  625. procedure thlcgobj.a_label(list: TAsmList; l: tasmlabel); inline;
  626. begin
  627. cg.a_label(list,l);
  628. end;
  629. procedure thlcgobj.a_reg_alloc(list: TAsmList; r: tregister);
  630. begin
  631. cg.a_reg_alloc(list,r);
  632. end;
  633. procedure thlcgobj.a_reg_dealloc(list: TAsmList; r: tregister);
  634. begin
  635. cg.a_reg_dealloc(list,r);
  636. end;
  637. procedure thlcgobj.a_reg_sync(list: TAsmList; r: tregister);
  638. begin
  639. cg.a_reg_sync(list,r);
  640. end;
  641. procedure thlcgobj.a_load_reg_cgpara(list: TAsmList; size: tdef; r: tregister; const cgpara: TCGPara);
  642. var
  643. ref: treference;
  644. begin
  645. cgpara.check_simple_location;
  646. paramanager.alloccgpara(list,cgpara);
  647. case cgpara.location^.loc of
  648. LOC_REGISTER,LOC_CREGISTER:
  649. a_load_reg_reg(list,size,cgpara.def,r,cgpara.location^.register);
  650. LOC_REFERENCE,LOC_CREFERENCE:
  651. begin
  652. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  653. a_load_reg_ref(list,size,cgpara.def,r,ref);
  654. end;
  655. (*
  656. LOC_MMREGISTER,LOC_CMMREGISTER:
  657. a_loadmm_intreg_reg(list,size,cgpara.def,r,cgpara.location^.register,mms_movescalar);
  658. *)
  659. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  660. begin
  661. tg.gethltemp(list,size,size.size,tt_normal,ref);
  662. a_load_reg_ref(list,size,cgpara.def,r,ref);
  663. a_loadfpu_ref_cgpara(list,cgpara.def,ref,cgpara);
  664. tg.ungettemp(list,ref);
  665. end
  666. else
  667. internalerror(2010120415);
  668. end;
  669. end;
  670. procedure thlcgobj.a_load_const_cgpara(list: TAsmList; tosize: tdef; a: aint; const cgpara: TCGPara);
  671. var
  672. ref : treference;
  673. begin
  674. cgpara.check_simple_location;
  675. paramanager.alloccgpara(list,cgpara);
  676. case cgpara.location^.loc of
  677. LOC_REGISTER,LOC_CREGISTER:
  678. a_load_const_reg(list,cgpara.def,a,cgpara.location^.register);
  679. LOC_REFERENCE,LOC_CREFERENCE:
  680. begin
  681. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  682. a_load_const_ref(list,cgpara.def,a,ref);
  683. end
  684. else
  685. internalerror(2010120416);
  686. end;
  687. end;
  688. procedure thlcgobj.a_load_ref_cgpara(list: TAsmList; size: tdef; const r: treference; const cgpara: TCGPara);
  689. var
  690. ref: treference;
  691. begin
  692. cgpara.check_simple_location;
  693. paramanager.alloccgpara(list,cgpara);
  694. case cgpara.location^.loc of
  695. LOC_REGISTER,LOC_CREGISTER:
  696. a_load_ref_reg(list,size,cgpara.def,r,cgpara.location^.register);
  697. LOC_REFERENCE,LOC_CREFERENCE:
  698. begin
  699. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  700. a_load_ref_ref(list,size,cgpara.def,r,ref);
  701. end
  702. (*
  703. LOC_MMREGISTER,LOC_CMMREGISTER:
  704. begin
  705. case location^.size of
  706. OS_F32,
  707. OS_F64,
  708. OS_F128:
  709. a_loadmm_ref_reg(list,cgpara.def,cgpara.def,r,location^.register,mms_movescalar);
  710. OS_M8..OS_M128,
  711. OS_MS8..OS_MS128:
  712. a_loadmm_ref_reg(list,cgpara.def,cgpara.def,r,location^.register,nil);
  713. else
  714. internalerror(2010120417);
  715. end;
  716. end
  717. *)
  718. else
  719. internalerror(2010120418);
  720. end;
  721. end;
  722. procedure thlcgobj.a_load_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: TCGPara);
  723. begin
  724. case l.loc of
  725. LOC_REGISTER,
  726. LOC_CREGISTER :
  727. a_load_reg_cgpara(list,size,l.register,cgpara);
  728. LOC_CONSTANT :
  729. a_load_const_cgpara(list,size,l.value,cgpara);
  730. LOC_CREFERENCE,
  731. LOC_REFERENCE :
  732. a_load_ref_cgpara(list,size,l.reference,cgpara);
  733. else
  734. internalerror(2010120419);
  735. end;
  736. end;
  737. procedure thlcgobj.a_loadaddr_ref_cgpara(list: TAsmList; fromsize: tdef; const r: treference; const cgpara: TCGPara);
  738. var
  739. hr : tregister;
  740. begin
  741. cgpara.check_simple_location;
  742. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  743. begin
  744. paramanager.allocparaloc(list,cgpara.location);
  745. a_loadaddr_ref_reg(list,fromsize,cgpara.def,r,cgpara.location^.register)
  746. end
  747. else
  748. begin
  749. hr:=getaddressregister(list,cgpara.def);
  750. a_loadaddr_ref_reg(list,fromsize,cgpara.def,r,hr);
  751. a_load_reg_cgpara(list,cgpara.def,hr,cgpara);
  752. end;
  753. end;
  754. procedure thlcgobj.a_call_ref(list: TAsmList; pd: tabstractprocdef; const ref: treference);
  755. var
  756. reg: tregister;
  757. size: tdef;
  758. begin
  759. { the loaded data is always a pointer to a procdef. A procvardef is
  760. implicitly a pointer already, but a procdef isn't -> create one }
  761. if pd.typ=procvardef then
  762. size:=pd
  763. else
  764. size:=getpointerdef(pd);
  765. reg:=getaddressregister(list,size);
  766. a_load_ref_reg(list,size,size,ref,reg);
  767. a_call_reg(list,pd,reg);
  768. end;
  769. function thlcgobj.a_call_name_static(list: TAsmList; pd: tprocdef; const s: TSymStr; forceresdef: tdef): tcgpara;
  770. begin
  771. result:=a_call_name(list,pd,s,forceresdef,false);
  772. end;
  773. procedure thlcgobj.a_call_name_inherited(list: TAsmList; pd: tprocdef; const s: TSymStr);
  774. begin
  775. a_call_name(list,pd,s,nil,false);
  776. end;
  777. procedure thlcgobj.a_load_const_ref(list: TAsmList; tosize: tdef; a: aint; const ref: treference);
  778. var
  779. tmpreg: tregister;
  780. begin
  781. tmpreg:=getintregister(list,tosize);
  782. a_load_const_reg(list,tosize,a,tmpreg);
  783. a_load_reg_ref(list,tosize,tosize,tmpreg,ref);
  784. end;
  785. procedure thlcgobj.a_load_const_loc(list: TAsmList; tosize: tdef; a: aint; const loc: tlocation);
  786. begin
  787. case loc.loc of
  788. LOC_REFERENCE,LOC_CREFERENCE:
  789. a_load_const_ref(list,tosize,a,loc.reference);
  790. LOC_REGISTER,LOC_CREGISTER:
  791. a_load_const_reg(list,tosize,a,loc.register);
  792. LOC_SUBSETREG,LOC_CSUBSETREG:
  793. a_load_const_subsetreg(list,tosize,a,loc.sreg);
  794. LOC_SUBSETREF,LOC_CSUBSETREF:
  795. a_load_const_subsetref(list,tosize,a,loc.sref);
  796. else
  797. internalerror(2010120401);
  798. end;
  799. end;
  800. procedure thlcgobj.a_load_reg_ref_unaligned(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  801. begin
  802. a_load_reg_ref(list,fromsize,tosize,register,ref);
  803. end;
  804. procedure thlcgobj.a_load_reg_loc(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const loc: tlocation);
  805. begin
  806. case loc.loc of
  807. LOC_REFERENCE,LOC_CREFERENCE:
  808. a_load_reg_ref(list,fromsize,tosize,reg,loc.reference);
  809. LOC_REGISTER,LOC_CREGISTER:
  810. a_load_reg_reg(list,fromsize,tosize,reg,loc.register);
  811. LOC_SUBSETREG,LOC_CSUBSETREG:
  812. a_load_reg_subsetreg(list,fromsize,tosize,reg,loc.sreg);
  813. LOC_SUBSETREF,LOC_CSUBSETREF:
  814. a_load_reg_subsetref(list,fromsize,tosize,reg,loc.sref);
  815. { we don't have enough type information to handle these here
  816. LOC_MMREGISTER,LOC_CMMREGISTER:
  817. a_loadmm_intreg_reg(list,fromsize,loc.size,reg,loc.register,mms_movescalar);
  818. }
  819. else
  820. internalerror(2010120402);
  821. end;
  822. end;
  823. procedure thlcgobj.a_load_ref_reg_unaligned(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  824. begin
  825. a_load_ref_reg(list,fromsize,tosize,ref,register);
  826. end;
  827. procedure thlcgobj.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  828. var
  829. tmpreg: tregister;
  830. begin
  831. { verify if we have the same reference }
  832. if references_equal(sref,dref) then
  833. exit;
  834. tmpreg:=getintregister(list,tosize);
  835. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  836. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  837. end;
  838. procedure thlcgobj.a_load_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; reg: tregister);
  839. begin
  840. case loc.loc of
  841. LOC_REFERENCE,LOC_CREFERENCE:
  842. a_load_ref_reg(list,fromsize,tosize,loc.reference,reg);
  843. LOC_REGISTER,LOC_CREGISTER:
  844. a_load_reg_reg(list,fromsize,tosize,loc.register,reg);
  845. LOC_CONSTANT:
  846. a_load_const_reg(list,tosize,loc.value,reg);
  847. LOC_SUBSETREG,LOC_CSUBSETREG:
  848. a_load_subsetreg_reg(list,fromsize,tosize,loc.sreg,reg);
  849. LOC_SUBSETREF,LOC_CSUBSETREF:
  850. a_load_subsetref_reg(list,fromsize,tosize,loc.sref,reg);
  851. else
  852. internalerror(2010120201);
  853. end;
  854. end;
  855. procedure thlcgobj.a_load_loc_ref(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const ref: treference);
  856. begin
  857. case loc.loc of
  858. LOC_REFERENCE,LOC_CREFERENCE:
  859. a_load_ref_ref(list,fromsize,tosize,loc.reference,ref);
  860. LOC_REGISTER,LOC_CREGISTER:
  861. a_load_reg_ref(list,fromsize,tosize,loc.register,ref);
  862. LOC_CONSTANT:
  863. a_load_const_ref(list,tosize,loc.value,ref);
  864. LOC_SUBSETREG,LOC_CSUBSETREG:
  865. a_load_subsetreg_ref(list,fromsize,tosize,loc.sreg,ref);
  866. LOC_SUBSETREF,LOC_CSUBSETREF:
  867. a_load_subsetref_ref(list,fromsize,tosize,loc.sref,ref);
  868. else
  869. internalerror(2010120403);
  870. end;
  871. end;
  872. procedure thlcgobj.a_load_loc_subsetreg(list: TAsmList; fromsize, tosubsetsize: tdef; const loc: tlocation; const sreg: tsubsetregister);
  873. begin
  874. case loc.loc of
  875. LOC_REFERENCE,LOC_CREFERENCE:
  876. a_load_ref_subsetreg(list,fromsize,tosubsetsize,loc.reference,sreg);
  877. LOC_REGISTER,LOC_CREGISTER:
  878. a_load_reg_subsetreg(list,fromsize,tosubsetsize,loc.register,sreg);
  879. LOC_CONSTANT:
  880. a_load_const_subsetreg(list,tosubsetsize,loc.value,sreg);
  881. LOC_SUBSETREG,LOC_CSUBSETREG:
  882. a_load_subsetreg_subsetreg(list,fromsize,tosubsetsize,loc.sreg,sreg);
  883. LOC_SUBSETREF,LOC_CSUBSETREF:
  884. a_load_subsetref_subsetreg(list,fromsize,tosubsetsize,loc.sref,sreg);
  885. else
  886. internalerror(2010120404);
  887. end;
  888. end;
  889. procedure thlcgobj.a_load_loc_subsetref(list: TAsmList; fromsize, tosubsetsize: tdef; const loc: tlocation; const sref: tsubsetreference);
  890. begin
  891. case loc.loc of
  892. LOC_REFERENCE,LOC_CREFERENCE:
  893. a_load_ref_subsetref(list,fromsize,tosubsetsize,loc.reference,sref);
  894. LOC_REGISTER,LOC_CREGISTER:
  895. a_load_reg_subsetref(list,fromsize,tosubsetsize,loc.register,sref);
  896. LOC_CONSTANT:
  897. a_load_const_subsetref(list,tosubsetsize,loc.value,sref);
  898. LOC_SUBSETREG,LOC_CSUBSETREG:
  899. a_load_subsetreg_subsetref(list,fromsize,tosubsetsize,loc.sreg,sref);
  900. LOC_SUBSETREF,LOC_CSUBSETREF:
  901. a_load_subsetref_subsetref(list,fromsize,tosubsetsize,loc.sref,sref);
  902. else
  903. internalerror(2010120405);
  904. end;
  905. end;
  906. {$push}
  907. {$r-,q-}
  908. procedure thlcgobj.a_load_subsetreg_reg(list: TAsmList; subsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister);
  909. var
  910. subsetregdef: torddef;
  911. bitmask: aword;
  912. tmpreg,
  913. subsetsizereg: tregister;
  914. stopbit: byte;
  915. begin
  916. subsetregdef:=tcgsize2orddef(sreg.subsetregsize);
  917. tmpreg:=getintregister(list,subsetregdef);
  918. if is_signed(subsetsize) then
  919. begin
  920. { sign extend in case the value has a bitsize mod 8 <> 0 }
  921. { both instructions will be optimized away if not }
  922. a_op_const_reg_reg(list,OP_SHL,subsetregdef,(tcgsize2size[sreg.subsetregsize]*8)-sreg.startbit-sreg.bitlen,sreg.subsetreg,tmpreg);
  923. a_op_const_reg(list,OP_SAR,subsetregdef,(tcgsize2size[sreg.subsetregsize]*8)-sreg.bitlen,tmpreg);
  924. end
  925. else
  926. begin
  927. a_op_const_reg_reg(list,OP_SHR,subsetregdef,sreg.startbit,sreg.subsetreg,tmpreg);
  928. stopbit:=sreg.startbit+sreg.bitlen;
  929. // on x86(64), 1 shl 32(64) = 1 instead of 0
  930. // use aword to prevent overflow with 1 shl 31
  931. if (stopbit-sreg.startbit<>AIntBits) then
  932. bitmask:=(aword(1) shl (stopbit-sreg.startbit))-1
  933. else
  934. bitmask:=high(aword);
  935. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(bitmask),tmpreg);
  936. end;
  937. subsetsizereg:=getintregister(list,subsetsize);
  938. a_load_reg_reg(list,subsetregdef,subsetsize,tmpreg,subsetsizereg);
  939. a_load_reg_reg(list,subsetsize,tosize,subsetsizereg,destreg);
  940. end;
  941. procedure thlcgobj.a_load_reg_subsetreg(list: TAsmList; fromsize, tosubsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister);
  942. begin
  943. a_load_regconst_subsetreg_intern(list,fromsize,tosubsetsize,fromreg,sreg,SL_REG);
  944. end;
  945. procedure thlcgobj.a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsreg, tosreg: tsubsetregister);
  946. var
  947. fromsubsetregdef,
  948. tosubsetregdef: torddef;
  949. tmpreg, tmpreg2: tregister;
  950. bitmask: aword;
  951. stopbit: byte;
  952. begin
  953. if (fromsreg.bitlen>=tosreg.bitlen) then
  954. begin
  955. fromsubsetregdef:=tcgsize2orddef(fromsreg.subsetregsize);
  956. tosubsetregdef:=tcgsize2orddef(tosreg.subsetregsize);
  957. if (fromsreg.startbit<=tosreg.startbit) then
  958. begin
  959. { tosreg may be larger -> use its size to perform the shift }
  960. tmpreg:=getintregister(list,tosubsetregdef);
  961. a_load_reg_reg(list,fromsubsetregdef,tosubsetregdef,fromsreg.subsetreg,tmpreg);
  962. a_op_const_reg(list,OP_SHL,tosubsetregdef,tosreg.startbit-fromsreg.startbit,tmpreg)
  963. end
  964. else
  965. begin
  966. { fromsreg may be larger -> use its size to perform the shift }
  967. tmpreg:=getintregister(list,fromsubsetregdef);
  968. a_op_const_reg_reg(list,OP_SHR,fromsubsetregdef,fromsreg.startbit-tosreg.startbit,fromsreg.subsetreg,tmpreg);
  969. tmpreg2:=getintregister(list,tosubsetregdef);
  970. a_load_reg_reg(list,fromsubsetregdef,tosubsetregdef,tmpreg,tmpreg2);
  971. tmpreg:=tmpreg2;
  972. end;
  973. stopbit:=tosreg.startbit + tosreg.bitlen;
  974. // on x86(64), 1 shl 32(64) = 1 instead of 0
  975. if (stopbit<>AIntBits) then
  976. bitmask:=not(((aword(1) shl stopbit)-1) xor ((aword(1) shl tosreg.startbit)-1))
  977. else
  978. bitmask:=(aword(1) shl tosreg.startbit) - 1;
  979. a_op_const_reg(list,OP_AND,tosubsetregdef,tcgint(bitmask),tosreg.subsetreg);
  980. a_op_const_reg(list,OP_AND,tosubsetregdef,tcgint(not(bitmask)),tmpreg);
  981. a_op_reg_reg(list,OP_OR,tosubsetregdef,tmpreg,tosreg.subsetreg);
  982. end
  983. else
  984. begin
  985. tmpreg:=getintregister(list,tosubsetsize);
  986. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  987. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  988. end;
  989. end;
  990. procedure thlcgobj.a_load_subsetreg_ref(list: TAsmList; fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const destref: treference);
  991. var
  992. tmpreg: tregister;
  993. begin
  994. tmpreg:=getintregister(list,tosize);
  995. a_load_subsetreg_reg(list,fromsubsetsize,tosize,sreg,tmpreg);
  996. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  997. end;
  998. procedure thlcgobj.a_load_ref_subsetreg(list: TAsmList; fromsize, tosubsetsize: tdef; const fromref: treference; const sreg: tsubsetregister);
  999. var
  1000. tmpreg: tregister;
  1001. begin
  1002. tmpreg := getintregister(list,tosubsetsize);
  1003. a_load_ref_reg(list,fromsize,tosubsetsize,fromref,tmpreg);
  1004. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,sreg);
  1005. end;
  1006. procedure thlcgobj.a_load_const_subsetreg(list: TAsmlist; tosubsetsize: tdef; a: aint; const sreg: tsubsetregister);
  1007. var
  1008. subsetregdef: torddef;
  1009. bitmask: aword;
  1010. stopbit: byte;
  1011. begin
  1012. subsetregdef:=tcgsize2orddef(sreg.subsetregsize);
  1013. stopbit:=sreg.startbit+sreg.bitlen;
  1014. // on x86(64), 1 shl 32(64) = 1 instead of 0
  1015. if (stopbit<>AIntBits) then
  1016. bitmask:=not(((aword(1) shl stopbit)-1) xor ((aword(1) shl sreg.startbit)-1))
  1017. else
  1018. bitmask:=(aword(1) shl sreg.startbit)-1;
  1019. if (((aword(a) shl sreg.startbit) and not bitmask)<>not bitmask) then
  1020. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(bitmask),sreg.subsetreg);
  1021. a_op_const_reg(list,OP_OR,subsetregdef,tcgint((aword(a) shl sreg.startbit) and not(bitmask)),sreg.subsetreg);
  1022. end;
  1023. procedure thlcgobj.a_load_subsetreg_loc(list: TAsmlist; fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const loc: tlocation);
  1024. begin
  1025. case loc.loc of
  1026. LOC_REFERENCE,LOC_CREFERENCE:
  1027. a_load_subsetreg_ref(list,fromsubsetsize,tosize,sreg,loc.reference);
  1028. LOC_REGISTER,LOC_CREGISTER:
  1029. a_load_subsetreg_reg(list,fromsubsetsize,tosize,sreg,loc.register);
  1030. LOC_SUBSETREG,LOC_CSUBSETREG:
  1031. a_load_subsetreg_subsetreg(list,fromsubsetsize,tosize,sreg,loc.sreg);
  1032. LOC_SUBSETREF,LOC_CSUBSETREF:
  1033. a_load_subsetreg_subsetref(list,fromsubsetsize,tosize,sreg,loc.sref);
  1034. else
  1035. internalerror(2010120406);
  1036. end;
  1037. end;
  1038. procedure thlcgobj.a_load_subsetref_reg(list: TAsmList; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; destreg: tregister);
  1039. var
  1040. tmpref: treference;
  1041. valuereg,extra_value_reg: tregister;
  1042. tosreg: tsubsetregister;
  1043. loadsize: torddef;
  1044. loadbitsize: byte;
  1045. extra_load: boolean;
  1046. begin
  1047. get_subsetref_load_info(sref,loadsize,extra_load);
  1048. loadbitsize:=loadsize.size*8;
  1049. { load the (first part) of the bit sequence }
  1050. valuereg:=getintregister(list,osuinttype);
  1051. a_load_ref_reg(list,loadsize,osuinttype,sref.ref,valuereg);
  1052. if not extra_load then
  1053. begin
  1054. { everything is guaranteed to be in a single register of loadsize }
  1055. if (sref.bitindexreg=NR_NO) then
  1056. begin
  1057. { use subsetreg routine, it may have been overridden with an optimized version }
  1058. tosreg.subsetreg:=valuereg;
  1059. tosreg.subsetregsize:=def_cgsize(osuinttype);
  1060. { subsetregs always count bits from right to left }
  1061. if (target_info.endian=endian_big) then
  1062. tosreg.startbit:=loadbitsize-(sref.startbit+sref.bitlen)
  1063. else
  1064. tosreg.startbit:=sref.startbit;
  1065. tosreg.bitlen:=sref.bitlen;
  1066. a_load_subsetreg_reg(list,fromsubsetsize,tosize,tosreg,destreg);
  1067. exit;
  1068. end
  1069. else
  1070. begin
  1071. if (sref.startbit<>0) then
  1072. internalerror(2006081510);
  1073. if (target_info.endian=endian_big) then
  1074. begin
  1075. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,valuereg);
  1076. if is_signed(fromsubsetsize) then
  1077. begin
  1078. { sign extend to entire register }
  1079. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-loadbitsize,valuereg);
  1080. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1081. end
  1082. else
  1083. a_op_const_reg(list,OP_SHR,osuinttype,loadbitsize-sref.bitlen,valuereg);
  1084. end
  1085. else
  1086. begin
  1087. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,valuereg);
  1088. if is_signed(fromsubsetsize) then
  1089. begin
  1090. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-sref.bitlen,valuereg);
  1091. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1092. end
  1093. end;
  1094. { mask other bits/sign extend }
  1095. if not is_signed(fromsubsetsize) then
  1096. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1097. end
  1098. end
  1099. else
  1100. begin
  1101. { load next value as well }
  1102. extra_value_reg:=getintregister(list,osuinttype);
  1103. if (sref.bitindexreg=NR_NO) then
  1104. begin
  1105. tmpref:=sref.ref;
  1106. inc(tmpref.offset,loadbitsize div 8);
  1107. a_load_ref_reg(list,loadsize,osuinttype,tmpref,extra_value_reg);
  1108. { can be overridden to optimize }
  1109. a_load_subsetref_regs_noindex(list,fromsubsetsize,loadbitsize,sref,valuereg,extra_value_reg)
  1110. end
  1111. else
  1112. begin
  1113. if (sref.startbit<>0) then
  1114. internalerror(2006080610);
  1115. a_load_subsetref_regs_index(list,fromsubsetsize,loadbitsize,sref,valuereg);
  1116. end;
  1117. end;
  1118. { store in destination }
  1119. {$ifndef cpuhighleveltarget}
  1120. { avoid unnecessary sign extension and zeroing }
  1121. valuereg:=cg.makeregsize(list,valuereg,OS_INT);
  1122. destreg:=cg.makeregsize(list,destreg,OS_INT);
  1123. cg.a_load_reg_reg(list,OS_INT,OS_INT,valuereg,destreg);
  1124. destreg:=cg.makeregsize(list,destreg,def_cgsize(tosize));
  1125. {$else}
  1126. { can't juggle with register sizes, they are actually typed entities
  1127. here }
  1128. a_load_reg_reg(list,osuinttype,tosize,valuereg,destreg);
  1129. {$endif}
  1130. end;
  1131. procedure thlcgobj.a_load_reg_subsetref(list: TAsmList; fromsize, tosubsetsize: tdef; fromreg: tregister; const sref: tsubsetreference);
  1132. begin
  1133. a_load_regconst_subsetref_intern(list,fromsize,tosubsetsize,fromreg,sref,SL_REG);
  1134. end;
  1135. procedure thlcgobj.a_load_subsetref_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsref, tosref: tsubsetreference);
  1136. var
  1137. tmpreg: tregister;
  1138. begin
  1139. tmpreg:=getintregister(list,tosubsetsize);
  1140. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1141. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1142. end;
  1143. procedure thlcgobj.a_load_subsetref_ref(list: TAsmList; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const destref: treference);
  1144. var
  1145. tmpreg: tregister;
  1146. begin
  1147. tmpreg:=getintregister(list,tosize);
  1148. a_load_subsetref_reg(list,fromsubsetsize,tosize,sref,tmpreg);
  1149. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  1150. end;
  1151. procedure thlcgobj.a_load_ref_subsetref(list: TAsmList; fromsize, tosubsetsize: tdef; const fromref: treference; const sref: tsubsetreference);
  1152. var
  1153. tmpreg: tregister;
  1154. begin
  1155. tmpreg := getintregister(list,tosubsetsize);
  1156. a_load_ref_reg(list,fromsize,tosubsetsize,fromref,tmpreg);
  1157. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,sref);
  1158. end;
  1159. procedure thlcgobj.a_load_const_subsetref(list: TAsmlist; tosubsetsize: tdef; a: tcgint; const sref: tsubsetreference);
  1160. var
  1161. tmpreg: tregister;
  1162. slopt: tsubsetloadopt;
  1163. begin
  1164. { perform masking of the source value in advance }
  1165. slopt:=SL_REGNOSRCMASK;
  1166. if (sref.bitlen<>AIntBits) then
  1167. a:=tcgint(aword(a) and ((aword(1) shl sref.bitlen) -1));
  1168. if (
  1169. { broken x86 "x shl regbitsize = x" }
  1170. ((sref.bitlen<>AIntBits) and
  1171. ((aword(a) and ((aword(1) shl sref.bitlen)-1))=(aword(1) shl sref.bitlen)-1)) or
  1172. ((sref.bitlen=AIntBits) and
  1173. (a=-1))
  1174. ) then
  1175. slopt:=SL_SETMAX
  1176. else if (a=0) then
  1177. slopt:=SL_SETZERO;
  1178. if not(slopt in [SL_SETZERO,SL_SETMAX]) then
  1179. begin
  1180. tmpreg:=getintregister(list,tosubsetsize);
  1181. a_load_const_reg(list,tosubsetsize,a,tmpreg);
  1182. end
  1183. else
  1184. tmpreg:=NR_NO;
  1185. a_load_regconst_subsetref_intern(list,tosubsetsize,tosubsetsize,tmpreg,sref,slopt);
  1186. end;
  1187. procedure thlcgobj.a_load_subsetref_loc(list: TAsmlist; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const loc: tlocation);
  1188. begin
  1189. case loc.loc of
  1190. LOC_REFERENCE,LOC_CREFERENCE:
  1191. a_load_subsetref_ref(list,fromsubsetsize,tosize,sref,loc.reference);
  1192. LOC_REGISTER,LOC_CREGISTER:
  1193. a_load_subsetref_reg(list,fromsubsetsize,tosize,sref,loc.register);
  1194. LOC_SUBSETREG,LOC_CSUBSETREG:
  1195. a_load_subsetref_subsetreg(list,fromsubsetsize,tosize,sref,loc.sreg);
  1196. LOC_SUBSETREF,LOC_CSUBSETREF:
  1197. a_load_subsetref_subsetref(list,fromsubsetsize,tosize,sref,loc.sref);
  1198. else
  1199. internalerror(2010120407);
  1200. end;
  1201. end;
  1202. procedure thlcgobj.a_load_subsetref_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsref: tsubsetreference; const tosreg: tsubsetregister);
  1203. var
  1204. tmpreg: tregister;
  1205. begin
  1206. tmpreg:=getintregister(list,tosubsetsize);
  1207. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1208. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  1209. end;
  1210. procedure thlcgobj.a_load_subsetreg_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsreg: tsubsetregister; const tosref: tsubsetreference);
  1211. var
  1212. tmpreg: tregister;
  1213. begin
  1214. tmpreg := getintregister(list,tosubsetsize);
  1215. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  1216. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1217. end;
  1218. procedure thlcgobj.a_bit_test_reg_reg_reg(list: TAsmList; bitnumbersize, valuesize, destsize: tdef; bitnumber, value, destreg: tregister);
  1219. var
  1220. tmpvalue: tregister;
  1221. begin
  1222. tmpvalue:=getintregister(list,valuesize);
  1223. if (target_info.endian=endian_little) then
  1224. begin
  1225. { rotate value register "bitnumber" bits to the right }
  1226. a_op_reg_reg_reg(list,OP_SHR,valuesize,bitnumber,value,tmpvalue);
  1227. { extract the bit we want }
  1228. a_op_const_reg(list,OP_AND,valuesize,1,tmpvalue);
  1229. end
  1230. else
  1231. begin
  1232. { highest (leftmost) bit = bit 0 -> shl bitnumber results in wanted }
  1233. { bit in uppermost position, then move it to the lowest position }
  1234. { "and" is not necessary since combination of shl/shr will clear }
  1235. { all other bits }
  1236. a_op_reg_reg_reg(list,OP_SHL,valuesize,bitnumber,value,tmpvalue);
  1237. a_op_const_reg(list,OP_SHR,valuesize,valuesize.size*8-1,tmpvalue);
  1238. end;
  1239. a_load_reg_reg(list,valuesize,destsize,tmpvalue,destreg);
  1240. end;
  1241. procedure thlcgobj.a_bit_test_const_ref_reg(list: TAsmList; fromsize, destsize: tdef; bitnumber: aint; const ref: treference; destreg: tregister);
  1242. begin
  1243. a_load_subsetref_reg(list,u8inttype,destsize,get_bit_const_ref_sref(bitnumber,fromsize,ref),destreg);
  1244. end;
  1245. procedure thlcgobj.a_bit_test_const_reg_reg(list: TAsmList; setregsize, destsize: tdef; bitnumber: aint; setreg, destreg: tregister);
  1246. begin
  1247. a_load_subsetreg_reg(list,setregsize,destsize,get_bit_const_reg_sreg(setregsize,bitnumber,setreg),destreg);
  1248. end;
  1249. procedure thlcgobj.a_bit_test_const_subsetreg_reg(list: TAsmList; fromsubsetsize, destsize: tdef; bitnumber: aint; const setreg: tsubsetregister; destreg: tregister);
  1250. var
  1251. tmpsreg: tsubsetregister;
  1252. begin
  1253. { the first parameter is used to calculate the bit offset in }
  1254. { case of big endian, and therefore must be the size of the }
  1255. { set and not of the whole subsetreg }
  1256. tmpsreg:=get_bit_const_reg_sreg(fromsubsetsize,bitnumber,setreg.subsetreg);
  1257. { now fix the size of the subsetreg }
  1258. tmpsreg.subsetregsize:=setreg.subsetregsize;
  1259. { correct offset of the set in the subsetreg }
  1260. inc(tmpsreg.startbit,setreg.startbit);
  1261. a_load_subsetreg_reg(list,fromsubsetsize,destsize,tmpsreg,destreg);
  1262. end;
  1263. procedure thlcgobj.a_bit_test_reg_ref_reg(list: TAsmList; bitnumbersize, refsize, destsize: tdef; bitnumber: tregister; const ref: treference; destreg: tregister);
  1264. begin
  1265. a_load_subsetref_reg(list,u8inttype,destsize,get_bit_reg_ref_sref(list,bitnumbersize,refsize,bitnumber,ref),destreg);
  1266. end;
  1267. procedure thlcgobj.a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, locsize, destsize: tdef; bitnumber: tregister; const loc: tlocation; destreg: tregister);
  1268. var
  1269. tmpreg: tregister;
  1270. begin
  1271. case loc.loc of
  1272. LOC_REFERENCE,LOC_CREFERENCE:
  1273. a_bit_test_reg_ref_reg(list,bitnumbersize,locsize,destsize,bitnumber,loc.reference,destreg);
  1274. LOC_REGISTER,LOC_CREGISTER,
  1275. LOC_SUBSETREG,LOC_CSUBSETREG,
  1276. LOC_CONSTANT:
  1277. begin
  1278. case loc.loc of
  1279. LOC_REGISTER,LOC_CREGISTER:
  1280. tmpreg:=loc.register;
  1281. LOC_SUBSETREG,LOC_CSUBSETREG:
  1282. begin
  1283. tmpreg:=getintregister(list,locsize);
  1284. a_load_subsetreg_reg(list,locsize,locsize,loc.sreg,tmpreg);
  1285. end;
  1286. LOC_CONSTANT:
  1287. begin
  1288. tmpreg:=getintregister(list,locsize);
  1289. a_load_const_reg(list,locsize,loc.value,tmpreg);
  1290. end;
  1291. end;
  1292. a_bit_test_reg_reg_reg(list,bitnumbersize,locsize,destsize,bitnumber,tmpreg,destreg);
  1293. end;
  1294. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1295. else
  1296. internalerror(2010120411);
  1297. end;
  1298. end;
  1299. procedure thlcgobj.a_bit_test_const_loc_reg(list: TAsmList; locsize, destsize: tdef; bitnumber: aint; const loc: tlocation; destreg: tregister);
  1300. begin
  1301. case loc.loc of
  1302. LOC_REFERENCE,LOC_CREFERENCE:
  1303. a_bit_test_const_ref_reg(list,locsize,destsize,bitnumber,loc.reference,destreg);
  1304. LOC_REGISTER,LOC_CREGISTER:
  1305. a_bit_test_const_reg_reg(list,locsize,destsize,bitnumber,loc.register,destreg);
  1306. LOC_SUBSETREG,LOC_CSUBSETREG:
  1307. a_bit_test_const_subsetreg_reg(list,locsize,destsize,bitnumber,loc.sreg,destreg);
  1308. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1309. else
  1310. internalerror(2010120410);
  1311. end;
  1312. end;
  1313. procedure thlcgobj.a_bit_set_reg_reg(list: TAsmList; doset: boolean; bitnumbersize, destsize: tdef; bitnumber, dest: tregister);
  1314. var
  1315. tmpvalue: tregister;
  1316. begin
  1317. tmpvalue:=getintregister(list,destsize);
  1318. if (target_info.endian=endian_little) then
  1319. begin
  1320. a_load_const_reg(list,destsize,1,tmpvalue);
  1321. { rotate bit "bitnumber" bits to the left }
  1322. a_op_reg_reg(list,OP_SHL,destsize,bitnumber,tmpvalue);
  1323. end
  1324. else
  1325. begin
  1326. { highest (leftmost) bit = bit 0 -> "$80/$8000/$80000000/ ... }
  1327. { shr bitnumber" results in correct mask }
  1328. a_load_const_reg(list,destsize,1 shl (destsize.size*8-1),tmpvalue);
  1329. a_op_reg_reg(list,OP_SHR,destsize,bitnumber,tmpvalue);
  1330. end;
  1331. { set/clear the bit we want }
  1332. if doset then
  1333. a_op_reg_reg(list,OP_OR,destsize,tmpvalue,dest)
  1334. else
  1335. begin
  1336. a_op_reg_reg(list,OP_NOT,destsize,tmpvalue,tmpvalue);
  1337. a_op_reg_reg(list,OP_AND,destsize,tmpvalue,dest)
  1338. end;
  1339. end;
  1340. procedure thlcgobj.a_bit_set_const_ref(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; const ref: treference);
  1341. begin
  1342. a_load_const_subsetref(list,u8inttype,ord(doset),get_bit_const_ref_sref(bitnumber,destsize,ref));
  1343. end;
  1344. procedure thlcgobj.a_bit_set_const_reg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; destreg: tregister);
  1345. begin
  1346. a_load_const_subsetreg(list,u8inttype,ord(doset),get_bit_const_reg_sreg(destsize,bitnumber,destreg));
  1347. end;
  1348. procedure thlcgobj.a_bit_set_const_subsetreg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; const destreg: tsubsetregister);
  1349. var
  1350. tmpsreg: tsubsetregister;
  1351. begin
  1352. { the first parameter is used to calculate the bit offset in }
  1353. { case of big endian, and therefore must be the size of the }
  1354. { set and not of the whole subsetreg }
  1355. tmpsreg:=get_bit_const_reg_sreg(destsize,bitnumber,destreg.subsetreg);
  1356. { now fix the size of the subsetreg }
  1357. tmpsreg.subsetregsize:=destreg.subsetregsize;
  1358. { correct offset of the set in the subsetreg }
  1359. inc(tmpsreg.startbit,destreg.startbit);
  1360. a_load_const_subsetreg(list,u8inttype,ord(doset),tmpsreg);
  1361. end;
  1362. procedure thlcgobj.a_bit_set_reg_ref(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const ref: treference);
  1363. begin
  1364. a_load_const_subsetref(list,u8inttype,ord(doset),get_bit_reg_ref_sref(list,fromsize,tosize,bitnumber,ref));
  1365. end;
  1366. procedure thlcgobj.a_bit_set_reg_loc(list: TAsmList; doset: boolean; regsize, tosize: tdef; bitnumber: tregister; const loc: tlocation);
  1367. var
  1368. tmpreg: tregister;
  1369. begin
  1370. case loc.loc of
  1371. LOC_REFERENCE:
  1372. a_bit_set_reg_ref(list,doset,regsize,tosize,bitnumber,loc.reference);
  1373. LOC_CREGISTER:
  1374. a_bit_set_reg_reg(list,doset,regsize,tosize,bitnumber,loc.register);
  1375. { e.g. a 2-byte set in a record regvar }
  1376. LOC_CSUBSETREG:
  1377. begin
  1378. { hard to do in-place in a generic way, so operate on a copy }
  1379. tmpreg:=getintregister(list,tosize);
  1380. a_load_subsetreg_reg(list,tosize,tosize,loc.sreg,tmpreg);
  1381. a_bit_set_reg_reg(list,doset,regsize,tosize,bitnumber,tmpreg);
  1382. a_load_reg_subsetreg(list,tosize,tosize,tmpreg,loc.sreg);
  1383. end;
  1384. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1385. else
  1386. internalerror(2010120408)
  1387. end;
  1388. end;
  1389. procedure thlcgobj.a_bit_set_const_loc(list: TAsmList; doset: boolean; tosize: tdef; bitnumber: aint; const loc: tlocation);
  1390. begin
  1391. case loc.loc of
  1392. LOC_REFERENCE:
  1393. a_bit_set_const_ref(list,doset,tosize,bitnumber,loc.reference);
  1394. LOC_CREGISTER:
  1395. a_bit_set_const_reg(list,doset,tosize,bitnumber,loc.register);
  1396. LOC_CSUBSETREG:
  1397. a_bit_set_const_subsetreg(list,doset,tosize,bitnumber,loc.sreg);
  1398. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1399. else
  1400. internalerror(2010120409)
  1401. end;
  1402. end;
  1403. function thlcgobj.get_call_result_cgpara(pd: tprocdef; forceresdef: tdef): tcgpara;
  1404. begin
  1405. if not assigned(forceresdef) then
  1406. begin
  1407. pd.init_paraloc_info(callerside);
  1408. result:=pd.funcretloc[callerside];
  1409. end
  1410. else
  1411. result:=paramanager.get_funcretloc(pd,callerside,forceresdef);
  1412. end;
  1413. (*
  1414. Subsetrefs are used for (bit)packed arrays and (bit)packed records stored
  1415. in memory. They are like a regular reference, but contain an extra bit
  1416. offset (either constant -startbit- or variable -bitindexreg-, always OS_INT)
  1417. and a bit length (always constant).
  1418. Bit packed values are stored differently in memory depending on whether we
  1419. are on a big or a little endian system (compatible with at least GPC). The
  1420. size of the basic working unit is always the smallest power-of-2 byte size
  1421. which can contain the bit value (so 1..8 bits -> 1 byte, 9..16 bits -> 2
  1422. bytes, 17..32 bits -> 4 bytes etc).
  1423. On a big endian, 5-bit: values are stored like this:
  1424. 11111222 22333334 44445555 56666677 77788888
  1425. The leftmost bit of each 5-bit value corresponds to the most significant
  1426. bit.
  1427. On little endian, it goes like this:
  1428. 22211111 43333322 55554444 77666665 88888777
  1429. In this case, per byte the left-most bit is more significant than those on
  1430. the right, but the bits in the next byte are all more significant than
  1431. those in the previous byte (e.g., the 222 in the first byte are the low
  1432. three bits of that value, while the 22 in the second byte are the upper
  1433. two bits.
  1434. Big endian, 9 bit values:
  1435. 11111111 12222222 22333333 33344444 ...
  1436. Little endian, 9 bit values:
  1437. 11111111 22222221 33333322 44444333 ...
  1438. This is memory representation and the 16 bit values are byteswapped.
  1439. Similarly as in the previous case, the 2222222 string contains the lower
  1440. bits of value 2 and the 22 string contains the upper bits. Once loaded into
  1441. registers (two 16 bit registers in the current implementation, although a
  1442. single 32 bit register would be possible too, in particular if 32 bit
  1443. alignment can be guaranteed), this becomes:
  1444. 22222221 11111111 44444333 33333322 ...
  1445. (l)ow u l l u l u
  1446. The startbit/bitindex in a subsetreference always refers to
  1447. a) on big endian: the most significant bit of the value
  1448. (bits counted from left to right, both memory an registers)
  1449. b) on little endian: the least significant bit when the value
  1450. is loaded in a register (bit counted from right to left)
  1451. Although a) results in more complex code for big endian systems, it's
  1452. needed for compatibility both with GPC and with e.g. bitpacked arrays in
  1453. Apple's universal interfaces which depend on these layout differences).
  1454. Note: when changing the loadsize calculated in get_subsetref_load_info,
  1455. make sure the appropriate alignment is guaranteed, at least in case of
  1456. {$defined cpurequiresproperalignment}.
  1457. *)
  1458. procedure thlcgobj.get_subsetref_load_info(const sref: tsubsetreference; out loadsize: torddef; out extra_load: boolean);
  1459. var
  1460. intloadsize: tcgint;
  1461. begin
  1462. intloadsize:=packedbitsloadsize(sref.bitlen);
  1463. if (intloadsize=0) then
  1464. internalerror(2006081310);
  1465. if (intloadsize>sizeof(aint)) then
  1466. intloadsize:=sizeof(aint);
  1467. loadsize:=tcgsize2orddef(int_cgsize(intloadsize));
  1468. if (sref.bitlen>sizeof(aint)*8) then
  1469. internalerror(2006081312);
  1470. extra_load:=
  1471. (sref.bitlen<>1) and
  1472. ((sref.bitindexreg<>NR_NO) or
  1473. (byte(sref.startbit+sref.bitlen)>byte(intloadsize*8)));
  1474. end;
  1475. procedure thlcgobj.a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister);
  1476. var
  1477. restbits: byte;
  1478. begin
  1479. if (target_info.endian=endian_big) then
  1480. begin
  1481. { valuereg contains the upper bits, extra_value_reg the lower }
  1482. restbits:=(sref.bitlen-(loadbitsize-sref.startbit));
  1483. if is_signed(subsetsize) then
  1484. begin
  1485. { sign extend }
  1486. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-loadbitsize+sref.startbit,valuereg);
  1487. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1488. end
  1489. else
  1490. begin
  1491. a_op_const_reg(list,OP_SHL,osuinttype,restbits,valuereg);
  1492. { mask other bits }
  1493. if (sref.bitlen<>AIntBits) then
  1494. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1495. end;
  1496. a_op_const_reg(list,OP_SHR,osuinttype,loadbitsize-restbits,extra_value_reg)
  1497. end
  1498. else
  1499. begin
  1500. { valuereg contains the lower bits, extra_value_reg the upper }
  1501. a_op_const_reg(list,OP_SHR,osuinttype,sref.startbit,valuereg);
  1502. if is_signed(subsetsize) then
  1503. begin
  1504. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-sref.bitlen+loadbitsize-sref.startbit,extra_value_reg);
  1505. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,extra_value_reg);
  1506. end
  1507. else
  1508. begin
  1509. a_op_const_reg(list,OP_SHL,osuinttype,loadbitsize-sref.startbit,extra_value_reg);
  1510. { mask other bits }
  1511. if (sref.bitlen <> AIntBits) then
  1512. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),extra_value_reg);
  1513. end;
  1514. end;
  1515. { merge }
  1516. a_op_reg_reg(list,OP_OR,osuinttype,extra_value_reg,valuereg);
  1517. end;
  1518. procedure thlcgobj.a_load_subsetref_regs_index(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg: tregister);
  1519. var
  1520. hl: tasmlabel;
  1521. tmpref: treference;
  1522. extra_value_reg,
  1523. tmpreg: tregister;
  1524. begin
  1525. tmpreg:=getintregister(list,osuinttype);
  1526. tmpref:=sref.ref;
  1527. inc(tmpref.offset,loadbitsize div 8);
  1528. extra_value_reg:=getintregister(list,osuinttype);
  1529. if (target_info.endian=endian_big) then
  1530. begin
  1531. { since this is a dynamic index, it's possible that the value }
  1532. { is entirely in valuereg. }
  1533. { get the data in valuereg in the right place }
  1534. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,valuereg);
  1535. if is_signed(subsetsize) then
  1536. begin
  1537. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-loadbitsize,valuereg);
  1538. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg)
  1539. end
  1540. else
  1541. begin
  1542. a_op_const_reg(list,OP_SHR,osuinttype,loadbitsize-sref.bitlen,valuereg);
  1543. if (loadbitsize<>AIntBits) then
  1544. { mask left over bits }
  1545. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1546. end;
  1547. tmpreg := getintregister(list,osuinttype);
  1548. { ensure we don't load anything past the end of the array }
  1549. current_asmdata.getjumplabel(hl);
  1550. a_cmp_const_reg_label(list,osuinttype,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  1551. { the bits in extra_value_reg (if any) start at the most significant bit => }
  1552. { extra_value_reg must be shr by (loadbitsize-sref.bitlen)+(loadsize-sref.bitindex) }
  1553. { => = -(sref.bitindex+(sref.bitlen-2*loadbitsize)) }
  1554. a_op_const_reg_reg(list,OP_ADD,osuinttype,sref.bitlen-2*loadbitsize,sref.bitindexreg,tmpreg);
  1555. a_op_reg_reg(list,OP_NEG,osuinttype,tmpreg,tmpreg);
  1556. { load next "loadbitsize" bits of the array }
  1557. a_load_ref_reg(list,tcgsize2orddef(int_cgsize(loadbitsize div 8)),osuinttype,tmpref,extra_value_reg);
  1558. a_op_reg_reg(list,OP_SHR,osuinttype,tmpreg,extra_value_reg);
  1559. { if there are no bits in extra_value_reg, then sref.bitindex was }
  1560. { < loadsize-sref.bitlen, and therefore tmpreg will now be >= loadsize }
  1561. { => extra_value_reg is now 0 }
  1562. { merge }
  1563. a_op_reg_reg(list,OP_OR,osuinttype,extra_value_reg,valuereg);
  1564. { no need to mask, necessary masking happened earlier on }
  1565. a_label(list,hl);
  1566. end
  1567. else
  1568. begin
  1569. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,valuereg);
  1570. { ensure we don't load anything past the end of the array }
  1571. current_asmdata.getjumplabel(hl);
  1572. a_cmp_const_reg_label(list,osuinttype,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  1573. { Y-x = -(Y-x) }
  1574. a_op_const_reg_reg(list,OP_SUB,osuinttype,loadbitsize,sref.bitindexreg,tmpreg);
  1575. a_op_reg_reg(list,OP_NEG,osuinttype,tmpreg,tmpreg);
  1576. { load next "loadbitsize" bits of the array }
  1577. a_load_ref_reg(list,tcgsize2orddef(int_cgsize(loadbitsize div 8)),osuinttype,tmpref,extra_value_reg);
  1578. { tmpreg is in the range 1..<cpu_bitsize>-1 -> always ok }
  1579. a_op_reg_reg(list,OP_SHL,osuinttype,tmpreg,extra_value_reg);
  1580. { merge }
  1581. a_op_reg_reg(list,OP_OR,osuinttype,extra_value_reg,valuereg);
  1582. a_label(list,hl);
  1583. { sign extend or mask other bits }
  1584. if is_signed(subsetsize) then
  1585. begin
  1586. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-sref.bitlen,valuereg);
  1587. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1588. end
  1589. else
  1590. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1591. end;
  1592. end;
  1593. procedure thlcgobj.a_load_regconst_subsetref_intern(list: TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sref: tsubsetreference; slopt: tsubsetloadopt);
  1594. var
  1595. hl: tasmlabel;
  1596. tmpreg, tmpindexreg, valuereg, extra_value_reg, maskreg: tregister;
  1597. tosreg, fromsreg: tsubsetregister;
  1598. tmpref: treference;
  1599. bitmask: aword;
  1600. loadsize: torddef;
  1601. loadbitsize: byte;
  1602. extra_load: boolean;
  1603. begin
  1604. { the register must be able to contain the requested value }
  1605. if (fromsize.size*8<sref.bitlen) then
  1606. internalerror(2006081613);
  1607. get_subsetref_load_info(sref,loadsize,extra_load);
  1608. loadbitsize:=loadsize.size*8;
  1609. { load the (first part) of the bit sequence }
  1610. valuereg:=getintregister(list,osuinttype);
  1611. a_load_ref_reg(list,loadsize,osuinttype,sref.ref,valuereg);
  1612. { constant offset of bit sequence? }
  1613. if not extra_load then
  1614. begin
  1615. if (sref.bitindexreg=NR_NO) then
  1616. begin
  1617. { use subsetreg routine, it may have been overridden with an optimized version }
  1618. tosreg.subsetreg:=valuereg;
  1619. tosreg.subsetregsize:=def_cgsize(osuinttype);
  1620. { subsetregs always count bits from right to left }
  1621. if (target_info.endian=endian_big) then
  1622. tosreg.startbit:=loadbitsize-(sref.startbit+sref.bitlen)
  1623. else
  1624. tosreg.startbit:=sref.startbit;
  1625. tosreg.bitlen:=sref.bitlen;
  1626. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1627. end
  1628. else
  1629. begin
  1630. if (sref.startbit<>0) then
  1631. internalerror(2006081710);
  1632. { should be handled by normal code and will give wrong result }
  1633. { on x86 for the '1 shl bitlen' below }
  1634. if (sref.bitlen=AIntBits) then
  1635. internalerror(2006081711);
  1636. { zero the bits we have to insert }
  1637. if (slopt<>SL_SETMAX) then
  1638. begin
  1639. maskreg:=getintregister(list,osuinttype);
  1640. if (target_info.endian = endian_big) then
  1641. begin
  1642. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen),maskreg);
  1643. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,maskreg);
  1644. end
  1645. else
  1646. begin
  1647. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1648. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,maskreg);
  1649. end;
  1650. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  1651. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,valuereg);
  1652. end;
  1653. { insert the value }
  1654. if (slopt<>SL_SETZERO) then
  1655. begin
  1656. tmpreg:=getintregister(list,osuinttype);
  1657. if (slopt<>SL_SETMAX) then
  1658. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  1659. else if (sref.bitlen<>AIntBits) then
  1660. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1), tmpreg)
  1661. else
  1662. a_load_const_reg(list,osuinttype,-1,tmpreg);
  1663. if (target_info.endian=endian_big) then
  1664. begin
  1665. a_op_const_reg(list,OP_SHL,osuinttype,loadbitsize-sref.bitlen,tmpreg);
  1666. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1667. begin
  1668. if (loadbitsize<>AIntBits) then
  1669. bitmask:=(((aword(1) shl loadbitsize)-1) xor ((aword(1) shl (loadbitsize-sref.bitlen))-1))
  1670. else
  1671. bitmask:=(high(aword) xor ((aword(1) shl (loadbitsize-sref.bitlen))-1));
  1672. a_op_const_reg(list,OP_AND,osuinttype,bitmask,tmpreg);
  1673. end;
  1674. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,tmpreg);
  1675. end
  1676. else
  1677. begin
  1678. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1679. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1680. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,tmpreg);
  1681. end;
  1682. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,valuereg);
  1683. end;
  1684. end;
  1685. { store back to memory }
  1686. tmpreg:=getintregister(list,loadsize);
  1687. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  1688. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  1689. exit;
  1690. end
  1691. else
  1692. begin
  1693. { load next value }
  1694. extra_value_reg:=getintregister(list,osuinttype);
  1695. tmpref:=sref.ref;
  1696. inc(tmpref.offset,loadbitsize div 8);
  1697. { should maybe be taken out too, can be done more efficiently }
  1698. { on e.g. i386 with shld/shrd }
  1699. if (sref.bitindexreg = NR_NO) then
  1700. begin
  1701. a_load_ref_reg(list,loadsize,osuinttype,tmpref,extra_value_reg);
  1702. fromsreg.subsetreg:=fromreg;
  1703. fromsreg.subsetregsize:=def_cgsize(fromsize);
  1704. tosreg.subsetreg:=valuereg;
  1705. tosreg.subsetregsize:=def_cgsize(osuinttype);
  1706. { transfer first part }
  1707. fromsreg.bitlen:=loadbitsize-sref.startbit;
  1708. tosreg.bitlen:=fromsreg.bitlen;
  1709. if (target_info.endian=endian_big) then
  1710. begin
  1711. { valuereg must contain the upper bits of the value at bits [0..loadbitsize-startbit] }
  1712. { upper bits of the value ... }
  1713. fromsreg.startbit:=sref.bitlen-(loadbitsize-sref.startbit);
  1714. { ... to bit 0 }
  1715. tosreg.startbit:=0
  1716. end
  1717. else
  1718. begin
  1719. { valuereg must contain the lower bits of the value at bits [startbit..loadbitsize] }
  1720. { lower bits of the value ... }
  1721. fromsreg.startbit:=0;
  1722. { ... to startbit }
  1723. tosreg.startbit:=sref.startbit;
  1724. end;
  1725. case slopt of
  1726. SL_SETZERO,
  1727. SL_SETMAX:
  1728. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1729. else
  1730. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1731. end;
  1732. {$ifndef cpuhighleveltarget}
  1733. valuereg:=cg.makeregsize(list,valuereg,def_cgsize(loadsize));
  1734. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1735. {$else}
  1736. tmpreg:=getintregister(list,loadsize);
  1737. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  1738. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  1739. {$endif}
  1740. { transfer second part }
  1741. if (target_info.endian = endian_big) then
  1742. begin
  1743. { extra_value_reg must contain the lower bits of the value at bits }
  1744. { [(loadbitsize-(bitlen-(loadbitsize-startbit)))..loadbitsize] }
  1745. { (loadbitsize-(bitlen-(loadbitsize-startbit))) = 2*loadbitsize }
  1746. { - bitlen - startbit }
  1747. fromsreg.startbit:=0;
  1748. tosreg.startbit:=2*loadbitsize-sref.bitlen-sref.startbit
  1749. end
  1750. else
  1751. begin
  1752. { extra_value_reg must contain the upper bits of the value at bits [0..bitlen-(loadbitsize-startbit)] }
  1753. fromsreg.startbit:=fromsreg.bitlen;
  1754. tosreg.startbit:=0;
  1755. end;
  1756. tosreg.subsetreg:=extra_value_reg;
  1757. fromsreg.bitlen:=sref.bitlen-fromsreg.bitlen;
  1758. tosreg.bitlen:=fromsreg.bitlen;
  1759. case slopt of
  1760. SL_SETZERO,
  1761. SL_SETMAX:
  1762. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1763. else
  1764. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1765. end;
  1766. tmpreg:=getintregister(list,loadsize);
  1767. a_load_reg_reg(list,osuinttype,loadsize,extra_value_reg,tmpreg);
  1768. a_load_reg_ref(list,loadsize,loadsize,tmpreg,tmpref);
  1769. exit;
  1770. end
  1771. else
  1772. begin
  1773. if (sref.startbit <> 0) then
  1774. internalerror(2006081812);
  1775. { should be handled by normal code and will give wrong result }
  1776. { on x86 for the '1 shl bitlen' below }
  1777. if (sref.bitlen = AIntBits) then
  1778. internalerror(2006081713);
  1779. { generate mask to zero the bits we have to insert }
  1780. if (slopt <> SL_SETMAX) then
  1781. begin
  1782. maskreg := getintregister(list,osuinttype);
  1783. if (target_info.endian = endian_big) then
  1784. begin
  1785. a_load_const_reg(list,osuinttype,tcgint(((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen)),maskreg);
  1786. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,maskreg);
  1787. end
  1788. else
  1789. begin
  1790. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1791. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,maskreg);
  1792. end;
  1793. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  1794. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,valuereg);
  1795. end;
  1796. { insert the value }
  1797. if (slopt <> SL_SETZERO) then
  1798. begin
  1799. tmpreg := getintregister(list,osuinttype);
  1800. if (slopt <> SL_SETMAX) then
  1801. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  1802. else if (sref.bitlen <> AIntBits) then
  1803. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  1804. else
  1805. a_load_const_reg(list,osuinttype,-1,tmpreg);
  1806. if (target_info.endian = endian_big) then
  1807. begin
  1808. a_op_const_reg(list,OP_SHL,osuinttype,loadbitsize-sref.bitlen,tmpreg);
  1809. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1810. { mask left over bits }
  1811. a_op_const_reg(list,OP_AND,osuinttype,tcgint(((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen)),tmpreg);
  1812. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,tmpreg);
  1813. end
  1814. else
  1815. begin
  1816. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1817. { mask left over bits }
  1818. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1819. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,tmpreg);
  1820. end;
  1821. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,valuereg);
  1822. end;
  1823. tmpreg:=getintregister(list,loadsize);
  1824. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  1825. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  1826. { make sure we do not read/write past the end of the array }
  1827. current_asmdata.getjumplabel(hl);
  1828. a_cmp_const_reg_label(list,osuinttype,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  1829. a_load_ref_reg(list,loadsize,osuinttype,tmpref,extra_value_reg);
  1830. tmpindexreg:=getintregister(list,osuinttype);
  1831. { load current array value }
  1832. if (slopt<>SL_SETZERO) then
  1833. begin
  1834. tmpreg:=getintregister(list,osuinttype);
  1835. if (slopt<>SL_SETMAX) then
  1836. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  1837. else if (sref.bitlen<>AIntBits) then
  1838. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  1839. else
  1840. a_load_const_reg(list,osuinttype,-1,tmpreg);
  1841. end;
  1842. { generate mask to zero the bits we have to insert }
  1843. if (slopt<>SL_SETMAX) then
  1844. begin
  1845. maskreg:=getintregister(list,osuinttype);
  1846. if (target_info.endian=endian_big) then
  1847. begin
  1848. a_op_const_reg_reg(list,OP_ADD,osuinttype,sref.bitlen-2*loadbitsize,sref.bitindexreg,tmpindexreg);
  1849. a_op_reg_reg(list,OP_NEG,osuinttype,tmpindexreg,tmpindexreg);
  1850. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1851. a_op_reg_reg(list,OP_SHL,osuinttype,tmpindexreg,maskreg);
  1852. end
  1853. else
  1854. begin
  1855. { Y-x = -(x-Y) }
  1856. a_op_const_reg_reg(list,OP_SUB,osuinttype,loadbitsize,sref.bitindexreg,tmpindexreg);
  1857. a_op_reg_reg(list,OP_NEG,osuinttype,tmpindexreg,tmpindexreg);
  1858. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1859. a_op_reg_reg(list,OP_SHR,osuinttype,tmpindexreg,maskreg);
  1860. end;
  1861. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  1862. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,extra_value_reg);
  1863. end;
  1864. if (slopt<>SL_SETZERO) then
  1865. begin
  1866. if (target_info.endian=endian_big) then
  1867. a_op_reg_reg(list,OP_SHL,osuinttype,tmpindexreg,tmpreg)
  1868. else
  1869. begin
  1870. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1871. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1872. a_op_reg_reg(list,OP_SHR,osuinttype,tmpindexreg,tmpreg);
  1873. end;
  1874. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,extra_value_reg);
  1875. end;
  1876. {$ifndef cpuhighleveltarget}
  1877. extra_value_reg:=cg.makeregsize(list,extra_value_reg,def_cgsize(loadsize));
  1878. a_load_reg_ref(list,loadsize,loadsize,extra_value_reg,tmpref);
  1879. {$else}
  1880. tmpreg:=getintregister(list,loadsize);
  1881. a_load_reg_reg(list,osuinttype,loadsize,extra_value_reg,tmpreg);
  1882. a_load_reg_ref(list,loadsize,loadsize,tmpreg,tmpref);
  1883. {$endif}
  1884. a_label(list,hl);
  1885. end;
  1886. end;
  1887. end;
  1888. procedure thlcgobj.a_load_regconst_subsetreg_intern(list: TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt);
  1889. var
  1890. bitmask: aword;
  1891. tmpreg: tregister;
  1892. subsetregdef: torddef;
  1893. stopbit: byte;
  1894. begin
  1895. subsetregdef:=tcgsize2orddef(sreg.subsetregsize);
  1896. stopbit:=sreg.startbit+sreg.bitlen;
  1897. // on x86(64), 1 shl 32(64) = 1 instead of 0
  1898. if (stopbit<>AIntBits) then
  1899. bitmask:=not(((aword(1) shl stopbit)-1) xor ((aword(1) shl sreg.startbit)-1))
  1900. else
  1901. bitmask:=not(high(aword) xor ((aword(1) shl sreg.startbit)-1));
  1902. if not(slopt in [SL_SETZERO,SL_SETMAX]) then
  1903. begin
  1904. tmpreg:=getintregister(list,subsetregdef);
  1905. a_load_reg_reg(list,fromsize,subsetregdef,fromreg,tmpreg);
  1906. a_op_const_reg(list,OP_SHL,subsetregdef,sreg.startbit,tmpreg);
  1907. if (slopt<>SL_REGNOSRCMASK) then
  1908. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(not(bitmask)),tmpreg);
  1909. end;
  1910. if (slopt<>SL_SETMAX) then
  1911. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(bitmask),sreg.subsetreg);
  1912. case slopt of
  1913. SL_SETZERO : ;
  1914. SL_SETMAX :
  1915. if (sreg.bitlen<>AIntBits) then
  1916. a_op_const_reg(list,OP_OR,subsetregdef,
  1917. tcgint(((aword(1) shl sreg.bitlen)-1) shl sreg.startbit),
  1918. sreg.subsetreg)
  1919. else
  1920. a_load_const_reg(list,subsetregdef,-1,sreg.subsetreg);
  1921. else
  1922. a_op_reg_reg(list,OP_OR,subsetregdef,tmpreg,sreg.subsetreg);
  1923. end;
  1924. end;
  1925. {$pop}
  1926. function thlcgobj.get_bit_const_ref_sref(bitnumber: tcgint; refdef: tdef; const ref: treference): tsubsetreference;
  1927. begin
  1928. result.ref:=ref;
  1929. inc(result.ref.offset,bitnumber div 8);
  1930. result.bitindexreg:=NR_NO;
  1931. result.startbit:=bitnumber mod 8;
  1932. result.bitlen:=1;
  1933. end;
  1934. function thlcgobj.get_bit_const_reg_sreg(setregsize: tdef; bitnumber: tcgint; setreg: tregister): tsubsetregister;
  1935. begin
  1936. result.subsetreg:=setreg;
  1937. result.subsetregsize:=def_cgsize(setregsize);
  1938. { subsetregs always count from the least significant to the most significant bit }
  1939. if (target_info.endian=endian_big) then
  1940. result.startbit:=(setregsize.size*8)-bitnumber-1
  1941. else
  1942. result.startbit:=bitnumber;
  1943. result.bitlen:=1;
  1944. end;
  1945. function thlcgobj.get_bit_reg_ref_sref(list: TAsmList; bitnumbersize, refsize: tdef; bitnumber: tregister; const ref: treference): tsubsetreference;
  1946. var
  1947. tmpreg: tregister;
  1948. begin
  1949. result.ref:=ref;
  1950. result.startbit:=0;
  1951. result.bitlen:=1;
  1952. tmpreg:=getintregister(list,ptruinttype);
  1953. a_load_reg_reg(list,bitnumbersize,ptruinttype,bitnumber,tmpreg);
  1954. a_op_const_reg(list,OP_SHR,ptruinttype,3,tmpreg);
  1955. { don't assign to ref.base, that one is for pointers and this is an index
  1956. (important for platforms like LLVM) }
  1957. if (result.ref.index=NR_NO) then
  1958. result.ref.index:=tmpreg
  1959. else
  1960. begin
  1961. a_op_reg_reg(list,OP_ADD,ptruinttype,result.ref.index,tmpreg);
  1962. result.ref.index:=tmpreg;
  1963. end;
  1964. tmpreg:=getintregister(list,ptruinttype);
  1965. a_load_reg_reg(list,bitnumbersize,ptruinttype,bitnumber,tmpreg);
  1966. a_op_const_reg(list,OP_AND,ptruinttype,7,tmpreg);
  1967. result.bitindexreg:=tmpreg;
  1968. end;
  1969. procedure thlcgobj.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  1970. var
  1971. reg: tregister;
  1972. regsize: tdef;
  1973. begin
  1974. if (fromsize.size>=tosize.size) then
  1975. regsize:=fromsize
  1976. else
  1977. regsize:=tosize;
  1978. reg:=getfpuregister(list,regsize);
  1979. a_loadfpu_ref_reg(list,fromsize,regsize,ref1,reg);
  1980. a_loadfpu_reg_ref(list,regsize,tosize,reg,ref2);
  1981. end;
  1982. procedure thlcgobj.a_loadfpu_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister);
  1983. begin
  1984. case loc.loc of
  1985. LOC_REFERENCE, LOC_CREFERENCE:
  1986. a_loadfpu_ref_reg(list,fromsize,tosize,loc.reference,reg);
  1987. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1988. a_loadfpu_reg_reg(list,fromsize,tosize,loc.register,reg);
  1989. else
  1990. internalerror(2010120412);
  1991. end;
  1992. end;
  1993. procedure thlcgobj.a_loadfpu_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation);
  1994. begin
  1995. case loc.loc of
  1996. LOC_REFERENCE, LOC_CREFERENCE:
  1997. a_loadfpu_reg_ref(list,fromsize,tosize,reg,loc.reference);
  1998. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1999. a_loadfpu_reg_reg(list,fromsize,tosize,reg,loc.register);
  2000. else
  2001. internalerror(2010120413);
  2002. end;
  2003. end;
  2004. procedure thlcgobj.a_loadfpu_reg_cgpara(list: TAsmList; fromsize: tdef; const r: tregister; const cgpara: TCGPara);
  2005. var
  2006. ref : treference;
  2007. begin
  2008. paramanager.alloccgpara(list,cgpara);
  2009. case cgpara.location^.loc of
  2010. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  2011. begin
  2012. cgpara.check_simple_location;
  2013. a_loadfpu_reg_reg(list,fromsize,cgpara.def,r,cgpara.location^.register);
  2014. end;
  2015. LOC_REFERENCE,LOC_CREFERENCE:
  2016. begin
  2017. cgpara.check_simple_location;
  2018. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  2019. a_loadfpu_reg_ref(list,fromsize,cgpara.def,r,ref);
  2020. end;
  2021. LOC_REGISTER,LOC_CREGISTER:
  2022. begin
  2023. { paramfpu_ref does the check_simpe_location check here if necessary }
  2024. tg.gethltemp(list,fromsize,fromsize.size,tt_normal,ref);
  2025. a_loadfpu_reg_ref(list,fromsize,fromsize,r,ref);
  2026. a_loadfpu_ref_cgpara(list,fromsize,ref,cgpara);
  2027. tg.Ungettemp(list,ref);
  2028. end;
  2029. else
  2030. internalerror(2010120422);
  2031. end;
  2032. end;
  2033. procedure thlcgobj.a_loadfpu_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference; const cgpara: TCGPara);
  2034. var
  2035. href : treference;
  2036. // hsize: tcgsize;
  2037. begin
  2038. case cgpara.location^.loc of
  2039. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  2040. begin
  2041. cgpara.check_simple_location;
  2042. paramanager.alloccgpara(list,cgpara);
  2043. a_loadfpu_ref_reg(list,fromsize,cgpara.def,ref,cgpara.location^.register);
  2044. end;
  2045. LOC_REFERENCE,LOC_CREFERENCE:
  2046. begin
  2047. cgpara.check_simple_location;
  2048. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  2049. { concatcopy should choose the best way to copy the data }
  2050. g_concatcopy(list,fromsize,ref,href);
  2051. end;
  2052. (* not yet supported
  2053. LOC_REGISTER,LOC_CREGISTER:
  2054. begin
  2055. { force integer size }
  2056. hsize:=int_cgsize(tcgsize2size[size]);
  2057. {$ifndef cpu64bitalu}
  2058. if (hsize in [OS_S64,OS_64]) then
  2059. cg64.a_load64_ref_cgpara(list,ref,cgpara)
  2060. else
  2061. {$endif not cpu64bitalu}
  2062. begin
  2063. cgpara.check_simple_location;
  2064. a_load_ref_cgpara(list,hsize,ref,cgpara)
  2065. end;
  2066. end
  2067. *)
  2068. else
  2069. internalerror(2010120423);
  2070. end;
  2071. end;
  2072. (*
  2073. procedure thlcgobj.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle);
  2074. begin
  2075. cg.a_loadmm_reg_reg(list,def_cgsize(fromsize),def_cgsize(tosize),reg1,reg2,shuffle);
  2076. end;
  2077. procedure thlcgobj.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2078. begin
  2079. cg.a_loadmm_ref_reg(list,def_cgsize(fromsize),def_cgsize(tosize),ref,reg,shuffle);
  2080. end;
  2081. procedure thlcgobj.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2082. begin
  2083. cg.a_loadmm_reg_ref(list,def_cgsize(fromsize),def_cgsize(tosize),reg,ref,shuffle);
  2084. end;
  2085. procedure thlcgobj.a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle);
  2086. begin
  2087. case loc.loc of
  2088. LOC_MMREGISTER,LOC_CMMREGISTER:
  2089. a_loadmm_reg_reg(list,fromsize,tosize,loc.register,reg,shuffle);
  2090. LOC_REFERENCE,LOC_CREFERENCE:
  2091. a_loadmm_ref_reg(list,fromsize,tosize,loc.reference,reg,shuffle);
  2092. LOC_REGISTER,LOC_CREGISTER:
  2093. a_loadmm_intreg_reg(list,fromsize,tosize,loc.register,reg,shuffle);
  2094. else
  2095. internalerror(2010120414);
  2096. end;
  2097. end;
  2098. procedure thlcgobj.a_loadmm_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation; shuffle: pmmshuffle);
  2099. begin
  2100. case loc.loc of
  2101. LOC_MMREGISTER,LOC_CMMREGISTER:
  2102. a_loadmm_reg_reg(list,fromsize,tosize,reg,loc.register,shuffle);
  2103. LOC_REFERENCE,LOC_CREFERENCE:
  2104. a_loadmm_reg_ref(list,fromsize,tosize,reg,loc.reference,shuffle);
  2105. else
  2106. internalerror(2010120415);
  2107. end;
  2108. end;
  2109. procedure thlcgobj.a_loadmm_reg_cgpara(list: TAsmList; fromsize: tdef; reg: tregister; const cgpara: TCGPara; shuffle: pmmshuffle);
  2110. var
  2111. href : treference;
  2112. begin
  2113. cgpara.check_simple_location;
  2114. paramanager.alloccgpara(list,cgpara);
  2115. case cgpara.location^.loc of
  2116. LOC_MMREGISTER,LOC_CMMREGISTER:
  2117. a_loadmm_reg_reg(list,fromsize,cgpara.def,reg,cgpara.location^.register,shuffle);
  2118. LOC_REFERENCE,LOC_CREFERENCE:
  2119. begin
  2120. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  2121. a_loadmm_reg_ref(list,fromsize,cgpara.def,reg,href,shuffle);
  2122. end;
  2123. LOC_REGISTER,LOC_CREGISTER:
  2124. begin
  2125. if assigned(shuffle) and
  2126. not shufflescalar(shuffle) then
  2127. internalerror(2009112510);
  2128. a_loadmm_reg_intreg(list,deomsize,cgpara.def,reg,cgpara.location^.register,mms_movescalar);
  2129. end
  2130. else
  2131. internalerror(2010120427);
  2132. end;
  2133. end;
  2134. procedure thlcgobj.a_loadmm_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference; const cgpara: TCGPara; shuffle: pmmshuffle);
  2135. var
  2136. hr : tregister;
  2137. hs : tmmshuffle;
  2138. begin
  2139. cgpara.check_simple_location;
  2140. hr:=cg.getmmregister(list,cgpara.size);
  2141. a_loadmm_ref_reg(list,deomsize,cgpara.def,ref,hr,shuffle);
  2142. if realshuffle(shuffle) then
  2143. begin
  2144. hs:=shuffle^;
  2145. removeshuffles(hs);
  2146. a_loadmm_reg_cgpara(list,cgpara.def,hr,cgpara,@hs);
  2147. end
  2148. else
  2149. a_loadmm_reg_cgpara(list,cgpara.def,hr,cgpara,shuffle);
  2150. end;
  2151. procedure thlcgobj.a_loadmm_loc_cgpara(list: TAsmList; fromsize: tdef; const loc: tlocation; const cgpara: TCGPara; shuffle: pmmshuffle);
  2152. begin
  2153. {$ifdef extdebug}
  2154. if def_cgsize(fromsize)<>loc.size then
  2155. internalerror(2010112105);
  2156. {$endif}
  2157. cg.a_loadmm_loc_cgpara(list,loc,cgpara,shuffle);
  2158. end;
  2159. procedure thlcgobj.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle);
  2160. begin
  2161. cg.a_opmm_reg_reg(list,op,def_cgsize(size),src,dst,shuffle);
  2162. end;
  2163. procedure thlcgobj.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2164. begin
  2165. cg.a_opmm_ref_reg(list,op,def_cgsize(size),ref,reg,shuffle)
  2166. end;
  2167. procedure thlcgobj.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size: tdef; const loc: tlocation; reg: tregister; shuffle: pmmshuffle);
  2168. begin
  2169. cg.a_opmm_loc_reg(list,op,def_cgsize(size),loc,reg,shuffle);
  2170. end;
  2171. procedure thlcgobj.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2172. begin
  2173. cg.a_opmm_reg_ref(list,op,def_cgsize(size),reg,ref,shuffle);
  2174. end;
  2175. *)
  2176. (*
  2177. procedure thlcgobj.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle);
  2178. begin
  2179. cg.a_loadmm_intreg_reg(list,def_cgsize(fromsize),def_cgsize(tosize),intreg,mmreg,shuffle);
  2180. end;
  2181. procedure thlcgobj.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle);
  2182. begin
  2183. cg.a_loadmm_reg_intreg(list,def_cgsize(fromsize),def_cgsize(tosize),mmreg,intreg,shuffle);
  2184. end;
  2185. *)
  2186. procedure thlcgobj.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference);
  2187. var
  2188. tmpreg : tregister;
  2189. begin
  2190. tmpreg:=getintregister(list,size);
  2191. a_load_ref_reg(list,size,size,ref,tmpreg);
  2192. a_op_const_reg(list,op,size,a,tmpreg);
  2193. a_load_reg_ref(list,size,size,tmpreg,ref);
  2194. end;
  2195. procedure thlcgobj.a_op_const_subsetreg(list: TAsmList; Op: TOpCG; size, subsetsize: tdef; a: aint; const sreg: tsubsetregister);
  2196. var
  2197. tmpreg: tregister;
  2198. begin
  2199. tmpreg:=getintregister(list,size);
  2200. a_load_subsetreg_reg(list,subsetsize,size,sreg,tmpreg);
  2201. a_op_const_reg(list,op,size,a,tmpreg);
  2202. a_load_reg_subsetreg(list,size,subsetsize,tmpreg,sreg);
  2203. end;
  2204. procedure thlcgobj.a_op_const_subsetref(list: TAsmList; Op: TOpCG; size, subsetsize: tdef; a: aint; const sref: tsubsetreference);
  2205. var
  2206. tmpreg: tregister;
  2207. begin
  2208. tmpreg:=getintregister(list,size);
  2209. a_load_subsetref_reg(list,subsetsize,size,sref,tmpreg);
  2210. a_op_const_reg(list,op,size,a,tmpreg);
  2211. a_load_reg_subsetref(list,size,subsetsize,tmpreg,sref);
  2212. end;
  2213. procedure thlcgobj.a_op_const_loc(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const loc: tlocation);
  2214. begin
  2215. case loc.loc of
  2216. LOC_REGISTER, LOC_CREGISTER:
  2217. a_op_const_reg(list,op,size,a,loc.register);
  2218. LOC_REFERENCE, LOC_CREFERENCE:
  2219. a_op_const_ref(list,op,size,a,loc.reference);
  2220. LOC_SUBSETREG, LOC_CSUBSETREG:
  2221. a_op_const_subsetreg(list,op,size,size,a,loc.sreg);
  2222. LOC_SUBSETREF, LOC_CSUBSETREF:
  2223. a_op_const_subsetref(list,op,size,size,a,loc.sref);
  2224. else
  2225. internalerror(2010120428);
  2226. end;
  2227. end;
  2228. procedure thlcgobj.a_op_reg_ref(list: TAsmList; Op: TOpCG; size: tdef; reg: TRegister; const ref: TReference);
  2229. var
  2230. tmpreg: tregister;
  2231. begin
  2232. tmpreg:=getintregister(list,size);
  2233. a_load_ref_reg(list,size,size,ref,tmpreg);
  2234. case op of
  2235. OP_NOT,OP_NEG:
  2236. begin
  2237. a_op_reg_reg(list,op,size,tmpreg,tmpreg);
  2238. end;
  2239. else
  2240. begin
  2241. a_op_reg_reg(list,op,size,reg,tmpreg);
  2242. end;
  2243. end;
  2244. a_load_reg_ref(list,size,size,tmpreg,ref);
  2245. end;
  2246. procedure thlcgobj.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  2247. var
  2248. tmpreg: tregister;
  2249. begin
  2250. case op of
  2251. OP_NOT,OP_NEG:
  2252. { handle it as "load ref,reg; op reg" }
  2253. begin
  2254. a_load_ref_reg(list,size,size,ref,reg);
  2255. a_op_reg_reg(list,op,size,reg,reg);
  2256. end;
  2257. else
  2258. begin
  2259. tmpreg:=getintregister(list,size);
  2260. a_load_ref_reg(list,size,size,ref,tmpreg);
  2261. a_op_reg_reg(list,op,size,tmpreg,reg);
  2262. end;
  2263. end;
  2264. end;
  2265. procedure thlcgobj.a_op_reg_subsetreg(list: TAsmList; Op: TOpCG; opsize, destsubsetsize: tdef; reg: TRegister; const sreg: tsubsetregister);
  2266. var
  2267. tmpreg: tregister;
  2268. begin
  2269. tmpreg:=getintregister(list,opsize);
  2270. a_load_subsetreg_reg(list,destsubsetsize,opsize,sreg,tmpreg);
  2271. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  2272. a_load_reg_subsetreg(list,opsize,destsubsetsize,tmpreg,sreg);
  2273. end;
  2274. procedure thlcgobj.a_op_reg_subsetref(list: TAsmList; Op: TOpCG; opsize, destsubsetsize: tdef; reg: TRegister; const sref: tsubsetreference);
  2275. var
  2276. tmpreg: tregister;
  2277. begin
  2278. tmpreg:=getintregister(list,opsize);
  2279. a_load_subsetref_reg(list,destsubsetsize,opsize,sref,tmpreg);
  2280. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  2281. a_load_reg_subsetref(list,opsize,destsubsetsize,tmpreg,sref);
  2282. end;
  2283. procedure thlcgobj.a_op_reg_loc(list: TAsmList; Op: TOpCG; size: tdef; reg: tregister; const loc: tlocation);
  2284. begin
  2285. case loc.loc of
  2286. LOC_REGISTER, LOC_CREGISTER:
  2287. a_op_reg_reg(list,op,size,reg,loc.register);
  2288. LOC_REFERENCE, LOC_CREFERENCE:
  2289. a_op_reg_ref(list,op,size,reg,loc.reference);
  2290. LOC_SUBSETREG, LOC_CSUBSETREG:
  2291. a_op_reg_subsetreg(list,op,size,size,reg,loc.sreg);
  2292. LOC_SUBSETREF, LOC_CSUBSETREF:
  2293. a_op_reg_subsetref(list,op,size,size,reg,loc.sref);
  2294. else
  2295. internalerror(2010120429);
  2296. end;
  2297. end;
  2298. procedure thlcgobj.a_op_ref_loc(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; const loc: tlocation);
  2299. var
  2300. tmpreg: tregister;
  2301. begin
  2302. case loc.loc of
  2303. LOC_REGISTER,LOC_CREGISTER:
  2304. a_op_ref_reg(list,op,size,ref,loc.register);
  2305. LOC_REFERENCE,LOC_CREFERENCE:
  2306. begin
  2307. tmpreg:=getintregister(list,size);
  2308. a_load_ref_reg(list,size,size,ref,tmpreg);
  2309. a_op_reg_ref(list,op,size,tmpreg,loc.reference);
  2310. end;
  2311. LOC_SUBSETREG, LOC_CSUBSETREG:
  2312. begin
  2313. tmpreg:=getintregister(list,size);
  2314. a_load_subsetreg_reg(list,size,size,loc.sreg,tmpreg);
  2315. a_op_ref_reg(list,op,size,ref,tmpreg);
  2316. a_load_reg_subsetreg(list,size,size,tmpreg,loc.sreg);
  2317. end;
  2318. LOC_SUBSETREF, LOC_CSUBSETREF:
  2319. begin
  2320. tmpreg:=getintregister(list,size);
  2321. a_load_subsetref_reg(list,size,size,loc.sref,tmpreg);
  2322. a_op_ref_reg(list,op,size,ref,tmpreg);
  2323. a_load_reg_subsetref(list,size,size,tmpreg,loc.sref);
  2324. end;
  2325. else
  2326. internalerror(2010120429);
  2327. end;
  2328. end;
  2329. procedure thlcgobj.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister);
  2330. begin
  2331. a_load_reg_reg(list,size,size,src,dst);
  2332. a_op_const_reg(list,op,size,a,dst);
  2333. end;
  2334. procedure thlcgobj.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  2335. var
  2336. tmpreg: tregister;
  2337. begin
  2338. if (dst<>src1) then
  2339. begin
  2340. a_load_reg_reg(list,size,size,src2,dst);
  2341. a_op_reg_reg(list,op,size,src1,dst);
  2342. end
  2343. else
  2344. begin
  2345. { can we do a direct operation on the target register ? }
  2346. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  2347. a_op_reg_reg(list,op,size,src2,dst)
  2348. else
  2349. begin
  2350. tmpreg:=getintregister(list,size);
  2351. a_load_reg_reg(list,size,size,src2,tmpreg);
  2352. a_op_reg_reg(list,op,size,src1,tmpreg);
  2353. a_load_reg_reg(list,size,size,tmpreg,dst);
  2354. end;
  2355. end;
  2356. end;
  2357. procedure thlcgobj.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  2358. begin
  2359. ovloc.loc:=LOC_VOID;
  2360. if not setflags then
  2361. a_op_const_reg_reg(list,op,size,a,src,dst)
  2362. else
  2363. internalerror(2010122910);
  2364. end;
  2365. procedure thlcgobj.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  2366. begin
  2367. ovloc.loc:=LOC_VOID;
  2368. if not setflags then
  2369. a_op_reg_reg_reg(list,op,size,src1,src2,dst)
  2370. else
  2371. internalerror(2010122911);
  2372. end;
  2373. procedure thlcgobj.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  2374. var
  2375. tmpreg: tregister;
  2376. begin
  2377. tmpreg:=getintregister(list,size);
  2378. a_load_const_reg(list,size,a,tmpreg);
  2379. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2380. end;
  2381. procedure thlcgobj.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const ref: treference; l: tasmlabel);
  2382. var
  2383. tmpreg: tregister;
  2384. begin
  2385. tmpreg:=getintregister(list,size);
  2386. a_load_ref_reg(list,size,size,ref,tmpreg);
  2387. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2388. end;
  2389. procedure thlcgobj.a_cmp_const_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const loc: tlocation; l: tasmlabel);
  2390. var
  2391. tmpreg: tregister;
  2392. begin
  2393. case loc.loc of
  2394. LOC_REGISTER,LOC_CREGISTER:
  2395. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  2396. LOC_REFERENCE,LOC_CREFERENCE:
  2397. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  2398. LOC_SUBSETREG, LOC_CSUBSETREG:
  2399. begin
  2400. tmpreg:=getintregister(list,size);
  2401. a_load_subsetreg_reg(list,size,size,loc.sreg,tmpreg);
  2402. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2403. end;
  2404. LOC_SUBSETREF, LOC_CSUBSETREF:
  2405. begin
  2406. tmpreg:=getintregister(list,size);
  2407. a_load_subsetref_reg(list,size,size,loc.sref,tmpreg);
  2408. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2409. end;
  2410. else
  2411. internalerror(2010120430);
  2412. end;
  2413. end;
  2414. procedure thlcgobj.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  2415. var
  2416. tmpreg: tregister;
  2417. begin
  2418. tmpreg:=getintregister(list,size);
  2419. a_load_ref_reg(list,size,size,ref,tmpreg);
  2420. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2421. end;
  2422. procedure thlcgobj.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  2423. var
  2424. tmpreg: tregister;
  2425. begin
  2426. tmpreg:=getintregister(list,size);
  2427. a_load_ref_reg(list,size,size,ref,tmpreg);
  2428. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  2429. end;
  2430. procedure thlcgobj.a_cmp_subsetreg_reg_label(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; l: tasmlabel);
  2431. var
  2432. tmpreg: tregister;
  2433. begin
  2434. tmpreg:=getintregister(list,cmpsize);
  2435. a_load_subsetreg_reg(list,fromsubsetsize,cmpsize,sreg,tmpreg);
  2436. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  2437. end;
  2438. procedure thlcgobj.a_cmp_subsetref_reg_label(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; l: tasmlabel);
  2439. var
  2440. tmpreg: tregister;
  2441. begin
  2442. tmpreg:=getintregister(list,cmpsize);
  2443. a_load_subsetref_reg(list,fromsubsetsize,cmpsize,sref,tmpreg);
  2444. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  2445. end;
  2446. procedure thlcgobj.a_cmp_loc_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const loc: tlocation; reg: tregister; l: tasmlabel);
  2447. begin
  2448. case loc.loc of
  2449. LOC_REGISTER,
  2450. LOC_CREGISTER:
  2451. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  2452. LOC_REFERENCE,
  2453. LOC_CREFERENCE :
  2454. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  2455. LOC_CONSTANT:
  2456. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  2457. LOC_SUBSETREG,
  2458. LOC_CSUBSETREG:
  2459. a_cmp_subsetreg_reg_label(list,size,size,cmp_op,loc.sreg,reg,l);
  2460. LOC_SUBSETREF,
  2461. LOC_CSUBSETREF:
  2462. a_cmp_subsetref_reg_label(list,size,size,cmp_op,loc.sref,reg,l);
  2463. else
  2464. internalerror(2010120431);
  2465. end;
  2466. end;
  2467. procedure thlcgobj.a_cmp_reg_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const loc: tlocation; l: tasmlabel);
  2468. begin
  2469. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  2470. end;
  2471. procedure thlcgobj.a_cmp_ref_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; const loc: tlocation; l: tasmlabel);
  2472. var
  2473. tmpreg: tregister;
  2474. begin
  2475. case loc.loc of
  2476. LOC_REGISTER,LOC_CREGISTER:
  2477. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  2478. LOC_REFERENCE,LOC_CREFERENCE:
  2479. begin
  2480. tmpreg:=getintregister(list,size);
  2481. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2482. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  2483. end;
  2484. LOC_CONSTANT:
  2485. begin
  2486. a_cmp_const_ref_label(list,size,swap_opcmp(cmp_op),loc.value,ref,l);
  2487. end;
  2488. LOC_SUBSETREG, LOC_CSUBSETREG:
  2489. begin
  2490. tmpreg:=getintregister(list,size);
  2491. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2492. a_cmp_subsetreg_reg_label(list,size,size,swap_opcmp(cmp_op),loc.sreg,tmpreg,l);
  2493. end;
  2494. LOC_SUBSETREF, LOC_CSUBSETREF:
  2495. begin
  2496. tmpreg:=getintregister(list,size);
  2497. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2498. a_cmp_subsetref_reg_label(list,size,size,swap_opcmp(cmp_op),loc.sref,tmpreg,l);
  2499. end;
  2500. else
  2501. internalerror(2010120432);
  2502. end;
  2503. end;
  2504. procedure thlcgobj.g_maybe_testself(list: TAsmList; selftype: tdef; reg: tregister);
  2505. var
  2506. OKLabel : tasmlabel;
  2507. cgpara1 : TCGPara;
  2508. pd : tprocdef;
  2509. begin
  2510. if (cs_check_object in current_settings.localswitches) or
  2511. (cs_check_range in current_settings.localswitches) then
  2512. begin
  2513. pd:=search_system_proc('fpc_handleerror');
  2514. current_asmdata.getjumplabel(oklabel);
  2515. a_cmp_const_reg_label(list,selftype,OC_NE,0,reg,oklabel);
  2516. cgpara1.init;
  2517. paramanager.getintparaloc(pd,1,cgpara1);
  2518. a_load_const_cgpara(list,s32inttype,aint(210),cgpara1);
  2519. paramanager.freecgpara(list,cgpara1);
  2520. g_call_system_proc(list,pd,nil);
  2521. cgpara1.done;
  2522. a_label(list,oklabel);
  2523. end;
  2524. end;
  2525. procedure thlcgobj.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  2526. begin
  2527. {
  2528. if use_vectorfpu(size) then
  2529. a_loadmm_ref_ref()
  2530. else
  2531. }
  2532. if size.typ<>floatdef then
  2533. a_load_ref_ref(list,size,size,source,dest)
  2534. else
  2535. a_loadfpu_ref_ref(list,size,size,source,dest);
  2536. end;
  2537. procedure thlcgobj.g_concatcopy_unaligned(list: TAsmList; size: tdef; const source, dest: treference);
  2538. begin
  2539. g_concatcopy(list,size,source,dest);
  2540. end;
  2541. procedure thlcgobj.g_copyshortstring(list: TAsmList; const source, dest: treference; strdef: tstringdef);
  2542. var
  2543. cgpara1,cgpara2,cgpara3 : TCGPara;
  2544. pd : tprocdef;
  2545. begin
  2546. pd:=search_system_proc('fpc_shortstr_assign');
  2547. cgpara1.init;
  2548. cgpara2.init;
  2549. cgpara3.init;
  2550. paramanager.getintparaloc(pd,1,cgpara1);
  2551. paramanager.getintparaloc(pd,2,cgpara2);
  2552. paramanager.getintparaloc(pd,3,cgpara3);
  2553. a_loadaddr_ref_cgpara(list,strdef,dest,cgpara3);
  2554. a_loadaddr_ref_cgpara(list,strdef,source,cgpara2);
  2555. a_load_const_cgpara(list,s32inttype,strdef.len,cgpara1);
  2556. paramanager.freecgpara(list,cgpara3);
  2557. paramanager.freecgpara(list,cgpara2);
  2558. paramanager.freecgpara(list,cgpara1);
  2559. g_call_system_proc(list,pd,nil);
  2560. cgpara3.done;
  2561. cgpara2.done;
  2562. cgpara1.done;
  2563. end;
  2564. procedure thlcgobj.g_copyvariant(list: TAsmList; const source, dest: treference; vardef: tvariantdef);
  2565. var
  2566. cgpara1,cgpara2 : TCGPara;
  2567. pd : tprocdef;
  2568. begin
  2569. pd:=search_system_proc('fpc_variant_copy_overwrite');
  2570. cgpara1.init;
  2571. cgpara2.init;
  2572. paramanager.getintparaloc(pd,1,cgpara1);
  2573. paramanager.getintparaloc(pd,2,cgpara2);
  2574. a_loadaddr_ref_cgpara(list,vardef,dest,cgpara2);
  2575. a_loadaddr_ref_cgpara(list,vardef,source,cgpara1);
  2576. paramanager.freecgpara(list,cgpara2);
  2577. paramanager.freecgpara(list,cgpara1);
  2578. g_call_system_proc(list,pd,nil);
  2579. cgpara2.done;
  2580. cgpara1.done;
  2581. end;
  2582. procedure thlcgobj.g_incrrefcount(list: TAsmList; t: tdef; const ref: treference);
  2583. var
  2584. href : treference;
  2585. incrfunc : string;
  2586. cgpara1,cgpara2 : TCGPara;
  2587. pd : tprocdef;
  2588. begin
  2589. cgpara1.init;
  2590. cgpara2.init;
  2591. if is_interfacecom_or_dispinterface(t) then
  2592. incrfunc:='fpc_intf_incr_ref'
  2593. else if is_ansistring(t) then
  2594. incrfunc:='fpc_ansistr_incr_ref'
  2595. else if is_widestring(t) then
  2596. incrfunc:='fpc_widestr_incr_ref'
  2597. else if is_unicodestring(t) then
  2598. incrfunc:='fpc_unicodestr_incr_ref'
  2599. else if is_dynamic_array(t) then
  2600. incrfunc:='fpc_dynarray_incr_ref'
  2601. else
  2602. incrfunc:='';
  2603. { call the special incr function or the generic addref }
  2604. if incrfunc<>'' then
  2605. begin
  2606. pd:=search_system_proc(incrfunc);
  2607. paramanager.getintparaloc(pd,1,cgpara1);
  2608. { widestrings aren't ref. counted on all platforms so we need the address
  2609. to create a real copy }
  2610. if is_widestring(t) then
  2611. a_loadaddr_ref_cgpara(list,t,ref,cgpara1)
  2612. else
  2613. { these functions get the pointer by value }
  2614. a_load_ref_cgpara(list,t,ref,cgpara1);
  2615. paramanager.freecgpara(list,cgpara1);
  2616. g_call_system_proc(list,pd,nil);
  2617. end
  2618. else
  2619. begin
  2620. pd:=search_system_proc('fpc_addref');
  2621. paramanager.getintparaloc(pd,1,cgpara1);
  2622. paramanager.getintparaloc(pd,2,cgpara2);
  2623. if is_open_array(t) then
  2624. InternalError(201103054);
  2625. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  2626. a_loadaddr_ref_cgpara(list,voidpointertype,href,cgpara2);
  2627. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  2628. paramanager.freecgpara(list,cgpara1);
  2629. paramanager.freecgpara(list,cgpara2);
  2630. g_call_system_proc(list,pd,nil);
  2631. end;
  2632. cgpara2.done;
  2633. cgpara1.done;
  2634. end;
  2635. procedure thlcgobj.g_initialize(list: TAsmList; t: tdef; const ref: treference);
  2636. var
  2637. href : treference;
  2638. cgpara1,cgpara2 : TCGPara;
  2639. pd : tprocdef;
  2640. begin
  2641. cgpara1.init;
  2642. cgpara2.init;
  2643. if is_ansistring(t) or
  2644. is_widestring(t) or
  2645. is_unicodestring(t) or
  2646. is_interfacecom_or_dispinterface(t) or
  2647. is_dynamic_array(t) then
  2648. a_load_const_ref(list,t,0,ref)
  2649. else if t.typ=variantdef then
  2650. begin
  2651. pd:=search_system_proc('fpc_variant_init');
  2652. paramanager.getintparaloc(pd,1,cgpara1);
  2653. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  2654. paramanager.freecgpara(list,cgpara1);
  2655. g_call_system_proc(list,pd,nil);
  2656. end
  2657. else
  2658. begin
  2659. if is_open_array(t) then
  2660. InternalError(201103052);
  2661. pd:=search_system_proc('fpc_initialize');
  2662. paramanager.getintparaloc(pd,1,cgpara1);
  2663. paramanager.getintparaloc(pd,2,cgpara2);
  2664. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  2665. a_loadaddr_ref_cgpara(list,voidpointertype,href,cgpara2);
  2666. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  2667. paramanager.freecgpara(list,cgpara1);
  2668. paramanager.freecgpara(list,cgpara2);
  2669. g_call_system_proc(list,pd,nil);
  2670. end;
  2671. cgpara1.done;
  2672. cgpara2.done;
  2673. end;
  2674. procedure thlcgobj.g_finalize(list: TAsmList; t: tdef; const ref: treference);
  2675. var
  2676. href : treference;
  2677. cgpara1,cgpara2 : TCGPara;
  2678. pd : tprocdef;
  2679. decrfunc : string;
  2680. begin
  2681. if is_interfacecom_or_dispinterface(t) then
  2682. decrfunc:='fpc_intf_decr_ref'
  2683. else if is_ansistring(t) then
  2684. decrfunc:='fpc_ansistr_decr_ref'
  2685. else if is_widestring(t) then
  2686. decrfunc:='fpc_widestr_decr_ref'
  2687. else if is_unicodestring(t) then
  2688. decrfunc:='fpc_unicodestr_decr_ref'
  2689. else if t.typ=variantdef then
  2690. decrfunc:='fpc_variant_clear'
  2691. else
  2692. begin
  2693. cgpara1.init;
  2694. cgpara2.init;
  2695. if is_open_array(t) then
  2696. InternalError(201103051);
  2697. { fpc_finalize takes a pointer value parameter, fpc_dynarray_clear a
  2698. pointer var parameter }
  2699. if is_dynamic_array(t) then
  2700. pd:=search_system_proc('fpc_dynarray_clear')
  2701. else
  2702. pd:=search_system_proc('fpc_finalize');
  2703. paramanager.getintparaloc(pd,1,cgpara1);
  2704. paramanager.getintparaloc(pd,2,cgpara2);
  2705. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  2706. a_loadaddr_ref_cgpara(list,voidpointertype,href,cgpara2);
  2707. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  2708. paramanager.freecgpara(list,cgpara1);
  2709. paramanager.freecgpara(list,cgpara2);
  2710. g_call_system_proc(list,pd,nil);
  2711. cgpara1.done;
  2712. cgpara2.done;
  2713. exit;
  2714. end;
  2715. pd:=search_system_proc(decrfunc);
  2716. cgpara1.init;
  2717. paramanager.getintparaloc(pd,1,cgpara1);
  2718. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  2719. paramanager.freecgpara(list,cgpara1);
  2720. g_call_system_proc(list,pd,nil);
  2721. cgpara1.done;
  2722. end;
  2723. procedure thlcgobj.g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string);
  2724. var
  2725. cgpara1,cgpara2,cgpara3: TCGPara;
  2726. href: TReference;
  2727. hreg, lenreg: TRegister;
  2728. pd: tprocdef;
  2729. begin
  2730. cgpara1.init;
  2731. cgpara2.init;
  2732. cgpara3.init;
  2733. pd:=search_system_proc(name);
  2734. paramanager.getintparaloc(pd,1,cgpara1);
  2735. paramanager.getintparaloc(pd,2,cgpara2);
  2736. paramanager.getintparaloc(pd,3,cgpara3);
  2737. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  2738. if highloc.loc=LOC_CONSTANT then
  2739. a_load_const_cgpara(list,ptrsinttype,highloc.value+1,cgpara3)
  2740. else
  2741. begin
  2742. if highloc.loc in [LOC_REGISTER,LOC_CREGISTER] then
  2743. hreg:=highloc.register
  2744. else
  2745. begin
  2746. hreg:=getintregister(list,ptrsinttype);
  2747. a_load_loc_reg(list,ptrsinttype,ptrsinttype,highloc,hreg);
  2748. end;
  2749. { increment, converts high(x) to length(x) }
  2750. lenreg:=getintregister(list,ptrsinttype);
  2751. a_op_const_reg_reg(list,OP_ADD,ptrsinttype,1,hreg,lenreg);
  2752. a_load_reg_cgpara(list,ptrsinttype,lenreg,cgpara3);
  2753. end;
  2754. a_loadaddr_ref_cgpara(list,voidpointertype,href,cgpara2);
  2755. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  2756. paramanager.freecgpara(list,cgpara1);
  2757. paramanager.freecgpara(list,cgpara2);
  2758. paramanager.freecgpara(list,cgpara3);
  2759. g_call_system_proc(list,pd,nil);
  2760. cgpara3.done;
  2761. cgpara2.done;
  2762. cgpara1.done;
  2763. end;
  2764. procedure thlcgobj.g_rangecheck(list: TAsmList; const l: tlocation; fromdef, todef: tdef);
  2765. var
  2766. {$if defined(cpu64bitalu) or defined(cpu32bitalu)}
  2767. aintmax: aint;
  2768. {$else}
  2769. aintmax: longint;
  2770. {$endif}
  2771. neglabel : tasmlabel;
  2772. hreg : tregister;
  2773. lto,hto,
  2774. lfrom,hfrom : TConstExprInt;
  2775. fromsize, tosize: cardinal;
  2776. maxdef: tdef;
  2777. from_signed, to_signed: boolean;
  2778. begin
  2779. { range checking on and range checkable value? }
  2780. if not(cs_check_range in current_settings.localswitches) or
  2781. not(fromdef.typ in [orddef,enumdef]) or
  2782. { C-style booleans can't really fail range checks, }
  2783. { all values are always valid }
  2784. is_cbool(todef) then
  2785. exit;
  2786. {$if not defined(cpuhighleveltarget) and not defined(cpu64bitalu)}
  2787. { handle 64bit rangechecks separate for 32bit processors }
  2788. if is_64bit(fromdef) or is_64bit(todef) then
  2789. begin
  2790. cg64.g_rangecheck64(list,l,fromdef,todef);
  2791. exit;
  2792. end;
  2793. {$endif ndef cpuhighleveltarget and ndef cpu64bitalu}
  2794. { only check when assigning to scalar, subranges are different, }
  2795. { when todef=fromdef then the check is always generated }
  2796. getrange(fromdef,lfrom,hfrom);
  2797. getrange(todef,lto,hto);
  2798. from_signed := is_signed(fromdef);
  2799. to_signed := is_signed(todef);
  2800. { check the rangedef of the array, not the array itself }
  2801. { (only change now, since getrange needs the arraydef) }
  2802. if (todef.typ = arraydef) then
  2803. todef := tarraydef(todef).rangedef;
  2804. { no range check if from and to are equal and are both longint/dword }
  2805. { (if we have a 32bit processor) or int64/qword, since such }
  2806. { operations can at most cause overflows (JM) }
  2807. { Note that these checks are mostly processor independent, they only }
  2808. { have to be changed once we introduce 64bit subrange types }
  2809. {$if defined(cpuhighleveltarget) or defined(cpu64bitalu)}
  2810. if (fromdef=todef) and
  2811. (fromdef.typ=orddef) and
  2812. (((((torddef(fromdef).ordtype=s64bit) and
  2813. (lfrom = low(int64)) and
  2814. (hfrom = high(int64))) or
  2815. ((torddef(fromdef).ordtype=u64bit) and
  2816. (lfrom = low(qword)) and
  2817. (hfrom = high(qword))) or
  2818. ((torddef(fromdef).ordtype=scurrency) and
  2819. (lfrom = low(int64)) and
  2820. (hfrom = high(int64)))))) then
  2821. exit;
  2822. {$endif cpuhighleveltarget or cpu64bitalu}
  2823. { 32 bit operations are automatically widened to 64 bit on 64 bit addr
  2824. targets }
  2825. {$ifdef cpu32bitaddr}
  2826. if (fromdef = todef) and
  2827. (fromdef.typ=orddef) and
  2828. (((((torddef(fromdef).ordtype = s32bit) and
  2829. (lfrom = int64(low(longint))) and
  2830. (hfrom = int64(high(longint)))) or
  2831. ((torddef(fromdef).ordtype = u32bit) and
  2832. (lfrom = low(cardinal)) and
  2833. (hfrom = high(cardinal)))))) then
  2834. exit;
  2835. {$endif cpu32bitaddr}
  2836. { optimize some range checks away in safe cases }
  2837. fromsize := fromdef.size;
  2838. tosize := todef.size;
  2839. if ((from_signed = to_signed) or
  2840. (not from_signed)) and
  2841. (lto<=lfrom) and (hto>=hfrom) and
  2842. (fromsize <= tosize) then
  2843. begin
  2844. { if fromsize < tosize, and both have the same signed-ness or }
  2845. { fromdef is unsigned, then all bit patterns from fromdef are }
  2846. { valid for todef as well }
  2847. if (fromsize < tosize) then
  2848. exit;
  2849. if (fromsize = tosize) and
  2850. (from_signed = to_signed) then
  2851. { only optimize away if all bit patterns which fit in fromsize }
  2852. { are valid for the todef }
  2853. begin
  2854. {$ifopt Q+}
  2855. {$define overflowon}
  2856. {$Q-}
  2857. {$endif}
  2858. {$ifopt R+}
  2859. {$define rangeon}
  2860. {$R-}
  2861. {$endif}
  2862. if to_signed then
  2863. begin
  2864. { calculation of the low/high ranges must not overflow 64 bit
  2865. otherwise we end up comparing with zero for 64 bit data types on
  2866. 64 bit processors }
  2867. if (lto = (int64(-1) << (tosize * 8 - 1))) and
  2868. (hto = (-((int64(-1) << (tosize * 8 - 1))+1))) then
  2869. exit
  2870. end
  2871. else
  2872. begin
  2873. { calculation of the low/high ranges must not overflow 64 bit
  2874. otherwise we end up having all zeros for 64 bit data types on
  2875. 64 bit processors }
  2876. if (lto = 0) and
  2877. (qword(hto) = (qword(-1) >> (64-(tosize * 8))) ) then
  2878. exit
  2879. end;
  2880. {$ifdef overflowon}
  2881. {$Q+}
  2882. {$undef overflowon}
  2883. {$endif}
  2884. {$ifdef rangeon}
  2885. {$R+}
  2886. {$undef rangeon}
  2887. {$endif}
  2888. end
  2889. end;
  2890. { depending on the types involved, we perform the range check for 64 or
  2891. for 32 bit }
  2892. if fromsize=8 then
  2893. maxdef:=fromdef
  2894. else
  2895. maxdef:=todef;
  2896. {$if sizeof(aintmax) = 8}
  2897. if maxdef.size=8 then
  2898. aintmax:=high(int64)
  2899. else
  2900. {$endif}
  2901. begin
  2902. aintmax:=high(longint);
  2903. maxdef:=u32inttype;
  2904. end;
  2905. { generate the rangecheck code for the def where we are going to }
  2906. { store the result }
  2907. { use the trick that }
  2908. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  2909. { To be able to do that, we have to make sure however that either }
  2910. { fromdef and todef are both signed or unsigned, or that we leave }
  2911. { the parts < 0 and > maxlongint out }
  2912. if from_signed xor to_signed then
  2913. begin
  2914. if from_signed then
  2915. { from is signed, to is unsigned }
  2916. begin
  2917. { if high(from) < 0 -> always range error }
  2918. if (hfrom < 0) or
  2919. { if low(to) > maxlongint also range error }
  2920. (lto > aintmax) then
  2921. begin
  2922. g_call_system_proc(list,'fpc_rangeerror',nil);
  2923. exit
  2924. end;
  2925. { from is signed and to is unsigned -> when looking at to }
  2926. { as an signed value, it must be < maxaint (otherwise }
  2927. { it will become negative, which is invalid since "to" is unsigned) }
  2928. if hto > aintmax then
  2929. hto := aintmax;
  2930. end
  2931. else
  2932. { from is unsigned, to is signed }
  2933. begin
  2934. if (lfrom > aintmax) or
  2935. (hto < 0) then
  2936. begin
  2937. g_call_system_proc(list,'fpc_rangeerror',nil);
  2938. exit
  2939. end;
  2940. { from is unsigned and to is signed -> when looking at to }
  2941. { as an unsigned value, it must be >= 0 (since negative }
  2942. { values are the same as values > maxlongint) }
  2943. if lto < 0 then
  2944. lto := 0;
  2945. end;
  2946. end;
  2947. hreg:=getintregister(list,maxdef);
  2948. a_load_loc_reg(list,fromdef,maxdef,l,hreg);
  2949. a_op_const_reg(list,OP_SUB,maxdef,tcgint(int64(lto)),hreg);
  2950. current_asmdata.getjumplabel(neglabel);
  2951. {
  2952. if from_signed then
  2953. a_cmp_const_reg_label(list,OS_INT,OC_GTE,aint(hto-lto),hreg,neglabel)
  2954. else
  2955. }
  2956. if qword(hto-lto)>qword(aintmax) then
  2957. a_cmp_const_reg_label(list,maxdef,OC_BE,aintmax,hreg,neglabel)
  2958. else
  2959. a_cmp_const_reg_label(list,maxdef,OC_BE,tcgint(int64(hto-lto)),hreg,neglabel);
  2960. g_call_system_proc(list,'fpc_rangeerror',nil);
  2961. a_label(list,neglabel);
  2962. end;
  2963. procedure thlcgobj.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  2964. var
  2965. sizereg,sourcereg,lenreg : tregister;
  2966. cgpara1,cgpara2,cgpara3 : TCGPara;
  2967. ptrarrdef : tdef;
  2968. pd : tprocdef;
  2969. getmemres : tcgpara;
  2970. destloc : tlocation;
  2971. begin
  2972. { because some abis don't support dynamic stack allocation properly
  2973. open array value parameters are copied onto the heap
  2974. }
  2975. { calculate necessary memory }
  2976. { read/write operations on one register make the life of the register allocator hard }
  2977. if not(lenloc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  2978. begin
  2979. lenreg:=getintregister(list,sinttype);
  2980. a_load_loc_reg(list,sinttype,sinttype,lenloc,lenreg);
  2981. end
  2982. else
  2983. lenreg:=lenloc.register;
  2984. sizereg:=getintregister(list,sinttype);
  2985. a_op_const_reg_reg(list,OP_ADD,sinttype,1,lenreg,sizereg);
  2986. a_op_const_reg(list,OP_IMUL,sinttype,arrdef.elesize,sizereg);
  2987. { load source }
  2988. ptrarrdef:=getpointerdef(arrdef);
  2989. sourcereg:=getaddressregister(list,ptrarrdef);
  2990. a_loadaddr_ref_reg(list,arrdef,ptrarrdef,ref,sourcereg);
  2991. { do getmem call }
  2992. pd:=search_system_proc('fpc_getmem');
  2993. cgpara1.init;
  2994. paramanager.getintparaloc(pd,1,cgpara1);
  2995. a_load_reg_cgpara(list,sinttype,sizereg,cgpara1);
  2996. paramanager.freecgpara(list,cgpara1);
  2997. getmemres:=g_call_system_proc(list,pd,ptrarrdef);
  2998. cgpara1.done;
  2999. { return the new address }
  3000. location_reset(destloc,LOC_REGISTER,OS_ADDR);
  3001. destloc.register:=destreg;
  3002. gen_load_cgpara_loc(list,ptrarrdef,getmemres,destloc,false);
  3003. { do move call }
  3004. pd:=search_system_proc('MOVE');
  3005. cgpara1.init;
  3006. cgpara2.init;
  3007. cgpara3.init;
  3008. paramanager.getintparaloc(pd,1,cgpara1);
  3009. paramanager.getintparaloc(pd,2,cgpara2);
  3010. paramanager.getintparaloc(pd,3,cgpara3);
  3011. { load size }
  3012. a_load_reg_cgpara(list,ptrsinttype,sizereg,cgpara3);
  3013. { load destination }
  3014. a_load_reg_cgpara(list,ptrarrdef,destreg,cgpara2);
  3015. { load source }
  3016. a_load_reg_cgpara(list,ptrarrdef,sourcereg,cgpara1);
  3017. paramanager.freecgpara(list,cgpara3);
  3018. paramanager.freecgpara(list,cgpara2);
  3019. paramanager.freecgpara(list,cgpara1);
  3020. g_call_system_proc(list,pd,nil);
  3021. cgpara3.done;
  3022. cgpara2.done;
  3023. cgpara1.done;
  3024. getmemres.resetiftemp;
  3025. end;
  3026. procedure thlcgobj.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  3027. var
  3028. cgpara1 : TCGPara;
  3029. pd : tprocdef;
  3030. begin
  3031. { do freemem call }
  3032. pd:=search_system_proc('fpc_freemem');
  3033. cgpara1.init;
  3034. paramanager.getintparaloc(pd,1,cgpara1);
  3035. { load source }
  3036. a_load_loc_cgpara(list,getpointerdef(arrdef),l,cgpara1);
  3037. paramanager.freecgpara(list,cgpara1);
  3038. g_call_system_proc(list,pd,nil);
  3039. cgpara1.done;
  3040. end;
  3041. procedure thlcgobj.g_profilecode(list: TAsmList);
  3042. begin
  3043. end;
  3044. procedure thlcgobj.g_allocload_reg_reg(list: TAsmList; regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  3045. begin
  3046. case regtyp of
  3047. R_INTREGISTER:
  3048. toreg:=getintregister(list,regsize);
  3049. R_ADDRESSREGISTER:
  3050. toreg:=getaddressregister(list,regsize);
  3051. R_FPUREGISTER:
  3052. toreg:=getfpuregister(list,regsize);
  3053. end;
  3054. a_load_reg_reg(list,regsize,regsize,fromreg,toreg);
  3055. end;
  3056. procedure thlcgobj.g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation);
  3057. procedure handle_reg_move(regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  3058. begin
  3059. case regtyp of
  3060. R_INTREGISTER:
  3061. toreg:=getintregister(list,regsize);
  3062. R_ADDRESSREGISTER:
  3063. toreg:=getaddressregister(list,regsize);
  3064. R_FPUREGISTER:
  3065. toreg:=getfpuregister(list,regsize);
  3066. end;
  3067. a_load_reg_reg(list,regsize,regsize,fromreg,toreg);
  3068. end;
  3069. begin
  3070. toloc:=fromloc;
  3071. case fromloc.loc of
  3072. { volatile location, can't get a permanent reference }
  3073. LOC_REGISTER,
  3074. LOC_FPUREGISTER:
  3075. internalerror(2012012702);
  3076. LOC_CONSTANT:
  3077. { finished }
  3078. ;
  3079. LOC_CREGISTER:
  3080. handle_reg_move(def,fromloc.reference.index,toloc.reference.index,R_INTREGISTER);
  3081. LOC_CFPUREGISTER:
  3082. handle_reg_move(def,fromloc.reference.index,toloc.reference.index,R_FPUREGISTER);
  3083. { although LOC_CREFERENCE cannot be an lvalue, we may want to take a
  3084. reference to such a location for multiple reading }
  3085. LOC_CREFERENCE,
  3086. LOC_REFERENCE:
  3087. begin
  3088. if (fromloc.reference.base<>NR_NO) and
  3089. (fromloc.reference.base<>current_procinfo.framepointer) and
  3090. (fromloc.reference.base<>NR_STACK_POINTER_REG) then
  3091. handle_reg_move(voidpointertype,fromloc.reference.base,toloc.reference.base,getregtype(fromloc.reference.base));
  3092. if (fromloc.reference.index<>NR_NO) and
  3093. (fromloc.reference.index<>current_procinfo.framepointer) and
  3094. (fromloc.reference.index<>NR_STACK_POINTER_REG) then
  3095. handle_reg_move(voidpointertype,fromloc.reference.index,toloc.reference.index,getregtype(fromloc.reference.index));
  3096. end;
  3097. else
  3098. internalerror(2012012701);
  3099. end;
  3100. end;
  3101. procedure thlcgobj.location_force_reg(list: TAsmList; var l: tlocation; src_size, dst_size: tdef; maybeconst: boolean);
  3102. var
  3103. hregister,
  3104. hregister2: tregister;
  3105. hl : tasmlabel;
  3106. oldloc : tlocation;
  3107. begin
  3108. oldloc:=l;
  3109. hregister:=getregisterfordef(list,dst_size);
  3110. { load value in new register }
  3111. case l.loc of
  3112. {$ifdef cpuflags}
  3113. LOC_FLAGS :
  3114. begin
  3115. g_flags2reg(list,dst_size,l.resflags,hregister);
  3116. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3117. end;
  3118. {$endif cpuflags}
  3119. LOC_JUMP :
  3120. begin
  3121. a_label(list,current_procinfo.CurrTrueLabel);
  3122. a_load_const_reg(list,dst_size,1,hregister);
  3123. current_asmdata.getjumplabel(hl);
  3124. a_jmp_always(list,hl);
  3125. a_label(list,current_procinfo.CurrFalseLabel);
  3126. a_load_const_reg(list,dst_size,0,hregister);
  3127. a_label(list,hl);
  3128. end;
  3129. else
  3130. begin
  3131. { load_loc_reg can only handle size >= l.size, when the
  3132. new size is smaller then we need to adjust the size
  3133. of the orignal and maybe recalculate l.register for i386 }
  3134. if (dst_size.size<src_size.size) then
  3135. begin
  3136. hregister2:=getregisterfordef(list,src_size);
  3137. { prevent problems with memory locations -- at this high
  3138. level we cannot twiddle with the reference offset, since
  3139. that may not mean anything (e.g., it refers to fixed-sized
  3140. stack slots on Java) }
  3141. a_load_loc_reg(list,src_size,src_size,l,hregister2);
  3142. a_load_reg_reg(list,src_size,dst_size,hregister2,hregister);
  3143. end
  3144. else
  3145. a_load_loc_reg(list,src_size,dst_size,l,hregister);
  3146. end;
  3147. end;
  3148. if (l.loc <> LOC_CREGISTER) or
  3149. not maybeconst then
  3150. location_reset(l,LOC_REGISTER,def_cgsize(dst_size))
  3151. else
  3152. location_reset(l,LOC_CREGISTER,def_cgsize(dst_size));
  3153. l.register:=hregister;
  3154. { Release temp if it was a reference }
  3155. if oldloc.loc=LOC_REFERENCE then
  3156. location_freetemp(list,oldloc);
  3157. end;
  3158. procedure thlcgobj.location_force_fpureg(list: TAsmList; var l: tlocation; size: tdef; maybeconst: boolean);
  3159. var
  3160. reg : tregister;
  3161. begin
  3162. if (l.loc<>LOC_FPUREGISTER) and
  3163. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  3164. begin
  3165. { if it's in an mm register, store to memory first }
  3166. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  3167. internalerror(2011012903);
  3168. reg:=getfpuregister(list,size);
  3169. a_loadfpu_loc_reg(list,size,size,l,reg);
  3170. location_freetemp(list,l);
  3171. location_reset(l,LOC_FPUREGISTER,l.size);
  3172. l.register:=reg;
  3173. end;
  3174. end;
  3175. procedure thlcgobj.location_force_mem(list: TAsmList; var l: tlocation; size: tdef);
  3176. var
  3177. r : treference;
  3178. forcesize: aint;
  3179. begin
  3180. case l.loc of
  3181. LOC_FPUREGISTER,
  3182. LOC_CFPUREGISTER :
  3183. begin
  3184. tg.gethltemp(list,size,size.size,tt_normal,r);
  3185. a_loadfpu_reg_ref(list,size,size,l.register,r);
  3186. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  3187. l.reference:=r;
  3188. end;
  3189. (*
  3190. LOC_MMREGISTER,
  3191. LOC_CMMREGISTER:
  3192. begin
  3193. tg.gethltemp(list,size,size.size,tt_normal,r);
  3194. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  3195. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  3196. l.reference:=r;
  3197. end;
  3198. *)
  3199. LOC_CONSTANT,
  3200. LOC_REGISTER,
  3201. LOC_CREGISTER,
  3202. LOC_SUBSETREG,
  3203. LOC_CSUBSETREG,
  3204. LOC_SUBSETREF,
  3205. LOC_CSUBSETREF:
  3206. begin
  3207. if not is_dynamic_array(size) and
  3208. not is_open_array(size) then
  3209. forcesize:=size.size
  3210. else
  3211. forcesize:=voidpointertype.size;
  3212. tg.gethltemp(list,size,forcesize,tt_normal,r);
  3213. a_load_loc_ref(list,size,size,l,r);
  3214. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  3215. l.reference:=r;
  3216. end;
  3217. LOC_CREFERENCE,
  3218. LOC_REFERENCE : ;
  3219. else
  3220. internalerror(2011010304);
  3221. end;
  3222. end;
  3223. procedure thlcgobj.location_get_data_ref(list: TAsmList; def: tdef; const l: tlocation; var ref: treference; loadref: boolean; alignment: longint);
  3224. begin
  3225. case l.loc of
  3226. LOC_REGISTER,
  3227. LOC_CREGISTER :
  3228. begin
  3229. if not loadref then
  3230. internalerror(200410231);
  3231. reference_reset_base(ref,l.register,0,alignment);
  3232. end;
  3233. LOC_REFERENCE,
  3234. LOC_CREFERENCE :
  3235. begin
  3236. if loadref then
  3237. begin
  3238. reference_reset_base(ref,getaddressregister(list,voidpointertype),0,alignment);
  3239. { it's a pointer to def }
  3240. a_load_ref_reg(list,voidpointertype,voidpointertype,l.reference,ref.base);
  3241. end
  3242. else
  3243. ref:=l.reference;
  3244. end;
  3245. else
  3246. internalerror(200309181);
  3247. end;
  3248. end;
  3249. procedure thlcgobj.maketojumpbool(list: TAsmList; p: tnode);
  3250. {
  3251. produces jumps to true respectively false labels using boolean expressions
  3252. depending on whether the loading of regvars is currently being
  3253. synchronized manually (such as in an if-node) or automatically (most of
  3254. the other cases where this procedure is called), loadregvars can be
  3255. "lr_load_regvars" or "lr_dont_load_regvars"
  3256. }
  3257. var
  3258. storepos : tfileposinfo;
  3259. begin
  3260. if nf_error in p.flags then
  3261. exit;
  3262. storepos:=current_filepos;
  3263. current_filepos:=p.fileinfo;
  3264. if is_boolean(p.resultdef) then
  3265. begin
  3266. if is_constboolnode(p) then
  3267. begin
  3268. if Tordconstnode(p).value.uvalue<>0 then
  3269. a_jmp_always(list,current_procinfo.CurrTrueLabel)
  3270. else
  3271. a_jmp_always(list,current_procinfo.CurrFalseLabel)
  3272. end
  3273. else
  3274. begin
  3275. case p.location.loc of
  3276. LOC_SUBSETREG,LOC_CSUBSETREG,
  3277. LOC_SUBSETREF,LOC_CSUBSETREF,
  3278. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  3279. begin
  3280. a_cmp_const_loc_label(list,p.resultdef,OC_NE,0,p.location,current_procinfo.CurrTrueLabel);
  3281. a_jmp_always(list,current_procinfo.CurrFalseLabel);
  3282. end;
  3283. LOC_JUMP:
  3284. ;
  3285. {$ifdef cpuflags}
  3286. LOC_FLAGS :
  3287. begin
  3288. a_jmp_flags(list,p.location.resflags,current_procinfo.CurrTrueLabel);
  3289. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3290. a_jmp_always(list,current_procinfo.CurrFalseLabel);
  3291. end;
  3292. {$endif cpuflags}
  3293. else
  3294. begin
  3295. printnode(output,p);
  3296. internalerror(2011010418);
  3297. end;
  3298. end;
  3299. end;
  3300. end
  3301. else
  3302. internalerror(2011010419);
  3303. current_filepos:=storepos;
  3304. end;
  3305. function use_ent : boolean;
  3306. begin
  3307. use_ent := (target_info.system in [system_mipsel_linux,system_mipseb_linux])
  3308. or (target_info.cpu=cpu_alpha);
  3309. end;
  3310. procedure thlcgobj.gen_proc_symbol(list: TAsmList);
  3311. var
  3312. item,
  3313. previtem : TCmdStrListItem;
  3314. begin
  3315. previtem:=nil;
  3316. item := TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
  3317. while assigned(item) do
  3318. begin
  3319. {$ifdef arm}
  3320. if current_settings.cputype in cpu_thumb2 then
  3321. list.concat(tai_thumb_func.create);
  3322. {$endif arm}
  3323. { "double link" all procedure entry symbols via .reference }
  3324. { directives on darwin, because otherwise the linker }
  3325. { sometimes strips the procedure if only on of the symbols }
  3326. { is referenced }
  3327. if assigned(previtem) and
  3328. (target_info.system in systems_darwin) then
  3329. list.concat(tai_directive.create(asd_reference,item.str));
  3330. if (cs_profile in current_settings.moduleswitches) or
  3331. (po_global in current_procinfo.procdef.procoptions) then
  3332. list.concat(Tai_symbol.createname_global(item.str,AT_FUNCTION,0))
  3333. else
  3334. list.concat(Tai_symbol.createname(item.str,AT_FUNCTION,0));
  3335. if assigned(previtem) and
  3336. (target_info.system in systems_darwin) then
  3337. list.concat(tai_directive.create(asd_reference,previtem.str));
  3338. if not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  3339. list.concat(Tai_function_name.create(item.str));
  3340. previtem:=item;
  3341. item := TCmdStrListItem(item.next);
  3342. end;
  3343. if (use_ent) then
  3344. list.concat(Tai_ent.create(current_procinfo.procdef.mangledname));
  3345. current_procinfo.procdef.procstarttai:=tai(list.last);
  3346. end;
  3347. procedure thlcgobj.gen_proc_symbol_end(list: TAsmList);
  3348. begin
  3349. if (use_ent) then
  3350. list.concat(Tai_ent_end.create(current_procinfo.procdef.mangledname));
  3351. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  3352. current_procinfo.procdef.procendtai:=tai(list.last);
  3353. if (current_module.islibrary) then
  3354. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  3355. { setinitname may generate a new section -> don't add to the
  3356. current list, because we assume this remains a text section }
  3357. exportlib.setinitname(current_asmdata.AsmLists[al_exports],current_procinfo.procdef.mangledname);
  3358. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  3359. begin
  3360. if (target_info.system in (systems_darwin+[system_powerpc_macos]+systems_aix)) and
  3361. not(current_module.islibrary) then
  3362. begin
  3363. new_section(list,sec_code,'',4);
  3364. list.concat(tai_symbol.createname_global(
  3365. target_info.cprefix+mainaliasname,AT_FUNCTION,0));
  3366. { keep argc, argv and envp properly on the stack }
  3367. if not(target_info.system in systems_aix) then
  3368. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN')
  3369. else
  3370. cg.a_call_name(list,target_info.cprefix+'FPC_SYSTEMMAIN',false)
  3371. end;
  3372. end;
  3373. end;
  3374. procedure thlcgobj.gen_initialize_code(list: TAsmList);
  3375. begin
  3376. { initialize local data like ansistrings }
  3377. case current_procinfo.procdef.proctypeoption of
  3378. potype_unitinit:
  3379. begin
  3380. { this is also used for initialization of variables in a
  3381. program which does not have a globalsymtable }
  3382. if assigned(current_module.globalsymtable) then
  3383. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@initialize_data,list);
  3384. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_data,list);
  3385. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  3386. end;
  3387. { units have seperate code for initilization and finalization }
  3388. potype_unitfinalize: ;
  3389. { program init/final is generated in separate procedure }
  3390. potype_proginit:
  3391. begin
  3392. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  3393. end;
  3394. else
  3395. current_procinfo.procdef.localst.SymList.ForEachCall(@initialize_data,list);
  3396. end;
  3397. { initialises temp. ansi/wide string data }
  3398. if (current_procinfo.procdef.proctypeoption<>potype_exceptfilter) then
  3399. inittempvariables(list);
  3400. {$ifdef OLDREGVARS}
  3401. load_regvars(list,nil);
  3402. {$endif OLDREGVARS}
  3403. end;
  3404. procedure thlcgobj.gen_finalize_code(list: TAsmList);
  3405. var
  3406. old_current_procinfo: tprocinfo;
  3407. begin
  3408. old_current_procinfo:=current_procinfo;
  3409. if (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  3410. begin
  3411. if (current_procinfo.parent.finalize_procinfo<>current_procinfo) then
  3412. exit;
  3413. current_procinfo:=current_procinfo.parent;
  3414. end;
  3415. {$ifdef OLDREGVARS}
  3416. cleanup_regvars(list);
  3417. {$endif OLDREGVARS}
  3418. { finalize temporary data }
  3419. finalizetempvariables(list);
  3420. { finalize local data like ansistrings}
  3421. case current_procinfo.procdef.proctypeoption of
  3422. potype_unitfinalize:
  3423. begin
  3424. { this is also used for initialization of variables in a
  3425. program which does not have a globalsymtable }
  3426. if assigned(current_module.globalsymtable) then
  3427. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@finalize_static_data,list);
  3428. TSymtable(current_module.localsymtable).SymList.ForEachCall(@finalize_static_data,list);
  3429. end;
  3430. { units/progs have separate code for initialization and finalization }
  3431. potype_unitinit: ;
  3432. { program init/final is generated in separate procedure }
  3433. potype_proginit: ;
  3434. else
  3435. current_procinfo.procdef.localst.SymList.ForEachCall(@finalize_local_vars,list);
  3436. end;
  3437. { finalize paras data }
  3438. if assigned(current_procinfo.procdef.parast) and
  3439. not(po_assembler in current_procinfo.procdef.procoptions) then
  3440. current_procinfo.procdef.parast.SymList.ForEachCall(@final_paras,list);
  3441. current_procinfo:=old_current_procinfo;
  3442. end;
  3443. procedure thlcgobj.gen_entry_code(list: TAsmList);
  3444. begin
  3445. { the actual profile code can clobber some registers,
  3446. therefore if the context must be saved, do it before
  3447. the actual call to the profile code
  3448. }
  3449. if (cs_profile in current_settings.moduleswitches) and
  3450. not(po_assembler in current_procinfo.procdef.procoptions) then
  3451. begin
  3452. { non-win32 can call mcout even in main }
  3453. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  3454. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  3455. begin
  3456. g_profilecode(list);
  3457. end;
  3458. end;
  3459. { TODO: create high level version (create compilerprocs in system unit,
  3460. look up procdef, use hlcgobj.a_call_name()) }
  3461. { call startup helpers from main program }
  3462. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  3463. begin
  3464. { initialize units }
  3465. cg.allocallcpuregisters(list);
  3466. if not(current_module.islibrary) then
  3467. cg.a_call_name(list,'FPC_INITIALIZEUNITS',false)
  3468. else
  3469. cg.a_call_name(list,'FPC_LIBINITIALIZEUNITS',false);
  3470. cg.deallocallcpuregisters(list);
  3471. end;
  3472. list.concat(Tai_force_line.Create);
  3473. {$ifdef OLDREGVARS}
  3474. load_regvars(list,nil);
  3475. {$endif OLDREGVARS}
  3476. end;
  3477. procedure thlcgobj.gen_exit_code(list: TAsmList);
  3478. begin
  3479. { TODO: create high level version (create compilerproc in system unit,
  3480. look up procdef, use hlcgobj.a_call_name()) }
  3481. { call __EXIT for main program }
  3482. if (not DLLsource) and
  3483. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  3484. cg.a_call_name(list,'FPC_DO_EXIT',false);
  3485. end;
  3486. procedure thlcgobj.inittempvariables(list: TAsmList);
  3487. var
  3488. hp : ptemprecord;
  3489. href : treference;
  3490. begin
  3491. hp:=tg.templist;
  3492. while assigned(hp) do
  3493. begin
  3494. if assigned(hp^.def) and
  3495. is_managed_type(hp^.def) then
  3496. begin
  3497. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  3498. g_initialize(list,hp^.def,href);
  3499. end;
  3500. hp:=hp^.next;
  3501. end;
  3502. end;
  3503. procedure thlcgobj.initialize_data(p: TObject; arg: pointer);
  3504. var
  3505. OldAsmList : TAsmList;
  3506. hp : tnode;
  3507. begin
  3508. if (tsym(p).typ = localvarsym) and
  3509. { local (procedure or unit) variables only need initialization if
  3510. they are used }
  3511. ((tabstractvarsym(p).refs>0) or
  3512. { managed return symbols must be inited }
  3513. ((tsym(p).typ=localvarsym) and (vo_is_funcret in tlocalvarsym(p).varoptions))
  3514. ) and
  3515. not(vo_is_typed_const in tabstractvarsym(p).varoptions) and
  3516. not(vo_is_external in tabstractvarsym(p).varoptions) and
  3517. not(vo_is_default_var in tabstractvarsym(p).varoptions) and
  3518. (is_managed_type(tabstractvarsym(p).vardef) or
  3519. ((m_iso in current_settings.modeswitches) and (tabstractvarsym(p).vardef.typ=filedef))
  3520. ) then
  3521. begin
  3522. OldAsmList:=current_asmdata.CurrAsmList;
  3523. current_asmdata.CurrAsmList:=TAsmList(arg);
  3524. hp:=cnodeutils.initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner),false);
  3525. firstpass(hp);
  3526. secondpass(hp);
  3527. hp.free;
  3528. current_asmdata.CurrAsmList:=OldAsmList;
  3529. end;
  3530. end;
  3531. procedure thlcgobj.finalizetempvariables(list: TAsmList);
  3532. var
  3533. hp : ptemprecord;
  3534. href : treference;
  3535. begin
  3536. hp:=tg.templist;
  3537. while assigned(hp) do
  3538. begin
  3539. if assigned(hp^.def) and
  3540. is_managed_type(hp^.def) then
  3541. begin
  3542. include(current_procinfo.flags,pi_needs_implicit_finally);
  3543. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  3544. g_finalize(list,hp^.def,href);
  3545. end;
  3546. hp:=hp^.next;
  3547. end;
  3548. end;
  3549. procedure thlcgobj.initialize_regvars(p: TObject; arg: pointer);
  3550. var
  3551. href : treference;
  3552. begin
  3553. if (tsym(p).typ=staticvarsym) then
  3554. begin
  3555. { Static variables can have the initialloc only set to LOC_CxREGISTER
  3556. or LOC_INVALID, for explaination see gen_alloc_symtable (PFV) }
  3557. case tstaticvarsym(p).initialloc.loc of
  3558. LOC_CREGISTER :
  3559. begin
  3560. {$ifdef cpu64bitalu}
  3561. if (tstaticvarsym(p).initialloc.size in [OS_128,OS_S128]) then
  3562. cg128.a_load128_const_reg(TAsmList(arg),0,0,tstaticvarsym(p).initialloc.register128)
  3563. else
  3564. {$else cpu64bitalu}
  3565. if (tstaticvarsym(p).initialloc.size in [OS_64,OS_S64]) then
  3566. cg64.a_load64_const_reg(TAsmList(arg),0,tstaticvarsym(p).initialloc.register64)
  3567. else
  3568. {$endif cpu64bitalu}
  3569. a_load_const_reg(TAsmList(arg),tstaticvarsym(p).vardef,0,
  3570. tstaticvarsym(p).initialloc.register);
  3571. end;
  3572. (*
  3573. LOC_CMMREGISTER :
  3574. { clear the whole register }
  3575. cg.a_opmm_reg_reg(TAsmList(arg),OP_XOR,reg_cgsize(tstaticvarsym(p).initialloc.register),
  3576. tstaticvarsym(p).initialloc.register,
  3577. tstaticvarsym(p).initialloc.register,
  3578. nil);
  3579. *)
  3580. LOC_CFPUREGISTER :
  3581. begin
  3582. { initialize fpu regvar by loading from memory }
  3583. reference_reset_symbol(href,
  3584. current_asmdata.RefAsmSymbol(tstaticvarsym(p).mangledname), 0,
  3585. var_align(tstaticvarsym(p).vardef.alignment));
  3586. a_loadfpu_ref_reg(TAsmList(arg), tstaticvarsym(p).vardef,
  3587. tstaticvarsym(p).vardef, href, tstaticvarsym(p).initialloc.register);
  3588. end;
  3589. LOC_INVALID :
  3590. ;
  3591. else
  3592. internalerror(200410124);
  3593. end;
  3594. end;
  3595. end;
  3596. procedure thlcgobj.finalize_sym(asmlist: TAsmList; sym: tsym);
  3597. var
  3598. hp : tnode;
  3599. OldAsmList : TAsmList;
  3600. begin
  3601. include(current_procinfo.flags,pi_needs_implicit_finally);
  3602. OldAsmList:=current_asmdata.CurrAsmList;
  3603. current_asmdata.CurrAsmList:=asmlist;
  3604. hp:=cloadnode.create(sym,sym.owner);
  3605. if (sym.typ=staticvarsym) and (vo_force_finalize in tstaticvarsym(sym).varoptions) then
  3606. include(tloadnode(hp).loadnodeflags,loadnf_isinternal_ignoreconst);
  3607. hp:=cnodeutils.finalize_data_node(hp);
  3608. firstpass(hp);
  3609. secondpass(hp);
  3610. hp.free;
  3611. current_asmdata.CurrAsmList:=OldAsmList;
  3612. end;
  3613. procedure thlcgobj.finalize_local_vars(p: TObject; arg: pointer);
  3614. begin
  3615. if (tsym(p).typ=localvarsym) and
  3616. (tlocalvarsym(p).refs>0) and
  3617. not(vo_is_external in tlocalvarsym(p).varoptions) and
  3618. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  3619. not(vo_is_default_var in tabstractvarsym(p).varoptions) and
  3620. is_managed_type(tlocalvarsym(p).vardef) then
  3621. finalize_sym(TAsmList(arg),tsym(p));
  3622. end;
  3623. procedure thlcgobj.finalize_static_data(p: TObject; arg: pointer);
  3624. var
  3625. i : longint;
  3626. pd : tprocdef;
  3627. begin
  3628. case tsym(p).typ of
  3629. staticvarsym :
  3630. begin
  3631. { local (procedure or unit) variables only need finalization
  3632. if they are used
  3633. }
  3634. if ((tstaticvarsym(p).refs>0) or
  3635. { global (unit) variables always need finalization, since
  3636. they may also be used in another unit
  3637. }
  3638. (tstaticvarsym(p).owner.symtabletype=globalsymtable)) and
  3639. (
  3640. (tstaticvarsym(p).varspez<>vs_const) or
  3641. (vo_force_finalize in tstaticvarsym(p).varoptions)
  3642. ) and
  3643. not(vo_is_funcret in tstaticvarsym(p).varoptions) and
  3644. not(vo_is_external in tstaticvarsym(p).varoptions) and
  3645. is_managed_type(tstaticvarsym(p).vardef) then
  3646. finalize_sym(TAsmList(arg),tsym(p));
  3647. end;
  3648. procsym :
  3649. begin
  3650. for i:=0 to tprocsym(p).ProcdefList.Count-1 do
  3651. begin
  3652. pd:=tprocdef(tprocsym(p).ProcdefList[i]);
  3653. if assigned(pd.localst) and
  3654. (pd.procsym=tprocsym(p)) and
  3655. (pd.localst.symtabletype<>staticsymtable) then
  3656. pd.localst.SymList.ForEachCall(@finalize_static_data,arg);
  3657. end;
  3658. end;
  3659. end;
  3660. end;
  3661. procedure thlcgobj.final_paras(p: TObject; arg: pointer);
  3662. var
  3663. list : TAsmList;
  3664. href : treference;
  3665. hsym : tparavarsym;
  3666. eldef : tdef;
  3667. highloc : tlocation;
  3668. begin
  3669. if not(tsym(p).typ=paravarsym) then
  3670. exit;
  3671. list:=TAsmList(arg);
  3672. if is_managed_type(tparavarsym(p).vardef) then
  3673. begin
  3674. if (tparavarsym(p).varspez=vs_value) then
  3675. begin
  3676. include(current_procinfo.flags,pi_needs_implicit_finally);
  3677. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  3678. if is_open_array(tparavarsym(p).vardef) then
  3679. begin
  3680. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  3681. begin
  3682. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  3683. if not assigned(hsym) then
  3684. internalerror(201003032);
  3685. highloc:=hsym.initialloc
  3686. end
  3687. else
  3688. highloc.loc:=LOC_INVALID;
  3689. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  3690. g_array_rtti_helper(list,eldef,href,highloc,'fpc_finalize_array');
  3691. end
  3692. else
  3693. g_finalize(list,tparavarsym(p).vardef,href);
  3694. end;
  3695. end;
  3696. { open arrays can contain elements requiring init/final code, so the else has been removed here }
  3697. if (tparavarsym(p).varspez=vs_value) and
  3698. (is_open_array(tparavarsym(p).vardef) or
  3699. is_array_of_const(tparavarsym(p).vardef)) then
  3700. begin
  3701. { cdecl functions don't have a high pointer so it is not possible to generate
  3702. a local copy }
  3703. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  3704. g_releasevaluepara_openarray(list,tarraydef(tparavarsym(p).vardef),tparavarsym(p).localloc);
  3705. end;
  3706. end;
  3707. { generates the code for incrementing the reference count of parameters and
  3708. initialize out parameters }
  3709. { generates the code for incrementing the reference count of parameters and
  3710. initialize out parameters }
  3711. procedure thlcgobj.init_paras(p:TObject;arg:pointer);
  3712. var
  3713. href : treference;
  3714. hsym : tparavarsym;
  3715. eldef : tdef;
  3716. list : TAsmList;
  3717. highloc : tlocation;
  3718. needs_inittable : boolean;
  3719. begin
  3720. list:=TAsmList(arg);
  3721. if (tsym(p).typ=paravarsym) then
  3722. begin
  3723. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  3724. case tparavarsym(p).varspez of
  3725. vs_value :
  3726. if needs_inittable then
  3727. begin
  3728. { variants are already handled by the call to fpc_variant_copy_overwrite if
  3729. they are passed by reference }
  3730. if not((tparavarsym(p).vardef.typ=variantdef) and
  3731. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  3732. begin
  3733. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  3734. if is_open_array(tparavarsym(p).vardef) then
  3735. begin
  3736. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  3737. begin
  3738. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  3739. if not assigned(hsym) then
  3740. internalerror(201003032);
  3741. highloc:=hsym.initialloc
  3742. end
  3743. else
  3744. highloc.loc:=LOC_INVALID;
  3745. { open arrays do not contain correct element count in their rtti,
  3746. the actual count must be passed separately. }
  3747. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  3748. g_array_rtti_helper(list,eldef,href,highloc,'fpc_addref_array');
  3749. end
  3750. else
  3751. g_incrrefcount(list,tparavarsym(p).vardef,href);
  3752. end;
  3753. end;
  3754. vs_out :
  3755. begin
  3756. if needs_inittable then
  3757. begin
  3758. { we have no idea about the alignment at the callee side,
  3759. and the user also cannot specify "unaligned" here, so
  3760. assume worst case }
  3761. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  3762. if needs_inittable then
  3763. begin
  3764. if is_open_array(tparavarsym(p).vardef) then
  3765. begin
  3766. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  3767. begin
  3768. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  3769. if not assigned(hsym) then
  3770. internalerror(201003032);
  3771. highloc:=hsym.initialloc
  3772. end
  3773. else
  3774. highloc.loc:=LOC_INVALID;
  3775. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  3776. g_array_rtti_helper(list,eldef,href,highloc,'fpc_initialize_array');
  3777. end
  3778. else
  3779. g_initialize(list,tparavarsym(p).vardef,href);
  3780. end;
  3781. end;
  3782. end;
  3783. end;
  3784. end;
  3785. end;
  3786. procedure thlcgobj.gen_load_para_value(list: TAsmList);
  3787. var
  3788. i: longint;
  3789. currpara: tparavarsym;
  3790. begin
  3791. if (po_assembler in current_procinfo.procdef.procoptions) or
  3792. { exceptfilters have a single hidden 'parentfp' parameter, which
  3793. is handled by tcg.g_proc_entry. }
  3794. (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  3795. exit;
  3796. { Copy parameters to local references/registers }
  3797. for i:=0 to current_procinfo.procdef.paras.count-1 do
  3798. begin
  3799. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  3800. gen_load_cgpara_loc(list,currpara.vardef,currpara.paraloc[calleeside],currpara.initialloc,paramanager.param_use_paraloc(currpara.paraloc[calleeside]));
  3801. end;
  3802. { generate copies of call by value parameters, must be done before
  3803. the initialization and body is parsed because the refcounts are
  3804. incremented using the local copies }
  3805. current_procinfo.procdef.parast.SymList.ForEachCall(@g_copyvalueparas,list);
  3806. if not(po_assembler in current_procinfo.procdef.procoptions) then
  3807. begin
  3808. { initialize refcounted paras, and trash others. Needed here
  3809. instead of in gen_initialize_code, because when a reference is
  3810. intialised or trashed while the pointer to that reference is kept
  3811. in a regvar, we add a register move and that one again has to
  3812. come after the parameter loading code as far as the register
  3813. allocator is concerned }
  3814. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  3815. end;
  3816. end;
  3817. procedure thlcgobj.g_copyvalueparas(p: TObject; arg: pointer);
  3818. var
  3819. href : treference;
  3820. hreg : tregister;
  3821. list : TAsmList;
  3822. hsym : tparavarsym;
  3823. l : longint;
  3824. highloc,
  3825. localcopyloc : tlocation;
  3826. begin
  3827. list:=TAsmList(arg);
  3828. if (tsym(p).typ=paravarsym) and
  3829. (tparavarsym(p).varspez=vs_value) and
  3830. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  3831. begin
  3832. { we have no idea about the alignment at the caller side }
  3833. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  3834. if is_open_array(tparavarsym(p).vardef) or
  3835. is_array_of_const(tparavarsym(p).vardef) then
  3836. begin
  3837. { cdecl functions don't have a high pointer so it is not possible to generate
  3838. a local copy }
  3839. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  3840. begin
  3841. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  3842. begin
  3843. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  3844. if not assigned(hsym) then
  3845. internalerror(2011020506);
  3846. highloc:=hsym.initialloc
  3847. end
  3848. else
  3849. highloc.loc:=LOC_INVALID;
  3850. hreg:=getaddressregister(list,voidpointertype);
  3851. if not is_packed_array(tparavarsym(p).vardef) then
  3852. g_copyvaluepara_openarray(list,href,highloc,tarraydef(tparavarsym(p).vardef),hreg)
  3853. else
  3854. internalerror(2011020507);
  3855. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  3856. a_load_reg_loc(list,tparavarsym(p).vardef,tparavarsym(p).vardef,hreg,tparavarsym(p).initialloc);
  3857. end;
  3858. end
  3859. else
  3860. begin
  3861. { Allocate space for the local copy }
  3862. l:=tparavarsym(p).getsize;
  3863. localcopyloc.loc:=LOC_REFERENCE;
  3864. localcopyloc.size:=int_cgsize(l);
  3865. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  3866. { Copy data }
  3867. if is_shortstring(tparavarsym(p).vardef) then
  3868. begin
  3869. { this code is only executed before the code for the body and the entry/exit code is generated
  3870. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  3871. }
  3872. include(current_procinfo.flags,pi_do_call);
  3873. g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef))
  3874. end
  3875. else if tparavarsym(p).vardef.typ=variantdef then
  3876. begin
  3877. { this code is only executed before the code for the body and the entry/exit code is generated
  3878. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  3879. }
  3880. include(current_procinfo.flags,pi_do_call);
  3881. g_copyvariant(list,href,localcopyloc.reference,tvariantdef(tparavarsym(p).vardef))
  3882. end
  3883. else
  3884. begin
  3885. { pass proper alignment info }
  3886. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  3887. g_concatcopy(list,tparavarsym(p).vardef,href,localcopyloc.reference);
  3888. end;
  3889. { update localloc of varsym }
  3890. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  3891. tparavarsym(p).localloc:=localcopyloc;
  3892. tparavarsym(p).initialloc:=localcopyloc;
  3893. end;
  3894. end;
  3895. end;
  3896. procedure thlcgobj.gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint);
  3897. begin
  3898. case l.loc of
  3899. (*
  3900. LOC_MMREGISTER,
  3901. LOC_CMMREGISTER:
  3902. case cgpara.location^.loc of
  3903. LOC_REFERENCE,
  3904. LOC_CREFERENCE,
  3905. LOC_MMREGISTER,
  3906. LOC_CMMREGISTER,
  3907. LOC_REGISTER,
  3908. LOC_CREGISTER :
  3909. cg.a_loadmm_reg_cgpara(list,locsize,l.register,cgpara,mms_movescalar);
  3910. LOC_FPUREGISTER,
  3911. LOC_CFPUREGISTER:
  3912. begin
  3913. tmploc:=l;
  3914. location_force_fpureg(list,tmploc,false);
  3915. cg.a_loadfpu_reg_cgpara(list,tmploc.size,tmploc.register,cgpara);
  3916. end;
  3917. else
  3918. internalerror(200204249);
  3919. end;
  3920. *)
  3921. LOC_FPUREGISTER,
  3922. LOC_CFPUREGISTER:
  3923. case cgpara.location^.loc of
  3924. (*
  3925. LOC_MMREGISTER,
  3926. LOC_CMMREGISTER:
  3927. begin
  3928. tmploc:=l;
  3929. location_force_mmregscalar(list,tmploc,false);
  3930. cg.a_loadmm_reg_cgpara(list,tmploc.size,tmploc.register,cgpara,mms_movescalar);
  3931. end;
  3932. *)
  3933. { Some targets pass floats in normal registers }
  3934. LOC_REGISTER,
  3935. LOC_CREGISTER,
  3936. LOC_REFERENCE,
  3937. LOC_CREFERENCE,
  3938. LOC_FPUREGISTER,
  3939. LOC_CFPUREGISTER:
  3940. a_loadfpu_reg_cgpara(list,size,l.register,cgpara);
  3941. else
  3942. internalerror(2011010210);
  3943. end;
  3944. LOC_REFERENCE,
  3945. LOC_CREFERENCE:
  3946. case cgpara.location^.loc of
  3947. (*
  3948. LOC_MMREGISTER,
  3949. LOC_CMMREGISTER:
  3950. cg.a_loadmm_ref_cgpara(list,locsize,l.reference,cgpara,mms_movescalar);
  3951. *)
  3952. { Some targets pass floats in normal registers }
  3953. LOC_REGISTER,
  3954. LOC_CREGISTER,
  3955. LOC_REFERENCE,
  3956. LOC_CREFERENCE,
  3957. LOC_FPUREGISTER,
  3958. LOC_CFPUREGISTER:
  3959. a_loadfpu_ref_cgpara(list,size,l.reference,cgpara);
  3960. else
  3961. internalerror(2011010211);
  3962. end;
  3963. LOC_REGISTER,
  3964. LOC_CREGISTER :
  3965. a_load_loc_cgpara(list,size,l,cgpara);
  3966. else
  3967. internalerror(2011010212);
  3968. end;
  3969. end;
  3970. procedure thlcgobj.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  3971. begin
  3972. { do nothing by default }
  3973. end;
  3974. procedure thlcgobj.gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);
  3975. begin
  3976. { Handle Floating point types differently
  3977. This doesn't depend on emulator settings, emulator settings should
  3978. be handled by cpupara }
  3979. if (vardef.typ=floatdef) or
  3980. { some ABIs return certain records in an fpu register }
  3981. (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) or
  3982. (assigned(cgpara.location) and
  3983. (cgpara.Location^.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) then
  3984. begin
  3985. gen_loadfpu_loc_cgpara(list,vardef,l,cgpara,vardef.size);
  3986. exit;
  3987. end;
  3988. case l.loc of
  3989. LOC_CONSTANT,
  3990. LOC_REGISTER,
  3991. LOC_CREGISTER,
  3992. LOC_REFERENCE,
  3993. LOC_CREFERENCE :
  3994. begin
  3995. a_load_loc_cgpara(list,vardef,l,cgpara);
  3996. end;
  3997. (*
  3998. LOC_MMREGISTER,
  3999. LOC_CMMREGISTER:
  4000. begin
  4001. case l.size of
  4002. OS_F32,
  4003. OS_F64:
  4004. cg.a_loadmm_loc_cgpara(list,l,cgpara,mms_movescalar);
  4005. else
  4006. cg.a_loadmm_loc_cgpara(list,l,cgpara,nil);
  4007. end;
  4008. end;
  4009. *)
  4010. else
  4011. internalerror(2011010213);
  4012. end;
  4013. end;
  4014. procedure thlcgobj.gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  4015. var
  4016. href : treference;
  4017. begin
  4018. para.check_simple_location;
  4019. { skip e.g. empty records }
  4020. if (para.location^.loc = LOC_VOID) then
  4021. exit;
  4022. case destloc.loc of
  4023. LOC_REFERENCE :
  4024. begin
  4025. { If the parameter location is reused we don't need to copy
  4026. anything }
  4027. if not reusepara then
  4028. begin
  4029. reference_reset_base(href,para.location^.reference.index,para.location^.reference.offset,para.alignment);
  4030. a_load_ref_ref(list,para.def,para.def,href,destloc.reference);
  4031. end;
  4032. end;
  4033. { TODO other possible locations }
  4034. else
  4035. internalerror(2011010308);
  4036. end;
  4037. end;
  4038. procedure thlcgobj.gen_load_return_value(list: TAsmList);
  4039. var
  4040. ressym : tabstractnormalvarsym;
  4041. funcretloc : TCGPara;
  4042. begin
  4043. { Is the loading needed? }
  4044. if is_void(current_procinfo.procdef.returndef) or
  4045. (
  4046. (po_assembler in current_procinfo.procdef.procoptions) and
  4047. (not(assigned(current_procinfo.procdef.funcretsym)) or
  4048. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0) or
  4049. (po_nostackframe in current_procinfo.procdef.procoptions))
  4050. ) then
  4051. exit;
  4052. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  4053. { constructors return self }
  4054. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  4055. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.Find('self'))
  4056. else
  4057. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  4058. if (ressym.refs>0) or
  4059. is_managed_type(ressym.vardef) then
  4060. begin
  4061. { was: don't do anything if funcretloc.loc in [LOC_INVALID,LOC_REFERENCE] }
  4062. if not paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  4063. gen_load_loc_cgpara(list,ressym.vardef,ressym.localloc,funcretloc);
  4064. end
  4065. else
  4066. gen_load_uninitialized_function_result(list,current_procinfo.procdef,ressym.vardef,funcretloc)
  4067. end;
  4068. procedure thlcgobj.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  4069. begin
  4070. { add the procedure to the al_procedures }
  4071. maybe_new_object_file(current_asmdata.asmlists[al_procedures]);
  4072. new_section(current_asmdata.asmlists[al_procedures],sec_code,lower(pd.mangledname),getprocalign);
  4073. current_asmdata.asmlists[al_procedures].concatlist(code);
  4074. { save local data (casetable) also in the same file }
  4075. if assigned(data) and
  4076. (not data.empty) then
  4077. current_asmdata.asmlists[al_procedures].concatlist(data);
  4078. end;
  4079. function thlcgobj.g_call_system_proc(list: TAsmList; const procname: string; forceresdef: tdef): tcgpara;
  4080. var
  4081. pd: tprocdef;
  4082. begin
  4083. pd:=search_system_proc(procname);
  4084. result:=g_call_system_proc_intern(list,pd,forceresdef);
  4085. end;
  4086. function thlcgobj.g_call_system_proc(list: TAsmList; pd: tprocdef; forceresdef: tdef): tcgpara;
  4087. begin
  4088. { separate non-virtual routine to make it clear that the routine to
  4089. override, if any, is g_call_system_proc_intern (and that none of
  4090. the g_call_system_proc variants should be made virtual) }
  4091. result:=g_call_system_proc_intern(list,pd,forceresdef);
  4092. end;
  4093. function thlcgobj.g_call_system_proc_intern(list: TAsmList; pd: tprocdef; forceresdef: tdef): tcgpara;
  4094. begin
  4095. allocallcpuregisters(list);
  4096. result:=a_call_name(list,pd,pd.mangledname,forceresdef,false);
  4097. deallocallcpuregisters(list);
  4098. end;
  4099. end.