hlcgobj.pas 193 KB

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