hlcgobj.pas 188 KB

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