hlcgobj.pas 174 KB

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