hlcgobj.pas 234 KB

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