hlcgobj.pas 183 KB

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