hlcgobj.pas 201 KB

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