cgobj.pas 135 KB

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