hlcgobj.pas 190 KB

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