cgobj.pas 171 KB

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