hlcgobj.pas 236 KB

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