cgobj.pas 190 KB

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