hlcgobj.pas 200 KB

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