cgobj.pas 171 KB

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