hlcgobj.pas 200 KB

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