hlcgobj.pas 193 KB

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