hlcgobj.pas 235 KB

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