hlcgobj.pas 236 KB

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