cgobj.pas 183 KB

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