cgobj.pas 190 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452
  1. {
  2. Copyright (c) 1998-2005 by Florian Klaempfl
  3. Member of the Free Pascal development team
  4. This unit implements the basic 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. Abstreact code generator unit. This contains the base class
  20. to implement for all new supported processors.
  21. WARNING: None of the routines implemented in these modules,
  22. or their descendants, should use the temp. allocator, as
  23. these routines may be called inside genentrycode, and the
  24. stack frame is already setup!
  25. }
  26. unit cgobj;
  27. {$i fpcdefs.inc}
  28. interface
  29. uses
  30. cclasses,globtype,constexp,
  31. cpubase,cgbase,cgutils,parabase,
  32. aasmbase,aasmtai,aasmdata,aasmcpu,
  33. symconst,symtype,symdef,rgobj
  34. ;
  35. type
  36. talignment = (AM_NATURAL,AM_NONE,AM_2BYTE,AM_4BYTE,AM_8BYTE);
  37. tsubsetloadopt = (SL_REG,SL_REGNOSRCMASK,SL_SETZERO,SL_SETMAX);
  38. {# @abstract(Abstract code generator)
  39. This class implements an abstract instruction generator. Some of
  40. the methods of this class are generic, while others must
  41. be overridden for all new processors which will be supported
  42. by Free Pascal. For 32-bit processors, the base class
  43. should be @link(tcg64f32) and not @var(tcg).
  44. }
  45. tcg = class
  46. public
  47. { how many times is this current code executed }
  48. executionweight : longint;
  49. alignment : talignment;
  50. rg : array[tregistertype] of trgobj;
  51. {$ifdef flowgraph}
  52. aktflownode:word;
  53. {$endif}
  54. {************************************************}
  55. { basic routines }
  56. constructor create;
  57. {# Initialize the register allocators needed for the codegenerator.}
  58. procedure init_register_allocators;virtual;
  59. {# Clean up the register allocators needed for the codegenerator.}
  60. procedure done_register_allocators;virtual;
  61. {# Set whether live_start or live_end should be updated when allocating registers, needed when e.g. generating initcode after the rest of the code. }
  62. procedure set_regalloc_live_range_direction(dir: TRADirection);
  63. {$ifdef flowgraph}
  64. procedure init_flowgraph;
  65. procedure done_flowgraph;
  66. {$endif}
  67. {# Gets a register suitable to do integer operations on.}
  68. function getintregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  69. {# Gets a register suitable to do integer operations on.}
  70. function getaddressregister(list:TAsmList):Tregister;virtual;
  71. function getfpuregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  72. function getmmregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  73. function getflagregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  74. {Does the generic cg need SIMD registers, like getmmxregister? Or should
  75. the cpu specific child cg object have such a method?}
  76. procedure add_reg_instruction(instr:Tai;r:tregister);virtual;
  77. procedure add_move_instruction(instr:Taicpu);virtual;
  78. function uses_registers(rt:Tregistertype):boolean;virtual;
  79. {# Get a specific register.}
  80. procedure getcpuregister(list:TAsmList;r:Tregister);virtual;
  81. procedure ungetcpuregister(list:TAsmList;r:Tregister);virtual;
  82. {# Get multiple registers specified.}
  83. procedure alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  84. {# Free multiple registers specified.}
  85. procedure dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  86. procedure allocallcpuregisters(list:TAsmList);virtual;
  87. procedure deallocallcpuregisters(list:TAsmList);virtual;
  88. procedure do_register_allocation(list:TAsmList;headertai:tai);virtual;
  89. procedure translate_register(var reg : tregister);
  90. function makeregsize(list:TAsmList;reg:Tregister;size:Tcgsize):Tregister;
  91. {# Emit a label to the instruction stream. }
  92. procedure a_label(list : TAsmList;l : tasmlabel);virtual;
  93. {# Allocates register r by inserting a pai_realloc record }
  94. procedure a_reg_alloc(list : TAsmList;r : tregister);
  95. {# Deallocates register r by inserting a pa_regdealloc record}
  96. procedure a_reg_dealloc(list : TAsmList;r : tregister);
  97. { Synchronize register, make sure it is still valid }
  98. procedure a_reg_sync(list : TAsmList;r : tregister);
  99. {# Pass a parameter, which is located in a register, to a routine.
  100. This routine should push/send the parameter to the routine, as
  101. required by the specific processor ABI and routine modifiers.
  102. It must generate register allocation information for the cgpara in
  103. case it consists of cpuregisters.
  104. @param(size size of the operand in the register)
  105. @param(r register source of the operand)
  106. @param(cgpara where the parameter will be stored)
  107. }
  108. procedure a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : TCGPara);virtual;
  109. {# Pass a parameter, which is a constant, to a routine.
  110. A generic version is provided. This routine should
  111. be overridden for optimization purposes if the cpu
  112. permits directly sending this type of parameter.
  113. It must generate register allocation information for the cgpara in
  114. case it consists of cpuregisters.
  115. @param(size size of the operand in constant)
  116. @param(a value of constant to send)
  117. @param(cgpara where the parameter will be stored)
  118. }
  119. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : TCGPara);virtual;
  120. {# Pass the value of a parameter, which is located in memory, to a routine.
  121. A generic version is provided. This routine should
  122. be overridden for optimization purposes if the cpu
  123. permits directly sending this type of parameter.
  124. It must generate register allocation information for the cgpara in
  125. case it consists of cpuregisters.
  126. @param(size size of the operand in constant)
  127. @param(r Memory reference of value to send)
  128. @param(cgpara where the parameter will be stored)
  129. }
  130. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : TCGPara);virtual;
  131. {# Pass the value of a parameter, which can be located either in a register or memory location,
  132. to a routine.
  133. A generic version is provided.
  134. @param(l location of the operand to send)
  135. @param(nr parameter number (starting from one) of routine (from left to right))
  136. @param(cgpara where the parameter will be stored)
  137. }
  138. procedure a_load_loc_cgpara(list : TAsmList;const l : tlocation;const cgpara : TCGPara);
  139. {# Pass the address of a reference to a routine. This routine
  140. will calculate the address of the reference, and pass this
  141. calculated address as a parameter.
  142. It must generate register allocation information for the cgpara in
  143. case it consists of cpuregisters.
  144. A generic version is provided. This routine should
  145. be overridden for optimization purposes if the cpu
  146. permits directly sending this type of parameter.
  147. @param(r reference to get address from)
  148. @param(nr parameter number (starting from one) of routine (from left to right))
  149. }
  150. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : TCGPara);virtual;
  151. {# Load a cgparaloc into a memory reference.
  152. It must generate register allocation information for the cgpara in
  153. case it consists of cpuregisters.
  154. @param(paraloc the source parameter sublocation)
  155. @param(ref the destination reference)
  156. @param(sizeleft indicates the total number of bytes left in all of
  157. the remaining sublocations of this parameter (the current
  158. sublocation and all of the sublocations coming after it).
  159. In case this location is also a reference, it is assumed
  160. to be the final part sublocation of the parameter and that it
  161. contains all of the "sizeleft" bytes).)
  162. @param(align the alignment of the paraloc in case it's a reference)
  163. }
  164. procedure a_load_cgparaloc_ref(list : TAsmList;const paraloc : TCGParaLocation;const ref : treference;sizeleft : tcgint;align : longint);
  165. {# Load a cgparaloc into any kind of register (int, fp, mm).
  166. @param(regsize the size of the destination register)
  167. @param(paraloc the source parameter sublocation)
  168. @param(reg the destination register)
  169. @param(align the alignment of the paraloc in case it's a reference)
  170. }
  171. procedure a_load_cgparaloc_anyreg(list : TAsmList;regsize : tcgsize;const paraloc : TCGParaLocation;reg : tregister;align : longint);
  172. { Remarks:
  173. * If a method specifies a size you have only to take care
  174. of that number of bits, i.e. load_const_reg with OP_8 must
  175. only load the lower 8 bit of the specified register
  176. the rest of the register can be undefined
  177. if necessary the compiler will call a method
  178. to zero or sign extend the register
  179. * The a_load_XX_XX with OP_64 needn't to be
  180. implemented for 32 bit
  181. processors, the code generator takes care of that
  182. * the addr size is for work with the natural pointer
  183. size
  184. * the procedures without fpu/mm are only for integer usage
  185. * normally the first location is the source and the
  186. second the destination
  187. }
  188. {# Emits instruction to call the method specified by symbol name.
  189. This routine must be overridden for each new target cpu.
  190. }
  191. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);virtual; abstract;
  192. procedure a_call_reg(list : TAsmList;reg : tregister);virtual; abstract;
  193. procedure a_call_ref(list : TAsmList;ref : treference);virtual;
  194. { same as a_call_name, might be overridden on certain architectures to emit
  195. static calls without usage of a got trampoline }
  196. procedure a_call_name_static(list : TAsmList;const s : string);virtual;
  197. { move instructions }
  198. procedure a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);virtual; abstract;
  199. procedure a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : treference);virtual;
  200. procedure a_load_const_loc(list : TAsmList;a : tcgint;const loc : tlocation);
  201. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  202. procedure a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual;
  203. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  204. procedure a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  205. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  206. procedure a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual;
  207. procedure a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);virtual;
  208. procedure a_load_loc_reg(list : TAsmList;tosize: tcgsize; const loc: tlocation; reg : tregister);
  209. procedure a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  210. procedure a_load_loc_subsetreg(list : TAsmList;subsetsize: tcgsize; const loc: tlocation; const sreg : tsubsetregister);
  211. procedure a_load_loc_subsetref(list : TAsmList;subsetsize: tcgsize; const loc: tlocation; const sref : tsubsetreference);
  212. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);virtual; abstract;
  213. procedure a_load_subsetreg_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; destreg: tregister); virtual;
  214. procedure a_load_reg_subsetreg(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister); virtual;
  215. procedure a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsreg, tosreg: tsubsetregister); virtual;
  216. procedure a_load_subsetreg_ref(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; const destref: treference); virtual;
  217. procedure a_load_ref_subsetreg(list : TAsmList; fromsize, subsetsize: tcgsize; const fromref: treference; const sreg: tsubsetregister); virtual;
  218. procedure a_load_const_subsetreg(list: TAsmlist; subsetsize: tcgsize; a: tcgint; const sreg: tsubsetregister); virtual;
  219. procedure a_load_subsetreg_loc(list: TAsmlist; subsetsize: tcgsize; const sreg: tsubsetregister; const loc: tlocation); virtual;
  220. procedure a_load_subsetref_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sref: tsubsetreference; destreg: tregister); virtual;
  221. procedure a_load_reg_subsetref(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sref: tsubsetreference);
  222. procedure a_load_subsetref_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsref, tosref: tsubsetreference); virtual;
  223. procedure a_load_subsetref_ref(list : TAsmList; subsetsize, tosize: tcgsize; const sref: tsubsetreference; const destref: treference); virtual;
  224. procedure a_load_ref_subsetref(list : TAsmList; fromsize, subsetsize: tcgsize; const fromref: treference; const sref: tsubsetreference); virtual;
  225. procedure a_load_const_subsetref(list: TAsmlist; subsetsize: tcgsize; a: tcgint; const sref: tsubsetreference); virtual;
  226. procedure a_load_subsetref_loc(list: TAsmlist; subsetsize: tcgsize; const sref: tsubsetreference; const loc: tlocation); virtual;
  227. procedure a_load_subsetref_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsref: tsubsetreference; const tosreg: tsubsetregister); virtual;
  228. procedure a_load_subsetreg_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsreg: tsubsetregister; const tosref: tsubsetreference); virtual;
  229. { bit test instructions }
  230. procedure a_bit_test_reg_reg_reg(list : TAsmList; bitnumbersize,valuesize,destsize: tcgsize;bitnumber,value,destreg: tregister); virtual;
  231. procedure a_bit_test_const_ref_reg(list: TAsmList; destsize: tcgsize; bitnumber: tcgint; const ref: treference; destreg: tregister); virtual;
  232. procedure a_bit_test_const_reg_reg(list: TAsmList; setregsize, destsize: tcgsize; bitnumber: tcgint; setreg, destreg: tregister); virtual;
  233. procedure a_bit_test_const_subsetreg_reg(list: TAsmList; setregsize, destsize: tcgsize; bitnumber: tcgint; const setreg: tsubsetregister; destreg: tregister); virtual;
  234. procedure a_bit_test_reg_ref_reg(list: TAsmList; bitnumbersize, destsize: tcgsize; bitnumber: tregister; const ref: treference; destreg: tregister); virtual;
  235. procedure a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, destsize: tcgsize; bitnumber: tregister; const loc: tlocation; destreg: tregister);
  236. procedure a_bit_test_const_loc_reg(list: TAsmList; destsize: tcgsize; bitnumber: tcgint; const loc: tlocation; destreg: tregister);
  237. { bit set/clear instructions }
  238. procedure a_bit_set_reg_reg(list : TAsmList; doset: boolean; bitnumbersize, destsize: tcgsize; bitnumber,dest: tregister); virtual;
  239. procedure a_bit_set_const_ref(list: TAsmList; doset: boolean;destsize: tcgsize; bitnumber: tcgint; const ref: treference); virtual;
  240. procedure a_bit_set_const_reg(list: TAsmList; doset: boolean; destsize: tcgsize; bitnumber: tcgint; destreg: tregister); virtual;
  241. procedure a_bit_set_const_subsetreg(list: TAsmList; doset: boolean; destsize: tcgsize; bitnumber: tcgint; const destreg: tsubsetregister); virtual;
  242. procedure a_bit_set_reg_ref(list: TAsmList; doset: boolean; bitnumbersize: tcgsize; bitnumber: tregister; const ref: treference); virtual;
  243. procedure a_bit_set_reg_loc(list: TAsmList; doset: boolean; bitnumbersize: tcgsize; bitnumber: tregister; const loc: tlocation);
  244. procedure a_bit_set_const_loc(list: TAsmList; doset: boolean; bitnumber: tcgint; const loc: tlocation);
  245. { bit scan instructions }
  246. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: tcgsize; src, dst: TRegister); virtual; abstract;
  247. { fpu move instructions }
  248. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize:tcgsize; reg1, reg2: tregister); virtual; abstract;
  249. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  250. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  251. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tcgsize; const ref1,ref2: treference);
  252. procedure a_loadfpu_loc_reg(list: TAsmList; tosize: tcgsize; const loc: tlocation; const reg: tregister);
  253. procedure a_loadfpu_reg_loc(list: TAsmList; fromsize: tcgsize; const reg: tregister; const loc: tlocation);
  254. procedure a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);virtual;
  255. procedure a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);virtual;
  256. { vector register move instructions }
  257. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); virtual;
  258. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  259. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual;
  260. procedure a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  261. procedure a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  262. procedure a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  263. procedure a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  264. procedure a_loadmm_loc_cgpara(list: TAsmList; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  265. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle); virtual;
  266. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  267. procedure a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  268. procedure a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  269. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle); virtual;
  270. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize; mmreg, intreg: tregister; shuffle : pmmshuffle); virtual;
  271. { basic arithmetic operations }
  272. { note: for operators which require only one argument (not, neg), use }
  273. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  274. { that in this case the *second* operand is used as both source and }
  275. { destination (JM) }
  276. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); virtual; abstract;
  277. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); virtual;
  278. procedure a_op_const_subsetreg(list : TAsmList; Op : TOpCG; size, subsetsize : TCGSize; a : tcgint; const sreg: tsubsetregister); virtual;
  279. procedure a_op_const_subsetref(list : TAsmList; Op : TOpCG; size, subsetsize : TCGSize; a : tcgint; const sref: tsubsetreference); virtual;
  280. procedure a_op_const_loc(list : TAsmList; Op: TOpCG; a: tcgint; const loc: tlocation);
  281. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  282. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  283. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  284. procedure a_op_reg_subsetreg(list : TAsmList; Op : TOpCG; opsize, subsetsize : TCGSize; reg: TRegister; const sreg: tsubsetregister); virtual;
  285. procedure a_op_reg_subsetref(list : TAsmList; Op : TOpCG; opsize, subsetsize : TCGSize; reg: TRegister; const sref: tsubsetreference); virtual;
  286. procedure a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  287. procedure a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  288. { trinary operations for processors that support them, 'emulated' }
  289. { on others. None with "ref" arguments since I don't think there }
  290. { are any processors that support it (JM) }
  291. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister); virtual;
  292. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister); virtual;
  293. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  294. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  295. { comparison operations }
  296. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  297. l : tasmlabel); virtual;
  298. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  299. l : tasmlabel); virtual;
  300. procedure a_cmp_const_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; a: tcgint; const loc: tlocation;
  301. l : tasmlabel);
  302. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  303. procedure a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  304. procedure a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  305. procedure a_cmp_subsetreg_reg_label(list : TAsmList; subsetsize, cmpsize : tcgsize; cmp_op : topcmp; const sreg: tsubsetregister; reg : tregister; l : tasmlabel); virtual;
  306. procedure a_cmp_subsetref_reg_label(list : TAsmList; subsetsize, cmpsize : tcgsize; cmp_op : topcmp; const sref: tsubsetreference; reg : tregister; l : tasmlabel); virtual;
  307. procedure a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  308. procedure a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  309. procedure a_cmp_ref_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  310. l : tasmlabel);
  311. procedure a_jmp_name(list : TAsmList;const s : string); virtual; abstract;
  312. procedure a_jmp_always(list : TAsmList;l: tasmlabel); virtual; abstract;
  313. {$ifdef cpuflags}
  314. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); virtual; abstract;
  315. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  316. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  317. }
  318. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  319. procedure g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  320. {$endif cpuflags}
  321. {
  322. This routine tries to optimize the op_const_reg/ref opcode, and should be
  323. called at the start of a_op_const_reg/ref. It returns the actual opcode
  324. to emit, and the constant value to emit. This function can opcode OP_NONE to
  325. remove the opcode and OP_MOVE to replace it with a simple load
  326. @param(op The opcode to emit, returns the opcode which must be emitted)
  327. @param(a The constant which should be emitted, returns the constant which must
  328. be emitted)
  329. }
  330. procedure optimize_op_const(var op: topcg; var a : tcgint);virtual;
  331. {#
  332. This routine is used in exception management nodes. It should
  333. save the exception reason currently in the FUNCTION_RETURN_REG. The
  334. save should be done either to a temp (pointed to by href).
  335. or on the stack (pushing the value on the stack).
  336. The size of the value to save is OS_S32. The default version
  337. saves the exception reason to a temp. memory area.
  338. }
  339. procedure g_exception_reason_save(list : TAsmList; const href : treference);virtual;
  340. {#
  341. This routine is used in exception management nodes. It should
  342. save the exception reason constant. The
  343. save should be done either to a temp (pointed to by href).
  344. or on the stack (pushing the value on the stack).
  345. The size of the value to save is OS_S32. The default version
  346. saves the exception reason to a temp. memory area.
  347. }
  348. procedure g_exception_reason_save_const(list : TAsmList; const href : treference; a: tcgint);virtual;
  349. {#
  350. This routine is used in exception management nodes. It should
  351. load the exception reason to the FUNCTION_RETURN_REG. The saved value
  352. should either be in the temp. area (pointed to by href , href should
  353. *NOT* be freed) or on the stack (the value should be popped).
  354. The size of the value to save is OS_S32. The default version
  355. saves the exception reason to a temp. memory area.
  356. }
  357. procedure g_exception_reason_load(list : TAsmList; const href : treference);virtual;
  358. procedure g_maybe_testself(list : TAsmList;reg:tregister);
  359. procedure g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  360. {# This should emit the opcode to copy len bytes from the source
  361. to destination.
  362. It must be overridden for each new target processor.
  363. @param(source Source reference of copy)
  364. @param(dest Destination reference of copy)
  365. }
  366. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);virtual; abstract;
  367. {# This should emit the opcode to copy len bytes from the an unaligned source
  368. to destination.
  369. It must be overridden for each new target processor.
  370. @param(source Source reference of copy)
  371. @param(dest Destination reference of copy)
  372. }
  373. procedure g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);virtual;
  374. {# This should emit the opcode to a shortrstring from the source
  375. to destination.
  376. @param(source Source reference of copy)
  377. @param(dest Destination reference of copy)
  378. }
  379. procedure g_copyshortstring(list : TAsmList;const source,dest : treference;len:byte);
  380. procedure g_copyvariant(list : TAsmList;const source,dest : treference);
  381. procedure g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);
  382. procedure g_decrrefcount(list : TAsmList;t: tdef; const ref: treference);
  383. procedure g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation;
  384. const name: string);
  385. procedure g_initialize(list : TAsmList;t : tdef;const ref : treference);
  386. procedure g_finalize(list : TAsmList;t : tdef;const ref : treference);
  387. {# Generates range checking code. It is to note
  388. that this routine does not need to be overridden,
  389. as it takes care of everything.
  390. @param(p Node which contains the value to check)
  391. @param(todef Type definition of node to range check)
  392. }
  393. procedure g_rangecheck(list: TAsmList; const l:tlocation; fromdef,todef: tdef); virtual;
  394. {# Generates overflow checking code for a node }
  395. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef); virtual;abstract;
  396. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);virtual;
  397. procedure g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:tcgint;destreg:tregister);virtual;
  398. procedure g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);virtual;
  399. {# Emits instructions when compilation is done in profile
  400. mode (this is set as a command line option). The default
  401. behavior does nothing, should be overridden as required.
  402. }
  403. procedure g_profilecode(list : TAsmList);virtual;
  404. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  405. @param(size Number of bytes to allocate)
  406. }
  407. procedure g_stackpointer_alloc(list : TAsmList;size : longint);virtual; abstract;
  408. {# Emits instruction for allocating the locals in entry
  409. code of a routine. This is one of the first
  410. routine called in @var(genentrycode).
  411. @param(localsize Number of bytes to allocate as locals)
  412. }
  413. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);virtual; abstract;
  414. {# Emits instructions for returning from a subroutine.
  415. Should also restore the framepointer and stack.
  416. @param(parasize Number of bytes of parameters to deallocate from stack)
  417. }
  418. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);virtual;abstract;
  419. {# This routine is called when generating the code for the entry point
  420. of a routine. It should save all registers which are not used in this
  421. routine, and which should be declared as saved in the std_saved_registers
  422. set.
  423. This routine is mainly used when linking to code which is generated
  424. by ABI-compliant compilers (like GCC), to make sure that the reserved
  425. registers of that ABI are not clobbered.
  426. @param(usedinproc Registers which are used in the code of this routine)
  427. }
  428. procedure g_save_registers(list:TAsmList);virtual;
  429. {# This routine is called when generating the code for the exit point
  430. of a routine. It should restore all registers which were previously
  431. saved in @var(g_save_standard_registers).
  432. @param(usedinproc Registers which are used in the code of this routine)
  433. }
  434. procedure g_restore_registers(list:TAsmList);virtual;
  435. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);virtual;abstract;
  436. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);virtual;
  437. function g_indirect_sym_load(list:TAsmList;const symname: string; weak: boolean): tregister;virtual;
  438. { generate a stub which only purpose is to pass control the given external method,
  439. setting up any additional environment before doing so (if required).
  440. The default implementation issues a jump instruction to the external name. }
  441. procedure g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string); virtual;
  442. { initialize the pic/got register }
  443. procedure g_maybe_got_init(list: TAsmList); virtual;
  444. { allocallcpuregisters, a_call_name, deallocallcpuregisters sequence }
  445. procedure g_call(list: TAsmList; const s: string);
  446. protected
  447. procedure get_subsetref_load_info(const sref: tsubsetreference; out loadsize: tcgsize; out extra_load: boolean);
  448. procedure a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister); virtual;
  449. procedure a_load_subsetref_regs_index(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg: tregister); virtual;
  450. procedure a_load_regconst_subsetref_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sref: tsubsetreference; slopt: tsubsetloadopt); virtual;
  451. procedure a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt); virtual;
  452. function get_bit_const_ref_sref(bitnumber: tcgint; const ref: treference): tsubsetreference;
  453. function get_bit_const_reg_sreg(setregsize: tcgsize; bitnumber: tcgint; setreg: tregister): tsubsetregister;
  454. function get_bit_reg_ref_sref(list: TAsmList; bitnumbersize: tcgsize; bitnumber: tregister; const ref: treference): tsubsetreference;
  455. end;
  456. {$ifndef cpu64bitalu}
  457. {# @abstract(Abstract code generator for 64 Bit operations)
  458. This class implements an abstract code generator class
  459. for 64 Bit operations.
  460. }
  461. tcg64 = class
  462. procedure a_load64_const_ref(list : TAsmList;value : int64;const ref : treference);virtual;abstract;
  463. procedure a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference);virtual;abstract;
  464. procedure a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64);virtual;abstract;
  465. procedure a_load64_reg_reg(list : TAsmList;regsrc,regdst : tregister64);virtual;abstract;
  466. procedure a_load64_const_reg(list : TAsmList;value : int64;reg : tregister64);virtual;abstract;
  467. procedure a_load64_loc_reg(list : TAsmList;const l : tlocation;reg : tregister64);virtual;abstract;
  468. procedure a_load64_loc_ref(list : TAsmList;const l : tlocation;const ref : treference);virtual;abstract;
  469. procedure a_load64_const_loc(list : TAsmList;value : int64;const l : tlocation);virtual;abstract;
  470. procedure a_load64_reg_loc(list : TAsmList;reg : tregister64;const l : tlocation);virtual;abstract;
  471. procedure a_load64_subsetref_reg(list : TAsmList; const sref: tsubsetreference; destreg: tregister64);virtual;abstract;
  472. procedure a_load64_reg_subsetref(list : TAsmList; fromreg: tregister64; const sref: tsubsetreference);virtual;abstract;
  473. procedure a_load64_const_subsetref(list: TAsmlist; a: int64; const sref: tsubsetreference);virtual;abstract;
  474. procedure a_load64_ref_subsetref(list : TAsmList; const fromref: treference; const sref: tsubsetreference);virtual;abstract;
  475. procedure a_load64_subsetref_subsetref(list: TAsmlist; const fromsref, tosref: tsubsetreference); virtual;abstract;
  476. procedure a_load64_subsetref_ref(list : TAsmList; const sref: tsubsetreference; const destref: treference); virtual;abstract;
  477. procedure a_load64_loc_subsetref(list : TAsmList; const l: tlocation; const sref : tsubsetreference);
  478. procedure a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  479. procedure a_load64high_reg_ref(list : TAsmList;reg : tregister;const ref : treference);virtual;abstract;
  480. procedure a_load64low_reg_ref(list : TAsmList;reg : tregister;const ref : treference);virtual;abstract;
  481. procedure a_load64high_ref_reg(list : TAsmList;const ref : treference;reg : tregister);virtual;abstract;
  482. procedure a_load64low_ref_reg(list : TAsmList;const ref : treference;reg : tregister);virtual;abstract;
  483. procedure a_load64high_loc_reg(list : TAsmList;const l : tlocation;reg : tregister);virtual;abstract;
  484. procedure a_load64low_loc_reg(list : TAsmList;const l : tlocation;reg : tregister);virtual;abstract;
  485. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);virtual;abstract;
  486. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);virtual;abstract;
  487. procedure a_op64_reg_ref(list : TAsmList;op:TOpCG;size : tcgsize;regsrc : tregister64;const ref : treference);virtual;abstract;
  488. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;regdst : tregister64);virtual;abstract;
  489. procedure a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);virtual;abstract;
  490. procedure a_op64_const_loc(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const l: tlocation);virtual;abstract;
  491. procedure a_op64_reg_loc(list : TAsmList;op:TOpCG;size : tcgsize;reg : tregister64;const l : tlocation);virtual;abstract;
  492. procedure a_op64_loc_reg(list : TAsmList;op:TOpCG;size : tcgsize;const l : tlocation;reg64 : tregister64);virtual;abstract;
  493. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);virtual;
  494. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);virtual;
  495. procedure a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  496. procedure a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  497. procedure a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  498. procedure a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  499. procedure a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  500. procedure a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  501. procedure a_load64_reg_cgpara(list : TAsmList;reg64 : tregister64;const loc : TCGPara);virtual;abstract;
  502. procedure a_load64_const_cgpara(list : TAsmList;value : int64;const loc : TCGPara);virtual;abstract;
  503. procedure a_load64_ref_cgpara(list : TAsmList;const r : treference;const loc : TCGPara);virtual;abstract;
  504. procedure a_load64_loc_cgpara(list : TAsmList;const l : tlocation;const loc : TCGPara);virtual;abstract;
  505. procedure a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister); virtual;abstract;
  506. procedure a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64); virtual;abstract;
  507. {
  508. This routine tries to optimize the const_reg opcode, and should be
  509. called at the start of a_op64_const_reg. It returns the actual opcode
  510. to emit, and the constant value to emit. If this routine returns
  511. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  512. @param(op The opcode to emit, returns the opcode which must be emitted)
  513. @param(a The constant which should be emitted, returns the constant which must
  514. be emitted)
  515. @param(reg The register to emit the opcode with, returns the register with
  516. which the opcode will be emitted)
  517. }
  518. function optimize64_op_const_reg(list: TAsmList; var op: topcg; var a : int64; var reg: tregister64): boolean;virtual;abstract;
  519. { override to catch 64bit rangechecks }
  520. procedure g_rangecheck64(list: TAsmList; const l:tlocation; fromdef,todef: tdef);virtual;abstract;
  521. end;
  522. {$endif cpu64bitalu}
  523. var
  524. {# Main code generator class }
  525. cg : tcg;
  526. {$ifndef cpu64bitalu}
  527. {# Code generator class for all operations working with 64-Bit operands }
  528. cg64 : tcg64;
  529. {$endif cpu64bitalu}
  530. procedure destroy_codegen;
  531. implementation
  532. uses
  533. globals,options,systems,
  534. verbose,defutil,paramgr,symsym,
  535. tgobj,cutils,procinfo,
  536. ncgrtti;
  537. {*****************************************************************************
  538. basic functionallity
  539. ******************************************************************************}
  540. constructor tcg.create;
  541. begin
  542. end;
  543. {*****************************************************************************
  544. register allocation
  545. ******************************************************************************}
  546. procedure tcg.init_register_allocators;
  547. begin
  548. fillchar(rg,sizeof(rg),0);
  549. add_reg_instruction_hook:=@add_reg_instruction;
  550. executionweight:=1;
  551. end;
  552. procedure tcg.done_register_allocators;
  553. begin
  554. { Safety }
  555. fillchar(rg,sizeof(rg),0);
  556. add_reg_instruction_hook:=nil;
  557. end;
  558. {$ifdef flowgraph}
  559. procedure Tcg.init_flowgraph;
  560. begin
  561. aktflownode:=0;
  562. end;
  563. procedure Tcg.done_flowgraph;
  564. begin
  565. end;
  566. {$endif}
  567. function tcg.getintregister(list:TAsmList;size:Tcgsize):Tregister;
  568. begin
  569. if not assigned(rg[R_INTREGISTER]) then
  570. internalerror(200312122);
  571. result:=rg[R_INTREGISTER].getregister(list,cgsize2subreg(R_INTREGISTER,size));
  572. end;
  573. function tcg.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  574. begin
  575. if not assigned(rg[R_FPUREGISTER]) then
  576. internalerror(200312123);
  577. result:=rg[R_FPUREGISTER].getregister(list,cgsize2subreg(R_FPUREGISTER,size));
  578. end;
  579. function tcg.getmmregister(list:TAsmList;size:Tcgsize):Tregister;
  580. begin
  581. if not assigned(rg[R_MMREGISTER]) then
  582. internalerror(2003121214);
  583. result:=rg[R_MMREGISTER].getregister(list,cgsize2subreg(R_MMREGISTER,size));
  584. end;
  585. function tcg.getaddressregister(list:TAsmList):Tregister;
  586. begin
  587. if assigned(rg[R_ADDRESSREGISTER]) then
  588. result:=rg[R_ADDRESSREGISTER].getregister(list,R_SUBWHOLE)
  589. else
  590. begin
  591. if not assigned(rg[R_INTREGISTER]) then
  592. internalerror(200312121);
  593. result:=rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  594. end;
  595. end;
  596. function Tcg.makeregsize(list:TAsmList;reg:Tregister;size:Tcgsize):Tregister;
  597. var
  598. subreg:Tsubregister;
  599. begin
  600. subreg:=cgsize2subreg(getregtype(reg),size);
  601. result:=reg;
  602. setsubreg(result,subreg);
  603. { notify RA }
  604. if result<>reg then
  605. list.concat(tai_regalloc.resize(result));
  606. end;
  607. procedure tcg.getcpuregister(list:TAsmList;r:Tregister);
  608. begin
  609. if not assigned(rg[getregtype(r)]) then
  610. internalerror(200312125);
  611. rg[getregtype(r)].getcpuregister(list,r);
  612. end;
  613. procedure tcg.ungetcpuregister(list:TAsmList;r:Tregister);
  614. begin
  615. if not assigned(rg[getregtype(r)]) then
  616. internalerror(200312126);
  617. rg[getregtype(r)].ungetcpuregister(list,r);
  618. end;
  619. procedure tcg.alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  620. begin
  621. if assigned(rg[rt]) then
  622. rg[rt].alloccpuregisters(list,r)
  623. else
  624. internalerror(200310092);
  625. end;
  626. procedure tcg.allocallcpuregisters(list:TAsmList);
  627. begin
  628. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  629. {$if not(defined(i386)) and not(defined(avr))}
  630. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  631. {$ifdef cpumm}
  632. alloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  633. {$endif cpumm}
  634. {$endif not(defined(i386)) and not(defined(avr))}
  635. end;
  636. procedure tcg.dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  637. begin
  638. if assigned(rg[rt]) then
  639. rg[rt].dealloccpuregisters(list,r)
  640. else
  641. internalerror(200310093);
  642. end;
  643. procedure tcg.deallocallcpuregisters(list:TAsmList);
  644. begin
  645. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  646. {$if not(defined(i386)) and not(defined(avr))}
  647. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  648. {$ifdef cpumm}
  649. dealloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  650. {$endif cpumm}
  651. {$endif not(defined(i386)) and not(defined(avr))}
  652. end;
  653. function tcg.uses_registers(rt:Tregistertype):boolean;
  654. begin
  655. if assigned(rg[rt]) then
  656. result:=rg[rt].uses_registers
  657. else
  658. result:=false;
  659. end;
  660. procedure tcg.add_reg_instruction(instr:Tai;r:tregister);
  661. var
  662. rt : tregistertype;
  663. begin
  664. rt:=getregtype(r);
  665. { Only add it when a register allocator is configured.
  666. No IE can be generated, because the VMT is written
  667. without a valid rg[] }
  668. if assigned(rg[rt]) then
  669. rg[rt].add_reg_instruction(instr,r,cg.executionweight);
  670. end;
  671. procedure tcg.add_move_instruction(instr:Taicpu);
  672. var
  673. rt : tregistertype;
  674. begin
  675. rt:=getregtype(instr.oper[O_MOV_SOURCE]^.reg);
  676. if assigned(rg[rt]) then
  677. rg[rt].add_move_instruction(instr)
  678. else
  679. internalerror(200310095);
  680. end;
  681. procedure tcg.set_regalloc_live_range_direction(dir: TRADirection);
  682. var
  683. rt : tregistertype;
  684. begin
  685. for rt:=low(rg) to high(rg) do
  686. begin
  687. if assigned(rg[rt]) then
  688. rg[rt].live_range_direction:=dir;
  689. end;
  690. end;
  691. procedure tcg.do_register_allocation(list:TAsmList;headertai:tai);
  692. var
  693. rt : tregistertype;
  694. begin
  695. for rt:=R_FPUREGISTER to R_SPECIALREGISTER do
  696. begin
  697. if assigned(rg[rt]) then
  698. rg[rt].do_register_allocation(list,headertai);
  699. end;
  700. { running the other register allocator passes could require addition int/addr. registers
  701. when spilling so run int/addr register allocation at the end }
  702. if assigned(rg[R_INTREGISTER]) then
  703. rg[R_INTREGISTER].do_register_allocation(list,headertai);
  704. if assigned(rg[R_ADDRESSREGISTER]) then
  705. rg[R_ADDRESSREGISTER].do_register_allocation(list,headertai);
  706. end;
  707. procedure tcg.translate_register(var reg : tregister);
  708. begin
  709. rg[getregtype(reg)].translate_register(reg);
  710. end;
  711. procedure tcg.a_reg_alloc(list : TAsmList;r : tregister);
  712. begin
  713. list.concat(tai_regalloc.alloc(r,nil));
  714. end;
  715. procedure tcg.a_reg_dealloc(list : TAsmList;r : tregister);
  716. begin
  717. list.concat(tai_regalloc.dealloc(r,nil));
  718. end;
  719. procedure tcg.a_reg_sync(list : TAsmList;r : tregister);
  720. var
  721. instr : tai;
  722. begin
  723. instr:=tai_regalloc.sync(r);
  724. list.concat(instr);
  725. add_reg_instruction(instr,r);
  726. end;
  727. procedure tcg.a_label(list : TAsmList;l : tasmlabel);
  728. begin
  729. list.concat(tai_label.create(l));
  730. end;
  731. {*****************************************************************************
  732. for better code generation these methods should be overridden
  733. ******************************************************************************}
  734. procedure tcg.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : TCGPara);
  735. var
  736. ref : treference;
  737. begin
  738. cgpara.check_simple_location;
  739. paramanager.alloccgpara(list,cgpara);
  740. case cgpara.location^.loc of
  741. LOC_REGISTER,LOC_CREGISTER:
  742. a_load_reg_reg(list,size,cgpara.location^.size,r,cgpara.location^.register);
  743. LOC_REFERENCE,LOC_CREFERENCE:
  744. begin
  745. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  746. a_load_reg_ref(list,size,cgpara.location^.size,r,ref);
  747. end;
  748. LOC_MMREGISTER,LOC_CMMREGISTER:
  749. a_loadmm_intreg_reg(list,size,cgpara.location^.size,r,cgpara.location^.register,mms_movescalar);
  750. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  751. begin
  752. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,ref);
  753. a_load_reg_ref(list,size,size,r,ref);
  754. a_loadfpu_ref_cgpara(list,cgpara.location^.size,ref,cgpara);
  755. tg.Ungettemp(list,ref);
  756. end
  757. else
  758. internalerror(2002071004);
  759. end;
  760. end;
  761. procedure tcg.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : TCGPara);
  762. var
  763. ref : treference;
  764. begin
  765. cgpara.check_simple_location;
  766. paramanager.alloccgpara(list,cgpara);
  767. case cgpara.location^.loc of
  768. LOC_REGISTER,LOC_CREGISTER:
  769. a_load_const_reg(list,cgpara.location^.size,a,cgpara.location^.register);
  770. LOC_REFERENCE,LOC_CREFERENCE:
  771. begin
  772. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  773. a_load_const_ref(list,cgpara.location^.size,a,ref);
  774. end
  775. else
  776. internalerror(2010053109);
  777. end;
  778. end;
  779. procedure tcg.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : TCGPara);
  780. var
  781. tmpref, ref: treference;
  782. tmpreg: tregister;
  783. location: pcgparalocation;
  784. orgsizeleft,
  785. sizeleft: tcgint;
  786. reghasvalue: boolean;
  787. begin
  788. location:=cgpara.location;
  789. tmpref:=r;
  790. sizeleft:=cgpara.intsize;
  791. while assigned(location) do
  792. begin
  793. paramanager.allocparaloc(list,location);
  794. case location^.loc of
  795. LOC_REGISTER,LOC_CREGISTER:
  796. begin
  797. { Parameter locations are often allocated in multiples of
  798. entire registers. If a parameter only occupies a part of
  799. such a register (e.g. a 16 bit int on a 32 bit
  800. architecture), the size of this parameter can only be
  801. determined by looking at the "size" parameter of this
  802. method -> if the size parameter is <= sizeof(aint), then
  803. we check that there is only one parameter location and
  804. then use this "size" to load the value into the parameter
  805. location }
  806. if (size<>OS_NO) and
  807. (tcgsize2size[size]<=sizeof(aint)) then
  808. begin
  809. cgpara.check_simple_location;
  810. a_load_ref_reg(list,size,location^.size,tmpref,location^.register);
  811. end
  812. { there's a lot more data left, and the current paraloc's
  813. register is entirely filled with part of that data }
  814. else if (sizeleft>sizeof(aint)) then
  815. begin
  816. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  817. end
  818. { we're at the end of the data, and it can be loaded into
  819. the current location's register with a single regular
  820. load }
  821. else if (sizeleft in [1,2{$ifndef cpu16bitalu},4{$endif}{$ifdef cpu64bitalu},8{$endif}]) then
  822. begin
  823. a_load_ref_reg(list,int_cgsize(sizeleft),location^.size,tmpref,location^.register);
  824. end
  825. { we're at the end of the data, and we need multiple loads
  826. to get it in the register because it's an irregular size }
  827. else
  828. begin
  829. { should be the last part }
  830. if assigned(location^.next) then
  831. internalerror(2010052907);
  832. { load the value piecewise to get it into the register }
  833. orgsizeleft:=sizeleft;
  834. reghasvalue:=false;
  835. {$ifdef cpu64bitalu}
  836. if sizeleft>=4 then
  837. begin
  838. a_load_ref_reg(list,OS_32,location^.size,tmpref,location^.register);
  839. dec(sizeleft,4);
  840. if target_info.endian=endian_big then
  841. a_op_const_reg(list,OP_SHL,location^.size,sizeleft*8,location^.register);
  842. inc(tmpref.offset,4);
  843. reghasvalue:=true;
  844. end;
  845. {$endif cpu64bitalu}
  846. if sizeleft>=2 then
  847. begin
  848. tmpreg:=getintregister(list,location^.size);
  849. a_load_ref_reg(list,OS_16,location^.size,tmpref,tmpreg);
  850. dec(sizeleft,2);
  851. if reghasvalue then
  852. begin
  853. if target_info.endian=endian_big then
  854. a_op_const_reg(list,OP_SHL,location^.size,sizeleft*8,tmpreg)
  855. else
  856. a_op_const_reg(list,OP_SHL,location^.size,(orgsizeleft-(sizeleft+2))*8,tmpreg);
  857. a_op_reg_reg(list,OP_OR,location^.size,tmpreg,location^.register);
  858. end
  859. else
  860. begin
  861. if target_info.endian=endian_big then
  862. a_op_const_reg_reg(list,OP_SHL,location^.size,sizeleft*8,tmpreg,location^.register)
  863. else
  864. a_load_reg_reg(list,location^.size,location^.size,tmpreg,location^.register);
  865. end;
  866. inc(tmpref.offset,2);
  867. reghasvalue:=true;
  868. end;
  869. if sizeleft=1 then
  870. begin
  871. tmpreg:=getintregister(list,location^.size);
  872. a_load_ref_reg(list,OS_8,location^.size,tmpref,tmpreg);
  873. dec(sizeleft,1);
  874. if reghasvalue then
  875. begin
  876. if target_info.endian=endian_little then
  877. a_op_const_reg(list,OP_SHL,location^.size,(orgsizeleft-(sizeleft+1))*8,tmpreg);
  878. a_op_reg_reg(list,OP_OR,location^.size,tmpreg,location^.register)
  879. end
  880. else
  881. a_load_reg_reg(list,location^.size,location^.size,tmpreg,location^.register);
  882. inc(tmpref.offset);
  883. end;
  884. { the loop will already adjust the offset and sizeleft }
  885. dec(tmpref.offset,orgsizeleft);
  886. sizeleft:=orgsizeleft;
  887. end;
  888. end;
  889. LOC_REFERENCE,LOC_CREFERENCE:
  890. begin
  891. if assigned(location^.next) then
  892. internalerror(2010052906);
  893. reference_reset_base(ref,location^.reference.index,location^.reference.offset,newalignment(cgpara.alignment,cgpara.intsize-sizeleft));
  894. if (size <> OS_NO) and
  895. (tcgsize2size[size] <= sizeof(aint)) then
  896. a_load_ref_ref(list,size,location^.size,tmpref,ref)
  897. else
  898. { use concatcopy, because the parameter can be larger than }
  899. { what the OS_* constants can handle }
  900. g_concatcopy(list,tmpref,ref,sizeleft);
  901. end;
  902. LOC_MMREGISTER,LOC_CMMREGISTER:
  903. begin
  904. case location^.size of
  905. OS_F32,
  906. OS_F64,
  907. OS_F128:
  908. a_loadmm_ref_reg(list,location^.size,location^.size,tmpref,location^.register,mms_movescalar);
  909. OS_M8..OS_M128,
  910. OS_MS8..OS_MS128:
  911. a_loadmm_ref_reg(list,location^.size,location^.size,tmpref,location^.register,nil);
  912. else
  913. internalerror(2010053101);
  914. end;
  915. end
  916. else
  917. internalerror(2010053111);
  918. end;
  919. inc(tmpref.offset,tcgsize2size[location^.size]);
  920. dec(sizeleft,tcgsize2size[location^.size]);
  921. location:=location^.next;
  922. end;
  923. end;
  924. procedure tcg.a_load_loc_cgpara(list : TAsmList;const l:tlocation;const cgpara : TCGPara);
  925. begin
  926. case l.loc of
  927. LOC_REGISTER,
  928. LOC_CREGISTER :
  929. a_load_reg_cgpara(list,l.size,l.register,cgpara);
  930. LOC_CONSTANT :
  931. a_load_const_cgpara(list,l.size,l.value,cgpara);
  932. LOC_CREFERENCE,
  933. LOC_REFERENCE :
  934. a_load_ref_cgpara(list,l.size,l.reference,cgpara);
  935. else
  936. internalerror(2002032211);
  937. end;
  938. end;
  939. procedure tcg.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : TCGPara);
  940. var
  941. hr : tregister;
  942. begin
  943. cgpara.check_simple_location;
  944. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  945. begin
  946. paramanager.allocparaloc(list,cgpara.location);
  947. a_loadaddr_ref_reg(list,r,cgpara.location^.register)
  948. end
  949. else
  950. begin
  951. hr:=getaddressregister(list);
  952. a_loadaddr_ref_reg(list,r,hr);
  953. a_load_reg_cgpara(list,OS_ADDR,hr,cgpara);
  954. end;
  955. end;
  956. procedure tcg.a_load_cgparaloc_ref(list : TAsmList;const paraloc : TCGParaLocation;const ref : treference;sizeleft : tcgint;align : longint);
  957. var
  958. href : treference;
  959. begin
  960. case paraloc.loc of
  961. LOC_REGISTER :
  962. begin
  963. {$IFDEF POWERPC64}
  964. if (paraloc.shiftval <> 0) then
  965. a_op_const_reg_reg(list, OP_SHL, OS_INT, paraloc.shiftval, paraloc.register, paraloc.register);
  966. {$ENDIF POWERPC64}
  967. a_load_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  968. end;
  969. LOC_MMREGISTER :
  970. begin
  971. case paraloc.size of
  972. OS_F32,
  973. OS_F64,
  974. OS_F128:
  975. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  976. OS_M8..OS_M128,
  977. OS_MS8..OS_MS128:
  978. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,nil);
  979. else
  980. internalerror(2010053102);
  981. end;
  982. end;
  983. LOC_FPUREGISTER :
  984. cg.a_loadfpu_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  985. LOC_REFERENCE :
  986. begin
  987. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,align);
  988. { use concatcopy, because it can also be a float which fails when
  989. load_ref_ref is used. Don't copy data when the references are equal }
  990. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  991. g_concatcopy(list,href,ref,sizeleft);
  992. end;
  993. else
  994. internalerror(2002081302);
  995. end;
  996. end;
  997. procedure tcg.a_load_cgparaloc_anyreg(list: TAsmList;regsize: tcgsize;const paraloc: TCGParaLocation;reg: tregister;align: longint);
  998. var
  999. href : treference;
  1000. begin
  1001. case paraloc.loc of
  1002. LOC_REGISTER :
  1003. begin
  1004. case getregtype(reg) of
  1005. R_INTREGISTER:
  1006. a_load_reg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1007. R_MMREGISTER:
  1008. a_loadmm_intreg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1009. else
  1010. internalerror(2009112422);
  1011. end;
  1012. end;
  1013. LOC_MMREGISTER :
  1014. begin
  1015. case getregtype(reg) of
  1016. R_INTREGISTER:
  1017. a_loadmm_reg_intreg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1018. R_MMREGISTER:
  1019. begin
  1020. case paraloc.size of
  1021. OS_F32,
  1022. OS_F64,
  1023. OS_F128:
  1024. a_loadmm_reg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1025. OS_M8..OS_M128,
  1026. OS_MS8..OS_MS128:
  1027. a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,nil);
  1028. else
  1029. internalerror(2010053102);
  1030. end;
  1031. end;
  1032. else
  1033. internalerror(2010053104);
  1034. end;
  1035. end;
  1036. LOC_FPUREGISTER :
  1037. a_loadfpu_reg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1038. LOC_REFERENCE :
  1039. begin
  1040. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,align);
  1041. case getregtype(reg) of
  1042. R_INTREGISTER :
  1043. a_load_ref_reg(list,paraloc.size,regsize,href,reg);
  1044. R_FPUREGISTER :
  1045. a_loadfpu_ref_reg(list,paraloc.size,regsize,href,reg);
  1046. R_MMREGISTER :
  1047. { not paraloc.size, because it may be OS_64 instead of
  1048. OS_F64 in case the parameter is passed using integer
  1049. conventions (e.g., on ARM) }
  1050. a_loadmm_ref_reg(list,regsize,regsize,href,reg,mms_movescalar);
  1051. else
  1052. internalerror(2004101012);
  1053. end;
  1054. end;
  1055. else
  1056. internalerror(2002081302);
  1057. end;
  1058. end;
  1059. {****************************************************************************
  1060. some generic implementations
  1061. ****************************************************************************}
  1062. {$push}
  1063. {$r-}
  1064. {$q-}
  1065. procedure tcg.a_load_subsetreg_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; destreg: tregister);
  1066. var
  1067. bitmask: aword;
  1068. tmpreg: tregister;
  1069. stopbit: byte;
  1070. begin
  1071. tmpreg:=getintregister(list,sreg.subsetregsize);
  1072. if (subsetsize in [OS_S8..OS_S128]) then
  1073. begin
  1074. { sign extend in case the value has a bitsize mod 8 <> 0 }
  1075. { both instructions will be optimized away if not }
  1076. a_op_const_reg_reg(list,OP_SHL,sreg.subsetregsize,(tcgsize2size[sreg.subsetregsize]*8)-sreg.startbit-sreg.bitlen,sreg.subsetreg,tmpreg);
  1077. a_op_const_reg(list,OP_SAR,sreg.subsetregsize,(tcgsize2size[sreg.subsetregsize]*8)-sreg.bitlen,tmpreg);
  1078. end
  1079. else
  1080. begin
  1081. a_op_const_reg_reg(list,OP_SHR,sreg.subsetregsize,sreg.startbit,sreg.subsetreg,tmpreg);
  1082. stopbit := sreg.startbit + sreg.bitlen;
  1083. // on x86(64), 1 shl 32(64) = 1 instead of 0
  1084. // use aword to prevent overflow with 1 shl 31
  1085. if (stopbit - sreg.startbit <> AIntBits) then
  1086. bitmask := (aword(1) shl (stopbit - sreg.startbit)) - 1
  1087. else
  1088. bitmask := high(aword);
  1089. a_op_const_reg(list,OP_AND,sreg.subsetregsize,tcgint(bitmask),tmpreg);
  1090. end;
  1091. tmpreg := makeregsize(list,tmpreg,subsetsize);
  1092. a_load_reg_reg(list,tcgsize2unsigned[subsetsize],subsetsize,tmpreg,tmpreg);
  1093. a_load_reg_reg(list,subsetsize,tosize,tmpreg,destreg);
  1094. end;
  1095. procedure tcg.a_load_reg_subsetreg(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister);
  1096. begin
  1097. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,sreg,SL_REG);
  1098. end;
  1099. procedure tcg.a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt);
  1100. var
  1101. bitmask: aword;
  1102. tmpreg: tregister;
  1103. stopbit: byte;
  1104. begin
  1105. stopbit := sreg.startbit + sreg.bitlen;
  1106. // on x86(64), 1 shl 32(64) = 1 instead of 0
  1107. if (stopbit <> AIntBits) then
  1108. bitmask := not(((aword(1) shl stopbit)-1) xor ((aword(1) shl sreg.startbit)-1))
  1109. else
  1110. bitmask := not(high(aword) xor ((aword(1) shl sreg.startbit)-1));
  1111. if not(slopt in [SL_SETZERO,SL_SETMAX]) then
  1112. begin
  1113. tmpreg:=getintregister(list,sreg.subsetregsize);
  1114. a_load_reg_reg(list,fromsize,sreg.subsetregsize,fromreg,tmpreg);
  1115. a_op_const_reg(list,OP_SHL,sreg.subsetregsize,sreg.startbit,tmpreg);
  1116. if (slopt <> SL_REGNOSRCMASK) then
  1117. a_op_const_reg(list,OP_AND,sreg.subsetregsize,tcgint(not(bitmask)),tmpreg);
  1118. end;
  1119. if (slopt <> SL_SETMAX) then
  1120. a_op_const_reg(list,OP_AND,sreg.subsetregsize,tcgint(bitmask),sreg.subsetreg);
  1121. case slopt of
  1122. SL_SETZERO : ;
  1123. SL_SETMAX :
  1124. if (sreg.bitlen <> AIntBits) then
  1125. a_op_const_reg(list,OP_OR,sreg.subsetregsize,
  1126. tcgint(((aword(1) shl sreg.bitlen)-1) shl sreg.startbit),
  1127. sreg.subsetreg)
  1128. else
  1129. a_load_const_reg(list,sreg.subsetregsize,-1,sreg.subsetreg);
  1130. else
  1131. a_op_reg_reg(list,OP_OR,sreg.subsetregsize,tmpreg,sreg.subsetreg);
  1132. end;
  1133. end;
  1134. procedure tcg.a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsreg, tosreg: tsubsetregister);
  1135. var
  1136. tmpreg: tregister;
  1137. bitmask: aword;
  1138. stopbit: byte;
  1139. begin
  1140. if (fromsreg.bitlen >= tosreg.bitlen) then
  1141. begin
  1142. tmpreg := getintregister(list,tosreg.subsetregsize);
  1143. a_load_reg_reg(list,fromsreg.subsetregsize,tosreg.subsetregsize,fromsreg.subsetreg,tmpreg);
  1144. if (fromsreg.startbit <= tosreg.startbit) then
  1145. a_op_const_reg(list,OP_SHL,tosreg.subsetregsize,tosreg.startbit-fromsreg.startbit,tmpreg)
  1146. else
  1147. a_op_const_reg(list,OP_SHR,tosreg.subsetregsize,fromsreg.startbit-tosreg.startbit,tmpreg);
  1148. stopbit := tosreg.startbit + tosreg.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 tosreg.startbit)-1))
  1152. else
  1153. bitmask := (aword(1) shl tosreg.startbit) - 1;
  1154. a_op_const_reg(list,OP_AND,tosreg.subsetregsize,tcgint(bitmask),tosreg.subsetreg);
  1155. a_op_const_reg(list,OP_AND,tosreg.subsetregsize,tcgint(not(bitmask)),tmpreg);
  1156. a_op_reg_reg(list,OP_OR,tosreg.subsetregsize,tmpreg,tosreg.subsetreg);
  1157. end
  1158. else
  1159. begin
  1160. tmpreg := getintregister(list,tosubsetsize);
  1161. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  1162. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  1163. end;
  1164. end;
  1165. procedure tcg.a_load_subsetreg_ref(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; const destref: treference);
  1166. var
  1167. tmpreg: tregister;
  1168. begin
  1169. tmpreg := getintregister(list,tosize);
  1170. a_load_subsetreg_reg(list,subsetsize,tosize,sreg,tmpreg);
  1171. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  1172. end;
  1173. procedure tcg.a_load_ref_subsetreg(list : TAsmList; fromsize, subsetsize: tcgsize; const fromref: treference; const sreg: tsubsetregister);
  1174. var
  1175. tmpreg: tregister;
  1176. begin
  1177. tmpreg := getintregister(list,subsetsize);
  1178. a_load_ref_reg(list,fromsize,subsetsize,fromref,tmpreg);
  1179. a_load_reg_subsetreg(list,subsetsize,subsetsize,tmpreg,sreg);
  1180. end;
  1181. procedure tcg.a_load_const_subsetreg(list: TAsmlist; subsetsize: tcgsize; a: tcgint; const sreg: tsubsetregister);
  1182. var
  1183. bitmask: aword;
  1184. stopbit: byte;
  1185. begin
  1186. stopbit := sreg.startbit + sreg.bitlen;
  1187. // on x86(64), 1 shl 32(64) = 1 instead of 0
  1188. if (stopbit <> AIntBits) then
  1189. bitmask := not(((aword(1) shl stopbit)-1) xor ((aword(1) shl sreg.startbit)-1))
  1190. else
  1191. bitmask := (aword(1) shl sreg.startbit) - 1;
  1192. if (((aword(a) shl sreg.startbit) and not bitmask) <> not bitmask) then
  1193. a_op_const_reg(list,OP_AND,sreg.subsetregsize,tcgint(bitmask),sreg.subsetreg);
  1194. a_op_const_reg(list,OP_OR,sreg.subsetregsize,tcgint((aword(a) shl sreg.startbit) and not(bitmask)),sreg.subsetreg);
  1195. end;
  1196. procedure tcg.a_load_loc_subsetref(list : TAsmList;subsetsize: tcgsize; const loc: tlocation; const sref : tsubsetreference);
  1197. begin
  1198. case loc.loc of
  1199. LOC_REFERENCE,LOC_CREFERENCE:
  1200. a_load_ref_subsetref(list,loc.size,subsetsize,loc.reference,sref);
  1201. LOC_REGISTER,LOC_CREGISTER:
  1202. a_load_reg_subsetref(list,loc.size,subsetsize,loc.register,sref);
  1203. LOC_CONSTANT:
  1204. a_load_const_subsetref(list,subsetsize,loc.value,sref);
  1205. LOC_SUBSETREG,LOC_CSUBSETREG:
  1206. a_load_subsetreg_subsetref(list,loc.size,subsetsize,loc.sreg,sref);
  1207. LOC_SUBSETREF,LOC_CSUBSETREF:
  1208. a_load_subsetref_subsetref(list,loc.size,subsetsize,loc.sref,sref);
  1209. else
  1210. internalerror(200608053);
  1211. end;
  1212. end;
  1213. (*
  1214. Subsetrefs are used for (bit)packed arrays and (bit)packed records stored
  1215. in memory. They are like a regular reference, but contain an extra bit
  1216. offset (either constant -startbit- or variable -bitindexreg-, always OS_INT)
  1217. and a bit length (always constant).
  1218. Bit packed values are stored differently in memory depending on whether we
  1219. are on a big or a little endian system (compatible with at least GPC). The
  1220. size of the basic working unit is always the smallest power-of-2 byte size
  1221. which can contain the bit value (so 1..8 bits -> 1 byte, 9..16 bits -> 2
  1222. bytes, 17..32 bits -> 4 bytes etc).
  1223. On a big endian, 5-bit: values are stored like this:
  1224. 11111222 22333334 44445555 56666677 77788888
  1225. The leftmost bit of each 5-bit value corresponds to the most significant
  1226. bit.
  1227. On little endian, it goes like this:
  1228. 22211111 43333322 55554444 77666665 88888777
  1229. In this case, per byte the left-most bit is more significant than those on
  1230. the right, but the bits in the next byte are all more significant than
  1231. those in the previous byte (e.g., the 222 in the first byte are the low
  1232. three bits of that value, while the 22 in the second byte are the upper
  1233. two bits.
  1234. Big endian, 9 bit values:
  1235. 11111111 12222222 22333333 33344444 ...
  1236. Little endian, 9 bit values:
  1237. 11111111 22222221 33333322 44444333 ...
  1238. This is memory representation and the 16 bit values are byteswapped.
  1239. Similarly as in the previous case, the 2222222 string contains the lower
  1240. bits of value 2 and the 22 string contains the upper bits. Once loaded into
  1241. registers (two 16 bit registers in the current implementation, although a
  1242. single 32 bit register would be possible too, in particular if 32 bit
  1243. alignment can be guaranteed), this becomes:
  1244. 22222221 11111111 44444333 33333322 ...
  1245. (l)ow u l l u l u
  1246. The startbit/bitindex in a subsetreference always refers to
  1247. a) on big endian: the most significant bit of the value
  1248. (bits counted from left to right, both memory an registers)
  1249. b) on little endian: the least significant bit when the value
  1250. is loaded in a register (bit counted from right to left)
  1251. Although a) results in more complex code for big endian systems, it's
  1252. needed for compatibility both with GPC and with e.g. bitpacked arrays in
  1253. Apple's universal interfaces which depend on these layout differences).
  1254. Note: when changing the loadsize calculated in get_subsetref_load_info,
  1255. make sure the appropriate alignment is guaranteed, at least in case of
  1256. {$defined cpurequiresproperalignment}.
  1257. *)
  1258. procedure tcg.get_subsetref_load_info(const sref: tsubsetreference; out loadsize: tcgsize; out extra_load: boolean);
  1259. var
  1260. intloadsize: tcgint;
  1261. begin
  1262. intloadsize := packedbitsloadsize(sref.bitlen);
  1263. if (intloadsize = 0) then
  1264. internalerror(2006081310);
  1265. if (intloadsize > sizeof(aint)) then
  1266. intloadsize := sizeof(aint);
  1267. loadsize := int_cgsize(intloadsize);
  1268. if (loadsize = OS_NO) then
  1269. internalerror(2006081311);
  1270. if (sref.bitlen > sizeof(aint)*8) then
  1271. internalerror(2006081312);
  1272. extra_load :=
  1273. (sref.bitlen <> 1) and
  1274. ((sref.bitindexreg <> NR_NO) or
  1275. (byte(sref.startbit+sref.bitlen) > byte(intloadsize*8)));
  1276. end;
  1277. procedure tcg.a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister);
  1278. var
  1279. restbits: byte;
  1280. begin
  1281. if (target_info.endian = endian_big) then
  1282. begin
  1283. { valuereg contains the upper bits, extra_value_reg the lower }
  1284. restbits := (sref.bitlen - (loadbitsize - sref.startbit));
  1285. if (subsetsize in [OS_S8..OS_S128]) then
  1286. begin
  1287. { sign extend }
  1288. a_op_const_reg(list,OP_SHL,OS_INT,AIntBits-loadbitsize+sref.startbit,valuereg);
  1289. a_op_const_reg(list,OP_SAR,OS_INT,AIntBits-sref.bitlen,valuereg);
  1290. end
  1291. else
  1292. begin
  1293. a_op_const_reg(list,OP_SHL,OS_INT,restbits,valuereg);
  1294. { mask other bits }
  1295. if (sref.bitlen <> AIntBits) then
  1296. a_op_const_reg(list,OP_AND,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1297. end;
  1298. a_op_const_reg(list,OP_SHR,OS_INT,loadbitsize-restbits,extra_value_reg)
  1299. end
  1300. else
  1301. begin
  1302. { valuereg contains the lower bits, extra_value_reg the upper }
  1303. a_op_const_reg(list,OP_SHR,OS_INT,sref.startbit,valuereg);
  1304. if (subsetsize in [OS_S8..OS_S128]) then
  1305. begin
  1306. a_op_const_reg(list,OP_SHL,OS_INT,AIntBits-sref.bitlen+loadbitsize-sref.startbit,extra_value_reg);
  1307. a_op_const_reg(list,OP_SAR,OS_INT,AIntBits-sref.bitlen,extra_value_reg);
  1308. end
  1309. else
  1310. begin
  1311. a_op_const_reg(list,OP_SHL,OS_INT,loadbitsize-sref.startbit,extra_value_reg);
  1312. { mask other bits }
  1313. if (sref.bitlen <> AIntBits) then
  1314. a_op_const_reg(list,OP_AND,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),extra_value_reg);
  1315. end;
  1316. end;
  1317. { merge }
  1318. a_op_reg_reg(list,OP_OR,OS_INT,extra_value_reg,valuereg);
  1319. end;
  1320. procedure tcg.a_load_subsetref_regs_index(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg: tregister);
  1321. var
  1322. hl: tasmlabel;
  1323. tmpref: treference;
  1324. extra_value_reg,
  1325. tmpreg: tregister;
  1326. begin
  1327. tmpreg := getintregister(list,OS_INT);
  1328. tmpref := sref.ref;
  1329. inc(tmpref.offset,loadbitsize div 8);
  1330. extra_value_reg := getintregister(list,OS_INT);
  1331. if (target_info.endian = endian_big) then
  1332. begin
  1333. { since this is a dynamic index, it's possible that the value }
  1334. { is entirely in valuereg. }
  1335. { get the data in valuereg in the right place }
  1336. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,valuereg);
  1337. if (subsetsize in [OS_S8..OS_S128]) then
  1338. begin
  1339. a_op_const_reg(list,OP_SHL,OS_INT,AIntBits-loadbitsize,valuereg);
  1340. a_op_const_reg(list,OP_SAR,OS_INT,AIntBits-sref.bitlen,valuereg)
  1341. end
  1342. else
  1343. begin
  1344. a_op_const_reg(list,OP_SHR,OS_INT,loadbitsize-sref.bitlen,valuereg);
  1345. if (loadbitsize <> AIntBits) then
  1346. { mask left over bits }
  1347. a_op_const_reg(list,OP_AND,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1348. end;
  1349. tmpreg := getintregister(list,OS_INT);
  1350. { ensure we don't load anything past the end of the array }
  1351. current_asmdata.getjumplabel(hl);
  1352. a_cmp_const_reg_label(list,OS_INT,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  1353. { the bits in extra_value_reg (if any) start at the most significant bit => }
  1354. { extra_value_reg must be shr by (loadbitsize-sref.bitlen)+(loadsize-sref.bitindex) }
  1355. { => = -(sref.bitindex+(sref.bitlen-2*loadbitsize)) }
  1356. a_op_const_reg_reg(list,OP_ADD,OS_INT,sref.bitlen-2*loadbitsize,sref.bitindexreg,tmpreg);
  1357. a_op_reg_reg(list,OP_NEG,OS_INT,tmpreg,tmpreg);
  1358. { load next "loadbitsize" bits of the array }
  1359. a_load_ref_reg(list,int_cgsize(loadbitsize div 8),OS_INT,tmpref,extra_value_reg);
  1360. a_op_reg_reg(list,OP_SHR,OS_INT,tmpreg,extra_value_reg);
  1361. { if there are no bits in extra_value_reg, then sref.bitindex was }
  1362. { < loadsize-sref.bitlen, and therefore tmpreg will now be >= loadsize }
  1363. { => extra_value_reg is now 0 }
  1364. { merge }
  1365. a_op_reg_reg(list,OP_OR,OS_INT,extra_value_reg,valuereg);
  1366. { no need to mask, necessary masking happened earlier on }
  1367. a_label(list,hl);
  1368. end
  1369. else
  1370. begin
  1371. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,valuereg);
  1372. { ensure we don't load anything past the end of the array }
  1373. current_asmdata.getjumplabel(hl);
  1374. a_cmp_const_reg_label(list,OS_INT,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  1375. { Y-x = -(Y-x) }
  1376. a_op_const_reg_reg(list,OP_SUB,OS_INT,loadbitsize,sref.bitindexreg,tmpreg);
  1377. a_op_reg_reg(list,OP_NEG,OS_INT,tmpreg,tmpreg);
  1378. { load next "loadbitsize" bits of the array }
  1379. a_load_ref_reg(list,int_cgsize(loadbitsize div 8),OS_INT,tmpref,extra_value_reg);
  1380. { tmpreg is in the range 1..<cpu_bitsize>-1 -> always ok }
  1381. a_op_reg_reg(list,OP_SHL,OS_INT,tmpreg,extra_value_reg);
  1382. { merge }
  1383. a_op_reg_reg(list,OP_OR,OS_INT,extra_value_reg,valuereg);
  1384. a_label(list,hl);
  1385. { sign extend or mask other bits }
  1386. if (subsetsize in [OS_S8..OS_S128]) then
  1387. begin
  1388. a_op_const_reg(list,OP_SHL,OS_INT,AIntBits-sref.bitlen,valuereg);
  1389. a_op_const_reg(list,OP_SAR,OS_INT,AIntBits-sref.bitlen,valuereg);
  1390. end
  1391. else
  1392. a_op_const_reg(list,OP_AND,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1393. end;
  1394. end;
  1395. procedure tcg.a_load_subsetref_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sref: tsubsetreference; destreg: tregister);
  1396. var
  1397. tmpref: treference;
  1398. valuereg,extra_value_reg: tregister;
  1399. tosreg: tsubsetregister;
  1400. loadsize: tcgsize;
  1401. loadbitsize: byte;
  1402. extra_load: boolean;
  1403. begin
  1404. get_subsetref_load_info(sref,loadsize,extra_load);
  1405. loadbitsize := tcgsize2size[loadsize]*8;
  1406. { load the (first part) of the bit sequence }
  1407. valuereg := getintregister(list,OS_INT);
  1408. a_load_ref_reg(list,loadsize,OS_INT,sref.ref,valuereg);
  1409. if not extra_load then
  1410. begin
  1411. { everything is guaranteed to be in a single register of loadsize }
  1412. if (sref.bitindexreg = NR_NO) then
  1413. begin
  1414. { use subsetreg routine, it may have been overridden with an optimized version }
  1415. tosreg.subsetreg := valuereg;
  1416. tosreg.subsetregsize := OS_INT;
  1417. { subsetregs always count bits from right to left }
  1418. if (target_info.endian = endian_big) then
  1419. tosreg.startbit := loadbitsize - (sref.startbit+sref.bitlen)
  1420. else
  1421. tosreg.startbit := sref.startbit;
  1422. tosreg.bitlen := sref.bitlen;
  1423. a_load_subsetreg_reg(list,subsetsize,tosize,tosreg,destreg);
  1424. exit;
  1425. end
  1426. else
  1427. begin
  1428. if (sref.startbit <> 0) then
  1429. internalerror(2006081510);
  1430. if (target_info.endian = endian_big) then
  1431. begin
  1432. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,valuereg);
  1433. if (subsetsize in [OS_S8..OS_S128]) then
  1434. begin
  1435. { sign extend to entire register }
  1436. a_op_const_reg(list,OP_SHL,OS_INT,AIntBits-loadbitsize,valuereg);
  1437. a_op_const_reg(list,OP_SAR,OS_INT,AIntBits-sref.bitlen,valuereg);
  1438. end
  1439. else
  1440. a_op_const_reg(list,OP_SHR,OS_INT,loadbitsize-sref.bitlen,valuereg);
  1441. end
  1442. else
  1443. begin
  1444. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,valuereg);
  1445. if (subsetsize in [OS_S8..OS_S128]) then
  1446. begin
  1447. a_op_const_reg(list,OP_SHL,OS_INT,AIntBits-sref.bitlen,valuereg);
  1448. a_op_const_reg(list,OP_SAR,OS_INT,AIntBits-sref.bitlen,valuereg);
  1449. end
  1450. end;
  1451. { mask other bits/sign extend }
  1452. if not(subsetsize in [OS_S8..OS_S128]) then
  1453. a_op_const_reg(list,OP_AND,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1454. end
  1455. end
  1456. else
  1457. begin
  1458. { load next value as well }
  1459. extra_value_reg := getintregister(list,OS_INT);
  1460. if (sref.bitindexreg = NR_NO) then
  1461. begin
  1462. tmpref := sref.ref;
  1463. inc(tmpref.offset,loadbitsize div 8);
  1464. a_load_ref_reg(list,loadsize,OS_INT,tmpref,extra_value_reg);
  1465. { can be overridden to optimize }
  1466. a_load_subsetref_regs_noindex(list,subsetsize,loadbitsize,sref,valuereg,extra_value_reg)
  1467. end
  1468. else
  1469. begin
  1470. if (sref.startbit <> 0) then
  1471. internalerror(2006080610);
  1472. a_load_subsetref_regs_index(list,subsetsize,loadbitsize,sref,valuereg);
  1473. end;
  1474. end;
  1475. { store in destination }
  1476. { avoid unnecessary sign extension and zeroing }
  1477. valuereg := makeregsize(list,valuereg,OS_INT);
  1478. destreg := makeregsize(list,destreg,OS_INT);
  1479. a_load_reg_reg(list,OS_INT,OS_INT,valuereg,destreg);
  1480. destreg := makeregsize(list,destreg,tosize);
  1481. end;
  1482. procedure tcg.a_load_reg_subsetref(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sref: tsubsetreference);
  1483. begin
  1484. a_load_regconst_subsetref_intern(list,fromsize,subsetsize,fromreg,sref,SL_REG);
  1485. end;
  1486. procedure tcg.a_load_regconst_subsetref_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sref: tsubsetreference; slopt: tsubsetloadopt);
  1487. var
  1488. hl: tasmlabel;
  1489. tmpreg, tmpindexreg, valuereg, extra_value_reg, maskreg: tregister;
  1490. tosreg, fromsreg: tsubsetregister;
  1491. tmpref: treference;
  1492. bitmask: aword;
  1493. loadsize: tcgsize;
  1494. loadbitsize: byte;
  1495. extra_load: boolean;
  1496. begin
  1497. { the register must be able to contain the requested value }
  1498. if (tcgsize2size[fromsize]*8 < sref.bitlen) then
  1499. internalerror(2006081613);
  1500. get_subsetref_load_info(sref,loadsize,extra_load);
  1501. loadbitsize := tcgsize2size[loadsize]*8;
  1502. { load the (first part) of the bit sequence }
  1503. valuereg := getintregister(list,OS_INT);
  1504. a_load_ref_reg(list,loadsize,OS_INT,sref.ref,valuereg);
  1505. { constant offset of bit sequence? }
  1506. if not extra_load then
  1507. begin
  1508. if (sref.bitindexreg = NR_NO) then
  1509. begin
  1510. { use subsetreg routine, it may have been overridden with an optimized version }
  1511. tosreg.subsetreg := valuereg;
  1512. tosreg.subsetregsize := OS_INT;
  1513. { subsetregs always count bits from right to left }
  1514. if (target_info.endian = endian_big) then
  1515. tosreg.startbit := loadbitsize - (sref.startbit+sref.bitlen)
  1516. else
  1517. tosreg.startbit := sref.startbit;
  1518. tosreg.bitlen := sref.bitlen;
  1519. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1520. end
  1521. else
  1522. begin
  1523. if (sref.startbit <> 0) then
  1524. internalerror(2006081710);
  1525. { should be handled by normal code and will give wrong result }
  1526. { on x86 for the '1 shl bitlen' below }
  1527. if (sref.bitlen = AIntBits) then
  1528. internalerror(2006081711);
  1529. { zero the bits we have to insert }
  1530. if (slopt <> SL_SETMAX) then
  1531. begin
  1532. maskreg := getintregister(list,OS_INT);
  1533. if (target_info.endian = endian_big) then
  1534. begin
  1535. a_load_const_reg(list,OS_INT,tcgint((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen),maskreg);
  1536. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,maskreg);
  1537. end
  1538. else
  1539. begin
  1540. a_load_const_reg(list,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1541. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,maskreg);
  1542. end;
  1543. a_op_reg_reg(list,OP_NOT,OS_INT,maskreg,maskreg);
  1544. a_op_reg_reg(list,OP_AND,OS_INT,maskreg,valuereg);
  1545. end;
  1546. { insert the value }
  1547. if (slopt <> SL_SETZERO) then
  1548. begin
  1549. tmpreg := getintregister(list,OS_INT);
  1550. if (slopt <> SL_SETMAX) then
  1551. a_load_reg_reg(list,fromsize,OS_INT,fromreg,tmpreg)
  1552. else if (sref.bitlen <> AIntBits) then
  1553. a_load_const_reg(list,OS_INT,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  1554. else
  1555. a_load_const_reg(list,OS_INT,-1,tmpreg);
  1556. if (target_info.endian = endian_big) then
  1557. begin
  1558. a_op_const_reg(list,OP_SHL,OS_INT,loadbitsize-sref.bitlen,tmpreg);
  1559. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1560. begin
  1561. if (loadbitsize <> AIntBits) then
  1562. bitmask := (((aword(1) shl loadbitsize)-1) xor ((aword(1) shl (loadbitsize-sref.bitlen))-1))
  1563. else
  1564. bitmask := (high(aword) xor ((aword(1) shl (loadbitsize-sref.bitlen))-1));
  1565. a_op_const_reg(list,OP_AND,OS_INT,bitmask,tmpreg);
  1566. end;
  1567. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,tmpreg);
  1568. end
  1569. else
  1570. begin
  1571. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1572. a_op_const_reg(list,OP_AND,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1573. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,tmpreg);
  1574. end;
  1575. a_op_reg_reg(list,OP_OR,OS_INT,tmpreg,valuereg);
  1576. end;
  1577. end;
  1578. { store back to memory }
  1579. valuereg := makeregsize(list,valuereg,loadsize);
  1580. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1581. exit;
  1582. end
  1583. else
  1584. begin
  1585. { load next value }
  1586. extra_value_reg := getintregister(list,OS_INT);
  1587. tmpref := sref.ref;
  1588. inc(tmpref.offset,loadbitsize div 8);
  1589. { should maybe be taken out too, can be done more efficiently }
  1590. { on e.g. i386 with shld/shrd }
  1591. if (sref.bitindexreg = NR_NO) then
  1592. begin
  1593. a_load_ref_reg(list,loadsize,OS_INT,tmpref,extra_value_reg);
  1594. fromsreg.subsetreg := fromreg;
  1595. fromsreg.subsetregsize := fromsize;
  1596. tosreg.subsetreg := valuereg;
  1597. tosreg.subsetregsize := OS_INT;
  1598. { transfer first part }
  1599. fromsreg.bitlen := loadbitsize-sref.startbit;
  1600. tosreg.bitlen := fromsreg.bitlen;
  1601. if (target_info.endian = endian_big) then
  1602. begin
  1603. { valuereg must contain the upper bits of the value at bits [0..loadbitsize-startbit] }
  1604. { upper bits of the value ... }
  1605. fromsreg.startbit := sref.bitlen-(loadbitsize-sref.startbit);
  1606. { ... to bit 0 }
  1607. tosreg.startbit := 0
  1608. end
  1609. else
  1610. begin
  1611. { valuereg must contain the lower bits of the value at bits [startbit..loadbitsize] }
  1612. { lower bits of the value ... }
  1613. fromsreg.startbit := 0;
  1614. { ... to startbit }
  1615. tosreg.startbit := sref.startbit;
  1616. end;
  1617. case slopt of
  1618. SL_SETZERO,
  1619. SL_SETMAX:
  1620. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1621. else
  1622. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1623. end;
  1624. valuereg := makeregsize(list,valuereg,loadsize);
  1625. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1626. { transfer second part }
  1627. if (target_info.endian = endian_big) then
  1628. begin
  1629. { extra_value_reg must contain the lower bits of the value at bits }
  1630. { [(loadbitsize-(bitlen-(loadbitsize-startbit)))..loadbitsize] }
  1631. { (loadbitsize-(bitlen-(loadbitsize-startbit))) = 2*loadbitsize }
  1632. { - bitlen - startbit }
  1633. fromsreg.startbit := 0;
  1634. tosreg.startbit := 2*loadbitsize - sref.bitlen - sref.startbit
  1635. end
  1636. else
  1637. begin
  1638. { extra_value_reg must contain the upper bits of the value at bits [0..bitlen-(loadbitsize-startbit)] }
  1639. fromsreg.startbit := fromsreg.bitlen;
  1640. tosreg.startbit := 0;
  1641. end;
  1642. tosreg.subsetreg := extra_value_reg;
  1643. fromsreg.bitlen := sref.bitlen-fromsreg.bitlen;
  1644. tosreg.bitlen := fromsreg.bitlen;
  1645. case slopt of
  1646. SL_SETZERO,
  1647. SL_SETMAX:
  1648. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1649. else
  1650. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1651. end;
  1652. extra_value_reg := makeregsize(list,extra_value_reg,loadsize);
  1653. a_load_reg_ref(list,loadsize,loadsize,extra_value_reg,tmpref);
  1654. exit;
  1655. end
  1656. else
  1657. begin
  1658. if (sref.startbit <> 0) then
  1659. internalerror(2006081812);
  1660. { should be handled by normal code and will give wrong result }
  1661. { on x86 for the '1 shl bitlen' below }
  1662. if (sref.bitlen = AIntBits) then
  1663. internalerror(2006081713);
  1664. { generate mask to zero the bits we have to insert }
  1665. if (slopt <> SL_SETMAX) then
  1666. begin
  1667. maskreg := getintregister(list,OS_INT);
  1668. if (target_info.endian = endian_big) then
  1669. begin
  1670. a_load_const_reg(list,OS_INT,tcgint(((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen)),maskreg);
  1671. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,maskreg);
  1672. end
  1673. else
  1674. begin
  1675. a_load_const_reg(list,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1676. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,maskreg);
  1677. end;
  1678. a_op_reg_reg(list,OP_NOT,OS_INT,maskreg,maskreg);
  1679. a_op_reg_reg(list,OP_AND,OS_INT,maskreg,valuereg);
  1680. end;
  1681. { insert the value }
  1682. if (slopt <> SL_SETZERO) then
  1683. begin
  1684. tmpreg := getintregister(list,OS_INT);
  1685. if (slopt <> SL_SETMAX) then
  1686. a_load_reg_reg(list,fromsize,OS_INT,fromreg,tmpreg)
  1687. else if (sref.bitlen <> AIntBits) then
  1688. a_load_const_reg(list,OS_INT,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  1689. else
  1690. a_load_const_reg(list,OS_INT,-1,tmpreg);
  1691. if (target_info.endian = endian_big) then
  1692. begin
  1693. a_op_const_reg(list,OP_SHL,OS_INT,loadbitsize-sref.bitlen,tmpreg);
  1694. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1695. { mask left over bits }
  1696. a_op_const_reg(list,OP_AND,OS_INT,tcgint(((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen)),tmpreg);
  1697. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,tmpreg);
  1698. end
  1699. else
  1700. begin
  1701. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1702. { mask left over bits }
  1703. a_op_const_reg(list,OP_AND,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1704. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,tmpreg);
  1705. end;
  1706. a_op_reg_reg(list,OP_OR,OS_INT,tmpreg,valuereg);
  1707. end;
  1708. valuereg := makeregsize(list,valuereg,loadsize);
  1709. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1710. { make sure we do not read/write past the end of the array }
  1711. current_asmdata.getjumplabel(hl);
  1712. a_cmp_const_reg_label(list,OS_INT,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  1713. a_load_ref_reg(list,loadsize,OS_INT,tmpref,extra_value_reg);
  1714. tmpindexreg := getintregister(list,OS_INT);
  1715. { load current array value }
  1716. if (slopt <> SL_SETZERO) then
  1717. begin
  1718. tmpreg := getintregister(list,OS_INT);
  1719. if (slopt <> SL_SETMAX) then
  1720. a_load_reg_reg(list,fromsize,OS_INT,fromreg,tmpreg)
  1721. else if (sref.bitlen <> AIntBits) then
  1722. a_load_const_reg(list,OS_INT,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  1723. else
  1724. a_load_const_reg(list,OS_INT,-1,tmpreg);
  1725. end;
  1726. { generate mask to zero the bits we have to insert }
  1727. if (slopt <> SL_SETMAX) then
  1728. begin
  1729. maskreg := getintregister(list,OS_INT);
  1730. if (target_info.endian = endian_big) then
  1731. begin
  1732. a_op_const_reg_reg(list,OP_ADD,OS_INT,sref.bitlen-2*loadbitsize,sref.bitindexreg,tmpindexreg);
  1733. a_op_reg_reg(list,OP_NEG,OS_INT,tmpindexreg,tmpindexreg);
  1734. a_load_const_reg(list,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1735. a_op_reg_reg(list,OP_SHL,OS_INT,tmpindexreg,maskreg);
  1736. end
  1737. else
  1738. begin
  1739. { Y-x = -(x-Y) }
  1740. a_op_const_reg_reg(list,OP_SUB,OS_INT,loadbitsize,sref.bitindexreg,tmpindexreg);
  1741. a_op_reg_reg(list,OP_NEG,OS_INT,tmpindexreg,tmpindexreg);
  1742. a_load_const_reg(list,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1743. a_op_reg_reg(list,OP_SHR,OS_INT,tmpindexreg,maskreg);
  1744. end;
  1745. a_op_reg_reg(list,OP_NOT,OS_INT,maskreg,maskreg);
  1746. a_op_reg_reg(list,OP_AND,OS_INT,maskreg,extra_value_reg);
  1747. end;
  1748. if (slopt <> SL_SETZERO) then
  1749. begin
  1750. if (target_info.endian = endian_big) then
  1751. a_op_reg_reg(list,OP_SHL,OS_INT,tmpindexreg,tmpreg)
  1752. else
  1753. begin
  1754. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1755. a_op_const_reg(list,OP_AND,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1756. a_op_reg_reg(list,OP_SHR,OS_INT,tmpindexreg,tmpreg);
  1757. end;
  1758. a_op_reg_reg(list,OP_OR,OS_INT,tmpreg,extra_value_reg);
  1759. end;
  1760. extra_value_reg := makeregsize(list,extra_value_reg,loadsize);
  1761. a_load_reg_ref(list,loadsize,loadsize,extra_value_reg,tmpref);
  1762. a_label(list,hl);
  1763. end;
  1764. end;
  1765. end;
  1766. procedure tcg.a_load_subsetref_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsref, tosref: tsubsetreference);
  1767. var
  1768. tmpreg: tregister;
  1769. begin
  1770. tmpreg := getintregister(list,tosubsetsize);
  1771. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1772. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1773. end;
  1774. procedure tcg.a_load_subsetref_ref(list : TAsmList; subsetsize, tosize: tcgsize; const sref: tsubsetreference; const destref: treference);
  1775. var
  1776. tmpreg: tregister;
  1777. begin
  1778. tmpreg := getintregister(list,tosize);
  1779. a_load_subsetref_reg(list,subsetsize,tosize,sref,tmpreg);
  1780. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  1781. end;
  1782. procedure tcg.a_load_ref_subsetref(list : TAsmList; fromsize, subsetsize: tcgsize; const fromref: treference; const sref: tsubsetreference);
  1783. var
  1784. tmpreg: tregister;
  1785. begin
  1786. tmpreg := getintregister(list,subsetsize);
  1787. a_load_ref_reg(list,fromsize,subsetsize,fromref,tmpreg);
  1788. a_load_reg_subsetref(list,subsetsize,subsetsize,tmpreg,sref);
  1789. end;
  1790. procedure tcg.a_load_const_subsetref(list: TAsmlist; subsetsize: tcgsize; a: tcgint; const sref: tsubsetreference);
  1791. var
  1792. tmpreg: tregister;
  1793. slopt: tsubsetloadopt;
  1794. begin
  1795. { perform masking of the source value in advance }
  1796. slopt := SL_REGNOSRCMASK;
  1797. if (sref.bitlen <> AIntBits) then
  1798. a := tcgint(aword(a) and ((aword(1) shl sref.bitlen) -1));
  1799. if (
  1800. { broken x86 "x shl regbitsize = x" }
  1801. ((sref.bitlen <> AIntBits) and
  1802. ((aword(a) and ((aword(1) shl sref.bitlen) -1)) = (aword(1) shl sref.bitlen) -1)) or
  1803. ((sref.bitlen = AIntBits) and
  1804. (a = -1))
  1805. ) then
  1806. slopt := SL_SETMAX
  1807. else if (a = 0) then
  1808. slopt := SL_SETZERO;
  1809. tmpreg := getintregister(list,subsetsize);
  1810. if not(slopt in [SL_SETZERO,SL_SETMAX]) then
  1811. a_load_const_reg(list,subsetsize,a,tmpreg);
  1812. a_load_regconst_subsetref_intern(list,subsetsize,subsetsize,tmpreg,sref,slopt);
  1813. end;
  1814. procedure tcg.a_load_subsetref_loc(list: TAsmlist; subsetsize: tcgsize; const sref: tsubsetreference; const loc: tlocation);
  1815. begin
  1816. case loc.loc of
  1817. LOC_REFERENCE,LOC_CREFERENCE:
  1818. a_load_subsetref_ref(list,subsetsize,loc.size,sref,loc.reference);
  1819. LOC_REGISTER,LOC_CREGISTER:
  1820. a_load_subsetref_reg(list,subsetsize,loc.size,sref,loc.register);
  1821. LOC_SUBSETREG,LOC_CSUBSETREG:
  1822. a_load_subsetref_subsetreg(list,subsetsize,loc.size,sref,loc.sreg);
  1823. LOC_SUBSETREF,LOC_CSUBSETREF:
  1824. a_load_subsetref_subsetref(list,subsetsize,loc.size,sref,loc.sref);
  1825. else
  1826. internalerror(200608054);
  1827. end;
  1828. end;
  1829. procedure tcg.a_load_subsetref_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsref: tsubsetreference; const tosreg: tsubsetregister);
  1830. var
  1831. tmpreg: tregister;
  1832. begin
  1833. tmpreg := getintregister(list,tosubsetsize);
  1834. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1835. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  1836. end;
  1837. procedure tcg.a_load_subsetreg_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsreg: tsubsetregister; const tosref: tsubsetreference);
  1838. var
  1839. tmpreg: tregister;
  1840. begin
  1841. tmpreg := getintregister(list,tosubsetsize);
  1842. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  1843. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1844. end;
  1845. {$pop}
  1846. { generic bit address calculation routines }
  1847. function tcg.get_bit_const_ref_sref(bitnumber: tcgint; const ref: treference): tsubsetreference;
  1848. begin
  1849. result.ref:=ref;
  1850. inc(result.ref.offset,bitnumber div 8);
  1851. result.bitindexreg:=NR_NO;
  1852. result.startbit:=bitnumber mod 8;
  1853. result.bitlen:=1;
  1854. end;
  1855. function tcg.get_bit_const_reg_sreg(setregsize: tcgsize; bitnumber: tcgint; setreg: tregister): tsubsetregister;
  1856. begin
  1857. result.subsetreg:=setreg;
  1858. result.subsetregsize:=setregsize;
  1859. { subsetregs always count from the least significant to the most significant bit }
  1860. if (target_info.endian=endian_big) then
  1861. result.startbit:=(tcgsize2size[setregsize]*8)-bitnumber-1
  1862. else
  1863. result.startbit:=bitnumber;
  1864. result.bitlen:=1;
  1865. end;
  1866. function tcg.get_bit_reg_ref_sref(list: TAsmList; bitnumbersize: tcgsize; bitnumber: tregister; const ref: treference): tsubsetreference;
  1867. var
  1868. tmpreg,
  1869. tmpaddrreg: tregister;
  1870. begin
  1871. result.ref:=ref;
  1872. result.startbit:=0;
  1873. result.bitlen:=1;
  1874. tmpreg:=getintregister(list,bitnumbersize);
  1875. a_op_const_reg_reg(list,OP_SHR,bitnumbersize,3,bitnumber,tmpreg);
  1876. tmpaddrreg:=getaddressregister(list);
  1877. a_load_reg_reg(list,bitnumbersize,OS_ADDR,tmpreg,tmpaddrreg);
  1878. if (result.ref.base=NR_NO) then
  1879. result.ref.base:=tmpaddrreg
  1880. else if (result.ref.index=NR_NO) then
  1881. result.ref.index:=tmpaddrreg
  1882. else
  1883. begin
  1884. a_op_reg_reg(list,OP_ADD,OS_ADDR,result.ref.index,tmpaddrreg);
  1885. result.ref.index:=tmpaddrreg;
  1886. end;
  1887. tmpreg:=getintregister(list,OS_INT);
  1888. a_op_const_reg_reg(list,OP_AND,OS_INT,7,bitnumber,tmpreg);
  1889. result.bitindexreg:=tmpreg;
  1890. end;
  1891. { bit testing routines }
  1892. procedure tcg.a_bit_test_reg_reg_reg(list : TAsmList; bitnumbersize,valuesize,destsize: tcgsize;bitnumber,value,destreg: tregister);
  1893. var
  1894. tmpvalue: tregister;
  1895. begin
  1896. tmpvalue:=getintregister(list,valuesize);
  1897. if (target_info.endian=endian_little) then
  1898. begin
  1899. { rotate value register "bitnumber" bits to the right }
  1900. a_op_reg_reg_reg(list,OP_SHR,valuesize,bitnumber,value,tmpvalue);
  1901. { extract the bit we want }
  1902. a_op_const_reg(list,OP_AND,valuesize,1,tmpvalue);
  1903. end
  1904. else
  1905. begin
  1906. { highest (leftmost) bit = bit 0 -> shl bitnumber results in wanted }
  1907. { bit in uppermost position, then move it to the lowest position }
  1908. { "and" is not necessary since combination of shl/shr will clear }
  1909. { all other bits }
  1910. a_op_reg_reg_reg(list,OP_SHL,valuesize,bitnumber,value,tmpvalue);
  1911. a_op_const_reg(list,OP_SHR,valuesize,tcgsize2size[valuesize]*8-1,tmpvalue);
  1912. end;
  1913. a_load_reg_reg(list,valuesize,destsize,tmpvalue,destreg);
  1914. end;
  1915. procedure tcg.a_bit_test_const_ref_reg(list: TAsmList; destsize: tcgsize; bitnumber: tcgint; const ref: treference; destreg: tregister);
  1916. begin
  1917. a_load_subsetref_reg(list,OS_8,destsize,get_bit_const_ref_sref(bitnumber,ref),destreg);
  1918. end;
  1919. procedure tcg.a_bit_test_const_reg_reg(list: TAsmList; setregsize, destsize: tcgsize; bitnumber: tcgint; setreg, destreg: tregister);
  1920. begin
  1921. a_load_subsetreg_reg(list,setregsize,destsize,get_bit_const_reg_sreg(setregsize,bitnumber,setreg),destreg);
  1922. end;
  1923. procedure tcg.a_bit_test_const_subsetreg_reg(list: TAsmList; setregsize, destsize: tcgsize; bitnumber: tcgint; const setreg: tsubsetregister; destreg: tregister);
  1924. var
  1925. tmpsreg: tsubsetregister;
  1926. begin
  1927. { the first parameter is used to calculate the bit offset in }
  1928. { case of big endian, and therefore must be the size of the }
  1929. { set and not of the whole subsetreg }
  1930. tmpsreg:=get_bit_const_reg_sreg(setregsize,bitnumber,setreg.subsetreg);
  1931. { now fix the size of the subsetreg }
  1932. tmpsreg.subsetregsize:=setreg.subsetregsize;
  1933. { correct offset of the set in the subsetreg }
  1934. inc(tmpsreg.startbit,setreg.startbit);
  1935. a_load_subsetreg_reg(list,setregsize,destsize,tmpsreg,destreg);
  1936. end;
  1937. procedure tcg.a_bit_test_reg_ref_reg(list: TAsmList; bitnumbersize, destsize: tcgsize; bitnumber: tregister; const ref: treference; destreg: tregister);
  1938. begin
  1939. a_load_subsetref_reg(list,OS_8,destsize,get_bit_reg_ref_sref(list,bitnumbersize,bitnumber,ref),destreg);
  1940. end;
  1941. procedure tcg.a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, destsize: tcgsize; bitnumber: tregister; const loc: tlocation; destreg: tregister);
  1942. var
  1943. tmpreg: tregister;
  1944. begin
  1945. case loc.loc of
  1946. LOC_REFERENCE,LOC_CREFERENCE:
  1947. a_bit_test_reg_ref_reg(list,bitnumbersize,destsize,bitnumber,loc.reference,destreg);
  1948. LOC_REGISTER,LOC_CREGISTER,
  1949. LOC_SUBSETREG,LOC_CSUBSETREG,
  1950. LOC_CONSTANT:
  1951. begin
  1952. case loc.loc of
  1953. LOC_REGISTER,LOC_CREGISTER:
  1954. tmpreg:=loc.register;
  1955. LOC_SUBSETREG,LOC_CSUBSETREG:
  1956. begin
  1957. tmpreg:=getintregister(list,loc.size);
  1958. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  1959. end;
  1960. LOC_CONSTANT:
  1961. begin
  1962. tmpreg:=getintregister(list,loc.size);
  1963. a_load_const_reg(list,loc.size,loc.value,tmpreg);
  1964. end;
  1965. end;
  1966. a_bit_test_reg_reg_reg(list,bitnumbersize,loc.size,destsize,bitnumber,tmpreg,destreg);
  1967. end;
  1968. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1969. else
  1970. internalerror(2007051701);
  1971. end;
  1972. end;
  1973. procedure tcg.a_bit_test_const_loc_reg(list: TAsmList; destsize: tcgsize; bitnumber: tcgint; const loc: tlocation; destreg: tregister);
  1974. begin
  1975. case loc.loc of
  1976. LOC_REFERENCE,LOC_CREFERENCE:
  1977. a_bit_test_const_ref_reg(list,destsize,bitnumber,loc.reference,destreg);
  1978. LOC_REGISTER,LOC_CREGISTER:
  1979. a_bit_test_const_reg_reg(list,loc.size,destsize,bitnumber,loc.register,destreg);
  1980. LOC_SUBSETREG,LOC_CSUBSETREG:
  1981. a_bit_test_const_subsetreg_reg(list,loc.size,destsize,bitnumber,loc.sreg,destreg);
  1982. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1983. else
  1984. internalerror(2007051702);
  1985. end;
  1986. end;
  1987. { bit setting/clearing routines }
  1988. procedure tcg.a_bit_set_reg_reg(list : TAsmList; doset: boolean; bitnumbersize, destsize: tcgsize; bitnumber,dest: tregister);
  1989. var
  1990. tmpvalue: tregister;
  1991. begin
  1992. tmpvalue:=getintregister(list,destsize);
  1993. if (target_info.endian=endian_little) then
  1994. begin
  1995. a_load_const_reg(list,destsize,1,tmpvalue);
  1996. { rotate bit "bitnumber" bits to the left }
  1997. a_op_reg_reg(list,OP_SHL,destsize,bitnumber,tmpvalue);
  1998. end
  1999. else
  2000. begin
  2001. { highest (leftmost) bit = bit 0 -> "$80/$8000/$80000000/ ... }
  2002. { shr bitnumber" results in correct mask }
  2003. a_load_const_reg(list,destsize,1 shl (tcgsize2size[destsize]*8-1),tmpvalue);
  2004. a_op_reg_reg(list,OP_SHR,destsize,bitnumber,tmpvalue);
  2005. end;
  2006. { set/clear the bit we want }
  2007. if (doset) then
  2008. a_op_reg_reg(list,OP_OR,destsize,tmpvalue,dest)
  2009. else
  2010. begin
  2011. a_op_reg_reg(list,OP_NOT,destsize,tmpvalue,tmpvalue);
  2012. a_op_reg_reg(list,OP_AND,destsize,tmpvalue,dest)
  2013. end;
  2014. end;
  2015. procedure tcg.a_bit_set_const_ref(list: TAsmList; doset: boolean;destsize: tcgsize; bitnumber: tcgint; const ref: treference);
  2016. begin
  2017. a_load_const_subsetref(list,OS_8,ord(doset),get_bit_const_ref_sref(bitnumber,ref));
  2018. end;
  2019. procedure tcg.a_bit_set_const_reg(list: TAsmList; doset: boolean; destsize: tcgsize; bitnumber: tcgint; destreg: tregister);
  2020. begin
  2021. a_load_const_subsetreg(list,OS_8,ord(doset),get_bit_const_reg_sreg(destsize,bitnumber,destreg));
  2022. end;
  2023. procedure tcg.a_bit_set_const_subsetreg(list: TAsmList; doset: boolean; destsize: tcgsize; bitnumber: tcgint; const destreg: tsubsetregister);
  2024. var
  2025. tmpsreg: tsubsetregister;
  2026. begin
  2027. { the first parameter is used to calculate the bit offset in }
  2028. { case of big endian, and therefore must be the size of the }
  2029. { set and not of the whole subsetreg }
  2030. tmpsreg:=get_bit_const_reg_sreg(destsize,bitnumber,destreg.subsetreg);
  2031. { now fix the size of the subsetreg }
  2032. tmpsreg.subsetregsize:=destreg.subsetregsize;
  2033. { correct offset of the set in the subsetreg }
  2034. inc(tmpsreg.startbit,destreg.startbit);
  2035. a_load_const_subsetreg(list,OS_8,ord(doset),tmpsreg);
  2036. end;
  2037. procedure tcg.a_bit_set_reg_ref(list: TAsmList; doset: boolean; bitnumbersize: tcgsize; bitnumber: tregister; const ref: treference);
  2038. begin
  2039. a_load_const_subsetref(list,OS_8,ord(doset),get_bit_reg_ref_sref(list,bitnumbersize,bitnumber,ref));
  2040. end;
  2041. procedure tcg.a_bit_set_reg_loc(list: TAsmList; doset: boolean; bitnumbersize: tcgsize; bitnumber: tregister; const loc: tlocation);
  2042. var
  2043. tmpreg: tregister;
  2044. begin
  2045. case loc.loc of
  2046. LOC_REFERENCE:
  2047. a_bit_set_reg_ref(list,doset,bitnumbersize,bitnumber,loc.reference);
  2048. LOC_CREGISTER:
  2049. a_bit_set_reg_reg(list,doset,bitnumbersize,loc.size,bitnumber,loc.register);
  2050. { e.g. a 2-byte set in a record regvar }
  2051. LOC_CSUBSETREG:
  2052. begin
  2053. { hard to do in-place in a generic way, so operate on a copy }
  2054. tmpreg:=getintregister(list,loc.size);
  2055. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  2056. a_bit_set_reg_reg(list,doset,bitnumbersize,loc.size,bitnumber,tmpreg);
  2057. a_load_reg_subsetreg(list,loc.size,loc.size,tmpreg,loc.sreg);
  2058. end;
  2059. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  2060. else
  2061. internalerror(2007051703)
  2062. end;
  2063. end;
  2064. procedure tcg.a_bit_set_const_loc(list: TAsmList; doset: boolean; bitnumber: tcgint; const loc: tlocation);
  2065. begin
  2066. case loc.loc of
  2067. LOC_REFERENCE:
  2068. a_bit_set_const_ref(list,doset,loc.size,bitnumber,loc.reference);
  2069. LOC_CREGISTER:
  2070. a_bit_set_const_reg(list,doset,loc.size,bitnumber,loc.register);
  2071. LOC_CSUBSETREG:
  2072. a_bit_set_const_subsetreg(list,doset,loc.size,bitnumber,loc.sreg);
  2073. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  2074. else
  2075. internalerror(2007051704)
  2076. end;
  2077. end;
  2078. { memory/register loading }
  2079. procedure tcg.a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  2080. var
  2081. tmpref : treference;
  2082. tmpreg : tregister;
  2083. i : longint;
  2084. begin
  2085. if ref.alignment<tcgsize2size[fromsize] then
  2086. begin
  2087. tmpref:=ref;
  2088. { we take care of the alignment now }
  2089. tmpref.alignment:=0;
  2090. case FromSize of
  2091. OS_16,OS_S16:
  2092. begin
  2093. tmpreg:=getintregister(list,OS_16);
  2094. a_load_reg_reg(list,fromsize,OS_16,register,tmpreg);
  2095. if target_info.endian=endian_big then
  2096. inc(tmpref.offset);
  2097. tmpreg:=makeregsize(list,tmpreg,OS_8);
  2098. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  2099. tmpreg:=makeregsize(list,tmpreg,OS_16);
  2100. a_op_const_reg(list,OP_SHR,OS_16,8,tmpreg);
  2101. if target_info.endian=endian_big then
  2102. dec(tmpref.offset)
  2103. else
  2104. inc(tmpref.offset);
  2105. tmpreg:=makeregsize(list,tmpreg,OS_8);
  2106. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  2107. end;
  2108. OS_32,OS_S32:
  2109. begin
  2110. { could add an optimised case for ref.alignment=2 }
  2111. tmpreg:=getintregister(list,OS_32);
  2112. a_load_reg_reg(list,fromsize,OS_32,register,tmpreg);
  2113. if target_info.endian=endian_big then
  2114. inc(tmpref.offset,3);
  2115. tmpreg:=makeregsize(list,tmpreg,OS_8);
  2116. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  2117. tmpreg:=makeregsize(list,tmpreg,OS_32);
  2118. for i:=1 to 3 do
  2119. begin
  2120. a_op_const_reg(list,OP_SHR,OS_32,8,tmpreg);
  2121. if target_info.endian=endian_big then
  2122. dec(tmpref.offset)
  2123. else
  2124. inc(tmpref.offset);
  2125. tmpreg:=makeregsize(list,tmpreg,OS_8);
  2126. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  2127. tmpreg:=makeregsize(list,tmpreg,OS_32);
  2128. end;
  2129. end
  2130. else
  2131. a_load_reg_ref(list,fromsize,tosize,register,tmpref);
  2132. end;
  2133. end
  2134. else
  2135. a_load_reg_ref(list,fromsize,tosize,register,ref);
  2136. end;
  2137. procedure tcg.a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  2138. var
  2139. tmpref : treference;
  2140. tmpreg,
  2141. tmpreg2 : tregister;
  2142. i : longint;
  2143. begin
  2144. if ref.alignment in [1,2] then
  2145. begin
  2146. tmpref:=ref;
  2147. { we take care of the alignment now }
  2148. tmpref.alignment:=0;
  2149. case FromSize of
  2150. OS_16,OS_S16:
  2151. if ref.alignment=2 then
  2152. a_load_ref_reg(list,fromsize,tosize,tmpref,register)
  2153. else
  2154. begin
  2155. { first load in tmpreg, because the target register }
  2156. { may be used in ref as well }
  2157. if target_info.endian=endian_little then
  2158. inc(tmpref.offset);
  2159. tmpreg:=getintregister(list,OS_8);
  2160. a_load_ref_reg(list,OS_8,OS_8,tmpref,tmpreg);
  2161. tmpreg:=makeregsize(list,tmpreg,OS_16);
  2162. a_op_const_reg(list,OP_SHL,OS_16,8,tmpreg);
  2163. if target_info.endian=endian_little then
  2164. dec(tmpref.offset)
  2165. else
  2166. inc(tmpref.offset);
  2167. a_load_ref_reg(list,OS_8,OS_16,tmpref,register);
  2168. a_op_reg_reg(list,OP_OR,OS_16,tmpreg,register);
  2169. end;
  2170. OS_32,OS_S32:
  2171. if ref.alignment=2 then
  2172. begin
  2173. if target_info.endian=endian_little then
  2174. inc(tmpref.offset,2);
  2175. tmpreg:=getintregister(list,OS_32);
  2176. a_load_ref_reg(list,OS_16,OS_32,tmpref,tmpreg);
  2177. a_op_const_reg(list,OP_SHL,OS_32,16,tmpreg);
  2178. if target_info.endian=endian_little then
  2179. dec(tmpref.offset,2)
  2180. else
  2181. inc(tmpref.offset,2);
  2182. a_load_ref_reg(list,OS_16,OS_32,tmpref,register);
  2183. a_op_reg_reg(list,OP_OR,OS_32,tmpreg,register);
  2184. end
  2185. else
  2186. begin
  2187. if target_info.endian=endian_little then
  2188. inc(tmpref.offset,3);
  2189. tmpreg:=getintregister(list,OS_32);
  2190. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg);
  2191. tmpreg2:=getintregister(list,OS_32);
  2192. for i:=1 to 3 do
  2193. begin
  2194. a_op_const_reg(list,OP_SHL,OS_32,8,tmpreg);
  2195. if target_info.endian=endian_little then
  2196. dec(tmpref.offset)
  2197. else
  2198. inc(tmpref.offset);
  2199. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg2);
  2200. a_op_reg_reg(list,OP_OR,OS_32,tmpreg2,tmpreg);
  2201. end;
  2202. a_load_reg_reg(list,OS_32,OS_32,tmpreg,register);
  2203. end
  2204. else
  2205. a_load_ref_reg(list,fromsize,tosize,tmpref,register);
  2206. end;
  2207. end
  2208. else
  2209. a_load_ref_reg(list,fromsize,tosize,ref,register);
  2210. end;
  2211. procedure tcg.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  2212. var
  2213. tmpreg: tregister;
  2214. begin
  2215. { verify if we have the same reference }
  2216. if references_equal(sref,dref) then
  2217. exit;
  2218. tmpreg:=getintregister(list,tosize);
  2219. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  2220. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  2221. end;
  2222. procedure tcg.a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : treference);
  2223. var
  2224. tmpreg: tregister;
  2225. begin
  2226. tmpreg:=getintregister(list,size);
  2227. a_load_const_reg(list,size,a,tmpreg);
  2228. a_load_reg_ref(list,size,size,tmpreg,ref);
  2229. end;
  2230. procedure tcg.a_load_const_loc(list : TAsmList;a : tcgint;const loc: tlocation);
  2231. begin
  2232. case loc.loc of
  2233. LOC_REFERENCE,LOC_CREFERENCE:
  2234. a_load_const_ref(list,loc.size,a,loc.reference);
  2235. LOC_REGISTER,LOC_CREGISTER:
  2236. a_load_const_reg(list,loc.size,a,loc.register);
  2237. LOC_SUBSETREG,LOC_CSUBSETREG:
  2238. a_load_const_subsetreg(list,loc.size,a,loc.sreg);
  2239. LOC_SUBSETREF,LOC_CSUBSETREF:
  2240. a_load_const_subsetref(list,loc.size,a,loc.sref);
  2241. else
  2242. internalerror(200203272);
  2243. end;
  2244. end;
  2245. procedure tcg.a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  2246. begin
  2247. case loc.loc of
  2248. LOC_REFERENCE,LOC_CREFERENCE:
  2249. a_load_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  2250. LOC_REGISTER,LOC_CREGISTER:
  2251. a_load_reg_reg(list,fromsize,loc.size,reg,loc.register);
  2252. LOC_SUBSETREG,LOC_CSUBSETREG:
  2253. a_load_reg_subsetreg(list,fromsize,loc.size,reg,loc.sreg);
  2254. LOC_SUBSETREF,LOC_CSUBSETREF:
  2255. a_load_reg_subsetref(list,fromsize,loc.size,reg,loc.sref);
  2256. LOC_MMREGISTER,LOC_CMMREGISTER:
  2257. a_loadmm_intreg_reg(list,fromsize,loc.size,reg,loc.register,mms_movescalar);
  2258. else
  2259. internalerror(200203271);
  2260. end;
  2261. end;
  2262. procedure tcg.a_load_loc_reg(list : TAsmList; tosize: tcgsize; const loc: tlocation; reg : tregister);
  2263. begin
  2264. case loc.loc of
  2265. LOC_REFERENCE,LOC_CREFERENCE:
  2266. a_load_ref_reg(list,loc.size,tosize,loc.reference,reg);
  2267. LOC_REGISTER,LOC_CREGISTER:
  2268. a_load_reg_reg(list,loc.size,tosize,loc.register,reg);
  2269. LOC_CONSTANT:
  2270. a_load_const_reg(list,tosize,loc.value,reg);
  2271. LOC_SUBSETREG,LOC_CSUBSETREG:
  2272. a_load_subsetreg_reg(list,loc.size,tosize,loc.sreg,reg);
  2273. LOC_SUBSETREF,LOC_CSUBSETREF:
  2274. a_load_subsetref_reg(list,loc.size,tosize,loc.sref,reg);
  2275. else
  2276. internalerror(200109092);
  2277. end;
  2278. end;
  2279. procedure tcg.a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  2280. begin
  2281. case loc.loc of
  2282. LOC_REFERENCE,LOC_CREFERENCE:
  2283. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  2284. LOC_REGISTER,LOC_CREGISTER:
  2285. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  2286. LOC_CONSTANT:
  2287. a_load_const_ref(list,tosize,loc.value,ref);
  2288. LOC_SUBSETREG,LOC_CSUBSETREG:
  2289. a_load_subsetreg_ref(list,loc.size,tosize,loc.sreg,ref);
  2290. LOC_SUBSETREF,LOC_CSUBSETREF:
  2291. a_load_subsetref_ref(list,loc.size,tosize,loc.sref,ref);
  2292. else
  2293. internalerror(200109302);
  2294. end;
  2295. end;
  2296. procedure tcg.a_load_loc_subsetreg(list : TAsmList; subsetsize: tcgsize; const loc: tlocation; const sreg : tsubsetregister);
  2297. begin
  2298. case loc.loc of
  2299. LOC_REFERENCE,LOC_CREFERENCE:
  2300. a_load_ref_subsetreg(list,loc.size,subsetsize,loc.reference,sreg);
  2301. LOC_REGISTER,LOC_CREGISTER:
  2302. a_load_reg_subsetreg(list,loc.size,subsetsize,loc.register,sreg);
  2303. LOC_CONSTANT:
  2304. a_load_const_subsetreg(list,subsetsize,loc.value,sreg);
  2305. LOC_SUBSETREG,LOC_CSUBSETREG:
  2306. a_load_subsetreg_subsetreg(list,loc.size,subsetsize,loc.sreg,sreg);
  2307. LOC_SUBSETREF,LOC_CSUBSETREF:
  2308. a_load_subsetref_subsetreg(list,loc.size,subsetsize,loc.sref,sreg);
  2309. else
  2310. internalerror(2006052310);
  2311. end;
  2312. end;
  2313. procedure tcg.a_load_subsetreg_loc(list: TAsmlist; subsetsize: tcgsize; const sreg: tsubsetregister; const loc: tlocation);
  2314. begin
  2315. case loc.loc of
  2316. LOC_REFERENCE,LOC_CREFERENCE:
  2317. a_load_subsetreg_ref(list,subsetsize,loc.size,sreg,loc.reference);
  2318. LOC_REGISTER,LOC_CREGISTER:
  2319. a_load_subsetreg_reg(list,subsetsize,loc.size,sreg,loc.register);
  2320. LOC_SUBSETREG,LOC_CSUBSETREG:
  2321. a_load_subsetreg_subsetreg(list,subsetsize,loc.size,sreg,loc.sreg);
  2322. LOC_SUBSETREF,LOC_CSUBSETREF:
  2323. a_load_subsetreg_subsetref(list,subsetsize,loc.size,sreg,loc.sref);
  2324. else
  2325. internalerror(2006051510);
  2326. end;
  2327. end;
  2328. procedure tcg.optimize_op_const(var op: topcg; var a : tcgint);
  2329. var
  2330. powerval : longint;
  2331. begin
  2332. case op of
  2333. OP_OR :
  2334. begin
  2335. { or with zero returns same result }
  2336. if a = 0 then
  2337. op:=OP_NONE
  2338. else
  2339. { or with max returns max }
  2340. if a = -1 then
  2341. op:=OP_MOVE;
  2342. end;
  2343. OP_AND :
  2344. begin
  2345. { and with max returns same result }
  2346. if (a = -1) then
  2347. op:=OP_NONE
  2348. else
  2349. { and with 0 returns 0 }
  2350. if a=0 then
  2351. op:=OP_MOVE;
  2352. end;
  2353. OP_DIV :
  2354. begin
  2355. { division by 1 returns result }
  2356. if a = 1 then
  2357. op:=OP_NONE
  2358. else if ispowerof2(int64(a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  2359. begin
  2360. a := powerval;
  2361. op:= OP_SHR;
  2362. end;
  2363. end;
  2364. OP_IDIV:
  2365. begin
  2366. if a = 1 then
  2367. op:=OP_NONE;
  2368. end;
  2369. OP_MUL,OP_IMUL:
  2370. begin
  2371. if a = 1 then
  2372. op:=OP_NONE
  2373. else
  2374. if a=0 then
  2375. op:=OP_MOVE
  2376. else if ispowerof2(int64(a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  2377. begin
  2378. a := powerval;
  2379. op:= OP_SHL;
  2380. end;
  2381. end;
  2382. OP_ADD,OP_SUB:
  2383. begin
  2384. if a = 0 then
  2385. op:=OP_NONE;
  2386. end;
  2387. OP_SAR,OP_SHL,OP_SHR,OP_ROL,OP_ROR:
  2388. begin
  2389. if a = 0 then
  2390. op:=OP_NONE;
  2391. end;
  2392. end;
  2393. end;
  2394. procedure tcg.a_loadfpu_loc_reg(list: TAsmList; tosize: tcgsize; const loc: tlocation; const reg: tregister);
  2395. begin
  2396. case loc.loc of
  2397. LOC_REFERENCE, LOC_CREFERENCE:
  2398. a_loadfpu_ref_reg(list,loc.size,tosize,loc.reference,reg);
  2399. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  2400. a_loadfpu_reg_reg(list,loc.size,tosize,loc.register,reg);
  2401. else
  2402. internalerror(200203301);
  2403. end;
  2404. end;
  2405. procedure tcg.a_loadfpu_reg_loc(list: TAsmList; fromsize: tcgsize; const reg: tregister; const loc: tlocation);
  2406. begin
  2407. case loc.loc of
  2408. LOC_REFERENCE, LOC_CREFERENCE:
  2409. a_loadfpu_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  2410. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  2411. a_loadfpu_reg_reg(list,fromsize,loc.size,reg,loc.register);
  2412. else
  2413. internalerror(48991);
  2414. end;
  2415. end;
  2416. procedure tcg.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tcgsize; const ref1,ref2: treference);
  2417. var
  2418. reg: tregister;
  2419. regsize: tcgsize;
  2420. begin
  2421. if (fromsize>=tosize) then
  2422. regsize:=fromsize
  2423. else
  2424. regsize:=tosize;
  2425. reg:=getfpuregister(list,regsize);
  2426. a_loadfpu_ref_reg(list,fromsize,regsize,ref1,reg);
  2427. a_loadfpu_reg_ref(list,regsize,tosize,reg,ref2);
  2428. end;
  2429. procedure tcg.a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);
  2430. var
  2431. ref : treference;
  2432. begin
  2433. paramanager.alloccgpara(list,cgpara);
  2434. case cgpara.location^.loc of
  2435. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  2436. begin
  2437. cgpara.check_simple_location;
  2438. a_loadfpu_reg_reg(list,size,size,r,cgpara.location^.register);
  2439. end;
  2440. LOC_REFERENCE,LOC_CREFERENCE:
  2441. begin
  2442. cgpara.check_simple_location;
  2443. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  2444. a_loadfpu_reg_ref(list,size,size,r,ref);
  2445. end;
  2446. LOC_REGISTER,LOC_CREGISTER:
  2447. begin
  2448. { paramfpu_ref does the check_simpe_location check here if necessary }
  2449. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,ref);
  2450. a_loadfpu_reg_ref(list,size,size,r,ref);
  2451. a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  2452. tg.Ungettemp(list,ref);
  2453. end;
  2454. else
  2455. internalerror(2010053112);
  2456. end;
  2457. end;
  2458. procedure tcg.a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);
  2459. var
  2460. href : treference;
  2461. hsize: tcgsize;
  2462. begin
  2463. case cgpara.location^.loc of
  2464. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  2465. begin
  2466. cgpara.check_simple_location;
  2467. paramanager.alloccgpara(list,cgpara);
  2468. a_loadfpu_ref_reg(list,size,size,ref,cgpara.location^.register);
  2469. end;
  2470. LOC_REFERENCE,LOC_CREFERENCE:
  2471. begin
  2472. cgpara.check_simple_location;
  2473. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  2474. { concatcopy should choose the best way to copy the data }
  2475. g_concatcopy(list,ref,href,tcgsize2size[size]);
  2476. end;
  2477. LOC_REGISTER,LOC_CREGISTER:
  2478. begin
  2479. { force integer size }
  2480. hsize:=int_cgsize(tcgsize2size[size]);
  2481. {$ifndef cpu64bitalu}
  2482. if (hsize in [OS_S64,OS_64]) then
  2483. cg64.a_load64_ref_cgpara(list,ref,cgpara)
  2484. else
  2485. {$endif not cpu64bitalu}
  2486. begin
  2487. cgpara.check_simple_location;
  2488. a_load_ref_cgpara(list,hsize,ref,cgpara)
  2489. end;
  2490. end
  2491. else
  2492. internalerror(200402201);
  2493. end;
  2494. end;
  2495. procedure tcg.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  2496. var
  2497. tmpreg : tregister;
  2498. begin
  2499. tmpreg:=getintregister(list,size);
  2500. a_load_ref_reg(list,size,size,ref,tmpreg);
  2501. a_op_const_reg(list,op,size,a,tmpreg);
  2502. a_load_reg_ref(list,size,size,tmpreg,ref);
  2503. end;
  2504. procedure tcg.a_op_const_subsetreg(list : TAsmList; Op : TOpCG; size, subsetsize : TCGSize; a : tcgint; const sreg: tsubsetregister);
  2505. var
  2506. tmpreg: tregister;
  2507. begin
  2508. tmpreg := getintregister(list, size);
  2509. a_load_subsetreg_reg(list,subsetsize,size,sreg,tmpreg);
  2510. a_op_const_reg(list,op,size,a,tmpreg);
  2511. a_load_reg_subsetreg(list,size,subsetsize,tmpreg,sreg);
  2512. end;
  2513. procedure tcg.a_op_const_subsetref(list : TAsmList; Op : TOpCG; size, subsetsize : TCGSize; a : tcgint; const sref: tsubsetreference);
  2514. var
  2515. tmpreg: tregister;
  2516. begin
  2517. tmpreg := getintregister(list, size);
  2518. a_load_subsetref_reg(list,subsetsize,size,sref,tmpreg);
  2519. a_op_const_reg(list,op,size,a,tmpreg);
  2520. a_load_reg_subsetref(list,size,subsetsize,tmpreg,sref);
  2521. end;
  2522. procedure tcg.a_op_const_loc(list : TAsmList; Op: TOpCG; a: tcgint; const loc: tlocation);
  2523. begin
  2524. case loc.loc of
  2525. LOC_REGISTER, LOC_CREGISTER:
  2526. a_op_const_reg(list,op,loc.size,a,loc.register);
  2527. LOC_REFERENCE, LOC_CREFERENCE:
  2528. a_op_const_ref(list,op,loc.size,a,loc.reference);
  2529. LOC_SUBSETREG, LOC_CSUBSETREG:
  2530. a_op_const_subsetreg(list,op,loc.size,loc.size,a,loc.sreg);
  2531. LOC_SUBSETREF, LOC_CSUBSETREF:
  2532. a_op_const_subsetref(list,op,loc.size,loc.size,a,loc.sref);
  2533. else
  2534. internalerror(200109061);
  2535. end;
  2536. end;
  2537. procedure tcg.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  2538. var
  2539. tmpreg : tregister;
  2540. begin
  2541. tmpreg:=getintregister(list,size);
  2542. a_load_ref_reg(list,size,size,ref,tmpreg);
  2543. a_op_reg_reg(list,op,size,reg,tmpreg);
  2544. a_load_reg_ref(list,size,size,tmpreg,ref);
  2545. end;
  2546. procedure tcg.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  2547. var
  2548. tmpreg: tregister;
  2549. begin
  2550. case op of
  2551. OP_NOT,OP_NEG:
  2552. { handle it as "load ref,reg; op reg" }
  2553. begin
  2554. a_load_ref_reg(list,size,size,ref,reg);
  2555. a_op_reg_reg(list,op,size,reg,reg);
  2556. end;
  2557. else
  2558. begin
  2559. tmpreg:=getintregister(list,size);
  2560. a_load_ref_reg(list,size,size,ref,tmpreg);
  2561. a_op_reg_reg(list,op,size,tmpreg,reg);
  2562. end;
  2563. end;
  2564. end;
  2565. procedure tcg.a_op_reg_subsetreg(list : TAsmList; Op : TOpCG; opsize, subsetsize : TCGSize; reg: TRegister; const sreg: tsubsetregister);
  2566. var
  2567. tmpreg: tregister;
  2568. begin
  2569. tmpreg := getintregister(list, opsize);
  2570. a_load_subsetreg_reg(list,subsetsize,opsize,sreg,tmpreg);
  2571. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  2572. a_load_reg_subsetreg(list,opsize,subsetsize,tmpreg,sreg);
  2573. end;
  2574. procedure tcg.a_op_reg_subsetref(list : TAsmList; Op : TOpCG; opsize, subsetsize : TCGSize; reg: TRegister; const sref: tsubsetreference);
  2575. var
  2576. tmpreg: tregister;
  2577. begin
  2578. tmpreg := getintregister(list, opsize);
  2579. a_load_subsetref_reg(list,subsetsize,opsize,sref,tmpreg);
  2580. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  2581. a_load_reg_subsetref(list,opsize,subsetsize,tmpreg,sref);
  2582. end;
  2583. procedure tcg.a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  2584. begin
  2585. case loc.loc of
  2586. LOC_REGISTER, LOC_CREGISTER:
  2587. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  2588. LOC_REFERENCE, LOC_CREFERENCE:
  2589. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  2590. LOC_SUBSETREG, LOC_CSUBSETREG:
  2591. a_op_reg_subsetreg(list,op,loc.size,loc.size,reg,loc.sreg);
  2592. LOC_SUBSETREF, LOC_CSUBSETREF:
  2593. a_op_reg_subsetref(list,op,loc.size,loc.size,reg,loc.sref);
  2594. else
  2595. internalerror(200109061);
  2596. end;
  2597. end;
  2598. procedure tcg.a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  2599. var
  2600. tmpreg: tregister;
  2601. begin
  2602. case loc.loc of
  2603. LOC_REGISTER,LOC_CREGISTER:
  2604. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  2605. LOC_REFERENCE,LOC_CREFERENCE:
  2606. begin
  2607. tmpreg:=getintregister(list,loc.size);
  2608. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  2609. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  2610. end;
  2611. LOC_SUBSETREG, LOC_CSUBSETREG:
  2612. begin
  2613. tmpreg:=getintregister(list,loc.size);
  2614. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  2615. a_op_ref_reg(list,op,loc.size,ref,tmpreg);
  2616. a_load_reg_subsetreg(list,loc.size,loc.size,tmpreg,loc.sreg);
  2617. end;
  2618. LOC_SUBSETREF, LOC_CSUBSETREF:
  2619. begin
  2620. tmpreg:=getintregister(list,loc.size);
  2621. a_load_subsetreF_reg(list,loc.size,loc.size,loc.sref,tmpreg);
  2622. a_op_ref_reg(list,op,loc.size,ref,tmpreg);
  2623. a_load_reg_subsetref(list,loc.size,loc.size,tmpreg,loc.sref);
  2624. end;
  2625. else
  2626. internalerror(200109061);
  2627. end;
  2628. end;
  2629. procedure Tcg.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  2630. a:tcgint;src,dst:Tregister);
  2631. begin
  2632. a_load_reg_reg(list,size,size,src,dst);
  2633. a_op_const_reg(list,op,size,a,dst);
  2634. end;
  2635. procedure tcg.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  2636. size: tcgsize; src1, src2, dst: tregister);
  2637. var
  2638. tmpreg: tregister;
  2639. begin
  2640. if (dst<>src1) then
  2641. begin
  2642. a_load_reg_reg(list,size,size,src2,dst);
  2643. a_op_reg_reg(list,op,size,src1,dst);
  2644. end
  2645. else
  2646. begin
  2647. { can we do a direct operation on the target register ? }
  2648. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  2649. a_op_reg_reg(list,op,size,src2,dst)
  2650. else
  2651. begin
  2652. tmpreg:=getintregister(list,size);
  2653. a_load_reg_reg(list,size,size,src2,tmpreg);
  2654. a_op_reg_reg(list,op,size,src1,tmpreg);
  2655. a_load_reg_reg(list,size,size,tmpreg,dst);
  2656. end;
  2657. end;
  2658. end;
  2659. procedure tcg.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  2660. begin
  2661. a_op_const_reg_reg(list,op,size,a,src,dst);
  2662. ovloc.loc:=LOC_VOID;
  2663. end;
  2664. procedure tcg.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  2665. begin
  2666. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  2667. ovloc.loc:=LOC_VOID;
  2668. end;
  2669. procedure tcg.a_cmp_const_reg_label(list: TAsmList; size: tcgsize;
  2670. cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  2671. var
  2672. tmpreg: tregister;
  2673. begin
  2674. tmpreg:=getintregister(list,size);
  2675. a_load_const_reg(list,size,a,tmpreg);
  2676. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2677. end;
  2678. procedure tcg.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  2679. l : tasmlabel);
  2680. var
  2681. tmpreg: tregister;
  2682. begin
  2683. tmpreg:=getintregister(list,size);
  2684. a_load_ref_reg(list,size,size,ref,tmpreg);
  2685. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2686. end;
  2687. procedure tcg.a_cmp_const_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const loc : tlocation;
  2688. l : tasmlabel);
  2689. var
  2690. tmpreg : tregister;
  2691. begin
  2692. case loc.loc of
  2693. LOC_REGISTER,LOC_CREGISTER:
  2694. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  2695. LOC_REFERENCE,LOC_CREFERENCE:
  2696. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  2697. LOC_SUBSETREG, LOC_CSUBSETREG:
  2698. begin
  2699. tmpreg:=getintregister(list,size);
  2700. a_load_subsetreg_reg(list,loc.size,size,loc.sreg,tmpreg);
  2701. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2702. end;
  2703. LOC_SUBSETREF, LOC_CSUBSETREF:
  2704. begin
  2705. tmpreg:=getintregister(list,size);
  2706. a_load_subsetref_reg(list,loc.size,size,loc.sref,tmpreg);
  2707. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2708. end;
  2709. else
  2710. internalerror(200109061);
  2711. end;
  2712. end;
  2713. procedure tcg.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  2714. var
  2715. tmpreg: tregister;
  2716. begin
  2717. tmpreg:=getintregister(list,size);
  2718. a_load_ref_reg(list,size,size,ref,tmpreg);
  2719. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2720. end;
  2721. procedure tcg.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg : tregister; const ref: treference; l : tasmlabel);
  2722. var
  2723. tmpreg: tregister;
  2724. begin
  2725. tmpreg:=getintregister(list,size);
  2726. a_load_ref_reg(list,size,size,ref,tmpreg);
  2727. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  2728. end;
  2729. procedure tcg.a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  2730. begin
  2731. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  2732. end;
  2733. procedure tcg.a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  2734. begin
  2735. case loc.loc of
  2736. LOC_REGISTER,
  2737. LOC_CREGISTER:
  2738. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  2739. LOC_REFERENCE,
  2740. LOC_CREFERENCE :
  2741. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  2742. LOC_CONSTANT:
  2743. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  2744. LOC_SUBSETREG,
  2745. LOC_CSUBSETREG:
  2746. a_cmp_subsetreg_reg_label(list,loc.size,size,cmp_op,loc.sreg,reg,l);
  2747. LOC_SUBSETREF,
  2748. LOC_CSUBSETREF:
  2749. a_cmp_subsetref_reg_label(list,loc.size,size,cmp_op,loc.sref,reg,l);
  2750. else
  2751. internalerror(200203231);
  2752. end;
  2753. end;
  2754. procedure tcg.a_cmp_subsetreg_reg_label(list : TAsmList; subsetsize : tcgsize; cmpsize : tcgsize; cmp_op : topcmp; const sreg: tsubsetregister; reg : tregister; l : tasmlabel);
  2755. var
  2756. tmpreg: tregister;
  2757. begin
  2758. tmpreg:=getintregister(list, cmpsize);
  2759. a_load_subsetreg_reg(list,subsetsize,cmpsize,sreg,tmpreg);
  2760. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  2761. end;
  2762. procedure tcg.a_cmp_subsetref_reg_label(list : TAsmList; subsetsize, cmpsize : tcgsize; cmp_op : topcmp; const sref: tsubsetreference; reg : tregister; l : tasmlabel);
  2763. var
  2764. tmpreg: tregister;
  2765. begin
  2766. tmpreg:=getintregister(list, cmpsize);
  2767. a_load_subsetref_reg(list,subsetsize,cmpsize,sref,tmpreg);
  2768. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  2769. end;
  2770. procedure tcg.a_cmp_ref_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  2771. l : tasmlabel);
  2772. var
  2773. tmpreg: tregister;
  2774. begin
  2775. case loc.loc of
  2776. LOC_REGISTER,LOC_CREGISTER:
  2777. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  2778. LOC_REFERENCE,LOC_CREFERENCE:
  2779. begin
  2780. tmpreg:=getintregister(list,size);
  2781. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2782. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  2783. end;
  2784. LOC_SUBSETREG, LOC_CSUBSETREG:
  2785. begin
  2786. tmpreg:=getintregister(list, size);
  2787. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2788. a_cmp_subsetreg_reg_label(list,loc.size,size,swap_opcmp(cmp_op),loc.sreg,tmpreg,l);
  2789. end;
  2790. LOC_SUBSETREF, LOC_CSUBSETREF:
  2791. begin
  2792. tmpreg:=getintregister(list, size);
  2793. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2794. a_cmp_subsetref_reg_label(list,loc.size,size,swap_opcmp(cmp_op),loc.sref,tmpreg,l);
  2795. end;
  2796. else
  2797. internalerror(200109061);
  2798. end;
  2799. end;
  2800. procedure tcg.a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  2801. begin
  2802. case loc.loc of
  2803. LOC_MMREGISTER,LOC_CMMREGISTER:
  2804. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  2805. LOC_REFERENCE,LOC_CREFERENCE:
  2806. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  2807. LOC_REGISTER,LOC_CREGISTER:
  2808. a_loadmm_intreg_reg(list,loc.size,size,loc.register,reg,shuffle);
  2809. else
  2810. internalerror(200310121);
  2811. end;
  2812. end;
  2813. procedure tcg.a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  2814. begin
  2815. case loc.loc of
  2816. LOC_MMREGISTER,LOC_CMMREGISTER:
  2817. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  2818. LOC_REFERENCE,LOC_CREFERENCE:
  2819. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  2820. else
  2821. internalerror(200310122);
  2822. end;
  2823. end;
  2824. procedure tcg.a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle);
  2825. var
  2826. href : treference;
  2827. {$ifndef cpu64bitalu}
  2828. tmpreg : tregister;
  2829. reg64 : tregister64;
  2830. {$endif not cpu64bitalu}
  2831. begin
  2832. {$ifndef cpu64bitalu}
  2833. if not(cgpara.location^.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  2834. (size<>OS_F64) then
  2835. {$endif not cpu64bitalu}
  2836. cgpara.check_simple_location;
  2837. paramanager.alloccgpara(list,cgpara);
  2838. case cgpara.location^.loc of
  2839. LOC_MMREGISTER,LOC_CMMREGISTER:
  2840. a_loadmm_reg_reg(list,size,cgpara.location^.size,reg,cgpara.location^.register,shuffle);
  2841. LOC_REFERENCE,LOC_CREFERENCE:
  2842. begin
  2843. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  2844. a_loadmm_reg_ref(list,size,cgpara.location^.size,reg,href,shuffle);
  2845. end;
  2846. LOC_REGISTER,LOC_CREGISTER:
  2847. begin
  2848. if assigned(shuffle) and
  2849. not shufflescalar(shuffle) then
  2850. internalerror(2009112510);
  2851. {$ifndef cpu64bitalu}
  2852. if (size=OS_F64) then
  2853. begin
  2854. if not assigned(cgpara.location^.next) or
  2855. assigned(cgpara.location^.next^.next) then
  2856. internalerror(2009112512);
  2857. case cgpara.location^.next^.loc of
  2858. LOC_REGISTER,LOC_CREGISTER:
  2859. tmpreg:=cgpara.location^.next^.register;
  2860. LOC_REFERENCE,LOC_CREFERENCE:
  2861. tmpreg:=getintregister(list,OS_32);
  2862. else
  2863. internalerror(2009112910);
  2864. end;
  2865. if (target_info.endian=ENDIAN_BIG) then
  2866. begin
  2867. { paraloc^ -> high
  2868. paraloc^.next -> low }
  2869. reg64.reghi:=cgpara.location^.register;
  2870. reg64.reglo:=tmpreg;
  2871. end
  2872. else
  2873. begin
  2874. { paraloc^ -> low
  2875. paraloc^.next -> high }
  2876. reg64.reglo:=cgpara.location^.register;
  2877. reg64.reghi:=tmpreg;
  2878. end;
  2879. cg64.a_loadmm_reg_intreg64(list,size,reg,reg64);
  2880. if (cgpara.location^.next^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2881. begin
  2882. if not(cgpara.location^.next^.size in [OS_32,OS_S32]) then
  2883. internalerror(2009112911);
  2884. reference_reset_base(href,cgpara.location^.next^.reference.index,cgpara.location^.next^.reference.offset,cgpara.alignment);
  2885. a_load_reg_ref(list,OS_32,cgpara.location^.next^.size,tmpreg,href);
  2886. end;
  2887. end
  2888. else
  2889. {$endif not cpu64bitalu}
  2890. a_loadmm_reg_intreg(list,size,cgpara.location^.size,reg,cgpara.location^.register,mms_movescalar);
  2891. end
  2892. else
  2893. internalerror(200310123);
  2894. end;
  2895. end;
  2896. procedure tcg.a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize;const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle);
  2897. var
  2898. hr : tregister;
  2899. hs : tmmshuffle;
  2900. begin
  2901. cgpara.check_simple_location;
  2902. hr:=getmmregister(list,cgpara.location^.size);
  2903. a_loadmm_ref_reg(list,size,cgpara.location^.size,ref,hr,shuffle);
  2904. if realshuffle(shuffle) then
  2905. begin
  2906. hs:=shuffle^;
  2907. removeshuffles(hs);
  2908. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,@hs);
  2909. end
  2910. else
  2911. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,shuffle);
  2912. end;
  2913. procedure tcg.a_loadmm_loc_cgpara(list: TAsmList;const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle);
  2914. begin
  2915. case loc.loc of
  2916. LOC_MMREGISTER,LOC_CMMREGISTER:
  2917. a_loadmm_reg_cgpara(list,loc.size,loc.register,cgpara,shuffle);
  2918. LOC_REFERENCE,LOC_CREFERENCE:
  2919. a_loadmm_ref_cgpara(list,loc.size,loc.reference,cgpara,shuffle);
  2920. else
  2921. internalerror(200310123);
  2922. end;
  2923. end;
  2924. procedure tcg.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  2925. var
  2926. hr : tregister;
  2927. hs : tmmshuffle;
  2928. begin
  2929. hr:=getmmregister(list,size);
  2930. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  2931. if realshuffle(shuffle) then
  2932. begin
  2933. hs:=shuffle^;
  2934. removeshuffles(hs);
  2935. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  2936. end
  2937. else
  2938. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  2939. end;
  2940. procedure tcg.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  2941. var
  2942. hr : tregister;
  2943. hs : tmmshuffle;
  2944. begin
  2945. hr:=getmmregister(list,size);
  2946. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  2947. if realshuffle(shuffle) then
  2948. begin
  2949. hs:=shuffle^;
  2950. removeshuffles(hs);
  2951. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  2952. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  2953. end
  2954. else
  2955. begin
  2956. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  2957. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  2958. end;
  2959. end;
  2960. procedure tcg.a_loadmm_intreg_reg(list: tasmlist; fromsize,tosize: tcgsize; intreg,mmreg: tregister; shuffle: pmmshuffle);
  2961. var
  2962. tmpref: treference;
  2963. begin
  2964. if (tcgsize2size[fromsize]<>4) or
  2965. (tcgsize2size[tosize]<>4) then
  2966. internalerror(2009112503);
  2967. tg.gettemp(list,4,4,tt_normal,tmpref);
  2968. a_load_reg_ref(list,fromsize,fromsize,intreg,tmpref);
  2969. a_loadmm_ref_reg(list,tosize,tosize,tmpref,mmreg,shuffle);
  2970. tg.ungettemp(list,tmpref);
  2971. end;
  2972. procedure tcg.a_loadmm_reg_intreg(list: tasmlist; fromsize,tosize: tcgsize; mmreg,intreg: tregister; shuffle: pmmshuffle);
  2973. var
  2974. tmpref: treference;
  2975. begin
  2976. if (tcgsize2size[fromsize]<>4) or
  2977. (tcgsize2size[tosize]<>4) then
  2978. internalerror(2009112504);
  2979. tg.gettemp(list,8,8,tt_normal,tmpref);
  2980. cg.a_loadmm_reg_ref(list,fromsize,fromsize,mmreg,tmpref,shuffle);
  2981. a_load_ref_reg(list,tosize,tosize,tmpref,intreg);
  2982. tg.ungettemp(list,tmpref);
  2983. end;
  2984. procedure tcg.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  2985. begin
  2986. case loc.loc of
  2987. LOC_CMMREGISTER,LOC_MMREGISTER:
  2988. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  2989. LOC_CREFERENCE,LOC_REFERENCE:
  2990. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  2991. else
  2992. internalerror(200312232);
  2993. end;
  2994. end;
  2995. procedure tcg.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  2996. begin
  2997. g_concatcopy(list,source,dest,len);
  2998. end;
  2999. procedure tcg.g_copyshortstring(list : TAsmList;const source,dest : treference;len:byte);
  3000. var
  3001. cgpara1,cgpara2,cgpara3 : TCGPara;
  3002. begin
  3003. cgpara1.init;
  3004. cgpara2.init;
  3005. cgpara3.init;
  3006. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3007. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3008. paramanager.getintparaloc(pocall_default,3,cgpara3);
  3009. a_loadaddr_ref_cgpara(list,dest,cgpara3);
  3010. a_loadaddr_ref_cgpara(list,source,cgpara2);
  3011. a_load_const_cgpara(list,OS_INT,len,cgpara1);
  3012. paramanager.freecgpara(list,cgpara3);
  3013. paramanager.freecgpara(list,cgpara2);
  3014. paramanager.freecgpara(list,cgpara1);
  3015. allocallcpuregisters(list);
  3016. a_call_name(list,'FPC_SHORTSTR_ASSIGN',false);
  3017. deallocallcpuregisters(list);
  3018. cgpara3.done;
  3019. cgpara2.done;
  3020. cgpara1.done;
  3021. end;
  3022. procedure tcg.g_copyvariant(list : TAsmList;const source,dest : treference);
  3023. var
  3024. cgpara1,cgpara2 : TCGPara;
  3025. begin
  3026. cgpara1.init;
  3027. cgpara2.init;
  3028. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3029. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3030. a_loadaddr_ref_cgpara(list,dest,cgpara2);
  3031. a_loadaddr_ref_cgpara(list,source,cgpara1);
  3032. paramanager.freecgpara(list,cgpara2);
  3033. paramanager.freecgpara(list,cgpara1);
  3034. allocallcpuregisters(list);
  3035. a_call_name(list,'FPC_VARIANT_COPY_OVERWRITE',false);
  3036. deallocallcpuregisters(list);
  3037. cgpara2.done;
  3038. cgpara1.done;
  3039. end;
  3040. procedure tcg.g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);
  3041. var
  3042. href : treference;
  3043. incrfunc : string;
  3044. cgpara1,cgpara2 : TCGPara;
  3045. begin
  3046. cgpara1.init;
  3047. cgpara2.init;
  3048. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3049. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3050. if is_interfacecom_or_dispinterface(t) then
  3051. incrfunc:='FPC_INTF_INCR_REF'
  3052. else if is_ansistring(t) then
  3053. incrfunc:='FPC_ANSISTR_INCR_REF'
  3054. else if is_widestring(t) then
  3055. incrfunc:='FPC_WIDESTR_INCR_REF'
  3056. else if is_unicodestring(t) then
  3057. incrfunc:='FPC_UNICODESTR_INCR_REF'
  3058. else if is_dynamic_array(t) then
  3059. incrfunc:='FPC_DYNARRAY_INCR_REF'
  3060. else
  3061. incrfunc:='';
  3062. { call the special incr function or the generic addref }
  3063. if incrfunc<>'' then
  3064. begin
  3065. { widestrings aren't ref. counted on all platforms so we need the address
  3066. to create a real copy }
  3067. if is_widestring(t) then
  3068. a_loadaddr_ref_cgpara(list,ref,cgpara1)
  3069. else
  3070. { these functions get the pointer by value }
  3071. a_load_ref_cgpara(list,OS_ADDR,ref,cgpara1);
  3072. paramanager.freecgpara(list,cgpara1);
  3073. allocallcpuregisters(list);
  3074. a_call_name(list,incrfunc,false);
  3075. deallocallcpuregisters(list);
  3076. end
  3077. else
  3078. begin
  3079. if is_open_array(t) then
  3080. InternalError(201103054);
  3081. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  3082. a_loadaddr_ref_cgpara(list,href,cgpara2);
  3083. a_loadaddr_ref_cgpara(list,ref,cgpara1);
  3084. paramanager.freecgpara(list,cgpara1);
  3085. paramanager.freecgpara(list,cgpara2);
  3086. allocallcpuregisters(list);
  3087. a_call_name(list,'FPC_ADDREF',false);
  3088. deallocallcpuregisters(list);
  3089. end;
  3090. cgpara2.done;
  3091. cgpara1.done;
  3092. end;
  3093. procedure tcg.g_decrrefcount(list : TAsmList;t: tdef; const ref: treference);
  3094. var
  3095. href : treference;
  3096. decrfunc : string;
  3097. needrtti : boolean;
  3098. cgpara1,cgpara2 : TCGPara;
  3099. tempreg1,tempreg2 : TRegister;
  3100. begin
  3101. cgpara1.init;
  3102. cgpara2.init;
  3103. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3104. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3105. needrtti:=false;
  3106. if is_interfacecom_or_dispinterface(t) then
  3107. decrfunc:='FPC_INTF_DECR_REF'
  3108. else if is_ansistring(t) then
  3109. decrfunc:='FPC_ANSISTR_DECR_REF'
  3110. else if is_widestring(t) then
  3111. decrfunc:='FPC_WIDESTR_DECR_REF'
  3112. else if is_unicodestring(t) then
  3113. decrfunc:='FPC_UNICODESTR_DECR_REF'
  3114. else if is_dynamic_array(t) then
  3115. begin
  3116. decrfunc:='FPC_DYNARRAY_DECR_REF';
  3117. needrtti:=true;
  3118. end
  3119. else
  3120. decrfunc:='';
  3121. { call the special decr function or the generic decref }
  3122. if decrfunc<>'' then
  3123. begin
  3124. if needrtti then
  3125. begin
  3126. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  3127. tempreg2:=getaddressregister(list);
  3128. a_loadaddr_ref_reg(list,href,tempreg2);
  3129. end;
  3130. tempreg1:=getaddressregister(list);
  3131. a_loadaddr_ref_reg(list,ref,tempreg1);
  3132. if needrtti then
  3133. a_load_reg_cgpara(list,OS_ADDR,tempreg2,cgpara2);
  3134. a_load_reg_cgpara(list,OS_ADDR,tempreg1,cgpara1);
  3135. paramanager.freecgpara(list,cgpara1);
  3136. if needrtti then
  3137. paramanager.freecgpara(list,cgpara2);
  3138. allocallcpuregisters(list);
  3139. a_call_name(list,decrfunc,false);
  3140. deallocallcpuregisters(list);
  3141. end
  3142. else
  3143. begin
  3144. if is_open_array(t) then
  3145. InternalError(201103053);
  3146. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  3147. a_loadaddr_ref_cgpara(list,href,cgpara2);
  3148. a_loadaddr_ref_cgpara(list,ref,cgpara1);
  3149. paramanager.freecgpara(list,cgpara1);
  3150. paramanager.freecgpara(list,cgpara2);
  3151. allocallcpuregisters(list);
  3152. a_call_name(list,'FPC_DECREF',false);
  3153. deallocallcpuregisters(list);
  3154. end;
  3155. cgpara2.done;
  3156. cgpara1.done;
  3157. end;
  3158. procedure tcg.g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string);
  3159. var
  3160. cgpara1,cgpara2,cgpara3: TCGPara;
  3161. href: TReference;
  3162. hreg, lenreg: TRegister;
  3163. begin
  3164. cgpara1.init;
  3165. cgpara2.init;
  3166. cgpara3.init;
  3167. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3168. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3169. paramanager.getintparaloc(pocall_default,3,cgpara3);
  3170. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  3171. if highloc.loc=LOC_CONSTANT then
  3172. a_load_const_cgpara(list,OS_INT,highloc.value+1,cgpara3)
  3173. else
  3174. begin
  3175. if highloc.loc in [LOC_REGISTER,LOC_CREGISTER] then
  3176. hreg:=highloc.register
  3177. else
  3178. begin
  3179. hreg:=getintregister(list,OS_INT);
  3180. a_load_loc_reg(list,OS_INT,highloc,hreg);
  3181. end;
  3182. { increment, converts high(x) to length(x) }
  3183. lenreg:=getintregister(list,OS_INT);
  3184. a_op_const_reg_reg(list,OP_ADD,OS_INT,1,hreg,lenreg);
  3185. a_load_reg_cgpara(list,OS_INT,lenreg,cgpara3);
  3186. end;
  3187. a_loadaddr_ref_cgpara(list,href,cgpara2);
  3188. a_loadaddr_ref_cgpara(list,ref,cgpara1);
  3189. paramanager.freecgpara(list,cgpara1);
  3190. paramanager.freecgpara(list,cgpara2);
  3191. paramanager.freecgpara(list,cgpara3);
  3192. allocallcpuregisters(list);
  3193. a_call_name(list,name,false);
  3194. deallocallcpuregisters(list);
  3195. cgpara3.done;
  3196. cgpara2.done;
  3197. cgpara1.done;
  3198. end;
  3199. procedure tcg.g_initialize(list : TAsmList;t : tdef;const ref : treference);
  3200. var
  3201. href : treference;
  3202. cgpara1,cgpara2 : TCGPara;
  3203. begin
  3204. cgpara1.init;
  3205. cgpara2.init;
  3206. if is_ansistring(t) or
  3207. is_widestring(t) or
  3208. is_unicodestring(t) or
  3209. is_interfacecom_or_dispinterface(t) or
  3210. is_dynamic_array(t) then
  3211. a_load_const_ref(list,OS_ADDR,0,ref)
  3212. else if t.typ=variantdef then
  3213. begin
  3214. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3215. a_loadaddr_ref_cgpara(list,ref,cgpara1);
  3216. paramanager.freecgpara(list,cgpara1);
  3217. allocallcpuregisters(list);
  3218. a_call_name(list,'FPC_VARIANT_INIT',false);
  3219. deallocallcpuregisters(list);
  3220. end
  3221. else
  3222. begin
  3223. if is_open_array(t) then
  3224. InternalError(201103052);
  3225. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3226. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3227. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  3228. a_loadaddr_ref_cgpara(list,href,cgpara2);
  3229. a_loadaddr_ref_cgpara(list,ref,cgpara1);
  3230. paramanager.freecgpara(list,cgpara1);
  3231. paramanager.freecgpara(list,cgpara2);
  3232. allocallcpuregisters(list);
  3233. a_call_name(list,'FPC_INITIALIZE',false);
  3234. deallocallcpuregisters(list);
  3235. end;
  3236. cgpara1.done;
  3237. cgpara2.done;
  3238. end;
  3239. procedure tcg.g_finalize(list : TAsmList;t : tdef;const ref : treference);
  3240. var
  3241. href : treference;
  3242. cgpara1,cgpara2 : TCGPara;
  3243. begin
  3244. cgpara1.init;
  3245. cgpara2.init;
  3246. if is_ansistring(t) or
  3247. is_widestring(t) or
  3248. is_unicodestring(t) or
  3249. is_interfacecom_or_dispinterface(t) then
  3250. begin
  3251. g_decrrefcount(list,t,ref);
  3252. a_load_const_ref(list,OS_ADDR,0,ref);
  3253. end
  3254. else if t.typ=variantdef then
  3255. begin
  3256. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3257. a_loadaddr_ref_cgpara(list,ref,cgpara1);
  3258. paramanager.freecgpara(list,cgpara1);
  3259. allocallcpuregisters(list);
  3260. a_call_name(list,'FPC_VARIANT_CLEAR',false);
  3261. deallocallcpuregisters(list);
  3262. end
  3263. else
  3264. begin
  3265. if is_open_array(t) then
  3266. InternalError(201103051);
  3267. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3268. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3269. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  3270. a_loadaddr_ref_cgpara(list,href,cgpara2);
  3271. a_loadaddr_ref_cgpara(list,ref,cgpara1);
  3272. paramanager.freecgpara(list,cgpara1);
  3273. paramanager.freecgpara(list,cgpara2);
  3274. allocallcpuregisters(list);
  3275. a_call_name(list,'FPC_FINALIZE',false);
  3276. deallocallcpuregisters(list);
  3277. end;
  3278. cgpara1.done;
  3279. cgpara2.done;
  3280. end;
  3281. procedure tcg.g_rangecheck(list: TAsmList; const l:tlocation;fromdef,todef: tdef);
  3282. { generate range checking code for the value at location p. The type }
  3283. { type used is checked against todefs ranges. fromdef (p.resultdef) }
  3284. { is the original type used at that location. When both defs are equal }
  3285. { the check is also insert (needed for succ,pref,inc,dec) }
  3286. const
  3287. aintmax=high(aint);
  3288. var
  3289. neglabel : tasmlabel;
  3290. hreg : tregister;
  3291. lto,hto,
  3292. lfrom,hfrom : TConstExprInt;
  3293. fromsize, tosize: cardinal;
  3294. from_signed, to_signed: boolean;
  3295. begin
  3296. { range checking on and range checkable value? }
  3297. if not(cs_check_range in current_settings.localswitches) or
  3298. not(fromdef.typ in [orddef,enumdef]) or
  3299. { C-style booleans can't really fail range checks, }
  3300. { all values are always valid }
  3301. is_cbool(todef) then
  3302. exit;
  3303. {$ifndef cpu64bitalu}
  3304. { handle 64bit rangechecks separate for 32bit processors }
  3305. if is_64bit(fromdef) or is_64bit(todef) then
  3306. begin
  3307. cg64.g_rangecheck64(list,l,fromdef,todef);
  3308. exit;
  3309. end;
  3310. {$endif cpu64bitalu}
  3311. { only check when assigning to scalar, subranges are different, }
  3312. { when todef=fromdef then the check is always generated }
  3313. getrange(fromdef,lfrom,hfrom);
  3314. getrange(todef,lto,hto);
  3315. from_signed := is_signed(fromdef);
  3316. to_signed := is_signed(todef);
  3317. { check the rangedef of the array, not the array itself }
  3318. { (only change now, since getrange needs the arraydef) }
  3319. if (todef.typ = arraydef) then
  3320. todef := tarraydef(todef).rangedef;
  3321. { no range check if from and to are equal and are both longint/dword }
  3322. { (if we have a 32bit processor) or int64/qword, since such }
  3323. { operations can at most cause overflows (JM) }
  3324. { Note that these checks are mostly processor independent, they only }
  3325. { have to be changed once we introduce 64bit subrange types }
  3326. {$ifdef cpu64bitalu}
  3327. if (fromdef = todef) and
  3328. (fromdef.typ=orddef) and
  3329. (((((torddef(fromdef).ordtype = s64bit) and
  3330. (lfrom = low(int64)) and
  3331. (hfrom = high(int64))) or
  3332. ((torddef(fromdef).ordtype = u64bit) and
  3333. (lfrom = low(qword)) and
  3334. (hfrom = high(qword))) or
  3335. ((torddef(fromdef).ordtype = scurrency) and
  3336. (lfrom = low(int64)) and
  3337. (hfrom = high(int64)))))) then
  3338. exit;
  3339. {$else cpu64bitalu}
  3340. if (fromdef = todef) and
  3341. (fromdef.typ=orddef) and
  3342. (((((torddef(fromdef).ordtype = s32bit) and
  3343. (lfrom = int64(low(longint))) and
  3344. (hfrom = int64(high(longint)))) or
  3345. ((torddef(fromdef).ordtype = u32bit) and
  3346. (lfrom = low(cardinal)) and
  3347. (hfrom = high(cardinal)))))) then
  3348. exit;
  3349. {$endif cpu64bitalu}
  3350. { optimize some range checks away in safe cases }
  3351. fromsize := fromdef.size;
  3352. tosize := todef.size;
  3353. if ((from_signed = to_signed) or
  3354. (not from_signed)) and
  3355. (lto<=lfrom) and (hto>=hfrom) and
  3356. (fromsize <= tosize) then
  3357. begin
  3358. { if fromsize < tosize, and both have the same signed-ness or }
  3359. { fromdef is unsigned, then all bit patterns from fromdef are }
  3360. { valid for todef as well }
  3361. if (fromsize < tosize) then
  3362. exit;
  3363. if (fromsize = tosize) and
  3364. (from_signed = to_signed) then
  3365. { only optimize away if all bit patterns which fit in fromsize }
  3366. { are valid for the todef }
  3367. begin
  3368. {$push}
  3369. {$Q-}
  3370. {$R-}
  3371. if to_signed then
  3372. begin
  3373. { calculation of the low/high ranges must not overflow 64 bit
  3374. otherwise we end up comparing with zero for 64 bit data types on
  3375. 64 bit processors }
  3376. if (lto = (int64(-1) << (tosize * 8 - 1))) and
  3377. (hto = (-((int64(-1) << (tosize * 8 - 1))+1))) then
  3378. exit
  3379. end
  3380. else
  3381. begin
  3382. { calculation of the low/high ranges must not overflow 64 bit
  3383. otherwise we end up having all zeros for 64 bit data types on
  3384. 64 bit processors }
  3385. if (lto = 0) and
  3386. (qword(hto) = (qword(-1) >> (64-(tosize * 8))) ) then
  3387. exit
  3388. end;
  3389. {$pop}
  3390. end
  3391. end;
  3392. { generate the rangecheck code for the def where we are going to }
  3393. { store the result }
  3394. { use the trick that }
  3395. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  3396. { To be able to do that, we have to make sure however that either }
  3397. { fromdef and todef are both signed or unsigned, or that we leave }
  3398. { the parts < 0 and > maxlongint out }
  3399. if from_signed xor to_signed then
  3400. begin
  3401. if from_signed then
  3402. { from is signed, to is unsigned }
  3403. begin
  3404. { if high(from) < 0 -> always range error }
  3405. if (hfrom < 0) or
  3406. { if low(to) > maxlongint also range error }
  3407. (lto > aintmax) then
  3408. begin
  3409. a_call_name(list,'FPC_RANGEERROR',false);
  3410. exit
  3411. end;
  3412. { from is signed and to is unsigned -> when looking at to }
  3413. { as an signed value, it must be < maxaint (otherwise }
  3414. { it will become negative, which is invalid since "to" is unsigned) }
  3415. if hto > aintmax then
  3416. hto := aintmax;
  3417. end
  3418. else
  3419. { from is unsigned, to is signed }
  3420. begin
  3421. if (lfrom > aintmax) or
  3422. (hto < 0) then
  3423. begin
  3424. a_call_name(list,'FPC_RANGEERROR',false);
  3425. exit
  3426. end;
  3427. { from is unsigned and to is signed -> when looking at to }
  3428. { as an unsigned value, it must be >= 0 (since negative }
  3429. { values are the same as values > maxlongint) }
  3430. if lto < 0 then
  3431. lto := 0;
  3432. end;
  3433. end;
  3434. hreg:=getintregister(list,OS_INT);
  3435. a_load_loc_reg(list,OS_INT,l,hreg);
  3436. a_op_const_reg(list,OP_SUB,OS_INT,tcgint(int64(lto)),hreg);
  3437. current_asmdata.getjumplabel(neglabel);
  3438. {
  3439. if from_signed then
  3440. a_cmp_const_reg_label(list,OS_INT,OC_GTE,aint(hto-lto),hreg,neglabel)
  3441. else
  3442. }
  3443. {$ifdef cpu64bitalu}
  3444. if qword(hto-lto)>qword(aintmax) then
  3445. a_cmp_const_reg_label(list,OS_INT,OC_BE,aintmax,hreg,neglabel)
  3446. else
  3447. {$endif cpu64bitalu}
  3448. a_cmp_const_reg_label(list,OS_INT,OC_BE,tcgint(int64(hto-lto)),hreg,neglabel);
  3449. a_call_name(list,'FPC_RANGEERROR',false);
  3450. a_label(list,neglabel);
  3451. end;
  3452. procedure tcg.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  3453. begin
  3454. g_overflowCheck(list,loc,def);
  3455. end;
  3456. {$ifdef cpuflags}
  3457. procedure tcg.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference);
  3458. var
  3459. tmpreg : tregister;
  3460. begin
  3461. tmpreg:=getintregister(list,size);
  3462. g_flags2reg(list,size,f,tmpreg);
  3463. a_load_reg_ref(list,size,size,tmpreg,ref);
  3464. end;
  3465. {$endif cpuflags}
  3466. procedure tcg.g_maybe_testself(list : TAsmList;reg:tregister);
  3467. var
  3468. OKLabel : tasmlabel;
  3469. cgpara1 : TCGPara;
  3470. begin
  3471. if (cs_check_object in current_settings.localswitches) or
  3472. (cs_check_range in current_settings.localswitches) then
  3473. begin
  3474. current_asmdata.getjumplabel(oklabel);
  3475. a_cmp_const_reg_label(list,OS_ADDR,OC_NE,0,reg,oklabel);
  3476. cgpara1.init;
  3477. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3478. a_load_const_cgpara(list,OS_INT,tcgint(210),cgpara1);
  3479. paramanager.freecgpara(list,cgpara1);
  3480. a_call_name(list,'FPC_HANDLEERROR',false);
  3481. a_label(list,oklabel);
  3482. cgpara1.done;
  3483. end;
  3484. end;
  3485. procedure tcg.g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  3486. var
  3487. hrefvmt : treference;
  3488. cgpara1,cgpara2 : TCGPara;
  3489. begin
  3490. cgpara1.init;
  3491. cgpara2.init;
  3492. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3493. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3494. if (cs_check_object in current_settings.localswitches) then
  3495. begin
  3496. reference_reset_symbol(hrefvmt,current_asmdata.RefAsmSymbol(objdef.vmt_mangledname),0,sizeof(pint));
  3497. a_loadaddr_ref_cgpara(list,hrefvmt,cgpara2);
  3498. a_load_reg_cgpara(list,OS_ADDR,reg,cgpara1);
  3499. paramanager.freecgpara(list,cgpara1);
  3500. paramanager.freecgpara(list,cgpara2);
  3501. allocallcpuregisters(list);
  3502. a_call_name(list,'FPC_CHECK_OBJECT_EXT',false);
  3503. deallocallcpuregisters(list);
  3504. end
  3505. else
  3506. if (cs_check_range in current_settings.localswitches) then
  3507. begin
  3508. a_load_reg_cgpara(list,OS_ADDR,reg,cgpara1);
  3509. paramanager.freecgpara(list,cgpara1);
  3510. allocallcpuregisters(list);
  3511. a_call_name(list,'FPC_CHECK_OBJECT',false);
  3512. deallocallcpuregisters(list);
  3513. end;
  3514. cgpara1.done;
  3515. cgpara2.done;
  3516. end;
  3517. {*****************************************************************************
  3518. Entry/Exit Code Functions
  3519. *****************************************************************************}
  3520. procedure tcg.g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:tcgint;destreg:tregister);
  3521. var
  3522. sizereg,sourcereg,lenreg : tregister;
  3523. cgpara1,cgpara2,cgpara3 : TCGPara;
  3524. begin
  3525. { because some abis don't support dynamic stack allocation properly
  3526. open array value parameters are copied onto the heap
  3527. }
  3528. { calculate necessary memory }
  3529. { read/write operations on one register make the life of the register allocator hard }
  3530. if not(lenloc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  3531. begin
  3532. lenreg:=getintregister(list,OS_INT);
  3533. a_load_loc_reg(list,OS_INT,lenloc,lenreg);
  3534. end
  3535. else
  3536. lenreg:=lenloc.register;
  3537. sizereg:=getintregister(list,OS_INT);
  3538. a_op_const_reg_reg(list,OP_ADD,OS_INT,1,lenreg,sizereg);
  3539. a_op_const_reg(list,OP_IMUL,OS_INT,elesize,sizereg);
  3540. { load source }
  3541. sourcereg:=getaddressregister(list);
  3542. a_loadaddr_ref_reg(list,ref,sourcereg);
  3543. { do getmem call }
  3544. cgpara1.init;
  3545. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3546. a_load_reg_cgpara(list,OS_INT,sizereg,cgpara1);
  3547. paramanager.freecgpara(list,cgpara1);
  3548. allocallcpuregisters(list);
  3549. a_call_name(list,'FPC_GETMEM',false);
  3550. deallocallcpuregisters(list);
  3551. cgpara1.done;
  3552. { return the new address }
  3553. a_load_reg_reg(list,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,destreg);
  3554. { do move call }
  3555. cgpara1.init;
  3556. cgpara2.init;
  3557. cgpara3.init;
  3558. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3559. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3560. paramanager.getintparaloc(pocall_default,3,cgpara3);
  3561. { load size }
  3562. a_load_reg_cgpara(list,OS_INT,sizereg,cgpara3);
  3563. { load destination }
  3564. a_load_reg_cgpara(list,OS_ADDR,destreg,cgpara2);
  3565. { load source }
  3566. a_load_reg_cgpara(list,OS_ADDR,sourcereg,cgpara1);
  3567. paramanager.freecgpara(list,cgpara3);
  3568. paramanager.freecgpara(list,cgpara2);
  3569. paramanager.freecgpara(list,cgpara1);
  3570. allocallcpuregisters(list);
  3571. a_call_name(list,'FPC_MOVE',false);
  3572. deallocallcpuregisters(list);
  3573. cgpara3.done;
  3574. cgpara2.done;
  3575. cgpara1.done;
  3576. end;
  3577. procedure tcg.g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);
  3578. var
  3579. cgpara1 : TCGPara;
  3580. begin
  3581. { do move call }
  3582. cgpara1.init;
  3583. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3584. { load source }
  3585. a_load_loc_cgpara(list,l,cgpara1);
  3586. paramanager.freecgpara(list,cgpara1);
  3587. allocallcpuregisters(list);
  3588. a_call_name(list,'FPC_FREEMEM',false);
  3589. deallocallcpuregisters(list);
  3590. cgpara1.done;
  3591. end;
  3592. procedure tcg.g_save_registers(list:TAsmList);
  3593. var
  3594. href : treference;
  3595. size : longint;
  3596. r : integer;
  3597. begin
  3598. { calculate temp. size }
  3599. size:=0;
  3600. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  3601. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  3602. inc(size,sizeof(aint));
  3603. { mm registers }
  3604. if uses_registers(R_MMREGISTER) then
  3605. begin
  3606. { Make sure we reserve enough space to do the alignment based on the offset
  3607. later on. We can't use the size for this, because the alignment of the start
  3608. of the temp is smaller than needed for an OS_VECTOR }
  3609. inc(size,tcgsize2size[OS_VECTOR]);
  3610. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  3611. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  3612. inc(size,tcgsize2size[OS_VECTOR]);
  3613. end;
  3614. if size>0 then
  3615. begin
  3616. tg.GetTemp(list,size,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  3617. include(current_procinfo.flags,pi_has_saved_regs);
  3618. { Copy registers to temp }
  3619. href:=current_procinfo.save_regs_ref;
  3620. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  3621. begin
  3622. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  3623. begin
  3624. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE),href);
  3625. inc(href.offset,sizeof(aint));
  3626. end;
  3627. include(rg[R_INTREGISTER].preserved_by_proc,saved_standard_registers[r]);
  3628. end;
  3629. if uses_registers(R_MMREGISTER) then
  3630. begin
  3631. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  3632. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  3633. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  3634. begin
  3635. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  3636. begin
  3637. a_loadmm_reg_ref(list,OS_VECTOR,OS_VECTOR,newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBNONE),href,nil);
  3638. inc(href.offset,tcgsize2size[OS_VECTOR]);
  3639. end;
  3640. include(rg[R_MMREGISTER].preserved_by_proc,saved_mm_registers[r]);
  3641. end;
  3642. end;
  3643. end;
  3644. end;
  3645. procedure tcg.g_restore_registers(list:TAsmList);
  3646. var
  3647. href : treference;
  3648. r : integer;
  3649. hreg : tregister;
  3650. begin
  3651. if not(pi_has_saved_regs in current_procinfo.flags) then
  3652. exit;
  3653. { Copy registers from temp }
  3654. href:=current_procinfo.save_regs_ref;
  3655. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  3656. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  3657. begin
  3658. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  3659. { Allocate register so the optimizer does not remove the load }
  3660. a_reg_alloc(list,hreg);
  3661. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  3662. inc(href.offset,sizeof(aint));
  3663. end;
  3664. if uses_registers(R_MMREGISTER) then
  3665. begin
  3666. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  3667. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  3668. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  3669. begin
  3670. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  3671. begin
  3672. hreg:=newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBNONE);
  3673. { Allocate register so the optimizer does not remove the load }
  3674. a_reg_alloc(list,hreg);
  3675. a_loadmm_ref_reg(list,OS_VECTOR,OS_VECTOR,href,hreg,nil);
  3676. inc(href.offset,tcgsize2size[OS_VECTOR]);
  3677. end;
  3678. end;
  3679. end;
  3680. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  3681. end;
  3682. procedure tcg.g_profilecode(list : TAsmList);
  3683. begin
  3684. end;
  3685. procedure tcg.g_exception_reason_save(list : TAsmList; const href : treference);
  3686. begin
  3687. a_load_reg_ref(list, OS_INT, OS_INT, NR_FUNCTION_RESULT_REG, href);
  3688. end;
  3689. procedure tcg.g_exception_reason_save_const(list : TAsmList; const href : treference; a: tcgint);
  3690. begin
  3691. a_load_const_ref(list, OS_INT, a, href);
  3692. end;
  3693. procedure tcg.g_exception_reason_load(list : TAsmList; const href : treference);
  3694. begin
  3695. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  3696. a_load_ref_reg(list, OS_INT, OS_INT, href, NR_FUNCTION_RESULT_REG);
  3697. end;
  3698. procedure tcg.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  3699. var
  3700. hsym : tsym;
  3701. href : treference;
  3702. paraloc : Pcgparalocation;
  3703. begin
  3704. { calculate the parameter info for the procdef }
  3705. procdef.init_paraloc_info(callerside);
  3706. hsym:=tsym(procdef.parast.Find('self'));
  3707. if not(assigned(hsym) and
  3708. (hsym.typ=paravarsym)) then
  3709. internalerror(200305251);
  3710. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  3711. while paraloc<>nil do
  3712. with paraloc^ do
  3713. begin
  3714. case loc of
  3715. LOC_REGISTER:
  3716. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  3717. LOC_REFERENCE:
  3718. begin
  3719. { offset in the wrapper needs to be adjusted for the stored
  3720. return address }
  3721. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),sizeof(pint));
  3722. a_op_const_ref(list,OP_SUB,size,ioffset,href);
  3723. end
  3724. else
  3725. internalerror(200309189);
  3726. end;
  3727. paraloc:=next;
  3728. end;
  3729. end;
  3730. procedure tcg.g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string);
  3731. begin
  3732. a_jmp_name(list,externalname);
  3733. end;
  3734. procedure tcg.a_call_name_static(list : TAsmList;const s : string);
  3735. begin
  3736. a_call_name(list,s,false);
  3737. end;
  3738. procedure tcg.a_call_ref(list : TAsmList;ref: treference);
  3739. var
  3740. tempreg : TRegister;
  3741. begin
  3742. tempreg := getintregister(list, OS_ADDR);
  3743. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,tempreg);
  3744. a_call_reg(list,tempreg);
  3745. end;
  3746. function tcg.g_indirect_sym_load(list:TAsmList;const symname: string; weak: boolean): tregister;
  3747. var
  3748. l: tasmsymbol;
  3749. ref: treference;
  3750. nlsymname: string;
  3751. begin
  3752. result := NR_NO;
  3753. case target_info.system of
  3754. system_powerpc_darwin,
  3755. system_i386_darwin,
  3756. system_i386_iphonesim,
  3757. system_powerpc64_darwin,
  3758. system_arm_darwin:
  3759. begin
  3760. nlsymname:='L'+symname+'$non_lazy_ptr';
  3761. l:=current_asmdata.getasmsymbol(nlsymname);
  3762. if not(assigned(l)) then
  3763. begin
  3764. new_section(current_asmdata.asmlists[al_picdata],sec_data_nonlazy,'',sizeof(pint));
  3765. l:=current_asmdata.DefineAsmSymbol(nlsymname,AB_LOCAL,AT_DATA);
  3766. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create(l,0));
  3767. if not(weak) then
  3768. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.RefAsmSymbol(symname).Name))
  3769. else
  3770. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.WeakRefAsmSymbol(symname).Name));
  3771. {$ifdef cpu64bitaddr}
  3772. current_asmdata.asmlists[al_picdata].concat(tai_const.create_64bit(0));
  3773. {$else cpu64bitaddr}
  3774. current_asmdata.asmlists[al_picdata].concat(tai_const.create_32bit(0));
  3775. {$endif cpu64bitaddr}
  3776. end;
  3777. result := getaddressregister(list);
  3778. reference_reset_symbol(ref,l,0,sizeof(pint));
  3779. { a_load_ref_reg will turn this into a pic-load if needed }
  3780. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,result);
  3781. end;
  3782. end;
  3783. end;
  3784. procedure tcg.g_maybe_got_init(list: TAsmList);
  3785. begin
  3786. end;
  3787. procedure tcg.g_call(list: TAsmList;const s: string);
  3788. begin
  3789. allocallcpuregisters(list);
  3790. a_call_name(list,s,false);
  3791. deallocallcpuregisters(list);
  3792. end;
  3793. procedure tcg.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  3794. begin
  3795. internalerror(200807231);
  3796. end;
  3797. procedure tcg.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  3798. begin
  3799. internalerror(200807232);
  3800. end;
  3801. procedure tcg.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  3802. begin
  3803. internalerror(200807233);
  3804. end;
  3805. procedure tcg.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  3806. begin
  3807. internalerror(200807234);
  3808. end;
  3809. function tcg.getflagregister(list: TAsmList; size: Tcgsize): Tregister;
  3810. begin
  3811. Result:=TRegister(0);
  3812. internalerror(200807238);
  3813. end;
  3814. {*****************************************************************************
  3815. TCG64
  3816. *****************************************************************************}
  3817. {$ifndef cpu64bitalu}
  3818. procedure tcg64.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64; regsrc,regdst : tregister64);
  3819. begin
  3820. a_load64_reg_reg(list,regsrc,regdst);
  3821. a_op64_const_reg(list,op,size,value,regdst);
  3822. end;
  3823. procedure tcg64.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  3824. var
  3825. tmpreg64 : tregister64;
  3826. begin
  3827. { when src1=dst then we need to first create a temp to prevent
  3828. overwriting src1 with src2 }
  3829. if (regsrc1.reghi=regdst.reghi) or
  3830. (regsrc1.reglo=regdst.reghi) or
  3831. (regsrc1.reghi=regdst.reglo) or
  3832. (regsrc1.reglo=regdst.reglo) then
  3833. begin
  3834. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  3835. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  3836. a_load64_reg_reg(list,regsrc2,tmpreg64);
  3837. a_op64_reg_reg(list,op,size,regsrc1,tmpreg64);
  3838. a_load64_reg_reg(list,tmpreg64,regdst);
  3839. end
  3840. else
  3841. begin
  3842. a_load64_reg_reg(list,regsrc2,regdst);
  3843. a_op64_reg_reg(list,op,size,regsrc1,regdst);
  3844. end;
  3845. end;
  3846. procedure tcg64.a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  3847. var
  3848. tmpreg64 : tregister64;
  3849. begin
  3850. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  3851. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  3852. a_load64_subsetref_reg(list,sref,tmpreg64);
  3853. a_op64_const_reg(list,op,size,a,tmpreg64);
  3854. a_load64_reg_subsetref(list,tmpreg64,sref);
  3855. end;
  3856. procedure tcg64.a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  3857. var
  3858. tmpreg64 : tregister64;
  3859. begin
  3860. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  3861. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  3862. a_load64_subsetref_reg(list,sref,tmpreg64);
  3863. a_op64_reg_reg(list,op,size,reg,tmpreg64);
  3864. a_load64_reg_subsetref(list,tmpreg64,sref);
  3865. end;
  3866. procedure tcg64.a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  3867. var
  3868. tmpreg64 : tregister64;
  3869. begin
  3870. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  3871. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  3872. a_load64_subsetref_reg(list,sref,tmpreg64);
  3873. a_op64_ref_reg(list,op,size,ref,tmpreg64);
  3874. a_load64_reg_subsetref(list,tmpreg64,sref);
  3875. end;
  3876. procedure tcg64.a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  3877. var
  3878. tmpreg64 : tregister64;
  3879. begin
  3880. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  3881. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  3882. a_load64_subsetref_reg(list,ssref,tmpreg64);
  3883. a_op64_reg_subsetref(list,op,size,tmpreg64,dsref);
  3884. end;
  3885. procedure tcg64.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  3886. begin
  3887. a_op64_const_reg_reg(list,op,size,value,regsrc,regdst);
  3888. ovloc.loc:=LOC_VOID;
  3889. end;
  3890. procedure tcg64.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  3891. begin
  3892. a_op64_reg_reg_reg(list,op,size,regsrc1,regsrc2,regdst);
  3893. ovloc.loc:=LOC_VOID;
  3894. end;
  3895. procedure tcg64.a_load64_loc_subsetref(list : TAsmList;const l: tlocation; const sref : tsubsetreference);
  3896. begin
  3897. case l.loc of
  3898. LOC_REFERENCE, LOC_CREFERENCE:
  3899. a_load64_ref_subsetref(list,l.reference,sref);
  3900. LOC_REGISTER,LOC_CREGISTER:
  3901. a_load64_reg_subsetref(list,l.register64,sref);
  3902. LOC_CONSTANT :
  3903. a_load64_const_subsetref(list,l.value64,sref);
  3904. LOC_SUBSETREF,LOC_CSUBSETREF:
  3905. a_load64_subsetref_subsetref(list,l.sref,sref);
  3906. else
  3907. internalerror(2006082210);
  3908. end;
  3909. end;
  3910. procedure tcg64.a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  3911. begin
  3912. case l.loc of
  3913. LOC_REFERENCE, LOC_CREFERENCE:
  3914. a_load64_subsetref_ref(list,sref,l.reference);
  3915. LOC_REGISTER,LOC_CREGISTER:
  3916. a_load64_subsetref_reg(list,sref,l.register64);
  3917. LOC_SUBSETREF,LOC_CSUBSETREF:
  3918. a_load64_subsetref_subsetref(list,sref,l.sref);
  3919. else
  3920. internalerror(2006082211);
  3921. end;
  3922. end;
  3923. {$endif cpu64bitalu}
  3924. procedure destroy_codegen;
  3925. begin
  3926. cg.free;
  3927. cg:=nil;
  3928. {$ifndef cpu64bitalu}
  3929. cg64.free;
  3930. cg64:=nil;
  3931. {$endif cpu64bitalu}
  3932. end;
  3933. end.