2
0

hlcgobj.pas 239 KB

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