hlcgobj.pas 217 KB

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