hlcgobj.pas 200 KB

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