cgobj.pas 191 KB

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