hlcgobj.pas 241 KB

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