hlcgobj.pas 230 KB

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