cgobj.pas 191 KB

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