hlcgobj.pas 185 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365
  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,rgobj,
  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;
  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. procedure get_subsetref_load_info(const sref: tsubsetreference; out loadsize: torddef; out extra_load: boolean);
  227. procedure a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister); virtual;
  228. procedure a_load_subsetref_regs_index(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg: tregister); virtual;
  229. procedure a_load_regconst_subsetref_intern(list : TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sref: tsubsetreference; slopt: tsubsetloadopt); virtual;
  230. procedure a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt); virtual;
  231. { return a subsetref that represents bit "bitnumber" in ref, if ref
  232. has the type "refdef". The subsetref must be addressable via
  233. (unsigned) 8 bit access, unless all the *_bit_* methods are
  234. overloaded and use something else. }
  235. function get_bit_const_ref_sref(bitnumber: tcgint; refdef: tdef; const ref: treference): tsubsetreference;
  236. function get_bit_const_reg_sreg(setregsize: tdef; bitnumber: tcgint; setreg: tregister): tsubsetregister;
  237. function get_bit_reg_ref_sref(list: TAsmList; bitnumbersize, refsize: tdef; bitnumber: tregister; const ref: treference): tsubsetreference;
  238. public
  239. { bit scan instructions (still need transformation to thlcgobj) }
  240. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: tdef; src, dst: tregister); virtual; abstract;
  241. { fpu move instructions }
  242. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister); virtual; abstract;
  243. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister); virtual; abstract;
  244. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference); virtual; abstract;
  245. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1,ref2: treference);virtual;
  246. procedure a_loadfpu_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister);virtual;
  247. procedure a_loadfpu_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation);virtual;
  248. procedure a_loadfpu_reg_cgpara(list : TAsmList;fromsize: tdef;const r : tregister;const cgpara : TCGPara);virtual;
  249. procedure a_loadfpu_ref_cgpara(list : TAsmList;fromsize : tdef;const ref : treference;const cgpara : TCGPara);virtual;
  250. { vector register move instructions }
  251. // we don't have high level defs yet that translate into all mm cgsizes
  252. {
  253. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef;reg1, reg2: tregister;shuffle : pmmshuffle); virtual;
  254. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  255. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual;
  256. }
  257. { required for subsetreg/ref; still tcgsize rather than tdef because of reason mentioned above }
  258. procedure a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);virtual; abstract;
  259. {
  260. procedure a_loadmm_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);virtual;
  261. procedure a_loadmm_reg_cgpara(list: TAsmList; fromsize: tdef; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  262. procedure a_loadmm_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  263. procedure a_loadmm_loc_cgpara(list: TAsmList; fromsize: tdef; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  264. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tdef;src,dst: tregister;shuffle : pmmshuffle); virtual;
  265. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tdef;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  266. procedure a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tdef;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  267. procedure a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tdef;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  268. }
  269. // we don't have high level defs yet that translate into all mm cgsizes
  270. // procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tdef; intreg, mmreg: tregister; shuffle: pmmshuffle); virtual;
  271. // procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tdef; mmreg, intreg: tregister; shuffle : pmmshuffle); virtual;
  272. { basic arithmetic operations }
  273. { note: for operators which require only one argument (not, neg), use }
  274. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  275. { that in this case the *second* operand is used as both source and }
  276. { destination (JM) }
  277. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; reg: TRegister); virtual; abstract;
  278. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference); virtual;
  279. procedure a_op_const_subsetreg(list : TAsmList; Op : TOpCG; size, subsetsize : tdef; a : aint; const sreg: tsubsetregister); virtual;
  280. procedure a_op_const_subsetref(list : TAsmList; Op : TOpCG; size, subsetsize : tdef; a : aint; const sref: tsubsetreference); virtual;
  281. procedure a_op_const_loc(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; const loc: tlocation);virtual;
  282. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister); virtual; abstract;
  283. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: tdef; reg: TRegister; const ref: TReference); virtual;
  284. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister); virtual;
  285. procedure a_op_reg_subsetreg(list: TAsmList; Op: TOpCG; opsize, destsubsetsize: tdef; reg: TRegister; const sreg: tsubsetregister); virtual;
  286. procedure a_op_reg_subsetref(list: TAsmList; Op: TOpCG; opsize, destsubsetsize: tdef; reg: TRegister; const sref: tsubsetreference); virtual;
  287. procedure a_op_reg_loc(list : TAsmList; Op: TOpCG; size: tdef; reg: tregister; const loc: tlocation);virtual;
  288. procedure a_op_ref_loc(list : TAsmList; Op: TOpCG; size: tdef; const ref: TReference; const loc: tlocation);virtual;
  289. { trinary operations for processors that support them, 'emulated' }
  290. { on others. None with "ref" arguments since I don't think there }
  291. { are any processors that support it (JM) }
  292. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister); virtual;
  293. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister); virtual;
  294. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  295. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  296. { comparison operations }
  297. procedure a_cmp_const_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp;a : aint;reg : tregister;
  298. l : tasmlabel);virtual;
  299. procedure a_cmp_const_ref_label(list : TAsmList;size : tdef;cmp_op : topcmp;a : aint;const ref : treference;
  300. l : tasmlabel); virtual;
  301. procedure a_cmp_const_loc_label(list: TAsmList; size: tdef;cmp_op: topcmp; a: aint; const loc: tlocation;
  302. l : tasmlabel);virtual;
  303. procedure a_cmp_reg_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  304. procedure a_cmp_ref_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  305. procedure a_cmp_reg_ref_label(list : TAsmList;size : tdef;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  306. procedure a_cmp_subsetreg_reg_label(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; l: tasmlabel); virtual;
  307. procedure a_cmp_subsetref_reg_label(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; l: tasmlabel); virtual;
  308. procedure a_cmp_loc_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);virtual;
  309. procedure a_cmp_reg_loc_label(list : TAsmList;size : tdef;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  310. procedure a_cmp_ref_loc_label(list: TAsmList; size: tdef;cmp_op: topcmp; const ref: treference; const loc: tlocation; l : tasmlabel);virtual;
  311. procedure a_jmp_always(list : TAsmList;l: tasmlabel); virtual;abstract;
  312. {$ifdef cpuflags}
  313. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); virtual; abstract;
  314. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  315. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  316. }
  317. procedure g_flags2reg(list: TAsmList; size: tdef; const f: tresflags; reg: TRegister); virtual; abstract;
  318. procedure g_flags2ref(list: TAsmList; size: tdef; const f: tresflags; const ref:TReference); virtual; abstract;
  319. {$endif cpuflags}
  320. procedure g_maybe_testself(list : TAsmList; selftype: tdef; reg:tregister);
  321. // procedure g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  322. {# This should emit the opcode to copy len bytes from the source
  323. to destination.
  324. It must be overridden for each new target processor.
  325. @param(source Source reference of copy)
  326. @param(dest Destination reference of copy)
  327. }
  328. procedure g_concatcopy(list : TAsmList;size: tdef; const source,dest : treference);virtual;
  329. {# This should emit the opcode to copy len bytes from the an unaligned source
  330. to destination.
  331. It must be overridden for each new target processor.
  332. @param(source Source reference of copy)
  333. @param(dest Destination reference of copy)
  334. }
  335. procedure g_concatcopy_unaligned(list : TAsmList;size: tdef; const source,dest : treference);virtual;
  336. {# This should emit the opcode to a shortrstring from the source
  337. to destination.
  338. @param(source Source reference of copy)
  339. @param(dest Destination reference of copy)
  340. }
  341. procedure g_copyshortstring(list : TAsmList;const source,dest : treference;strdef:tstringdef);virtual;
  342. procedure g_copyvariant(list : TAsmList;const source,dest : treference;vardef:tvariantdef);virtual;
  343. procedure g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);virtual;
  344. procedure g_initialize(list : TAsmList;t : tdef;const ref : treference);virtual;
  345. procedure g_finalize(list : TAsmList;t : tdef;const ref : treference);virtual;
  346. procedure g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation;
  347. const name: string);virtual;
  348. {# Generates range checking code. It is to note
  349. that this routine does not need to be overridden,
  350. as it takes care of everything.
  351. @param(p Node which contains the value to check)
  352. @param(todef Type definition of node to range check)
  353. }
  354. procedure g_rangecheck(list: TAsmList; const l:tlocation; fromdef,todef: tdef); virtual;
  355. {# Generates overflow checking code for a node }
  356. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef); virtual; abstract;
  357. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;var ovloc : tlocation);virtual; abstract;
  358. procedure g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;arrdef: tarraydef;destreg:tregister);virtual;abstract;
  359. procedure g_releasevaluepara_openarray(list : TAsmList;arrdef: tarraydef;const l:tlocation);virtual;abstract;
  360. {# Emits instructions when compilation is done in profile
  361. mode (this is set as a command line option). The default
  362. behavior does nothing, should be overridden as required.
  363. }
  364. procedure g_profilecode(list : TAsmList);virtual;
  365. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  366. @param(size Number of bytes to allocate)
  367. }
  368. procedure g_stackpointer_alloc(list : TAsmList;size : longint);virtual; abstract;
  369. {# Emits instruction for allocating the locals in entry
  370. code of a routine. This is one of the first
  371. routine called in @var(genentrycode).
  372. @param(localsize Number of bytes to allocate as locals)
  373. }
  374. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);virtual; abstract;
  375. {# Emits instructions for returning from a subroutine.
  376. Should also restore the framepointer and stack.
  377. @param(parasize Number of bytes of parameters to deallocate from stack)
  378. }
  379. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);virtual; abstract;
  380. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);virtual; abstract;
  381. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);virtual; abstract;
  382. { generate a stub which only purpose is to pass control the given external method,
  383. setting up any additional environment before doing so (if required).
  384. The default implementation issues a jump instruction to the external name. }
  385. // procedure g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string); virtual;
  386. protected
  387. procedure g_allocload_reg_reg(list: TAsmList; regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  388. public
  389. { create "safe copy" of a tlocation that can be used later: all
  390. registers used in the tlocation are copied to new ones, so that
  391. even if the original ones change, things stay the same (except if
  392. the original location was already a register, then the register is
  393. kept). Must only be used on lvalue locations.
  394. It's intended as some kind of replacement for a_loadaddr_ref_reg()
  395. for targets without pointers. }
  396. procedure g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation); virtual;
  397. { routines migrated from ncgutil }
  398. procedure location_force_reg(list:TAsmList;var l:tlocation;src_size,dst_size:tdef;maybeconst:boolean);virtual;
  399. procedure location_force_fpureg(list:TAsmList;var l: tlocation;size: tdef;maybeconst:boolean);virtual;
  400. procedure location_force_mem(list:TAsmList;var l:tlocation;size:tdef);virtual;
  401. // procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;abstract;
  402. // procedure location_force_mmreg(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;abstract;
  403. { Retrieve the location of the data pointed to in location l, when the location is
  404. a register it is expected to contain the address of the data }
  405. procedure location_get_data_ref(list:TAsmList;def: tdef; const l:tlocation;var ref:treference;loadref:boolean; alignment: longint);virtual;
  406. procedure maketojumpbool(list:TAsmList; p : tnode);virtual;
  407. procedure gen_proc_symbol(list:TAsmList);virtual;
  408. procedure gen_proc_symbol_end(list:TAsmList);virtual;
  409. procedure gen_initialize_code(list:TAsmList);virtual;
  410. procedure gen_finalize_code(list:TAsmList);virtual;
  411. procedure gen_entry_code(list:TAsmList);virtual;
  412. procedure gen_exit_code(list:TAsmList);virtual;
  413. protected
  414. { helpers called by gen_initialize_code/gen_finalize_code }
  415. procedure inittempvariables(list:TAsmList);virtual;
  416. procedure initialize_data(p:TObject;arg:pointer);virtual;
  417. procedure finalizetempvariables(list:TAsmList);virtual;
  418. procedure initialize_regvars(p:TObject;arg:pointer);virtual;
  419. procedure finalize_sym(asmlist:TAsmList;sym:tsym);virtual;
  420. { generates the code for finalisation of local variables }
  421. procedure finalize_local_vars(p:TObject;arg:pointer);virtual;
  422. { generates the code for finalization of static symtable and
  423. all local (static) typed consts }
  424. procedure finalize_static_data(p:TObject;arg:pointer);virtual;
  425. { generates the code for decrementing the reference count of parameters }
  426. procedure final_paras(p:TObject;arg:pointer);
  427. public
  428. procedure gen_load_para_value(list:TAsmList);virtual;
  429. protected
  430. { helpers called by gen_load_para_value }
  431. procedure g_copyvalueparas(p:TObject;arg:pointer);virtual;
  432. procedure gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation;const cgpara: tcgpara;locintsize: longint);virtual;
  433. procedure init_paras(p:TObject;arg:pointer);
  434. protected
  435. { Some targets have to put "something" in the function result
  436. location if it's not initialised by the Pascal code, e.g.
  437. stack-based architectures. By default it does nothing }
  438. procedure gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);virtual;
  439. public
  440. { load a tlocation into a cgpara }
  441. procedure gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);virtual;
  442. { load a cgpara into a tlocation }
  443. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);virtual;
  444. { load the function return value into the ABI-defined function return location }
  445. procedure gen_load_return_value(list:TAsmList);virtual;
  446. { extras refactored from other units }
  447. { queue the code/data generated for a procedure for writing out to
  448. the assembler/object file }
  449. procedure record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList); virtual;
  450. { generate a call to a routine in the system unit }
  451. function g_call_system_proc(list: TAsmList; const procname: string; forceresdef: tdef): tcgpara;
  452. protected
  453. function g_call_system_proc_intern(list: TAsmList; pd: tprocdef; forceresdef: tdef): tcgpara; virtual;
  454. public
  455. { Generate code to exit an unwind-protected region. The default implementation
  456. produces a simple jump to destination label. }
  457. procedure g_local_unwind(list: TAsmList; l: TAsmLabel);virtual;abstract;
  458. end;
  459. var
  460. {# Main high level code generator class }
  461. hlcg : thlcgobj;
  462. procedure destroy_hlcodegen;
  463. implementation
  464. uses
  465. globals,options,systems,
  466. fmodule,export,
  467. verbose,defutil,paramgr,
  468. symbase,symsym,symtable,
  469. ncon,nld,ncgrtti,pass_1,pass_2,
  470. cpuinfo,cgobj,tgobj,cutils,procinfo,
  471. ncgutil,ngenutil;
  472. procedure destroy_hlcodegen;
  473. begin
  474. hlcg.free;
  475. hlcg:=nil;
  476. destroy_codegen;
  477. end;
  478. { thlcgobj }
  479. constructor thlcgobj.create;
  480. begin
  481. end;
  482. procedure thlcgobj.init_register_allocators;
  483. begin
  484. cg.init_register_allocators;
  485. end;
  486. procedure thlcgobj.done_register_allocators;
  487. begin
  488. cg.done_register_allocators;
  489. end;
  490. procedure thlcgobj.set_regalloc_live_range_direction(dir: TRADirection);
  491. begin
  492. cg.set_regalloc_live_range_direction(dir);
  493. end;
  494. function thlcgobj.getintregister(list: TAsmList; size: tdef): Tregister;
  495. begin
  496. result:=cg.getintregister(list,def_cgsize(size));
  497. end;
  498. function thlcgobj.getaddressregister(list: TAsmList; size: tdef): Tregister;
  499. begin
  500. result:=cg.getaddressregister(list);
  501. end;
  502. function thlcgobj.getfpuregister(list: TAsmList; size: tdef): Tregister;
  503. begin
  504. result:=cg.getfpuregister(list,def_cgsize(size));
  505. end;
  506. (*
  507. function thlcgobj.getmmregister(list: TAsmList; size: tdef): Tregister;
  508. begin
  509. result:=cg.getmmregister(list,def_cgsize(size));
  510. end;
  511. *)
  512. function thlcgobj.getflagregister(list: TAsmList; size: tdef): Tregister;
  513. begin
  514. result:=cg.getflagregister(list,def_cgsize(size));
  515. end;
  516. function thlcgobj.getregisterfordef(list: TAsmList; size: tdef): Tregister;
  517. begin
  518. case def2regtyp(size) of
  519. R_INTREGISTER:
  520. result:=getintregister(list,size);
  521. R_ADDRESSREGISTER:
  522. result:=getaddressregister(list,size);
  523. R_FPUREGISTER:
  524. result:=getfpuregister(list,size);
  525. (*
  526. R_MMREGISTER:
  527. result:=getmmregister(list,size);
  528. *)
  529. else
  530. internalerror(2010122901);
  531. end;
  532. end;
  533. function thlcgobj.uses_registers(rt: Tregistertype): boolean;
  534. begin
  535. result:=cg.uses_registers(rt);
  536. end;
  537. procedure thlcgobj.getcpuregister(list: TAsmList; r: Tregister);
  538. begin
  539. cg.getcpuregister(list,r);
  540. end;
  541. procedure thlcgobj.ungetcpuregister(list: TAsmList; r: Tregister);
  542. begin
  543. cg.ungetcpuregister(list,r);
  544. end;
  545. procedure thlcgobj.alloccpuregisters(list: TAsmList; rt: Tregistertype; const r: Tcpuregisterset);
  546. begin
  547. cg.alloccpuregisters(list,rt,r);
  548. end;
  549. procedure thlcgobj.dealloccpuregisters(list: TAsmList; rt: Tregistertype; const r: Tcpuregisterset);
  550. begin
  551. cg.dealloccpuregisters(list,rt,r);
  552. end;
  553. procedure thlcgobj.allocallcpuregisters(list: TAsmList);
  554. begin
  555. cg.allocallcpuregisters(list);
  556. end;
  557. procedure thlcgobj.deallocallcpuregisters(list: TAsmList);
  558. begin
  559. cg.deallocallcpuregisters(list);
  560. end;
  561. procedure thlcgobj.do_register_allocation(list: TAsmList; headertai: tai);
  562. begin
  563. cg.do_register_allocation(list,headertai);
  564. end;
  565. procedure thlcgobj.translate_register(var reg: tregister);
  566. begin
  567. cg.translate_register(reg);
  568. end;
  569. function thlcgobj.def2regtyp(def: tdef): tregistertype;
  570. begin
  571. case def.typ of
  572. enumdef,
  573. orddef,
  574. recorddef,
  575. setdef:
  576. result:=R_INTREGISTER;
  577. stringdef,
  578. pointerdef,
  579. classrefdef,
  580. objectdef,
  581. procvardef,
  582. procdef,
  583. arraydef,
  584. formaldef:
  585. result:=R_ADDRESSREGISTER;
  586. floatdef:
  587. if use_vectorfpu(def) then
  588. result:=R_MMREGISTER
  589. else if cs_fp_emulation in current_settings.moduleswitches then
  590. result:=R_INTREGISTER
  591. else
  592. result:=R_FPUREGISTER;
  593. filedef,
  594. variantdef:
  595. internalerror(2010120507);
  596. else
  597. internalerror(2010120506);
  598. end;
  599. end;
  600. class function thlcgobj.tcgsize2orddef(size: tcgsize): torddef;
  601. begin
  602. case size of
  603. OS_8:
  604. result:=torddef(u8inttype);
  605. OS_S8:
  606. result:=torddef(s8inttype);
  607. OS_16:
  608. result:=torddef(u16inttype);
  609. OS_S16:
  610. result:=torddef(s16inttype);
  611. OS_32:
  612. result:=torddef(u32inttype);
  613. OS_S32:
  614. result:=torddef(s32inttype);
  615. OS_64:
  616. result:=torddef(u64inttype);
  617. OS_S64:
  618. result:=torddef(s64inttype);
  619. else
  620. internalerror(2012050401);
  621. end;
  622. end;
  623. procedure thlcgobj.a_label(list: TAsmList; l: tasmlabel); inline;
  624. begin
  625. cg.a_label(list,l);
  626. end;
  627. procedure thlcgobj.a_reg_alloc(list: TAsmList; r: tregister);
  628. begin
  629. cg.a_reg_alloc(list,r);
  630. end;
  631. procedure thlcgobj.a_reg_dealloc(list: TAsmList; r: tregister);
  632. begin
  633. cg.a_reg_dealloc(list,r);
  634. end;
  635. procedure thlcgobj.a_reg_sync(list: TAsmList; r: tregister);
  636. begin
  637. cg.a_reg_sync(list,r);
  638. end;
  639. procedure thlcgobj.a_load_reg_cgpara(list: TAsmList; size: tdef; r: tregister; const cgpara: TCGPara);
  640. var
  641. ref: treference;
  642. begin
  643. cgpara.check_simple_location;
  644. paramanager.alloccgpara(list,cgpara);
  645. case cgpara.location^.loc of
  646. LOC_REGISTER,LOC_CREGISTER:
  647. a_load_reg_reg(list,size,cgpara.def,r,cgpara.location^.register);
  648. LOC_REFERENCE,LOC_CREFERENCE:
  649. begin
  650. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  651. a_load_reg_ref(list,size,cgpara.def,r,ref);
  652. end;
  653. (*
  654. LOC_MMREGISTER,LOC_CMMREGISTER:
  655. a_loadmm_intreg_reg(list,size,cgpara.def,r,cgpara.location^.register,mms_movescalar);
  656. *)
  657. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  658. begin
  659. tg.gethltemp(list,size,size.size,tt_normal,ref);
  660. a_load_reg_ref(list,size,cgpara.def,r,ref);
  661. a_loadfpu_ref_cgpara(list,cgpara.def,ref,cgpara);
  662. tg.ungettemp(list,ref);
  663. end
  664. else
  665. internalerror(2010120415);
  666. end;
  667. end;
  668. procedure thlcgobj.a_load_const_cgpara(list: TAsmList; tosize: tdef; a: aint; const cgpara: TCGPara);
  669. var
  670. ref : treference;
  671. begin
  672. cgpara.check_simple_location;
  673. paramanager.alloccgpara(list,cgpara);
  674. case cgpara.location^.loc of
  675. LOC_REGISTER,LOC_CREGISTER:
  676. a_load_const_reg(list,cgpara.def,a,cgpara.location^.register);
  677. LOC_REFERENCE,LOC_CREFERENCE:
  678. begin
  679. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  680. a_load_const_ref(list,cgpara.def,a,ref);
  681. end
  682. else
  683. internalerror(2010120416);
  684. end;
  685. end;
  686. procedure thlcgobj.a_load_ref_cgpara(list: TAsmList; size: tdef; const r: treference; const cgpara: TCGPara);
  687. var
  688. ref: treference;
  689. begin
  690. cgpara.check_simple_location;
  691. paramanager.alloccgpara(list,cgpara);
  692. case cgpara.location^.loc of
  693. LOC_REGISTER,LOC_CREGISTER:
  694. a_load_ref_reg(list,size,cgpara.def,r,cgpara.location^.register);
  695. LOC_REFERENCE,LOC_CREFERENCE:
  696. begin
  697. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  698. a_load_ref_ref(list,size,cgpara.def,r,ref);
  699. end
  700. (*
  701. LOC_MMREGISTER,LOC_CMMREGISTER:
  702. begin
  703. case location^.size of
  704. OS_F32,
  705. OS_F64,
  706. OS_F128:
  707. a_loadmm_ref_reg(list,cgpara.def,cgpara.def,r,location^.register,mms_movescalar);
  708. OS_M8..OS_M128,
  709. OS_MS8..OS_MS128:
  710. a_loadmm_ref_reg(list,cgpara.def,cgpara.def,r,location^.register,nil);
  711. else
  712. internalerror(2010120417);
  713. end;
  714. end
  715. *)
  716. else
  717. internalerror(2010120418);
  718. end;
  719. end;
  720. procedure thlcgobj.a_load_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: TCGPara);
  721. begin
  722. case l.loc of
  723. LOC_REGISTER,
  724. LOC_CREGISTER :
  725. a_load_reg_cgpara(list,size,l.register,cgpara);
  726. LOC_CONSTANT :
  727. a_load_const_cgpara(list,size,l.value,cgpara);
  728. LOC_CREFERENCE,
  729. LOC_REFERENCE :
  730. a_load_ref_cgpara(list,size,l.reference,cgpara);
  731. else
  732. internalerror(2010120419);
  733. end;
  734. end;
  735. procedure thlcgobj.a_loadaddr_ref_cgpara(list: TAsmList; fromsize: tdef; const r: treference; const cgpara: TCGPara);
  736. var
  737. hr : tregister;
  738. begin
  739. cgpara.check_simple_location;
  740. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  741. begin
  742. paramanager.allocparaloc(list,cgpara.location);
  743. a_loadaddr_ref_reg(list,fromsize,cgpara.def,r,cgpara.location^.register)
  744. end
  745. else
  746. begin
  747. hr:=getaddressregister(list,cgpara.def);
  748. a_loadaddr_ref_reg(list,fromsize,cgpara.def,r,hr);
  749. a_load_reg_cgpara(list,cgpara.def,hr,cgpara);
  750. end;
  751. end;
  752. function thlcgobj.a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; forceresdef: tdef; weak: boolean): tcgpara;
  753. begin
  754. { this is incomplete, it only sets the default function result location;
  755. for use by descendants }
  756. if not assigned(forceresdef) then
  757. begin
  758. pd.init_paraloc_info(callerside);
  759. result:=pd.funcretloc[callerside];
  760. end
  761. else
  762. result:=paramanager.get_funcretloc(pd,callerside,forceresdef);
  763. end;
  764. procedure thlcgobj.a_call_ref(list: TAsmList; pd: tabstractprocdef; const ref: treference);
  765. var
  766. reg: tregister;
  767. size: tdef;
  768. begin
  769. { the loaded data is always a pointer to a procdef. A procvardef is
  770. implicitly a pointer already, but a procdef isn't -> create one }
  771. if pd.typ=procvardef then
  772. size:=pd
  773. else
  774. size:=getpointerdef(pd);
  775. reg:=getaddressregister(list,size);
  776. a_load_ref_reg(list,size,size,ref,reg);
  777. a_call_reg(list,pd,reg);
  778. end;
  779. function thlcgobj.a_call_name_static(list: TAsmList; pd: tprocdef; const s: TSymStr; forceresdef: tdef): tcgpara;
  780. begin
  781. result:=a_call_name(list,pd,s,forceresdef,false);
  782. end;
  783. procedure thlcgobj.a_call_name_inherited(list: TAsmList; pd: tprocdef; const s: TSymStr);
  784. begin
  785. a_call_name(list,pd,s,nil,false);
  786. end;
  787. procedure thlcgobj.a_load_const_ref(list: TAsmList; tosize: tdef; a: aint; const ref: treference);
  788. var
  789. tmpreg: tregister;
  790. begin
  791. tmpreg:=getintregister(list,tosize);
  792. a_load_const_reg(list,tosize,a,tmpreg);
  793. a_load_reg_ref(list,tosize,tosize,tmpreg,ref);
  794. end;
  795. procedure thlcgobj.a_load_const_loc(list: TAsmList; tosize: tdef; a: aint; const loc: tlocation);
  796. begin
  797. case loc.loc of
  798. LOC_REFERENCE,LOC_CREFERENCE:
  799. a_load_const_ref(list,tosize,a,loc.reference);
  800. LOC_REGISTER,LOC_CREGISTER:
  801. a_load_const_reg(list,tosize,a,loc.register);
  802. LOC_SUBSETREG,LOC_CSUBSETREG:
  803. a_load_const_subsetreg(list,tosize,a,loc.sreg);
  804. LOC_SUBSETREF,LOC_CSUBSETREF:
  805. a_load_const_subsetref(list,tosize,a,loc.sref);
  806. else
  807. internalerror(2010120401);
  808. end;
  809. end;
  810. procedure thlcgobj.a_load_reg_ref_unaligned(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  811. begin
  812. a_load_reg_ref(list,fromsize,tosize,register,ref);
  813. end;
  814. procedure thlcgobj.a_load_reg_loc(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const loc: tlocation);
  815. begin
  816. case loc.loc of
  817. LOC_REFERENCE,LOC_CREFERENCE:
  818. a_load_reg_ref(list,fromsize,tosize,reg,loc.reference);
  819. LOC_REGISTER,LOC_CREGISTER:
  820. a_load_reg_reg(list,fromsize,tosize,reg,loc.register);
  821. LOC_SUBSETREG,LOC_CSUBSETREG:
  822. a_load_reg_subsetreg(list,fromsize,tosize,reg,loc.sreg);
  823. LOC_SUBSETREF,LOC_CSUBSETREF:
  824. a_load_reg_subsetref(list,fromsize,tosize,reg,loc.sref);
  825. { we don't have enough type information to handle these here
  826. LOC_MMREGISTER,LOC_CMMREGISTER:
  827. a_loadmm_intreg_reg(list,fromsize,loc.size,reg,loc.register,mms_movescalar);
  828. }
  829. else
  830. internalerror(2010120402);
  831. end;
  832. end;
  833. procedure thlcgobj.a_load_ref_reg_unaligned(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  834. begin
  835. a_load_ref_reg(list,fromsize,tosize,ref,register);
  836. end;
  837. procedure thlcgobj.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  838. var
  839. tmpreg: tregister;
  840. begin
  841. { verify if we have the same reference }
  842. if references_equal(sref,dref) then
  843. exit;
  844. tmpreg:=getintregister(list,tosize);
  845. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  846. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  847. end;
  848. procedure thlcgobj.a_load_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; reg: tregister);
  849. begin
  850. case loc.loc of
  851. LOC_REFERENCE,LOC_CREFERENCE:
  852. a_load_ref_reg(list,fromsize,tosize,loc.reference,reg);
  853. LOC_REGISTER,LOC_CREGISTER:
  854. a_load_reg_reg(list,fromsize,tosize,loc.register,reg);
  855. LOC_CONSTANT:
  856. a_load_const_reg(list,tosize,loc.value,reg);
  857. LOC_SUBSETREG,LOC_CSUBSETREG:
  858. a_load_subsetreg_reg(list,fromsize,tosize,loc.sreg,reg);
  859. LOC_SUBSETREF,LOC_CSUBSETREF:
  860. a_load_subsetref_reg(list,fromsize,tosize,loc.sref,reg);
  861. else
  862. internalerror(2010120201);
  863. end;
  864. end;
  865. procedure thlcgobj.a_load_loc_ref(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const ref: treference);
  866. begin
  867. case loc.loc of
  868. LOC_REFERENCE,LOC_CREFERENCE:
  869. a_load_ref_ref(list,fromsize,tosize,loc.reference,ref);
  870. LOC_REGISTER,LOC_CREGISTER:
  871. a_load_reg_ref(list,fromsize,tosize,loc.register,ref);
  872. LOC_CONSTANT:
  873. a_load_const_ref(list,tosize,loc.value,ref);
  874. LOC_SUBSETREG,LOC_CSUBSETREG:
  875. a_load_subsetreg_ref(list,fromsize,tosize,loc.sreg,ref);
  876. LOC_SUBSETREF,LOC_CSUBSETREF:
  877. a_load_subsetref_ref(list,fromsize,tosize,loc.sref,ref);
  878. else
  879. internalerror(2010120403);
  880. end;
  881. end;
  882. procedure thlcgobj.a_load_loc_subsetreg(list: TAsmList; fromsize, tosubsetsize: tdef; const loc: tlocation; const sreg: tsubsetregister);
  883. begin
  884. case loc.loc of
  885. LOC_REFERENCE,LOC_CREFERENCE:
  886. a_load_ref_subsetreg(list,fromsize,tosubsetsize,loc.reference,sreg);
  887. LOC_REGISTER,LOC_CREGISTER:
  888. a_load_reg_subsetreg(list,fromsize,tosubsetsize,loc.register,sreg);
  889. LOC_CONSTANT:
  890. a_load_const_subsetreg(list,tosubsetsize,loc.value,sreg);
  891. LOC_SUBSETREG,LOC_CSUBSETREG:
  892. a_load_subsetreg_subsetreg(list,fromsize,tosubsetsize,loc.sreg,sreg);
  893. LOC_SUBSETREF,LOC_CSUBSETREF:
  894. a_load_subsetref_subsetreg(list,fromsize,tosubsetsize,loc.sref,sreg);
  895. else
  896. internalerror(2010120404);
  897. end;
  898. end;
  899. procedure thlcgobj.a_load_loc_subsetref(list: TAsmList; fromsize, tosubsetsize: tdef; const loc: tlocation; const sref: tsubsetreference);
  900. begin
  901. case loc.loc of
  902. LOC_REFERENCE,LOC_CREFERENCE:
  903. a_load_ref_subsetref(list,fromsize,tosubsetsize,loc.reference,sref);
  904. LOC_REGISTER,LOC_CREGISTER:
  905. a_load_reg_subsetref(list,fromsize,tosubsetsize,loc.register,sref);
  906. LOC_CONSTANT:
  907. a_load_const_subsetref(list,tosubsetsize,loc.value,sref);
  908. LOC_SUBSETREG,LOC_CSUBSETREG:
  909. a_load_subsetreg_subsetref(list,fromsize,tosubsetsize,loc.sreg,sref);
  910. LOC_SUBSETREF,LOC_CSUBSETREF:
  911. a_load_subsetref_subsetref(list,fromsize,tosubsetsize,loc.sref,sref);
  912. else
  913. internalerror(2010120405);
  914. end;
  915. end;
  916. {$push}
  917. {$r-,q-}
  918. procedure thlcgobj.a_load_subsetreg_reg(list: TAsmList; subsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister);
  919. var
  920. subsetregdef: torddef;
  921. bitmask: aword;
  922. tmpreg,
  923. subsetsizereg: tregister;
  924. stopbit: byte;
  925. begin
  926. subsetregdef:=tcgsize2orddef(sreg.subsetregsize);
  927. tmpreg:=getintregister(list,subsetregdef);
  928. if is_signed(subsetsize) then
  929. begin
  930. { sign extend in case the value has a bitsize mod 8 <> 0 }
  931. { both instructions will be optimized away if not }
  932. a_op_const_reg_reg(list,OP_SHL,subsetregdef,(tcgsize2size[sreg.subsetregsize]*8)-sreg.startbit-sreg.bitlen,sreg.subsetreg,tmpreg);
  933. a_op_const_reg(list,OP_SAR,subsetregdef,(tcgsize2size[sreg.subsetregsize]*8)-sreg.bitlen,tmpreg);
  934. end
  935. else
  936. begin
  937. a_op_const_reg_reg(list,OP_SHR,subsetregdef,sreg.startbit,sreg.subsetreg,tmpreg);
  938. stopbit:=sreg.startbit+sreg.bitlen;
  939. // on x86(64), 1 shl 32(64) = 1 instead of 0
  940. // use aword to prevent overflow with 1 shl 31
  941. if (stopbit-sreg.startbit<>AIntBits) then
  942. bitmask:=(aword(1) shl (stopbit-sreg.startbit))-1
  943. else
  944. bitmask:=high(aword);
  945. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(bitmask),tmpreg);
  946. end;
  947. subsetsizereg:=getintregister(list,subsetsize);
  948. a_load_reg_reg(list,subsetregdef,subsetsize,tmpreg,subsetsizereg);
  949. a_load_reg_reg(list,subsetsize,tosize,subsetsizereg,destreg);
  950. end;
  951. procedure thlcgobj.a_load_reg_subsetreg(list: TAsmList; fromsize, tosubsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister);
  952. begin
  953. a_load_regconst_subsetreg_intern(list,fromsize,tosubsetsize,fromreg,sreg,SL_REG);
  954. end;
  955. procedure thlcgobj.a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsreg, tosreg: tsubsetregister);
  956. var
  957. fromsubsetregdef,
  958. tosubsetregdef: torddef;
  959. tmpreg, tmpreg2: tregister;
  960. bitmask: aword;
  961. stopbit: byte;
  962. begin
  963. if (fromsreg.bitlen>=tosreg.bitlen) then
  964. begin
  965. fromsubsetregdef:=tcgsize2orddef(fromsreg.subsetregsize);
  966. tosubsetregdef:=tcgsize2orddef(tosreg.subsetregsize);
  967. if (fromsreg.startbit<=tosreg.startbit) then
  968. begin
  969. { tosreg may be larger -> use its size to perform the shift }
  970. tmpreg:=getintregister(list,tosubsetregdef);
  971. a_load_reg_reg(list,fromsubsetregdef,tosubsetregdef,fromsreg.subsetreg,tmpreg);
  972. a_op_const_reg(list,OP_SHL,tosubsetregdef,tosreg.startbit-fromsreg.startbit,tmpreg)
  973. end
  974. else
  975. begin
  976. { fromsreg may be larger -> use its size to perform the shift }
  977. tmpreg:=getintregister(list,fromsubsetregdef);
  978. a_op_const_reg_reg(list,OP_SHR,fromsubsetregdef,fromsreg.startbit-tosreg.startbit,fromsreg.subsetreg,tmpreg);
  979. tmpreg2:=getintregister(list,tosubsetregdef);
  980. a_load_reg_reg(list,fromsubsetregdef,tosubsetregdef,tmpreg,tmpreg2);
  981. tmpreg:=tmpreg2;
  982. end;
  983. stopbit:=tosreg.startbit + tosreg.bitlen;
  984. // on x86(64), 1 shl 32(64) = 1 instead of 0
  985. if (stopbit<>AIntBits) then
  986. bitmask:=not(((aword(1) shl stopbit)-1) xor ((aword(1) shl tosreg.startbit)-1))
  987. else
  988. bitmask:=(aword(1) shl tosreg.startbit) - 1;
  989. a_op_const_reg(list,OP_AND,tosubsetregdef,tcgint(bitmask),tosreg.subsetreg);
  990. a_op_const_reg(list,OP_AND,tosubsetregdef,tcgint(not(bitmask)),tmpreg);
  991. a_op_reg_reg(list,OP_OR,tosubsetregdef,tmpreg,tosreg.subsetreg);
  992. end
  993. else
  994. begin
  995. tmpreg:=getintregister(list,tosubsetsize);
  996. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  997. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  998. end;
  999. end;
  1000. procedure thlcgobj.a_load_subsetreg_ref(list: TAsmList; fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const destref: treference);
  1001. var
  1002. tmpreg: tregister;
  1003. begin
  1004. tmpreg:=getintregister(list,tosize);
  1005. a_load_subsetreg_reg(list,fromsubsetsize,tosize,sreg,tmpreg);
  1006. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  1007. end;
  1008. procedure thlcgobj.a_load_ref_subsetreg(list: TAsmList; fromsize, tosubsetsize: tdef; const fromref: treference; const sreg: tsubsetregister);
  1009. var
  1010. tmpreg: tregister;
  1011. begin
  1012. tmpreg := getintregister(list,tosubsetsize);
  1013. a_load_ref_reg(list,fromsize,tosubsetsize,fromref,tmpreg);
  1014. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,sreg);
  1015. end;
  1016. procedure thlcgobj.a_load_const_subsetreg(list: TAsmlist; tosubsetsize: tdef; a: aint; const sreg: tsubsetregister);
  1017. var
  1018. subsetregdef: torddef;
  1019. bitmask: aword;
  1020. stopbit: byte;
  1021. begin
  1022. subsetregdef:=tcgsize2orddef(sreg.subsetregsize);
  1023. stopbit:=sreg.startbit+sreg.bitlen;
  1024. // on x86(64), 1 shl 32(64) = 1 instead of 0
  1025. if (stopbit<>AIntBits) then
  1026. bitmask:=not(((aword(1) shl stopbit)-1) xor ((aword(1) shl sreg.startbit)-1))
  1027. else
  1028. bitmask:=(aword(1) shl sreg.startbit)-1;
  1029. if (((aword(a) shl sreg.startbit) and not bitmask)<>not bitmask) then
  1030. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(bitmask),sreg.subsetreg);
  1031. a_op_const_reg(list,OP_OR,subsetregdef,tcgint((aword(a) shl sreg.startbit) and not(bitmask)),sreg.subsetreg);
  1032. end;
  1033. procedure thlcgobj.a_load_subsetreg_loc(list: TAsmlist; fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const loc: tlocation);
  1034. begin
  1035. case loc.loc of
  1036. LOC_REFERENCE,LOC_CREFERENCE:
  1037. a_load_subsetreg_ref(list,fromsubsetsize,tosize,sreg,loc.reference);
  1038. LOC_REGISTER,LOC_CREGISTER:
  1039. a_load_subsetreg_reg(list,fromsubsetsize,tosize,sreg,loc.register);
  1040. LOC_SUBSETREG,LOC_CSUBSETREG:
  1041. a_load_subsetreg_subsetreg(list,fromsubsetsize,tosize,sreg,loc.sreg);
  1042. LOC_SUBSETREF,LOC_CSUBSETREF:
  1043. a_load_subsetreg_subsetref(list,fromsubsetsize,tosize,sreg,loc.sref);
  1044. else
  1045. internalerror(2010120406);
  1046. end;
  1047. end;
  1048. procedure thlcgobj.a_load_subsetref_reg(list: TAsmList; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; destreg: tregister);
  1049. var
  1050. tmpref: treference;
  1051. valuereg,extra_value_reg: tregister;
  1052. tosreg: tsubsetregister;
  1053. loadsize: torddef;
  1054. loadbitsize: byte;
  1055. extra_load: boolean;
  1056. begin
  1057. get_subsetref_load_info(sref,loadsize,extra_load);
  1058. loadbitsize:=loadsize.size*8;
  1059. { load the (first part) of the bit sequence }
  1060. valuereg:=getintregister(list,osuinttype);
  1061. a_load_ref_reg(list,loadsize,osuinttype,sref.ref,valuereg);
  1062. if not extra_load then
  1063. begin
  1064. { everything is guaranteed to be in a single register of loadsize }
  1065. if (sref.bitindexreg=NR_NO) then
  1066. begin
  1067. { use subsetreg routine, it may have been overridden with an optimized version }
  1068. tosreg.subsetreg:=valuereg;
  1069. tosreg.subsetregsize:=def_cgsize(osuinttype);
  1070. { subsetregs always count bits from right to left }
  1071. if (target_info.endian=endian_big) then
  1072. tosreg.startbit:=loadbitsize-(sref.startbit+sref.bitlen)
  1073. else
  1074. tosreg.startbit:=sref.startbit;
  1075. tosreg.bitlen:=sref.bitlen;
  1076. a_load_subsetreg_reg(list,fromsubsetsize,tosize,tosreg,destreg);
  1077. exit;
  1078. end
  1079. else
  1080. begin
  1081. if (sref.startbit<>0) then
  1082. internalerror(2006081510);
  1083. if (target_info.endian=endian_big) then
  1084. begin
  1085. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,valuereg);
  1086. if is_signed(fromsubsetsize) then
  1087. begin
  1088. { sign extend to entire register }
  1089. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-loadbitsize,valuereg);
  1090. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1091. end
  1092. else
  1093. a_op_const_reg(list,OP_SHR,osuinttype,loadbitsize-sref.bitlen,valuereg);
  1094. end
  1095. else
  1096. begin
  1097. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,valuereg);
  1098. if is_signed(fromsubsetsize) then
  1099. begin
  1100. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-sref.bitlen,valuereg);
  1101. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1102. end
  1103. end;
  1104. { mask other bits/sign extend }
  1105. if not is_signed(fromsubsetsize) then
  1106. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1107. end
  1108. end
  1109. else
  1110. begin
  1111. { load next value as well }
  1112. extra_value_reg:=getintregister(list,osuinttype);
  1113. if (sref.bitindexreg=NR_NO) then
  1114. begin
  1115. tmpref:=sref.ref;
  1116. inc(tmpref.offset,loadbitsize div 8);
  1117. a_load_ref_reg(list,loadsize,osuinttype,tmpref,extra_value_reg);
  1118. { can be overridden to optimize }
  1119. a_load_subsetref_regs_noindex(list,fromsubsetsize,loadbitsize,sref,valuereg,extra_value_reg)
  1120. end
  1121. else
  1122. begin
  1123. if (sref.startbit<>0) then
  1124. internalerror(2006080610);
  1125. a_load_subsetref_regs_index(list,fromsubsetsize,loadbitsize,sref,valuereg);
  1126. end;
  1127. end;
  1128. { store in destination }
  1129. {$ifndef cpuhighleveltarget}
  1130. { avoid unnecessary sign extension and zeroing }
  1131. valuereg:=cg.makeregsize(list,valuereg,OS_INT);
  1132. destreg:=cg.makeregsize(list,destreg,OS_INT);
  1133. cg.a_load_reg_reg(list,OS_INT,OS_INT,valuereg,destreg);
  1134. destreg:=cg.makeregsize(list,destreg,def_cgsize(tosize));
  1135. {$else}
  1136. { can't juggle with register sizes, they are actually typed entities
  1137. here }
  1138. a_load_reg_reg(list,osuinttype,tosize,valuereg,destreg);
  1139. {$endif}
  1140. end;
  1141. procedure thlcgobj.a_load_reg_subsetref(list: TAsmList; fromsize, tosubsetsize: tdef; fromreg: tregister; const sref: tsubsetreference);
  1142. begin
  1143. a_load_regconst_subsetref_intern(list,fromsize,tosubsetsize,fromreg,sref,SL_REG);
  1144. end;
  1145. procedure thlcgobj.a_load_subsetref_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsref, tosref: tsubsetreference);
  1146. var
  1147. tmpreg: tregister;
  1148. begin
  1149. tmpreg:=getintregister(list,tosubsetsize);
  1150. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1151. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1152. end;
  1153. procedure thlcgobj.a_load_subsetref_ref(list: TAsmList; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const destref: treference);
  1154. var
  1155. tmpreg: tregister;
  1156. begin
  1157. tmpreg:=getintregister(list,tosize);
  1158. a_load_subsetref_reg(list,fromsubsetsize,tosize,sref,tmpreg);
  1159. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  1160. end;
  1161. procedure thlcgobj.a_load_ref_subsetref(list: TAsmList; fromsize, tosubsetsize: tdef; const fromref: treference; const sref: tsubsetreference);
  1162. var
  1163. tmpreg: tregister;
  1164. begin
  1165. tmpreg := getintregister(list,tosubsetsize);
  1166. a_load_ref_reg(list,fromsize,tosubsetsize,fromref,tmpreg);
  1167. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,sref);
  1168. end;
  1169. procedure thlcgobj.a_load_const_subsetref(list: TAsmlist; tosubsetsize: tdef; a: tcgint; const sref: tsubsetreference);
  1170. var
  1171. tmpreg: tregister;
  1172. slopt: tsubsetloadopt;
  1173. begin
  1174. { perform masking of the source value in advance }
  1175. slopt:=SL_REGNOSRCMASK;
  1176. if (sref.bitlen<>AIntBits) then
  1177. a:=tcgint(aword(a) and ((aword(1) shl sref.bitlen) -1));
  1178. if (
  1179. { broken x86 "x shl regbitsize = x" }
  1180. ((sref.bitlen<>AIntBits) and
  1181. ((aword(a) and ((aword(1) shl sref.bitlen)-1))=(aword(1) shl sref.bitlen)-1)) or
  1182. ((sref.bitlen=AIntBits) and
  1183. (a=-1))
  1184. ) then
  1185. slopt:=SL_SETMAX
  1186. else if (a=0) then
  1187. slopt:=SL_SETZERO;
  1188. if not(slopt in [SL_SETZERO,SL_SETMAX]) then
  1189. begin
  1190. tmpreg:=getintregister(list,tosubsetsize);
  1191. a_load_const_reg(list,tosubsetsize,a,tmpreg);
  1192. end
  1193. else
  1194. tmpreg:=NR_NO;
  1195. a_load_regconst_subsetref_intern(list,tosubsetsize,tosubsetsize,tmpreg,sref,slopt);
  1196. end;
  1197. procedure thlcgobj.a_load_subsetref_loc(list: TAsmlist; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const loc: tlocation);
  1198. begin
  1199. case loc.loc of
  1200. LOC_REFERENCE,LOC_CREFERENCE:
  1201. a_load_subsetref_ref(list,fromsubsetsize,tosize,sref,loc.reference);
  1202. LOC_REGISTER,LOC_CREGISTER:
  1203. a_load_subsetref_reg(list,fromsubsetsize,tosize,sref,loc.register);
  1204. LOC_SUBSETREG,LOC_CSUBSETREG:
  1205. a_load_subsetref_subsetreg(list,fromsubsetsize,tosize,sref,loc.sreg);
  1206. LOC_SUBSETREF,LOC_CSUBSETREF:
  1207. a_load_subsetref_subsetref(list,fromsubsetsize,tosize,sref,loc.sref);
  1208. else
  1209. internalerror(2010120407);
  1210. end;
  1211. end;
  1212. procedure thlcgobj.a_load_subsetref_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsref: tsubsetreference; const tosreg: tsubsetregister);
  1213. var
  1214. tmpreg: tregister;
  1215. begin
  1216. tmpreg:=getintregister(list,tosubsetsize);
  1217. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1218. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  1219. end;
  1220. procedure thlcgobj.a_load_subsetreg_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsreg: tsubsetregister; const tosref: tsubsetreference);
  1221. var
  1222. tmpreg: tregister;
  1223. begin
  1224. tmpreg := getintregister(list,tosubsetsize);
  1225. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  1226. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1227. end;
  1228. procedure thlcgobj.a_bit_test_reg_reg_reg(list: TAsmList; bitnumbersize, valuesize, destsize: tdef; bitnumber, value, destreg: tregister);
  1229. var
  1230. tmpvalue: tregister;
  1231. begin
  1232. tmpvalue:=getintregister(list,valuesize);
  1233. if (target_info.endian=endian_little) then
  1234. begin
  1235. { rotate value register "bitnumber" bits to the right }
  1236. a_op_reg_reg_reg(list,OP_SHR,valuesize,bitnumber,value,tmpvalue);
  1237. { extract the bit we want }
  1238. a_op_const_reg(list,OP_AND,valuesize,1,tmpvalue);
  1239. end
  1240. else
  1241. begin
  1242. { highest (leftmost) bit = bit 0 -> shl bitnumber results in wanted }
  1243. { bit in uppermost position, then move it to the lowest position }
  1244. { "and" is not necessary since combination of shl/shr will clear }
  1245. { all other bits }
  1246. a_op_reg_reg_reg(list,OP_SHL,valuesize,bitnumber,value,tmpvalue);
  1247. a_op_const_reg(list,OP_SHR,valuesize,valuesize.size*8-1,tmpvalue);
  1248. end;
  1249. a_load_reg_reg(list,valuesize,destsize,tmpvalue,destreg);
  1250. end;
  1251. procedure thlcgobj.a_bit_test_const_ref_reg(list: TAsmList; fromsize, destsize: tdef; bitnumber: aint; const ref: treference; destreg: tregister);
  1252. begin
  1253. a_load_subsetref_reg(list,u8inttype,destsize,get_bit_const_ref_sref(bitnumber,fromsize,ref),destreg);
  1254. end;
  1255. procedure thlcgobj.a_bit_test_const_reg_reg(list: TAsmList; setregsize, destsize: tdef; bitnumber: aint; setreg, destreg: tregister);
  1256. begin
  1257. a_load_subsetreg_reg(list,setregsize,destsize,get_bit_const_reg_sreg(setregsize,bitnumber,setreg),destreg);
  1258. end;
  1259. procedure thlcgobj.a_bit_test_const_subsetreg_reg(list: TAsmList; fromsubsetsize, destsize: tdef; bitnumber: aint; const setreg: tsubsetregister; destreg: tregister);
  1260. var
  1261. tmpsreg: tsubsetregister;
  1262. begin
  1263. { the first parameter is used to calculate the bit offset in }
  1264. { case of big endian, and therefore must be the size of the }
  1265. { set and not of the whole subsetreg }
  1266. tmpsreg:=get_bit_const_reg_sreg(fromsubsetsize,bitnumber,setreg.subsetreg);
  1267. { now fix the size of the subsetreg }
  1268. tmpsreg.subsetregsize:=setreg.subsetregsize;
  1269. { correct offset of the set in the subsetreg }
  1270. inc(tmpsreg.startbit,setreg.startbit);
  1271. a_load_subsetreg_reg(list,fromsubsetsize,destsize,tmpsreg,destreg);
  1272. end;
  1273. procedure thlcgobj.a_bit_test_reg_ref_reg(list: TAsmList; bitnumbersize, refsize, destsize: tdef; bitnumber: tregister; const ref: treference; destreg: tregister);
  1274. begin
  1275. a_load_subsetref_reg(list,u8inttype,destsize,get_bit_reg_ref_sref(list,bitnumbersize,refsize,bitnumber,ref),destreg);
  1276. end;
  1277. procedure thlcgobj.a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, locsize, destsize: tdef; bitnumber: tregister; const loc: tlocation; destreg: tregister);
  1278. var
  1279. tmpreg: tregister;
  1280. begin
  1281. case loc.loc of
  1282. LOC_REFERENCE,LOC_CREFERENCE:
  1283. a_bit_test_reg_ref_reg(list,bitnumbersize,locsize,destsize,bitnumber,loc.reference,destreg);
  1284. LOC_REGISTER,LOC_CREGISTER,
  1285. LOC_SUBSETREG,LOC_CSUBSETREG,
  1286. LOC_CONSTANT:
  1287. begin
  1288. case loc.loc of
  1289. LOC_REGISTER,LOC_CREGISTER:
  1290. tmpreg:=loc.register;
  1291. LOC_SUBSETREG,LOC_CSUBSETREG:
  1292. begin
  1293. tmpreg:=getintregister(list,locsize);
  1294. a_load_subsetreg_reg(list,locsize,locsize,loc.sreg,tmpreg);
  1295. end;
  1296. LOC_CONSTANT:
  1297. begin
  1298. tmpreg:=getintregister(list,locsize);
  1299. a_load_const_reg(list,locsize,loc.value,tmpreg);
  1300. end;
  1301. end;
  1302. a_bit_test_reg_reg_reg(list,bitnumbersize,locsize,destsize,bitnumber,tmpreg,destreg);
  1303. end;
  1304. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1305. else
  1306. internalerror(2010120411);
  1307. end;
  1308. end;
  1309. procedure thlcgobj.a_bit_test_const_loc_reg(list: TAsmList; locsize, destsize: tdef; bitnumber: aint; const loc: tlocation; destreg: tregister);
  1310. begin
  1311. case loc.loc of
  1312. LOC_REFERENCE,LOC_CREFERENCE:
  1313. a_bit_test_const_ref_reg(list,locsize,destsize,bitnumber,loc.reference,destreg);
  1314. LOC_REGISTER,LOC_CREGISTER:
  1315. a_bit_test_const_reg_reg(list,locsize,destsize,bitnumber,loc.register,destreg);
  1316. LOC_SUBSETREG,LOC_CSUBSETREG:
  1317. a_bit_test_const_subsetreg_reg(list,locsize,destsize,bitnumber,loc.sreg,destreg);
  1318. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1319. else
  1320. internalerror(2010120410);
  1321. end;
  1322. end;
  1323. procedure thlcgobj.a_bit_set_reg_reg(list: TAsmList; doset: boolean; bitnumbersize, destsize: tdef; bitnumber, dest: tregister);
  1324. var
  1325. tmpvalue: tregister;
  1326. begin
  1327. tmpvalue:=getintregister(list,destsize);
  1328. if (target_info.endian=endian_little) then
  1329. begin
  1330. a_load_const_reg(list,destsize,1,tmpvalue);
  1331. { rotate bit "bitnumber" bits to the left }
  1332. a_op_reg_reg(list,OP_SHL,destsize,bitnumber,tmpvalue);
  1333. end
  1334. else
  1335. begin
  1336. { highest (leftmost) bit = bit 0 -> "$80/$8000/$80000000/ ... }
  1337. { shr bitnumber" results in correct mask }
  1338. a_load_const_reg(list,destsize,1 shl (destsize.size*8-1),tmpvalue);
  1339. a_op_reg_reg(list,OP_SHR,destsize,bitnumber,tmpvalue);
  1340. end;
  1341. { set/clear the bit we want }
  1342. if doset then
  1343. a_op_reg_reg(list,OP_OR,destsize,tmpvalue,dest)
  1344. else
  1345. begin
  1346. a_op_reg_reg(list,OP_NOT,destsize,tmpvalue,tmpvalue);
  1347. a_op_reg_reg(list,OP_AND,destsize,tmpvalue,dest)
  1348. end;
  1349. end;
  1350. procedure thlcgobj.a_bit_set_const_ref(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; const ref: treference);
  1351. begin
  1352. a_load_const_subsetref(list,u8inttype,ord(doset),get_bit_const_ref_sref(bitnumber,destsize,ref));
  1353. end;
  1354. procedure thlcgobj.a_bit_set_const_reg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; destreg: tregister);
  1355. begin
  1356. a_load_const_subsetreg(list,u8inttype,ord(doset),get_bit_const_reg_sreg(destsize,bitnumber,destreg));
  1357. end;
  1358. procedure thlcgobj.a_bit_set_const_subsetreg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; const destreg: tsubsetregister);
  1359. var
  1360. tmpsreg: tsubsetregister;
  1361. begin
  1362. { the first parameter is used to calculate the bit offset in }
  1363. { case of big endian, and therefore must be the size of the }
  1364. { set and not of the whole subsetreg }
  1365. tmpsreg:=get_bit_const_reg_sreg(destsize,bitnumber,destreg.subsetreg);
  1366. { now fix the size of the subsetreg }
  1367. tmpsreg.subsetregsize:=destreg.subsetregsize;
  1368. { correct offset of the set in the subsetreg }
  1369. inc(tmpsreg.startbit,destreg.startbit);
  1370. a_load_const_subsetreg(list,u8inttype,ord(doset),tmpsreg);
  1371. end;
  1372. procedure thlcgobj.a_bit_set_reg_ref(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const ref: treference);
  1373. begin
  1374. a_load_const_subsetref(list,u8inttype,ord(doset),get_bit_reg_ref_sref(list,fromsize,tosize,bitnumber,ref));
  1375. end;
  1376. procedure thlcgobj.a_bit_set_reg_loc(list: TAsmList; doset: boolean; regsize, tosize: tdef; bitnumber: tregister; const loc: tlocation);
  1377. var
  1378. tmpreg: tregister;
  1379. begin
  1380. case loc.loc of
  1381. LOC_REFERENCE:
  1382. a_bit_set_reg_ref(list,doset,regsize,tosize,bitnumber,loc.reference);
  1383. LOC_CREGISTER:
  1384. a_bit_set_reg_reg(list,doset,regsize,tosize,bitnumber,loc.register);
  1385. { e.g. a 2-byte set in a record regvar }
  1386. LOC_CSUBSETREG:
  1387. begin
  1388. { hard to do in-place in a generic way, so operate on a copy }
  1389. tmpreg:=getintregister(list,tosize);
  1390. a_load_subsetreg_reg(list,tosize,tosize,loc.sreg,tmpreg);
  1391. a_bit_set_reg_reg(list,doset,regsize,tosize,bitnumber,tmpreg);
  1392. a_load_reg_subsetreg(list,tosize,tosize,tmpreg,loc.sreg);
  1393. end;
  1394. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1395. else
  1396. internalerror(2010120408)
  1397. end;
  1398. end;
  1399. procedure thlcgobj.a_bit_set_const_loc(list: TAsmList; doset: boolean; tosize: tdef; bitnumber: aint; const loc: tlocation);
  1400. begin
  1401. case loc.loc of
  1402. LOC_REFERENCE:
  1403. a_bit_set_const_ref(list,doset,tosize,bitnumber,loc.reference);
  1404. LOC_CREGISTER:
  1405. a_bit_set_const_reg(list,doset,tosize,bitnumber,loc.register);
  1406. LOC_CSUBSETREG:
  1407. a_bit_set_const_subsetreg(list,doset,tosize,bitnumber,loc.sreg);
  1408. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1409. else
  1410. internalerror(2010120409)
  1411. end;
  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. if not setflags then
  2360. a_op_const_reg_reg(list,op,size,a,src,dst)
  2361. else
  2362. internalerror(2010122910);
  2363. end;
  2364. procedure thlcgobj.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  2365. begin
  2366. if not setflags then
  2367. a_op_reg_reg_reg(list,op,size,src1,src2,dst)
  2368. else
  2369. internalerror(2010122911);
  2370. end;
  2371. procedure thlcgobj.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  2372. var
  2373. tmpreg: tregister;
  2374. begin
  2375. tmpreg:=getintregister(list,size);
  2376. a_load_const_reg(list,size,a,tmpreg);
  2377. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2378. end;
  2379. procedure thlcgobj.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const ref: treference; l: tasmlabel);
  2380. var
  2381. tmpreg: tregister;
  2382. begin
  2383. tmpreg:=getintregister(list,size);
  2384. a_load_ref_reg(list,size,size,ref,tmpreg);
  2385. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2386. end;
  2387. procedure thlcgobj.a_cmp_const_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const loc: tlocation; l: tasmlabel);
  2388. var
  2389. tmpreg: tregister;
  2390. begin
  2391. case loc.loc of
  2392. LOC_REGISTER,LOC_CREGISTER:
  2393. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  2394. LOC_REFERENCE,LOC_CREFERENCE:
  2395. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  2396. LOC_SUBSETREG, LOC_CSUBSETREG:
  2397. begin
  2398. tmpreg:=getintregister(list,size);
  2399. a_load_subsetreg_reg(list,size,size,loc.sreg,tmpreg);
  2400. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2401. end;
  2402. LOC_SUBSETREF, LOC_CSUBSETREF:
  2403. begin
  2404. tmpreg:=getintregister(list,size);
  2405. a_load_subsetref_reg(list,size,size,loc.sref,tmpreg);
  2406. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2407. end;
  2408. else
  2409. internalerror(2010120430);
  2410. end;
  2411. end;
  2412. procedure thlcgobj.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  2413. var
  2414. tmpreg: tregister;
  2415. begin
  2416. tmpreg:=getintregister(list,size);
  2417. a_load_ref_reg(list,size,size,ref,tmpreg);
  2418. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2419. end;
  2420. procedure thlcgobj.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  2421. var
  2422. tmpreg: tregister;
  2423. begin
  2424. tmpreg:=getintregister(list,size);
  2425. a_load_ref_reg(list,size,size,ref,tmpreg);
  2426. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  2427. end;
  2428. procedure thlcgobj.a_cmp_subsetreg_reg_label(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; l: tasmlabel);
  2429. var
  2430. tmpreg: tregister;
  2431. begin
  2432. tmpreg:=getintregister(list,cmpsize);
  2433. a_load_subsetreg_reg(list,fromsubsetsize,cmpsize,sreg,tmpreg);
  2434. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  2435. end;
  2436. procedure thlcgobj.a_cmp_subsetref_reg_label(list: TAsmList; fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; l: tasmlabel);
  2437. var
  2438. tmpreg: tregister;
  2439. begin
  2440. tmpreg:=getintregister(list,cmpsize);
  2441. a_load_subsetref_reg(list,fromsubsetsize,cmpsize,sref,tmpreg);
  2442. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  2443. end;
  2444. procedure thlcgobj.a_cmp_loc_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const loc: tlocation; reg: tregister; l: tasmlabel);
  2445. begin
  2446. case loc.loc of
  2447. LOC_REGISTER,
  2448. LOC_CREGISTER:
  2449. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  2450. LOC_REFERENCE,
  2451. LOC_CREFERENCE :
  2452. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  2453. LOC_CONSTANT:
  2454. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  2455. LOC_SUBSETREG,
  2456. LOC_CSUBSETREG:
  2457. a_cmp_subsetreg_reg_label(list,size,size,cmp_op,loc.sreg,reg,l);
  2458. LOC_SUBSETREF,
  2459. LOC_CSUBSETREF:
  2460. a_cmp_subsetref_reg_label(list,size,size,cmp_op,loc.sref,reg,l);
  2461. else
  2462. internalerror(2010120431);
  2463. end;
  2464. end;
  2465. procedure thlcgobj.a_cmp_reg_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const loc: tlocation; l: tasmlabel);
  2466. begin
  2467. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  2468. end;
  2469. procedure thlcgobj.a_cmp_ref_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; const loc: tlocation; l: tasmlabel);
  2470. var
  2471. tmpreg: tregister;
  2472. begin
  2473. case loc.loc of
  2474. LOC_REGISTER,LOC_CREGISTER:
  2475. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  2476. LOC_REFERENCE,LOC_CREFERENCE:
  2477. begin
  2478. tmpreg:=getintregister(list,size);
  2479. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2480. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  2481. end;
  2482. LOC_CONSTANT:
  2483. begin
  2484. a_cmp_const_ref_label(list,size,swap_opcmp(cmp_op),loc.value,ref,l);
  2485. end;
  2486. LOC_SUBSETREG, LOC_CSUBSETREG:
  2487. begin
  2488. tmpreg:=getintregister(list,size);
  2489. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2490. a_cmp_subsetreg_reg_label(list,size,size,swap_opcmp(cmp_op),loc.sreg,tmpreg,l);
  2491. end;
  2492. LOC_SUBSETREF, LOC_CSUBSETREF:
  2493. begin
  2494. tmpreg:=getintregister(list,size);
  2495. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2496. a_cmp_subsetref_reg_label(list,size,size,swap_opcmp(cmp_op),loc.sref,tmpreg,l);
  2497. end;
  2498. else
  2499. internalerror(2010120432);
  2500. end;
  2501. end;
  2502. procedure thlcgobj.g_maybe_testself(list: TAsmList; selftype: tdef; reg: tregister);
  2503. var
  2504. OKLabel : tasmlabel;
  2505. cgpara1 : TCGPara;
  2506. begin
  2507. if (cs_check_object in current_settings.localswitches) or
  2508. (cs_check_range in current_settings.localswitches) then
  2509. begin
  2510. current_asmdata.getjumplabel(oklabel);
  2511. a_cmp_const_reg_label(list,selftype,OC_NE,0,reg,oklabel);
  2512. cgpara1.init;
  2513. paramanager.getintparaloc(pocall_default,1,s32inttype,cgpara1);
  2514. a_load_const_cgpara(list,s32inttype,aint(210),cgpara1);
  2515. paramanager.freecgpara(list,cgpara1);
  2516. g_call_system_proc(list,'fpc_handleerror',nil);
  2517. cgpara1.done;
  2518. a_label(list,oklabel);
  2519. end;
  2520. end;
  2521. procedure thlcgobj.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  2522. begin
  2523. {
  2524. if use_vectorfpu(size) then
  2525. a_loadmm_ref_ref()
  2526. else
  2527. }
  2528. if size.typ<>floatdef then
  2529. a_load_ref_ref(list,size,size,source,dest)
  2530. else
  2531. a_loadfpu_ref_ref(list,size,size,source,dest);
  2532. end;
  2533. procedure thlcgobj.g_concatcopy_unaligned(list: TAsmList; size: tdef; const source, dest: treference);
  2534. begin
  2535. g_concatcopy(list,size,source,dest);
  2536. end;
  2537. procedure thlcgobj.g_copyshortstring(list: TAsmList; const source, dest: treference; strdef: tstringdef);
  2538. var
  2539. cgpara1,cgpara2,cgpara3 : TCGPara;
  2540. begin
  2541. cgpara1.init;
  2542. cgpara2.init;
  2543. cgpara3.init;
  2544. paramanager.getintparaloc(pocall_default,1,voidpointertype,cgpara1);
  2545. paramanager.getintparaloc(pocall_default,2,voidpointertype,cgpara2);
  2546. paramanager.getintparaloc(pocall_default,3,s32inttype,cgpara3);
  2547. a_loadaddr_ref_cgpara(list,strdef,dest,cgpara3);
  2548. a_loadaddr_ref_cgpara(list,strdef,source,cgpara2);
  2549. a_load_const_cgpara(list,s32inttype,strdef.len,cgpara1);
  2550. paramanager.freecgpara(list,cgpara3);
  2551. paramanager.freecgpara(list,cgpara2);
  2552. paramanager.freecgpara(list,cgpara1);
  2553. g_call_system_proc(list,'fpc_shortstr_assign',nil);
  2554. cgpara3.done;
  2555. cgpara2.done;
  2556. cgpara1.done;
  2557. end;
  2558. procedure thlcgobj.g_copyvariant(list: TAsmList; const source, dest: treference; vardef: tvariantdef);
  2559. var
  2560. cgpara1,cgpara2 : TCGPara;
  2561. pvardata : tdef;
  2562. begin
  2563. cgpara1.init;
  2564. cgpara2.init;
  2565. pvardata:=getpointerdef(search_system_type('TVARDATA').typedef);
  2566. paramanager.getintparaloc(pocall_default,1,pvardata,cgpara1);
  2567. paramanager.getintparaloc(pocall_default,2,pvardata,cgpara2);
  2568. a_loadaddr_ref_cgpara(list,vardef,dest,cgpara2);
  2569. a_loadaddr_ref_cgpara(list,vardef,source,cgpara1);
  2570. paramanager.freecgpara(list,cgpara2);
  2571. paramanager.freecgpara(list,cgpara1);
  2572. g_call_system_proc(list,'fpc_variant_copy_overwrite',nil);
  2573. cgpara2.done;
  2574. cgpara1.done;
  2575. end;
  2576. procedure thlcgobj.g_incrrefcount(list: TAsmList; t: tdef; const ref: treference);
  2577. var
  2578. href : treference;
  2579. incrfunc : string;
  2580. cgpara1,cgpara2 : TCGPara;
  2581. begin
  2582. cgpara1.init;
  2583. cgpara2.init;
  2584. paramanager.getintparaloc(pocall_default,1,voidpointertype,cgpara1);
  2585. paramanager.getintparaloc(pocall_default,2,voidpointertype,cgpara2);
  2586. if is_interfacecom_or_dispinterface(t) then
  2587. incrfunc:='fpc_intf_incr_ref'
  2588. else if is_ansistring(t) then
  2589. incrfunc:='fpc_ansistr_incr_ref'
  2590. else if is_widestring(t) then
  2591. incrfunc:='fpc_widestr_incr_ref'
  2592. else if is_unicodestring(t) then
  2593. incrfunc:='fpc_unicodestr_incr_ref'
  2594. else if is_dynamic_array(t) then
  2595. incrfunc:='fpc_dynarray_incr_ref'
  2596. else
  2597. incrfunc:='';
  2598. { call the special incr function or the generic addref }
  2599. if incrfunc<>'' then
  2600. begin
  2601. { widestrings aren't ref. counted on all platforms so we need the address
  2602. to create a real copy }
  2603. if is_widestring(t) then
  2604. a_loadaddr_ref_cgpara(list,t,ref,cgpara1)
  2605. else
  2606. { these functions get the pointer by value }
  2607. a_load_ref_cgpara(list,t,ref,cgpara1);
  2608. paramanager.freecgpara(list,cgpara1);
  2609. g_call_system_proc(list,incrfunc,nil);
  2610. end
  2611. else
  2612. begin
  2613. if is_open_array(t) then
  2614. InternalError(201103054);
  2615. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  2616. a_loadaddr_ref_cgpara(list,voidpointertype,href,cgpara2);
  2617. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  2618. paramanager.freecgpara(list,cgpara1);
  2619. paramanager.freecgpara(list,cgpara2);
  2620. g_call_system_proc(list,'fpc_addref',nil);
  2621. end;
  2622. cgpara2.done;
  2623. cgpara1.done;
  2624. end;
  2625. procedure thlcgobj.g_initialize(list: TAsmList; t: tdef; const ref: treference);
  2626. var
  2627. href : treference;
  2628. cgpara1,cgpara2 : TCGPara;
  2629. pvardata : tdef;
  2630. begin
  2631. cgpara1.init;
  2632. cgpara2.init;
  2633. if is_ansistring(t) or
  2634. is_widestring(t) or
  2635. is_unicodestring(t) or
  2636. is_interfacecom_or_dispinterface(t) or
  2637. is_dynamic_array(t) then
  2638. a_load_const_ref(list,t,0,ref)
  2639. else if t.typ=variantdef then
  2640. begin
  2641. pvardata:=getpointerdef(search_system_type('TVARDATA').typedef);
  2642. paramanager.getintparaloc(pocall_default,1,pvardata,cgpara1);
  2643. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  2644. paramanager.freecgpara(list,cgpara1);
  2645. g_call_system_proc(list,'fpc_variant_init',nil);
  2646. end
  2647. else
  2648. begin
  2649. if is_open_array(t) then
  2650. InternalError(201103052);
  2651. paramanager.getintparaloc(pocall_default,1,voidpointertype,cgpara1);
  2652. paramanager.getintparaloc(pocall_default,2,voidpointertype,cgpara2);
  2653. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  2654. a_loadaddr_ref_cgpara(list,voidpointertype,href,cgpara2);
  2655. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  2656. paramanager.freecgpara(list,cgpara1);
  2657. paramanager.freecgpara(list,cgpara2);
  2658. g_call_system_proc(list,'fpc_initialize',nil);
  2659. end;
  2660. cgpara1.done;
  2661. cgpara2.done;
  2662. end;
  2663. procedure thlcgobj.g_finalize(list: TAsmList; t: tdef; const ref: treference);
  2664. var
  2665. href : treference;
  2666. cgpara1,cgpara2 : TCGPara;
  2667. paratype : tdef;
  2668. decrfunc : string;
  2669. dynarr: boolean;
  2670. begin
  2671. paratype:=getpointerdef(voidpointertype);
  2672. if is_interfacecom_or_dispinterface(t) then
  2673. decrfunc:='fpc_intf_decr_ref'
  2674. else if is_ansistring(t) then
  2675. decrfunc:='fpc_ansistr_decr_ref'
  2676. else if is_widestring(t) then
  2677. decrfunc:='fpc_widestr_decr_ref'
  2678. else if is_unicodestring(t) then
  2679. decrfunc:='fpc_unicodestr_decr_ref'
  2680. else if t.typ=variantdef then
  2681. begin
  2682. paratype:=getpointerdef(search_system_type('TVARDATA').typedef);
  2683. decrfunc:='fpc_variant_clear'
  2684. end
  2685. else
  2686. begin
  2687. cgpara1.init;
  2688. cgpara2.init;
  2689. if is_open_array(t) then
  2690. InternalError(201103051);
  2691. dynarr:=is_dynamic_array(t);
  2692. { fpc_finalize takes a pointer value parameter, fpc_dynarray_clear a
  2693. pointer var parameter }
  2694. if not dynarr then
  2695. paratype:=voidpointertype;
  2696. paramanager.getintparaloc(pocall_default,1,paratype,cgpara1);
  2697. paramanager.getintparaloc(pocall_default,2,voidpointertype,cgpara2);
  2698. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  2699. a_loadaddr_ref_cgpara(list,voidpointertype,href,cgpara2);
  2700. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  2701. paramanager.freecgpara(list,cgpara1);
  2702. paramanager.freecgpara(list,cgpara2);
  2703. if dynarr then
  2704. g_call_system_proc(list,'fpc_dynarray_clear',nil)
  2705. else
  2706. g_call_system_proc(list,'fpc_finalize',nil);
  2707. cgpara1.done;
  2708. cgpara2.done;
  2709. exit;
  2710. end;
  2711. cgpara1.init;
  2712. paramanager.getintparaloc(pocall_default,1,paratype,cgpara1);
  2713. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  2714. paramanager.freecgpara(list,cgpara1);
  2715. g_call_system_proc(list,decrfunc,nil);
  2716. cgpara1.done;
  2717. end;
  2718. procedure thlcgobj.g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string);
  2719. var
  2720. cgpara1,cgpara2,cgpara3: TCGPara;
  2721. href: TReference;
  2722. hreg, lenreg: TRegister;
  2723. begin
  2724. cgpara1.init;
  2725. cgpara2.init;
  2726. cgpara3.init;
  2727. paramanager.getintparaloc(pocall_default,1,voidpointertype,cgpara1);
  2728. paramanager.getintparaloc(pocall_default,2,voidpointertype,cgpara2);
  2729. paramanager.getintparaloc(pocall_default,3,ptrsinttype,cgpara3);
  2730. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  2731. if highloc.loc=LOC_CONSTANT then
  2732. a_load_const_cgpara(list,ptrsinttype,highloc.value+1,cgpara3)
  2733. else
  2734. begin
  2735. if highloc.loc in [LOC_REGISTER,LOC_CREGISTER] then
  2736. hreg:=highloc.register
  2737. else
  2738. begin
  2739. hreg:=getintregister(list,ptrsinttype);
  2740. a_load_loc_reg(list,ptrsinttype,ptrsinttype,highloc,hreg);
  2741. end;
  2742. { increment, converts high(x) to length(x) }
  2743. lenreg:=getintregister(list,ptrsinttype);
  2744. a_op_const_reg_reg(list,OP_ADD,ptrsinttype,1,hreg,lenreg);
  2745. a_load_reg_cgpara(list,ptrsinttype,lenreg,cgpara3);
  2746. end;
  2747. a_loadaddr_ref_cgpara(list,voidpointertype,href,cgpara2);
  2748. a_loadaddr_ref_cgpara(list,t,ref,cgpara1);
  2749. paramanager.freecgpara(list,cgpara1);
  2750. paramanager.freecgpara(list,cgpara2);
  2751. paramanager.freecgpara(list,cgpara3);
  2752. g_call_system_proc(list,name,nil);
  2753. cgpara3.done;
  2754. cgpara2.done;
  2755. cgpara1.done;
  2756. end;
  2757. procedure thlcgobj.g_rangecheck(list: TAsmList; const l: tlocation; fromdef, todef: tdef);
  2758. var
  2759. {$if defined(cpu64bitalu) or defined(cpu32bitalu)}
  2760. aintmax: aint;
  2761. {$else}
  2762. aintmax: longint;
  2763. {$endif}
  2764. neglabel : tasmlabel;
  2765. hreg : tregister;
  2766. lto,hto,
  2767. lfrom,hfrom : TConstExprInt;
  2768. fromsize, tosize: cardinal;
  2769. maxdef: tdef;
  2770. from_signed, to_signed: boolean;
  2771. begin
  2772. { range checking on and range checkable value? }
  2773. if not(cs_check_range in current_settings.localswitches) or
  2774. not(fromdef.typ in [orddef,enumdef]) or
  2775. { C-style booleans can't really fail range checks, }
  2776. { all values are always valid }
  2777. is_cbool(todef) then
  2778. exit;
  2779. {$if not defined(cpuhighleveltarget) and not defined(cpu64bitalu)}
  2780. { handle 64bit rangechecks separate for 32bit processors }
  2781. if is_64bit(fromdef) or is_64bit(todef) then
  2782. begin
  2783. cg64.g_rangecheck64(list,l,fromdef,todef);
  2784. exit;
  2785. end;
  2786. {$endif ndef cpuhighleveltarget and ndef cpu64bitalu}
  2787. { only check when assigning to scalar, subranges are different, }
  2788. { when todef=fromdef then the check is always generated }
  2789. getrange(fromdef,lfrom,hfrom);
  2790. getrange(todef,lto,hto);
  2791. from_signed := is_signed(fromdef);
  2792. to_signed := is_signed(todef);
  2793. { check the rangedef of the array, not the array itself }
  2794. { (only change now, since getrange needs the arraydef) }
  2795. if (todef.typ = arraydef) then
  2796. todef := tarraydef(todef).rangedef;
  2797. { no range check if from and to are equal and are both longint/dword }
  2798. { (if we have a 32bit processor) or int64/qword, since such }
  2799. { operations can at most cause overflows (JM) }
  2800. { Note that these checks are mostly processor independent, they only }
  2801. { have to be changed once we introduce 64bit subrange types }
  2802. {$if defined(cpuhighleveltarget) or defined(cpu64bitalu)}
  2803. if (fromdef=todef) and
  2804. (fromdef.typ=orddef) and
  2805. (((((torddef(fromdef).ordtype=s64bit) and
  2806. (lfrom = low(int64)) and
  2807. (hfrom = high(int64))) or
  2808. ((torddef(fromdef).ordtype=u64bit) and
  2809. (lfrom = low(qword)) and
  2810. (hfrom = high(qword))) or
  2811. ((torddef(fromdef).ordtype=scurrency) and
  2812. (lfrom = low(int64)) and
  2813. (hfrom = high(int64)))))) then
  2814. exit;
  2815. {$endif cpuhighleveltarget or cpu64bitalu}
  2816. { 32 bit operations are automatically widened to 64 bit on 64 bit addr
  2817. targets }
  2818. {$ifdef cpu32bitaddr}
  2819. if (fromdef = todef) and
  2820. (fromdef.typ=orddef) and
  2821. (((((torddef(fromdef).ordtype = s32bit) and
  2822. (lfrom = int64(low(longint))) and
  2823. (hfrom = int64(high(longint)))) or
  2824. ((torddef(fromdef).ordtype = u32bit) and
  2825. (lfrom = low(cardinal)) and
  2826. (hfrom = high(cardinal)))))) then
  2827. exit;
  2828. {$endif cpu32bitaddr}
  2829. { optimize some range checks away in safe cases }
  2830. fromsize := fromdef.size;
  2831. tosize := todef.size;
  2832. if ((from_signed = to_signed) or
  2833. (not from_signed)) and
  2834. (lto<=lfrom) and (hto>=hfrom) and
  2835. (fromsize <= tosize) then
  2836. begin
  2837. { if fromsize < tosize, and both have the same signed-ness or }
  2838. { fromdef is unsigned, then all bit patterns from fromdef are }
  2839. { valid for todef as well }
  2840. if (fromsize < tosize) then
  2841. exit;
  2842. if (fromsize = tosize) and
  2843. (from_signed = to_signed) then
  2844. { only optimize away if all bit patterns which fit in fromsize }
  2845. { are valid for the todef }
  2846. begin
  2847. {$ifopt Q+}
  2848. {$define overflowon}
  2849. {$Q-}
  2850. {$endif}
  2851. {$ifopt R+}
  2852. {$define rangeon}
  2853. {$R-}
  2854. {$endif}
  2855. if to_signed then
  2856. begin
  2857. { calculation of the low/high ranges must not overflow 64 bit
  2858. otherwise we end up comparing with zero for 64 bit data types on
  2859. 64 bit processors }
  2860. if (lto = (int64(-1) << (tosize * 8 - 1))) and
  2861. (hto = (-((int64(-1) << (tosize * 8 - 1))+1))) then
  2862. exit
  2863. end
  2864. else
  2865. begin
  2866. { calculation of the low/high ranges must not overflow 64 bit
  2867. otherwise we end up having all zeros for 64 bit data types on
  2868. 64 bit processors }
  2869. if (lto = 0) and
  2870. (qword(hto) = (qword(-1) >> (64-(tosize * 8))) ) then
  2871. exit
  2872. end;
  2873. {$ifdef overflowon}
  2874. {$Q+}
  2875. {$undef overflowon}
  2876. {$endif}
  2877. {$ifdef rangeon}
  2878. {$R+}
  2879. {$undef rangeon}
  2880. {$endif}
  2881. end
  2882. end;
  2883. { depending on the types involved, we perform the range check for 64 or
  2884. for 32 bit }
  2885. if fromsize=8 then
  2886. maxdef:=fromdef
  2887. else
  2888. maxdef:=todef;
  2889. {$if sizeof(aintmax) = 8}
  2890. if maxdef.size=8 then
  2891. aintmax:=high(int64)
  2892. else
  2893. {$endif}
  2894. begin
  2895. aintmax:=high(longint);
  2896. maxdef:=u32inttype;
  2897. end;
  2898. { generate the rangecheck code for the def where we are going to }
  2899. { store the result }
  2900. { use the trick that }
  2901. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  2902. { To be able to do that, we have to make sure however that either }
  2903. { fromdef and todef are both signed or unsigned, or that we leave }
  2904. { the parts < 0 and > maxlongint out }
  2905. if from_signed xor to_signed then
  2906. begin
  2907. if from_signed then
  2908. { from is signed, to is unsigned }
  2909. begin
  2910. { if high(from) < 0 -> always range error }
  2911. if (hfrom < 0) or
  2912. { if low(to) > maxlongint also range error }
  2913. (lto > aintmax) then
  2914. begin
  2915. g_call_system_proc(list,'fpc_rangeerror',nil);
  2916. exit
  2917. end;
  2918. { from is signed and to is unsigned -> when looking at to }
  2919. { as an signed value, it must be < maxaint (otherwise }
  2920. { it will become negative, which is invalid since "to" is unsigned) }
  2921. if hto > aintmax then
  2922. hto := aintmax;
  2923. end
  2924. else
  2925. { from is unsigned, to is signed }
  2926. begin
  2927. if (lfrom > aintmax) or
  2928. (hto < 0) then
  2929. begin
  2930. g_call_system_proc(list,'fpc_rangeerror',nil);
  2931. exit
  2932. end;
  2933. { from is unsigned and to is signed -> when looking at to }
  2934. { as an unsigned value, it must be >= 0 (since negative }
  2935. { values are the same as values > maxlongint) }
  2936. if lto < 0 then
  2937. lto := 0;
  2938. end;
  2939. end;
  2940. hreg:=getintregister(list,maxdef);
  2941. a_load_loc_reg(list,fromdef,maxdef,l,hreg);
  2942. a_op_const_reg(list,OP_SUB,maxdef,tcgint(int64(lto)),hreg);
  2943. current_asmdata.getjumplabel(neglabel);
  2944. {
  2945. if from_signed then
  2946. a_cmp_const_reg_label(list,OS_INT,OC_GTE,aint(hto-lto),hreg,neglabel)
  2947. else
  2948. }
  2949. if qword(hto-lto)>qword(aintmax) then
  2950. a_cmp_const_reg_label(list,maxdef,OC_BE,aintmax,hreg,neglabel)
  2951. else
  2952. a_cmp_const_reg_label(list,maxdef,OC_BE,tcgint(int64(hto-lto)),hreg,neglabel);
  2953. g_call_system_proc(list,'fpc_rangeerror',nil);
  2954. a_label(list,neglabel);
  2955. end;
  2956. procedure thlcgobj.g_profilecode(list: TAsmList);
  2957. begin
  2958. end;
  2959. procedure thlcgobj.g_allocload_reg_reg(list: TAsmList; regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  2960. begin
  2961. case regtyp of
  2962. R_INTREGISTER:
  2963. toreg:=getintregister(list,regsize);
  2964. R_ADDRESSREGISTER:
  2965. toreg:=getaddressregister(list,regsize);
  2966. R_FPUREGISTER:
  2967. toreg:=getfpuregister(list,regsize);
  2968. end;
  2969. a_load_reg_reg(list,regsize,regsize,fromreg,toreg);
  2970. end;
  2971. procedure thlcgobj.g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation);
  2972. procedure handle_reg_move(regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  2973. begin
  2974. case regtyp of
  2975. R_INTREGISTER:
  2976. toreg:=getintregister(list,regsize);
  2977. R_ADDRESSREGISTER:
  2978. toreg:=getaddressregister(list,regsize);
  2979. R_FPUREGISTER:
  2980. toreg:=getfpuregister(list,regsize);
  2981. end;
  2982. a_load_reg_reg(list,regsize,regsize,fromreg,toreg);
  2983. end;
  2984. begin
  2985. toloc:=fromloc;
  2986. case fromloc.loc of
  2987. { volatile location, can't get a permanent reference }
  2988. LOC_REGISTER,
  2989. LOC_FPUREGISTER:
  2990. internalerror(2012012702);
  2991. LOC_CONSTANT:
  2992. { finished }
  2993. ;
  2994. LOC_CREGISTER:
  2995. handle_reg_move(def,fromloc.reference.index,toloc.reference.index,R_INTREGISTER);
  2996. LOC_CFPUREGISTER:
  2997. handle_reg_move(def,fromloc.reference.index,toloc.reference.index,R_FPUREGISTER);
  2998. { although LOC_CREFERENCE cannot be an lvalue, we may want to take a
  2999. reference to such a location for multiple reading }
  3000. LOC_CREFERENCE,
  3001. LOC_REFERENCE:
  3002. begin
  3003. if (fromloc.reference.base<>NR_NO) and
  3004. (fromloc.reference.base<>current_procinfo.framepointer) and
  3005. (fromloc.reference.base<>NR_STACK_POINTER_REG) then
  3006. handle_reg_move(voidpointertype,fromloc.reference.base,toloc.reference.base,getregtype(fromloc.reference.base));
  3007. if (fromloc.reference.index<>NR_NO) and
  3008. (fromloc.reference.index<>current_procinfo.framepointer) and
  3009. (fromloc.reference.index<>NR_STACK_POINTER_REG) then
  3010. handle_reg_move(voidpointertype,fromloc.reference.index,toloc.reference.index,getregtype(fromloc.reference.index));
  3011. end;
  3012. else
  3013. internalerror(2012012701);
  3014. end;
  3015. end;
  3016. procedure thlcgobj.location_force_reg(list: TAsmList; var l: tlocation; src_size, dst_size: tdef; maybeconst: boolean);
  3017. var
  3018. hregister,
  3019. hregister2: tregister;
  3020. hl : tasmlabel;
  3021. oldloc : tlocation;
  3022. begin
  3023. oldloc:=l;
  3024. hregister:=getregisterfordef(list,dst_size);
  3025. { load value in new register }
  3026. case l.loc of
  3027. {$ifdef cpuflags}
  3028. LOC_FLAGS :
  3029. g_flags2reg(list,dst_size,l.resflags,hregister);
  3030. {$endif cpuflags}
  3031. LOC_JUMP :
  3032. begin
  3033. a_label(list,current_procinfo.CurrTrueLabel);
  3034. a_load_const_reg(list,dst_size,1,hregister);
  3035. current_asmdata.getjumplabel(hl);
  3036. a_jmp_always(list,hl);
  3037. a_label(list,current_procinfo.CurrFalseLabel);
  3038. a_load_const_reg(list,dst_size,0,hregister);
  3039. a_label(list,hl);
  3040. end;
  3041. else
  3042. begin
  3043. { load_loc_reg can only handle size >= l.size, when the
  3044. new size is smaller then we need to adjust the size
  3045. of the orignal and maybe recalculate l.register for i386 }
  3046. if (dst_size.size<src_size.size) then
  3047. begin
  3048. hregister2:=getregisterfordef(list,src_size);
  3049. { prevent problems with memory locations -- at this high
  3050. level we cannot twiddle with the reference offset, since
  3051. that may not mean anything (e.g., it refers to fixed-sized
  3052. stack slots on Java) }
  3053. a_load_loc_reg(list,src_size,src_size,l,hregister2);
  3054. a_load_reg_reg(list,src_size,dst_size,hregister2,hregister);
  3055. end
  3056. else
  3057. a_load_loc_reg(list,src_size,dst_size,l,hregister);
  3058. end;
  3059. end;
  3060. if (l.loc <> LOC_CREGISTER) or
  3061. not maybeconst then
  3062. location_reset(l,LOC_REGISTER,def_cgsize(dst_size))
  3063. else
  3064. location_reset(l,LOC_CREGISTER,def_cgsize(dst_size));
  3065. l.register:=hregister;
  3066. { Release temp if it was a reference }
  3067. if oldloc.loc=LOC_REFERENCE then
  3068. location_freetemp(list,oldloc);
  3069. end;
  3070. procedure thlcgobj.location_force_fpureg(list: TAsmList; var l: tlocation; size: tdef; maybeconst: boolean);
  3071. var
  3072. reg : tregister;
  3073. begin
  3074. if (l.loc<>LOC_FPUREGISTER) and
  3075. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  3076. begin
  3077. { if it's in an mm register, store to memory first }
  3078. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  3079. internalerror(2011012903);
  3080. reg:=getfpuregister(list,size);
  3081. a_loadfpu_loc_reg(list,size,size,l,reg);
  3082. location_freetemp(list,l);
  3083. location_reset(l,LOC_FPUREGISTER,l.size);
  3084. l.register:=reg;
  3085. end;
  3086. end;
  3087. procedure thlcgobj.location_force_mem(list: TAsmList; var l: tlocation; size: tdef);
  3088. var
  3089. r : treference;
  3090. forcesize: aint;
  3091. begin
  3092. case l.loc of
  3093. LOC_FPUREGISTER,
  3094. LOC_CFPUREGISTER :
  3095. begin
  3096. tg.gethltemp(list,size,size.size,tt_normal,r);
  3097. a_loadfpu_reg_ref(list,size,size,l.register,r);
  3098. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  3099. l.reference:=r;
  3100. end;
  3101. (*
  3102. LOC_MMREGISTER,
  3103. LOC_CMMREGISTER:
  3104. begin
  3105. tg.gethltemp(list,size,size.size,tt_normal,r);
  3106. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  3107. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  3108. l.reference:=r;
  3109. end;
  3110. *)
  3111. LOC_CONSTANT,
  3112. LOC_REGISTER,
  3113. LOC_CREGISTER,
  3114. LOC_SUBSETREG,
  3115. LOC_CSUBSETREG,
  3116. LOC_SUBSETREF,
  3117. LOC_CSUBSETREF:
  3118. begin
  3119. if not is_dynamic_array(size) and
  3120. not is_open_array(size) then
  3121. forcesize:=size.size
  3122. else
  3123. forcesize:=voidpointertype.size;
  3124. tg.gethltemp(list,size,forcesize,tt_normal,r);
  3125. a_load_loc_ref(list,size,size,l,r);
  3126. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  3127. l.reference:=r;
  3128. end;
  3129. LOC_CREFERENCE,
  3130. LOC_REFERENCE : ;
  3131. else
  3132. internalerror(2011010304);
  3133. end;
  3134. end;
  3135. procedure thlcgobj.location_get_data_ref(list: TAsmList; def: tdef; const l: tlocation; var ref: treference; loadref: boolean; alignment: longint);
  3136. begin
  3137. case l.loc of
  3138. LOC_REGISTER,
  3139. LOC_CREGISTER :
  3140. begin
  3141. if not loadref then
  3142. internalerror(200410231);
  3143. reference_reset_base(ref,l.register,0,alignment);
  3144. end;
  3145. LOC_REFERENCE,
  3146. LOC_CREFERENCE :
  3147. begin
  3148. if loadref then
  3149. begin
  3150. reference_reset_base(ref,getaddressregister(list,voidpointertype),0,alignment);
  3151. { it's a pointer to def }
  3152. a_load_ref_reg(list,voidpointertype,voidpointertype,l.reference,ref.base);
  3153. end
  3154. else
  3155. ref:=l.reference;
  3156. end;
  3157. else
  3158. internalerror(200309181);
  3159. end;
  3160. end;
  3161. procedure thlcgobj.maketojumpbool(list: TAsmList; p: tnode);
  3162. {
  3163. produces jumps to true respectively false labels using boolean expressions
  3164. depending on whether the loading of regvars is currently being
  3165. synchronized manually (such as in an if-node) or automatically (most of
  3166. the other cases where this procedure is called), loadregvars can be
  3167. "lr_load_regvars" or "lr_dont_load_regvars"
  3168. }
  3169. var
  3170. storepos : tfileposinfo;
  3171. begin
  3172. if nf_error in p.flags then
  3173. exit;
  3174. storepos:=current_filepos;
  3175. current_filepos:=p.fileinfo;
  3176. if is_boolean(p.resultdef) then
  3177. begin
  3178. if is_constboolnode(p) then
  3179. begin
  3180. if Tordconstnode(p).value.uvalue<>0 then
  3181. a_jmp_always(list,current_procinfo.CurrTrueLabel)
  3182. else
  3183. a_jmp_always(list,current_procinfo.CurrFalseLabel)
  3184. end
  3185. else
  3186. begin
  3187. case p.location.loc of
  3188. LOC_SUBSETREG,LOC_CSUBSETREG,
  3189. LOC_SUBSETREF,LOC_CSUBSETREF,
  3190. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  3191. begin
  3192. a_cmp_const_loc_label(list,p.resultdef,OC_NE,0,p.location,current_procinfo.CurrTrueLabel);
  3193. a_jmp_always(list,current_procinfo.CurrFalseLabel);
  3194. end;
  3195. LOC_JUMP:
  3196. ;
  3197. {$ifdef cpuflags}
  3198. LOC_FLAGS :
  3199. begin
  3200. a_jmp_flags(list,p.location.resflags,current_procinfo.CurrTrueLabel);
  3201. a_jmp_always(list,current_procinfo.CurrFalseLabel);
  3202. end;
  3203. {$endif cpuflags}
  3204. else
  3205. begin
  3206. printnode(output,p);
  3207. internalerror(2011010418);
  3208. end;
  3209. end;
  3210. end;
  3211. end
  3212. else
  3213. internalerror(2011010419);
  3214. current_filepos:=storepos;
  3215. end;
  3216. function use_ent : boolean;
  3217. begin
  3218. use_ent := (target_info.system in [system_mipsel_linux,system_mipseb_linux])
  3219. or (target_info.cpu=cpu_alpha);
  3220. end;
  3221. procedure thlcgobj.gen_proc_symbol(list: TAsmList);
  3222. var
  3223. item,
  3224. previtem : TCmdStrListItem;
  3225. begin
  3226. previtem:=nil;
  3227. item := TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
  3228. while assigned(item) do
  3229. begin
  3230. {$ifdef arm}
  3231. if current_settings.cputype in cpu_thumb2 then
  3232. list.concat(tai_thumb_func.create);
  3233. {$endif arm}
  3234. { "double link" all procedure entry symbols via .reference }
  3235. { directives on darwin, because otherwise the linker }
  3236. { sometimes strips the procedure if only on of the symbols }
  3237. { is referenced }
  3238. if assigned(previtem) and
  3239. (target_info.system in systems_darwin) then
  3240. list.concat(tai_directive.create(asd_reference,item.str));
  3241. if (cs_profile in current_settings.moduleswitches) or
  3242. (po_global in current_procinfo.procdef.procoptions) then
  3243. list.concat(Tai_symbol.createname_global(item.str,AT_FUNCTION,0))
  3244. else
  3245. list.concat(Tai_symbol.createname(item.str,AT_FUNCTION,0));
  3246. if assigned(previtem) and
  3247. (target_info.system in systems_darwin) then
  3248. list.concat(tai_directive.create(asd_reference,previtem.str));
  3249. if not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  3250. list.concat(Tai_function_name.create(item.str));
  3251. previtem:=item;
  3252. item := TCmdStrListItem(item.next);
  3253. end;
  3254. if (use_ent) then
  3255. list.concat(Tai_ent.create(current_procinfo.procdef.mangledname));
  3256. current_procinfo.procdef.procstarttai:=tai(list.last);
  3257. end;
  3258. procedure thlcgobj.gen_proc_symbol_end(list: TAsmList);
  3259. begin
  3260. if (use_ent) then
  3261. list.concat(Tai_ent_end.create(current_procinfo.procdef.mangledname));
  3262. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  3263. current_procinfo.procdef.procendtai:=tai(list.last);
  3264. if (current_module.islibrary) then
  3265. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  3266. { setinitname may generate a new section -> don't add to the
  3267. current list, because we assume this remains a text section }
  3268. exportlib.setinitname(current_asmdata.AsmLists[al_exports],current_procinfo.procdef.mangledname);
  3269. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  3270. begin
  3271. if (target_info.system in (systems_darwin+[system_powerpc_macos]+systems_aix)) and
  3272. not(current_module.islibrary) then
  3273. begin
  3274. new_section(list,sec_code,'',4);
  3275. list.concat(tai_symbol.createname_global(
  3276. target_info.cprefix+mainaliasname,AT_FUNCTION,0));
  3277. { keep argc, argv and envp properly on the stack }
  3278. if not(target_info.system in systems_aix) then
  3279. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN')
  3280. else
  3281. cg.a_call_name(list,target_info.cprefix+'FPC_SYSTEMMAIN',false)
  3282. end;
  3283. end;
  3284. end;
  3285. procedure thlcgobj.gen_initialize_code(list: TAsmList);
  3286. begin
  3287. { initialize local data like ansistrings }
  3288. case current_procinfo.procdef.proctypeoption of
  3289. potype_unitinit:
  3290. begin
  3291. { this is also used for initialization of variables in a
  3292. program which does not have a globalsymtable }
  3293. if assigned(current_module.globalsymtable) then
  3294. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@initialize_data,list);
  3295. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_data,list);
  3296. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  3297. end;
  3298. { units have seperate code for initilization and finalization }
  3299. potype_unitfinalize: ;
  3300. { program init/final is generated in separate procedure }
  3301. potype_proginit:
  3302. begin
  3303. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  3304. end;
  3305. else
  3306. current_procinfo.procdef.localst.SymList.ForEachCall(@initialize_data,list);
  3307. end;
  3308. { initialises temp. ansi/wide string data }
  3309. if (current_procinfo.procdef.proctypeoption<>potype_exceptfilter) then
  3310. inittempvariables(list);
  3311. {$ifdef OLDREGVARS}
  3312. load_regvars(list,nil);
  3313. {$endif OLDREGVARS}
  3314. end;
  3315. procedure thlcgobj.gen_finalize_code(list: TAsmList);
  3316. var
  3317. old_current_procinfo: tprocinfo;
  3318. begin
  3319. old_current_procinfo:=current_procinfo;
  3320. if (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  3321. begin
  3322. if (current_procinfo.parent.finalize_procinfo<>current_procinfo) then
  3323. exit;
  3324. current_procinfo:=current_procinfo.parent;
  3325. end;
  3326. {$ifdef OLDREGVARS}
  3327. cleanup_regvars(list);
  3328. {$endif OLDREGVARS}
  3329. { finalize temporary data }
  3330. finalizetempvariables(list);
  3331. { finalize local data like ansistrings}
  3332. case current_procinfo.procdef.proctypeoption of
  3333. potype_unitfinalize:
  3334. begin
  3335. { this is also used for initialization of variables in a
  3336. program which does not have a globalsymtable }
  3337. if assigned(current_module.globalsymtable) then
  3338. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@finalize_static_data,list);
  3339. TSymtable(current_module.localsymtable).SymList.ForEachCall(@finalize_static_data,list);
  3340. end;
  3341. { units/progs have separate code for initialization and finalization }
  3342. potype_unitinit: ;
  3343. { program init/final is generated in separate procedure }
  3344. potype_proginit: ;
  3345. else
  3346. current_procinfo.procdef.localst.SymList.ForEachCall(@finalize_local_vars,list);
  3347. end;
  3348. { finalize paras data }
  3349. if assigned(current_procinfo.procdef.parast) and
  3350. not(po_assembler in current_procinfo.procdef.procoptions) then
  3351. current_procinfo.procdef.parast.SymList.ForEachCall(@final_paras,list);
  3352. current_procinfo:=old_current_procinfo;
  3353. end;
  3354. procedure thlcgobj.gen_entry_code(list: TAsmList);
  3355. begin
  3356. { the actual profile code can clobber some registers,
  3357. therefore if the context must be saved, do it before
  3358. the actual call to the profile code
  3359. }
  3360. if (cs_profile in current_settings.moduleswitches) and
  3361. not(po_assembler in current_procinfo.procdef.procoptions) then
  3362. begin
  3363. { non-win32 can call mcout even in main }
  3364. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  3365. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  3366. begin
  3367. g_profilecode(list);
  3368. end;
  3369. end;
  3370. { TODO: create high level version (create compilerprocs in system unit,
  3371. look up procdef, use hlcgobj.a_call_name()) }
  3372. { call startup helpers from main program }
  3373. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  3374. begin
  3375. { initialize units }
  3376. cg.allocallcpuregisters(list);
  3377. if not(current_module.islibrary) then
  3378. cg.a_call_name(list,'FPC_INITIALIZEUNITS',false)
  3379. else
  3380. cg.a_call_name(list,'FPC_LIBINITIALIZEUNITS',false);
  3381. cg.deallocallcpuregisters(list);
  3382. end;
  3383. list.concat(Tai_force_line.Create);
  3384. {$ifdef OLDREGVARS}
  3385. load_regvars(list,nil);
  3386. {$endif OLDREGVARS}
  3387. end;
  3388. procedure thlcgobj.gen_exit_code(list: TAsmList);
  3389. begin
  3390. { TODO: create high level version (create compilerproc in system unit,
  3391. look up procdef, use hlcgobj.a_call_name()) }
  3392. { call __EXIT for main program }
  3393. if (not DLLsource) and
  3394. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  3395. cg.a_call_name(list,'FPC_DO_EXIT',false);
  3396. end;
  3397. procedure thlcgobj.inittempvariables(list: TAsmList);
  3398. var
  3399. hp : ptemprecord;
  3400. href : treference;
  3401. begin
  3402. hp:=tg.templist;
  3403. while assigned(hp) do
  3404. begin
  3405. if assigned(hp^.def) and
  3406. is_managed_type(hp^.def) then
  3407. begin
  3408. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  3409. g_initialize(list,hp^.def,href);
  3410. end;
  3411. hp:=hp^.next;
  3412. end;
  3413. end;
  3414. procedure thlcgobj.initialize_data(p: TObject; arg: pointer);
  3415. var
  3416. OldAsmList : TAsmList;
  3417. hp : tnode;
  3418. begin
  3419. if (tsym(p).typ = localvarsym) and
  3420. { local (procedure or unit) variables only need initialization if
  3421. they are used }
  3422. ((tabstractvarsym(p).refs>0) or
  3423. { managed return symbols must be inited }
  3424. ((tsym(p).typ=localvarsym) and (vo_is_funcret in tlocalvarsym(p).varoptions))
  3425. ) and
  3426. not(vo_is_typed_const in tabstractvarsym(p).varoptions) and
  3427. not(vo_is_external in tabstractvarsym(p).varoptions) and
  3428. not(vo_is_default_var in tabstractvarsym(p).varoptions) and
  3429. (is_managed_type(tabstractvarsym(p).vardef) or
  3430. ((m_iso in current_settings.modeswitches) and (tabstractvarsym(p).vardef.typ=filedef))
  3431. ) then
  3432. begin
  3433. OldAsmList:=current_asmdata.CurrAsmList;
  3434. current_asmdata.CurrAsmList:=TAsmList(arg);
  3435. hp:=cnodeutils.initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner),false);
  3436. firstpass(hp);
  3437. secondpass(hp);
  3438. hp.free;
  3439. current_asmdata.CurrAsmList:=OldAsmList;
  3440. end;
  3441. end;
  3442. procedure thlcgobj.finalizetempvariables(list: TAsmList);
  3443. var
  3444. hp : ptemprecord;
  3445. href : treference;
  3446. begin
  3447. hp:=tg.templist;
  3448. while assigned(hp) do
  3449. begin
  3450. if assigned(hp^.def) and
  3451. is_managed_type(hp^.def) then
  3452. begin
  3453. include(current_procinfo.flags,pi_needs_implicit_finally);
  3454. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  3455. g_finalize(list,hp^.def,href);
  3456. end;
  3457. hp:=hp^.next;
  3458. end;
  3459. end;
  3460. procedure thlcgobj.initialize_regvars(p: TObject; arg: pointer);
  3461. var
  3462. href : treference;
  3463. begin
  3464. if (tsym(p).typ=staticvarsym) then
  3465. begin
  3466. { Static variables can have the initialloc only set to LOC_CxREGISTER
  3467. or LOC_INVALID, for explaination see gen_alloc_symtable (PFV) }
  3468. case tstaticvarsym(p).initialloc.loc of
  3469. LOC_CREGISTER :
  3470. begin
  3471. {$ifndef cpu64bitalu}
  3472. if (tstaticvarsym(p).initialloc.size in [OS_64,OS_S64]) then
  3473. cg64.a_load64_const_reg(TAsmList(arg),0,tstaticvarsym(p).initialloc.register64)
  3474. else
  3475. {$endif not cpu64bitalu}
  3476. a_load_const_reg(TAsmList(arg),tstaticvarsym(p).vardef,0,
  3477. tstaticvarsym(p).initialloc.register);
  3478. end;
  3479. (*
  3480. LOC_CMMREGISTER :
  3481. { clear the whole register }
  3482. cg.a_opmm_reg_reg(TAsmList(arg),OP_XOR,reg_cgsize(tstaticvarsym(p).initialloc.register),
  3483. tstaticvarsym(p).initialloc.register,
  3484. tstaticvarsym(p).initialloc.register,
  3485. nil);
  3486. *)
  3487. LOC_CFPUREGISTER :
  3488. begin
  3489. { initialize fpu regvar by loading from memory }
  3490. reference_reset_symbol(href,
  3491. current_asmdata.RefAsmSymbol(tstaticvarsym(p).mangledname), 0,
  3492. var_align(tstaticvarsym(p).vardef.alignment));
  3493. a_loadfpu_ref_reg(TAsmList(arg), tstaticvarsym(p).vardef,
  3494. tstaticvarsym(p).vardef, href, tstaticvarsym(p).initialloc.register);
  3495. end;
  3496. LOC_INVALID :
  3497. ;
  3498. else
  3499. internalerror(200410124);
  3500. end;
  3501. end;
  3502. end;
  3503. procedure thlcgobj.finalize_sym(asmlist: TAsmList; sym: tsym);
  3504. var
  3505. hp : tnode;
  3506. OldAsmList : TAsmList;
  3507. begin
  3508. include(current_procinfo.flags,pi_needs_implicit_finally);
  3509. OldAsmList:=current_asmdata.CurrAsmList;
  3510. current_asmdata.CurrAsmList:=asmlist;
  3511. hp:=cloadnode.create(sym,sym.owner);
  3512. if (sym.typ=staticvarsym) and (vo_force_finalize in tstaticvarsym(sym).varoptions) then
  3513. include(tloadnode(hp).loadnodeflags,loadnf_isinternal_ignoreconst);
  3514. hp:=cnodeutils.finalize_data_node(hp);
  3515. firstpass(hp);
  3516. secondpass(hp);
  3517. hp.free;
  3518. current_asmdata.CurrAsmList:=OldAsmList;
  3519. end;
  3520. procedure thlcgobj.finalize_local_vars(p: TObject; arg: pointer);
  3521. begin
  3522. if (tsym(p).typ=localvarsym) and
  3523. (tlocalvarsym(p).refs>0) and
  3524. not(vo_is_external in tlocalvarsym(p).varoptions) and
  3525. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  3526. not(vo_is_default_var in tabstractvarsym(p).varoptions) and
  3527. is_managed_type(tlocalvarsym(p).vardef) then
  3528. finalize_sym(TAsmList(arg),tsym(p));
  3529. end;
  3530. procedure thlcgobj.finalize_static_data(p: TObject; arg: pointer);
  3531. var
  3532. i : longint;
  3533. pd : tprocdef;
  3534. begin
  3535. case tsym(p).typ of
  3536. staticvarsym :
  3537. begin
  3538. { local (procedure or unit) variables only need finalization
  3539. if they are used
  3540. }
  3541. if ((tstaticvarsym(p).refs>0) or
  3542. { global (unit) variables always need finalization, since
  3543. they may also be used in another unit
  3544. }
  3545. (tstaticvarsym(p).owner.symtabletype=globalsymtable)) and
  3546. (
  3547. (tstaticvarsym(p).varspez<>vs_const) or
  3548. (vo_force_finalize in tstaticvarsym(p).varoptions)
  3549. ) and
  3550. not(vo_is_funcret in tstaticvarsym(p).varoptions) and
  3551. not(vo_is_external in tstaticvarsym(p).varoptions) and
  3552. is_managed_type(tstaticvarsym(p).vardef) then
  3553. finalize_sym(TAsmList(arg),tsym(p));
  3554. end;
  3555. procsym :
  3556. begin
  3557. for i:=0 to tprocsym(p).ProcdefList.Count-1 do
  3558. begin
  3559. pd:=tprocdef(tprocsym(p).ProcdefList[i]);
  3560. if assigned(pd.localst) and
  3561. (pd.procsym=tprocsym(p)) and
  3562. (pd.localst.symtabletype<>staticsymtable) then
  3563. pd.localst.SymList.ForEachCall(@finalize_static_data,arg);
  3564. end;
  3565. end;
  3566. end;
  3567. end;
  3568. procedure thlcgobj.final_paras(p: TObject; arg: pointer);
  3569. var
  3570. list : TAsmList;
  3571. href : treference;
  3572. hsym : tparavarsym;
  3573. eldef : tdef;
  3574. highloc : tlocation;
  3575. begin
  3576. if not(tsym(p).typ=paravarsym) then
  3577. exit;
  3578. list:=TAsmList(arg);
  3579. if is_managed_type(tparavarsym(p).vardef) then
  3580. begin
  3581. if (tparavarsym(p).varspez=vs_value) then
  3582. begin
  3583. include(current_procinfo.flags,pi_needs_implicit_finally);
  3584. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  3585. if is_open_array(tparavarsym(p).vardef) then
  3586. begin
  3587. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  3588. begin
  3589. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  3590. if not assigned(hsym) then
  3591. internalerror(201003032);
  3592. highloc:=hsym.initialloc
  3593. end
  3594. else
  3595. highloc.loc:=LOC_INVALID;
  3596. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  3597. g_array_rtti_helper(list,eldef,href,highloc,'fpc_finalize_array');
  3598. end
  3599. else
  3600. g_finalize(list,tparavarsym(p).vardef,href);
  3601. end;
  3602. end;
  3603. { open arrays can contain elements requiring init/final code, so the else has been removed here }
  3604. if (tparavarsym(p).varspez=vs_value) and
  3605. (is_open_array(tparavarsym(p).vardef) or
  3606. is_array_of_const(tparavarsym(p).vardef)) then
  3607. begin
  3608. { cdecl functions don't have a high pointer so it is not possible to generate
  3609. a local copy }
  3610. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  3611. g_releasevaluepara_openarray(list,tarraydef(tparavarsym(p).vardef),tparavarsym(p).localloc);
  3612. end;
  3613. end;
  3614. { generates the code for incrementing the reference count of parameters and
  3615. initialize out parameters }
  3616. { generates the code for incrementing the reference count of parameters and
  3617. initialize out parameters }
  3618. procedure thlcgobj.init_paras(p:TObject;arg:pointer);
  3619. var
  3620. href : treference;
  3621. hsym : tparavarsym;
  3622. eldef : tdef;
  3623. list : TAsmList;
  3624. highloc : tlocation;
  3625. needs_inittable : boolean;
  3626. begin
  3627. list:=TAsmList(arg);
  3628. if (tsym(p).typ=paravarsym) then
  3629. begin
  3630. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  3631. case tparavarsym(p).varspez of
  3632. vs_value :
  3633. if needs_inittable then
  3634. begin
  3635. { variants are already handled by the call to fpc_variant_copy_overwrite if
  3636. they are passed by reference }
  3637. if not((tparavarsym(p).vardef.typ=variantdef) and
  3638. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  3639. begin
  3640. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  3641. if is_open_array(tparavarsym(p).vardef) then
  3642. begin
  3643. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  3644. begin
  3645. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  3646. if not assigned(hsym) then
  3647. internalerror(201003032);
  3648. highloc:=hsym.initialloc
  3649. end
  3650. else
  3651. highloc.loc:=LOC_INVALID;
  3652. { open arrays do not contain correct element count in their rtti,
  3653. the actual count must be passed separately. }
  3654. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  3655. g_array_rtti_helper(list,eldef,href,highloc,'fpc_addref_array');
  3656. end
  3657. else
  3658. g_incrrefcount(list,tparavarsym(p).vardef,href);
  3659. end;
  3660. end;
  3661. vs_out :
  3662. begin
  3663. if needs_inittable then
  3664. begin
  3665. { we have no idea about the alignment at the callee side,
  3666. and the user also cannot specify "unaligned" here, so
  3667. assume worst case }
  3668. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  3669. if needs_inittable then
  3670. begin
  3671. if is_open_array(tparavarsym(p).vardef) then
  3672. begin
  3673. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  3674. begin
  3675. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  3676. if not assigned(hsym) then
  3677. internalerror(201003032);
  3678. highloc:=hsym.initialloc
  3679. end
  3680. else
  3681. highloc.loc:=LOC_INVALID;
  3682. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  3683. g_array_rtti_helper(list,eldef,href,highloc,'fpc_initialize_array');
  3684. end
  3685. else
  3686. g_initialize(list,tparavarsym(p).vardef,href);
  3687. end;
  3688. end;
  3689. end;
  3690. end;
  3691. end;
  3692. end;
  3693. procedure thlcgobj.gen_load_para_value(list: TAsmList);
  3694. var
  3695. i: longint;
  3696. currpara: tparavarsym;
  3697. begin
  3698. if (po_assembler in current_procinfo.procdef.procoptions) or
  3699. { exceptfilters have a single hidden 'parentfp' parameter, which
  3700. is handled by tcg.g_proc_entry. }
  3701. (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  3702. exit;
  3703. { Copy parameters to local references/registers }
  3704. for i:=0 to current_procinfo.procdef.paras.count-1 do
  3705. begin
  3706. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  3707. gen_load_cgpara_loc(list,currpara.vardef,currpara.paraloc[calleeside],currpara.initialloc,paramanager.param_use_paraloc(currpara.paraloc[calleeside]));
  3708. end;
  3709. { generate copies of call by value parameters, must be done before
  3710. the initialization and body is parsed because the refcounts are
  3711. incremented using the local copies }
  3712. current_procinfo.procdef.parast.SymList.ForEachCall(@g_copyvalueparas,list);
  3713. if not(po_assembler in current_procinfo.procdef.procoptions) then
  3714. begin
  3715. { initialize refcounted paras, and trash others. Needed here
  3716. instead of in gen_initialize_code, because when a reference is
  3717. intialised or trashed while the pointer to that reference is kept
  3718. in a regvar, we add a register move and that one again has to
  3719. come after the parameter loading code as far as the register
  3720. allocator is concerned }
  3721. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  3722. end;
  3723. end;
  3724. procedure thlcgobj.g_copyvalueparas(p: TObject; arg: pointer);
  3725. var
  3726. href : treference;
  3727. hreg : tregister;
  3728. list : TAsmList;
  3729. hsym : tparavarsym;
  3730. l : longint;
  3731. highloc,
  3732. localcopyloc : tlocation;
  3733. begin
  3734. list:=TAsmList(arg);
  3735. if (tsym(p).typ=paravarsym) and
  3736. (tparavarsym(p).varspez=vs_value) and
  3737. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  3738. begin
  3739. { we have no idea about the alignment at the caller side }
  3740. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  3741. if is_open_array(tparavarsym(p).vardef) or
  3742. is_array_of_const(tparavarsym(p).vardef) then
  3743. begin
  3744. { cdecl functions don't have a high pointer so it is not possible to generate
  3745. a local copy }
  3746. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  3747. begin
  3748. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  3749. begin
  3750. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  3751. if not assigned(hsym) then
  3752. internalerror(2011020506);
  3753. highloc:=hsym.initialloc
  3754. end
  3755. else
  3756. highloc.loc:=LOC_INVALID;
  3757. hreg:=getaddressregister(list,voidpointertype);
  3758. if not is_packed_array(tparavarsym(p).vardef) then
  3759. g_copyvaluepara_openarray(list,href,highloc,tarraydef(tparavarsym(p).vardef),hreg)
  3760. else
  3761. internalerror(2011020507);
  3762. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  3763. a_load_reg_loc(list,tparavarsym(p).vardef,tparavarsym(p).vardef,hreg,tparavarsym(p).initialloc);
  3764. end;
  3765. end
  3766. else
  3767. begin
  3768. { Allocate space for the local copy }
  3769. l:=tparavarsym(p).getsize;
  3770. localcopyloc.loc:=LOC_REFERENCE;
  3771. localcopyloc.size:=int_cgsize(l);
  3772. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  3773. { Copy data }
  3774. if is_shortstring(tparavarsym(p).vardef) then
  3775. begin
  3776. { this code is only executed before the code for the body and the entry/exit code is generated
  3777. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  3778. }
  3779. include(current_procinfo.flags,pi_do_call);
  3780. g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef))
  3781. end
  3782. else if tparavarsym(p).vardef.typ=variantdef then
  3783. begin
  3784. { this code is only executed before the code for the body and the entry/exit code is generated
  3785. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  3786. }
  3787. include(current_procinfo.flags,pi_do_call);
  3788. g_copyvariant(list,href,localcopyloc.reference,tvariantdef(tparavarsym(p).vardef))
  3789. end
  3790. else
  3791. begin
  3792. { pass proper alignment info }
  3793. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  3794. g_concatcopy(list,tparavarsym(p).vardef,href,localcopyloc.reference);
  3795. end;
  3796. { update localloc of varsym }
  3797. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  3798. tparavarsym(p).localloc:=localcopyloc;
  3799. tparavarsym(p).initialloc:=localcopyloc;
  3800. end;
  3801. end;
  3802. end;
  3803. procedure thlcgobj.gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint);
  3804. begin
  3805. case l.loc of
  3806. (*
  3807. LOC_MMREGISTER,
  3808. LOC_CMMREGISTER:
  3809. case cgpara.location^.loc of
  3810. LOC_REFERENCE,
  3811. LOC_CREFERENCE,
  3812. LOC_MMREGISTER,
  3813. LOC_CMMREGISTER,
  3814. LOC_REGISTER,
  3815. LOC_CREGISTER :
  3816. cg.a_loadmm_reg_cgpara(list,locsize,l.register,cgpara,mms_movescalar);
  3817. LOC_FPUREGISTER,
  3818. LOC_CFPUREGISTER:
  3819. begin
  3820. tmploc:=l;
  3821. location_force_fpureg(list,tmploc,false);
  3822. cg.a_loadfpu_reg_cgpara(list,tmploc.size,tmploc.register,cgpara);
  3823. end;
  3824. else
  3825. internalerror(200204249);
  3826. end;
  3827. *)
  3828. LOC_FPUREGISTER,
  3829. LOC_CFPUREGISTER:
  3830. case cgpara.location^.loc of
  3831. (*
  3832. LOC_MMREGISTER,
  3833. LOC_CMMREGISTER:
  3834. begin
  3835. tmploc:=l;
  3836. location_force_mmregscalar(list,tmploc,false);
  3837. cg.a_loadmm_reg_cgpara(list,tmploc.size,tmploc.register,cgpara,mms_movescalar);
  3838. end;
  3839. *)
  3840. { Some targets pass floats in normal registers }
  3841. LOC_REGISTER,
  3842. LOC_CREGISTER,
  3843. LOC_REFERENCE,
  3844. LOC_CREFERENCE,
  3845. LOC_FPUREGISTER,
  3846. LOC_CFPUREGISTER:
  3847. a_loadfpu_reg_cgpara(list,size,l.register,cgpara);
  3848. else
  3849. internalerror(2011010210);
  3850. end;
  3851. LOC_REFERENCE,
  3852. LOC_CREFERENCE:
  3853. case cgpara.location^.loc of
  3854. (*
  3855. LOC_MMREGISTER,
  3856. LOC_CMMREGISTER:
  3857. cg.a_loadmm_ref_cgpara(list,locsize,l.reference,cgpara,mms_movescalar);
  3858. *)
  3859. { Some targets pass floats in normal registers }
  3860. LOC_REGISTER,
  3861. LOC_CREGISTER,
  3862. LOC_REFERENCE,
  3863. LOC_CREFERENCE,
  3864. LOC_FPUREGISTER,
  3865. LOC_CFPUREGISTER:
  3866. a_loadfpu_ref_cgpara(list,size,l.reference,cgpara);
  3867. else
  3868. internalerror(2011010211);
  3869. end;
  3870. LOC_REGISTER,
  3871. LOC_CREGISTER :
  3872. a_load_loc_cgpara(list,size,l,cgpara);
  3873. else
  3874. internalerror(2011010212);
  3875. end;
  3876. end;
  3877. procedure thlcgobj.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  3878. begin
  3879. { do nothing by default }
  3880. end;
  3881. procedure thlcgobj.gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);
  3882. begin
  3883. { Handle Floating point types differently
  3884. This doesn't depend on emulator settings, emulator settings should
  3885. be handled by cpupara }
  3886. if (vardef.typ=floatdef) or
  3887. { some ABIs return certain records in an fpu register }
  3888. (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) or
  3889. (assigned(cgpara.location) and
  3890. (cgpara.Location^.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) then
  3891. begin
  3892. gen_loadfpu_loc_cgpara(list,vardef,l,cgpara,vardef.size);
  3893. exit;
  3894. end;
  3895. case l.loc of
  3896. LOC_CONSTANT,
  3897. LOC_REGISTER,
  3898. LOC_CREGISTER,
  3899. LOC_REFERENCE,
  3900. LOC_CREFERENCE :
  3901. begin
  3902. a_load_loc_cgpara(list,vardef,l,cgpara);
  3903. end;
  3904. (*
  3905. LOC_MMREGISTER,
  3906. LOC_CMMREGISTER:
  3907. begin
  3908. case l.size of
  3909. OS_F32,
  3910. OS_F64:
  3911. cg.a_loadmm_loc_cgpara(list,l,cgpara,mms_movescalar);
  3912. else
  3913. cg.a_loadmm_loc_cgpara(list,l,cgpara,nil);
  3914. end;
  3915. end;
  3916. *)
  3917. else
  3918. internalerror(2011010213);
  3919. end;
  3920. end;
  3921. procedure thlcgobj.gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  3922. var
  3923. href : treference;
  3924. begin
  3925. para.check_simple_location;
  3926. { skip e.g. empty records }
  3927. if (para.location^.loc = LOC_VOID) then
  3928. exit;
  3929. case destloc.loc of
  3930. LOC_REFERENCE :
  3931. begin
  3932. { If the parameter location is reused we don't need to copy
  3933. anything }
  3934. if not reusepara then
  3935. begin
  3936. reference_reset_base(href,para.location^.reference.index,para.location^.reference.offset,para.alignment);
  3937. a_load_ref_ref(list,para.def,para.def,href,destloc.reference);
  3938. end;
  3939. end;
  3940. { TODO other possible locations }
  3941. else
  3942. internalerror(2011010308);
  3943. end;
  3944. end;
  3945. procedure thlcgobj.gen_load_return_value(list: TAsmList);
  3946. var
  3947. ressym : tabstractnormalvarsym;
  3948. funcretloc : TCGPara;
  3949. begin
  3950. { Is the loading needed? }
  3951. if is_void(current_procinfo.procdef.returndef) or
  3952. (
  3953. (po_assembler in current_procinfo.procdef.procoptions) and
  3954. (not(assigned(current_procinfo.procdef.funcretsym)) or
  3955. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  3956. ) then
  3957. exit;
  3958. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  3959. { constructors return self }
  3960. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  3961. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.Find('self'))
  3962. else
  3963. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  3964. if (ressym.refs>0) or
  3965. is_managed_type(ressym.vardef) then
  3966. begin
  3967. { was: don't do anything if funcretloc.loc in [LOC_INVALID,LOC_REFERENCE] }
  3968. if not paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  3969. gen_load_loc_cgpara(list,ressym.vardef,ressym.localloc,funcretloc);
  3970. end
  3971. else
  3972. gen_load_uninitialized_function_result(list,current_procinfo.procdef,ressym.vardef,funcretloc)
  3973. end;
  3974. procedure thlcgobj.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  3975. begin
  3976. { add the procedure to the al_procedures }
  3977. maybe_new_object_file(current_asmdata.asmlists[al_procedures]);
  3978. new_section(current_asmdata.asmlists[al_procedures],sec_code,lower(pd.mangledname),getprocalign);
  3979. current_asmdata.asmlists[al_procedures].concatlist(code);
  3980. { save local data (casetable) also in the same file }
  3981. if assigned(data) and
  3982. (not data.empty) then
  3983. current_asmdata.asmlists[al_procedures].concatlist(data);
  3984. end;
  3985. function thlcgobj.g_call_system_proc(list: TAsmList; const procname: string; forceresdef: tdef): tcgpara;
  3986. var
  3987. srsym: tsym;
  3988. pd: tprocdef;
  3989. begin
  3990. srsym:=tsym(systemunit.find(procname));
  3991. if not assigned(srsym) and
  3992. (cs_compilesystem in current_settings.moduleswitches) then
  3993. srsym:=tsym(systemunit.Find(upper(procname)));
  3994. if not assigned(srsym) or
  3995. (srsym.typ<>procsym) then
  3996. Message1(cg_f_unknown_compilerproc,procname);
  3997. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  3998. result:=g_call_system_proc_intern(list,pd,forceresdef);
  3999. end;
  4000. function thlcgobj.g_call_system_proc_intern(list: TAsmList; pd: tprocdef; forceresdef: tdef): tcgpara;
  4001. begin
  4002. allocallcpuregisters(list);
  4003. result:=a_call_name(list,pd,pd.mangledname,forceresdef,false);
  4004. deallocallcpuregisters(list);
  4005. end;
  4006. end.