hlcgobj.pas 229 KB

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