hlcgobj.pas 214 KB

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