cgobj.pas 171 KB

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