hlcgobj.pas 220 KB

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