hlcgobj.pas 229 KB

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