hlcgobj.pas 226 KB

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