cgobj.pas 150 KB

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