cgobj.pas 167 KB

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