hlcgobj.pas 216 KB

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