hlcgobj.pas 224 KB

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