cgobj.pas 190 KB

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