hlcgobj.pas 235 KB

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