hlcgobj.pas 210 KB

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