cgobj.pas 184 KB

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