hlcgobj.pas 172 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030
  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. { generate a stub which only purpose is to pass control the given external method,
  373. setting up any additional environment before doing so (if required).
  374. The default implementation issues a jump instruction to the external name. }
  375. // procedure g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string); virtual;
  376. protected
  377. procedure g_allocload_reg_reg(list: TAsmList; regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  378. public
  379. { create "safe copy" of a tlocation that can be used later: all
  380. registers used in the tlocation are copied to new ones, so that
  381. even if the original ones change, things stay the same (except if
  382. the original location was already a register, then the register is
  383. kept). Must only be used on lvalue locations.
  384. It's intended as some kind of replacement for a_loadaddr_ref_reg()
  385. for targets without pointers. }
  386. procedure g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation); virtual;
  387. { routines migrated from ncgutil }
  388. procedure location_force_reg(list:TAsmList;var l:tlocation;src_size,dst_size:tdef;maybeconst:boolean);virtual;
  389. procedure location_force_fpureg(list:TAsmList;var l: tlocation;size: tdef;maybeconst:boolean);virtual;
  390. procedure location_force_mem(list:TAsmList;var l:tlocation;size:tdef);virtual;
  391. // procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;abstract;
  392. // procedure location_force_mmreg(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;abstract;
  393. { Retrieve the location of the data pointed to in location l, when the location is
  394. a register it is expected to contain the address of the data }
  395. procedure location_get_data_ref(list:TAsmList;def: tdef; const l:tlocation;var ref:treference;loadref:boolean; alignment: longint);virtual;
  396. procedure maketojumpbool(list:TAsmList; p : tnode);virtual;
  397. procedure gen_proc_symbol(list:TAsmList);virtual;
  398. procedure gen_proc_symbol_end(list:TAsmList);virtual;
  399. procedure gen_initialize_code(list:TAsmList);virtual;
  400. procedure gen_finalize_code(list:TAsmList);virtual;
  401. procedure gen_entry_code(list:TAsmList);virtual;
  402. procedure gen_exit_code(list:TAsmList);virtual;
  403. protected
  404. { helpers called by gen_initialize_code/gen_finalize_code }
  405. procedure inittempvariables(list:TAsmList);virtual;
  406. procedure initialize_data(p:TObject;arg:pointer);virtual;
  407. procedure finalizetempvariables(list:TAsmList);virtual;
  408. procedure initialize_regvars(p:TObject;arg:pointer);virtual;
  409. procedure finalize_sym(asmlist:TAsmList;sym:tsym);virtual;
  410. { generates the code for finalisation of local variables }
  411. procedure finalize_local_vars(p:TObject;arg:pointer);virtual;
  412. { generates the code for finalization of static symtable and
  413. all local (static) typed consts }
  414. procedure finalize_static_data(p:TObject;arg:pointer);virtual;
  415. { generates the code for decrementing the reference count of parameters }
  416. procedure final_paras(p:TObject;arg:pointer);
  417. public
  418. procedure gen_load_para_value(list:TAsmList);virtual;
  419. protected
  420. { helpers called by gen_load_para_value }
  421. procedure g_copyvalueparas(p:TObject;arg:pointer);virtual;
  422. procedure gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation;const cgpara: tcgpara;locintsize: longint);virtual;
  423. procedure init_paras(p:TObject;arg:pointer);
  424. protected
  425. { Some targets have to put "something" in the function result
  426. location if it's not initialised by the Pascal code, e.g.
  427. stack-based architectures. By default it does nothing }
  428. procedure gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);virtual;
  429. public
  430. { load a tlocation into a cgpara }
  431. procedure gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);virtual;
  432. { load a cgpara into a tlocation }
  433. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);virtual;
  434. { load the function return value into the ABI-defined function return location }
  435. procedure gen_load_return_value(list:TAsmList);virtual;
  436. { extras refactored from other units }
  437. { queue the code/data generated for a procedure for writing out to
  438. the assembler/object file }
  439. procedure record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList); virtual;
  440. { generate a call to a routine in the system unit }
  441. procedure g_call_system_proc(list: TAsmList; const procname: string);
  442. { Generate code to exit an unwind-protected region. The default implementation
  443. produces a simple jump to destination label. }
  444. procedure g_local_unwind(list: TAsmList; l: TAsmLabel);virtual;abstract;
  445. end;
  446. var
  447. {# Main high level code generator class }
  448. hlcg : thlcgobj;
  449. procedure destroy_hlcodegen;
  450. implementation
  451. uses
  452. globals,options,systems,
  453. fmodule,export,
  454. verbose,defutil,paramgr,
  455. symbase,symsym,symtable,
  456. ncon,nld,pass_1,pass_2,
  457. cpuinfo,cgobj,tgobj,cutils,procinfo,
  458. ncgutil,ngenutil;
  459. procedure destroy_hlcodegen;
  460. begin
  461. hlcg.free;
  462. hlcg:=nil;
  463. destroy_codegen;
  464. end;
  465. { thlcgobj }
  466. constructor thlcgobj.create;
  467. begin
  468. end;
  469. procedure thlcgobj.init_register_allocators;
  470. begin
  471. cg.init_register_allocators;
  472. end;
  473. procedure thlcgobj.done_register_allocators;
  474. begin
  475. cg.done_register_allocators;
  476. end;
  477. procedure thlcgobj.set_regalloc_live_range_direction(dir: TRADirection);
  478. begin
  479. cg.set_regalloc_live_range_direction(dir);
  480. end;
  481. function thlcgobj.getintregister(list: TAsmList; size: tdef): Tregister;
  482. begin
  483. result:=cg.getintregister(list,def_cgsize(size));
  484. end;
  485. function thlcgobj.getaddressregister(list: TAsmList; size: tdef): Tregister;
  486. begin
  487. result:=cg.getaddressregister(list);
  488. end;
  489. function thlcgobj.getfpuregister(list: TAsmList; size: tdef): Tregister;
  490. begin
  491. result:=cg.getfpuregister(list,def_cgsize(size));
  492. end;
  493. (*
  494. function thlcgobj.getmmregister(list: TAsmList; size: tdef): Tregister;
  495. begin
  496. result:=cg.getmmregister(list,def_cgsize(size));
  497. end;
  498. *)
  499. function thlcgobj.getflagregister(list: TAsmList; size: tdef): Tregister;
  500. begin
  501. result:=cg.getflagregister(list,def_cgsize(size));
  502. end;
  503. function thlcgobj.getregisterfordef(list: TAsmList; size: tdef): Tregister;
  504. begin
  505. case def2regtyp(size) of
  506. R_INTREGISTER:
  507. result:=getintregister(list,size);
  508. R_ADDRESSREGISTER:
  509. result:=getaddressregister(list,size);
  510. R_FPUREGISTER:
  511. result:=getfpuregister(list,size);
  512. (*
  513. R_MMREGISTER:
  514. result:=getmmregister(list,size);
  515. *)
  516. else
  517. internalerror(2010122901);
  518. end;
  519. end;
  520. function thlcgobj.uses_registers(rt: Tregistertype): boolean;
  521. begin
  522. result:=cg.uses_registers(rt);
  523. end;
  524. procedure thlcgobj.do_register_allocation(list: TAsmList; headertai: tai);
  525. begin
  526. cg.do_register_allocation(list,headertai);
  527. end;
  528. procedure thlcgobj.translate_register(var reg: tregister);
  529. begin
  530. cg.translate_register(reg);
  531. end;
  532. function thlcgobj.def2regtyp(def: tdef): tregistertype;
  533. begin
  534. case def.typ of
  535. enumdef,
  536. orddef,
  537. recorddef,
  538. setdef:
  539. result:=R_INTREGISTER;
  540. stringdef,
  541. pointerdef,
  542. classrefdef,
  543. objectdef,
  544. procvardef,
  545. procdef,
  546. arraydef,
  547. formaldef:
  548. result:=R_ADDRESSREGISTER;
  549. floatdef:
  550. if use_vectorfpu(def) then
  551. result:=R_MMREGISTER
  552. else if cs_fp_emulation in current_settings.moduleswitches then
  553. result:=R_INTREGISTER
  554. else
  555. result:=R_FPUREGISTER;
  556. filedef,
  557. variantdef:
  558. internalerror(2010120507);
  559. else
  560. internalerror(2010120506);
  561. end;
  562. end;
  563. class function thlcgobj.tcgsize2orddef(size: tcgsize): torddef;
  564. begin
  565. case size of
  566. OS_8:
  567. result:=torddef(u8inttype);
  568. OS_S8:
  569. result:=torddef(s8inttype);
  570. OS_16:
  571. result:=torddef(u16inttype);
  572. OS_S16:
  573. result:=torddef(s16inttype);
  574. OS_32:
  575. result:=torddef(u32inttype);
  576. OS_S32:
  577. result:=torddef(s32inttype);
  578. OS_64:
  579. result:=torddef(u64inttype);
  580. OS_S64:
  581. result:=torddef(s64inttype);
  582. else
  583. internalerror(2012050401);
  584. end;
  585. end;
  586. procedure thlcgobj.a_label(list: TAsmList; l: tasmlabel); inline;
  587. begin
  588. cg.a_label(list,l);
  589. end;
  590. procedure thlcgobj.a_reg_alloc(list: TAsmList; r: tregister);
  591. begin
  592. cg.a_reg_alloc(list,r);
  593. end;
  594. procedure thlcgobj.a_reg_dealloc(list: TAsmList; r: tregister);
  595. begin
  596. cg.a_reg_dealloc(list,r);
  597. end;
  598. procedure thlcgobj.a_reg_sync(list: TAsmList; r: tregister);
  599. begin
  600. cg.a_reg_sync(list,r);
  601. end;
  602. procedure thlcgobj.a_load_reg_cgpara(list: TAsmList; size: tdef; r: tregister; const cgpara: TCGPara);
  603. var
  604. ref: treference;
  605. begin
  606. cgpara.check_simple_location;
  607. paramanager.alloccgpara(list,cgpara);
  608. case cgpara.location^.loc of
  609. LOC_REGISTER,LOC_CREGISTER:
  610. a_load_reg_reg(list,size,cgpara.def,r,cgpara.location^.register);
  611. LOC_REFERENCE,LOC_CREFERENCE:
  612. begin
  613. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  614. a_load_reg_ref(list,size,cgpara.def,r,ref);
  615. end;
  616. (*
  617. LOC_MMREGISTER,LOC_CMMREGISTER:
  618. a_loadmm_intreg_reg(list,size,cgpara.def,r,cgpara.location^.register,mms_movescalar);
  619. *)
  620. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  621. begin
  622. tg.gethltemp(list,size,size.size,tt_normal,ref);
  623. a_load_reg_ref(list,size,size,r,ref);
  624. a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  625. tg.ungettemp(list,ref);
  626. end
  627. else
  628. internalerror(2010120415);
  629. end;
  630. end;
  631. procedure thlcgobj.a_load_const_cgpara(list: TAsmList; tosize: tdef; a: aint; const cgpara: TCGPara);
  632. var
  633. ref : treference;
  634. begin
  635. cgpara.check_simple_location;
  636. paramanager.alloccgpara(list,cgpara);
  637. case cgpara.location^.loc of
  638. LOC_REGISTER,LOC_CREGISTER:
  639. a_load_const_reg(list,cgpara.def,a,cgpara.location^.register);
  640. LOC_REFERENCE,LOC_CREFERENCE:
  641. begin
  642. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  643. a_load_const_ref(list,cgpara.def,a,ref);
  644. end
  645. else
  646. internalerror(2010120416);
  647. end;
  648. end;
  649. procedure thlcgobj.a_load_ref_cgpara(list: TAsmList; size: tdef; const r: treference; const cgpara: TCGPara);
  650. var
  651. ref: treference;
  652. begin
  653. cgpara.check_simple_location;
  654. paramanager.alloccgpara(list,cgpara);
  655. case cgpara.location^.loc of
  656. LOC_REGISTER,LOC_CREGISTER:
  657. a_load_ref_reg(list,size,cgpara.def,r,cgpara.location^.register);
  658. LOC_REFERENCE,LOC_CREFERENCE:
  659. begin
  660. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  661. a_load_ref_ref(list,size,cgpara.def,r,ref);
  662. end
  663. (*
  664. LOC_MMREGISTER,LOC_CMMREGISTER:
  665. begin
  666. case location^.size of
  667. OS_F32,
  668. OS_F64,
  669. OS_F128:
  670. a_loadmm_ref_reg(list,cgpara.def,cgpara.def,r,location^.register,mms_movescalar);
  671. OS_M8..OS_M128,
  672. OS_MS8..OS_MS128:
  673. a_loadmm_ref_reg(list,cgpara.def,cgpara.def,r,location^.register,nil);
  674. else
  675. internalerror(2010120417);
  676. end;
  677. end
  678. *)
  679. else
  680. internalerror(2010120418);
  681. end;
  682. end;
  683. procedure thlcgobj.a_load_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: TCGPara);
  684. begin
  685. case l.loc of
  686. LOC_REGISTER,
  687. LOC_CREGISTER :
  688. a_load_reg_cgpara(list,size,l.register,cgpara);
  689. LOC_CONSTANT :
  690. a_load_const_cgpara(list,size,l.value,cgpara);
  691. LOC_CREFERENCE,
  692. LOC_REFERENCE :
  693. a_load_ref_cgpara(list,size,l.reference,cgpara);
  694. else
  695. internalerror(2010120419);
  696. end;
  697. end;
  698. procedure thlcgobj.a_loadaddr_ref_cgpara(list: TAsmList; fromsize, tosize: tdef; const r: treference; const cgpara: TCGPara);
  699. var
  700. hr : tregister;
  701. begin
  702. cgpara.check_simple_location;
  703. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  704. begin
  705. paramanager.allocparaloc(list,cgpara.location);
  706. a_loadaddr_ref_reg(list,fromsize,tosize,r,cgpara.location^.register)
  707. end
  708. else
  709. begin
  710. hr:=getaddressregister(list,tosize);
  711. a_loadaddr_ref_reg(list,fromsize,tosize,r,hr);
  712. a_load_reg_cgpara(list,tosize,hr,cgpara);
  713. end;
  714. end;
  715. procedure thlcgobj.a_call_ref(list: TAsmList; pd: tabstractprocdef; const ref: treference);
  716. var
  717. reg: tregister;
  718. size: tdef;
  719. begin
  720. { the loaded data is always a pointer to a procdef. A procvardef is
  721. implicitly a pointer already, but a procdef isn't -> create one }
  722. if pd.typ=procvardef then
  723. size:=pd
  724. else
  725. size:=getpointerdef(pd);
  726. reg:=getaddressregister(list,size);
  727. a_load_ref_reg(list,size,size,ref,reg);
  728. a_call_reg(list,pd,reg);
  729. end;
  730. procedure thlcgobj.a_call_name_static(list: TAsmList; pd: tprocdef; const s: TSymStr);
  731. begin
  732. a_call_name(list,pd,s,false);
  733. end;
  734. procedure thlcgobj.a_call_name_inherited(list: TAsmList; pd: tprocdef; const s: TSymStr);
  735. begin
  736. a_call_name(list,pd,s,false);
  737. end;
  738. procedure thlcgobj.a_load_const_ref(list: TAsmList; tosize: tdef; a: aint; const ref: treference);
  739. var
  740. tmpreg: tregister;
  741. begin
  742. tmpreg:=getintregister(list,tosize);
  743. a_load_const_reg(list,tosize,a,tmpreg);
  744. a_load_reg_ref(list,tosize,tosize,tmpreg,ref);
  745. end;
  746. procedure thlcgobj.a_load_const_loc(list: TAsmList; tosize: tdef; a: aint; const loc: tlocation);
  747. begin
  748. case loc.loc of
  749. LOC_REFERENCE,LOC_CREFERENCE:
  750. a_load_const_ref(list,tosize,a,loc.reference);
  751. LOC_REGISTER,LOC_CREGISTER:
  752. a_load_const_reg(list,tosize,a,loc.register);
  753. LOC_SUBSETREG,LOC_CSUBSETREG:
  754. a_load_const_subsetreg(list,tosize,a,loc.sreg);
  755. LOC_SUBSETREF,LOC_CSUBSETREF:
  756. a_load_const_subsetref(list,tosize,a,loc.sref);
  757. else
  758. internalerror(2010120401);
  759. end;
  760. end;
  761. procedure thlcgobj.a_load_reg_ref_unaligned(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  762. begin
  763. a_load_reg_ref(list,fromsize,tosize,register,ref);
  764. end;
  765. procedure thlcgobj.a_load_reg_loc(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const loc: tlocation);
  766. begin
  767. case loc.loc of
  768. LOC_REFERENCE,LOC_CREFERENCE:
  769. a_load_reg_ref(list,fromsize,tosize,reg,loc.reference);
  770. LOC_REGISTER,LOC_CREGISTER:
  771. a_load_reg_reg(list,fromsize,tosize,reg,loc.register);
  772. LOC_SUBSETREG,LOC_CSUBSETREG:
  773. a_load_reg_subsetreg(list,fromsize,tosize,reg,loc.sreg);
  774. LOC_SUBSETREF,LOC_CSUBSETREF:
  775. a_load_reg_subsetref(list,fromsize,tosize,reg,loc.sref);
  776. { we don't have enough type information to handle these here
  777. LOC_MMREGISTER,LOC_CMMREGISTER:
  778. a_loadmm_intreg_reg(list,fromsize,loc.size,reg,loc.register,mms_movescalar);
  779. }
  780. else
  781. internalerror(2010120402);
  782. end;
  783. end;
  784. procedure thlcgobj.a_load_ref_reg_unaligned(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  785. begin
  786. a_load_ref_reg(list,fromsize,tosize,ref,register);
  787. end;
  788. procedure thlcgobj.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  789. var
  790. tmpreg: tregister;
  791. begin
  792. { verify if we have the same reference }
  793. if references_equal(sref,dref) then
  794. exit;
  795. tmpreg:=getintregister(list,tosize);
  796. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  797. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  798. end;
  799. procedure thlcgobj.a_load_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; reg: tregister);
  800. begin
  801. case loc.loc of
  802. LOC_REFERENCE,LOC_CREFERENCE:
  803. a_load_ref_reg(list,fromsize,tosize,loc.reference,reg);
  804. LOC_REGISTER,LOC_CREGISTER:
  805. a_load_reg_reg(list,fromsize,tosize,loc.register,reg);
  806. LOC_CONSTANT:
  807. a_load_const_reg(list,tosize,loc.value,reg);
  808. LOC_SUBSETREG,LOC_CSUBSETREG:
  809. a_load_subsetreg_reg(list,fromsize,tosize,loc.sreg,reg);
  810. LOC_SUBSETREF,LOC_CSUBSETREF:
  811. a_load_subsetref_reg(list,fromsize,tosize,loc.sref,reg);
  812. else
  813. internalerror(2010120201);
  814. end;
  815. end;
  816. procedure thlcgobj.a_load_loc_ref(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const ref: treference);
  817. begin
  818. case loc.loc of
  819. LOC_REFERENCE,LOC_CREFERENCE:
  820. a_load_ref_ref(list,fromsize,tosize,loc.reference,ref);
  821. LOC_REGISTER,LOC_CREGISTER:
  822. a_load_reg_ref(list,fromsize,tosize,loc.register,ref);
  823. LOC_CONSTANT:
  824. a_load_const_ref(list,tosize,loc.value,ref);
  825. LOC_SUBSETREG,LOC_CSUBSETREG:
  826. a_load_subsetreg_ref(list,fromsize,tosize,loc.sreg,ref);
  827. LOC_SUBSETREF,LOC_CSUBSETREF:
  828. a_load_subsetref_ref(list,fromsize,tosize,loc.sref,ref);
  829. else
  830. internalerror(2010120403);
  831. end;
  832. end;
  833. procedure thlcgobj.a_load_loc_subsetreg(list: TAsmList; fromsize, tosubsetsize: tdef; const loc: tlocation; const sreg: tsubsetregister);
  834. begin
  835. case loc.loc of
  836. LOC_REFERENCE,LOC_CREFERENCE:
  837. a_load_ref_subsetreg(list,fromsize,tosubsetsize,loc.reference,sreg);
  838. LOC_REGISTER,LOC_CREGISTER:
  839. a_load_reg_subsetreg(list,fromsize,tosubsetsize,loc.register,sreg);
  840. LOC_CONSTANT:
  841. a_load_const_subsetreg(list,tosubsetsize,loc.value,sreg);
  842. LOC_SUBSETREG,LOC_CSUBSETREG:
  843. a_load_subsetreg_subsetreg(list,fromsize,tosubsetsize,loc.sreg,sreg);
  844. LOC_SUBSETREF,LOC_CSUBSETREF:
  845. a_load_subsetref_subsetreg(list,fromsize,tosubsetsize,loc.sref,sreg);
  846. else
  847. internalerror(2010120404);
  848. end;
  849. end;
  850. procedure thlcgobj.a_load_loc_subsetref(list: TAsmList; fromsize, tosubsetsize: tdef; const loc: tlocation; const sref: tsubsetreference);
  851. begin
  852. case loc.loc of
  853. LOC_REFERENCE,LOC_CREFERENCE:
  854. a_load_ref_subsetref(list,fromsize,tosubsetsize,loc.reference,sref);
  855. LOC_REGISTER,LOC_CREGISTER:
  856. a_load_reg_subsetref(list,fromsize,tosubsetsize,loc.register,sref);
  857. LOC_CONSTANT:
  858. a_load_const_subsetref(list,tosubsetsize,loc.value,sref);
  859. LOC_SUBSETREG,LOC_CSUBSETREG:
  860. a_load_subsetreg_subsetref(list,fromsize,tosubsetsize,loc.sreg,sref);
  861. LOC_SUBSETREF,LOC_CSUBSETREF:
  862. a_load_subsetref_subsetref(list,fromsize,tosubsetsize,loc.sref,sref);
  863. else
  864. internalerror(2010120405);
  865. end;
  866. end;
  867. {$push}
  868. {$r-,q-}
  869. procedure thlcgobj.a_load_subsetreg_reg(list: TAsmList; subsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister);
  870. var
  871. subsetregdef: torddef;
  872. bitmask: aword;
  873. tmpreg,
  874. subsetsizereg: tregister;
  875. stopbit: byte;
  876. begin
  877. subsetregdef:=tcgsize2orddef(sreg.subsetregsize);
  878. tmpreg:=getintregister(list,subsetregdef);
  879. if is_signed(subsetsize) then
  880. begin
  881. { sign extend in case the value has a bitsize mod 8 <> 0 }
  882. { both instructions will be optimized away if not }
  883. a_op_const_reg_reg(list,OP_SHL,subsetregdef,(tcgsize2size[sreg.subsetregsize]*8)-sreg.startbit-sreg.bitlen,sreg.subsetreg,tmpreg);
  884. a_op_const_reg(list,OP_SAR,subsetregdef,(tcgsize2size[sreg.subsetregsize]*8)-sreg.bitlen,tmpreg);
  885. end
  886. else
  887. begin
  888. a_op_const_reg_reg(list,OP_SHR,subsetregdef,sreg.startbit,sreg.subsetreg,tmpreg);
  889. stopbit:=sreg.startbit+sreg.bitlen;
  890. // on x86(64), 1 shl 32(64) = 1 instead of 0
  891. // use aword to prevent overflow with 1 shl 31
  892. if (stopbit-sreg.startbit<>AIntBits) then
  893. bitmask:=(aword(1) shl (stopbit-sreg.startbit))-1
  894. else
  895. bitmask:=high(aword);
  896. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(bitmask),tmpreg);
  897. end;
  898. subsetsizereg:=getintregister(list,subsetsize);
  899. a_load_reg_reg(list,subsetregdef,subsetsize,tmpreg,subsetsizereg);
  900. a_load_reg_reg(list,subsetsize,tosize,subsetsizereg,destreg);
  901. end;
  902. procedure thlcgobj.a_load_reg_subsetreg(list: TAsmList; fromsize, tosubsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister);
  903. begin
  904. a_load_regconst_subsetreg_intern(list,fromsize,tosubsetsize,fromreg,sreg,SL_REG);
  905. end;
  906. procedure thlcgobj.a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsreg, tosreg: tsubsetregister);
  907. var
  908. fromsubsetregdef,
  909. tosubsetregdef: torddef;
  910. tmpreg: tregister;
  911. bitmask: aword;
  912. stopbit: byte;
  913. begin
  914. if (fromsreg.bitlen>=tosreg.bitlen) then
  915. begin
  916. fromsubsetregdef:=tcgsize2orddef(fromsreg.subsetregsize);
  917. tosubsetregdef:=tcgsize2orddef(tosreg.subsetregsize);
  918. tmpreg:=getintregister(list,tosubsetregdef);
  919. a_load_reg_reg(list,fromsubsetregdef,tosubsetregdef,fromsreg.subsetreg,tmpreg);
  920. if (fromsreg.startbit<=tosreg.startbit) then
  921. a_op_const_reg(list,OP_SHL,tosubsetregdef,tosreg.startbit-fromsreg.startbit,tmpreg)
  922. else
  923. a_op_const_reg(list,OP_SHR,tosubsetregdef,fromsreg.startbit-tosreg.startbit,tmpreg);
  924. stopbit:=tosreg.startbit + tosreg.bitlen;
  925. // on x86(64), 1 shl 32(64) = 1 instead of 0
  926. if (stopbit<>AIntBits) then
  927. bitmask:=not(((aword(1) shl stopbit)-1) xor ((aword(1) shl tosreg.startbit)-1))
  928. else
  929. bitmask:=(aword(1) shl tosreg.startbit) - 1;
  930. a_op_const_reg(list,OP_AND,tosubsetregdef,tcgint(bitmask),tosreg.subsetreg);
  931. a_op_const_reg(list,OP_AND,tosubsetregdef,tcgint(not(bitmask)),tmpreg);
  932. a_op_reg_reg(list,OP_OR,tosubsetregdef,tmpreg,tosreg.subsetreg);
  933. end
  934. else
  935. begin
  936. tmpreg:=getintregister(list,tosubsetsize);
  937. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  938. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  939. end;
  940. end;
  941. procedure thlcgobj.a_load_subsetreg_ref(list: TAsmList; fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const destref: treference);
  942. var
  943. tmpreg: tregister;
  944. begin
  945. tmpreg:=getintregister(list,tosize);
  946. a_load_subsetreg_reg(list,fromsubsetsize,tosize,sreg,tmpreg);
  947. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  948. end;
  949. procedure thlcgobj.a_load_ref_subsetreg(list: TAsmList; fromsize, tosubsetsize: tdef; const fromref: treference; const sreg: tsubsetregister);
  950. var
  951. tmpreg: tregister;
  952. begin
  953. tmpreg := getintregister(list,tosubsetsize);
  954. a_load_ref_reg(list,fromsize,tosubsetsize,fromref,tmpreg);
  955. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,sreg);
  956. end;
  957. procedure thlcgobj.a_load_const_subsetreg(list: TAsmlist; tosubsetsize: tdef; a: aint; const sreg: tsubsetregister);
  958. var
  959. subsetregdef: torddef;
  960. bitmask: aword;
  961. stopbit: byte;
  962. begin
  963. subsetregdef:=tcgsize2orddef(sreg.subsetregsize);
  964. stopbit:=sreg.startbit+sreg.bitlen;
  965. // on x86(64), 1 shl 32(64) = 1 instead of 0
  966. if (stopbit<>AIntBits) then
  967. bitmask:=not(((aword(1) shl stopbit)-1) xor ((aword(1) shl sreg.startbit)-1))
  968. else
  969. bitmask:=(aword(1) shl sreg.startbit)-1;
  970. if (((aword(a) shl sreg.startbit) and not bitmask)<>not bitmask) then
  971. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(bitmask),sreg.subsetreg);
  972. a_op_const_reg(list,OP_OR,subsetregdef,tcgint((aword(a) shl sreg.startbit) and not(bitmask)),sreg.subsetreg);
  973. end;
  974. procedure thlcgobj.a_load_subsetreg_loc(list: TAsmlist; fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const loc: tlocation);
  975. begin
  976. case loc.loc of
  977. LOC_REFERENCE,LOC_CREFERENCE:
  978. a_load_subsetreg_ref(list,fromsubsetsize,tosize,sreg,loc.reference);
  979. LOC_REGISTER,LOC_CREGISTER:
  980. a_load_subsetreg_reg(list,fromsubsetsize,tosize,sreg,loc.register);
  981. LOC_SUBSETREG,LOC_CSUBSETREG:
  982. a_load_subsetreg_subsetreg(list,fromsubsetsize,tosize,sreg,loc.sreg);
  983. LOC_SUBSETREF,LOC_CSUBSETREF:
  984. a_load_subsetreg_subsetref(list,fromsubsetsize,tosize,sreg,loc.sref);
  985. else
  986. internalerror(2010120406);
  987. end;
  988. end;
  989. procedure thlcgobj.a_load_subsetref_reg(list: TAsmList; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; destreg: tregister);
  990. var
  991. tmpref: treference;
  992. valuereg,extra_value_reg: tregister;
  993. tosreg: tsubsetregister;
  994. loadsize: torddef;
  995. loadbitsize: byte;
  996. extra_load: boolean;
  997. begin
  998. get_subsetref_load_info(sref,loadsize,extra_load);
  999. loadbitsize:=loadsize.size*8;
  1000. { load the (first part) of the bit sequence }
  1001. valuereg:=getintregister(list,osuinttype);
  1002. a_load_ref_reg(list,loadsize,osuinttype,sref.ref,valuereg);
  1003. if not extra_load then
  1004. begin
  1005. { everything is guaranteed to be in a single register of loadsize }
  1006. if (sref.bitindexreg=NR_NO) then
  1007. begin
  1008. { use subsetreg routine, it may have been overridden with an optimized version }
  1009. tosreg.subsetreg:=valuereg;
  1010. tosreg.subsetregsize:=def_cgsize(osuinttype);
  1011. { subsetregs always count bits from right to left }
  1012. if (target_info.endian=endian_big) then
  1013. tosreg.startbit:=loadbitsize-(sref.startbit+sref.bitlen)
  1014. else
  1015. tosreg.startbit:=sref.startbit;
  1016. tosreg.bitlen:=sref.bitlen;
  1017. a_load_subsetreg_reg(list,fromsubsetsize,tosize,tosreg,destreg);
  1018. exit;
  1019. end
  1020. else
  1021. begin
  1022. if (sref.startbit<>0) then
  1023. internalerror(2006081510);
  1024. if (target_info.endian=endian_big) then
  1025. begin
  1026. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,valuereg);
  1027. if is_signed(fromsubsetsize) then
  1028. begin
  1029. { sign extend to entire register }
  1030. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-loadbitsize,valuereg);
  1031. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1032. end
  1033. else
  1034. a_op_const_reg(list,OP_SHR,osuinttype,loadbitsize-sref.bitlen,valuereg);
  1035. end
  1036. else
  1037. begin
  1038. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,valuereg);
  1039. if is_signed(fromsubsetsize) then
  1040. begin
  1041. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-sref.bitlen,valuereg);
  1042. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1043. end
  1044. end;
  1045. { mask other bits/sign extend }
  1046. if not is_signed(fromsubsetsize) then
  1047. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1048. end
  1049. end
  1050. else
  1051. begin
  1052. { load next value as well }
  1053. extra_value_reg:=getintregister(list,osuinttype);
  1054. if (sref.bitindexreg=NR_NO) then
  1055. begin
  1056. tmpref:=sref.ref;
  1057. inc(tmpref.offset,loadbitsize div 8);
  1058. a_load_ref_reg(list,loadsize,osuinttype,tmpref,extra_value_reg);
  1059. { can be overridden to optimize }
  1060. a_load_subsetref_regs_noindex(list,fromsubsetsize,loadbitsize,sref,valuereg,extra_value_reg)
  1061. end
  1062. else
  1063. begin
  1064. if (sref.startbit<>0) then
  1065. internalerror(2006080610);
  1066. a_load_subsetref_regs_index(list,fromsubsetsize,loadbitsize,sref,valuereg);
  1067. end;
  1068. end;
  1069. { store in destination }
  1070. {$ifndef cpuhighleveltarget}
  1071. { avoid unnecessary sign extension and zeroing }
  1072. valuereg:=cg.makeregsize(list,valuereg,OS_INT);
  1073. destreg:=cg.makeregsize(list,destreg,OS_INT);
  1074. cg.a_load_reg_reg(list,OS_INT,OS_INT,valuereg,destreg);
  1075. destreg:=cg.makeregsize(list,destreg,def_cgsize(tosize));
  1076. {$else}
  1077. { can't juggle with register sizes, they are actually typed entities
  1078. here }
  1079. a_load_reg_reg(list,osuinttype,tosize,valuereg,destreg);
  1080. {$endif}
  1081. end;
  1082. procedure thlcgobj.a_load_reg_subsetref(list: TAsmList; fromsize, tosubsetsize: tdef; fromreg: tregister; const sref: tsubsetreference);
  1083. begin
  1084. a_load_regconst_subsetref_intern(list,fromsize,tosubsetsize,fromreg,sref,SL_REG);
  1085. end;
  1086. procedure thlcgobj.a_load_subsetref_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsref, tosref: tsubsetreference);
  1087. var
  1088. tmpreg: tregister;
  1089. begin
  1090. tmpreg:=getintregister(list,tosubsetsize);
  1091. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1092. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1093. end;
  1094. procedure thlcgobj.a_load_subsetref_ref(list: TAsmList; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const destref: treference);
  1095. var
  1096. tmpreg: tregister;
  1097. begin
  1098. tmpreg:=getintregister(list,tosize);
  1099. a_load_subsetref_reg(list,fromsubsetsize,tosize,sref,tmpreg);
  1100. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  1101. end;
  1102. procedure thlcgobj.a_load_ref_subsetref(list: TAsmList; fromsize, tosubsetsize: tdef; const fromref: treference; const sref: tsubsetreference);
  1103. var
  1104. tmpreg: tregister;
  1105. begin
  1106. tmpreg := getintregister(list,tosubsetsize);
  1107. a_load_ref_reg(list,fromsize,tosubsetsize,fromref,tmpreg);
  1108. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,sref);
  1109. end;
  1110. procedure thlcgobj.a_load_const_subsetref(list: TAsmlist; tosubsetsize: tdef; a: tcgint; const sref: tsubsetreference);
  1111. var
  1112. tmpreg: tregister;
  1113. slopt: tsubsetloadopt;
  1114. begin
  1115. { perform masking of the source value in advance }
  1116. slopt:=SL_REGNOSRCMASK;
  1117. if (sref.bitlen<>AIntBits) then
  1118. a:=tcgint(aword(a) and ((aword(1) shl sref.bitlen) -1));
  1119. if (
  1120. { broken x86 "x shl regbitsize = x" }
  1121. ((sref.bitlen<>AIntBits) and
  1122. ((aword(a) and ((aword(1) shl sref.bitlen)-1))=(aword(1) shl sref.bitlen)-1)) or
  1123. ((sref.bitlen=AIntBits) and
  1124. (a=-1))
  1125. ) then
  1126. slopt:=SL_SETMAX
  1127. else if (a=0) then
  1128. slopt:=SL_SETZERO;
  1129. if not(slopt in [SL_SETZERO,SL_SETMAX]) then
  1130. begin
  1131. tmpreg:=getintregister(list,tosubsetsize);
  1132. a_load_const_reg(list,tosubsetsize,a,tmpreg);
  1133. end
  1134. else
  1135. tmpreg:=NR_NO;
  1136. a_load_regconst_subsetref_intern(list,tosubsetsize,tosubsetsize,tmpreg,sref,slopt);
  1137. end;
  1138. procedure thlcgobj.a_load_subsetref_loc(list: TAsmlist; fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const loc: tlocation);
  1139. begin
  1140. case loc.loc of
  1141. LOC_REFERENCE,LOC_CREFERENCE:
  1142. a_load_subsetref_ref(list,fromsubsetsize,tosize,sref,loc.reference);
  1143. LOC_REGISTER,LOC_CREGISTER:
  1144. a_load_subsetref_reg(list,fromsubsetsize,tosize,sref,loc.register);
  1145. LOC_SUBSETREG,LOC_CSUBSETREG:
  1146. a_load_subsetref_subsetreg(list,fromsubsetsize,tosize,sref,loc.sreg);
  1147. LOC_SUBSETREF,LOC_CSUBSETREF:
  1148. a_load_subsetref_subsetref(list,fromsubsetsize,tosize,sref,loc.sref);
  1149. else
  1150. internalerror(2010120407);
  1151. end;
  1152. end;
  1153. procedure thlcgobj.a_load_subsetref_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsref: tsubsetreference; const tosreg: tsubsetregister);
  1154. var
  1155. tmpreg: tregister;
  1156. begin
  1157. tmpreg:=getintregister(list,tosubsetsize);
  1158. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1159. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  1160. end;
  1161. procedure thlcgobj.a_load_subsetreg_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize: tdef; const fromsreg: tsubsetregister; const tosref: tsubsetreference);
  1162. var
  1163. tmpreg: tregister;
  1164. begin
  1165. tmpreg := getintregister(list,tosubsetsize);
  1166. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  1167. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1168. end;
  1169. procedure thlcgobj.a_bit_test_reg_reg_reg(list: TAsmList; bitnumbersize, valuesize, destsize: tdef; bitnumber, value, destreg: tregister);
  1170. var
  1171. tmpvalue: tregister;
  1172. begin
  1173. tmpvalue:=getintregister(list,valuesize);
  1174. if (target_info.endian=endian_little) then
  1175. begin
  1176. { rotate value register "bitnumber" bits to the right }
  1177. a_op_reg_reg_reg(list,OP_SHR,valuesize,bitnumber,value,tmpvalue);
  1178. { extract the bit we want }
  1179. a_op_const_reg(list,OP_AND,valuesize,1,tmpvalue);
  1180. end
  1181. else
  1182. begin
  1183. { highest (leftmost) bit = bit 0 -> shl bitnumber results in wanted }
  1184. { bit in uppermost position, then move it to the lowest position }
  1185. { "and" is not necessary since combination of shl/shr will clear }
  1186. { all other bits }
  1187. a_op_reg_reg_reg(list,OP_SHL,valuesize,bitnumber,value,tmpvalue);
  1188. a_op_const_reg(list,OP_SHR,valuesize,valuesize.size*8-1,tmpvalue);
  1189. end;
  1190. a_load_reg_reg(list,valuesize,destsize,tmpvalue,destreg);
  1191. end;
  1192. procedure thlcgobj.a_bit_test_const_ref_reg(list: TAsmList; fromsize, destsize: tdef; bitnumber: aint; const ref: treference; destreg: tregister);
  1193. begin
  1194. a_load_subsetref_reg(list,u8inttype,destsize,get_bit_const_ref_sref(bitnumber,fromsize,ref),destreg);
  1195. end;
  1196. procedure thlcgobj.a_bit_test_const_reg_reg(list: TAsmList; setregsize, destsize: tdef; bitnumber: aint; setreg, destreg: tregister);
  1197. begin
  1198. a_load_subsetreg_reg(list,setregsize,destsize,get_bit_const_reg_sreg(setregsize,bitnumber,setreg),destreg);
  1199. end;
  1200. procedure thlcgobj.a_bit_test_const_subsetreg_reg(list: TAsmList; fromsubsetsize, destsize: tdef; bitnumber: aint; const setreg: tsubsetregister; destreg: tregister);
  1201. var
  1202. tmpsreg: tsubsetregister;
  1203. begin
  1204. { the first parameter is used to calculate the bit offset in }
  1205. { case of big endian, and therefore must be the size of the }
  1206. { set and not of the whole subsetreg }
  1207. tmpsreg:=get_bit_const_reg_sreg(fromsubsetsize,bitnumber,setreg.subsetreg);
  1208. { now fix the size of the subsetreg }
  1209. tmpsreg.subsetregsize:=setreg.subsetregsize;
  1210. { correct offset of the set in the subsetreg }
  1211. inc(tmpsreg.startbit,setreg.startbit);
  1212. a_load_subsetreg_reg(list,fromsubsetsize,destsize,tmpsreg,destreg);
  1213. end;
  1214. procedure thlcgobj.a_bit_test_reg_ref_reg(list: TAsmList; bitnumbersize, refsize, destsize: tdef; bitnumber: tregister; const ref: treference; destreg: tregister);
  1215. begin
  1216. a_load_subsetref_reg(list,u8inttype,destsize,get_bit_reg_ref_sref(list,bitnumbersize,refsize,bitnumber,ref),destreg);
  1217. end;
  1218. procedure thlcgobj.a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, locsize, destsize: tdef; bitnumber: tregister; const loc: tlocation; destreg: tregister);
  1219. var
  1220. tmpreg: tregister;
  1221. begin
  1222. case loc.loc of
  1223. LOC_REFERENCE,LOC_CREFERENCE:
  1224. a_bit_test_reg_ref_reg(list,bitnumbersize,locsize,destsize,bitnumber,loc.reference,destreg);
  1225. LOC_REGISTER,LOC_CREGISTER,
  1226. LOC_SUBSETREG,LOC_CSUBSETREG,
  1227. LOC_CONSTANT:
  1228. begin
  1229. case loc.loc of
  1230. LOC_REGISTER,LOC_CREGISTER:
  1231. tmpreg:=loc.register;
  1232. LOC_SUBSETREG,LOC_CSUBSETREG:
  1233. begin
  1234. tmpreg:=getintregister(list,locsize);
  1235. a_load_subsetreg_reg(list,locsize,locsize,loc.sreg,tmpreg);
  1236. end;
  1237. LOC_CONSTANT:
  1238. begin
  1239. tmpreg:=getintregister(list,locsize);
  1240. a_load_const_reg(list,locsize,loc.value,tmpreg);
  1241. end;
  1242. end;
  1243. a_bit_test_reg_reg_reg(list,bitnumbersize,locsize,destsize,bitnumber,tmpreg,destreg);
  1244. end;
  1245. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1246. else
  1247. internalerror(2010120411);
  1248. end;
  1249. end;
  1250. procedure thlcgobj.a_bit_test_const_loc_reg(list: TAsmList; locsize, destsize: tdef; bitnumber: aint; const loc: tlocation; destreg: tregister);
  1251. begin
  1252. case loc.loc of
  1253. LOC_REFERENCE,LOC_CREFERENCE:
  1254. a_bit_test_const_ref_reg(list,locsize,destsize,bitnumber,loc.reference,destreg);
  1255. LOC_REGISTER,LOC_CREGISTER:
  1256. a_bit_test_const_reg_reg(list,locsize,destsize,bitnumber,loc.register,destreg);
  1257. LOC_SUBSETREG,LOC_CSUBSETREG:
  1258. a_bit_test_const_subsetreg_reg(list,locsize,destsize,bitnumber,loc.sreg,destreg);
  1259. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1260. else
  1261. internalerror(2010120410);
  1262. end;
  1263. end;
  1264. procedure thlcgobj.a_bit_set_reg_reg(list: TAsmList; doset: boolean; bitnumbersize, destsize: tdef; bitnumber, dest: tregister);
  1265. var
  1266. tmpvalue: tregister;
  1267. begin
  1268. tmpvalue:=getintregister(list,destsize);
  1269. if (target_info.endian=endian_little) then
  1270. begin
  1271. a_load_const_reg(list,destsize,1,tmpvalue);
  1272. { rotate bit "bitnumber" bits to the left }
  1273. a_op_reg_reg(list,OP_SHL,destsize,bitnumber,tmpvalue);
  1274. end
  1275. else
  1276. begin
  1277. { highest (leftmost) bit = bit 0 -> "$80/$8000/$80000000/ ... }
  1278. { shr bitnumber" results in correct mask }
  1279. a_load_const_reg(list,destsize,1 shl (destsize.size*8-1),tmpvalue);
  1280. a_op_reg_reg(list,OP_SHR,destsize,bitnumber,tmpvalue);
  1281. end;
  1282. { set/clear the bit we want }
  1283. if doset then
  1284. a_op_reg_reg(list,OP_OR,destsize,tmpvalue,dest)
  1285. else
  1286. begin
  1287. a_op_reg_reg(list,OP_NOT,destsize,tmpvalue,tmpvalue);
  1288. a_op_reg_reg(list,OP_AND,destsize,tmpvalue,dest)
  1289. end;
  1290. end;
  1291. procedure thlcgobj.a_bit_set_const_ref(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; const ref: treference);
  1292. begin
  1293. a_load_const_subsetref(list,u8inttype,ord(doset),get_bit_const_ref_sref(bitnumber,destsize,ref));
  1294. end;
  1295. procedure thlcgobj.a_bit_set_const_reg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; destreg: tregister);
  1296. begin
  1297. a_load_const_subsetreg(list,u8inttype,ord(doset),get_bit_const_reg_sreg(destsize,bitnumber,destreg));
  1298. end;
  1299. procedure thlcgobj.a_bit_set_const_subsetreg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; const destreg: tsubsetregister);
  1300. var
  1301. tmpsreg: tsubsetregister;
  1302. begin
  1303. { the first parameter is used to calculate the bit offset in }
  1304. { case of big endian, and therefore must be the size of the }
  1305. { set and not of the whole subsetreg }
  1306. tmpsreg:=get_bit_const_reg_sreg(destsize,bitnumber,destreg.subsetreg);
  1307. { now fix the size of the subsetreg }
  1308. tmpsreg.subsetregsize:=destreg.subsetregsize;
  1309. { correct offset of the set in the subsetreg }
  1310. inc(tmpsreg.startbit,destreg.startbit);
  1311. a_load_const_subsetreg(list,u8inttype,ord(doset),tmpsreg);
  1312. end;
  1313. procedure thlcgobj.a_bit_set_reg_ref(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const ref: treference);
  1314. begin
  1315. a_load_const_subsetref(list,u8inttype,ord(doset),get_bit_reg_ref_sref(list,fromsize,tosize,bitnumber,ref));
  1316. end;
  1317. procedure thlcgobj.a_bit_set_reg_loc(list: TAsmList; doset: boolean; regsize, tosize: tdef; bitnumber: tregister; const loc: tlocation);
  1318. var
  1319. tmpreg: tregister;
  1320. begin
  1321. case loc.loc of
  1322. LOC_REFERENCE:
  1323. a_bit_set_reg_ref(list,doset,regsize,tosize,bitnumber,loc.reference);
  1324. LOC_CREGISTER:
  1325. a_bit_set_reg_reg(list,doset,regsize,tosize,bitnumber,loc.register);
  1326. { e.g. a 2-byte set in a record regvar }
  1327. LOC_CSUBSETREG:
  1328. begin
  1329. { hard to do in-place in a generic way, so operate on a copy }
  1330. tmpreg:=getintregister(list,tosize);
  1331. a_load_subsetreg_reg(list,tosize,tosize,loc.sreg,tmpreg);
  1332. a_bit_set_reg_reg(list,doset,regsize,tosize,bitnumber,tmpreg);
  1333. a_load_reg_subsetreg(list,tosize,tosize,tmpreg,loc.sreg);
  1334. end;
  1335. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1336. else
  1337. internalerror(2010120408)
  1338. end;
  1339. end;
  1340. procedure thlcgobj.a_bit_set_const_loc(list: TAsmList; doset: boolean; tosize: tdef; bitnumber: aint; const loc: tlocation);
  1341. begin
  1342. case loc.loc of
  1343. LOC_REFERENCE:
  1344. a_bit_set_const_ref(list,doset,tosize,bitnumber,loc.reference);
  1345. LOC_CREGISTER:
  1346. a_bit_set_const_reg(list,doset,tosize,bitnumber,loc.register);
  1347. LOC_CSUBSETREG:
  1348. a_bit_set_const_subsetreg(list,doset,tosize,bitnumber,loc.sreg);
  1349. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1350. else
  1351. internalerror(2010120409)
  1352. end;
  1353. end;
  1354. (*
  1355. Subsetrefs are used for (bit)packed arrays and (bit)packed records stored
  1356. in memory. They are like a regular reference, but contain an extra bit
  1357. offset (either constant -startbit- or variable -bitindexreg-, always OS_INT)
  1358. and a bit length (always constant).
  1359. Bit packed values are stored differently in memory depending on whether we
  1360. are on a big or a little endian system (compatible with at least GPC). The
  1361. size of the basic working unit is always the smallest power-of-2 byte size
  1362. which can contain the bit value (so 1..8 bits -> 1 byte, 9..16 bits -> 2
  1363. bytes, 17..32 bits -> 4 bytes etc).
  1364. On a big endian, 5-bit: values are stored like this:
  1365. 11111222 22333334 44445555 56666677 77788888
  1366. The leftmost bit of each 5-bit value corresponds to the most significant
  1367. bit.
  1368. On little endian, it goes like this:
  1369. 22211111 43333322 55554444 77666665 88888777
  1370. In this case, per byte the left-most bit is more significant than those on
  1371. the right, but the bits in the next byte are all more significant than
  1372. those in the previous byte (e.g., the 222 in the first byte are the low
  1373. three bits of that value, while the 22 in the second byte are the upper
  1374. two bits.
  1375. Big endian, 9 bit values:
  1376. 11111111 12222222 22333333 33344444 ...
  1377. Little endian, 9 bit values:
  1378. 11111111 22222221 33333322 44444333 ...
  1379. This is memory representation and the 16 bit values are byteswapped.
  1380. Similarly as in the previous case, the 2222222 string contains the lower
  1381. bits of value 2 and the 22 string contains the upper bits. Once loaded into
  1382. registers (two 16 bit registers in the current implementation, although a
  1383. single 32 bit register would be possible too, in particular if 32 bit
  1384. alignment can be guaranteed), this becomes:
  1385. 22222221 11111111 44444333 33333322 ...
  1386. (l)ow u l l u l u
  1387. The startbit/bitindex in a subsetreference always refers to
  1388. a) on big endian: the most significant bit of the value
  1389. (bits counted from left to right, both memory an registers)
  1390. b) on little endian: the least significant bit when the value
  1391. is loaded in a register (bit counted from right to left)
  1392. Although a) results in more complex code for big endian systems, it's
  1393. needed for compatibility both with GPC and with e.g. bitpacked arrays in
  1394. Apple's universal interfaces which depend on these layout differences).
  1395. Note: when changing the loadsize calculated in get_subsetref_load_info,
  1396. make sure the appropriate alignment is guaranteed, at least in case of
  1397. {$defined cpurequiresproperalignment}.
  1398. *)
  1399. procedure thlcgobj.get_subsetref_load_info(const sref: tsubsetreference; out loadsize: torddef; out extra_load: boolean);
  1400. var
  1401. intloadsize: tcgint;
  1402. begin
  1403. intloadsize:=packedbitsloadsize(sref.bitlen);
  1404. if (intloadsize=0) then
  1405. internalerror(2006081310);
  1406. if (intloadsize>sizeof(aint)) then
  1407. intloadsize:=sizeof(aint);
  1408. loadsize:=tcgsize2orddef(int_cgsize(intloadsize));
  1409. if (sref.bitlen>sizeof(aint)*8) then
  1410. internalerror(2006081312);
  1411. extra_load:=
  1412. (sref.bitlen<>1) and
  1413. ((sref.bitindexreg<>NR_NO) or
  1414. (byte(sref.startbit+sref.bitlen)>byte(intloadsize*8)));
  1415. end;
  1416. procedure thlcgobj.a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister);
  1417. var
  1418. restbits: byte;
  1419. begin
  1420. if (target_info.endian=endian_big) then
  1421. begin
  1422. { valuereg contains the upper bits, extra_value_reg the lower }
  1423. restbits:=(sref.bitlen-(loadbitsize-sref.startbit));
  1424. if is_signed(subsetsize) then
  1425. begin
  1426. { sign extend }
  1427. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-loadbitsize+sref.startbit,valuereg);
  1428. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1429. end
  1430. else
  1431. begin
  1432. a_op_const_reg(list,OP_SHL,osuinttype,restbits,valuereg);
  1433. { mask other bits }
  1434. if (sref.bitlen<>AIntBits) then
  1435. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1436. end;
  1437. a_op_const_reg(list,OP_SHR,osuinttype,loadbitsize-restbits,extra_value_reg)
  1438. end
  1439. else
  1440. begin
  1441. { valuereg contains the lower bits, extra_value_reg the upper }
  1442. a_op_const_reg(list,OP_SHR,osuinttype,sref.startbit,valuereg);
  1443. if is_signed(subsetsize) then
  1444. begin
  1445. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-sref.bitlen+loadbitsize-sref.startbit,extra_value_reg);
  1446. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,extra_value_reg);
  1447. end
  1448. else
  1449. begin
  1450. a_op_const_reg(list,OP_SHL,osuinttype,loadbitsize-sref.startbit,extra_value_reg);
  1451. { mask other bits }
  1452. if (sref.bitlen <> AIntBits) then
  1453. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),extra_value_reg);
  1454. end;
  1455. end;
  1456. { merge }
  1457. a_op_reg_reg(list,OP_OR,osuinttype,extra_value_reg,valuereg);
  1458. end;
  1459. procedure thlcgobj.a_load_subsetref_regs_index(list: TAsmList; subsetsize: tdef; loadbitsize: byte; const sref: tsubsetreference; valuereg: tregister);
  1460. var
  1461. hl: tasmlabel;
  1462. tmpref: treference;
  1463. extra_value_reg,
  1464. tmpreg: tregister;
  1465. begin
  1466. tmpreg:=getintregister(list,osuinttype);
  1467. tmpref:=sref.ref;
  1468. inc(tmpref.offset,loadbitsize div 8);
  1469. extra_value_reg:=getintregister(list,osuinttype);
  1470. if (target_info.endian=endian_big) then
  1471. begin
  1472. { since this is a dynamic index, it's possible that the value }
  1473. { is entirely in valuereg. }
  1474. { get the data in valuereg in the right place }
  1475. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,valuereg);
  1476. if is_signed(subsetsize) then
  1477. begin
  1478. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-loadbitsize,valuereg);
  1479. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg)
  1480. end
  1481. else
  1482. begin
  1483. a_op_const_reg(list,OP_SHR,osuinttype,loadbitsize-sref.bitlen,valuereg);
  1484. if (loadbitsize<>AIntBits) then
  1485. { mask left over bits }
  1486. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1487. end;
  1488. tmpreg := getintregister(list,osuinttype);
  1489. { ensure we don't load anything past the end of the array }
  1490. current_asmdata.getjumplabel(hl);
  1491. a_cmp_const_reg_label(list,osuinttype,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  1492. { the bits in extra_value_reg (if any) start at the most significant bit => }
  1493. { extra_value_reg must be shr by (loadbitsize-sref.bitlen)+(loadsize-sref.bitindex) }
  1494. { => = -(sref.bitindex+(sref.bitlen-2*loadbitsize)) }
  1495. a_op_const_reg_reg(list,OP_ADD,osuinttype,sref.bitlen-2*loadbitsize,sref.bitindexreg,tmpreg);
  1496. a_op_reg_reg(list,OP_NEG,osuinttype,tmpreg,tmpreg);
  1497. { load next "loadbitsize" bits of the array }
  1498. a_load_ref_reg(list,tcgsize2orddef(int_cgsize(loadbitsize div 8)),osuinttype,tmpref,extra_value_reg);
  1499. a_op_reg_reg(list,OP_SHR,osuinttype,tmpreg,extra_value_reg);
  1500. { if there are no bits in extra_value_reg, then sref.bitindex was }
  1501. { < loadsize-sref.bitlen, and therefore tmpreg will now be >= loadsize }
  1502. { => extra_value_reg is now 0 }
  1503. { merge }
  1504. a_op_reg_reg(list,OP_OR,osuinttype,extra_value_reg,valuereg);
  1505. { no need to mask, necessary masking happened earlier on }
  1506. a_label(list,hl);
  1507. end
  1508. else
  1509. begin
  1510. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,valuereg);
  1511. { ensure we don't load anything past the end of the array }
  1512. current_asmdata.getjumplabel(hl);
  1513. a_cmp_const_reg_label(list,osuinttype,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  1514. { Y-x = -(Y-x) }
  1515. a_op_const_reg_reg(list,OP_SUB,osuinttype,loadbitsize,sref.bitindexreg,tmpreg);
  1516. a_op_reg_reg(list,OP_NEG,osuinttype,tmpreg,tmpreg);
  1517. { load next "loadbitsize" bits of the array }
  1518. a_load_ref_reg(list,tcgsize2orddef(int_cgsize(loadbitsize div 8)),osuinttype,tmpref,extra_value_reg);
  1519. { tmpreg is in the range 1..<cpu_bitsize>-1 -> always ok }
  1520. a_op_reg_reg(list,OP_SHL,osuinttype,tmpreg,extra_value_reg);
  1521. { merge }
  1522. a_op_reg_reg(list,OP_OR,osuinttype,extra_value_reg,valuereg);
  1523. a_label(list,hl);
  1524. { sign extend or mask other bits }
  1525. if is_signed(subsetsize) then
  1526. begin
  1527. a_op_const_reg(list,OP_SHL,osuinttype,AIntBits-sref.bitlen,valuereg);
  1528. a_op_const_reg(list,OP_SAR,osuinttype,AIntBits-sref.bitlen,valuereg);
  1529. end
  1530. else
  1531. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1532. end;
  1533. end;
  1534. procedure thlcgobj.a_load_regconst_subsetref_intern(list: TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sref: tsubsetreference; slopt: tsubsetloadopt);
  1535. var
  1536. hl: tasmlabel;
  1537. tmpreg, tmpindexreg, valuereg, extra_value_reg, maskreg: tregister;
  1538. tosreg, fromsreg: tsubsetregister;
  1539. tmpref: treference;
  1540. bitmask: aword;
  1541. loadsize: torddef;
  1542. loadbitsize: byte;
  1543. extra_load: boolean;
  1544. begin
  1545. { the register must be able to contain the requested value }
  1546. if (fromsize.size*8<sref.bitlen) then
  1547. internalerror(2006081613);
  1548. get_subsetref_load_info(sref,loadsize,extra_load);
  1549. loadbitsize:=loadsize.size*8;
  1550. { load the (first part) of the bit sequence }
  1551. valuereg:=getintregister(list,osuinttype);
  1552. a_load_ref_reg(list,loadsize,osuinttype,sref.ref,valuereg);
  1553. { constant offset of bit sequence? }
  1554. if not extra_load then
  1555. begin
  1556. if (sref.bitindexreg=NR_NO) then
  1557. begin
  1558. { use subsetreg routine, it may have been overridden with an optimized version }
  1559. tosreg.subsetreg:=valuereg;
  1560. tosreg.subsetregsize:=def_cgsize(osuinttype);
  1561. { subsetregs always count bits from right to left }
  1562. if (target_info.endian=endian_big) then
  1563. tosreg.startbit:=loadbitsize-(sref.startbit+sref.bitlen)
  1564. else
  1565. tosreg.startbit:=sref.startbit;
  1566. tosreg.bitlen:=sref.bitlen;
  1567. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1568. end
  1569. else
  1570. begin
  1571. if (sref.startbit<>0) then
  1572. internalerror(2006081710);
  1573. { should be handled by normal code and will give wrong result }
  1574. { on x86 for the '1 shl bitlen' below }
  1575. if (sref.bitlen=AIntBits) then
  1576. internalerror(2006081711);
  1577. { zero the bits we have to insert }
  1578. if (slopt<>SL_SETMAX) then
  1579. begin
  1580. maskreg:=getintregister(list,osuinttype);
  1581. if (target_info.endian = endian_big) then
  1582. begin
  1583. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen),maskreg);
  1584. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,maskreg);
  1585. end
  1586. else
  1587. begin
  1588. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1589. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,maskreg);
  1590. end;
  1591. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  1592. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,valuereg);
  1593. end;
  1594. { insert the value }
  1595. if (slopt<>SL_SETZERO) then
  1596. begin
  1597. tmpreg:=getintregister(list,osuinttype);
  1598. if (slopt<>SL_SETMAX) then
  1599. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  1600. else if (sref.bitlen<>AIntBits) then
  1601. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1), tmpreg)
  1602. else
  1603. a_load_const_reg(list,osuinttype,-1,tmpreg);
  1604. if (target_info.endian=endian_big) then
  1605. begin
  1606. a_op_const_reg(list,OP_SHL,osuinttype,loadbitsize-sref.bitlen,tmpreg);
  1607. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1608. begin
  1609. if (loadbitsize<>AIntBits) then
  1610. bitmask:=(((aword(1) shl loadbitsize)-1) xor ((aword(1) shl (loadbitsize-sref.bitlen))-1))
  1611. else
  1612. bitmask:=(high(aword) xor ((aword(1) shl (loadbitsize-sref.bitlen))-1));
  1613. a_op_const_reg(list,OP_AND,osuinttype,bitmask,tmpreg);
  1614. end;
  1615. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,tmpreg);
  1616. end
  1617. else
  1618. begin
  1619. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1620. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1621. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,tmpreg);
  1622. end;
  1623. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,valuereg);
  1624. end;
  1625. end;
  1626. { store back to memory }
  1627. tmpreg:=getintregister(list,loadsize);
  1628. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  1629. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  1630. exit;
  1631. end
  1632. else
  1633. begin
  1634. { load next value }
  1635. extra_value_reg:=getintregister(list,osuinttype);
  1636. tmpref:=sref.ref;
  1637. inc(tmpref.offset,loadbitsize div 8);
  1638. { should maybe be taken out too, can be done more efficiently }
  1639. { on e.g. i386 with shld/shrd }
  1640. if (sref.bitindexreg = NR_NO) then
  1641. begin
  1642. a_load_ref_reg(list,loadsize,osuinttype,tmpref,extra_value_reg);
  1643. fromsreg.subsetreg:=fromreg;
  1644. fromsreg.subsetregsize:=def_cgsize(fromsize);
  1645. tosreg.subsetreg:=valuereg;
  1646. tosreg.subsetregsize:=def_cgsize(osuinttype);
  1647. { transfer first part }
  1648. fromsreg.bitlen:=loadbitsize-sref.startbit;
  1649. tosreg.bitlen:=fromsreg.bitlen;
  1650. if (target_info.endian=endian_big) then
  1651. begin
  1652. { valuereg must contain the upper bits of the value at bits [0..loadbitsize-startbit] }
  1653. { upper bits of the value ... }
  1654. fromsreg.startbit:=sref.bitlen-(loadbitsize-sref.startbit);
  1655. { ... to bit 0 }
  1656. tosreg.startbit:=0
  1657. end
  1658. else
  1659. begin
  1660. { valuereg must contain the lower bits of the value at bits [startbit..loadbitsize] }
  1661. { lower bits of the value ... }
  1662. fromsreg.startbit:=0;
  1663. { ... to startbit }
  1664. tosreg.startbit:=sref.startbit;
  1665. end;
  1666. case slopt of
  1667. SL_SETZERO,
  1668. SL_SETMAX:
  1669. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1670. else
  1671. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1672. end;
  1673. {$ifndef cpuhighleveltarget}
  1674. valuereg:=cg.makeregsize(list,valuereg,def_cgsize(loadsize));
  1675. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1676. {$else}
  1677. tmpreg:=getintregister(list,loadsize);
  1678. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  1679. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  1680. {$endif}
  1681. { transfer second part }
  1682. if (target_info.endian = endian_big) then
  1683. begin
  1684. { extra_value_reg must contain the lower bits of the value at bits }
  1685. { [(loadbitsize-(bitlen-(loadbitsize-startbit)))..loadbitsize] }
  1686. { (loadbitsize-(bitlen-(loadbitsize-startbit))) = 2*loadbitsize }
  1687. { - bitlen - startbit }
  1688. fromsreg.startbit:=0;
  1689. tosreg.startbit:=2*loadbitsize-sref.bitlen-sref.startbit
  1690. end
  1691. else
  1692. begin
  1693. { extra_value_reg must contain the upper bits of the value at bits [0..bitlen-(loadbitsize-startbit)] }
  1694. fromsreg.startbit:=fromsreg.bitlen;
  1695. tosreg.startbit:=0;
  1696. end;
  1697. tosreg.subsetreg:=extra_value_reg;
  1698. fromsreg.bitlen:=sref.bitlen-fromsreg.bitlen;
  1699. tosreg.bitlen:=fromsreg.bitlen;
  1700. case slopt of
  1701. SL_SETZERO,
  1702. SL_SETMAX:
  1703. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1704. else
  1705. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1706. end;
  1707. tmpreg:=getintregister(list,loadsize);
  1708. a_load_reg_reg(list,osuinttype,loadsize,extra_value_reg,tmpreg);
  1709. a_load_reg_ref(list,loadsize,loadsize,tmpreg,tmpref);
  1710. exit;
  1711. end
  1712. else
  1713. begin
  1714. if (sref.startbit <> 0) then
  1715. internalerror(2006081812);
  1716. { should be handled by normal code and will give wrong result }
  1717. { on x86 for the '1 shl bitlen' below }
  1718. if (sref.bitlen = AIntBits) then
  1719. internalerror(2006081713);
  1720. { generate mask to zero the bits we have to insert }
  1721. if (slopt <> SL_SETMAX) then
  1722. begin
  1723. maskreg := getintregister(list,osuinttype);
  1724. if (target_info.endian = endian_big) then
  1725. begin
  1726. a_load_const_reg(list,osuinttype,tcgint(((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen)),maskreg);
  1727. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,maskreg);
  1728. end
  1729. else
  1730. begin
  1731. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1732. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,maskreg);
  1733. end;
  1734. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  1735. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,valuereg);
  1736. end;
  1737. { insert the value }
  1738. if (slopt <> SL_SETZERO) then
  1739. begin
  1740. tmpreg := getintregister(list,osuinttype);
  1741. if (slopt <> SL_SETMAX) then
  1742. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  1743. else if (sref.bitlen <> AIntBits) then
  1744. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  1745. else
  1746. a_load_const_reg(list,osuinttype,-1,tmpreg);
  1747. if (target_info.endian = endian_big) then
  1748. begin
  1749. a_op_const_reg(list,OP_SHL,osuinttype,loadbitsize-sref.bitlen,tmpreg);
  1750. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1751. { mask left over bits }
  1752. a_op_const_reg(list,OP_AND,osuinttype,tcgint(((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen)),tmpreg);
  1753. a_op_reg_reg(list,OP_SHR,osuinttype,sref.bitindexreg,tmpreg);
  1754. end
  1755. else
  1756. begin
  1757. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1758. { mask left over bits }
  1759. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1760. a_op_reg_reg(list,OP_SHL,osuinttype,sref.bitindexreg,tmpreg);
  1761. end;
  1762. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,valuereg);
  1763. end;
  1764. tmpreg:=getintregister(list,loadsize);
  1765. a_load_reg_reg(list,osuinttype,loadsize,valuereg,tmpreg);
  1766. a_load_reg_ref(list,loadsize,loadsize,tmpreg,sref.ref);
  1767. { make sure we do not read/write past the end of the array }
  1768. current_asmdata.getjumplabel(hl);
  1769. a_cmp_const_reg_label(list,osuinttype,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  1770. a_load_ref_reg(list,loadsize,osuinttype,tmpref,extra_value_reg);
  1771. tmpindexreg:=getintregister(list,osuinttype);
  1772. { load current array value }
  1773. if (slopt<>SL_SETZERO) then
  1774. begin
  1775. tmpreg:=getintregister(list,osuinttype);
  1776. if (slopt<>SL_SETMAX) then
  1777. a_load_reg_reg(list,fromsize,osuinttype,fromreg,tmpreg)
  1778. else if (sref.bitlen<>AIntBits) then
  1779. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  1780. else
  1781. a_load_const_reg(list,osuinttype,-1,tmpreg);
  1782. end;
  1783. { generate mask to zero the bits we have to insert }
  1784. if (slopt<>SL_SETMAX) then
  1785. begin
  1786. maskreg:=getintregister(list,osuinttype);
  1787. if (target_info.endian=endian_big) then
  1788. begin
  1789. a_op_const_reg_reg(list,OP_ADD,osuinttype,sref.bitlen-2*loadbitsize,sref.bitindexreg,tmpindexreg);
  1790. a_op_reg_reg(list,OP_NEG,osuinttype,tmpindexreg,tmpindexreg);
  1791. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1792. a_op_reg_reg(list,OP_SHL,osuinttype,tmpindexreg,maskreg);
  1793. end
  1794. else
  1795. begin
  1796. { Y-x = -(x-Y) }
  1797. a_op_const_reg_reg(list,OP_SUB,osuinttype,loadbitsize,sref.bitindexreg,tmpindexreg);
  1798. a_op_reg_reg(list,OP_NEG,osuinttype,tmpindexreg,tmpindexreg);
  1799. a_load_const_reg(list,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1800. a_op_reg_reg(list,OP_SHR,osuinttype,tmpindexreg,maskreg);
  1801. end;
  1802. a_op_reg_reg(list,OP_NOT,osuinttype,maskreg,maskreg);
  1803. a_op_reg_reg(list,OP_AND,osuinttype,maskreg,extra_value_reg);
  1804. end;
  1805. if (slopt<>SL_SETZERO) then
  1806. begin
  1807. if (target_info.endian=endian_big) then
  1808. a_op_reg_reg(list,OP_SHL,osuinttype,tmpindexreg,tmpreg)
  1809. else
  1810. begin
  1811. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1812. a_op_const_reg(list,OP_AND,osuinttype,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1813. a_op_reg_reg(list,OP_SHR,osuinttype,tmpindexreg,tmpreg);
  1814. end;
  1815. a_op_reg_reg(list,OP_OR,osuinttype,tmpreg,extra_value_reg);
  1816. end;
  1817. {$ifndef cpuhighleveltarget}
  1818. extra_value_reg:=cg.makeregsize(list,extra_value_reg,def_cgsize(loadsize));
  1819. a_load_reg_ref(list,loadsize,loadsize,extra_value_reg,tmpref);
  1820. {$else}
  1821. tmpreg:=getintregister(list,loadsize);
  1822. a_load_reg_reg(list,osuinttype,loadsize,extra_value_reg,tmpreg);
  1823. a_load_reg_ref(list,loadsize,loadsize,tmpreg,tmpref);
  1824. {$endif}
  1825. a_label(list,hl);
  1826. end;
  1827. end;
  1828. end;
  1829. procedure thlcgobj.a_load_regconst_subsetreg_intern(list: TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt);
  1830. var
  1831. bitmask: aword;
  1832. tmpreg: tregister;
  1833. subsetregdef: torddef;
  1834. stopbit: byte;
  1835. begin
  1836. subsetregdef:=tcgsize2orddef(sreg.subsetregsize);
  1837. stopbit:=sreg.startbit+sreg.bitlen;
  1838. // on x86(64), 1 shl 32(64) = 1 instead of 0
  1839. if (stopbit<>AIntBits) then
  1840. bitmask:=not(((aword(1) shl stopbit)-1) xor ((aword(1) shl sreg.startbit)-1))
  1841. else
  1842. bitmask:=not(high(aword) xor ((aword(1) shl sreg.startbit)-1));
  1843. if not(slopt in [SL_SETZERO,SL_SETMAX]) then
  1844. begin
  1845. tmpreg:=getintregister(list,subsetregdef);
  1846. a_load_reg_reg(list,fromsize,subsetregdef,fromreg,tmpreg);
  1847. a_op_const_reg(list,OP_SHL,subsetregdef,sreg.startbit,tmpreg);
  1848. if (slopt<>SL_REGNOSRCMASK) then
  1849. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(not(bitmask)),tmpreg);
  1850. end;
  1851. if (slopt<>SL_SETMAX) then
  1852. a_op_const_reg(list,OP_AND,subsetregdef,tcgint(bitmask),sreg.subsetreg);
  1853. case slopt of
  1854. SL_SETZERO : ;
  1855. SL_SETMAX :
  1856. if (sreg.bitlen<>AIntBits) then
  1857. a_op_const_reg(list,OP_OR,subsetregdef,
  1858. tcgint(((aword(1) shl sreg.bitlen)-1) shl sreg.startbit),
  1859. sreg.subsetreg)
  1860. else
  1861. a_load_const_reg(list,subsetregdef,-1,sreg.subsetreg);
  1862. else
  1863. a_op_reg_reg(list,OP_OR,subsetregdef,tmpreg,sreg.subsetreg);
  1864. end;
  1865. end;
  1866. {$pop}
  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]+systems_aix)) 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. if not(target_info.system in systems_aix) then
  2972. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN')
  2973. else
  2974. cg.a_call_name(list,target_info.cprefix+'FPC_SYSTEMMAIN',false)
  2975. end;
  2976. end;
  2977. end;
  2978. procedure thlcgobj.gen_initialize_code(list: TAsmList);
  2979. begin
  2980. { initialize local data like ansistrings }
  2981. case current_procinfo.procdef.proctypeoption of
  2982. potype_unitinit:
  2983. begin
  2984. { this is also used for initialization of variables in a
  2985. program which does not have a globalsymtable }
  2986. if assigned(current_module.globalsymtable) then
  2987. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@initialize_data,list);
  2988. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_data,list);
  2989. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  2990. end;
  2991. { units have seperate code for initilization and finalization }
  2992. potype_unitfinalize: ;
  2993. { program init/final is generated in separate procedure }
  2994. potype_proginit:
  2995. begin
  2996. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  2997. end;
  2998. else
  2999. current_procinfo.procdef.localst.SymList.ForEachCall(@initialize_data,list);
  3000. end;
  3001. { initialises temp. ansi/wide string data }
  3002. if (current_procinfo.procdef.proctypeoption<>potype_exceptfilter) then
  3003. inittempvariables(list);
  3004. {$ifdef OLDREGVARS}
  3005. load_regvars(list,nil);
  3006. {$endif OLDREGVARS}
  3007. end;
  3008. procedure thlcgobj.gen_finalize_code(list: TAsmList);
  3009. var
  3010. old_current_procinfo: tprocinfo;
  3011. begin
  3012. old_current_procinfo:=current_procinfo;
  3013. if (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  3014. begin
  3015. if (current_procinfo.parent.finalize_procinfo<>current_procinfo) then
  3016. exit;
  3017. current_procinfo:=current_procinfo.parent;
  3018. end;
  3019. {$ifdef OLDREGVARS}
  3020. cleanup_regvars(list);
  3021. {$endif OLDREGVARS}
  3022. { finalize temporary data }
  3023. finalizetempvariables(list);
  3024. { finalize local data like ansistrings}
  3025. case current_procinfo.procdef.proctypeoption of
  3026. potype_unitfinalize:
  3027. begin
  3028. { this is also used for initialization of variables in a
  3029. program which does not have a globalsymtable }
  3030. if assigned(current_module.globalsymtable) then
  3031. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@finalize_static_data,list);
  3032. TSymtable(current_module.localsymtable).SymList.ForEachCall(@finalize_static_data,list);
  3033. end;
  3034. { units/progs have separate code for initialization and finalization }
  3035. potype_unitinit: ;
  3036. { program init/final is generated in separate procedure }
  3037. potype_proginit: ;
  3038. else
  3039. current_procinfo.procdef.localst.SymList.ForEachCall(@finalize_local_vars,list);
  3040. end;
  3041. { finalize paras data }
  3042. if assigned(current_procinfo.procdef.parast) and
  3043. not(po_assembler in current_procinfo.procdef.procoptions) then
  3044. current_procinfo.procdef.parast.SymList.ForEachCall(@final_paras,list);
  3045. current_procinfo:=old_current_procinfo;
  3046. end;
  3047. procedure thlcgobj.gen_entry_code(list: TAsmList);
  3048. begin
  3049. { the actual profile code can clobber some registers,
  3050. therefore if the context must be saved, do it before
  3051. the actual call to the profile code
  3052. }
  3053. if (cs_profile in current_settings.moduleswitches) and
  3054. not(po_assembler in current_procinfo.procdef.procoptions) then
  3055. begin
  3056. { non-win32 can call mcout even in main }
  3057. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  3058. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  3059. begin
  3060. g_profilecode(list);
  3061. end;
  3062. end;
  3063. { TODO: create high level version (create compilerprocs in system unit,
  3064. look up procdef, use hlcgobj.a_call_name()) }
  3065. { call startup helpers from main program }
  3066. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  3067. begin
  3068. { initialize units }
  3069. cg.allocallcpuregisters(list);
  3070. if not(current_module.islibrary) then
  3071. cg.a_call_name(list,'FPC_INITIALIZEUNITS',false)
  3072. else
  3073. cg.a_call_name(list,'FPC_LIBINITIALIZEUNITS',false);
  3074. cg.deallocallcpuregisters(list);
  3075. end;
  3076. list.concat(Tai_force_line.Create);
  3077. {$ifdef OLDREGVARS}
  3078. load_regvars(list,nil);
  3079. {$endif OLDREGVARS}
  3080. end;
  3081. procedure thlcgobj.gen_exit_code(list: TAsmList);
  3082. begin
  3083. { TODO: create high level version (create compilerproc in system unit,
  3084. look up procdef, use hlcgobj.a_call_name()) }
  3085. { call __EXIT for main program }
  3086. if (not DLLsource) and
  3087. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  3088. cg.a_call_name(list,'FPC_DO_EXIT',false);
  3089. end;
  3090. procedure thlcgobj.inittempvariables(list: TAsmList);
  3091. var
  3092. hp : ptemprecord;
  3093. href : treference;
  3094. begin
  3095. hp:=tg.templist;
  3096. while assigned(hp) do
  3097. begin
  3098. if assigned(hp^.def) and
  3099. is_managed_type(hp^.def) then
  3100. begin
  3101. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  3102. g_initialize(list,hp^.def,href);
  3103. end;
  3104. hp:=hp^.next;
  3105. end;
  3106. end;
  3107. procedure thlcgobj.initialize_data(p: TObject; arg: pointer);
  3108. var
  3109. OldAsmList : TAsmList;
  3110. hp : tnode;
  3111. begin
  3112. if (tsym(p).typ = localvarsym) and
  3113. { local (procedure or unit) variables only need initialization if
  3114. they are used }
  3115. ((tabstractvarsym(p).refs>0) or
  3116. { managed return symbols must be inited }
  3117. ((tsym(p).typ=localvarsym) and (vo_is_funcret in tlocalvarsym(p).varoptions))
  3118. ) and
  3119. not(vo_is_typed_const in tabstractvarsym(p).varoptions) and
  3120. not(vo_is_external in tabstractvarsym(p).varoptions) and
  3121. not(vo_is_default_var in tabstractvarsym(p).varoptions) and
  3122. (is_managed_type(tabstractvarsym(p).vardef) or
  3123. ((m_iso in current_settings.modeswitches) and (tabstractvarsym(p).vardef.typ=filedef))
  3124. ) then
  3125. begin
  3126. OldAsmList:=current_asmdata.CurrAsmList;
  3127. current_asmdata.CurrAsmList:=TAsmList(arg);
  3128. hp:=cnodeutils.initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner),false);
  3129. firstpass(hp);
  3130. secondpass(hp);
  3131. hp.free;
  3132. current_asmdata.CurrAsmList:=OldAsmList;
  3133. end;
  3134. end;
  3135. procedure thlcgobj.finalizetempvariables(list: TAsmList);
  3136. var
  3137. hp : ptemprecord;
  3138. href : treference;
  3139. begin
  3140. hp:=tg.templist;
  3141. while assigned(hp) do
  3142. begin
  3143. if assigned(hp^.def) and
  3144. is_managed_type(hp^.def) then
  3145. begin
  3146. include(current_procinfo.flags,pi_needs_implicit_finally);
  3147. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  3148. g_finalize(list,hp^.def,href);
  3149. end;
  3150. hp:=hp^.next;
  3151. end;
  3152. end;
  3153. procedure thlcgobj.initialize_regvars(p: TObject; arg: pointer);
  3154. var
  3155. href : treference;
  3156. begin
  3157. if (tsym(p).typ=staticvarsym) then
  3158. begin
  3159. { Static variables can have the initialloc only set to LOC_CxREGISTER
  3160. or LOC_INVALID, for explaination see gen_alloc_symtable (PFV) }
  3161. case tstaticvarsym(p).initialloc.loc of
  3162. LOC_CREGISTER :
  3163. begin
  3164. {$ifndef cpu64bitalu}
  3165. if (tstaticvarsym(p).initialloc.size in [OS_64,OS_S64]) then
  3166. cg64.a_load64_const_reg(TAsmList(arg),0,tstaticvarsym(p).initialloc.register64)
  3167. else
  3168. {$endif not cpu64bitalu}
  3169. a_load_const_reg(TAsmList(arg),tstaticvarsym(p).vardef,0,
  3170. tstaticvarsym(p).initialloc.register);
  3171. end;
  3172. (*
  3173. LOC_CMMREGISTER :
  3174. { clear the whole register }
  3175. cg.a_opmm_reg_reg(TAsmList(arg),OP_XOR,reg_cgsize(tstaticvarsym(p).initialloc.register),
  3176. tstaticvarsym(p).initialloc.register,
  3177. tstaticvarsym(p).initialloc.register,
  3178. nil);
  3179. *)
  3180. LOC_CFPUREGISTER :
  3181. begin
  3182. { initialize fpu regvar by loading from memory }
  3183. reference_reset_symbol(href,
  3184. current_asmdata.RefAsmSymbol(tstaticvarsym(p).mangledname), 0,
  3185. var_align(tstaticvarsym(p).vardef.alignment));
  3186. a_loadfpu_ref_reg(TAsmList(arg), tstaticvarsym(p).vardef,
  3187. tstaticvarsym(p).vardef, href, tstaticvarsym(p).initialloc.register);
  3188. end;
  3189. LOC_INVALID :
  3190. ;
  3191. else
  3192. internalerror(200410124);
  3193. end;
  3194. end;
  3195. end;
  3196. procedure thlcgobj.finalize_sym(asmlist: TAsmList; sym: tsym);
  3197. var
  3198. hp : tnode;
  3199. OldAsmList : TAsmList;
  3200. begin
  3201. include(current_procinfo.flags,pi_needs_implicit_finally);
  3202. OldAsmList:=current_asmdata.CurrAsmList;
  3203. current_asmdata.CurrAsmList:=asmlist;
  3204. hp:=cloadnode.create(sym,sym.owner);
  3205. if (sym.typ=staticvarsym) and (vo_force_finalize in tstaticvarsym(sym).varoptions) then
  3206. include(tloadnode(hp).loadnodeflags,loadnf_isinternal_ignoreconst);
  3207. hp:=cnodeutils.finalize_data_node(hp);
  3208. firstpass(hp);
  3209. secondpass(hp);
  3210. hp.free;
  3211. current_asmdata.CurrAsmList:=OldAsmList;
  3212. end;
  3213. procedure thlcgobj.finalize_local_vars(p: TObject; arg: pointer);
  3214. begin
  3215. if (tsym(p).typ=localvarsym) and
  3216. (tlocalvarsym(p).refs>0) and
  3217. not(vo_is_external in tlocalvarsym(p).varoptions) and
  3218. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  3219. not(vo_is_default_var in tabstractvarsym(p).varoptions) and
  3220. is_managed_type(tlocalvarsym(p).vardef) then
  3221. finalize_sym(TAsmList(arg),tsym(p));
  3222. end;
  3223. procedure thlcgobj.finalize_static_data(p: TObject; arg: pointer);
  3224. var
  3225. i : longint;
  3226. pd : tprocdef;
  3227. begin
  3228. case tsym(p).typ of
  3229. staticvarsym :
  3230. begin
  3231. { local (procedure or unit) variables only need finalization
  3232. if they are used
  3233. }
  3234. if ((tstaticvarsym(p).refs>0) or
  3235. { global (unit) variables always need finalization, since
  3236. they may also be used in another unit
  3237. }
  3238. (tstaticvarsym(p).owner.symtabletype=globalsymtable)) and
  3239. (
  3240. (tstaticvarsym(p).varspez<>vs_const) or
  3241. (vo_force_finalize in tstaticvarsym(p).varoptions)
  3242. ) and
  3243. not(vo_is_funcret in tstaticvarsym(p).varoptions) and
  3244. not(vo_is_external in tstaticvarsym(p).varoptions) and
  3245. is_managed_type(tstaticvarsym(p).vardef) then
  3246. finalize_sym(TAsmList(arg),tsym(p));
  3247. end;
  3248. procsym :
  3249. begin
  3250. for i:=0 to tprocsym(p).ProcdefList.Count-1 do
  3251. begin
  3252. pd:=tprocdef(tprocsym(p).ProcdefList[i]);
  3253. if assigned(pd.localst) and
  3254. (pd.procsym=tprocsym(p)) and
  3255. (pd.localst.symtabletype<>staticsymtable) then
  3256. pd.localst.SymList.ForEachCall(@finalize_static_data,arg);
  3257. end;
  3258. end;
  3259. end;
  3260. end;
  3261. procedure thlcgobj.final_paras(p: TObject; arg: pointer);
  3262. var
  3263. list : TAsmList;
  3264. href : treference;
  3265. hsym : tparavarsym;
  3266. eldef : tdef;
  3267. highloc : tlocation;
  3268. begin
  3269. if not(tsym(p).typ=paravarsym) then
  3270. exit;
  3271. list:=TAsmList(arg);
  3272. if is_managed_type(tparavarsym(p).vardef) then
  3273. begin
  3274. if (tparavarsym(p).varspez=vs_value) then
  3275. begin
  3276. include(current_procinfo.flags,pi_needs_implicit_finally);
  3277. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  3278. if is_open_array(tparavarsym(p).vardef) then
  3279. begin
  3280. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  3281. begin
  3282. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  3283. if not assigned(hsym) then
  3284. internalerror(201003032);
  3285. highloc:=hsym.initialloc
  3286. end
  3287. else
  3288. highloc.loc:=LOC_INVALID;
  3289. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  3290. g_array_rtti_helper(list,eldef,href,highloc,'FPC_FINALIZE_ARRAY');
  3291. end
  3292. else
  3293. g_finalize(list,tparavarsym(p).vardef,href);
  3294. end;
  3295. end;
  3296. { open arrays can contain elements requiring init/final code, so the else has been removed here }
  3297. if (tparavarsym(p).varspez=vs_value) and
  3298. (is_open_array(tparavarsym(p).vardef) or
  3299. is_array_of_const(tparavarsym(p).vardef)) then
  3300. begin
  3301. { cdecl functions don't have a high pointer so it is not possible to generate
  3302. a local copy }
  3303. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  3304. g_releasevaluepara_openarray(list,tarraydef(tparavarsym(p).vardef),tparavarsym(p).localloc);
  3305. end;
  3306. end;
  3307. { generates the code for incrementing the reference count of parameters and
  3308. initialize out parameters }
  3309. { generates the code for incrementing the reference count of parameters and
  3310. initialize out parameters }
  3311. procedure thlcgobj.init_paras(p:TObject;arg:pointer);
  3312. var
  3313. href : treference;
  3314. hsym : tparavarsym;
  3315. eldef : tdef;
  3316. list : TAsmList;
  3317. highloc : tlocation;
  3318. needs_inittable : boolean;
  3319. begin
  3320. list:=TAsmList(arg);
  3321. if (tsym(p).typ=paravarsym) then
  3322. begin
  3323. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  3324. case tparavarsym(p).varspez of
  3325. vs_value :
  3326. if needs_inittable then
  3327. begin
  3328. { variants are already handled by the call to fpc_variant_copy_overwrite if
  3329. they are passed by reference }
  3330. if not((tparavarsym(p).vardef.typ=variantdef) and
  3331. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  3332. begin
  3333. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  3334. if is_open_array(tparavarsym(p).vardef) then
  3335. begin
  3336. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  3337. begin
  3338. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  3339. if not assigned(hsym) then
  3340. internalerror(201003032);
  3341. highloc:=hsym.initialloc
  3342. end
  3343. else
  3344. highloc.loc:=LOC_INVALID;
  3345. { open arrays do not contain correct element count in their rtti,
  3346. the actual count must be passed separately. }
  3347. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  3348. g_array_rtti_helper(list,eldef,href,highloc,'FPC_ADDREF_ARRAY');
  3349. end
  3350. else
  3351. g_incrrefcount(list,tparavarsym(p).vardef,href);
  3352. end;
  3353. end;
  3354. vs_out :
  3355. begin
  3356. if needs_inittable then
  3357. begin
  3358. { we have no idea about the alignment at the callee side,
  3359. and the user also cannot specify "unaligned" here, so
  3360. assume worst case }
  3361. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  3362. if needs_inittable then
  3363. begin
  3364. if is_open_array(tparavarsym(p).vardef) then
  3365. begin
  3366. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  3367. begin
  3368. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  3369. if not assigned(hsym) then
  3370. internalerror(201003032);
  3371. highloc:=hsym.initialloc
  3372. end
  3373. else
  3374. highloc.loc:=LOC_INVALID;
  3375. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  3376. g_array_rtti_helper(list,eldef,href,highloc,'FPC_INITIALIZE_ARRAY');
  3377. end
  3378. else
  3379. g_initialize(list,tparavarsym(p).vardef,href);
  3380. end;
  3381. end;
  3382. end;
  3383. end;
  3384. end;
  3385. end;
  3386. procedure thlcgobj.gen_load_para_value(list: TAsmList);
  3387. var
  3388. i: longint;
  3389. currpara: tparavarsym;
  3390. begin
  3391. if (po_assembler in current_procinfo.procdef.procoptions) or
  3392. { exceptfilters have a single hidden 'parentfp' parameter, which
  3393. is handled by tcg.g_proc_entry. }
  3394. (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  3395. exit;
  3396. { Copy parameters to local references/registers }
  3397. for i:=0 to current_procinfo.procdef.paras.count-1 do
  3398. begin
  3399. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  3400. gen_load_cgpara_loc(list,currpara.vardef,currpara.paraloc[calleeside],currpara.initialloc,paramanager.param_use_paraloc(currpara.paraloc[calleeside]));
  3401. end;
  3402. { generate copies of call by value parameters, must be done before
  3403. the initialization and body is parsed because the refcounts are
  3404. incremented using the local copies }
  3405. current_procinfo.procdef.parast.SymList.ForEachCall(@g_copyvalueparas,list);
  3406. if not(po_assembler in current_procinfo.procdef.procoptions) then
  3407. begin
  3408. { initialize refcounted paras, and trash others. Needed here
  3409. instead of in gen_initialize_code, because when a reference is
  3410. intialised or trashed while the pointer to that reference is kept
  3411. in a regvar, we add a register move and that one again has to
  3412. come after the parameter loading code as far as the register
  3413. allocator is concerned }
  3414. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  3415. end;
  3416. end;
  3417. procedure thlcgobj.g_copyvalueparas(p: TObject; arg: pointer);
  3418. var
  3419. href : treference;
  3420. hreg : tregister;
  3421. list : TAsmList;
  3422. hsym : tparavarsym;
  3423. l : longint;
  3424. highloc,
  3425. localcopyloc : tlocation;
  3426. begin
  3427. list:=TAsmList(arg);
  3428. if (tsym(p).typ=paravarsym) and
  3429. (tparavarsym(p).varspez=vs_value) and
  3430. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  3431. begin
  3432. { we have no idea about the alignment at the caller side }
  3433. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  3434. if is_open_array(tparavarsym(p).vardef) or
  3435. is_array_of_const(tparavarsym(p).vardef) then
  3436. begin
  3437. { cdecl functions don't have a high pointer so it is not possible to generate
  3438. a local copy }
  3439. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  3440. begin
  3441. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  3442. begin
  3443. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  3444. if not assigned(hsym) then
  3445. internalerror(2011020506);
  3446. highloc:=hsym.initialloc
  3447. end
  3448. else
  3449. highloc.loc:=LOC_INVALID;
  3450. hreg:=getaddressregister(list,voidpointertype);
  3451. if not is_packed_array(tparavarsym(p).vardef) then
  3452. g_copyvaluepara_openarray(list,href,highloc,tarraydef(tparavarsym(p).vardef),hreg)
  3453. else
  3454. internalerror(2011020507);
  3455. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  3456. a_load_reg_loc(list,tparavarsym(p).vardef,tparavarsym(p).vardef,hreg,tparavarsym(p).initialloc);
  3457. end;
  3458. end
  3459. else
  3460. begin
  3461. { Allocate space for the local copy }
  3462. l:=tparavarsym(p).getsize;
  3463. localcopyloc.loc:=LOC_REFERENCE;
  3464. localcopyloc.size:=int_cgsize(l);
  3465. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  3466. { Copy data }
  3467. if is_shortstring(tparavarsym(p).vardef) then
  3468. begin
  3469. { this code is only executed before the code for the body and the entry/exit code is generated
  3470. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  3471. }
  3472. include(current_procinfo.flags,pi_do_call);
  3473. g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef))
  3474. end
  3475. else if tparavarsym(p).vardef.typ=variantdef then
  3476. begin
  3477. { this code is only executed before the code for the body and the entry/exit code is generated
  3478. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  3479. }
  3480. include(current_procinfo.flags,pi_do_call);
  3481. g_copyvariant(list,href,localcopyloc.reference,tvariantdef(tparavarsym(p).vardef))
  3482. end
  3483. else
  3484. begin
  3485. { pass proper alignment info }
  3486. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  3487. g_concatcopy(list,tparavarsym(p).vardef,href,localcopyloc.reference);
  3488. end;
  3489. { update localloc of varsym }
  3490. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  3491. tparavarsym(p).localloc:=localcopyloc;
  3492. tparavarsym(p).initialloc:=localcopyloc;
  3493. end;
  3494. end;
  3495. end;
  3496. procedure thlcgobj.gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint);
  3497. begin
  3498. case l.loc of
  3499. (*
  3500. LOC_MMREGISTER,
  3501. LOC_CMMREGISTER:
  3502. case cgpara.location^.loc of
  3503. LOC_REFERENCE,
  3504. LOC_CREFERENCE,
  3505. LOC_MMREGISTER,
  3506. LOC_CMMREGISTER,
  3507. LOC_REGISTER,
  3508. LOC_CREGISTER :
  3509. cg.a_loadmm_reg_cgpara(list,locsize,l.register,cgpara,mms_movescalar);
  3510. LOC_FPUREGISTER,
  3511. LOC_CFPUREGISTER:
  3512. begin
  3513. tmploc:=l;
  3514. location_force_fpureg(list,tmploc,false);
  3515. cg.a_loadfpu_reg_cgpara(list,tmploc.size,tmploc.register,cgpara);
  3516. end;
  3517. else
  3518. internalerror(200204249);
  3519. end;
  3520. *)
  3521. LOC_FPUREGISTER,
  3522. LOC_CFPUREGISTER:
  3523. case cgpara.location^.loc of
  3524. (*
  3525. LOC_MMREGISTER,
  3526. LOC_CMMREGISTER:
  3527. begin
  3528. tmploc:=l;
  3529. location_force_mmregscalar(list,tmploc,false);
  3530. cg.a_loadmm_reg_cgpara(list,tmploc.size,tmploc.register,cgpara,mms_movescalar);
  3531. end;
  3532. *)
  3533. { Some targets pass floats in normal registers }
  3534. LOC_REGISTER,
  3535. LOC_CREGISTER,
  3536. LOC_REFERENCE,
  3537. LOC_CREFERENCE,
  3538. LOC_FPUREGISTER,
  3539. LOC_CFPUREGISTER:
  3540. a_loadfpu_reg_cgpara(list,size,l.register,cgpara);
  3541. else
  3542. internalerror(2011010210);
  3543. end;
  3544. LOC_REFERENCE,
  3545. LOC_CREFERENCE:
  3546. case cgpara.location^.loc of
  3547. (*
  3548. LOC_MMREGISTER,
  3549. LOC_CMMREGISTER:
  3550. cg.a_loadmm_ref_cgpara(list,locsize,l.reference,cgpara,mms_movescalar);
  3551. *)
  3552. { Some targets pass floats in normal registers }
  3553. LOC_REGISTER,
  3554. LOC_CREGISTER,
  3555. LOC_REFERENCE,
  3556. LOC_CREFERENCE,
  3557. LOC_FPUREGISTER,
  3558. LOC_CFPUREGISTER:
  3559. a_loadfpu_ref_cgpara(list,size,l.reference,cgpara);
  3560. else
  3561. internalerror(2011010211);
  3562. end;
  3563. LOC_REGISTER,
  3564. LOC_CREGISTER :
  3565. a_load_loc_cgpara(list,size,l,cgpara);
  3566. else
  3567. internalerror(2011010212);
  3568. end;
  3569. end;
  3570. procedure thlcgobj.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  3571. begin
  3572. { do nothing by default }
  3573. end;
  3574. procedure thlcgobj.gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);
  3575. begin
  3576. { Handle Floating point types differently
  3577. This doesn't depend on emulator settings, emulator settings should
  3578. be handled by cpupara }
  3579. if (vardef.typ=floatdef) or
  3580. { some ABIs return certain records in an fpu register }
  3581. (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) or
  3582. (assigned(cgpara.location) and
  3583. (cgpara.Location^.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) then
  3584. begin
  3585. gen_loadfpu_loc_cgpara(list,vardef,l,cgpara,vardef.size);
  3586. exit;
  3587. end;
  3588. case l.loc of
  3589. LOC_CONSTANT,
  3590. LOC_REGISTER,
  3591. LOC_CREGISTER,
  3592. LOC_REFERENCE,
  3593. LOC_CREFERENCE :
  3594. begin
  3595. a_load_loc_cgpara(list,vardef,l,cgpara);
  3596. end;
  3597. (*
  3598. LOC_MMREGISTER,
  3599. LOC_CMMREGISTER:
  3600. begin
  3601. case l.size of
  3602. OS_F32,
  3603. OS_F64:
  3604. cg.a_loadmm_loc_cgpara(list,l,cgpara,mms_movescalar);
  3605. else
  3606. cg.a_loadmm_loc_cgpara(list,l,cgpara,nil);
  3607. end;
  3608. end;
  3609. *)
  3610. else
  3611. internalerror(2011010213);
  3612. end;
  3613. end;
  3614. procedure thlcgobj.gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  3615. var
  3616. href : treference;
  3617. begin
  3618. para.check_simple_location;
  3619. { skip e.g. empty records }
  3620. if (para.location^.loc = LOC_VOID) then
  3621. exit;
  3622. case destloc.loc of
  3623. LOC_REFERENCE :
  3624. begin
  3625. { If the parameter location is reused we don't need to copy
  3626. anything }
  3627. if not reusepara then
  3628. begin
  3629. reference_reset_base(href,para.location^.reference.index,para.location^.reference.offset,para.alignment);
  3630. a_load_ref_ref(list,para.def,para.def,href,destloc.reference);
  3631. end;
  3632. end;
  3633. { TODO other possible locations }
  3634. else
  3635. internalerror(2011010308);
  3636. end;
  3637. end;
  3638. procedure thlcgobj.gen_load_return_value(list: TAsmList);
  3639. var
  3640. ressym : tabstractnormalvarsym;
  3641. funcretloc : TCGPara;
  3642. begin
  3643. { Is the loading needed? }
  3644. if is_void(current_procinfo.procdef.returndef) or
  3645. (
  3646. (po_assembler in current_procinfo.procdef.procoptions) and
  3647. (not(assigned(current_procinfo.procdef.funcretsym)) or
  3648. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  3649. ) then
  3650. exit;
  3651. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  3652. { constructors return self }
  3653. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  3654. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.Find('self'))
  3655. else
  3656. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  3657. if (ressym.refs>0) or
  3658. is_managed_type(ressym.vardef) then
  3659. begin
  3660. { was: don't do anything if funcretloc.loc in [LOC_INVALID,LOC_REFERENCE] }
  3661. if not paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  3662. gen_load_loc_cgpara(list,ressym.vardef,ressym.localloc,funcretloc);
  3663. end
  3664. else
  3665. gen_load_uninitialized_function_result(list,current_procinfo.procdef,ressym.vardef,funcretloc)
  3666. end;
  3667. procedure thlcgobj.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  3668. begin
  3669. { add the procedure to the al_procedures }
  3670. maybe_new_object_file(current_asmdata.asmlists[al_procedures]);
  3671. new_section(current_asmdata.asmlists[al_procedures],sec_code,lower(pd.mangledname),getprocalign);
  3672. current_asmdata.asmlists[al_procedures].concatlist(code);
  3673. { save local data (casetable) also in the same file }
  3674. if assigned(data) and
  3675. (not data.empty) then
  3676. current_asmdata.asmlists[al_procedures].concatlist(data);
  3677. end;
  3678. procedure thlcgobj.g_call_system_proc(list: TAsmList; const procname: string);
  3679. var
  3680. srsym: tsym;
  3681. pd: tprocdef;
  3682. begin
  3683. srsym:=tsym(systemunit.find(procname));
  3684. if not assigned(srsym) and
  3685. (cs_compilesystem in current_settings.moduleswitches) then
  3686. srsym:=tsym(systemunit.Find(upper(procname)));
  3687. if not assigned(srsym) or
  3688. (srsym.typ<>procsym) then
  3689. Message1(cg_f_unknown_compilerproc,procname);
  3690. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  3691. a_call_name(list,pd,pd.mangledname,false);
  3692. end;
  3693. end.