hlcgobj.pas 199 KB

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