hlcgobj.pas 213 KB

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