hlcgobj.pas 229 KB

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