hlcgobj.pas 241 KB

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