cgobj.pas 190 KB

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