cgobj.pas 164 KB

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