hlcgobj.pas 199 KB

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