hlcgobj.pas 238 KB

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