cgobj.pas 144 KB

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