cgobj.pas 168 KB

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