hlcgobj.pas 238 KB

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