cgobj.pas 186 KB

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