hlcgobj.pas 231 KB

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