cgobj.pas 187 KB

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