hlcgobj.pas 201 KB

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