hlcgobj.pas 202 KB

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