cgobj.pas 187 KB

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