cgobj.pas 189 KB

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