cgobj.pas 190 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445
  1. {
  2. Copyright (c) 1998-2005 by Florian Klaempfl
  3. Member of the Free Pascal development team
  4. This unit implements the basic 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. Abstreact code generator unit. This contains the base class
  20. to implement for all new supported processors.
  21. WARNING: None of the routines implemented in these modules,
  22. or their descendants, should use the temp. allocator, as
  23. these routines may be called inside genentrycode, and the
  24. stack frame is already setup!
  25. }
  26. unit cgobj;
  27. {$i fpcdefs.inc}
  28. interface
  29. uses
  30. cclasses,globtype,constexp,
  31. cpubase,cgbase,cgutils,parabase,
  32. aasmbase,aasmtai,aasmdata,aasmcpu,
  33. symconst,symtype,symdef,rgobj
  34. ;
  35. type
  36. talignment = (AM_NATURAL,AM_NONE,AM_2BYTE,AM_4BYTE,AM_8BYTE);
  37. tsubsetloadopt = (SL_REG,SL_REGNOSRCMASK,SL_SETZERO,SL_SETMAX);
  38. {# @abstract(Abstract code generator)
  39. This class implements an abstract instruction generator. Some of
  40. the methods of this class are generic, while others must
  41. be overridden for all new processors which will be supported
  42. by Free Pascal. For 32-bit processors, the base class
  43. should be @link(tcg64f32) and not @var(tcg).
  44. }
  45. tcg = class
  46. public
  47. { how many times is this current code executed }
  48. executionweight : longint;
  49. alignment : talignment;
  50. rg : array[tregistertype] of trgobj;
  51. {$ifdef flowgraph}
  52. aktflownode:word;
  53. {$endif}
  54. {************************************************}
  55. { basic routines }
  56. constructor create;
  57. {# Initialize the register allocators needed for the codegenerator.}
  58. procedure init_register_allocators;virtual;
  59. {# Clean up the register allocators needed for the codegenerator.}
  60. procedure done_register_allocators;virtual;
  61. {# 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. }
  62. procedure set_regalloc_live_range_direction(dir: TRADirection);
  63. {$ifdef flowgraph}
  64. procedure init_flowgraph;
  65. procedure done_flowgraph;
  66. {$endif}
  67. {# Gets a register suitable to do integer operations on.}
  68. function getintregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  69. {# Gets a register suitable to do integer operations on.}
  70. function getaddressregister(list:TAsmList):Tregister;virtual;
  71. function getfpuregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  72. function getmmregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  73. function getflagregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  74. {Does the generic cg need SIMD registers, like getmmxregister? Or should
  75. the cpu specific child cg object have such a method?}
  76. procedure add_reg_instruction(instr:Tai;r:tregister);virtual;
  77. procedure add_move_instruction(instr:Taicpu);virtual;
  78. function uses_registers(rt:Tregistertype):boolean;virtual;
  79. {# Get a specific register.}
  80. procedure getcpuregister(list:TAsmList;r:Tregister);virtual;
  81. procedure ungetcpuregister(list:TAsmList;r:Tregister);virtual;
  82. {# Get multiple registers specified.}
  83. procedure alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  84. {# Free multiple registers specified.}
  85. procedure dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  86. procedure allocallcpuregisters(list:TAsmList);virtual;
  87. procedure deallocallcpuregisters(list:TAsmList);virtual;
  88. procedure do_register_allocation(list:TAsmList;headertai:tai);virtual;
  89. procedure translate_register(var reg : tregister);
  90. function makeregsize(list:TAsmList;reg:Tregister;size:Tcgsize):Tregister;
  91. {# Emit a label to the instruction stream. }
  92. procedure a_label(list : TAsmList;l : tasmlabel);virtual;
  93. {# Allocates register r by inserting a pai_realloc record }
  94. procedure a_reg_alloc(list : TAsmList;r : tregister);
  95. {# Deallocates register r by inserting a pa_regdealloc record}
  96. procedure a_reg_dealloc(list : TAsmList;r : tregister);
  97. { Synchronize register, make sure it is still valid }
  98. procedure a_reg_sync(list : TAsmList;r : tregister);
  99. {# Pass a parameter, which is located in a register, to a routine.
  100. This routine should push/send the parameter to the routine, as
  101. required by the specific processor ABI and routine modifiers.
  102. It must generate register allocation information for the cgpara in
  103. case it consists of cpuregisters.
  104. @param(size size of the operand in the register)
  105. @param(r register source of the operand)
  106. @param(cgpara where the parameter will be stored)
  107. }
  108. procedure a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : TCGPara);virtual;
  109. {# Pass a parameter, which is a constant, to a routine.
  110. A generic version is provided. This routine should
  111. be overridden for optimization purposes if the cpu
  112. permits directly sending this type of parameter.
  113. It must generate register allocation information for the cgpara in
  114. case it consists of cpuregisters.
  115. @param(size size of the operand in constant)
  116. @param(a value of constant to send)
  117. @param(cgpara where the parameter will be stored)
  118. }
  119. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : TCGPara);virtual;
  120. {# Pass the value of a parameter, which is located in memory, to a routine.
  121. A generic version is provided. This routine should
  122. be overridden for optimization purposes if the cpu
  123. permits directly sending this type of parameter.
  124. It must generate register allocation information for the cgpara in
  125. case it consists of cpuregisters.
  126. @param(size size of the operand in constant)
  127. @param(r Memory reference of value to send)
  128. @param(cgpara where the parameter will be stored)
  129. }
  130. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : TCGPara);virtual;
  131. {# Pass the value of a parameter, which can be located either in a register or memory location,
  132. to a routine.
  133. A generic version is provided.
  134. @param(l location of the operand to send)
  135. @param(nr parameter number (starting from one) of routine (from left to right))
  136. @param(cgpara where the parameter will be stored)
  137. }
  138. procedure a_load_loc_cgpara(list : TAsmList;const l : tlocation;const cgpara : TCGPara);
  139. {# Pass the address of a reference to a routine. This routine
  140. will calculate the address of the reference, and pass this
  141. calculated address as a parameter.
  142. It must generate register allocation information for the cgpara in
  143. case it consists of cpuregisters.
  144. A generic version is provided. This routine should
  145. be overridden for optimization purposes if the cpu
  146. permits directly sending this type of parameter.
  147. @param(r reference to get address from)
  148. @param(nr parameter number (starting from one) of routine (from left to right))
  149. }
  150. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : TCGPara);virtual;
  151. {# Load a cgparaloc into a memory reference.
  152. It must generate register allocation information for the cgpara in
  153. case it consists of cpuregisters.
  154. @param(paraloc the source parameter sublocation)
  155. @param(ref the destination reference)
  156. @param(sizeleft indicates the total number of bytes left in all of
  157. the remaining sublocations of this parameter (the current
  158. sublocation and all of the sublocations coming after it).
  159. In case this location is also a reference, it is assumed
  160. to be the final part sublocation of the parameter and that it
  161. contains all of the "sizeleft" bytes).)
  162. @param(align the alignment of the paraloc in case it's a reference)
  163. }
  164. procedure a_load_cgparaloc_ref(list : TAsmList;const paraloc : TCGParaLocation;const ref : treference;sizeleft : tcgint;align : longint);
  165. {# Load a cgparaloc into any kind of register (int, fp, mm).
  166. @param(regsize the size of the destination register)
  167. @param(paraloc the source parameter sublocation)
  168. @param(reg the destination register)
  169. @param(align the alignment of the paraloc in case it's a reference)
  170. }
  171. procedure a_load_cgparaloc_anyreg(list : TAsmList;regsize : tcgsize;const paraloc : TCGParaLocation;reg : tregister;align : longint);
  172. { Remarks:
  173. * If a method specifies a size you have only to take care
  174. of that number of bits, i.e. load_const_reg with OP_8 must
  175. only load the lower 8 bit of the specified register
  176. the rest of the register can be undefined
  177. if necessary the compiler will call a method
  178. to zero or sign extend the register
  179. * The a_load_XX_XX with OP_64 needn't to be
  180. implemented for 32 bit
  181. processors, the code generator takes care of that
  182. * the addr size is for work with the natural pointer
  183. size
  184. * the procedures without fpu/mm are only for integer usage
  185. * normally the first location is the source and the
  186. second the destination
  187. }
  188. {# Emits instruction to call the method specified by symbol name.
  189. This routine must be overridden for each new target cpu.
  190. There is no a_call_ref because loading the reference will use
  191. a temp register on most cpu's resulting in conflicts with the
  192. registers used for the parameters (PFV)
  193. }
  194. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);virtual; abstract;
  195. procedure a_call_reg(list : TAsmList;reg : tregister);virtual; abstract;
  196. procedure a_call_ref(list : TAsmList;ref : treference);virtual; abstract;
  197. { same as a_call_name, might be overridden on certain architectures to emit
  198. static calls without usage of a got trampoline }
  199. procedure a_call_name_static(list : TAsmList;const s : string);virtual;
  200. { move instructions }
  201. procedure a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);virtual; abstract;
  202. procedure a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : treference);virtual;
  203. procedure a_load_const_loc(list : TAsmList;a : tcgint;const loc : tlocation);
  204. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  205. procedure a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual;
  206. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  207. procedure a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  208. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  209. procedure a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual;
  210. procedure a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);virtual;
  211. procedure a_load_loc_reg(list : TAsmList;tosize: tcgsize; const loc: tlocation; reg : tregister);
  212. procedure a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  213. procedure a_load_loc_subsetreg(list : TAsmList;subsetsize: tcgsize; const loc: tlocation; const sreg : tsubsetregister);
  214. procedure a_load_loc_subsetref(list : TAsmList;subsetsize: tcgsize; const loc: tlocation; const sref : tsubsetreference);
  215. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);virtual; abstract;
  216. procedure a_load_subsetreg_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; destreg: tregister); virtual;
  217. procedure a_load_reg_subsetreg(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister); virtual;
  218. procedure a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsreg, tosreg: tsubsetregister); virtual;
  219. procedure a_load_subsetreg_ref(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; const destref: treference); virtual;
  220. procedure a_load_ref_subsetreg(list : TAsmList; fromsize, subsetsize: tcgsize; const fromref: treference; const sreg: tsubsetregister); virtual;
  221. procedure a_load_const_subsetreg(list: TAsmlist; subsetsize: tcgsize; a: tcgint; const sreg: tsubsetregister); virtual;
  222. procedure a_load_subsetreg_loc(list: TAsmlist; subsetsize: tcgsize; const sreg: tsubsetregister; const loc: tlocation); virtual;
  223. procedure a_load_subsetref_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sref: tsubsetreference; destreg: tregister); virtual;
  224. procedure a_load_reg_subsetref(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sref: tsubsetreference);
  225. procedure a_load_subsetref_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsref, tosref: tsubsetreference); virtual;
  226. procedure a_load_subsetref_ref(list : TAsmList; subsetsize, tosize: tcgsize; const sref: tsubsetreference; const destref: treference); virtual;
  227. procedure a_load_ref_subsetref(list : TAsmList; fromsize, subsetsize: tcgsize; const fromref: treference; const sref: tsubsetreference); virtual;
  228. procedure a_load_const_subsetref(list: TAsmlist; subsetsize: tcgsize; a: tcgint; const sref: tsubsetreference); virtual;
  229. procedure a_load_subsetref_loc(list: TAsmlist; subsetsize: tcgsize; const sref: tsubsetreference; const loc: tlocation); virtual;
  230. procedure a_load_subsetref_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsref: tsubsetreference; const tosreg: tsubsetregister); virtual;
  231. procedure a_load_subsetreg_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsreg: tsubsetregister; const tosref: tsubsetreference); virtual;
  232. { bit test instructions }
  233. procedure a_bit_test_reg_reg_reg(list : TAsmList; bitnumbersize,valuesize,destsize: tcgsize;bitnumber,value,destreg: tregister); virtual;
  234. procedure a_bit_test_const_ref_reg(list: TAsmList; destsize: tcgsize; bitnumber: tcgint; const ref: treference; destreg: tregister); virtual;
  235. procedure a_bit_test_const_reg_reg(list: TAsmList; setregsize, destsize: tcgsize; bitnumber: tcgint; setreg, destreg: tregister); virtual;
  236. procedure a_bit_test_const_subsetreg_reg(list: TAsmList; setregsize, destsize: tcgsize; bitnumber: tcgint; const setreg: tsubsetregister; destreg: tregister); virtual;
  237. procedure a_bit_test_reg_ref_reg(list: TAsmList; bitnumbersize, destsize: tcgsize; bitnumber: tregister; const ref: treference; destreg: tregister); virtual;
  238. procedure a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, destsize: tcgsize; bitnumber: tregister; const loc: tlocation; destreg: tregister);
  239. procedure a_bit_test_const_loc_reg(list: TAsmList; destsize: tcgsize; bitnumber: tcgint; const loc: tlocation; destreg: tregister);
  240. { bit set/clear instructions }
  241. procedure a_bit_set_reg_reg(list : TAsmList; doset: boolean; bitnumbersize, destsize: tcgsize; bitnumber,dest: tregister); virtual;
  242. procedure a_bit_set_const_ref(list: TAsmList; doset: boolean;destsize: tcgsize; bitnumber: tcgint; const ref: treference); virtual;
  243. procedure a_bit_set_const_reg(list: TAsmList; doset: boolean; destsize: tcgsize; bitnumber: tcgint; destreg: tregister); virtual;
  244. procedure a_bit_set_const_subsetreg(list: TAsmList; doset: boolean; destsize: tcgsize; bitnumber: tcgint; const destreg: tsubsetregister); virtual;
  245. procedure a_bit_set_reg_ref(list: TAsmList; doset: boolean; bitnumbersize: tcgsize; bitnumber: tregister; const ref: treference); virtual;
  246. procedure a_bit_set_reg_loc(list: TAsmList; doset: boolean; bitnumbersize: tcgsize; bitnumber: tregister; const loc: tlocation);
  247. procedure a_bit_set_const_loc(list: TAsmList; doset: boolean; bitnumber: tcgint; const loc: tlocation);
  248. { bit scan instructions }
  249. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: tcgsize; src, dst: TRegister); virtual; abstract;
  250. { fpu move instructions }
  251. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize:tcgsize; reg1, reg2: tregister); virtual; abstract;
  252. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  253. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  254. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tcgsize; const ref1,ref2: treference);
  255. procedure a_loadfpu_loc_reg(list: TAsmList; tosize: tcgsize; const loc: tlocation; const reg: tregister);
  256. procedure a_loadfpu_reg_loc(list: TAsmList; fromsize: tcgsize; const reg: tregister; const loc: tlocation);
  257. procedure a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);virtual;
  258. procedure a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);virtual;
  259. { vector register move instructions }
  260. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); virtual;
  261. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  262. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual;
  263. procedure a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  264. procedure a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  265. procedure a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  266. procedure a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  267. procedure a_loadmm_loc_cgpara(list: TAsmList; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  268. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle); virtual;
  269. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  270. procedure a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  271. procedure a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  272. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle); virtual;
  273. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize; mmreg, intreg: tregister; shuffle : pmmshuffle); virtual;
  274. { basic arithmetic operations }
  275. { note: for operators which require only one argument (not, neg), use }
  276. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  277. { that in this case the *second* operand is used as both source and }
  278. { destination (JM) }
  279. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); virtual; abstract;
  280. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); virtual;
  281. procedure a_op_const_subsetreg(list : TAsmList; Op : TOpCG; size, subsetsize : TCGSize; a : tcgint; const sreg: tsubsetregister); virtual;
  282. procedure a_op_const_subsetref(list : TAsmList; Op : TOpCG; size, subsetsize : TCGSize; a : tcgint; const sref: tsubsetreference); virtual;
  283. procedure a_op_const_loc(list : TAsmList; Op: TOpCG; a: tcgint; const loc: tlocation);
  284. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  285. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  286. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  287. procedure a_op_reg_subsetreg(list : TAsmList; Op : TOpCG; opsize, subsetsize : TCGSize; reg: TRegister; const sreg: tsubsetregister); virtual;
  288. procedure a_op_reg_subsetref(list : TAsmList; Op : TOpCG; opsize, subsetsize : TCGSize; reg: TRegister; const sref: tsubsetreference); virtual;
  289. procedure a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  290. procedure a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  291. { trinary operations for processors that support them, 'emulated' }
  292. { on others. None with "ref" arguments since I don't think there }
  293. { are any processors that support it (JM) }
  294. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister); virtual;
  295. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister); virtual;
  296. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  297. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  298. { comparison operations }
  299. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  300. l : tasmlabel); virtual;
  301. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  302. l : tasmlabel); virtual;
  303. procedure a_cmp_const_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; a: tcgint; const loc: tlocation;
  304. l : tasmlabel);
  305. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  306. procedure a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  307. procedure a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  308. procedure a_cmp_subsetreg_reg_label(list : TAsmList; subsetsize, cmpsize : tcgsize; cmp_op : topcmp; const sreg: tsubsetregister; reg : tregister; l : tasmlabel); virtual;
  309. procedure a_cmp_subsetref_reg_label(list : TAsmList; subsetsize, cmpsize : tcgsize; cmp_op : topcmp; const sref: tsubsetreference; reg : tregister; l : tasmlabel); virtual;
  310. procedure a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  311. procedure a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  312. procedure a_cmp_ref_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  313. l : tasmlabel);
  314. procedure a_jmp_name(list : TAsmList;const s : string); virtual; abstract;
  315. procedure a_jmp_always(list : TAsmList;l: tasmlabel); virtual; abstract;
  316. {$ifdef cpuflags}
  317. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); virtual; abstract;
  318. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  319. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  320. }
  321. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  322. procedure g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  323. {$endif cpuflags}
  324. {
  325. This routine tries to optimize the op_const_reg/ref opcode, and should be
  326. called at the start of a_op_const_reg/ref. It returns the actual opcode
  327. to emit, and the constant value to emit. This function can opcode OP_NONE to
  328. remove the opcode and OP_MOVE to replace it with a simple load
  329. @param(op The opcode to emit, returns the opcode which must be emitted)
  330. @param(a The constant which should be emitted, returns the constant which must
  331. be emitted)
  332. }
  333. procedure optimize_op_const(var op: topcg; var a : tcgint);virtual;
  334. {#
  335. This routine is used in exception management nodes. It should
  336. save the exception reason currently in the FUNCTION_RETURN_REG. The
  337. save should be done either to a temp (pointed to by href).
  338. or on the stack (pushing the value on the stack).
  339. The size of the value to save is OS_S32. The default version
  340. saves the exception reason to a temp. memory area.
  341. }
  342. procedure g_exception_reason_save(list : TAsmList; const href : treference);virtual;
  343. {#
  344. This routine is used in exception management nodes. It should
  345. save the exception reason constant. The
  346. save should be done either to a temp (pointed to by href).
  347. or on the stack (pushing the value on the stack).
  348. The size of the value to save is OS_S32. The default version
  349. saves the exception reason to a temp. memory area.
  350. }
  351. procedure g_exception_reason_save_const(list : TAsmList; const href : treference; a: tcgint);virtual;
  352. {#
  353. This routine is used in exception management nodes. It should
  354. load the exception reason to the FUNCTION_RETURN_REG. The saved value
  355. should either be in the temp. area (pointed to by href , href should
  356. *NOT* be freed) or on the stack (the value should be popped).
  357. The size of the value to save is OS_S32. The default version
  358. saves the exception reason to a temp. memory area.
  359. }
  360. procedure g_exception_reason_load(list : TAsmList; const href : treference);virtual;
  361. procedure g_maybe_testself(list : TAsmList;reg:tregister);
  362. procedure g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  363. {# This should emit the opcode to copy len bytes from the source
  364. to destination.
  365. It must be overridden for each new target processor.
  366. @param(source Source reference of copy)
  367. @param(dest Destination reference of copy)
  368. }
  369. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);virtual; abstract;
  370. {# This should emit the opcode to copy len bytes from the an unaligned source
  371. to destination.
  372. It must be overridden for each new target processor.
  373. @param(source Source reference of copy)
  374. @param(dest Destination reference of copy)
  375. }
  376. procedure g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);virtual;
  377. {# This should emit the opcode to a shortrstring from the source
  378. to destination.
  379. @param(source Source reference of copy)
  380. @param(dest Destination reference of copy)
  381. }
  382. procedure g_copyshortstring(list : TAsmList;const source,dest : treference;len:byte);
  383. procedure g_copyvariant(list : TAsmList;const source,dest : treference);
  384. procedure g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);
  385. procedure g_decrrefcount(list : TAsmList;t: tdef; const ref: treference);
  386. procedure g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation;
  387. const name: string);
  388. procedure g_initialize(list : TAsmList;t : tdef;const ref : treference);
  389. procedure g_finalize(list : TAsmList;t : tdef;const ref : treference);
  390. {# Generates range checking code. It is to note
  391. that this routine does not need to be overridden,
  392. as it takes care of everything.
  393. @param(p Node which contains the value to check)
  394. @param(todef Type definition of node to range check)
  395. }
  396. procedure g_rangecheck(list: TAsmList; const l:tlocation; fromdef,todef: tdef); virtual;
  397. {# Generates overflow checking code for a node }
  398. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef); virtual;abstract;
  399. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);virtual;
  400. procedure g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:tcgint;destreg:tregister);virtual;
  401. procedure g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);virtual;
  402. {# Emits instructions when compilation is done in profile
  403. mode (this is set as a command line option). The default
  404. behavior does nothing, should be overridden as required.
  405. }
  406. procedure g_profilecode(list : TAsmList);virtual;
  407. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  408. @param(size Number of bytes to allocate)
  409. }
  410. procedure g_stackpointer_alloc(list : TAsmList;size : longint);virtual; abstract;
  411. {# Emits instruction for allocating the locals in entry
  412. code of a routine. This is one of the first
  413. routine called in @var(genentrycode).
  414. @param(localsize Number of bytes to allocate as locals)
  415. }
  416. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);virtual; abstract;
  417. {# Emits instructions for returning from a subroutine.
  418. Should also restore the framepointer and stack.
  419. @param(parasize Number of bytes of parameters to deallocate from stack)
  420. }
  421. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);virtual;abstract;
  422. {# This routine is called when generating the code for the entry point
  423. of a routine. It should save all registers which are not used in this
  424. routine, and which should be declared as saved in the std_saved_registers
  425. set.
  426. This routine is mainly used when linking to code which is generated
  427. by ABI-compliant compilers (like GCC), to make sure that the reserved
  428. registers of that ABI are not clobbered.
  429. @param(usedinproc Registers which are used in the code of this routine)
  430. }
  431. procedure g_save_registers(list:TAsmList);virtual;
  432. {# This routine is called when generating the code for the exit point
  433. of a routine. It should restore all registers which were previously
  434. saved in @var(g_save_standard_registers).
  435. @param(usedinproc Registers which are used in the code of this routine)
  436. }
  437. procedure g_restore_registers(list:TAsmList);virtual;
  438. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);virtual;abstract;
  439. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);virtual;
  440. function g_indirect_sym_load(list:TAsmList;const symname: string; weak: boolean): tregister;virtual;
  441. { generate a stub which only purpose is to pass control the given external method,
  442. setting up any additional environment before doing so (if required).
  443. The default implementation issues a jump instruction to the external name. }
  444. procedure g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string); virtual;
  445. { initialize the pic/got register }
  446. procedure g_maybe_got_init(list: TAsmList); virtual;
  447. protected
  448. procedure get_subsetref_load_info(const sref: tsubsetreference; out loadsize: tcgsize; out extra_load: boolean);
  449. procedure a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister); virtual;
  450. procedure a_load_subsetref_regs_index(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg: tregister); virtual;
  451. procedure a_load_regconst_subsetref_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sref: tsubsetreference; slopt: tsubsetloadopt); virtual;
  452. procedure a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt); virtual;
  453. function get_bit_const_ref_sref(bitnumber: tcgint; const ref: treference): tsubsetreference;
  454. function get_bit_const_reg_sreg(setregsize: tcgsize; bitnumber: tcgint; setreg: tregister): tsubsetregister;
  455. function get_bit_reg_ref_sref(list: TAsmList; bitnumbersize: tcgsize; bitnumber: tregister; const ref: treference): tsubsetreference;
  456. end;
  457. {$ifndef cpu64bitalu}
  458. {# @abstract(Abstract code generator for 64 Bit operations)
  459. This class implements an abstract code generator class
  460. for 64 Bit operations.
  461. }
  462. tcg64 = class
  463. procedure a_load64_const_ref(list : TAsmList;value : int64;const ref : treference);virtual;abstract;
  464. procedure a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference);virtual;abstract;
  465. procedure a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64);virtual;abstract;
  466. procedure a_load64_reg_reg(list : TAsmList;regsrc,regdst : tregister64);virtual;abstract;
  467. procedure a_load64_const_reg(list : TAsmList;value : int64;reg : tregister64);virtual;abstract;
  468. procedure a_load64_loc_reg(list : TAsmList;const l : tlocation;reg : tregister64);virtual;abstract;
  469. procedure a_load64_loc_ref(list : TAsmList;const l : tlocation;const ref : treference);virtual;abstract;
  470. procedure a_load64_const_loc(list : TAsmList;value : int64;const l : tlocation);virtual;abstract;
  471. procedure a_load64_reg_loc(list : TAsmList;reg : tregister64;const l : tlocation);virtual;abstract;
  472. procedure a_load64_subsetref_reg(list : TAsmList; const sref: tsubsetreference; destreg: tregister64);virtual;abstract;
  473. procedure a_load64_reg_subsetref(list : TAsmList; fromreg: tregister64; const sref: tsubsetreference);virtual;abstract;
  474. procedure a_load64_const_subsetref(list: TAsmlist; a: int64; const sref: tsubsetreference);virtual;abstract;
  475. procedure a_load64_ref_subsetref(list : TAsmList; const fromref: treference; const sref: tsubsetreference);virtual;abstract;
  476. procedure a_load64_subsetref_subsetref(list: TAsmlist; const fromsref, tosref: tsubsetreference); virtual;abstract;
  477. procedure a_load64_subsetref_ref(list : TAsmList; const sref: tsubsetreference; const destref: treference); virtual;abstract;
  478. procedure a_load64_loc_subsetref(list : TAsmList; const l: tlocation; const sref : tsubsetreference);
  479. procedure a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  480. procedure a_load64high_reg_ref(list : TAsmList;reg : tregister;const ref : treference);virtual;abstract;
  481. procedure a_load64low_reg_ref(list : TAsmList;reg : tregister;const ref : treference);virtual;abstract;
  482. procedure a_load64high_ref_reg(list : TAsmList;const ref : treference;reg : tregister);virtual;abstract;
  483. procedure a_load64low_ref_reg(list : TAsmList;const ref : treference;reg : tregister);virtual;abstract;
  484. procedure a_load64high_loc_reg(list : TAsmList;const l : tlocation;reg : tregister);virtual;abstract;
  485. procedure a_load64low_loc_reg(list : TAsmList;const l : tlocation;reg : tregister);virtual;abstract;
  486. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);virtual;abstract;
  487. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);virtual;abstract;
  488. procedure a_op64_reg_ref(list : TAsmList;op:TOpCG;size : tcgsize;regsrc : tregister64;const ref : treference);virtual;abstract;
  489. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;regdst : tregister64);virtual;abstract;
  490. procedure a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);virtual;abstract;
  491. procedure a_op64_const_loc(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const l: tlocation);virtual;abstract;
  492. procedure a_op64_reg_loc(list : TAsmList;op:TOpCG;size : tcgsize;reg : tregister64;const l : tlocation);virtual;abstract;
  493. procedure a_op64_loc_reg(list : TAsmList;op:TOpCG;size : tcgsize;const l : tlocation;reg64 : tregister64);virtual;abstract;
  494. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);virtual;
  495. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);virtual;
  496. procedure a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  497. procedure a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  498. procedure a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  499. procedure a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  500. procedure a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  501. procedure a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  502. procedure a_load64_reg_cgpara(list : TAsmList;reg64 : tregister64;const loc : TCGPara);virtual;abstract;
  503. procedure a_load64_const_cgpara(list : TAsmList;value : int64;const loc : TCGPara);virtual;abstract;
  504. procedure a_load64_ref_cgpara(list : TAsmList;const r : treference;const loc : TCGPara);virtual;abstract;
  505. procedure a_load64_loc_cgpara(list : TAsmList;const l : tlocation;const loc : TCGPara);virtual;abstract;
  506. procedure a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister); virtual;abstract;
  507. procedure a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64); virtual;abstract;
  508. {
  509. This routine tries to optimize the const_reg opcode, and should be
  510. called at the start of a_op64_const_reg. It returns the actual opcode
  511. to emit, and the constant value to emit. If this routine returns
  512. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  513. @param(op The opcode to emit, returns the opcode which must be emitted)
  514. @param(a The constant which should be emitted, returns the constant which must
  515. be emitted)
  516. @param(reg The register to emit the opcode with, returns the register with
  517. which the opcode will be emitted)
  518. }
  519. function optimize64_op_const_reg(list: TAsmList; var op: topcg; var a : int64; var reg: tregister64): boolean;virtual;abstract;
  520. { override to catch 64bit rangechecks }
  521. procedure g_rangecheck64(list: TAsmList; const l:tlocation; fromdef,todef: tdef);virtual;abstract;
  522. end;
  523. {$endif cpu64bitalu}
  524. var
  525. {# Main code generator class }
  526. cg : tcg;
  527. {$ifndef cpu64bitalu}
  528. {# Code generator class for all operations working with 64-Bit operands }
  529. cg64 : tcg64;
  530. {$endif cpu64bitalu}
  531. procedure destroy_codegen;
  532. implementation
  533. uses
  534. globals,options,systems,
  535. verbose,defutil,paramgr,symsym,
  536. tgobj,cutils,procinfo,
  537. ncgrtti;
  538. {*****************************************************************************
  539. basic functionallity
  540. ******************************************************************************}
  541. constructor tcg.create;
  542. begin
  543. end;
  544. {*****************************************************************************
  545. register allocation
  546. ******************************************************************************}
  547. procedure tcg.init_register_allocators;
  548. begin
  549. fillchar(rg,sizeof(rg),0);
  550. add_reg_instruction_hook:=@add_reg_instruction;
  551. executionweight:=1;
  552. end;
  553. procedure tcg.done_register_allocators;
  554. begin
  555. { Safety }
  556. fillchar(rg,sizeof(rg),0);
  557. add_reg_instruction_hook:=nil;
  558. end;
  559. {$ifdef flowgraph}
  560. procedure Tcg.init_flowgraph;
  561. begin
  562. aktflownode:=0;
  563. end;
  564. procedure Tcg.done_flowgraph;
  565. begin
  566. end;
  567. {$endif}
  568. function tcg.getintregister(list:TAsmList;size:Tcgsize):Tregister;
  569. begin
  570. if not assigned(rg[R_INTREGISTER]) then
  571. internalerror(200312122);
  572. result:=rg[R_INTREGISTER].getregister(list,cgsize2subreg(R_INTREGISTER,size));
  573. end;
  574. function tcg.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  575. begin
  576. if not assigned(rg[R_FPUREGISTER]) then
  577. internalerror(200312123);
  578. result:=rg[R_FPUREGISTER].getregister(list,cgsize2subreg(R_FPUREGISTER,size));
  579. end;
  580. function tcg.getmmregister(list:TAsmList;size:Tcgsize):Tregister;
  581. begin
  582. if not assigned(rg[R_MMREGISTER]) then
  583. internalerror(2003121214);
  584. result:=rg[R_MMREGISTER].getregister(list,cgsize2subreg(R_MMREGISTER,size));
  585. end;
  586. function tcg.getaddressregister(list:TAsmList):Tregister;
  587. begin
  588. if assigned(rg[R_ADDRESSREGISTER]) then
  589. result:=rg[R_ADDRESSREGISTER].getregister(list,R_SUBWHOLE)
  590. else
  591. begin
  592. if not assigned(rg[R_INTREGISTER]) then
  593. internalerror(200312121);
  594. result:=rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  595. end;
  596. end;
  597. function Tcg.makeregsize(list:TAsmList;reg:Tregister;size:Tcgsize):Tregister;
  598. var
  599. subreg:Tsubregister;
  600. begin
  601. subreg:=cgsize2subreg(getregtype(reg),size);
  602. result:=reg;
  603. setsubreg(result,subreg);
  604. { notify RA }
  605. if result<>reg then
  606. list.concat(tai_regalloc.resize(result));
  607. end;
  608. procedure tcg.getcpuregister(list:TAsmList;r:Tregister);
  609. begin
  610. if not assigned(rg[getregtype(r)]) then
  611. internalerror(200312125);
  612. rg[getregtype(r)].getcpuregister(list,r);
  613. end;
  614. procedure tcg.ungetcpuregister(list:TAsmList;r:Tregister);
  615. begin
  616. if not assigned(rg[getregtype(r)]) then
  617. internalerror(200312126);
  618. rg[getregtype(r)].ungetcpuregister(list,r);
  619. end;
  620. procedure tcg.alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  621. begin
  622. if assigned(rg[rt]) then
  623. rg[rt].alloccpuregisters(list,r)
  624. else
  625. internalerror(200310092);
  626. end;
  627. procedure tcg.allocallcpuregisters(list:TAsmList);
  628. begin
  629. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  630. {$if not(defined(i386)) and not(defined(avr))}
  631. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  632. {$ifdef cpumm}
  633. alloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  634. {$endif cpumm}
  635. {$endif not(defined(i386)) and not(defined(avr))}
  636. end;
  637. procedure tcg.dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  638. begin
  639. if assigned(rg[rt]) then
  640. rg[rt].dealloccpuregisters(list,r)
  641. else
  642. internalerror(200310093);
  643. end;
  644. procedure tcg.deallocallcpuregisters(list:TAsmList);
  645. begin
  646. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  647. {$if not(defined(i386)) and not(defined(avr))}
  648. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  649. {$ifdef cpumm}
  650. dealloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  651. {$endif cpumm}
  652. {$endif not(defined(i386)) and not(defined(avr))}
  653. end;
  654. function tcg.uses_registers(rt:Tregistertype):boolean;
  655. begin
  656. if assigned(rg[rt]) then
  657. result:=rg[rt].uses_registers
  658. else
  659. result:=false;
  660. end;
  661. procedure tcg.add_reg_instruction(instr:Tai;r:tregister);
  662. var
  663. rt : tregistertype;
  664. begin
  665. rt:=getregtype(r);
  666. { Only add it when a register allocator is configured.
  667. No IE can be generated, because the VMT is written
  668. without a valid rg[] }
  669. if assigned(rg[rt]) then
  670. rg[rt].add_reg_instruction(instr,r,cg.executionweight);
  671. end;
  672. procedure tcg.add_move_instruction(instr:Taicpu);
  673. var
  674. rt : tregistertype;
  675. begin
  676. rt:=getregtype(instr.oper[O_MOV_SOURCE]^.reg);
  677. if assigned(rg[rt]) then
  678. rg[rt].add_move_instruction(instr)
  679. else
  680. internalerror(200310095);
  681. end;
  682. procedure tcg.set_regalloc_live_range_direction(dir: TRADirection);
  683. var
  684. rt : tregistertype;
  685. begin
  686. for rt:=low(rg) to high(rg) do
  687. begin
  688. if assigned(rg[rt]) then
  689. rg[rt].live_range_direction:=dir;
  690. end;
  691. end;
  692. procedure tcg.do_register_allocation(list:TAsmList;headertai:tai);
  693. var
  694. rt : tregistertype;
  695. begin
  696. for rt:=R_FPUREGISTER to R_SPECIALREGISTER do
  697. begin
  698. if assigned(rg[rt]) then
  699. rg[rt].do_register_allocation(list,headertai);
  700. end;
  701. { running the other register allocator passes could require addition int/addr. registers
  702. when spilling so run int/addr register allocation at the end }
  703. if assigned(rg[R_INTREGISTER]) then
  704. rg[R_INTREGISTER].do_register_allocation(list,headertai);
  705. if assigned(rg[R_ADDRESSREGISTER]) then
  706. rg[R_ADDRESSREGISTER].do_register_allocation(list,headertai);
  707. end;
  708. procedure tcg.translate_register(var reg : tregister);
  709. begin
  710. rg[getregtype(reg)].translate_register(reg);
  711. end;
  712. procedure tcg.a_reg_alloc(list : TAsmList;r : tregister);
  713. begin
  714. list.concat(tai_regalloc.alloc(r,nil));
  715. end;
  716. procedure tcg.a_reg_dealloc(list : TAsmList;r : tregister);
  717. begin
  718. list.concat(tai_regalloc.dealloc(r,nil));
  719. end;
  720. procedure tcg.a_reg_sync(list : TAsmList;r : tregister);
  721. var
  722. instr : tai;
  723. begin
  724. instr:=tai_regalloc.sync(r);
  725. list.concat(instr);
  726. add_reg_instruction(instr,r);
  727. end;
  728. procedure tcg.a_label(list : TAsmList;l : tasmlabel);
  729. begin
  730. list.concat(tai_label.create(l));
  731. end;
  732. {*****************************************************************************
  733. for better code generation these methods should be overridden
  734. ******************************************************************************}
  735. procedure tcg.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : TCGPara);
  736. var
  737. ref : treference;
  738. begin
  739. cgpara.check_simple_location;
  740. paramanager.alloccgpara(list,cgpara);
  741. case cgpara.location^.loc of
  742. LOC_REGISTER,LOC_CREGISTER:
  743. a_load_reg_reg(list,size,cgpara.location^.size,r,cgpara.location^.register);
  744. LOC_REFERENCE,LOC_CREFERENCE:
  745. begin
  746. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  747. a_load_reg_ref(list,size,cgpara.location^.size,r,ref);
  748. end;
  749. LOC_MMREGISTER,LOC_CMMREGISTER:
  750. a_loadmm_intreg_reg(list,size,cgpara.location^.size,r,cgpara.location^.register,mms_movescalar);
  751. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  752. begin
  753. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,ref);
  754. a_load_reg_ref(list,size,size,r,ref);
  755. a_loadfpu_ref_cgpara(list,cgpara.location^.size,ref,cgpara);
  756. tg.Ungettemp(list,ref);
  757. end
  758. else
  759. internalerror(2002071004);
  760. end;
  761. end;
  762. procedure tcg.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : TCGPara);
  763. var
  764. ref : treference;
  765. begin
  766. cgpara.check_simple_location;
  767. paramanager.alloccgpara(list,cgpara);
  768. case cgpara.location^.loc of
  769. LOC_REGISTER,LOC_CREGISTER:
  770. a_load_const_reg(list,cgpara.location^.size,a,cgpara.location^.register);
  771. LOC_REFERENCE,LOC_CREFERENCE:
  772. begin
  773. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  774. a_load_const_ref(list,cgpara.location^.size,a,ref);
  775. end
  776. else
  777. internalerror(2010053109);
  778. end;
  779. end;
  780. procedure tcg.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : TCGPara);
  781. var
  782. tmpref, ref: treference;
  783. tmpreg: tregister;
  784. location: pcgparalocation;
  785. orgsizeleft,
  786. sizeleft: tcgint;
  787. reghasvalue: boolean;
  788. begin
  789. location:=cgpara.location;
  790. tmpref:=r;
  791. sizeleft:=cgpara.intsize;
  792. while assigned(location) do
  793. begin
  794. paramanager.allocparaloc(list,location);
  795. case location^.loc of
  796. LOC_REGISTER,LOC_CREGISTER:
  797. begin
  798. { Parameter locations are often allocated in multiples of
  799. entire registers. If a parameter only occupies a part of
  800. such a register (e.g. a 16 bit int on a 32 bit
  801. architecture), the size of this parameter can only be
  802. determined by looking at the "size" parameter of this
  803. method -> if the size parameter is <= sizeof(aint), then
  804. we check that there is only one parameter location and
  805. then use this "size" to load the value into the parameter
  806. location }
  807. if (size<>OS_NO) and
  808. (tcgsize2size[size]<=sizeof(aint)) then
  809. begin
  810. cgpara.check_simple_location;
  811. a_load_ref_reg(list,size,location^.size,tmpref,location^.register);
  812. end
  813. { there's a lot more data left, and the current paraloc's
  814. register is entirely filled with part of that data }
  815. else if (sizeleft>sizeof(aint)) then
  816. begin
  817. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  818. end
  819. { we're at the end of the data, and it can be loaded into
  820. the current location's register with a single regular
  821. load }
  822. else if (sizeleft in [1,2{$ifndef cpu16bitalu},4{$endif}{$ifdef cpu64bitalu},8{$endif}]) then
  823. begin
  824. a_load_ref_reg(list,int_cgsize(sizeleft),location^.size,tmpref,location^.register);
  825. end
  826. { we're at the end of the data, and we need multiple loads
  827. to get it in the register because it's an irregular size }
  828. else
  829. begin
  830. { should be the last part }
  831. if assigned(location^.next) then
  832. internalerror(2010052907);
  833. { load the value piecewise to get it into the register }
  834. orgsizeleft:=sizeleft;
  835. reghasvalue:=false;
  836. {$ifdef cpu64bitalu}
  837. if sizeleft>=4 then
  838. begin
  839. a_load_ref_reg(list,OS_32,location^.size,tmpref,location^.register);
  840. dec(sizeleft,4);
  841. if target_info.endian=endian_big then
  842. a_op_const_reg(list,OP_SHL,location^.size,sizeleft*8,location^.register);
  843. inc(tmpref.offset,4);
  844. reghasvalue:=true;
  845. end;
  846. {$endif cpu64bitalu}
  847. if sizeleft>=2 then
  848. begin
  849. tmpreg:=getintregister(list,location^.size);
  850. a_load_ref_reg(list,OS_16,location^.size,tmpref,tmpreg);
  851. dec(sizeleft,2);
  852. if reghasvalue then
  853. begin
  854. if target_info.endian=endian_big then
  855. a_op_const_reg(list,OP_SHL,location^.size,sizeleft*8,tmpreg)
  856. else
  857. a_op_const_reg(list,OP_SHL,location^.size,(orgsizeleft-(sizeleft+2))*8,tmpreg);
  858. a_op_reg_reg(list,OP_OR,location^.size,tmpreg,location^.register);
  859. end
  860. else
  861. begin
  862. if target_info.endian=endian_big then
  863. a_op_const_reg_reg(list,OP_SHL,location^.size,sizeleft*8,tmpreg,location^.register)
  864. else
  865. a_load_reg_reg(list,location^.size,location^.size,tmpreg,location^.register);
  866. end;
  867. inc(tmpref.offset,2);
  868. reghasvalue:=true;
  869. end;
  870. if sizeleft=1 then
  871. begin
  872. tmpreg:=getintregister(list,location^.size);
  873. a_load_ref_reg(list,OS_8,location^.size,tmpref,tmpreg);
  874. dec(sizeleft,1);
  875. if reghasvalue then
  876. begin
  877. if target_info.endian=endian_little then
  878. a_op_const_reg(list,OP_SHL,location^.size,(orgsizeleft-(sizeleft+1))*8,tmpreg);
  879. a_op_reg_reg(list,OP_OR,location^.size,tmpreg,location^.register)
  880. end
  881. else
  882. a_load_reg_reg(list,location^.size,location^.size,tmpreg,location^.register);
  883. inc(tmpref.offset);
  884. end;
  885. { the loop will already adjust the offset and sizeleft }
  886. dec(tmpref.offset,orgsizeleft);
  887. sizeleft:=orgsizeleft;
  888. end;
  889. end;
  890. LOC_REFERENCE,LOC_CREFERENCE:
  891. begin
  892. if assigned(location^.next) then
  893. internalerror(2010052906);
  894. reference_reset_base(ref,location^.reference.index,location^.reference.offset,newalignment(cgpara.alignment,cgpara.intsize-sizeleft));
  895. if (size <> OS_NO) and
  896. (tcgsize2size[size] <= sizeof(aint)) then
  897. a_load_ref_ref(list,size,location^.size,tmpref,ref)
  898. else
  899. { use concatcopy, because the parameter can be larger than }
  900. { what the OS_* constants can handle }
  901. g_concatcopy(list,tmpref,ref,sizeleft);
  902. end;
  903. LOC_MMREGISTER,LOC_CMMREGISTER:
  904. begin
  905. case location^.size of
  906. OS_F32,
  907. OS_F64,
  908. OS_F128:
  909. a_loadmm_ref_reg(list,location^.size,location^.size,tmpref,location^.register,mms_movescalar);
  910. OS_M8..OS_M128,
  911. OS_MS8..OS_MS128:
  912. a_loadmm_ref_reg(list,location^.size,location^.size,tmpref,location^.register,nil);
  913. else
  914. internalerror(2010053101);
  915. end;
  916. end
  917. else
  918. internalerror(2010053111);
  919. end;
  920. inc(tmpref.offset,tcgsize2size[location^.size]);
  921. dec(sizeleft,tcgsize2size[location^.size]);
  922. location:=location^.next;
  923. end;
  924. end;
  925. procedure tcg.a_load_loc_cgpara(list : TAsmList;const l:tlocation;const cgpara : TCGPara);
  926. begin
  927. case l.loc of
  928. LOC_REGISTER,
  929. LOC_CREGISTER :
  930. a_load_reg_cgpara(list,l.size,l.register,cgpara);
  931. LOC_CONSTANT :
  932. a_load_const_cgpara(list,l.size,l.value,cgpara);
  933. LOC_CREFERENCE,
  934. LOC_REFERENCE :
  935. a_load_ref_cgpara(list,l.size,l.reference,cgpara);
  936. else
  937. internalerror(2002032211);
  938. end;
  939. end;
  940. procedure tcg.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : TCGPara);
  941. var
  942. hr : tregister;
  943. begin
  944. cgpara.check_simple_location;
  945. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  946. begin
  947. paramanager.allocparaloc(list,cgpara.location);
  948. a_loadaddr_ref_reg(list,r,cgpara.location^.register)
  949. end
  950. else
  951. begin
  952. hr:=getaddressregister(list);
  953. a_loadaddr_ref_reg(list,r,hr);
  954. a_load_reg_cgpara(list,OS_ADDR,hr,cgpara);
  955. end;
  956. end;
  957. procedure tcg.a_load_cgparaloc_ref(list : TAsmList;const paraloc : TCGParaLocation;const ref : treference;sizeleft : tcgint;align : longint);
  958. var
  959. href : treference;
  960. begin
  961. case paraloc.loc of
  962. LOC_REGISTER :
  963. begin
  964. {$IFDEF POWERPC64}
  965. if (paraloc.shiftval <> 0) then
  966. a_op_const_reg_reg(list, OP_SHL, OS_INT, paraloc.shiftval, paraloc.register, paraloc.register);
  967. {$ENDIF POWERPC64}
  968. a_load_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  969. end;
  970. LOC_MMREGISTER :
  971. begin
  972. case paraloc.size of
  973. OS_F32,
  974. OS_F64,
  975. OS_F128:
  976. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  977. OS_M8..OS_M128,
  978. OS_MS8..OS_MS128:
  979. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,nil);
  980. else
  981. internalerror(2010053102);
  982. end;
  983. end;
  984. LOC_FPUREGISTER :
  985. cg.a_loadfpu_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  986. LOC_REFERENCE :
  987. begin
  988. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,align);
  989. { use concatcopy, because it can also be a float which fails when
  990. load_ref_ref is used. Don't copy data when the references are equal }
  991. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  992. g_concatcopy(list,href,ref,sizeleft);
  993. end;
  994. else
  995. internalerror(2002081302);
  996. end;
  997. end;
  998. procedure tcg.a_load_cgparaloc_anyreg(list: TAsmList;regsize: tcgsize;const paraloc: TCGParaLocation;reg: tregister;align: longint);
  999. var
  1000. href : treference;
  1001. begin
  1002. case paraloc.loc of
  1003. LOC_REGISTER :
  1004. begin
  1005. case getregtype(reg) of
  1006. R_INTREGISTER:
  1007. a_load_reg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1008. R_MMREGISTER:
  1009. a_loadmm_intreg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1010. else
  1011. internalerror(2009112422);
  1012. end;
  1013. end;
  1014. LOC_MMREGISTER :
  1015. begin
  1016. case getregtype(reg) of
  1017. R_INTREGISTER:
  1018. a_loadmm_reg_intreg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1019. R_MMREGISTER:
  1020. begin
  1021. case paraloc.size of
  1022. OS_F32,
  1023. OS_F64,
  1024. OS_F128:
  1025. a_loadmm_reg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1026. OS_M8..OS_M128,
  1027. OS_MS8..OS_MS128:
  1028. a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,nil);
  1029. else
  1030. internalerror(2010053102);
  1031. end;
  1032. end;
  1033. else
  1034. internalerror(2010053104);
  1035. end;
  1036. end;
  1037. LOC_FPUREGISTER :
  1038. a_loadfpu_reg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1039. LOC_REFERENCE :
  1040. begin
  1041. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,align);
  1042. case getregtype(reg) of
  1043. R_INTREGISTER :
  1044. a_load_ref_reg(list,paraloc.size,regsize,href,reg);
  1045. R_FPUREGISTER :
  1046. a_loadfpu_ref_reg(list,paraloc.size,regsize,href,reg);
  1047. R_MMREGISTER :
  1048. { not paraloc.size, because it may be OS_64 instead of
  1049. OS_F64 in case the parameter is passed using integer
  1050. conventions (e.g., on ARM) }
  1051. a_loadmm_ref_reg(list,regsize,regsize,href,reg,mms_movescalar);
  1052. else
  1053. internalerror(2004101012);
  1054. end;
  1055. end;
  1056. else
  1057. internalerror(2002081302);
  1058. end;
  1059. end;
  1060. {****************************************************************************
  1061. some generic implementations
  1062. ****************************************************************************}
  1063. {$ifopt r+}
  1064. {$define rangeon}
  1065. {$r-}
  1066. {$endif}
  1067. {$ifopt q+}
  1068. {$define overflowon}
  1069. {$q-}
  1070. {$endif}
  1071. procedure tcg.a_load_subsetreg_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; destreg: tregister);
  1072. var
  1073. bitmask: aword;
  1074. tmpreg: tregister;
  1075. stopbit: byte;
  1076. begin
  1077. tmpreg:=getintregister(list,sreg.subsetregsize);
  1078. if (subsetsize in [OS_S8..OS_S128]) then
  1079. begin
  1080. { sign extend in case the value has a bitsize mod 8 <> 0 }
  1081. { both instructions will be optimized away if not }
  1082. a_op_const_reg_reg(list,OP_SHL,sreg.subsetregsize,(tcgsize2size[sreg.subsetregsize]*8)-sreg.startbit-sreg.bitlen,sreg.subsetreg,tmpreg);
  1083. a_op_const_reg(list,OP_SAR,sreg.subsetregsize,(tcgsize2size[sreg.subsetregsize]*8)-sreg.bitlen,tmpreg);
  1084. end
  1085. else
  1086. begin
  1087. a_op_const_reg_reg(list,OP_SHR,sreg.subsetregsize,sreg.startbit,sreg.subsetreg,tmpreg);
  1088. stopbit := sreg.startbit + sreg.bitlen;
  1089. // on x86(64), 1 shl 32(64) = 1 instead of 0
  1090. // use aword to prevent overflow with 1 shl 31
  1091. if (stopbit - sreg.startbit <> AIntBits) then
  1092. bitmask := (aword(1) shl (stopbit - sreg.startbit)) - 1
  1093. else
  1094. bitmask := high(aword);
  1095. a_op_const_reg(list,OP_AND,sreg.subsetregsize,tcgint(bitmask),tmpreg);
  1096. end;
  1097. tmpreg := makeregsize(list,tmpreg,subsetsize);
  1098. a_load_reg_reg(list,tcgsize2unsigned[subsetsize],subsetsize,tmpreg,tmpreg);
  1099. a_load_reg_reg(list,subsetsize,tosize,tmpreg,destreg);
  1100. end;
  1101. procedure tcg.a_load_reg_subsetreg(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister);
  1102. begin
  1103. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,sreg,SL_REG);
  1104. end;
  1105. procedure tcg.a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt);
  1106. var
  1107. bitmask: aword;
  1108. tmpreg: tregister;
  1109. stopbit: byte;
  1110. begin
  1111. stopbit := sreg.startbit + sreg.bitlen;
  1112. // on x86(64), 1 shl 32(64) = 1 instead of 0
  1113. if (stopbit <> AIntBits) then
  1114. bitmask := not(((aword(1) shl stopbit)-1) xor ((aword(1) shl sreg.startbit)-1))
  1115. else
  1116. bitmask := not(high(aword) xor ((aword(1) shl sreg.startbit)-1));
  1117. if not(slopt in [SL_SETZERO,SL_SETMAX]) then
  1118. begin
  1119. tmpreg:=getintregister(list,sreg.subsetregsize);
  1120. a_load_reg_reg(list,fromsize,sreg.subsetregsize,fromreg,tmpreg);
  1121. a_op_const_reg(list,OP_SHL,sreg.subsetregsize,sreg.startbit,tmpreg);
  1122. if (slopt <> SL_REGNOSRCMASK) then
  1123. a_op_const_reg(list,OP_AND,sreg.subsetregsize,tcgint(not(bitmask)),tmpreg);
  1124. end;
  1125. if (slopt <> SL_SETMAX) then
  1126. a_op_const_reg(list,OP_AND,sreg.subsetregsize,tcgint(bitmask),sreg.subsetreg);
  1127. case slopt of
  1128. SL_SETZERO : ;
  1129. SL_SETMAX :
  1130. if (sreg.bitlen <> AIntBits) then
  1131. a_op_const_reg(list,OP_OR,sreg.subsetregsize,
  1132. tcgint(((aword(1) shl sreg.bitlen)-1) shl sreg.startbit),
  1133. sreg.subsetreg)
  1134. else
  1135. a_load_const_reg(list,sreg.subsetregsize,-1,sreg.subsetreg);
  1136. else
  1137. a_op_reg_reg(list,OP_OR,sreg.subsetregsize,tmpreg,sreg.subsetreg);
  1138. end;
  1139. end;
  1140. procedure tcg.a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsreg, tosreg: tsubsetregister);
  1141. var
  1142. tmpreg: tregister;
  1143. bitmask: aword;
  1144. stopbit: byte;
  1145. begin
  1146. if (fromsreg.bitlen >= tosreg.bitlen) then
  1147. begin
  1148. tmpreg := getintregister(list,tosreg.subsetregsize);
  1149. a_load_reg_reg(list,fromsreg.subsetregsize,tosreg.subsetregsize,fromsreg.subsetreg,tmpreg);
  1150. if (fromsreg.startbit <= tosreg.startbit) then
  1151. a_op_const_reg(list,OP_SHL,tosreg.subsetregsize,tosreg.startbit-fromsreg.startbit,tmpreg)
  1152. else
  1153. a_op_const_reg(list,OP_SHR,tosreg.subsetregsize,fromsreg.startbit-tosreg.startbit,tmpreg);
  1154. stopbit := tosreg.startbit + tosreg.bitlen;
  1155. // on x86(64), 1 shl 32(64) = 1 instead of 0
  1156. if (stopbit <> AIntBits) then
  1157. bitmask := not(((aword(1) shl stopbit)-1) xor ((aword(1) shl tosreg.startbit)-1))
  1158. else
  1159. bitmask := (aword(1) shl tosreg.startbit) - 1;
  1160. a_op_const_reg(list,OP_AND,tosreg.subsetregsize,tcgint(bitmask),tosreg.subsetreg);
  1161. a_op_const_reg(list,OP_AND,tosreg.subsetregsize,tcgint(not(bitmask)),tmpreg);
  1162. a_op_reg_reg(list,OP_OR,tosreg.subsetregsize,tmpreg,tosreg.subsetreg);
  1163. end
  1164. else
  1165. begin
  1166. tmpreg := getintregister(list,tosubsetsize);
  1167. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  1168. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  1169. end;
  1170. end;
  1171. procedure tcg.a_load_subsetreg_ref(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; const destref: treference);
  1172. var
  1173. tmpreg: tregister;
  1174. begin
  1175. tmpreg := getintregister(list,tosize);
  1176. a_load_subsetreg_reg(list,subsetsize,tosize,sreg,tmpreg);
  1177. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  1178. end;
  1179. procedure tcg.a_load_ref_subsetreg(list : TAsmList; fromsize, subsetsize: tcgsize; const fromref: treference; const sreg: tsubsetregister);
  1180. var
  1181. tmpreg: tregister;
  1182. begin
  1183. tmpreg := getintregister(list,subsetsize);
  1184. a_load_ref_reg(list,fromsize,subsetsize,fromref,tmpreg);
  1185. a_load_reg_subsetreg(list,subsetsize,subsetsize,tmpreg,sreg);
  1186. end;
  1187. procedure tcg.a_load_const_subsetreg(list: TAsmlist; subsetsize: tcgsize; a: tcgint; const sreg: tsubsetregister);
  1188. var
  1189. bitmask: aword;
  1190. stopbit: byte;
  1191. begin
  1192. stopbit := sreg.startbit + sreg.bitlen;
  1193. // on x86(64), 1 shl 32(64) = 1 instead of 0
  1194. if (stopbit <> AIntBits) then
  1195. bitmask := not(((aword(1) shl stopbit)-1) xor ((aword(1) shl sreg.startbit)-1))
  1196. else
  1197. bitmask := (aword(1) shl sreg.startbit) - 1;
  1198. if (((aword(a) shl sreg.startbit) and not bitmask) <> not bitmask) then
  1199. a_op_const_reg(list,OP_AND,sreg.subsetregsize,tcgint(bitmask),sreg.subsetreg);
  1200. a_op_const_reg(list,OP_OR,sreg.subsetregsize,tcgint((aword(a) shl sreg.startbit) and not(bitmask)),sreg.subsetreg);
  1201. end;
  1202. procedure tcg.a_load_loc_subsetref(list : TAsmList;subsetsize: tcgsize; const loc: tlocation; const sref : tsubsetreference);
  1203. begin
  1204. case loc.loc of
  1205. LOC_REFERENCE,LOC_CREFERENCE:
  1206. a_load_ref_subsetref(list,loc.size,subsetsize,loc.reference,sref);
  1207. LOC_REGISTER,LOC_CREGISTER:
  1208. a_load_reg_subsetref(list,loc.size,subsetsize,loc.register,sref);
  1209. LOC_CONSTANT:
  1210. a_load_const_subsetref(list,subsetsize,loc.value,sref);
  1211. LOC_SUBSETREG,LOC_CSUBSETREG:
  1212. a_load_subsetreg_subsetref(list,loc.size,subsetsize,loc.sreg,sref);
  1213. LOC_SUBSETREF,LOC_CSUBSETREF:
  1214. a_load_subsetref_subsetref(list,loc.size,subsetsize,loc.sref,sref);
  1215. else
  1216. internalerror(200608053);
  1217. end;
  1218. end;
  1219. (*
  1220. Subsetrefs are used for (bit)packed arrays and (bit)packed records stored
  1221. in memory. They are like a regular reference, but contain an extra bit
  1222. offset (either constant -startbit- or variable -bitindexreg-, always OS_INT)
  1223. and a bit length (always constant).
  1224. Bit packed values are stored differently in memory depending on whether we
  1225. are on a big or a little endian system (compatible with at least GPC). The
  1226. size of the basic working unit is always the smallest power-of-2 byte size
  1227. which can contain the bit value (so 1..8 bits -> 1 byte, 9..16 bits -> 2
  1228. bytes, 17..32 bits -> 4 bytes etc).
  1229. On a big endian, 5-bit: values are stored like this:
  1230. 11111222 22333334 44445555 56666677 77788888
  1231. The leftmost bit of each 5-bit value corresponds to the most significant
  1232. bit.
  1233. On little endian, it goes like this:
  1234. 22211111 43333322 55554444 77666665 88888777
  1235. In this case, per byte the left-most bit is more significant than those on
  1236. the right, but the bits in the next byte are all more significant than
  1237. those in the previous byte (e.g., the 222 in the first byte are the low
  1238. three bits of that value, while the 22 in the second byte are the upper
  1239. two bits.
  1240. Big endian, 9 bit values:
  1241. 11111111 12222222 22333333 33344444 ...
  1242. Little endian, 9 bit values:
  1243. 11111111 22222221 33333322 44444333 ...
  1244. This is memory representation and the 16 bit values are byteswapped.
  1245. Similarly as in the previous case, the 2222222 string contains the lower
  1246. bits of value 2 and the 22 string contains the upper bits. Once loaded into
  1247. registers (two 16 bit registers in the current implementation, although a
  1248. single 32 bit register would be possible too, in particular if 32 bit
  1249. alignment can be guaranteed), this becomes:
  1250. 22222221 11111111 44444333 33333322 ...
  1251. (l)ow u l l u l u
  1252. The startbit/bitindex in a subsetreference always refers to
  1253. a) on big endian: the most significant bit of the value
  1254. (bits counted from left to right, both memory an registers)
  1255. b) on little endian: the least significant bit when the value
  1256. is loaded in a register (bit counted from right to left)
  1257. Although a) results in more complex code for big endian systems, it's
  1258. needed for compatibility both with GPC and with e.g. bitpacked arrays in
  1259. Apple's universal interfaces which depend on these layout differences).
  1260. Note: when changing the loadsize calculated in get_subsetref_load_info,
  1261. make sure the appropriate alignment is guaranteed, at least in case of
  1262. {$defined cpurequiresproperalignment}.
  1263. *)
  1264. procedure tcg.get_subsetref_load_info(const sref: tsubsetreference; out loadsize: tcgsize; out extra_load: boolean);
  1265. var
  1266. intloadsize: tcgint;
  1267. begin
  1268. intloadsize := packedbitsloadsize(sref.bitlen);
  1269. if (intloadsize = 0) then
  1270. internalerror(2006081310);
  1271. if (intloadsize > sizeof(aint)) then
  1272. intloadsize := sizeof(aint);
  1273. loadsize := int_cgsize(intloadsize);
  1274. if (loadsize = OS_NO) then
  1275. internalerror(2006081311);
  1276. if (sref.bitlen > sizeof(aint)*8) then
  1277. internalerror(2006081312);
  1278. extra_load :=
  1279. (sref.bitlen <> 1) and
  1280. ((sref.bitindexreg <> NR_NO) or
  1281. (byte(sref.startbit+sref.bitlen) > byte(intloadsize*8)));
  1282. end;
  1283. procedure tcg.a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister);
  1284. var
  1285. restbits: byte;
  1286. begin
  1287. if (target_info.endian = endian_big) then
  1288. begin
  1289. { valuereg contains the upper bits, extra_value_reg the lower }
  1290. restbits := (sref.bitlen - (loadbitsize - sref.startbit));
  1291. if (subsetsize in [OS_S8..OS_S128]) then
  1292. begin
  1293. { sign extend }
  1294. a_op_const_reg(list,OP_SHL,OS_INT,AIntBits-loadbitsize+sref.startbit,valuereg);
  1295. a_op_const_reg(list,OP_SAR,OS_INT,AIntBits-sref.bitlen,valuereg);
  1296. end
  1297. else
  1298. begin
  1299. a_op_const_reg(list,OP_SHL,OS_INT,restbits,valuereg);
  1300. { mask other bits }
  1301. if (sref.bitlen <> AIntBits) then
  1302. a_op_const_reg(list,OP_AND,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1303. end;
  1304. a_op_const_reg(list,OP_SHR,OS_INT,loadbitsize-restbits,extra_value_reg)
  1305. end
  1306. else
  1307. begin
  1308. { valuereg contains the lower bits, extra_value_reg the upper }
  1309. a_op_const_reg(list,OP_SHR,OS_INT,sref.startbit,valuereg);
  1310. if (subsetsize in [OS_S8..OS_S128]) then
  1311. begin
  1312. a_op_const_reg(list,OP_SHL,OS_INT,AIntBits-sref.bitlen+loadbitsize-sref.startbit,extra_value_reg);
  1313. a_op_const_reg(list,OP_SAR,OS_INT,AIntBits-sref.bitlen,extra_value_reg);
  1314. end
  1315. else
  1316. begin
  1317. a_op_const_reg(list,OP_SHL,OS_INT,loadbitsize-sref.startbit,extra_value_reg);
  1318. { mask other bits }
  1319. if (sref.bitlen <> AIntBits) then
  1320. a_op_const_reg(list,OP_AND,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),extra_value_reg);
  1321. end;
  1322. end;
  1323. { merge }
  1324. a_op_reg_reg(list,OP_OR,OS_INT,extra_value_reg,valuereg);
  1325. end;
  1326. procedure tcg.a_load_subsetref_regs_index(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg: tregister);
  1327. var
  1328. hl: tasmlabel;
  1329. tmpref: treference;
  1330. extra_value_reg,
  1331. tmpreg: tregister;
  1332. begin
  1333. tmpreg := getintregister(list,OS_INT);
  1334. tmpref := sref.ref;
  1335. inc(tmpref.offset,loadbitsize div 8);
  1336. extra_value_reg := getintregister(list,OS_INT);
  1337. if (target_info.endian = endian_big) then
  1338. begin
  1339. { since this is a dynamic index, it's possible that the value }
  1340. { is entirely in valuereg. }
  1341. { get the data in valuereg in the right place }
  1342. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,valuereg);
  1343. if (subsetsize in [OS_S8..OS_S128]) then
  1344. begin
  1345. a_op_const_reg(list,OP_SHL,OS_INT,AIntBits-loadbitsize,valuereg);
  1346. a_op_const_reg(list,OP_SAR,OS_INT,AIntBits-sref.bitlen,valuereg)
  1347. end
  1348. else
  1349. begin
  1350. a_op_const_reg(list,OP_SHR,OS_INT,loadbitsize-sref.bitlen,valuereg);
  1351. if (loadbitsize <> AIntBits) then
  1352. { mask left over bits }
  1353. a_op_const_reg(list,OP_AND,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1354. end;
  1355. tmpreg := getintregister(list,OS_INT);
  1356. { ensure we don't load anything past the end of the array }
  1357. current_asmdata.getjumplabel(hl);
  1358. a_cmp_const_reg_label(list,OS_INT,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  1359. { the bits in extra_value_reg (if any) start at the most significant bit => }
  1360. { extra_value_reg must be shr by (loadbitsize-sref.bitlen)+(loadsize-sref.bitindex) }
  1361. { => = -(sref.bitindex+(sref.bitlen-2*loadbitsize)) }
  1362. a_op_const_reg_reg(list,OP_ADD,OS_INT,sref.bitlen-2*loadbitsize,sref.bitindexreg,tmpreg);
  1363. a_op_reg_reg(list,OP_NEG,OS_INT,tmpreg,tmpreg);
  1364. { load next "loadbitsize" bits of the array }
  1365. a_load_ref_reg(list,int_cgsize(loadbitsize div 8),OS_INT,tmpref,extra_value_reg);
  1366. a_op_reg_reg(list,OP_SHR,OS_INT,tmpreg,extra_value_reg);
  1367. { if there are no bits in extra_value_reg, then sref.bitindex was }
  1368. { < loadsize-sref.bitlen, and therefore tmpreg will now be >= loadsize }
  1369. { => extra_value_reg is now 0 }
  1370. { merge }
  1371. a_op_reg_reg(list,OP_OR,OS_INT,extra_value_reg,valuereg);
  1372. { no need to mask, necessary masking happened earlier on }
  1373. a_label(list,hl);
  1374. end
  1375. else
  1376. begin
  1377. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,valuereg);
  1378. { ensure we don't load anything past the end of the array }
  1379. current_asmdata.getjumplabel(hl);
  1380. a_cmp_const_reg_label(list,OS_INT,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  1381. { Y-x = -(Y-x) }
  1382. a_op_const_reg_reg(list,OP_SUB,OS_INT,loadbitsize,sref.bitindexreg,tmpreg);
  1383. a_op_reg_reg(list,OP_NEG,OS_INT,tmpreg,tmpreg);
  1384. { load next "loadbitsize" bits of the array }
  1385. a_load_ref_reg(list,int_cgsize(loadbitsize div 8),OS_INT,tmpref,extra_value_reg);
  1386. { tmpreg is in the range 1..<cpu_bitsize>-1 -> always ok }
  1387. a_op_reg_reg(list,OP_SHL,OS_INT,tmpreg,extra_value_reg);
  1388. { merge }
  1389. a_op_reg_reg(list,OP_OR,OS_INT,extra_value_reg,valuereg);
  1390. a_label(list,hl);
  1391. { sign extend or mask other bits }
  1392. if (subsetsize in [OS_S8..OS_S128]) then
  1393. begin
  1394. a_op_const_reg(list,OP_SHL,OS_INT,AIntBits-sref.bitlen,valuereg);
  1395. a_op_const_reg(list,OP_SAR,OS_INT,AIntBits-sref.bitlen,valuereg);
  1396. end
  1397. else
  1398. a_op_const_reg(list,OP_AND,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1399. end;
  1400. end;
  1401. procedure tcg.a_load_subsetref_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sref: tsubsetreference; destreg: tregister);
  1402. var
  1403. tmpref: treference;
  1404. valuereg,extra_value_reg: tregister;
  1405. tosreg: tsubsetregister;
  1406. loadsize: tcgsize;
  1407. loadbitsize: byte;
  1408. extra_load: boolean;
  1409. begin
  1410. get_subsetref_load_info(sref,loadsize,extra_load);
  1411. loadbitsize := tcgsize2size[loadsize]*8;
  1412. { load the (first part) of the bit sequence }
  1413. valuereg := getintregister(list,OS_INT);
  1414. a_load_ref_reg(list,loadsize,OS_INT,sref.ref,valuereg);
  1415. if not extra_load then
  1416. begin
  1417. { everything is guaranteed to be in a single register of loadsize }
  1418. if (sref.bitindexreg = NR_NO) then
  1419. begin
  1420. { use subsetreg routine, it may have been overridden with an optimized version }
  1421. tosreg.subsetreg := valuereg;
  1422. tosreg.subsetregsize := OS_INT;
  1423. { subsetregs always count bits from right to left }
  1424. if (target_info.endian = endian_big) then
  1425. tosreg.startbit := loadbitsize - (sref.startbit+sref.bitlen)
  1426. else
  1427. tosreg.startbit := sref.startbit;
  1428. tosreg.bitlen := sref.bitlen;
  1429. a_load_subsetreg_reg(list,subsetsize,tosize,tosreg,destreg);
  1430. exit;
  1431. end
  1432. else
  1433. begin
  1434. if (sref.startbit <> 0) then
  1435. internalerror(2006081510);
  1436. if (target_info.endian = endian_big) then
  1437. begin
  1438. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,valuereg);
  1439. if (subsetsize in [OS_S8..OS_S128]) then
  1440. begin
  1441. { sign extend to entire register }
  1442. a_op_const_reg(list,OP_SHL,OS_INT,AIntBits-loadbitsize,valuereg);
  1443. a_op_const_reg(list,OP_SAR,OS_INT,AIntBits-sref.bitlen,valuereg);
  1444. end
  1445. else
  1446. a_op_const_reg(list,OP_SHR,OS_INT,loadbitsize-sref.bitlen,valuereg);
  1447. end
  1448. else
  1449. begin
  1450. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,valuereg);
  1451. if (subsetsize in [OS_S8..OS_S128]) then
  1452. begin
  1453. a_op_const_reg(list,OP_SHL,OS_INT,AIntBits-sref.bitlen,valuereg);
  1454. a_op_const_reg(list,OP_SAR,OS_INT,AIntBits-sref.bitlen,valuereg);
  1455. end
  1456. end;
  1457. { mask other bits/sign extend }
  1458. if not(subsetsize in [OS_S8..OS_S128]) then
  1459. a_op_const_reg(list,OP_AND,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),valuereg);
  1460. end
  1461. end
  1462. else
  1463. begin
  1464. { load next value as well }
  1465. extra_value_reg := getintregister(list,OS_INT);
  1466. if (sref.bitindexreg = NR_NO) then
  1467. begin
  1468. tmpref := sref.ref;
  1469. inc(tmpref.offset,loadbitsize div 8);
  1470. a_load_ref_reg(list,loadsize,OS_INT,tmpref,extra_value_reg);
  1471. { can be overridden to optimize }
  1472. a_load_subsetref_regs_noindex(list,subsetsize,loadbitsize,sref,valuereg,extra_value_reg)
  1473. end
  1474. else
  1475. begin
  1476. if (sref.startbit <> 0) then
  1477. internalerror(2006080610);
  1478. a_load_subsetref_regs_index(list,subsetsize,loadbitsize,sref,valuereg);
  1479. end;
  1480. end;
  1481. { store in destination }
  1482. { avoid unnecessary sign extension and zeroing }
  1483. valuereg := makeregsize(list,valuereg,OS_INT);
  1484. destreg := makeregsize(list,destreg,OS_INT);
  1485. a_load_reg_reg(list,OS_INT,OS_INT,valuereg,destreg);
  1486. destreg := makeregsize(list,destreg,tosize);
  1487. end;
  1488. procedure tcg.a_load_reg_subsetref(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sref: tsubsetreference);
  1489. begin
  1490. a_load_regconst_subsetref_intern(list,fromsize,subsetsize,fromreg,sref,SL_REG);
  1491. end;
  1492. procedure tcg.a_load_regconst_subsetref_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sref: tsubsetreference; slopt: tsubsetloadopt);
  1493. var
  1494. hl: tasmlabel;
  1495. tmpreg, tmpindexreg, valuereg, extra_value_reg, maskreg: tregister;
  1496. tosreg, fromsreg: tsubsetregister;
  1497. tmpref: treference;
  1498. bitmask: aword;
  1499. loadsize: tcgsize;
  1500. loadbitsize: byte;
  1501. extra_load: boolean;
  1502. begin
  1503. { the register must be able to contain the requested value }
  1504. if (tcgsize2size[fromsize]*8 < sref.bitlen) then
  1505. internalerror(2006081613);
  1506. get_subsetref_load_info(sref,loadsize,extra_load);
  1507. loadbitsize := tcgsize2size[loadsize]*8;
  1508. { load the (first part) of the bit sequence }
  1509. valuereg := getintregister(list,OS_INT);
  1510. a_load_ref_reg(list,loadsize,OS_INT,sref.ref,valuereg);
  1511. { constant offset of bit sequence? }
  1512. if not extra_load then
  1513. begin
  1514. if (sref.bitindexreg = NR_NO) then
  1515. begin
  1516. { use subsetreg routine, it may have been overridden with an optimized version }
  1517. tosreg.subsetreg := valuereg;
  1518. tosreg.subsetregsize := OS_INT;
  1519. { subsetregs always count bits from right to left }
  1520. if (target_info.endian = endian_big) then
  1521. tosreg.startbit := loadbitsize - (sref.startbit+sref.bitlen)
  1522. else
  1523. tosreg.startbit := sref.startbit;
  1524. tosreg.bitlen := sref.bitlen;
  1525. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1526. end
  1527. else
  1528. begin
  1529. if (sref.startbit <> 0) then
  1530. internalerror(2006081710);
  1531. { should be handled by normal code and will give wrong result }
  1532. { on x86 for the '1 shl bitlen' below }
  1533. if (sref.bitlen = AIntBits) then
  1534. internalerror(2006081711);
  1535. { zero the bits we have to insert }
  1536. if (slopt <> SL_SETMAX) then
  1537. begin
  1538. maskreg := getintregister(list,OS_INT);
  1539. if (target_info.endian = endian_big) then
  1540. begin
  1541. a_load_const_reg(list,OS_INT,tcgint((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen),maskreg);
  1542. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,maskreg);
  1543. end
  1544. else
  1545. begin
  1546. a_load_const_reg(list,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1547. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,maskreg);
  1548. end;
  1549. a_op_reg_reg(list,OP_NOT,OS_INT,maskreg,maskreg);
  1550. a_op_reg_reg(list,OP_AND,OS_INT,maskreg,valuereg);
  1551. end;
  1552. { insert the value }
  1553. if (slopt <> SL_SETZERO) then
  1554. begin
  1555. tmpreg := getintregister(list,OS_INT);
  1556. if (slopt <> SL_SETMAX) then
  1557. a_load_reg_reg(list,fromsize,OS_INT,fromreg,tmpreg)
  1558. else if (sref.bitlen <> AIntBits) then
  1559. a_load_const_reg(list,OS_INT,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  1560. else
  1561. a_load_const_reg(list,OS_INT,-1,tmpreg);
  1562. if (target_info.endian = endian_big) then
  1563. begin
  1564. a_op_const_reg(list,OP_SHL,OS_INT,loadbitsize-sref.bitlen,tmpreg);
  1565. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1566. begin
  1567. if (loadbitsize <> AIntBits) then
  1568. bitmask := (((aword(1) shl loadbitsize)-1) xor ((aword(1) shl (loadbitsize-sref.bitlen))-1))
  1569. else
  1570. bitmask := (high(aword) xor ((aword(1) shl (loadbitsize-sref.bitlen))-1));
  1571. a_op_const_reg(list,OP_AND,OS_INT,bitmask,tmpreg);
  1572. end;
  1573. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,tmpreg);
  1574. end
  1575. else
  1576. begin
  1577. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1578. a_op_const_reg(list,OP_AND,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1579. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,tmpreg);
  1580. end;
  1581. a_op_reg_reg(list,OP_OR,OS_INT,tmpreg,valuereg);
  1582. end;
  1583. end;
  1584. { store back to memory }
  1585. valuereg := makeregsize(list,valuereg,loadsize);
  1586. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1587. exit;
  1588. end
  1589. else
  1590. begin
  1591. { load next value }
  1592. extra_value_reg := getintregister(list,OS_INT);
  1593. tmpref := sref.ref;
  1594. inc(tmpref.offset,loadbitsize div 8);
  1595. { should maybe be taken out too, can be done more efficiently }
  1596. { on e.g. i386 with shld/shrd }
  1597. if (sref.bitindexreg = NR_NO) then
  1598. begin
  1599. a_load_ref_reg(list,loadsize,OS_INT,tmpref,extra_value_reg);
  1600. fromsreg.subsetreg := fromreg;
  1601. fromsreg.subsetregsize := fromsize;
  1602. tosreg.subsetreg := valuereg;
  1603. tosreg.subsetregsize := OS_INT;
  1604. { transfer first part }
  1605. fromsreg.bitlen := loadbitsize-sref.startbit;
  1606. tosreg.bitlen := fromsreg.bitlen;
  1607. if (target_info.endian = endian_big) then
  1608. begin
  1609. { valuereg must contain the upper bits of the value at bits [0..loadbitsize-startbit] }
  1610. { upper bits of the value ... }
  1611. fromsreg.startbit := sref.bitlen-(loadbitsize-sref.startbit);
  1612. { ... to bit 0 }
  1613. tosreg.startbit := 0
  1614. end
  1615. else
  1616. begin
  1617. { valuereg must contain the lower bits of the value at bits [startbit..loadbitsize] }
  1618. { lower bits of the value ... }
  1619. fromsreg.startbit := 0;
  1620. { ... to startbit }
  1621. tosreg.startbit := sref.startbit;
  1622. end;
  1623. case slopt of
  1624. SL_SETZERO,
  1625. SL_SETMAX:
  1626. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1627. else
  1628. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1629. end;
  1630. valuereg := makeregsize(list,valuereg,loadsize);
  1631. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1632. { transfer second part }
  1633. if (target_info.endian = endian_big) then
  1634. begin
  1635. { extra_value_reg must contain the lower bits of the value at bits }
  1636. { [(loadbitsize-(bitlen-(loadbitsize-startbit)))..loadbitsize] }
  1637. { (loadbitsize-(bitlen-(loadbitsize-startbit))) = 2*loadbitsize }
  1638. { - bitlen - startbit }
  1639. fromsreg.startbit := 0;
  1640. tosreg.startbit := 2*loadbitsize - sref.bitlen - sref.startbit
  1641. end
  1642. else
  1643. begin
  1644. { extra_value_reg must contain the upper bits of the value at bits [0..bitlen-(loadbitsize-startbit)] }
  1645. fromsreg.startbit := fromsreg.bitlen;
  1646. tosreg.startbit := 0;
  1647. end;
  1648. tosreg.subsetreg := extra_value_reg;
  1649. fromsreg.bitlen := sref.bitlen-fromsreg.bitlen;
  1650. tosreg.bitlen := fromsreg.bitlen;
  1651. case slopt of
  1652. SL_SETZERO,
  1653. SL_SETMAX:
  1654. a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,tosreg,slopt);
  1655. else
  1656. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1657. end;
  1658. extra_value_reg := makeregsize(list,extra_value_reg,loadsize);
  1659. a_load_reg_ref(list,loadsize,loadsize,extra_value_reg,tmpref);
  1660. exit;
  1661. end
  1662. else
  1663. begin
  1664. if (sref.startbit <> 0) then
  1665. internalerror(2006081812);
  1666. { should be handled by normal code and will give wrong result }
  1667. { on x86 for the '1 shl bitlen' below }
  1668. if (sref.bitlen = AIntBits) then
  1669. internalerror(2006081713);
  1670. { generate mask to zero the bits we have to insert }
  1671. if (slopt <> SL_SETMAX) then
  1672. begin
  1673. maskreg := getintregister(list,OS_INT);
  1674. if (target_info.endian = endian_big) then
  1675. begin
  1676. a_load_const_reg(list,OS_INT,tcgint(((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen)),maskreg);
  1677. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,maskreg);
  1678. end
  1679. else
  1680. begin
  1681. a_load_const_reg(list,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1682. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,maskreg);
  1683. end;
  1684. a_op_reg_reg(list,OP_NOT,OS_INT,maskreg,maskreg);
  1685. a_op_reg_reg(list,OP_AND,OS_INT,maskreg,valuereg);
  1686. end;
  1687. { insert the value }
  1688. if (slopt <> SL_SETZERO) then
  1689. begin
  1690. tmpreg := getintregister(list,OS_INT);
  1691. if (slopt <> SL_SETMAX) then
  1692. a_load_reg_reg(list,fromsize,OS_INT,fromreg,tmpreg)
  1693. else if (sref.bitlen <> AIntBits) then
  1694. a_load_const_reg(list,OS_INT,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  1695. else
  1696. a_load_const_reg(list,OS_INT,-1,tmpreg);
  1697. if (target_info.endian = endian_big) then
  1698. begin
  1699. a_op_const_reg(list,OP_SHL,OS_INT,loadbitsize-sref.bitlen,tmpreg);
  1700. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1701. { mask left over bits }
  1702. a_op_const_reg(list,OP_AND,OS_INT,tcgint(((aword(1) shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen)),tmpreg);
  1703. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,tmpreg);
  1704. end
  1705. else
  1706. begin
  1707. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1708. { mask left over bits }
  1709. a_op_const_reg(list,OP_AND,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1710. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,tmpreg);
  1711. end;
  1712. a_op_reg_reg(list,OP_OR,OS_INT,tmpreg,valuereg);
  1713. end;
  1714. valuereg := makeregsize(list,valuereg,loadsize);
  1715. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1716. { make sure we do not read/write past the end of the array }
  1717. current_asmdata.getjumplabel(hl);
  1718. a_cmp_const_reg_label(list,OS_INT,OC_BE,loadbitsize-sref.bitlen,sref.bitindexreg,hl);
  1719. a_load_ref_reg(list,loadsize,OS_INT,tmpref,extra_value_reg);
  1720. tmpindexreg := getintregister(list,OS_INT);
  1721. { load current array value }
  1722. if (slopt <> SL_SETZERO) then
  1723. begin
  1724. tmpreg := getintregister(list,OS_INT);
  1725. if (slopt <> SL_SETMAX) then
  1726. a_load_reg_reg(list,fromsize,OS_INT,fromreg,tmpreg)
  1727. else if (sref.bitlen <> AIntBits) then
  1728. a_load_const_reg(list,OS_INT,tcgint((aword(1) shl sref.bitlen) - 1), tmpreg)
  1729. else
  1730. a_load_const_reg(list,OS_INT,-1,tmpreg);
  1731. end;
  1732. { generate mask to zero the bits we have to insert }
  1733. if (slopt <> SL_SETMAX) then
  1734. begin
  1735. maskreg := getintregister(list,OS_INT);
  1736. if (target_info.endian = endian_big) then
  1737. begin
  1738. a_op_const_reg_reg(list,OP_ADD,OS_INT,sref.bitlen-2*loadbitsize,sref.bitindexreg,tmpindexreg);
  1739. a_op_reg_reg(list,OP_NEG,OS_INT,tmpindexreg,tmpindexreg);
  1740. a_load_const_reg(list,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1741. a_op_reg_reg(list,OP_SHL,OS_INT,tmpindexreg,maskreg);
  1742. end
  1743. else
  1744. begin
  1745. { Y-x = -(x-Y) }
  1746. a_op_const_reg_reg(list,OP_SUB,OS_INT,loadbitsize,sref.bitindexreg,tmpindexreg);
  1747. a_op_reg_reg(list,OP_NEG,OS_INT,tmpindexreg,tmpindexreg);
  1748. a_load_const_reg(list,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),maskreg);
  1749. a_op_reg_reg(list,OP_SHR,OS_INT,tmpindexreg,maskreg);
  1750. end;
  1751. a_op_reg_reg(list,OP_NOT,OS_INT,maskreg,maskreg);
  1752. a_op_reg_reg(list,OP_AND,OS_INT,maskreg,extra_value_reg);
  1753. end;
  1754. if (slopt <> SL_SETZERO) then
  1755. begin
  1756. if (target_info.endian = endian_big) then
  1757. a_op_reg_reg(list,OP_SHL,OS_INT,tmpindexreg,tmpreg)
  1758. else
  1759. begin
  1760. if not(slopt in [SL_REGNOSRCMASK,SL_SETMAX]) then
  1761. a_op_const_reg(list,OP_AND,OS_INT,tcgint((aword(1) shl sref.bitlen)-1),tmpreg);
  1762. a_op_reg_reg(list,OP_SHR,OS_INT,tmpindexreg,tmpreg);
  1763. end;
  1764. a_op_reg_reg(list,OP_OR,OS_INT,tmpreg,extra_value_reg);
  1765. end;
  1766. extra_value_reg := makeregsize(list,extra_value_reg,loadsize);
  1767. a_load_reg_ref(list,loadsize,loadsize,extra_value_reg,tmpref);
  1768. a_label(list,hl);
  1769. end;
  1770. end;
  1771. end;
  1772. procedure tcg.a_load_subsetref_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsref, tosref: tsubsetreference);
  1773. var
  1774. tmpreg: tregister;
  1775. begin
  1776. tmpreg := getintregister(list,tosubsetsize);
  1777. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1778. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1779. end;
  1780. procedure tcg.a_load_subsetref_ref(list : TAsmList; subsetsize, tosize: tcgsize; const sref: tsubsetreference; const destref: treference);
  1781. var
  1782. tmpreg: tregister;
  1783. begin
  1784. tmpreg := getintregister(list,tosize);
  1785. a_load_subsetref_reg(list,subsetsize,tosize,sref,tmpreg);
  1786. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  1787. end;
  1788. procedure tcg.a_load_ref_subsetref(list : TAsmList; fromsize, subsetsize: tcgsize; const fromref: treference; const sref: tsubsetreference);
  1789. var
  1790. tmpreg: tregister;
  1791. begin
  1792. tmpreg := getintregister(list,subsetsize);
  1793. a_load_ref_reg(list,fromsize,subsetsize,fromref,tmpreg);
  1794. a_load_reg_subsetref(list,subsetsize,subsetsize,tmpreg,sref);
  1795. end;
  1796. procedure tcg.a_load_const_subsetref(list: TAsmlist; subsetsize: tcgsize; a: tcgint; const sref: tsubsetreference);
  1797. var
  1798. tmpreg: tregister;
  1799. slopt: tsubsetloadopt;
  1800. begin
  1801. { perform masking of the source value in advance }
  1802. slopt := SL_REGNOSRCMASK;
  1803. if (sref.bitlen <> AIntBits) then
  1804. a := tcgint(aword(a) and ((aword(1) shl sref.bitlen) -1));
  1805. if (
  1806. { broken x86 "x shl regbitsize = x" }
  1807. ((sref.bitlen <> AIntBits) and
  1808. ((aword(a) and ((aword(1) shl sref.bitlen) -1)) = (aword(1) shl sref.bitlen) -1)) or
  1809. ((sref.bitlen = AIntBits) and
  1810. (a = -1))
  1811. ) then
  1812. slopt := SL_SETMAX
  1813. else if (a = 0) then
  1814. slopt := SL_SETZERO;
  1815. tmpreg := getintregister(list,subsetsize);
  1816. if not(slopt in [SL_SETZERO,SL_SETMAX]) then
  1817. a_load_const_reg(list,subsetsize,a,tmpreg);
  1818. a_load_regconst_subsetref_intern(list,subsetsize,subsetsize,tmpreg,sref,slopt);
  1819. end;
  1820. procedure tcg.a_load_subsetref_loc(list: TAsmlist; subsetsize: tcgsize; const sref: tsubsetreference; const loc: tlocation);
  1821. begin
  1822. case loc.loc of
  1823. LOC_REFERENCE,LOC_CREFERENCE:
  1824. a_load_subsetref_ref(list,subsetsize,loc.size,sref,loc.reference);
  1825. LOC_REGISTER,LOC_CREGISTER:
  1826. a_load_subsetref_reg(list,subsetsize,loc.size,sref,loc.register);
  1827. LOC_SUBSETREG,LOC_CSUBSETREG:
  1828. a_load_subsetref_subsetreg(list,subsetsize,loc.size,sref,loc.sreg);
  1829. LOC_SUBSETREF,LOC_CSUBSETREF:
  1830. a_load_subsetref_subsetref(list,subsetsize,loc.size,sref,loc.sref);
  1831. else
  1832. internalerror(200608054);
  1833. end;
  1834. end;
  1835. procedure tcg.a_load_subsetref_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsref: tsubsetreference; const tosreg: tsubsetregister);
  1836. var
  1837. tmpreg: tregister;
  1838. begin
  1839. tmpreg := getintregister(list,tosubsetsize);
  1840. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1841. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  1842. end;
  1843. procedure tcg.a_load_subsetreg_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsreg: tsubsetregister; const tosref: tsubsetreference);
  1844. var
  1845. tmpreg: tregister;
  1846. begin
  1847. tmpreg := getintregister(list,tosubsetsize);
  1848. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  1849. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1850. end;
  1851. {$ifdef rangeon}
  1852. {$r+}
  1853. {$undef rangeon}
  1854. {$endif}
  1855. {$ifdef overflowon}
  1856. {$q+}
  1857. {$undef overflowon}
  1858. {$endif}
  1859. { generic bit address calculation routines }
  1860. function tcg.get_bit_const_ref_sref(bitnumber: tcgint; const ref: treference): tsubsetreference;
  1861. begin
  1862. result.ref:=ref;
  1863. inc(result.ref.offset,bitnumber div 8);
  1864. result.bitindexreg:=NR_NO;
  1865. result.startbit:=bitnumber mod 8;
  1866. result.bitlen:=1;
  1867. end;
  1868. function tcg.get_bit_const_reg_sreg(setregsize: tcgsize; bitnumber: tcgint; setreg: tregister): tsubsetregister;
  1869. begin
  1870. result.subsetreg:=setreg;
  1871. result.subsetregsize:=setregsize;
  1872. { subsetregs always count from the least significant to the most significant bit }
  1873. if (target_info.endian=endian_big) then
  1874. result.startbit:=(tcgsize2size[setregsize]*8)-bitnumber-1
  1875. else
  1876. result.startbit:=bitnumber;
  1877. result.bitlen:=1;
  1878. end;
  1879. function tcg.get_bit_reg_ref_sref(list: TAsmList; bitnumbersize: tcgsize; bitnumber: tregister; const ref: treference): tsubsetreference;
  1880. var
  1881. tmpreg,
  1882. tmpaddrreg: tregister;
  1883. begin
  1884. result.ref:=ref;
  1885. result.startbit:=0;
  1886. result.bitlen:=1;
  1887. tmpreg:=getintregister(list,bitnumbersize);
  1888. a_op_const_reg_reg(list,OP_SHR,bitnumbersize,3,bitnumber,tmpreg);
  1889. tmpaddrreg:=getaddressregister(list);
  1890. a_load_reg_reg(list,bitnumbersize,OS_ADDR,tmpreg,tmpaddrreg);
  1891. if (result.ref.base=NR_NO) then
  1892. result.ref.base:=tmpaddrreg
  1893. else if (result.ref.index=NR_NO) then
  1894. result.ref.index:=tmpaddrreg
  1895. else
  1896. begin
  1897. a_op_reg_reg(list,OP_ADD,OS_ADDR,result.ref.index,tmpaddrreg);
  1898. result.ref.index:=tmpaddrreg;
  1899. end;
  1900. tmpreg:=getintregister(list,OS_INT);
  1901. a_op_const_reg_reg(list,OP_AND,OS_INT,7,bitnumber,tmpreg);
  1902. result.bitindexreg:=tmpreg;
  1903. end;
  1904. { bit testing routines }
  1905. procedure tcg.a_bit_test_reg_reg_reg(list : TAsmList; bitnumbersize,valuesize,destsize: tcgsize;bitnumber,value,destreg: tregister);
  1906. var
  1907. tmpvalue: tregister;
  1908. begin
  1909. tmpvalue:=getintregister(list,valuesize);
  1910. if (target_info.endian=endian_little) then
  1911. begin
  1912. { rotate value register "bitnumber" bits to the right }
  1913. a_op_reg_reg_reg(list,OP_SHR,valuesize,bitnumber,value,tmpvalue);
  1914. { extract the bit we want }
  1915. a_op_const_reg(list,OP_AND,valuesize,1,tmpvalue);
  1916. end
  1917. else
  1918. begin
  1919. { highest (leftmost) bit = bit 0 -> shl bitnumber results in wanted }
  1920. { bit in uppermost position, then move it to the lowest position }
  1921. { "and" is not necessary since combination of shl/shr will clear }
  1922. { all other bits }
  1923. a_op_reg_reg_reg(list,OP_SHL,valuesize,bitnumber,value,tmpvalue);
  1924. a_op_const_reg(list,OP_SHR,valuesize,tcgsize2size[valuesize]*8-1,tmpvalue);
  1925. end;
  1926. a_load_reg_reg(list,valuesize,destsize,tmpvalue,destreg);
  1927. end;
  1928. procedure tcg.a_bit_test_const_ref_reg(list: TAsmList; destsize: tcgsize; bitnumber: tcgint; const ref: treference; destreg: tregister);
  1929. begin
  1930. a_load_subsetref_reg(list,OS_8,destsize,get_bit_const_ref_sref(bitnumber,ref),destreg);
  1931. end;
  1932. procedure tcg.a_bit_test_const_reg_reg(list: TAsmList; setregsize, destsize: tcgsize; bitnumber: tcgint; setreg, destreg: tregister);
  1933. begin
  1934. a_load_subsetreg_reg(list,setregsize,destsize,get_bit_const_reg_sreg(setregsize,bitnumber,setreg),destreg);
  1935. end;
  1936. procedure tcg.a_bit_test_const_subsetreg_reg(list: TAsmList; setregsize, destsize: tcgsize; bitnumber: tcgint; const setreg: tsubsetregister; destreg: tregister);
  1937. var
  1938. tmpsreg: tsubsetregister;
  1939. begin
  1940. { the first parameter is used to calculate the bit offset in }
  1941. { case of big endian, and therefore must be the size of the }
  1942. { set and not of the whole subsetreg }
  1943. tmpsreg:=get_bit_const_reg_sreg(setregsize,bitnumber,setreg.subsetreg);
  1944. { now fix the size of the subsetreg }
  1945. tmpsreg.subsetregsize:=setreg.subsetregsize;
  1946. { correct offset of the set in the subsetreg }
  1947. inc(tmpsreg.startbit,setreg.startbit);
  1948. a_load_subsetreg_reg(list,setregsize,destsize,tmpsreg,destreg);
  1949. end;
  1950. procedure tcg.a_bit_test_reg_ref_reg(list: TAsmList; bitnumbersize, destsize: tcgsize; bitnumber: tregister; const ref: treference; destreg: tregister);
  1951. begin
  1952. a_load_subsetref_reg(list,OS_8,destsize,get_bit_reg_ref_sref(list,bitnumbersize,bitnumber,ref),destreg);
  1953. end;
  1954. procedure tcg.a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, destsize: tcgsize; bitnumber: tregister; const loc: tlocation; destreg: tregister);
  1955. var
  1956. tmpreg: tregister;
  1957. begin
  1958. case loc.loc of
  1959. LOC_REFERENCE,LOC_CREFERENCE:
  1960. a_bit_test_reg_ref_reg(list,bitnumbersize,destsize,bitnumber,loc.reference,destreg);
  1961. LOC_REGISTER,LOC_CREGISTER,
  1962. LOC_SUBSETREG,LOC_CSUBSETREG,
  1963. LOC_CONSTANT:
  1964. begin
  1965. case loc.loc of
  1966. LOC_REGISTER,LOC_CREGISTER:
  1967. tmpreg:=loc.register;
  1968. LOC_SUBSETREG,LOC_CSUBSETREG:
  1969. begin
  1970. tmpreg:=getintregister(list,loc.size);
  1971. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  1972. end;
  1973. LOC_CONSTANT:
  1974. begin
  1975. tmpreg:=getintregister(list,loc.size);
  1976. a_load_const_reg(list,loc.size,loc.value,tmpreg);
  1977. end;
  1978. end;
  1979. a_bit_test_reg_reg_reg(list,bitnumbersize,loc.size,destsize,bitnumber,tmpreg,destreg);
  1980. end;
  1981. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1982. else
  1983. internalerror(2007051701);
  1984. end;
  1985. end;
  1986. procedure tcg.a_bit_test_const_loc_reg(list: TAsmList; destsize: tcgsize; bitnumber: tcgint; const loc: tlocation; destreg: tregister);
  1987. begin
  1988. case loc.loc of
  1989. LOC_REFERENCE,LOC_CREFERENCE:
  1990. a_bit_test_const_ref_reg(list,destsize,bitnumber,loc.reference,destreg);
  1991. LOC_REGISTER,LOC_CREGISTER:
  1992. a_bit_test_const_reg_reg(list,loc.size,destsize,bitnumber,loc.register,destreg);
  1993. LOC_SUBSETREG,LOC_CSUBSETREG:
  1994. a_bit_test_const_subsetreg_reg(list,loc.size,destsize,bitnumber,loc.sreg,destreg);
  1995. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  1996. else
  1997. internalerror(2007051702);
  1998. end;
  1999. end;
  2000. { bit setting/clearing routines }
  2001. procedure tcg.a_bit_set_reg_reg(list : TAsmList; doset: boolean; bitnumbersize, destsize: tcgsize; bitnumber,dest: tregister);
  2002. var
  2003. tmpvalue: tregister;
  2004. begin
  2005. tmpvalue:=getintregister(list,destsize);
  2006. if (target_info.endian=endian_little) then
  2007. begin
  2008. a_load_const_reg(list,destsize,1,tmpvalue);
  2009. { rotate bit "bitnumber" bits to the left }
  2010. a_op_reg_reg(list,OP_SHL,destsize,bitnumber,tmpvalue);
  2011. end
  2012. else
  2013. begin
  2014. { highest (leftmost) bit = bit 0 -> "$80/$8000/$80000000/ ... }
  2015. { shr bitnumber" results in correct mask }
  2016. a_load_const_reg(list,destsize,1 shl (tcgsize2size[destsize]*8-1),tmpvalue);
  2017. a_op_reg_reg(list,OP_SHR,destsize,bitnumber,tmpvalue);
  2018. end;
  2019. { set/clear the bit we want }
  2020. if (doset) then
  2021. a_op_reg_reg(list,OP_OR,destsize,tmpvalue,dest)
  2022. else
  2023. begin
  2024. a_op_reg_reg(list,OP_NOT,destsize,tmpvalue,tmpvalue);
  2025. a_op_reg_reg(list,OP_AND,destsize,tmpvalue,dest)
  2026. end;
  2027. end;
  2028. procedure tcg.a_bit_set_const_ref(list: TAsmList; doset: boolean;destsize: tcgsize; bitnumber: tcgint; const ref: treference);
  2029. begin
  2030. a_load_const_subsetref(list,OS_8,ord(doset),get_bit_const_ref_sref(bitnumber,ref));
  2031. end;
  2032. procedure tcg.a_bit_set_const_reg(list: TAsmList; doset: boolean; destsize: tcgsize; bitnumber: tcgint; destreg: tregister);
  2033. begin
  2034. a_load_const_subsetreg(list,OS_8,ord(doset),get_bit_const_reg_sreg(destsize,bitnumber,destreg));
  2035. end;
  2036. procedure tcg.a_bit_set_const_subsetreg(list: TAsmList; doset: boolean; destsize: tcgsize; bitnumber: tcgint; const destreg: tsubsetregister);
  2037. var
  2038. tmpsreg: tsubsetregister;
  2039. begin
  2040. { the first parameter is used to calculate the bit offset in }
  2041. { case of big endian, and therefore must be the size of the }
  2042. { set and not of the whole subsetreg }
  2043. tmpsreg:=get_bit_const_reg_sreg(destsize,bitnumber,destreg.subsetreg);
  2044. { now fix the size of the subsetreg }
  2045. tmpsreg.subsetregsize:=destreg.subsetregsize;
  2046. { correct offset of the set in the subsetreg }
  2047. inc(tmpsreg.startbit,destreg.startbit);
  2048. a_load_const_subsetreg(list,OS_8,ord(doset),tmpsreg);
  2049. end;
  2050. procedure tcg.a_bit_set_reg_ref(list: TAsmList; doset: boolean; bitnumbersize: tcgsize; bitnumber: tregister; const ref: treference);
  2051. begin
  2052. a_load_const_subsetref(list,OS_8,ord(doset),get_bit_reg_ref_sref(list,bitnumbersize,bitnumber,ref));
  2053. end;
  2054. procedure tcg.a_bit_set_reg_loc(list: TAsmList; doset: boolean; bitnumbersize: tcgsize; bitnumber: tregister; const loc: tlocation);
  2055. var
  2056. tmpreg: tregister;
  2057. begin
  2058. case loc.loc of
  2059. LOC_REFERENCE:
  2060. a_bit_set_reg_ref(list,doset,bitnumbersize,bitnumber,loc.reference);
  2061. LOC_CREGISTER:
  2062. a_bit_set_reg_reg(list,doset,bitnumbersize,loc.size,bitnumber,loc.register);
  2063. { e.g. a 2-byte set in a record regvar }
  2064. LOC_CSUBSETREG:
  2065. begin
  2066. { hard to do in-place in a generic way, so operate on a copy }
  2067. tmpreg:=getintregister(list,loc.size);
  2068. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  2069. a_bit_set_reg_reg(list,doset,bitnumbersize,loc.size,bitnumber,tmpreg);
  2070. a_load_reg_subsetreg(list,loc.size,loc.size,tmpreg,loc.sreg);
  2071. end;
  2072. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  2073. else
  2074. internalerror(2007051703)
  2075. end;
  2076. end;
  2077. procedure tcg.a_bit_set_const_loc(list: TAsmList; doset: boolean; bitnumber: tcgint; const loc: tlocation);
  2078. begin
  2079. case loc.loc of
  2080. LOC_REFERENCE:
  2081. a_bit_set_const_ref(list,doset,loc.size,bitnumber,loc.reference);
  2082. LOC_CREGISTER:
  2083. a_bit_set_const_reg(list,doset,loc.size,bitnumber,loc.register);
  2084. LOC_CSUBSETREG:
  2085. a_bit_set_const_subsetreg(list,doset,loc.size,bitnumber,loc.sreg);
  2086. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  2087. else
  2088. internalerror(2007051704)
  2089. end;
  2090. end;
  2091. { memory/register loading }
  2092. procedure tcg.a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  2093. var
  2094. tmpref : treference;
  2095. tmpreg : tregister;
  2096. i : longint;
  2097. begin
  2098. if ref.alignment<tcgsize2size[fromsize] then
  2099. begin
  2100. tmpref:=ref;
  2101. { we take care of the alignment now }
  2102. tmpref.alignment:=0;
  2103. case FromSize of
  2104. OS_16,OS_S16:
  2105. begin
  2106. tmpreg:=getintregister(list,OS_16);
  2107. a_load_reg_reg(list,fromsize,OS_16,register,tmpreg);
  2108. if target_info.endian=endian_big then
  2109. inc(tmpref.offset);
  2110. tmpreg:=makeregsize(list,tmpreg,OS_8);
  2111. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  2112. tmpreg:=makeregsize(list,tmpreg,OS_16);
  2113. a_op_const_reg(list,OP_SHR,OS_16,8,tmpreg);
  2114. if target_info.endian=endian_big then
  2115. dec(tmpref.offset)
  2116. else
  2117. inc(tmpref.offset);
  2118. tmpreg:=makeregsize(list,tmpreg,OS_8);
  2119. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  2120. end;
  2121. OS_32,OS_S32:
  2122. begin
  2123. { could add an optimised case for ref.alignment=2 }
  2124. tmpreg:=getintregister(list,OS_32);
  2125. a_load_reg_reg(list,fromsize,OS_32,register,tmpreg);
  2126. if target_info.endian=endian_big then
  2127. inc(tmpref.offset,3);
  2128. tmpreg:=makeregsize(list,tmpreg,OS_8);
  2129. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  2130. tmpreg:=makeregsize(list,tmpreg,OS_32);
  2131. for i:=1 to 3 do
  2132. begin
  2133. a_op_const_reg(list,OP_SHR,OS_32,8,tmpreg);
  2134. if target_info.endian=endian_big then
  2135. dec(tmpref.offset)
  2136. else
  2137. inc(tmpref.offset);
  2138. tmpreg:=makeregsize(list,tmpreg,OS_8);
  2139. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  2140. tmpreg:=makeregsize(list,tmpreg,OS_32);
  2141. end;
  2142. end
  2143. else
  2144. a_load_reg_ref(list,fromsize,tosize,register,tmpref);
  2145. end;
  2146. end
  2147. else
  2148. a_load_reg_ref(list,fromsize,tosize,register,ref);
  2149. end;
  2150. procedure tcg.a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  2151. var
  2152. tmpref : treference;
  2153. tmpreg,
  2154. tmpreg2 : tregister;
  2155. i : longint;
  2156. begin
  2157. if ref.alignment in [1,2] then
  2158. begin
  2159. tmpref:=ref;
  2160. { we take care of the alignment now }
  2161. tmpref.alignment:=0;
  2162. case FromSize of
  2163. OS_16,OS_S16:
  2164. if ref.alignment=2 then
  2165. a_load_ref_reg(list,fromsize,tosize,tmpref,register)
  2166. else
  2167. begin
  2168. { first load in tmpreg, because the target register }
  2169. { may be used in ref as well }
  2170. if target_info.endian=endian_little then
  2171. inc(tmpref.offset);
  2172. tmpreg:=getintregister(list,OS_8);
  2173. a_load_ref_reg(list,OS_8,OS_8,tmpref,tmpreg);
  2174. tmpreg:=makeregsize(list,tmpreg,OS_16);
  2175. a_op_const_reg(list,OP_SHL,OS_16,8,tmpreg);
  2176. if target_info.endian=endian_little then
  2177. dec(tmpref.offset)
  2178. else
  2179. inc(tmpref.offset);
  2180. a_load_ref_reg(list,OS_8,OS_16,tmpref,register);
  2181. a_op_reg_reg(list,OP_OR,OS_16,tmpreg,register);
  2182. end;
  2183. OS_32,OS_S32:
  2184. if ref.alignment=2 then
  2185. begin
  2186. if target_info.endian=endian_little then
  2187. inc(tmpref.offset,2);
  2188. tmpreg:=getintregister(list,OS_32);
  2189. a_load_ref_reg(list,OS_16,OS_32,tmpref,tmpreg);
  2190. a_op_const_reg(list,OP_SHL,OS_32,16,tmpreg);
  2191. if target_info.endian=endian_little then
  2192. dec(tmpref.offset,2)
  2193. else
  2194. inc(tmpref.offset,2);
  2195. a_load_ref_reg(list,OS_16,OS_32,tmpref,register);
  2196. a_op_reg_reg(list,OP_OR,OS_32,tmpreg,register);
  2197. end
  2198. else
  2199. begin
  2200. if target_info.endian=endian_little then
  2201. inc(tmpref.offset,3);
  2202. tmpreg:=getintregister(list,OS_32);
  2203. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg);
  2204. tmpreg2:=getintregister(list,OS_32);
  2205. for i:=1 to 3 do
  2206. begin
  2207. a_op_const_reg(list,OP_SHL,OS_32,8,tmpreg);
  2208. if target_info.endian=endian_little then
  2209. dec(tmpref.offset)
  2210. else
  2211. inc(tmpref.offset);
  2212. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg2);
  2213. a_op_reg_reg(list,OP_OR,OS_32,tmpreg2,tmpreg);
  2214. end;
  2215. a_load_reg_reg(list,OS_32,OS_32,tmpreg,register);
  2216. end
  2217. else
  2218. a_load_ref_reg(list,fromsize,tosize,tmpref,register);
  2219. end;
  2220. end
  2221. else
  2222. a_load_ref_reg(list,fromsize,tosize,ref,register);
  2223. end;
  2224. procedure tcg.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  2225. var
  2226. tmpreg: tregister;
  2227. begin
  2228. { verify if we have the same reference }
  2229. if references_equal(sref,dref) then
  2230. exit;
  2231. tmpreg:=getintregister(list,tosize);
  2232. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  2233. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  2234. end;
  2235. procedure tcg.a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : treference);
  2236. var
  2237. tmpreg: tregister;
  2238. begin
  2239. tmpreg:=getintregister(list,size);
  2240. a_load_const_reg(list,size,a,tmpreg);
  2241. a_load_reg_ref(list,size,size,tmpreg,ref);
  2242. end;
  2243. procedure tcg.a_load_const_loc(list : TAsmList;a : tcgint;const loc: tlocation);
  2244. begin
  2245. case loc.loc of
  2246. LOC_REFERENCE,LOC_CREFERENCE:
  2247. a_load_const_ref(list,loc.size,a,loc.reference);
  2248. LOC_REGISTER,LOC_CREGISTER:
  2249. a_load_const_reg(list,loc.size,a,loc.register);
  2250. LOC_SUBSETREG,LOC_CSUBSETREG:
  2251. a_load_const_subsetreg(list,loc.size,a,loc.sreg);
  2252. LOC_SUBSETREF,LOC_CSUBSETREF:
  2253. a_load_const_subsetref(list,loc.size,a,loc.sref);
  2254. else
  2255. internalerror(200203272);
  2256. end;
  2257. end;
  2258. procedure tcg.a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  2259. begin
  2260. case loc.loc of
  2261. LOC_REFERENCE,LOC_CREFERENCE:
  2262. a_load_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  2263. LOC_REGISTER,LOC_CREGISTER:
  2264. a_load_reg_reg(list,fromsize,loc.size,reg,loc.register);
  2265. LOC_SUBSETREG,LOC_CSUBSETREG:
  2266. a_load_reg_subsetreg(list,fromsize,loc.size,reg,loc.sreg);
  2267. LOC_SUBSETREF,LOC_CSUBSETREF:
  2268. a_load_reg_subsetref(list,fromsize,loc.size,reg,loc.sref);
  2269. LOC_MMREGISTER,LOC_CMMREGISTER:
  2270. a_loadmm_intreg_reg(list,fromsize,loc.size,reg,loc.register,mms_movescalar);
  2271. else
  2272. internalerror(200203271);
  2273. end;
  2274. end;
  2275. procedure tcg.a_load_loc_reg(list : TAsmList; tosize: tcgsize; const loc: tlocation; reg : tregister);
  2276. begin
  2277. case loc.loc of
  2278. LOC_REFERENCE,LOC_CREFERENCE:
  2279. a_load_ref_reg(list,loc.size,tosize,loc.reference,reg);
  2280. LOC_REGISTER,LOC_CREGISTER:
  2281. a_load_reg_reg(list,loc.size,tosize,loc.register,reg);
  2282. LOC_CONSTANT:
  2283. a_load_const_reg(list,tosize,loc.value,reg);
  2284. LOC_SUBSETREG,LOC_CSUBSETREG:
  2285. a_load_subsetreg_reg(list,loc.size,tosize,loc.sreg,reg);
  2286. LOC_SUBSETREF,LOC_CSUBSETREF:
  2287. a_load_subsetref_reg(list,loc.size,tosize,loc.sref,reg);
  2288. else
  2289. internalerror(200109092);
  2290. end;
  2291. end;
  2292. procedure tcg.a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  2293. begin
  2294. case loc.loc of
  2295. LOC_REFERENCE,LOC_CREFERENCE:
  2296. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  2297. LOC_REGISTER,LOC_CREGISTER:
  2298. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  2299. LOC_CONSTANT:
  2300. a_load_const_ref(list,tosize,loc.value,ref);
  2301. LOC_SUBSETREG,LOC_CSUBSETREG:
  2302. a_load_subsetreg_ref(list,loc.size,tosize,loc.sreg,ref);
  2303. LOC_SUBSETREF,LOC_CSUBSETREF:
  2304. a_load_subsetref_ref(list,loc.size,tosize,loc.sref,ref);
  2305. else
  2306. internalerror(200109302);
  2307. end;
  2308. end;
  2309. procedure tcg.a_load_loc_subsetreg(list : TAsmList; subsetsize: tcgsize; const loc: tlocation; const sreg : tsubsetregister);
  2310. begin
  2311. case loc.loc of
  2312. LOC_REFERENCE,LOC_CREFERENCE:
  2313. a_load_ref_subsetreg(list,loc.size,subsetsize,loc.reference,sreg);
  2314. LOC_REGISTER,LOC_CREGISTER:
  2315. a_load_reg_subsetreg(list,loc.size,subsetsize,loc.register,sreg);
  2316. LOC_CONSTANT:
  2317. a_load_const_subsetreg(list,subsetsize,loc.value,sreg);
  2318. LOC_SUBSETREG,LOC_CSUBSETREG:
  2319. a_load_subsetreg_subsetreg(list,loc.size,subsetsize,loc.sreg,sreg);
  2320. LOC_SUBSETREF,LOC_CSUBSETREF:
  2321. a_load_subsetref_subsetreg(list,loc.size,subsetsize,loc.sref,sreg);
  2322. else
  2323. internalerror(2006052310);
  2324. end;
  2325. end;
  2326. procedure tcg.a_load_subsetreg_loc(list: TAsmlist; subsetsize: tcgsize; const sreg: tsubsetregister; const loc: tlocation);
  2327. begin
  2328. case loc.loc of
  2329. LOC_REFERENCE,LOC_CREFERENCE:
  2330. a_load_subsetreg_ref(list,subsetsize,loc.size,sreg,loc.reference);
  2331. LOC_REGISTER,LOC_CREGISTER:
  2332. a_load_subsetreg_reg(list,subsetsize,loc.size,sreg,loc.register);
  2333. LOC_SUBSETREG,LOC_CSUBSETREG:
  2334. a_load_subsetreg_subsetreg(list,subsetsize,loc.size,sreg,loc.sreg);
  2335. LOC_SUBSETREF,LOC_CSUBSETREF:
  2336. a_load_subsetreg_subsetref(list,subsetsize,loc.size,sreg,loc.sref);
  2337. else
  2338. internalerror(2006051510);
  2339. end;
  2340. end;
  2341. procedure tcg.optimize_op_const(var op: topcg; var a : tcgint);
  2342. var
  2343. powerval : longint;
  2344. begin
  2345. case op of
  2346. OP_OR :
  2347. begin
  2348. { or with zero returns same result }
  2349. if a = 0 then
  2350. op:=OP_NONE
  2351. else
  2352. { or with max returns max }
  2353. if a = -1 then
  2354. op:=OP_MOVE;
  2355. end;
  2356. OP_AND :
  2357. begin
  2358. { and with max returns same result }
  2359. if (a = -1) then
  2360. op:=OP_NONE
  2361. else
  2362. { and with 0 returns 0 }
  2363. if a=0 then
  2364. op:=OP_MOVE;
  2365. end;
  2366. OP_DIV :
  2367. begin
  2368. { division by 1 returns result }
  2369. if a = 1 then
  2370. op:=OP_NONE
  2371. else if ispowerof2(int64(a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  2372. begin
  2373. a := powerval;
  2374. op:= OP_SHR;
  2375. end;
  2376. end;
  2377. OP_IDIV:
  2378. begin
  2379. if a = 1 then
  2380. op:=OP_NONE;
  2381. end;
  2382. OP_MUL,OP_IMUL:
  2383. begin
  2384. if a = 1 then
  2385. op:=OP_NONE
  2386. else
  2387. if a=0 then
  2388. op:=OP_MOVE
  2389. else if ispowerof2(int64(a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  2390. begin
  2391. a := powerval;
  2392. op:= OP_SHL;
  2393. end;
  2394. end;
  2395. OP_ADD,OP_SUB:
  2396. begin
  2397. if a = 0 then
  2398. op:=OP_NONE;
  2399. end;
  2400. OP_SAR,OP_SHL,OP_SHR,OP_ROL,OP_ROR:
  2401. begin
  2402. if a = 0 then
  2403. op:=OP_NONE;
  2404. end;
  2405. end;
  2406. end;
  2407. procedure tcg.a_loadfpu_loc_reg(list: TAsmList; tosize: tcgsize; const loc: tlocation; const reg: tregister);
  2408. begin
  2409. case loc.loc of
  2410. LOC_REFERENCE, LOC_CREFERENCE:
  2411. a_loadfpu_ref_reg(list,loc.size,tosize,loc.reference,reg);
  2412. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  2413. a_loadfpu_reg_reg(list,loc.size,tosize,loc.register,reg);
  2414. else
  2415. internalerror(200203301);
  2416. end;
  2417. end;
  2418. procedure tcg.a_loadfpu_reg_loc(list: TAsmList; fromsize: tcgsize; const reg: tregister; const loc: tlocation);
  2419. begin
  2420. case loc.loc of
  2421. LOC_REFERENCE, LOC_CREFERENCE:
  2422. a_loadfpu_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  2423. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  2424. a_loadfpu_reg_reg(list,fromsize,loc.size,reg,loc.register);
  2425. else
  2426. internalerror(48991);
  2427. end;
  2428. end;
  2429. procedure tcg.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tcgsize; const ref1,ref2: treference);
  2430. var
  2431. reg: tregister;
  2432. regsize: tcgsize;
  2433. begin
  2434. if (fromsize>=tosize) then
  2435. regsize:=fromsize
  2436. else
  2437. regsize:=tosize;
  2438. reg:=getfpuregister(list,regsize);
  2439. a_loadfpu_ref_reg(list,fromsize,regsize,ref1,reg);
  2440. a_loadfpu_reg_ref(list,regsize,tosize,reg,ref2);
  2441. end;
  2442. procedure tcg.a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);
  2443. var
  2444. ref : treference;
  2445. begin
  2446. paramanager.alloccgpara(list,cgpara);
  2447. case cgpara.location^.loc of
  2448. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  2449. begin
  2450. cgpara.check_simple_location;
  2451. a_loadfpu_reg_reg(list,size,size,r,cgpara.location^.register);
  2452. end;
  2453. LOC_REFERENCE,LOC_CREFERENCE:
  2454. begin
  2455. cgpara.check_simple_location;
  2456. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  2457. a_loadfpu_reg_ref(list,size,size,r,ref);
  2458. end;
  2459. LOC_REGISTER,LOC_CREGISTER:
  2460. begin
  2461. { paramfpu_ref does the check_simpe_location check here if necessary }
  2462. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,ref);
  2463. a_loadfpu_reg_ref(list,size,size,r,ref);
  2464. a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  2465. tg.Ungettemp(list,ref);
  2466. end;
  2467. else
  2468. internalerror(2010053112);
  2469. end;
  2470. end;
  2471. procedure tcg.a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);
  2472. var
  2473. href : treference;
  2474. hsize: tcgsize;
  2475. begin
  2476. case cgpara.location^.loc of
  2477. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  2478. begin
  2479. cgpara.check_simple_location;
  2480. paramanager.alloccgpara(list,cgpara);
  2481. a_loadfpu_ref_reg(list,size,size,ref,cgpara.location^.register);
  2482. end;
  2483. LOC_REFERENCE,LOC_CREFERENCE:
  2484. begin
  2485. cgpara.check_simple_location;
  2486. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  2487. { concatcopy should choose the best way to copy the data }
  2488. g_concatcopy(list,ref,href,tcgsize2size[size]);
  2489. end;
  2490. LOC_REGISTER,LOC_CREGISTER:
  2491. begin
  2492. { force integer size }
  2493. hsize:=int_cgsize(tcgsize2size[size]);
  2494. {$ifndef cpu64bitalu}
  2495. if (hsize in [OS_S64,OS_64]) then
  2496. cg64.a_load64_ref_cgpara(list,ref,cgpara)
  2497. else
  2498. {$endif not cpu64bitalu}
  2499. begin
  2500. cgpara.check_simple_location;
  2501. a_load_ref_cgpara(list,hsize,ref,cgpara)
  2502. end;
  2503. end
  2504. else
  2505. internalerror(200402201);
  2506. end;
  2507. end;
  2508. procedure tcg.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  2509. var
  2510. tmpreg : tregister;
  2511. begin
  2512. tmpreg:=getintregister(list,size);
  2513. a_load_ref_reg(list,size,size,ref,tmpreg);
  2514. a_op_const_reg(list,op,size,a,tmpreg);
  2515. a_load_reg_ref(list,size,size,tmpreg,ref);
  2516. end;
  2517. procedure tcg.a_op_const_subsetreg(list : TAsmList; Op : TOpCG; size, subsetsize : TCGSize; a : tcgint; const sreg: tsubsetregister);
  2518. var
  2519. tmpreg: tregister;
  2520. begin
  2521. tmpreg := getintregister(list, size);
  2522. a_load_subsetreg_reg(list,subsetsize,size,sreg,tmpreg);
  2523. a_op_const_reg(list,op,size,a,tmpreg);
  2524. a_load_reg_subsetreg(list,size,subsetsize,tmpreg,sreg);
  2525. end;
  2526. procedure tcg.a_op_const_subsetref(list : TAsmList; Op : TOpCG; size, subsetsize : TCGSize; a : tcgint; const sref: tsubsetreference);
  2527. var
  2528. tmpreg: tregister;
  2529. begin
  2530. tmpreg := getintregister(list, size);
  2531. a_load_subsetref_reg(list,subsetsize,size,sref,tmpreg);
  2532. a_op_const_reg(list,op,size,a,tmpreg);
  2533. a_load_reg_subsetref(list,size,subsetsize,tmpreg,sref);
  2534. end;
  2535. procedure tcg.a_op_const_loc(list : TAsmList; Op: TOpCG; a: tcgint; const loc: tlocation);
  2536. begin
  2537. case loc.loc of
  2538. LOC_REGISTER, LOC_CREGISTER:
  2539. a_op_const_reg(list,op,loc.size,a,loc.register);
  2540. LOC_REFERENCE, LOC_CREFERENCE:
  2541. a_op_const_ref(list,op,loc.size,a,loc.reference);
  2542. LOC_SUBSETREG, LOC_CSUBSETREG:
  2543. a_op_const_subsetreg(list,op,loc.size,loc.size,a,loc.sreg);
  2544. LOC_SUBSETREF, LOC_CSUBSETREF:
  2545. a_op_const_subsetref(list,op,loc.size,loc.size,a,loc.sref);
  2546. else
  2547. internalerror(200109061);
  2548. end;
  2549. end;
  2550. procedure tcg.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  2551. var
  2552. tmpreg : tregister;
  2553. begin
  2554. tmpreg:=getintregister(list,size);
  2555. a_load_ref_reg(list,size,size,ref,tmpreg);
  2556. a_op_reg_reg(list,op,size,reg,tmpreg);
  2557. a_load_reg_ref(list,size,size,tmpreg,ref);
  2558. end;
  2559. procedure tcg.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  2560. var
  2561. tmpreg: tregister;
  2562. begin
  2563. case op of
  2564. OP_NOT,OP_NEG:
  2565. { handle it as "load ref,reg; op reg" }
  2566. begin
  2567. a_load_ref_reg(list,size,size,ref,reg);
  2568. a_op_reg_reg(list,op,size,reg,reg);
  2569. end;
  2570. else
  2571. begin
  2572. tmpreg:=getintregister(list,size);
  2573. a_load_ref_reg(list,size,size,ref,tmpreg);
  2574. a_op_reg_reg(list,op,size,tmpreg,reg);
  2575. end;
  2576. end;
  2577. end;
  2578. procedure tcg.a_op_reg_subsetreg(list : TAsmList; Op : TOpCG; opsize, subsetsize : TCGSize; reg: TRegister; const sreg: tsubsetregister);
  2579. var
  2580. tmpreg: tregister;
  2581. begin
  2582. tmpreg := getintregister(list, opsize);
  2583. a_load_subsetreg_reg(list,subsetsize,opsize,sreg,tmpreg);
  2584. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  2585. a_load_reg_subsetreg(list,opsize,subsetsize,tmpreg,sreg);
  2586. end;
  2587. procedure tcg.a_op_reg_subsetref(list : TAsmList; Op : TOpCG; opsize, subsetsize : TCGSize; reg: TRegister; const sref: tsubsetreference);
  2588. var
  2589. tmpreg: tregister;
  2590. begin
  2591. tmpreg := getintregister(list, opsize);
  2592. a_load_subsetref_reg(list,subsetsize,opsize,sref,tmpreg);
  2593. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  2594. a_load_reg_subsetref(list,opsize,subsetsize,tmpreg,sref);
  2595. end;
  2596. procedure tcg.a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  2597. begin
  2598. case loc.loc of
  2599. LOC_REGISTER, LOC_CREGISTER:
  2600. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  2601. LOC_REFERENCE, LOC_CREFERENCE:
  2602. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  2603. LOC_SUBSETREG, LOC_CSUBSETREG:
  2604. a_op_reg_subsetreg(list,op,loc.size,loc.size,reg,loc.sreg);
  2605. LOC_SUBSETREF, LOC_CSUBSETREF:
  2606. a_op_reg_subsetref(list,op,loc.size,loc.size,reg,loc.sref);
  2607. else
  2608. internalerror(200109061);
  2609. end;
  2610. end;
  2611. procedure tcg.a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  2612. var
  2613. tmpreg: tregister;
  2614. begin
  2615. case loc.loc of
  2616. LOC_REGISTER,LOC_CREGISTER:
  2617. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  2618. LOC_REFERENCE,LOC_CREFERENCE:
  2619. begin
  2620. tmpreg:=getintregister(list,loc.size);
  2621. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  2622. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  2623. end;
  2624. LOC_SUBSETREG, LOC_CSUBSETREG:
  2625. begin
  2626. tmpreg:=getintregister(list,loc.size);
  2627. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  2628. a_op_ref_reg(list,op,loc.size,ref,tmpreg);
  2629. a_load_reg_subsetreg(list,loc.size,loc.size,tmpreg,loc.sreg);
  2630. end;
  2631. LOC_SUBSETREF, LOC_CSUBSETREF:
  2632. begin
  2633. tmpreg:=getintregister(list,loc.size);
  2634. a_load_subsetreF_reg(list,loc.size,loc.size,loc.sref,tmpreg);
  2635. a_op_ref_reg(list,op,loc.size,ref,tmpreg);
  2636. a_load_reg_subsetref(list,loc.size,loc.size,tmpreg,loc.sref);
  2637. end;
  2638. else
  2639. internalerror(200109061);
  2640. end;
  2641. end;
  2642. procedure Tcg.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  2643. a:tcgint;src,dst:Tregister);
  2644. begin
  2645. a_load_reg_reg(list,size,size,src,dst);
  2646. a_op_const_reg(list,op,size,a,dst);
  2647. end;
  2648. procedure tcg.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  2649. size: tcgsize; src1, src2, dst: tregister);
  2650. var
  2651. tmpreg: tregister;
  2652. begin
  2653. if (dst<>src1) then
  2654. begin
  2655. a_load_reg_reg(list,size,size,src2,dst);
  2656. a_op_reg_reg(list,op,size,src1,dst);
  2657. end
  2658. else
  2659. begin
  2660. { can we do a direct operation on the target register ? }
  2661. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  2662. a_op_reg_reg(list,op,size,src2,dst)
  2663. else
  2664. begin
  2665. tmpreg:=getintregister(list,size);
  2666. a_load_reg_reg(list,size,size,src2,tmpreg);
  2667. a_op_reg_reg(list,op,size,src1,tmpreg);
  2668. a_load_reg_reg(list,size,size,tmpreg,dst);
  2669. end;
  2670. end;
  2671. end;
  2672. procedure tcg.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  2673. begin
  2674. a_op_const_reg_reg(list,op,size,a,src,dst);
  2675. ovloc.loc:=LOC_VOID;
  2676. end;
  2677. procedure tcg.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  2678. begin
  2679. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  2680. ovloc.loc:=LOC_VOID;
  2681. end;
  2682. procedure tcg.a_cmp_const_reg_label(list: TAsmList; size: tcgsize;
  2683. cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  2684. var
  2685. tmpreg: tregister;
  2686. begin
  2687. tmpreg:=getintregister(list,size);
  2688. a_load_const_reg(list,size,a,tmpreg);
  2689. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2690. end;
  2691. procedure tcg.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  2692. l : tasmlabel);
  2693. var
  2694. tmpreg: tregister;
  2695. begin
  2696. tmpreg:=getintregister(list,size);
  2697. a_load_ref_reg(list,size,size,ref,tmpreg);
  2698. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2699. end;
  2700. procedure tcg.a_cmp_const_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const loc : tlocation;
  2701. l : tasmlabel);
  2702. var
  2703. tmpreg : tregister;
  2704. begin
  2705. case loc.loc of
  2706. LOC_REGISTER,LOC_CREGISTER:
  2707. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  2708. LOC_REFERENCE,LOC_CREFERENCE:
  2709. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  2710. LOC_SUBSETREG, LOC_CSUBSETREG:
  2711. begin
  2712. tmpreg:=getintregister(list,size);
  2713. a_load_subsetreg_reg(list,loc.size,size,loc.sreg,tmpreg);
  2714. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2715. end;
  2716. LOC_SUBSETREF, LOC_CSUBSETREF:
  2717. begin
  2718. tmpreg:=getintregister(list,size);
  2719. a_load_subsetref_reg(list,loc.size,size,loc.sref,tmpreg);
  2720. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  2721. end;
  2722. else
  2723. internalerror(200109061);
  2724. end;
  2725. end;
  2726. procedure tcg.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  2727. var
  2728. tmpreg: tregister;
  2729. begin
  2730. tmpreg:=getintregister(list,size);
  2731. a_load_ref_reg(list,size,size,ref,tmpreg);
  2732. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2733. end;
  2734. procedure tcg.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg : tregister; const ref: treference; l : tasmlabel);
  2735. var
  2736. tmpreg: tregister;
  2737. begin
  2738. tmpreg:=getintregister(list,size);
  2739. a_load_ref_reg(list,size,size,ref,tmpreg);
  2740. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  2741. end;
  2742. procedure tcg.a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  2743. begin
  2744. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  2745. end;
  2746. procedure tcg.a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  2747. begin
  2748. case loc.loc of
  2749. LOC_REGISTER,
  2750. LOC_CREGISTER:
  2751. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  2752. LOC_REFERENCE,
  2753. LOC_CREFERENCE :
  2754. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  2755. LOC_CONSTANT:
  2756. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  2757. LOC_SUBSETREG,
  2758. LOC_CSUBSETREG:
  2759. a_cmp_subsetreg_reg_label(list,loc.size,size,cmp_op,loc.sreg,reg,l);
  2760. LOC_SUBSETREF,
  2761. LOC_CSUBSETREF:
  2762. a_cmp_subsetref_reg_label(list,loc.size,size,cmp_op,loc.sref,reg,l);
  2763. else
  2764. internalerror(200203231);
  2765. end;
  2766. end;
  2767. procedure tcg.a_cmp_subsetreg_reg_label(list : TAsmList; subsetsize : tcgsize; cmpsize : tcgsize; cmp_op : topcmp; const sreg: tsubsetregister; reg : tregister; l : tasmlabel);
  2768. var
  2769. tmpreg: tregister;
  2770. begin
  2771. tmpreg:=getintregister(list, cmpsize);
  2772. a_load_subsetreg_reg(list,subsetsize,cmpsize,sreg,tmpreg);
  2773. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  2774. end;
  2775. procedure tcg.a_cmp_subsetref_reg_label(list : TAsmList; subsetsize, cmpsize : tcgsize; cmp_op : topcmp; const sref: tsubsetreference; reg : tregister; l : tasmlabel);
  2776. var
  2777. tmpreg: tregister;
  2778. begin
  2779. tmpreg:=getintregister(list, cmpsize);
  2780. a_load_subsetref_reg(list,subsetsize,cmpsize,sref,tmpreg);
  2781. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  2782. end;
  2783. procedure tcg.a_cmp_ref_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  2784. l : tasmlabel);
  2785. var
  2786. tmpreg: tregister;
  2787. begin
  2788. case loc.loc of
  2789. LOC_REGISTER,LOC_CREGISTER:
  2790. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  2791. LOC_REFERENCE,LOC_CREFERENCE:
  2792. begin
  2793. tmpreg:=getintregister(list,size);
  2794. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2795. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  2796. end;
  2797. LOC_SUBSETREG, LOC_CSUBSETREG:
  2798. begin
  2799. tmpreg:=getintregister(list, size);
  2800. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2801. a_cmp_subsetreg_reg_label(list,loc.size,size,swap_opcmp(cmp_op),loc.sreg,tmpreg,l);
  2802. end;
  2803. LOC_SUBSETREF, LOC_CSUBSETREF:
  2804. begin
  2805. tmpreg:=getintregister(list, size);
  2806. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2807. a_cmp_subsetref_reg_label(list,loc.size,size,swap_opcmp(cmp_op),loc.sref,tmpreg,l);
  2808. end;
  2809. else
  2810. internalerror(200109061);
  2811. end;
  2812. end;
  2813. procedure tcg.a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  2814. begin
  2815. case loc.loc of
  2816. LOC_MMREGISTER,LOC_CMMREGISTER:
  2817. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  2818. LOC_REFERENCE,LOC_CREFERENCE:
  2819. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  2820. LOC_REGISTER,LOC_CREGISTER:
  2821. a_loadmm_intreg_reg(list,loc.size,size,loc.register,reg,shuffle);
  2822. else
  2823. internalerror(200310121);
  2824. end;
  2825. end;
  2826. procedure tcg.a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  2827. begin
  2828. case loc.loc of
  2829. LOC_MMREGISTER,LOC_CMMREGISTER:
  2830. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  2831. LOC_REFERENCE,LOC_CREFERENCE:
  2832. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  2833. else
  2834. internalerror(200310122);
  2835. end;
  2836. end;
  2837. procedure tcg.a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle);
  2838. var
  2839. href : treference;
  2840. {$ifndef cpu64bitalu}
  2841. tmpreg : tregister;
  2842. reg64 : tregister64;
  2843. {$endif not cpu64bitalu}
  2844. begin
  2845. {$ifndef cpu64bitalu}
  2846. if not(cgpara.location^.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  2847. (size<>OS_F64) then
  2848. {$endif not cpu64bitalu}
  2849. cgpara.check_simple_location;
  2850. paramanager.alloccgpara(list,cgpara);
  2851. case cgpara.location^.loc of
  2852. LOC_MMREGISTER,LOC_CMMREGISTER:
  2853. a_loadmm_reg_reg(list,size,cgpara.location^.size,reg,cgpara.location^.register,shuffle);
  2854. LOC_REFERENCE,LOC_CREFERENCE:
  2855. begin
  2856. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  2857. a_loadmm_reg_ref(list,size,cgpara.location^.size,reg,href,shuffle);
  2858. end;
  2859. LOC_REGISTER,LOC_CREGISTER:
  2860. begin
  2861. if assigned(shuffle) and
  2862. not shufflescalar(shuffle) then
  2863. internalerror(2009112510);
  2864. {$ifndef cpu64bitalu}
  2865. if (size=OS_F64) then
  2866. begin
  2867. if not assigned(cgpara.location^.next) or
  2868. assigned(cgpara.location^.next^.next) then
  2869. internalerror(2009112512);
  2870. case cgpara.location^.next^.loc of
  2871. LOC_REGISTER,LOC_CREGISTER:
  2872. tmpreg:=cgpara.location^.next^.register;
  2873. LOC_REFERENCE,LOC_CREFERENCE:
  2874. tmpreg:=getintregister(list,OS_32);
  2875. else
  2876. internalerror(2009112910);
  2877. end;
  2878. if (target_info.endian=ENDIAN_BIG) then
  2879. begin
  2880. { paraloc^ -> high
  2881. paraloc^.next -> low }
  2882. reg64.reghi:=cgpara.location^.register;
  2883. reg64.reglo:=tmpreg;
  2884. end
  2885. else
  2886. begin
  2887. { paraloc^ -> low
  2888. paraloc^.next -> high }
  2889. reg64.reglo:=cgpara.location^.register;
  2890. reg64.reghi:=tmpreg;
  2891. end;
  2892. cg64.a_loadmm_reg_intreg64(list,size,reg,reg64);
  2893. if (cgpara.location^.next^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2894. begin
  2895. if not(cgpara.location^.next^.size in [OS_32,OS_S32]) then
  2896. internalerror(2009112911);
  2897. reference_reset_base(href,cgpara.location^.next^.reference.index,cgpara.location^.next^.reference.offset,cgpara.alignment);
  2898. a_load_reg_ref(list,OS_32,cgpara.location^.next^.size,tmpreg,href);
  2899. end;
  2900. end
  2901. else
  2902. {$endif not cpu64bitalu}
  2903. a_loadmm_reg_intreg(list,size,cgpara.location^.size,reg,cgpara.location^.register,mms_movescalar);
  2904. end
  2905. else
  2906. internalerror(200310123);
  2907. end;
  2908. end;
  2909. procedure tcg.a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize;const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle);
  2910. var
  2911. hr : tregister;
  2912. hs : tmmshuffle;
  2913. begin
  2914. cgpara.check_simple_location;
  2915. hr:=getmmregister(list,cgpara.location^.size);
  2916. a_loadmm_ref_reg(list,size,cgpara.location^.size,ref,hr,shuffle);
  2917. if realshuffle(shuffle) then
  2918. begin
  2919. hs:=shuffle^;
  2920. removeshuffles(hs);
  2921. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,@hs);
  2922. end
  2923. else
  2924. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,shuffle);
  2925. end;
  2926. procedure tcg.a_loadmm_loc_cgpara(list: TAsmList;const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle);
  2927. begin
  2928. case loc.loc of
  2929. LOC_MMREGISTER,LOC_CMMREGISTER:
  2930. a_loadmm_reg_cgpara(list,loc.size,loc.register,cgpara,shuffle);
  2931. LOC_REFERENCE,LOC_CREFERENCE:
  2932. a_loadmm_ref_cgpara(list,loc.size,loc.reference,cgpara,shuffle);
  2933. else
  2934. internalerror(200310123);
  2935. end;
  2936. end;
  2937. procedure tcg.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  2938. var
  2939. hr : tregister;
  2940. hs : tmmshuffle;
  2941. begin
  2942. hr:=getmmregister(list,size);
  2943. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  2944. if realshuffle(shuffle) then
  2945. begin
  2946. hs:=shuffle^;
  2947. removeshuffles(hs);
  2948. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  2949. end
  2950. else
  2951. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  2952. end;
  2953. procedure tcg.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  2954. var
  2955. hr : tregister;
  2956. hs : tmmshuffle;
  2957. begin
  2958. hr:=getmmregister(list,size);
  2959. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  2960. if realshuffle(shuffle) then
  2961. begin
  2962. hs:=shuffle^;
  2963. removeshuffles(hs);
  2964. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  2965. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  2966. end
  2967. else
  2968. begin
  2969. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  2970. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  2971. end;
  2972. end;
  2973. procedure tcg.a_loadmm_intreg_reg(list: tasmlist; fromsize,tosize: tcgsize; intreg,mmreg: tregister; shuffle: pmmshuffle);
  2974. var
  2975. tmpref: treference;
  2976. begin
  2977. if (tcgsize2size[fromsize]<>4) or
  2978. (tcgsize2size[tosize]<>4) then
  2979. internalerror(2009112503);
  2980. tg.gettemp(list,4,4,tt_normal,tmpref);
  2981. a_load_reg_ref(list,fromsize,fromsize,intreg,tmpref);
  2982. a_loadmm_ref_reg(list,tosize,tosize,tmpref,mmreg,shuffle);
  2983. tg.ungettemp(list,tmpref);
  2984. end;
  2985. procedure tcg.a_loadmm_reg_intreg(list: tasmlist; fromsize,tosize: tcgsize; mmreg,intreg: tregister; shuffle: pmmshuffle);
  2986. var
  2987. tmpref: treference;
  2988. begin
  2989. if (tcgsize2size[fromsize]<>4) or
  2990. (tcgsize2size[tosize]<>4) then
  2991. internalerror(2009112504);
  2992. tg.gettemp(list,8,8,tt_normal,tmpref);
  2993. cg.a_loadmm_reg_ref(list,fromsize,fromsize,mmreg,tmpref,shuffle);
  2994. a_load_ref_reg(list,tosize,tosize,tmpref,intreg);
  2995. tg.ungettemp(list,tmpref);
  2996. end;
  2997. procedure tcg.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  2998. begin
  2999. case loc.loc of
  3000. LOC_CMMREGISTER,LOC_MMREGISTER:
  3001. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  3002. LOC_CREFERENCE,LOC_REFERENCE:
  3003. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  3004. else
  3005. internalerror(200312232);
  3006. end;
  3007. end;
  3008. procedure tcg.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  3009. begin
  3010. g_concatcopy(list,source,dest,len);
  3011. end;
  3012. procedure tcg.g_copyshortstring(list : TAsmList;const source,dest : treference;len:byte);
  3013. var
  3014. cgpara1,cgpara2,cgpara3 : TCGPara;
  3015. begin
  3016. cgpara1.init;
  3017. cgpara2.init;
  3018. cgpara3.init;
  3019. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3020. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3021. paramanager.getintparaloc(pocall_default,3,cgpara3);
  3022. a_loadaddr_ref_cgpara(list,dest,cgpara3);
  3023. a_loadaddr_ref_cgpara(list,source,cgpara2);
  3024. a_load_const_cgpara(list,OS_INT,len,cgpara1);
  3025. paramanager.freecgpara(list,cgpara3);
  3026. paramanager.freecgpara(list,cgpara2);
  3027. paramanager.freecgpara(list,cgpara1);
  3028. allocallcpuregisters(list);
  3029. a_call_name(list,'FPC_SHORTSTR_ASSIGN',false);
  3030. deallocallcpuregisters(list);
  3031. cgpara3.done;
  3032. cgpara2.done;
  3033. cgpara1.done;
  3034. end;
  3035. procedure tcg.g_copyvariant(list : TAsmList;const source,dest : treference);
  3036. var
  3037. cgpara1,cgpara2 : TCGPara;
  3038. begin
  3039. cgpara1.init;
  3040. cgpara2.init;
  3041. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3042. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3043. a_loadaddr_ref_cgpara(list,dest,cgpara2);
  3044. a_loadaddr_ref_cgpara(list,source,cgpara1);
  3045. paramanager.freecgpara(list,cgpara2);
  3046. paramanager.freecgpara(list,cgpara1);
  3047. allocallcpuregisters(list);
  3048. a_call_name(list,'FPC_VARIANT_COPY_OVERWRITE',false);
  3049. deallocallcpuregisters(list);
  3050. cgpara2.done;
  3051. cgpara1.done;
  3052. end;
  3053. procedure tcg.g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);
  3054. var
  3055. href : treference;
  3056. incrfunc : string;
  3057. cgpara1,cgpara2 : TCGPara;
  3058. begin
  3059. cgpara1.init;
  3060. cgpara2.init;
  3061. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3062. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3063. if is_interfacecom_or_dispinterface(t) then
  3064. incrfunc:='FPC_INTF_INCR_REF'
  3065. else if is_ansistring(t) then
  3066. incrfunc:='FPC_ANSISTR_INCR_REF'
  3067. else if is_widestring(t) then
  3068. incrfunc:='FPC_WIDESTR_INCR_REF'
  3069. else if is_unicodestring(t) then
  3070. incrfunc:='FPC_UNICODESTR_INCR_REF'
  3071. else if is_dynamic_array(t) then
  3072. incrfunc:='FPC_DYNARRAY_INCR_REF'
  3073. else
  3074. incrfunc:='';
  3075. { call the special incr function or the generic addref }
  3076. if incrfunc<>'' then
  3077. begin
  3078. { widestrings aren't ref. counted on all platforms so we need the address
  3079. to create a real copy }
  3080. if is_widestring(t) then
  3081. a_loadaddr_ref_cgpara(list,ref,cgpara1)
  3082. else
  3083. { these functions get the pointer by value }
  3084. a_load_ref_cgpara(list,OS_ADDR,ref,cgpara1);
  3085. paramanager.freecgpara(list,cgpara1);
  3086. allocallcpuregisters(list);
  3087. a_call_name(list,incrfunc,false);
  3088. deallocallcpuregisters(list);
  3089. end
  3090. else
  3091. begin
  3092. if is_open_array(t) then
  3093. InternalError(201103054);
  3094. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  3095. a_loadaddr_ref_cgpara(list,href,cgpara2);
  3096. a_loadaddr_ref_cgpara(list,ref,cgpara1);
  3097. paramanager.freecgpara(list,cgpara1);
  3098. paramanager.freecgpara(list,cgpara2);
  3099. allocallcpuregisters(list);
  3100. a_call_name(list,'FPC_ADDREF',false);
  3101. deallocallcpuregisters(list);
  3102. end;
  3103. cgpara2.done;
  3104. cgpara1.done;
  3105. end;
  3106. procedure tcg.g_decrrefcount(list : TAsmList;t: tdef; const ref: treference);
  3107. var
  3108. href : treference;
  3109. decrfunc : string;
  3110. needrtti : boolean;
  3111. cgpara1,cgpara2 : TCGPara;
  3112. tempreg1,tempreg2 : TRegister;
  3113. begin
  3114. cgpara1.init;
  3115. cgpara2.init;
  3116. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3117. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3118. needrtti:=false;
  3119. if is_interfacecom_or_dispinterface(t) then
  3120. decrfunc:='FPC_INTF_DECR_REF'
  3121. else if is_ansistring(t) then
  3122. decrfunc:='FPC_ANSISTR_DECR_REF'
  3123. else if is_widestring(t) then
  3124. decrfunc:='FPC_WIDESTR_DECR_REF'
  3125. else if is_unicodestring(t) then
  3126. decrfunc:='FPC_UNICODESTR_DECR_REF'
  3127. else if is_dynamic_array(t) then
  3128. begin
  3129. decrfunc:='FPC_DYNARRAY_DECR_REF';
  3130. needrtti:=true;
  3131. end
  3132. else
  3133. decrfunc:='';
  3134. { call the special decr function or the generic decref }
  3135. if decrfunc<>'' then
  3136. begin
  3137. if needrtti then
  3138. begin
  3139. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  3140. tempreg2:=getaddressregister(list);
  3141. a_loadaddr_ref_reg(list,href,tempreg2);
  3142. end;
  3143. tempreg1:=getaddressregister(list);
  3144. a_loadaddr_ref_reg(list,ref,tempreg1);
  3145. if needrtti then
  3146. a_load_reg_cgpara(list,OS_ADDR,tempreg2,cgpara2);
  3147. a_load_reg_cgpara(list,OS_ADDR,tempreg1,cgpara1);
  3148. paramanager.freecgpara(list,cgpara1);
  3149. if needrtti then
  3150. paramanager.freecgpara(list,cgpara2);
  3151. allocallcpuregisters(list);
  3152. a_call_name(list,decrfunc,false);
  3153. deallocallcpuregisters(list);
  3154. end
  3155. else
  3156. begin
  3157. if is_open_array(t) then
  3158. InternalError(201103053);
  3159. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  3160. a_loadaddr_ref_cgpara(list,href,cgpara2);
  3161. a_loadaddr_ref_cgpara(list,ref,cgpara1);
  3162. paramanager.freecgpara(list,cgpara1);
  3163. paramanager.freecgpara(list,cgpara2);
  3164. allocallcpuregisters(list);
  3165. a_call_name(list,'FPC_DECREF',false);
  3166. deallocallcpuregisters(list);
  3167. end;
  3168. cgpara2.done;
  3169. cgpara1.done;
  3170. end;
  3171. procedure tcg.g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string);
  3172. var
  3173. cgpara1,cgpara2,cgpara3: TCGPara;
  3174. href: TReference;
  3175. hreg, lenreg: TRegister;
  3176. begin
  3177. cgpara1.init;
  3178. cgpara2.init;
  3179. cgpara3.init;
  3180. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3181. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3182. paramanager.getintparaloc(pocall_default,3,cgpara3);
  3183. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  3184. if highloc.loc=LOC_CONSTANT then
  3185. a_load_const_cgpara(list,OS_INT,highloc.value+1,cgpara3)
  3186. else
  3187. begin
  3188. if highloc.loc in [LOC_REGISTER,LOC_CREGISTER] then
  3189. hreg:=highloc.register
  3190. else
  3191. begin
  3192. hreg:=getintregister(list,OS_INT);
  3193. a_load_loc_reg(list,OS_INT,highloc,hreg);
  3194. end;
  3195. { increment, converts high(x) to length(x) }
  3196. lenreg:=getintregister(list,OS_INT);
  3197. a_op_const_reg_reg(list,OP_ADD,OS_INT,1,hreg,lenreg);
  3198. a_load_reg_cgpara(list,OS_INT,lenreg,cgpara3);
  3199. end;
  3200. a_loadaddr_ref_cgpara(list,href,cgpara2);
  3201. a_loadaddr_ref_cgpara(list,ref,cgpara1);
  3202. paramanager.freecgpara(list,cgpara1);
  3203. paramanager.freecgpara(list,cgpara2);
  3204. paramanager.freecgpara(list,cgpara3);
  3205. allocallcpuregisters(list);
  3206. a_call_name(list,name,false);
  3207. deallocallcpuregisters(list);
  3208. cgpara3.done;
  3209. cgpara2.done;
  3210. cgpara1.done;
  3211. end;
  3212. procedure tcg.g_initialize(list : TAsmList;t : tdef;const ref : treference);
  3213. var
  3214. href : treference;
  3215. cgpara1,cgpara2 : TCGPara;
  3216. begin
  3217. cgpara1.init;
  3218. cgpara2.init;
  3219. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3220. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3221. if is_ansistring(t) or
  3222. is_widestring(t) or
  3223. is_unicodestring(t) or
  3224. is_interfacecom_or_dispinterface(t) or
  3225. is_dynamic_array(t) then
  3226. a_load_const_ref(list,OS_ADDR,0,ref)
  3227. else
  3228. begin
  3229. if is_open_array(t) then
  3230. InternalError(201103052);
  3231. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  3232. a_loadaddr_ref_cgpara(list,href,cgpara2);
  3233. a_loadaddr_ref_cgpara(list,ref,cgpara1);
  3234. paramanager.freecgpara(list,cgpara1);
  3235. paramanager.freecgpara(list,cgpara2);
  3236. allocallcpuregisters(list);
  3237. a_call_name(list,'FPC_INITIALIZE',false);
  3238. deallocallcpuregisters(list);
  3239. end;
  3240. cgpara1.done;
  3241. cgpara2.done;
  3242. end;
  3243. procedure tcg.g_finalize(list : TAsmList;t : tdef;const ref : treference);
  3244. var
  3245. href : treference;
  3246. cgpara1,cgpara2 : TCGPara;
  3247. begin
  3248. cgpara1.init;
  3249. cgpara2.init;
  3250. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3251. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3252. if is_ansistring(t) or
  3253. is_widestring(t) or
  3254. is_unicodestring(t) or
  3255. is_interfacecom_or_dispinterface(t) then
  3256. begin
  3257. g_decrrefcount(list,t,ref);
  3258. a_load_const_ref(list,OS_ADDR,0,ref);
  3259. end
  3260. else
  3261. begin
  3262. if is_open_array(t) then
  3263. InternalError(201103051);
  3264. reference_reset_symbol(href,RTTIWriter.get_rtti_label(t,initrtti),0,sizeof(pint));
  3265. a_loadaddr_ref_cgpara(list,href,cgpara2);
  3266. a_loadaddr_ref_cgpara(list,ref,cgpara1);
  3267. paramanager.freecgpara(list,cgpara1);
  3268. paramanager.freecgpara(list,cgpara2);
  3269. allocallcpuregisters(list);
  3270. a_call_name(list,'FPC_FINALIZE',false);
  3271. deallocallcpuregisters(list);
  3272. end;
  3273. cgpara1.done;
  3274. cgpara2.done;
  3275. end;
  3276. procedure tcg.g_rangecheck(list: TAsmList; const l:tlocation;fromdef,todef: tdef);
  3277. { generate range checking code for the value at location p. The type }
  3278. { type used is checked against todefs ranges. fromdef (p.resultdef) }
  3279. { is the original type used at that location. When both defs are equal }
  3280. { the check is also insert (needed for succ,pref,inc,dec) }
  3281. const
  3282. aintmax=high(aint);
  3283. var
  3284. neglabel : tasmlabel;
  3285. hreg : tregister;
  3286. lto,hto,
  3287. lfrom,hfrom : TConstExprInt;
  3288. fromsize, tosize: cardinal;
  3289. from_signed, to_signed: boolean;
  3290. begin
  3291. { range checking on and range checkable value? }
  3292. if not(cs_check_range in current_settings.localswitches) or
  3293. not(fromdef.typ in [orddef,enumdef]) or
  3294. { C-style booleans can't really fail range checks, }
  3295. { all values are always valid }
  3296. is_cbool(todef) then
  3297. exit;
  3298. {$ifndef cpu64bitalu}
  3299. { handle 64bit rangechecks separate for 32bit processors }
  3300. if is_64bit(fromdef) or is_64bit(todef) then
  3301. begin
  3302. cg64.g_rangecheck64(list,l,fromdef,todef);
  3303. exit;
  3304. end;
  3305. {$endif cpu64bitalu}
  3306. { only check when assigning to scalar, subranges are different, }
  3307. { when todef=fromdef then the check is always generated }
  3308. getrange(fromdef,lfrom,hfrom);
  3309. getrange(todef,lto,hto);
  3310. from_signed := is_signed(fromdef);
  3311. to_signed := is_signed(todef);
  3312. { check the rangedef of the array, not the array itself }
  3313. { (only change now, since getrange needs the arraydef) }
  3314. if (todef.typ = arraydef) then
  3315. todef := tarraydef(todef).rangedef;
  3316. { no range check if from and to are equal and are both longint/dword }
  3317. { (if we have a 32bit processor) or int64/qword, since such }
  3318. { operations can at most cause overflows (JM) }
  3319. { Note that these checks are mostly processor independent, they only }
  3320. { have to be changed once we introduce 64bit subrange types }
  3321. {$ifdef cpu64bitalu}
  3322. if (fromdef = todef) and
  3323. (fromdef.typ=orddef) and
  3324. (((((torddef(fromdef).ordtype = s64bit) and
  3325. (lfrom = low(int64)) and
  3326. (hfrom = high(int64))) or
  3327. ((torddef(fromdef).ordtype = u64bit) and
  3328. (lfrom = low(qword)) and
  3329. (hfrom = high(qword))) or
  3330. ((torddef(fromdef).ordtype = scurrency) and
  3331. (lfrom = low(int64)) and
  3332. (hfrom = high(int64)))))) then
  3333. exit;
  3334. {$else cpu64bitalu}
  3335. if (fromdef = todef) and
  3336. (fromdef.typ=orddef) and
  3337. (((((torddef(fromdef).ordtype = s32bit) and
  3338. (lfrom = int64(low(longint))) and
  3339. (hfrom = int64(high(longint)))) or
  3340. ((torddef(fromdef).ordtype = u32bit) and
  3341. (lfrom = low(cardinal)) and
  3342. (hfrom = high(cardinal)))))) then
  3343. exit;
  3344. {$endif cpu64bitalu}
  3345. { optimize some range checks away in safe cases }
  3346. fromsize := fromdef.size;
  3347. tosize := todef.size;
  3348. if ((from_signed = to_signed) or
  3349. (not from_signed)) and
  3350. (lto<=lfrom) and (hto>=hfrom) and
  3351. (fromsize <= tosize) then
  3352. begin
  3353. { if fromsize < tosize, and both have the same signed-ness or }
  3354. { fromdef is unsigned, then all bit patterns from fromdef are }
  3355. { valid for todef as well }
  3356. if (fromsize < tosize) then
  3357. exit;
  3358. if (fromsize = tosize) and
  3359. (from_signed = to_signed) then
  3360. { only optimize away if all bit patterns which fit in fromsize }
  3361. { are valid for the todef }
  3362. begin
  3363. {$ifopt Q+}
  3364. {$define overflowon}
  3365. {$Q-}
  3366. {$endif}
  3367. {$ifopt R+}
  3368. {$define rangeon}
  3369. {$R-}
  3370. {$endif}
  3371. if to_signed then
  3372. begin
  3373. { calculation of the low/high ranges must not overflow 64 bit
  3374. otherwise we end up comparing with zero for 64 bit data types on
  3375. 64 bit processors }
  3376. if (lto = (int64(-1) << (tosize * 8 - 1))) and
  3377. (hto = (-((int64(-1) << (tosize * 8 - 1))+1))) then
  3378. exit
  3379. end
  3380. else
  3381. begin
  3382. { calculation of the low/high ranges must not overflow 64 bit
  3383. otherwise we end up having all zeros for 64 bit data types on
  3384. 64 bit processors }
  3385. if (lto = 0) and
  3386. (qword(hto) = (qword(-1) >> (64-(tosize * 8))) ) then
  3387. exit
  3388. end;
  3389. {$ifdef overflowon}
  3390. {$Q+}
  3391. {$undef overflowon}
  3392. {$endif}
  3393. {$ifdef rangeon}
  3394. {$R+}
  3395. {$undef rangeon}
  3396. {$endif}
  3397. end
  3398. end;
  3399. { generate the rangecheck code for the def where we are going to }
  3400. { store the result }
  3401. { use the trick that }
  3402. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  3403. { To be able to do that, we have to make sure however that either }
  3404. { fromdef and todef are both signed or unsigned, or that we leave }
  3405. { the parts < 0 and > maxlongint out }
  3406. if from_signed xor to_signed then
  3407. begin
  3408. if from_signed then
  3409. { from is signed, to is unsigned }
  3410. begin
  3411. { if high(from) < 0 -> always range error }
  3412. if (hfrom < 0) or
  3413. { if low(to) > maxlongint also range error }
  3414. (lto > aintmax) then
  3415. begin
  3416. a_call_name(list,'FPC_RANGEERROR',false);
  3417. exit
  3418. end;
  3419. { from is signed and to is unsigned -> when looking at to }
  3420. { as an signed value, it must be < maxaint (otherwise }
  3421. { it will become negative, which is invalid since "to" is unsigned) }
  3422. if hto > aintmax then
  3423. hto := aintmax;
  3424. end
  3425. else
  3426. { from is unsigned, to is signed }
  3427. begin
  3428. if (lfrom > aintmax) or
  3429. (hto < 0) then
  3430. begin
  3431. a_call_name(list,'FPC_RANGEERROR',false);
  3432. exit
  3433. end;
  3434. { from is unsigned and to is signed -> when looking at to }
  3435. { as an unsigned value, it must be >= 0 (since negative }
  3436. { values are the same as values > maxlongint) }
  3437. if lto < 0 then
  3438. lto := 0;
  3439. end;
  3440. end;
  3441. hreg:=getintregister(list,OS_INT);
  3442. a_load_loc_reg(list,OS_INT,l,hreg);
  3443. a_op_const_reg(list,OP_SUB,OS_INT,tcgint(int64(lto)),hreg);
  3444. current_asmdata.getjumplabel(neglabel);
  3445. {
  3446. if from_signed then
  3447. a_cmp_const_reg_label(list,OS_INT,OC_GTE,aint(hto-lto),hreg,neglabel)
  3448. else
  3449. }
  3450. {$ifdef cpu64bitalu}
  3451. if qword(hto-lto)>qword(aintmax) then
  3452. a_cmp_const_reg_label(list,OS_INT,OC_BE,aintmax,hreg,neglabel)
  3453. else
  3454. {$endif cpu64bitalu}
  3455. a_cmp_const_reg_label(list,OS_INT,OC_BE,tcgint(int64(hto-lto)),hreg,neglabel);
  3456. a_call_name(list,'FPC_RANGEERROR',false);
  3457. a_label(list,neglabel);
  3458. end;
  3459. procedure tcg.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  3460. begin
  3461. g_overflowCheck(list,loc,def);
  3462. end;
  3463. {$ifdef cpuflags}
  3464. procedure tcg.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference);
  3465. var
  3466. tmpreg : tregister;
  3467. begin
  3468. tmpreg:=getintregister(list,size);
  3469. g_flags2reg(list,size,f,tmpreg);
  3470. a_load_reg_ref(list,size,size,tmpreg,ref);
  3471. end;
  3472. {$endif cpuflags}
  3473. procedure tcg.g_maybe_testself(list : TAsmList;reg:tregister);
  3474. var
  3475. OKLabel : tasmlabel;
  3476. cgpara1 : TCGPara;
  3477. begin
  3478. if (cs_check_object in current_settings.localswitches) or
  3479. (cs_check_range in current_settings.localswitches) then
  3480. begin
  3481. current_asmdata.getjumplabel(oklabel);
  3482. a_cmp_const_reg_label(list,OS_ADDR,OC_NE,0,reg,oklabel);
  3483. cgpara1.init;
  3484. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3485. a_load_const_cgpara(list,OS_INT,tcgint(210),cgpara1);
  3486. paramanager.freecgpara(list,cgpara1);
  3487. a_call_name(list,'FPC_HANDLEERROR',false);
  3488. a_label(list,oklabel);
  3489. cgpara1.done;
  3490. end;
  3491. end;
  3492. procedure tcg.g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  3493. var
  3494. hrefvmt : treference;
  3495. cgpara1,cgpara2 : TCGPara;
  3496. begin
  3497. cgpara1.init;
  3498. cgpara2.init;
  3499. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3500. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3501. if (cs_check_object in current_settings.localswitches) then
  3502. begin
  3503. reference_reset_symbol(hrefvmt,current_asmdata.RefAsmSymbol(objdef.vmt_mangledname),0,sizeof(pint));
  3504. a_loadaddr_ref_cgpara(list,hrefvmt,cgpara2);
  3505. a_load_reg_cgpara(list,OS_ADDR,reg,cgpara1);
  3506. paramanager.freecgpara(list,cgpara1);
  3507. paramanager.freecgpara(list,cgpara2);
  3508. allocallcpuregisters(list);
  3509. a_call_name(list,'FPC_CHECK_OBJECT_EXT',false);
  3510. deallocallcpuregisters(list);
  3511. end
  3512. else
  3513. if (cs_check_range in current_settings.localswitches) then
  3514. begin
  3515. a_load_reg_cgpara(list,OS_ADDR,reg,cgpara1);
  3516. paramanager.freecgpara(list,cgpara1);
  3517. allocallcpuregisters(list);
  3518. a_call_name(list,'FPC_CHECK_OBJECT',false);
  3519. deallocallcpuregisters(list);
  3520. end;
  3521. cgpara1.done;
  3522. cgpara2.done;
  3523. end;
  3524. {*****************************************************************************
  3525. Entry/Exit Code Functions
  3526. *****************************************************************************}
  3527. procedure tcg.g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:tcgint;destreg:tregister);
  3528. var
  3529. sizereg,sourcereg,lenreg : tregister;
  3530. cgpara1,cgpara2,cgpara3 : TCGPara;
  3531. begin
  3532. { because some abis don't support dynamic stack allocation properly
  3533. open array value parameters are copied onto the heap
  3534. }
  3535. { calculate necessary memory }
  3536. { read/write operations on one register make the life of the register allocator hard }
  3537. if not(lenloc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  3538. begin
  3539. lenreg:=getintregister(list,OS_INT);
  3540. a_load_loc_reg(list,OS_INT,lenloc,lenreg);
  3541. end
  3542. else
  3543. lenreg:=lenloc.register;
  3544. sizereg:=getintregister(list,OS_INT);
  3545. a_op_const_reg_reg(list,OP_ADD,OS_INT,1,lenreg,sizereg);
  3546. a_op_const_reg(list,OP_IMUL,OS_INT,elesize,sizereg);
  3547. { load source }
  3548. sourcereg:=getaddressregister(list);
  3549. a_loadaddr_ref_reg(list,ref,sourcereg);
  3550. { do getmem call }
  3551. cgpara1.init;
  3552. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3553. a_load_reg_cgpara(list,OS_INT,sizereg,cgpara1);
  3554. paramanager.freecgpara(list,cgpara1);
  3555. allocallcpuregisters(list);
  3556. a_call_name(list,'FPC_GETMEM',false);
  3557. deallocallcpuregisters(list);
  3558. cgpara1.done;
  3559. { return the new address }
  3560. a_load_reg_reg(list,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,destreg);
  3561. { do move call }
  3562. cgpara1.init;
  3563. cgpara2.init;
  3564. cgpara3.init;
  3565. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3566. paramanager.getintparaloc(pocall_default,2,cgpara2);
  3567. paramanager.getintparaloc(pocall_default,3,cgpara3);
  3568. { load size }
  3569. a_load_reg_cgpara(list,OS_INT,sizereg,cgpara3);
  3570. { load destination }
  3571. a_load_reg_cgpara(list,OS_ADDR,destreg,cgpara2);
  3572. { load source }
  3573. a_load_reg_cgpara(list,OS_ADDR,sourcereg,cgpara1);
  3574. paramanager.freecgpara(list,cgpara3);
  3575. paramanager.freecgpara(list,cgpara2);
  3576. paramanager.freecgpara(list,cgpara1);
  3577. allocallcpuregisters(list);
  3578. a_call_name(list,'FPC_MOVE',false);
  3579. deallocallcpuregisters(list);
  3580. cgpara3.done;
  3581. cgpara2.done;
  3582. cgpara1.done;
  3583. end;
  3584. procedure tcg.g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);
  3585. var
  3586. cgpara1 : TCGPara;
  3587. begin
  3588. { do move call }
  3589. cgpara1.init;
  3590. paramanager.getintparaloc(pocall_default,1,cgpara1);
  3591. { load source }
  3592. a_load_loc_cgpara(list,l,cgpara1);
  3593. paramanager.freecgpara(list,cgpara1);
  3594. allocallcpuregisters(list);
  3595. a_call_name(list,'FPC_FREEMEM',false);
  3596. deallocallcpuregisters(list);
  3597. cgpara1.done;
  3598. end;
  3599. procedure tcg.g_save_registers(list:TAsmList);
  3600. var
  3601. href : treference;
  3602. size : longint;
  3603. r : integer;
  3604. begin
  3605. { calculate temp. size }
  3606. size:=0;
  3607. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  3608. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  3609. inc(size,sizeof(aint));
  3610. { mm registers }
  3611. if uses_registers(R_MMREGISTER) then
  3612. begin
  3613. { Make sure we reserve enough space to do the alignment based on the offset
  3614. later on. We can't use the size for this, because the alignment of the start
  3615. of the temp is smaller than needed for an OS_VECTOR }
  3616. inc(size,tcgsize2size[OS_VECTOR]);
  3617. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  3618. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  3619. inc(size,tcgsize2size[OS_VECTOR]);
  3620. end;
  3621. if size>0 then
  3622. begin
  3623. tg.GetTemp(list,size,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  3624. include(current_procinfo.flags,pi_has_saved_regs);
  3625. { Copy registers to temp }
  3626. href:=current_procinfo.save_regs_ref;
  3627. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  3628. begin
  3629. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  3630. begin
  3631. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE),href);
  3632. inc(href.offset,sizeof(aint));
  3633. end;
  3634. include(rg[R_INTREGISTER].preserved_by_proc,saved_standard_registers[r]);
  3635. end;
  3636. if uses_registers(R_MMREGISTER) then
  3637. begin
  3638. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  3639. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  3640. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  3641. begin
  3642. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  3643. begin
  3644. a_loadmm_reg_ref(list,OS_VECTOR,OS_VECTOR,newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBNONE),href,nil);
  3645. inc(href.offset,tcgsize2size[OS_VECTOR]);
  3646. end;
  3647. include(rg[R_MMREGISTER].preserved_by_proc,saved_mm_registers[r]);
  3648. end;
  3649. end;
  3650. end;
  3651. end;
  3652. procedure tcg.g_restore_registers(list:TAsmList);
  3653. var
  3654. href : treference;
  3655. r : integer;
  3656. hreg : tregister;
  3657. begin
  3658. if not(pi_has_saved_regs in current_procinfo.flags) then
  3659. exit;
  3660. { Copy registers from temp }
  3661. href:=current_procinfo.save_regs_ref;
  3662. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  3663. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  3664. begin
  3665. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  3666. { Allocate register so the optimizer does not remove the load }
  3667. a_reg_alloc(list,hreg);
  3668. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  3669. inc(href.offset,sizeof(aint));
  3670. end;
  3671. if uses_registers(R_MMREGISTER) then
  3672. begin
  3673. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  3674. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  3675. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  3676. begin
  3677. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  3678. begin
  3679. hreg:=newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBNONE);
  3680. { Allocate register so the optimizer does not remove the load }
  3681. a_reg_alloc(list,hreg);
  3682. a_loadmm_ref_reg(list,OS_VECTOR,OS_VECTOR,href,hreg,nil);
  3683. inc(href.offset,tcgsize2size[OS_VECTOR]);
  3684. end;
  3685. end;
  3686. end;
  3687. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  3688. end;
  3689. procedure tcg.g_profilecode(list : TAsmList);
  3690. begin
  3691. end;
  3692. procedure tcg.g_exception_reason_save(list : TAsmList; const href : treference);
  3693. begin
  3694. a_load_reg_ref(list, OS_INT, OS_INT, NR_FUNCTION_RESULT_REG, href);
  3695. end;
  3696. procedure tcg.g_exception_reason_save_const(list : TAsmList; const href : treference; a: tcgint);
  3697. begin
  3698. a_load_const_ref(list, OS_INT, a, href);
  3699. end;
  3700. procedure tcg.g_exception_reason_load(list : TAsmList; const href : treference);
  3701. begin
  3702. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  3703. a_load_ref_reg(list, OS_INT, OS_INT, href, NR_FUNCTION_RESULT_REG);
  3704. end;
  3705. procedure tcg.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  3706. var
  3707. hsym : tsym;
  3708. href : treference;
  3709. paraloc : Pcgparalocation;
  3710. begin
  3711. { calculate the parameter info for the procdef }
  3712. procdef.init_paraloc_info(callerside);
  3713. hsym:=tsym(procdef.parast.Find('self'));
  3714. if not(assigned(hsym) and
  3715. (hsym.typ=paravarsym)) then
  3716. internalerror(200305251);
  3717. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  3718. while paraloc<>nil do
  3719. with paraloc^ do
  3720. begin
  3721. case loc of
  3722. LOC_REGISTER:
  3723. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  3724. LOC_REFERENCE:
  3725. begin
  3726. { offset in the wrapper needs to be adjusted for the stored
  3727. return address }
  3728. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),sizeof(pint));
  3729. a_op_const_ref(list,OP_SUB,size,ioffset,href);
  3730. end
  3731. else
  3732. internalerror(200309189);
  3733. end;
  3734. paraloc:=next;
  3735. end;
  3736. end;
  3737. procedure tcg.g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string);
  3738. begin
  3739. a_jmp_name(list,externalname);
  3740. end;
  3741. procedure tcg.a_call_name_static(list : TAsmList;const s : string);
  3742. begin
  3743. a_call_name(list,s,false);
  3744. end;
  3745. function tcg.g_indirect_sym_load(list:TAsmList;const symname: string; weak: boolean): tregister;
  3746. var
  3747. l: tasmsymbol;
  3748. ref: treference;
  3749. nlsymname: string;
  3750. begin
  3751. result := NR_NO;
  3752. case target_info.system of
  3753. system_powerpc_darwin,
  3754. system_i386_darwin,
  3755. system_i386_iphonesim,
  3756. system_powerpc64_darwin,
  3757. system_arm_darwin:
  3758. begin
  3759. nlsymname:='L'+symname+'$non_lazy_ptr';
  3760. l:=current_asmdata.getasmsymbol(nlsymname);
  3761. if not(assigned(l)) then
  3762. begin
  3763. new_section(current_asmdata.asmlists[al_picdata],sec_data_nonlazy,'',sizeof(pint));
  3764. l:=current_asmdata.DefineAsmSymbol(nlsymname,AB_LOCAL,AT_DATA);
  3765. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create(l,0));
  3766. if not(weak) then
  3767. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.RefAsmSymbol(symname).Name))
  3768. else
  3769. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.WeakRefAsmSymbol(symname).Name));
  3770. {$ifdef cpu64bitaddr}
  3771. current_asmdata.asmlists[al_picdata].concat(tai_const.create_64bit(0));
  3772. {$else cpu64bitaddr}
  3773. current_asmdata.asmlists[al_picdata].concat(tai_const.create_32bit(0));
  3774. {$endif cpu64bitaddr}
  3775. end;
  3776. result := getaddressregister(list);
  3777. reference_reset_symbol(ref,l,0,sizeof(pint));
  3778. { a_load_ref_reg will turn this into a pic-load if needed }
  3779. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,result);
  3780. end;
  3781. end;
  3782. end;
  3783. procedure tcg.g_maybe_got_init(list: TAsmList);
  3784. begin
  3785. end;
  3786. procedure tcg.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  3787. begin
  3788. internalerror(200807231);
  3789. end;
  3790. procedure tcg.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  3791. begin
  3792. internalerror(200807232);
  3793. end;
  3794. procedure tcg.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  3795. begin
  3796. internalerror(200807233);
  3797. end;
  3798. procedure tcg.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  3799. begin
  3800. internalerror(200807234);
  3801. end;
  3802. function tcg.getflagregister(list: TAsmList; size: Tcgsize): Tregister;
  3803. begin
  3804. Result:=TRegister(0);
  3805. internalerror(200807238);
  3806. end;
  3807. {*****************************************************************************
  3808. TCG64
  3809. *****************************************************************************}
  3810. {$ifndef cpu64bitalu}
  3811. procedure tcg64.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64; regsrc,regdst : tregister64);
  3812. begin
  3813. a_load64_reg_reg(list,regsrc,regdst);
  3814. a_op64_const_reg(list,op,size,value,regdst);
  3815. end;
  3816. procedure tcg64.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  3817. var
  3818. tmpreg64 : tregister64;
  3819. begin
  3820. { when src1=dst then we need to first create a temp to prevent
  3821. overwriting src1 with src2 }
  3822. if (regsrc1.reghi=regdst.reghi) or
  3823. (regsrc1.reglo=regdst.reghi) or
  3824. (regsrc1.reghi=regdst.reglo) or
  3825. (regsrc1.reglo=regdst.reglo) then
  3826. begin
  3827. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  3828. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  3829. a_load64_reg_reg(list,regsrc2,tmpreg64);
  3830. a_op64_reg_reg(list,op,size,regsrc1,tmpreg64);
  3831. a_load64_reg_reg(list,tmpreg64,regdst);
  3832. end
  3833. else
  3834. begin
  3835. a_load64_reg_reg(list,regsrc2,regdst);
  3836. a_op64_reg_reg(list,op,size,regsrc1,regdst);
  3837. end;
  3838. end;
  3839. procedure tcg64.a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  3840. var
  3841. tmpreg64 : tregister64;
  3842. begin
  3843. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  3844. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  3845. a_load64_subsetref_reg(list,sref,tmpreg64);
  3846. a_op64_const_reg(list,op,size,a,tmpreg64);
  3847. a_load64_reg_subsetref(list,tmpreg64,sref);
  3848. end;
  3849. procedure tcg64.a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  3850. var
  3851. tmpreg64 : tregister64;
  3852. begin
  3853. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  3854. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  3855. a_load64_subsetref_reg(list,sref,tmpreg64);
  3856. a_op64_reg_reg(list,op,size,reg,tmpreg64);
  3857. a_load64_reg_subsetref(list,tmpreg64,sref);
  3858. end;
  3859. procedure tcg64.a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  3860. var
  3861. tmpreg64 : tregister64;
  3862. begin
  3863. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  3864. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  3865. a_load64_subsetref_reg(list,sref,tmpreg64);
  3866. a_op64_ref_reg(list,op,size,ref,tmpreg64);
  3867. a_load64_reg_subsetref(list,tmpreg64,sref);
  3868. end;
  3869. procedure tcg64.a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  3870. var
  3871. tmpreg64 : tregister64;
  3872. begin
  3873. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  3874. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  3875. a_load64_subsetref_reg(list,ssref,tmpreg64);
  3876. a_op64_reg_subsetref(list,op,size,tmpreg64,dsref);
  3877. end;
  3878. procedure tcg64.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  3879. begin
  3880. a_op64_const_reg_reg(list,op,size,value,regsrc,regdst);
  3881. ovloc.loc:=LOC_VOID;
  3882. end;
  3883. procedure tcg64.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  3884. begin
  3885. a_op64_reg_reg_reg(list,op,size,regsrc1,regsrc2,regdst);
  3886. ovloc.loc:=LOC_VOID;
  3887. end;
  3888. procedure tcg64.a_load64_loc_subsetref(list : TAsmList;const l: tlocation; const sref : tsubsetreference);
  3889. begin
  3890. case l.loc of
  3891. LOC_REFERENCE, LOC_CREFERENCE:
  3892. a_load64_ref_subsetref(list,l.reference,sref);
  3893. LOC_REGISTER,LOC_CREGISTER:
  3894. a_load64_reg_subsetref(list,l.register64,sref);
  3895. LOC_CONSTANT :
  3896. a_load64_const_subsetref(list,l.value64,sref);
  3897. LOC_SUBSETREF,LOC_CSUBSETREF:
  3898. a_load64_subsetref_subsetref(list,l.sref,sref);
  3899. else
  3900. internalerror(2006082210);
  3901. end;
  3902. end;
  3903. procedure tcg64.a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  3904. begin
  3905. case l.loc of
  3906. LOC_REFERENCE, LOC_CREFERENCE:
  3907. a_load64_subsetref_ref(list,sref,l.reference);
  3908. LOC_REGISTER,LOC_CREGISTER:
  3909. a_load64_subsetref_reg(list,sref,l.register64);
  3910. LOC_SUBSETREF,LOC_CSUBSETREF:
  3911. a_load64_subsetref_subsetref(list,sref,l.sref);
  3912. else
  3913. internalerror(2006082211);
  3914. end;
  3915. end;
  3916. {$endif cpu64bitalu}
  3917. procedure destroy_codegen;
  3918. begin
  3919. cg.free;
  3920. cg:=nil;
  3921. {$ifndef cpu64bitalu}
  3922. cg64.free;
  3923. cg64:=nil;
  3924. {$endif cpu64bitalu}
  3925. end;
  3926. end.