hlcgobj.pas 224 KB

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