hlcgobj.pas 199 KB

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