cgobj.pas 165 KB

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