cgobj.pas 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999
  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. globtype,constexp,
  31. cpubase,cgbase,cgutils,parabase,
  32. aasmbase,aasmtai,aasmdata,aasmcpu,
  33. symconst,symtype,symdef,rgobj
  34. ;
  35. type
  36. talignment = (AM_NATURAL,AM_NONE,AM_2BYTE,AM_4BYTE,AM_8BYTE);
  37. {# @abstract(Abstract code generator)
  38. This class implements an abstract instruction generator. Some of
  39. the methods of this class are generic, while others must
  40. be overridden for all new processors which will be supported
  41. by Free Pascal. For 32-bit processors, the base class
  42. should be @link(tcg64f32) and not @var(tcg).
  43. }
  44. { tcg }
  45. tcg = class
  46. { how many times is this current code executed }
  47. executionweight : longint;
  48. alignment : talignment;
  49. rg : array[tregistertype] of trgobj;
  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_live_range_direction(dir: TRADirection);
  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;
  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. It must generate register allocation information for the cgpara in
  102. case it consists of cpuregisters.
  103. @param(size size of the operand in the register)
  104. @param(r register source of the operand)
  105. @param(cgpara where the parameter will be stored)
  106. }
  107. procedure a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : TCGPara);virtual;
  108. {# Pass a parameter, which is a constant, to a routine.
  109. A generic version is provided. This routine should
  110. be overridden for optimization purposes if the cpu
  111. permits directly sending this type of parameter.
  112. It must generate register allocation information for the cgpara in
  113. case it consists of cpuregisters.
  114. @param(size size of the operand in constant)
  115. @param(a value of constant to send)
  116. @param(cgpara where the parameter will be stored)
  117. }
  118. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : TCGPara);virtual;
  119. {# Pass the value of a parameter, which is located in memory, to a routine.
  120. A generic version is provided. This routine should
  121. be overridden for optimization purposes if the cpu
  122. permits directly sending this type of parameter.
  123. It must generate register allocation information for the cgpara in
  124. case it consists of cpuregisters.
  125. @param(size size of the operand in constant)
  126. @param(r Memory reference of value to send)
  127. @param(cgpara where the parameter will be stored)
  128. }
  129. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : TCGPara);virtual;
  130. {# Pass the value of a parameter, which can be located either in a register or memory location,
  131. to a routine.
  132. A generic version is provided.
  133. @param(l location of the operand to send)
  134. @param(nr parameter number (starting from one) of routine (from left to right))
  135. @param(cgpara where the parameter will be stored)
  136. }
  137. procedure a_load_loc_cgpara(list : TAsmList;const l : tlocation;const cgpara : TCGPara);
  138. {# Pass the address of a reference to a routine. This routine
  139. will calculate the address of the reference, and pass this
  140. calculated address as a parameter.
  141. It must generate register allocation information for the cgpara in
  142. case it consists of cpuregisters.
  143. A generic version is provided. This routine should
  144. be overridden for optimization purposes if the cpu
  145. permits directly sending this type of parameter.
  146. @param(r reference to get address from)
  147. @param(nr parameter number (starting from one) of routine (from left to right))
  148. }
  149. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : TCGPara);virtual;
  150. {# Load a cgparaloc into a memory reference.
  151. It must generate register allocation information for the cgpara in
  152. case it consists of cpuregisters.
  153. @param(paraloc the source parameter sublocation)
  154. @param(ref the destination reference)
  155. @param(sizeleft indicates the total number of bytes left in all of
  156. the remaining sublocations of this parameter (the current
  157. sublocation and all of the sublocations coming after it).
  158. In case this location is also a reference, it is assumed
  159. to be the final part sublocation of the parameter and that it
  160. contains all of the "sizeleft" bytes).)
  161. @param(align the alignment of the paraloc in case it's a reference)
  162. }
  163. procedure a_load_cgparaloc_ref(list : TAsmList;const paraloc : TCGParaLocation;const ref : treference;sizeleft : tcgint;align : longint);
  164. {# Load a cgparaloc into any kind of register (int, fp, mm).
  165. @param(regsize the size of the destination register)
  166. @param(paraloc the source parameter sublocation)
  167. @param(reg the destination register)
  168. @param(align the alignment of the paraloc in case it's a reference)
  169. }
  170. procedure a_load_cgparaloc_anyreg(list : TAsmList;regsize : tcgsize;const paraloc : TCGParaLocation;reg : tregister;align : longint);
  171. { Remarks:
  172. * If a method specifies a size you have only to take care
  173. of that number of bits, i.e. load_const_reg with OP_8 must
  174. only load the lower 8 bit of the specified register
  175. the rest of the register can be undefined
  176. if necessary the compiler will call a method
  177. to zero or sign extend the register
  178. * The a_load_XX_XX with OP_64 needn't to be
  179. implemented for 32 bit
  180. processors, the code generator takes care of that
  181. * the addr size is for work with the natural pointer
  182. size
  183. * the procedures without fpu/mm are only for integer usage
  184. * normally the first location is the source and the
  185. second the destination
  186. }
  187. {# Emits instruction to call the method specified by symbol name.
  188. This routine must be overridden for each new target cpu.
  189. }
  190. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);virtual; abstract;
  191. procedure a_call_reg(list : TAsmList;reg : tregister);virtual; abstract;
  192. { same as a_call_name, might be overridden on certain architectures to emit
  193. static calls without usage of a got trampoline }
  194. procedure a_call_name_static(list : TAsmList;const s : string);virtual;
  195. { move instructions }
  196. procedure a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);virtual; abstract;
  197. procedure a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : treference);virtual;
  198. procedure a_load_const_loc(list : TAsmList;a : tcgint;const loc : tlocation);
  199. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  200. procedure a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual;
  201. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  202. procedure a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  203. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  204. procedure a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual;
  205. procedure a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);virtual;
  206. procedure a_load_loc_reg(list : TAsmList;tosize: tcgsize; const loc: tlocation; reg : tregister);
  207. procedure a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  208. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);virtual; abstract;
  209. { bit scan instructions }
  210. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: tcgsize; src, dst: TRegister); virtual;
  211. { Multiplication with doubling result size.
  212. dstlo or dsthi may be NR_NO, in which case corresponding half of result is discarded. }
  213. procedure a_mul_reg_reg_pair(list: TAsmList; size: tcgsize; src1,src2,dstlo,dsthi: TRegister);virtual;
  214. { fpu move instructions }
  215. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize:tcgsize; reg1, reg2: tregister); virtual; abstract;
  216. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  217. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  218. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tcgsize; const ref1,ref2: treference);
  219. procedure a_loadfpu_loc_reg(list: TAsmList; tosize: tcgsize; const loc: tlocation; const reg: tregister);
  220. procedure a_loadfpu_reg_loc(list: TAsmList; fromsize: tcgsize; const reg: tregister; const loc: tlocation);
  221. procedure a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);virtual;
  222. procedure a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);virtual;
  223. { vector register move instructions }
  224. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); virtual;
  225. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  226. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual;
  227. procedure a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  228. procedure a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  229. procedure a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  230. procedure a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  231. procedure a_loadmm_loc_cgpara(list: TAsmList; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  232. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle); virtual;
  233. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  234. procedure a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  235. procedure a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  236. procedure a_opmm_loc_reg_reg(list: TAsmList;Op : TOpCG;size : tcgsize;const loc : tlocation;src,dst : tregister;shuffle : pmmshuffle); virtual;
  237. procedure a_opmm_reg_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src1,src2,dst: tregister;shuffle : pmmshuffle); virtual;
  238. procedure a_opmm_ref_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; src,dst: tregister;shuffle : pmmshuffle); virtual;
  239. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle); virtual;
  240. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize; mmreg, intreg: tregister; shuffle : pmmshuffle); virtual;
  241. { basic arithmetic operations }
  242. { note: for operators which require only one argument (not, neg), use }
  243. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  244. { that in this case the *second* operand is used as both source and }
  245. { destination (JM) }
  246. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); virtual; abstract;
  247. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); virtual;
  248. procedure a_op_const_loc(list : TAsmList; Op: TOpCG; a: tcgint; const loc: tlocation);
  249. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  250. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  251. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  252. procedure a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  253. procedure a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  254. { trinary operations for processors that support them, 'emulated' }
  255. { on others. None with "ref" arguments since I don't think there }
  256. { are any processors that support it (JM) }
  257. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister); virtual;
  258. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister); virtual;
  259. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  260. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  261. { comparison operations }
  262. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  263. l : tasmlabel); virtual;
  264. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  265. l : tasmlabel); virtual;
  266. procedure a_cmp_const_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; a: tcgint; const loc: tlocation;
  267. l : tasmlabel);
  268. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  269. procedure a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  270. procedure a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  271. procedure a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  272. procedure a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  273. procedure a_cmp_ref_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  274. l : tasmlabel);
  275. procedure a_jmp_name(list : TAsmList;const s : string); virtual; abstract;
  276. procedure a_jmp_always(list : TAsmList;l: tasmlabel); virtual; abstract;
  277. {$ifdef cpuflags}
  278. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); virtual; abstract;
  279. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  280. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  281. }
  282. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  283. procedure g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  284. {$endif cpuflags}
  285. {
  286. This routine tries to optimize the op_const_reg/ref opcode, and should be
  287. called at the start of a_op_const_reg/ref. It returns the actual opcode
  288. to emit, and the constant value to emit. This function can opcode OP_NONE to
  289. remove the opcode and OP_MOVE to replace it with a simple load
  290. @param(size Size of the operand in constant)
  291. @param(op The opcode to emit, returns the opcode which must be emitted)
  292. @param(a The constant which should be emitted, returns the constant which must
  293. be emitted)
  294. }
  295. procedure optimize_op_const(size: TCGSize; var op: topcg; var a : tcgint);virtual;
  296. {#
  297. This routine is used in exception management nodes. It should
  298. save the exception reason currently in the FUNCTION_RETURN_REG. The
  299. save should be done either to a temp (pointed to by href).
  300. or on the stack (pushing the value on the stack).
  301. The size of the value to save is OS_S32. The default version
  302. saves the exception reason to a temp. memory area.
  303. }
  304. procedure g_exception_reason_save(list : TAsmList; const href : treference);virtual;
  305. {#
  306. This routine is used in exception management nodes. It should
  307. save the exception reason constant. The
  308. save should be done either to a temp (pointed to by href).
  309. or on the stack (pushing the value on the stack).
  310. The size of the value to save is OS_S32. The default version
  311. saves the exception reason to a temp. memory area.
  312. }
  313. procedure g_exception_reason_save_const(list : TAsmList; const href : treference; a: tcgint);virtual;
  314. {#
  315. This routine is used in exception management nodes. It should
  316. load the exception reason to the FUNCTION_RETURN_REG. The saved value
  317. should either be in the temp. area (pointed to by href , href should
  318. *NOT* be freed) or on the stack (the value should be popped).
  319. The size of the value to save is OS_S32. The default version
  320. saves the exception reason to a temp. memory area.
  321. }
  322. procedure g_exception_reason_load(list : TAsmList; const href : treference);virtual;
  323. procedure g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  324. {# This should emit the opcode to copy len bytes from the source
  325. to destination.
  326. It must be overridden for each new target processor.
  327. @param(source Source reference of copy)
  328. @param(dest Destination reference of copy)
  329. }
  330. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);virtual; abstract;
  331. {# This should emit the opcode to copy len bytes from the an unaligned source
  332. to destination.
  333. It must be overridden for each new target processor.
  334. @param(source Source reference of copy)
  335. @param(dest Destination reference of copy)
  336. }
  337. procedure g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);virtual;
  338. {# Generates overflow checking code for a node }
  339. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef); virtual;abstract;
  340. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);virtual;
  341. {# Emits instructions when compilation is done in profile
  342. mode (this is set as a command line option). The default
  343. behavior does nothing, should be overridden as required.
  344. }
  345. procedure g_profilecode(list : TAsmList);virtual;
  346. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  347. @param(size Number of bytes to allocate)
  348. }
  349. procedure g_stackpointer_alloc(list : TAsmList;size : longint);virtual;
  350. {# Emits instruction for allocating the locals in entry
  351. code of a routine. This is one of the first
  352. routine called in @var(genentrycode).
  353. @param(localsize Number of bytes to allocate as locals)
  354. }
  355. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);virtual; abstract;
  356. {# Emits instructions for returning from a subroutine.
  357. Should also restore the framepointer and stack.
  358. @param(parasize Number of bytes of parameters to deallocate from stack)
  359. }
  360. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);virtual;abstract;
  361. {# This routine is called when generating the code for the entry point
  362. of a routine. It should save all registers which are not used in this
  363. routine, and which should be declared as saved in the std_saved_registers
  364. set.
  365. This routine is mainly used when linking to code which is generated
  366. by ABI-compliant compilers (like GCC), to make sure that the reserved
  367. registers of that ABI are not clobbered.
  368. @param(usedinproc Registers which are used in the code of this routine)
  369. }
  370. procedure g_save_registers(list:TAsmList);virtual;
  371. {# This routine is called when generating the code for the exit point
  372. of a routine. It should restore all registers which were previously
  373. saved in @var(g_save_standard_registers).
  374. @param(usedinproc Registers which are used in the code of this routine)
  375. }
  376. procedure g_restore_registers(list:TAsmList);virtual;
  377. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);virtual;abstract;
  378. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);virtual;
  379. { generate a stub which only purpose is to pass control the given external method,
  380. setting up any additional environment before doing so (if required).
  381. The default implementation issues a jump instruction to the external name. }
  382. procedure g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string); virtual;
  383. { initialize the pic/got register }
  384. procedure g_maybe_got_init(list: TAsmList); virtual;
  385. { allocallcpuregisters, a_call_name, deallocallcpuregisters sequence }
  386. procedure g_call(list: TAsmList; const s: string);
  387. { Generate code to exit an unwind-protected region. The default implementation
  388. produces a simple jump to destination label. }
  389. procedure g_local_unwind(list: TAsmList; l: TAsmLabel);virtual;
  390. { Generate code for integer division by constant,
  391. generic version is suitable for 3-address CPUs }
  392. procedure g_div_const_reg_reg(list:tasmlist; size: TCgSize; a: tcgint; src,dst: tregister); virtual;
  393. protected
  394. function g_indirect_sym_load(list:TAsmList;const symname: string; const flags: tindsymflags): tregister;virtual;
  395. end;
  396. {$ifdef cpu64bitalu}
  397. { This class implements an abstract code generator class
  398. for 128 Bit operations, it applies currently only to 64 Bit CPUs and supports only simple operations
  399. }
  400. tcg128 = class
  401. procedure a_load128_reg_reg(list : TAsmList;regsrc,regdst : tregister128);virtual;
  402. procedure a_load128_reg_ref(list : TAsmList;reg : tregister128;const ref : treference);virtual;
  403. procedure a_load128_ref_reg(list : TAsmList;const ref : treference;reg : tregister128);virtual;
  404. procedure a_load128_loc_ref(list : TAsmList;const l : tlocation;const ref : treference);virtual;
  405. procedure a_load128_reg_loc(list : TAsmList;reg : tregister128;const l : tlocation);virtual;
  406. procedure a_load128_const_reg(list : TAsmList;valuelo,valuehi : int64;reg : tregister128);virtual;
  407. procedure a_load128_loc_cgpara(list : TAsmList;const l : tlocation;const paraloc : TCGPara);virtual;
  408. procedure a_load128_ref_cgpara(list: TAsmList; const r: treference;const paraloc: tcgpara);
  409. procedure a_load128_reg_cgpara(list: TAsmList; reg: tregister128;const paraloc: tcgpara);
  410. end;
  411. { Creates a tregister128 record from 2 64 Bit registers. }
  412. function joinreg128(reglo,reghi : tregister) : tregister128;
  413. {$else cpu64bitalu}
  414. {# @abstract(Abstract code generator for 64 Bit operations)
  415. This class implements an abstract code generator class
  416. for 64 Bit operations.
  417. }
  418. tcg64 = class
  419. procedure a_load64_const_ref(list : TAsmList;value : int64;const ref : treference);virtual;abstract;
  420. procedure a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference);virtual;abstract;
  421. procedure a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64);virtual;abstract;
  422. procedure a_load64_reg_reg(list : TAsmList;regsrc,regdst : tregister64);virtual;abstract;
  423. procedure a_load64_const_reg(list : TAsmList;value : int64;reg : tregister64);virtual;abstract;
  424. procedure a_load64_loc_reg(list : TAsmList;const l : tlocation;reg : tregister64);virtual;abstract;
  425. procedure a_load64_loc_ref(list : TAsmList;const l : tlocation;const ref : treference);virtual;abstract;
  426. procedure a_load64_const_loc(list : TAsmList;value : int64;const l : tlocation);virtual;abstract;
  427. procedure a_load64_reg_loc(list : TAsmList;reg : tregister64;const l : tlocation);virtual;abstract;
  428. procedure a_load64_subsetref_reg(list : TAsmList; const sref: tsubsetreference; destreg: tregister64);virtual;abstract;
  429. procedure a_load64_reg_subsetref(list : TAsmList; fromreg: tregister64; const sref: tsubsetreference);virtual;abstract;
  430. procedure a_load64_const_subsetref(list: TAsmlist; a: int64; const sref: tsubsetreference);virtual;abstract;
  431. procedure a_load64_ref_subsetref(list : TAsmList; const fromref: treference; const sref: tsubsetreference);virtual;abstract;
  432. procedure a_load64_subsetref_subsetref(list: TAsmlist; const fromsref, tosref: tsubsetreference); virtual;abstract;
  433. procedure a_load64_subsetref_ref(list : TAsmList; const sref: tsubsetreference; const destref: treference); virtual;abstract;
  434. procedure a_load64_loc_subsetref(list : TAsmList; const l: tlocation; const sref : tsubsetreference);
  435. procedure a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  436. procedure a_load64high_reg_ref(list : TAsmList;reg : tregister;const ref : treference);virtual;abstract;
  437. procedure a_load64low_reg_ref(list : TAsmList;reg : tregister;const ref : treference);virtual;abstract;
  438. procedure a_load64high_ref_reg(list : TAsmList;const ref : treference;reg : tregister);virtual;abstract;
  439. procedure a_load64low_ref_reg(list : TAsmList;const ref : treference;reg : tregister);virtual;abstract;
  440. procedure a_load64high_loc_reg(list : TAsmList;const l : tlocation;reg : tregister);virtual;abstract;
  441. procedure a_load64low_loc_reg(list : TAsmList;const l : tlocation;reg : tregister);virtual;abstract;
  442. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);virtual;abstract;
  443. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);virtual;abstract;
  444. procedure a_op64_reg_ref(list : TAsmList;op:TOpCG;size : tcgsize;regsrc : tregister64;const ref : treference);virtual;abstract;
  445. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;regdst : tregister64);virtual;abstract;
  446. procedure a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);virtual;abstract;
  447. procedure a_op64_const_loc(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const l: tlocation);virtual;abstract;
  448. procedure a_op64_reg_loc(list : TAsmList;op:TOpCG;size : tcgsize;reg : tregister64;const l : tlocation);virtual;abstract;
  449. procedure a_op64_loc_reg(list : TAsmList;op:TOpCG;size : tcgsize;const l : tlocation;reg64 : tregister64);virtual;abstract;
  450. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);virtual;
  451. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);virtual;
  452. procedure a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  453. procedure a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  454. procedure a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  455. procedure a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  456. procedure a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  457. procedure a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  458. procedure a_load64_reg_cgpara(list : TAsmList;reg64 : tregister64;const loc : TCGPara);virtual;abstract;
  459. procedure a_load64_const_cgpara(list : TAsmList;value : int64;const loc : TCGPara);virtual;abstract;
  460. procedure a_load64_ref_cgpara(list : TAsmList;const r : treference;const loc : TCGPara);virtual;abstract;
  461. procedure a_load64_loc_cgpara(list : TAsmList;const l : tlocation;const loc : TCGPara);virtual;abstract;
  462. procedure a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister); virtual;abstract;
  463. procedure a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64); virtual;abstract;
  464. {
  465. This routine tries to optimize the const_reg opcode, and should be
  466. called at the start of a_op64_const_reg. It returns the actual opcode
  467. to emit, and the constant value to emit. If this routine returns
  468. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  469. @param(op The opcode to emit, returns the opcode which must be emitted)
  470. @param(a The constant which should be emitted, returns the constant which must
  471. be emitted)
  472. @param(reg The register to emit the opcode with, returns the register with
  473. which the opcode will be emitted)
  474. }
  475. function optimize64_op_const_reg(list: TAsmList; var op: topcg; var a : int64; var reg: tregister64): boolean;virtual;abstract;
  476. { override to catch 64bit rangechecks }
  477. procedure g_rangecheck64(list: TAsmList; const l:tlocation; fromdef,todef: tdef);virtual;abstract;
  478. end;
  479. { Creates a tregister64 record from 2 32 Bit registers. }
  480. function joinreg64(reglo,reghi : tregister) : tregister64;
  481. {$endif cpu64bitalu}
  482. var
  483. { Main code generator class }
  484. cg : tcg;
  485. {$ifdef cpu64bitalu}
  486. { Code generator class for all operations working with 128-Bit operands }
  487. cg128 : tcg128;
  488. {$else cpu64bitalu}
  489. { Code generator class for all operations working with 64-Bit operands }
  490. cg64 : tcg64;
  491. {$endif cpu64bitalu}
  492. function asmsym2indsymflags(sym: TAsmSymbol): tindsymflags;
  493. procedure destroy_codegen;
  494. implementation
  495. uses
  496. globals,systems,
  497. verbose,paramgr,symtable,symsym,
  498. tgobj,cutils,procinfo;
  499. {*****************************************************************************
  500. basic functionallity
  501. ******************************************************************************}
  502. constructor tcg.create;
  503. begin
  504. end;
  505. {*****************************************************************************
  506. register allocation
  507. ******************************************************************************}
  508. procedure tcg.init_register_allocators;
  509. begin
  510. fillchar(rg,sizeof(rg),0);
  511. add_reg_instruction_hook:=@add_reg_instruction;
  512. executionweight:=1;
  513. end;
  514. procedure tcg.done_register_allocators;
  515. begin
  516. { Safety }
  517. fillchar(rg,sizeof(rg),0);
  518. add_reg_instruction_hook:=nil;
  519. end;
  520. {$ifdef flowgraph}
  521. procedure Tcg.init_flowgraph;
  522. begin
  523. aktflownode:=0;
  524. end;
  525. procedure Tcg.done_flowgraph;
  526. begin
  527. end;
  528. {$endif}
  529. function tcg.getintregister(list:TAsmList;size:Tcgsize):Tregister;
  530. begin
  531. if not assigned(rg[R_INTREGISTER]) then
  532. internalerror(200312122);
  533. result:=rg[R_INTREGISTER].getregister(list,cgsize2subreg(R_INTREGISTER,size));
  534. end;
  535. function tcg.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  536. begin
  537. if not assigned(rg[R_FPUREGISTER]) then
  538. internalerror(200312123);
  539. result:=rg[R_FPUREGISTER].getregister(list,cgsize2subreg(R_FPUREGISTER,size));
  540. end;
  541. function tcg.getmmregister(list:TAsmList;size:Tcgsize):Tregister;
  542. begin
  543. if not assigned(rg[R_MMREGISTER]) then
  544. internalerror(2003121214);
  545. result:=rg[R_MMREGISTER].getregister(list,cgsize2subreg(R_MMREGISTER,size));
  546. end;
  547. function tcg.getaddressregister(list:TAsmList):Tregister;
  548. begin
  549. if assigned(rg[R_ADDRESSREGISTER]) then
  550. result:=rg[R_ADDRESSREGISTER].getregister(list,R_SUBWHOLE)
  551. else
  552. begin
  553. if not assigned(rg[R_INTREGISTER]) then
  554. internalerror(200312121);
  555. result:=rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  556. end;
  557. end;
  558. function Tcg.makeregsize(list:TAsmList;reg:Tregister;size:Tcgsize):Tregister;
  559. var
  560. subreg:Tsubregister;
  561. begin
  562. subreg:=cgsize2subreg(getregtype(reg),size);
  563. result:=reg;
  564. setsubreg(result,subreg);
  565. { notify RA }
  566. if result<>reg then
  567. list.concat(tai_regalloc.resize(result));
  568. end;
  569. procedure tcg.getcpuregister(list:TAsmList;r:Tregister);
  570. begin
  571. if not assigned(rg[getregtype(r)]) then
  572. internalerror(200312125);
  573. rg[getregtype(r)].getcpuregister(list,r);
  574. end;
  575. procedure tcg.ungetcpuregister(list:TAsmList;r:Tregister);
  576. begin
  577. if not assigned(rg[getregtype(r)]) then
  578. internalerror(200312126);
  579. rg[getregtype(r)].ungetcpuregister(list,r);
  580. end;
  581. procedure tcg.alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  582. begin
  583. if assigned(rg[rt]) then
  584. rg[rt].alloccpuregisters(list,r)
  585. else
  586. internalerror(200310092);
  587. end;
  588. procedure tcg.allocallcpuregisters(list:TAsmList);
  589. begin
  590. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  591. if uses_registers(R_ADDRESSREGISTER) then
  592. alloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  593. {$if not(defined(i386)) and not(defined(i8086)) and not(defined(avr))}
  594. if uses_registers(R_FPUREGISTER) then
  595. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  596. {$ifdef cpumm}
  597. if uses_registers(R_MMREGISTER) then
  598. alloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  599. {$endif cpumm}
  600. {$endif not(defined(i386)) and not(defined(i8086)) and not(defined(avr))}
  601. end;
  602. procedure tcg.dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  603. begin
  604. if assigned(rg[rt]) then
  605. rg[rt].dealloccpuregisters(list,r)
  606. else
  607. internalerror(200310093);
  608. end;
  609. procedure tcg.deallocallcpuregisters(list:TAsmList);
  610. begin
  611. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  612. if uses_registers(R_ADDRESSREGISTER) then
  613. dealloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  614. {$if not(defined(i386)) and not(defined(i8086)) and not(defined(avr))}
  615. if uses_registers(R_FPUREGISTER) then
  616. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  617. {$ifdef cpumm}
  618. if uses_registers(R_MMREGISTER) then
  619. dealloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  620. {$endif cpumm}
  621. {$endif not(defined(i386)) and not(defined(i8086)) and not(defined(avr))}
  622. end;
  623. function tcg.uses_registers(rt:Tregistertype):boolean;
  624. begin
  625. if assigned(rg[rt]) then
  626. result:=rg[rt].uses_registers
  627. else
  628. result:=false;
  629. end;
  630. procedure tcg.add_reg_instruction(instr:Tai;r:tregister);
  631. var
  632. rt : tregistertype;
  633. begin
  634. rt:=getregtype(r);
  635. { Only add it when a register allocator is configured.
  636. No IE can be generated, because the VMT is written
  637. without a valid rg[] }
  638. if assigned(rg[rt]) then
  639. rg[rt].add_reg_instruction(instr,r,executionweight);
  640. end;
  641. procedure tcg.add_move_instruction(instr:Taicpu);
  642. var
  643. rt : tregistertype;
  644. begin
  645. rt:=getregtype(instr.oper[O_MOV_SOURCE]^.reg);
  646. if assigned(rg[rt]) then
  647. rg[rt].add_move_instruction(instr)
  648. else
  649. internalerror(200310095);
  650. end;
  651. procedure tcg.set_regalloc_live_range_direction(dir: TRADirection);
  652. var
  653. rt : tregistertype;
  654. begin
  655. for rt:=low(rg) to high(rg) do
  656. begin
  657. if assigned(rg[rt]) then
  658. rg[rt].live_range_direction:=dir;
  659. end;
  660. end;
  661. procedure tcg.do_register_allocation(list:TAsmList;headertai:tai);
  662. var
  663. rt : tregistertype;
  664. begin
  665. for rt:=R_FPUREGISTER to R_SPECIALREGISTER do
  666. begin
  667. if assigned(rg[rt]) then
  668. rg[rt].do_register_allocation(list,headertai);
  669. end;
  670. { running the other register allocator passes could require addition int/addr. registers
  671. when spilling so run int/addr register allocation at the end }
  672. if assigned(rg[R_INTREGISTER]) then
  673. rg[R_INTREGISTER].do_register_allocation(list,headertai);
  674. if assigned(rg[R_ADDRESSREGISTER]) then
  675. rg[R_ADDRESSREGISTER].do_register_allocation(list,headertai);
  676. end;
  677. procedure tcg.translate_register(var reg : tregister);
  678. var
  679. rt: tregistertype;
  680. begin
  681. { Getting here without assigned rg is possible for an "assembler nostackframe"
  682. function returning x87 float, compiler tries to translate NR_ST which is used for
  683. result. }
  684. rt:=getregtype(reg);
  685. if assigned(rg[rt]) then
  686. rg[rt].translate_register(reg);
  687. end;
  688. procedure tcg.a_reg_alloc(list : TAsmList;r : tregister);
  689. begin
  690. list.concat(tai_regalloc.alloc(r,nil));
  691. end;
  692. procedure tcg.a_reg_dealloc(list : TAsmList;r : tregister);
  693. begin
  694. if (r<>NR_NO) then
  695. list.concat(tai_regalloc.dealloc(r,nil));
  696. end;
  697. procedure tcg.a_reg_sync(list : TAsmList;r : tregister);
  698. var
  699. instr : tai;
  700. begin
  701. instr:=tai_regalloc.sync(r);
  702. list.concat(instr);
  703. add_reg_instruction(instr,r);
  704. end;
  705. procedure tcg.a_label(list : TAsmList;l : tasmlabel);
  706. begin
  707. list.concat(tai_label.create(l));
  708. end;
  709. {*****************************************************************************
  710. for better code generation these methods should be overridden
  711. ******************************************************************************}
  712. procedure tcg.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : TCGPara);
  713. var
  714. ref : treference;
  715. tmpreg : tregister;
  716. begin
  717. cgpara.check_simple_location;
  718. paramanager.alloccgpara(list,cgpara);
  719. if cgpara.location^.shiftval<0 then
  720. begin
  721. tmpreg:=getintregister(list,cgpara.location^.size);
  722. a_op_const_reg_reg(list,OP_SHL,cgpara.location^.size,-cgpara.location^.shiftval,r,tmpreg);
  723. r:=tmpreg;
  724. end;
  725. case cgpara.location^.loc of
  726. LOC_REGISTER,LOC_CREGISTER:
  727. a_load_reg_reg(list,size,cgpara.location^.size,r,cgpara.location^.register);
  728. LOC_REFERENCE,LOC_CREFERENCE:
  729. begin
  730. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  731. a_load_reg_ref(list,size,cgpara.location^.size,r,ref);
  732. end;
  733. LOC_MMREGISTER,LOC_CMMREGISTER:
  734. a_loadmm_intreg_reg(list,size,cgpara.location^.size,r,cgpara.location^.register,mms_movescalar);
  735. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  736. begin
  737. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,ref);
  738. a_load_reg_ref(list,size,size,r,ref);
  739. a_loadfpu_ref_cgpara(list,cgpara.location^.size,ref,cgpara);
  740. tg.Ungettemp(list,ref);
  741. end
  742. else
  743. internalerror(2002071004);
  744. end;
  745. end;
  746. procedure tcg.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : TCGPara);
  747. var
  748. ref : treference;
  749. begin
  750. cgpara.check_simple_location;
  751. paramanager.alloccgpara(list,cgpara);
  752. if cgpara.location^.shiftval<0 then
  753. a:=a shl -cgpara.location^.shiftval;
  754. case cgpara.location^.loc of
  755. LOC_REGISTER,LOC_CREGISTER:
  756. a_load_const_reg(list,cgpara.location^.size,a,cgpara.location^.register);
  757. LOC_REFERENCE,LOC_CREFERENCE:
  758. begin
  759. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  760. a_load_const_ref(list,cgpara.location^.size,a,ref);
  761. end
  762. else
  763. internalerror(2010053109);
  764. end;
  765. end;
  766. procedure tcg.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : TCGPara);
  767. var
  768. tmpref, ref: treference;
  769. tmpreg: tregister;
  770. location: pcgparalocation;
  771. orgsizeleft,
  772. sizeleft: tcgint;
  773. reghasvalue: boolean;
  774. begin
  775. location:=cgpara.location;
  776. tmpref:=r;
  777. sizeleft:=cgpara.intsize;
  778. while assigned(location) do
  779. begin
  780. paramanager.allocparaloc(list,location);
  781. case location^.loc of
  782. LOC_REGISTER,LOC_CREGISTER:
  783. begin
  784. { Parameter locations are often allocated in multiples of
  785. entire registers. If a parameter only occupies a part of
  786. such a register (e.g. a 16 bit int on a 32 bit
  787. architecture), the size of this parameter can only be
  788. determined by looking at the "size" parameter of this
  789. method -> if the size parameter is <= sizeof(aint), then
  790. we check that there is only one parameter location and
  791. then use this "size" to load the value into the parameter
  792. location }
  793. if (size<>OS_NO) and
  794. (tcgsize2size[size]<=sizeof(aint)) then
  795. begin
  796. cgpara.check_simple_location;
  797. a_load_ref_reg(list,size,location^.size,tmpref,location^.register);
  798. if location^.shiftval<0 then
  799. a_op_const_reg(list,OP_SHL,location^.size,-location^.shiftval,location^.register);
  800. end
  801. { there's a lot more data left, and the current paraloc's
  802. register is entirely filled with part of that data }
  803. else if (sizeleft>sizeof(aint)) then
  804. begin
  805. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  806. end
  807. { we're at the end of the data, and it can be loaded into
  808. the current location's register with a single regular
  809. load }
  810. else if sizeleft in [1,2,4,8] then
  811. begin
  812. a_load_ref_reg(list,int_cgsize(sizeleft),location^.size,tmpref,location^.register);
  813. if location^.shiftval<0 then
  814. a_op_const_reg(list,OP_SHL,location^.size,-location^.shiftval,location^.register);
  815. end
  816. { we're at the end of the data, and we need multiple loads
  817. to get it in the register because it's an irregular size }
  818. else
  819. begin
  820. { should be the last part }
  821. if assigned(location^.next) then
  822. internalerror(2010052907);
  823. { load the value piecewise to get it into the register }
  824. orgsizeleft:=sizeleft;
  825. reghasvalue:=false;
  826. {$ifdef cpu64bitalu}
  827. if sizeleft>=4 then
  828. begin
  829. a_load_ref_reg(list,OS_32,location^.size,tmpref,location^.register);
  830. dec(sizeleft,4);
  831. if target_info.endian=endian_big then
  832. a_op_const_reg(list,OP_SHL,location^.size,sizeleft*8,location^.register);
  833. inc(tmpref.offset,4);
  834. reghasvalue:=true;
  835. end;
  836. {$endif cpu64bitalu}
  837. if sizeleft>=2 then
  838. begin
  839. tmpreg:=getintregister(list,location^.size);
  840. a_load_ref_reg(list,OS_16,location^.size,tmpref,tmpreg);
  841. dec(sizeleft,2);
  842. if reghasvalue then
  843. begin
  844. if target_info.endian=endian_big then
  845. a_op_const_reg(list,OP_SHL,location^.size,sizeleft*8,tmpreg)
  846. else
  847. a_op_const_reg(list,OP_SHL,location^.size,(orgsizeleft-(sizeleft+2))*8,tmpreg);
  848. a_op_reg_reg(list,OP_OR,location^.size,tmpreg,location^.register);
  849. end
  850. else
  851. begin
  852. if target_info.endian=endian_big then
  853. a_op_const_reg_reg(list,OP_SHL,location^.size,sizeleft*8,tmpreg,location^.register)
  854. else
  855. a_load_reg_reg(list,location^.size,location^.size,tmpreg,location^.register);
  856. end;
  857. inc(tmpref.offset,2);
  858. reghasvalue:=true;
  859. end;
  860. if sizeleft=1 then
  861. begin
  862. tmpreg:=getintregister(list,location^.size);
  863. a_load_ref_reg(list,OS_8,location^.size,tmpref,tmpreg);
  864. dec(sizeleft,1);
  865. if reghasvalue then
  866. begin
  867. if target_info.endian=endian_little then
  868. a_op_const_reg(list,OP_SHL,location^.size,(orgsizeleft-(sizeleft+1))*8,tmpreg);
  869. a_op_reg_reg(list,OP_OR,location^.size,tmpreg,location^.register)
  870. end
  871. else
  872. a_load_reg_reg(list,location^.size,location^.size,tmpreg,location^.register);
  873. inc(tmpref.offset);
  874. end;
  875. if location^.shiftval<0 then
  876. a_op_const_reg(list,OP_SHL,location^.size,-location^.shiftval,location^.register);
  877. { the loop will already adjust the offset and sizeleft }
  878. dec(tmpref.offset,orgsizeleft);
  879. sizeleft:=orgsizeleft;
  880. end;
  881. end;
  882. LOC_REFERENCE,LOC_CREFERENCE:
  883. begin
  884. if assigned(location^.next) then
  885. internalerror(2010052906);
  886. reference_reset_base(ref,location^.reference.index,location^.reference.offset,newalignment(cgpara.alignment,cgpara.intsize-sizeleft));
  887. if (size <> OS_NO) and
  888. (tcgsize2size[size] <= sizeof(aint)) then
  889. a_load_ref_ref(list,size,location^.size,tmpref,ref)
  890. else
  891. { use concatcopy, because the parameter can be larger than }
  892. { what the OS_* constants can handle }
  893. g_concatcopy(list,tmpref,ref,sizeleft);
  894. end;
  895. LOC_MMREGISTER,LOC_CMMREGISTER:
  896. begin
  897. case location^.size of
  898. OS_F32,
  899. OS_F64,
  900. OS_F128:
  901. a_loadmm_ref_reg(list,location^.size,location^.size,tmpref,location^.register,mms_movescalar);
  902. OS_M8..OS_M128,
  903. OS_MS8..OS_MS128:
  904. a_loadmm_ref_reg(list,location^.size,location^.size,tmpref,location^.register,nil);
  905. else
  906. internalerror(2010053101);
  907. end;
  908. end
  909. else
  910. internalerror(2010053111);
  911. end;
  912. inc(tmpref.offset,tcgsize2size[location^.size]);
  913. dec(sizeleft,tcgsize2size[location^.size]);
  914. location:=location^.next;
  915. end;
  916. end;
  917. procedure tcg.a_load_loc_cgpara(list : TAsmList;const l:tlocation;const cgpara : TCGPara);
  918. begin
  919. case l.loc of
  920. LOC_REGISTER,
  921. LOC_CREGISTER :
  922. a_load_reg_cgpara(list,l.size,l.register,cgpara);
  923. LOC_CONSTANT :
  924. a_load_const_cgpara(list,l.size,l.value,cgpara);
  925. LOC_CREFERENCE,
  926. LOC_REFERENCE :
  927. a_load_ref_cgpara(list,l.size,l.reference,cgpara);
  928. else
  929. internalerror(2002032211);
  930. end;
  931. end;
  932. procedure tcg.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : TCGPara);
  933. var
  934. hr : tregister;
  935. begin
  936. cgpara.check_simple_location;
  937. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  938. begin
  939. paramanager.allocparaloc(list,cgpara.location);
  940. a_loadaddr_ref_reg(list,r,cgpara.location^.register)
  941. end
  942. else
  943. begin
  944. hr:=getaddressregister(list);
  945. a_loadaddr_ref_reg(list,r,hr);
  946. a_load_reg_cgpara(list,OS_ADDR,hr,cgpara);
  947. end;
  948. end;
  949. procedure tcg.a_load_cgparaloc_ref(list : TAsmList;const paraloc : TCGParaLocation;const ref : treference;sizeleft : tcgint;align : longint);
  950. var
  951. href : treference;
  952. hreg : tregister;
  953. cgsize: tcgsize;
  954. begin
  955. case paraloc.loc of
  956. LOC_REGISTER :
  957. begin
  958. hreg:=paraloc.register;
  959. cgsize:=paraloc.size;
  960. if paraloc.shiftval>0 then
  961. a_op_const_reg_reg(list,OP_SHL,OS_INT,paraloc.shiftval,paraloc.register,paraloc.register)
  962. { in case the original size was 3 or 5/6/7 bytes, the value was
  963. shifted to the top of the to 4 resp. 8 byte register on the
  964. caller side and needs to be stored with those bytes at the
  965. start of the reference -> don't shift right }
  966. else if (paraloc.shiftval<0) and
  967. ((-paraloc.shiftval) in [1,2,4]) then
  968. begin
  969. a_op_const_reg_reg(list,OP_SHR,OS_INT,-paraloc.shiftval,paraloc.register,paraloc.register);
  970. { convert to a register of 1/2/4 bytes in size, since the
  971. original register had to be made larger to be able to hold
  972. the shifted value }
  973. cgsize:=int_cgsize(tcgsize2size[OS_INT]-(-paraloc.shiftval div 8));
  974. hreg:=getintregister(list,cgsize);
  975. a_load_reg_reg(list,OS_INT,cgsize,paraloc.register,hreg);
  976. end;
  977. a_load_reg_ref(list,paraloc.size,cgsize,hreg,ref);
  978. end;
  979. LOC_MMREGISTER :
  980. begin
  981. case paraloc.size of
  982. OS_F32,
  983. OS_F64,
  984. OS_F128:
  985. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  986. OS_M8..OS_M128,
  987. OS_MS8..OS_MS128:
  988. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,nil);
  989. else
  990. internalerror(2010053102);
  991. end;
  992. end;
  993. LOC_FPUREGISTER :
  994. a_loadfpu_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  995. LOC_REFERENCE :
  996. begin
  997. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,align);
  998. { use concatcopy, because it can also be a float which fails when
  999. load_ref_ref is used. Don't copy data when the references are equal }
  1000. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1001. g_concatcopy(list,href,ref,sizeleft);
  1002. end;
  1003. else
  1004. internalerror(2002081302);
  1005. end;
  1006. end;
  1007. procedure tcg.a_load_cgparaloc_anyreg(list: TAsmList;regsize: tcgsize;const paraloc: TCGParaLocation;reg: tregister;align: longint);
  1008. var
  1009. href : treference;
  1010. begin
  1011. case paraloc.loc of
  1012. LOC_REGISTER :
  1013. begin
  1014. if paraloc.shiftval<0 then
  1015. a_op_const_reg_reg(list,OP_SHR,OS_INT,-paraloc.shiftval,paraloc.register,paraloc.register);
  1016. case getregtype(reg) of
  1017. R_ADDRESSREGISTER,
  1018. R_INTREGISTER:
  1019. a_load_reg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1020. R_MMREGISTER:
  1021. a_loadmm_intreg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1022. else
  1023. internalerror(2009112422);
  1024. end;
  1025. end;
  1026. LOC_MMREGISTER :
  1027. begin
  1028. case getregtype(reg) of
  1029. R_ADDRESSREGISTER,
  1030. R_INTREGISTER:
  1031. a_loadmm_reg_intreg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1032. R_MMREGISTER:
  1033. begin
  1034. case paraloc.size of
  1035. OS_F32,
  1036. OS_F64,
  1037. OS_F128:
  1038. a_loadmm_reg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1039. OS_M8..OS_M128,
  1040. OS_MS8..OS_MS128:
  1041. a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,nil);
  1042. else
  1043. internalerror(2010053102);
  1044. end;
  1045. end;
  1046. else
  1047. internalerror(2010053104);
  1048. end;
  1049. end;
  1050. LOC_FPUREGISTER :
  1051. a_loadfpu_reg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1052. LOC_REFERENCE :
  1053. begin
  1054. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,align);
  1055. case getregtype(reg) of
  1056. R_ADDRESSREGISTER,
  1057. R_INTREGISTER :
  1058. a_load_ref_reg(list,paraloc.size,regsize,href,reg);
  1059. R_FPUREGISTER :
  1060. a_loadfpu_ref_reg(list,paraloc.size,regsize,href,reg);
  1061. R_MMREGISTER :
  1062. { not paraloc.size, because it may be OS_64 instead of
  1063. OS_F64 in case the parameter is passed using integer
  1064. conventions (e.g., on ARM) }
  1065. a_loadmm_ref_reg(list,regsize,regsize,href,reg,mms_movescalar);
  1066. else
  1067. internalerror(2004101012);
  1068. end;
  1069. end;
  1070. else
  1071. internalerror(2002081302);
  1072. end;
  1073. end;
  1074. {****************************************************************************
  1075. some generic implementations
  1076. ****************************************************************************}
  1077. { memory/register loading }
  1078. procedure tcg.a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  1079. var
  1080. tmpref : treference;
  1081. tmpreg : tregister;
  1082. i : longint;
  1083. begin
  1084. if ref.alignment<tcgsize2size[fromsize] then
  1085. begin
  1086. tmpref:=ref;
  1087. { we take care of the alignment now }
  1088. tmpref.alignment:=0;
  1089. case FromSize of
  1090. OS_16,OS_S16:
  1091. begin
  1092. tmpreg:=getintregister(list,OS_16);
  1093. a_load_reg_reg(list,fromsize,OS_16,register,tmpreg);
  1094. if target_info.endian=endian_big then
  1095. inc(tmpref.offset);
  1096. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1097. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1098. tmpreg:=makeregsize(list,tmpreg,OS_16);
  1099. a_op_const_reg(list,OP_SHR,OS_16,8,tmpreg);
  1100. if target_info.endian=endian_big then
  1101. dec(tmpref.offset)
  1102. else
  1103. inc(tmpref.offset);
  1104. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1105. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1106. end;
  1107. OS_32,OS_S32:
  1108. begin
  1109. { could add an optimised case for ref.alignment=2 }
  1110. tmpreg:=getintregister(list,OS_32);
  1111. a_load_reg_reg(list,fromsize,OS_32,register,tmpreg);
  1112. if target_info.endian=endian_big then
  1113. inc(tmpref.offset,3);
  1114. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1115. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1116. tmpreg:=makeregsize(list,tmpreg,OS_32);
  1117. for i:=1 to 3 do
  1118. begin
  1119. a_op_const_reg(list,OP_SHR,OS_32,8,tmpreg);
  1120. if target_info.endian=endian_big then
  1121. dec(tmpref.offset)
  1122. else
  1123. inc(tmpref.offset);
  1124. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1125. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1126. tmpreg:=makeregsize(list,tmpreg,OS_32);
  1127. end;
  1128. end
  1129. else
  1130. a_load_reg_ref(list,fromsize,tosize,register,tmpref);
  1131. end;
  1132. end
  1133. else
  1134. a_load_reg_ref(list,fromsize,tosize,register,ref);
  1135. end;
  1136. procedure tcg.a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  1137. var
  1138. tmpref : treference;
  1139. tmpreg,
  1140. tmpreg2 : tregister;
  1141. i : longint;
  1142. hisize : tcgsize;
  1143. begin
  1144. if ref.alignment in [1,2] then
  1145. begin
  1146. tmpref:=ref;
  1147. { we take care of the alignment now }
  1148. tmpref.alignment:=0;
  1149. case FromSize of
  1150. OS_16,OS_S16:
  1151. if ref.alignment=2 then
  1152. a_load_ref_reg(list,fromsize,tosize,tmpref,register)
  1153. else
  1154. begin
  1155. if FromSize=OS_16 then
  1156. hisize:=OS_8
  1157. else
  1158. hisize:=OS_S8;
  1159. { first load in tmpreg, because the target register }
  1160. { may be used in ref as well }
  1161. if target_info.endian=endian_little then
  1162. inc(tmpref.offset);
  1163. tmpreg:=getintregister(list,OS_8);
  1164. a_load_ref_reg(list,hisize,hisize,tmpref,tmpreg);
  1165. tmpreg:=makeregsize(list,tmpreg,FromSize);
  1166. a_op_const_reg(list,OP_SHL,FromSize,8,tmpreg);
  1167. if target_info.endian=endian_little then
  1168. dec(tmpref.offset)
  1169. else
  1170. inc(tmpref.offset);
  1171. tmpreg2:=makeregsize(list,register,OS_16);
  1172. a_load_ref_reg(list,OS_8,OS_16,tmpref,tmpreg2);
  1173. a_op_reg_reg(list,OP_OR,OS_16,tmpreg,tmpreg2);
  1174. a_load_reg_reg(list,fromsize,tosize,tmpreg2,register);
  1175. end;
  1176. OS_32,OS_S32:
  1177. if ref.alignment=2 then
  1178. begin
  1179. if target_info.endian=endian_little then
  1180. inc(tmpref.offset,2);
  1181. tmpreg:=getintregister(list,OS_32);
  1182. a_load_ref_reg(list,OS_16,OS_32,tmpref,tmpreg);
  1183. a_op_const_reg(list,OP_SHL,OS_32,16,tmpreg);
  1184. if target_info.endian=endian_little then
  1185. dec(tmpref.offset,2)
  1186. else
  1187. inc(tmpref.offset,2);
  1188. tmpreg2:=makeregsize(list,register,OS_32);
  1189. a_load_ref_reg(list,OS_16,OS_32,tmpref,tmpreg2);
  1190. a_op_reg_reg(list,OP_OR,OS_32,tmpreg,tmpreg2);
  1191. a_load_reg_reg(list,fromsize,tosize,tmpreg2,register);
  1192. end
  1193. else
  1194. begin
  1195. if target_info.endian=endian_little then
  1196. inc(tmpref.offset,3);
  1197. tmpreg:=getintregister(list,OS_32);
  1198. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg);
  1199. tmpreg2:=getintregister(list,OS_32);
  1200. for i:=1 to 3 do
  1201. begin
  1202. a_op_const_reg(list,OP_SHL,OS_32,8,tmpreg);
  1203. if target_info.endian=endian_little then
  1204. dec(tmpref.offset)
  1205. else
  1206. inc(tmpref.offset);
  1207. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg2);
  1208. a_op_reg_reg(list,OP_OR,OS_32,tmpreg2,tmpreg);
  1209. end;
  1210. a_load_reg_reg(list,fromsize,tosize,tmpreg,register);
  1211. end
  1212. else
  1213. a_load_ref_reg(list,fromsize,tosize,tmpref,register);
  1214. end;
  1215. end
  1216. else
  1217. a_load_ref_reg(list,fromsize,tosize,ref,register);
  1218. end;
  1219. procedure tcg.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  1220. var
  1221. tmpreg: tregister;
  1222. begin
  1223. { verify if we have the same reference }
  1224. if references_equal(sref,dref) then
  1225. exit;
  1226. tmpreg:=getintregister(list,tosize);
  1227. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  1228. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  1229. end;
  1230. procedure tcg.a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : treference);
  1231. var
  1232. tmpreg: tregister;
  1233. begin
  1234. tmpreg:=getintregister(list,size);
  1235. a_load_const_reg(list,size,a,tmpreg);
  1236. a_load_reg_ref(list,size,size,tmpreg,ref);
  1237. end;
  1238. procedure tcg.a_load_const_loc(list : TAsmList;a : tcgint;const loc: tlocation);
  1239. begin
  1240. case loc.loc of
  1241. LOC_REFERENCE,LOC_CREFERENCE:
  1242. a_load_const_ref(list,loc.size,a,loc.reference);
  1243. LOC_REGISTER,LOC_CREGISTER:
  1244. a_load_const_reg(list,loc.size,a,loc.register);
  1245. else
  1246. internalerror(200203272);
  1247. end;
  1248. end;
  1249. procedure tcg.a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  1250. begin
  1251. case loc.loc of
  1252. LOC_REFERENCE,LOC_CREFERENCE:
  1253. a_load_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  1254. LOC_REGISTER,LOC_CREGISTER:
  1255. a_load_reg_reg(list,fromsize,loc.size,reg,loc.register);
  1256. LOC_MMREGISTER,LOC_CMMREGISTER:
  1257. a_loadmm_intreg_reg(list,fromsize,loc.size,reg,loc.register,mms_movescalar);
  1258. else
  1259. internalerror(200203271);
  1260. end;
  1261. end;
  1262. procedure tcg.a_load_loc_reg(list : TAsmList; tosize: tcgsize; const loc: tlocation; reg : tregister);
  1263. begin
  1264. case loc.loc of
  1265. LOC_REFERENCE,LOC_CREFERENCE:
  1266. a_load_ref_reg(list,loc.size,tosize,loc.reference,reg);
  1267. LOC_REGISTER,LOC_CREGISTER:
  1268. a_load_reg_reg(list,loc.size,tosize,loc.register,reg);
  1269. LOC_CONSTANT:
  1270. a_load_const_reg(list,tosize,loc.value,reg);
  1271. else
  1272. internalerror(200109092);
  1273. end;
  1274. end;
  1275. procedure tcg.a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  1276. begin
  1277. case loc.loc of
  1278. LOC_REFERENCE,LOC_CREFERENCE:
  1279. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  1280. LOC_REGISTER,LOC_CREGISTER:
  1281. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  1282. LOC_CONSTANT:
  1283. a_load_const_ref(list,tosize,loc.value,ref);
  1284. else
  1285. internalerror(200109302);
  1286. end;
  1287. end;
  1288. procedure tcg.optimize_op_const(size: TCGSize; var op: topcg; var a : tcgint);
  1289. var
  1290. powerval : longint;
  1291. signext_a, zeroext_a: tcgint;
  1292. begin
  1293. case size of
  1294. OS_64,OS_S64:
  1295. begin
  1296. signext_a:=int64(a);
  1297. zeroext_a:=int64(a);
  1298. end;
  1299. OS_32,OS_S32:
  1300. begin
  1301. signext_a:=longint(a);
  1302. zeroext_a:=dword(a);
  1303. end;
  1304. OS_16,OS_S16:
  1305. begin
  1306. signext_a:=smallint(a);
  1307. zeroext_a:=word(a);
  1308. end;
  1309. OS_8,OS_S8:
  1310. begin
  1311. signext_a:=shortint(a);
  1312. zeroext_a:=byte(a);
  1313. end
  1314. else
  1315. begin
  1316. { Should we internalerror() here instead? }
  1317. signext_a:=a;
  1318. zeroext_a:=a;
  1319. end;
  1320. end;
  1321. case op of
  1322. OP_OR :
  1323. begin
  1324. { or with zero returns same result }
  1325. if a = 0 then
  1326. op:=OP_NONE
  1327. else
  1328. { or with max returns max }
  1329. if signext_a = -1 then
  1330. op:=OP_MOVE;
  1331. end;
  1332. OP_AND :
  1333. begin
  1334. { and with max returns same result }
  1335. if (signext_a = -1) then
  1336. op:=OP_NONE
  1337. else
  1338. { and with 0 returns 0 }
  1339. if a=0 then
  1340. op:=OP_MOVE;
  1341. end;
  1342. OP_DIV :
  1343. begin
  1344. { division by 1 returns result }
  1345. if a = 1 then
  1346. op:=OP_NONE
  1347. else if ispowerof2(int64(zeroext_a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  1348. begin
  1349. a := powerval;
  1350. op:= OP_SHR;
  1351. end;
  1352. end;
  1353. OP_IDIV:
  1354. begin
  1355. if a = 1 then
  1356. op:=OP_NONE;
  1357. end;
  1358. OP_MUL,OP_IMUL:
  1359. begin
  1360. if a = 1 then
  1361. op:=OP_NONE
  1362. else
  1363. if a=0 then
  1364. op:=OP_MOVE
  1365. else if ispowerof2(int64(zeroext_a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  1366. begin
  1367. a := powerval;
  1368. op:= OP_SHL;
  1369. end;
  1370. end;
  1371. OP_ADD,OP_SUB:
  1372. begin
  1373. if a = 0 then
  1374. op:=OP_NONE;
  1375. end;
  1376. OP_SAR,OP_SHL,OP_SHR,OP_ROL,OP_ROR:
  1377. begin
  1378. if a = 0 then
  1379. op:=OP_NONE;
  1380. end;
  1381. end;
  1382. end;
  1383. procedure tcg.a_loadfpu_loc_reg(list: TAsmList; tosize: tcgsize; const loc: tlocation; const reg: tregister);
  1384. begin
  1385. case loc.loc of
  1386. LOC_REFERENCE, LOC_CREFERENCE:
  1387. a_loadfpu_ref_reg(list,loc.size,tosize,loc.reference,reg);
  1388. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1389. a_loadfpu_reg_reg(list,loc.size,tosize,loc.register,reg);
  1390. else
  1391. internalerror(200203301);
  1392. end;
  1393. end;
  1394. procedure tcg.a_loadfpu_reg_loc(list: TAsmList; fromsize: tcgsize; const reg: tregister; const loc: tlocation);
  1395. begin
  1396. case loc.loc of
  1397. LOC_REFERENCE, LOC_CREFERENCE:
  1398. a_loadfpu_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  1399. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1400. a_loadfpu_reg_reg(list,fromsize,loc.size,reg,loc.register);
  1401. else
  1402. internalerror(48991);
  1403. end;
  1404. end;
  1405. procedure tcg.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tcgsize; const ref1,ref2: treference);
  1406. var
  1407. reg: tregister;
  1408. regsize: tcgsize;
  1409. begin
  1410. if (fromsize>=tosize) then
  1411. regsize:=fromsize
  1412. else
  1413. regsize:=tosize;
  1414. reg:=getfpuregister(list,regsize);
  1415. a_loadfpu_ref_reg(list,fromsize,regsize,ref1,reg);
  1416. a_loadfpu_reg_ref(list,regsize,tosize,reg,ref2);
  1417. end;
  1418. procedure tcg.a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);
  1419. var
  1420. ref : treference;
  1421. begin
  1422. paramanager.alloccgpara(list,cgpara);
  1423. case cgpara.location^.loc of
  1424. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1425. begin
  1426. cgpara.check_simple_location;
  1427. a_loadfpu_reg_reg(list,size,size,r,cgpara.location^.register);
  1428. end;
  1429. LOC_REFERENCE,LOC_CREFERENCE:
  1430. begin
  1431. cgpara.check_simple_location;
  1432. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1433. a_loadfpu_reg_ref(list,size,size,r,ref);
  1434. end;
  1435. LOC_REGISTER,LOC_CREGISTER:
  1436. begin
  1437. { paramfpu_ref does the check_simpe_location check here if necessary }
  1438. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,ref);
  1439. a_loadfpu_reg_ref(list,size,size,r,ref);
  1440. a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  1441. tg.Ungettemp(list,ref);
  1442. end;
  1443. else
  1444. internalerror(2010053112);
  1445. end;
  1446. end;
  1447. procedure tcg.a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);
  1448. var
  1449. href : treference;
  1450. hsize: tcgsize;
  1451. begin
  1452. case cgpara.location^.loc of
  1453. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1454. begin
  1455. cgpara.check_simple_location;
  1456. paramanager.alloccgpara(list,cgpara);
  1457. a_loadfpu_ref_reg(list,size,size,ref,cgpara.location^.register);
  1458. end;
  1459. LOC_REFERENCE,LOC_CREFERENCE:
  1460. begin
  1461. cgpara.check_simple_location;
  1462. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1463. { concatcopy should choose the best way to copy the data }
  1464. g_concatcopy(list,ref,href,tcgsize2size[size]);
  1465. end;
  1466. LOC_REGISTER,LOC_CREGISTER:
  1467. begin
  1468. { force integer size }
  1469. hsize:=int_cgsize(tcgsize2size[size]);
  1470. {$ifndef cpu64bitalu}
  1471. if (hsize in [OS_S64,OS_64]) then
  1472. cg64.a_load64_ref_cgpara(list,ref,cgpara)
  1473. else
  1474. {$endif not cpu64bitalu}
  1475. begin
  1476. cgpara.check_simple_location;
  1477. a_load_ref_cgpara(list,hsize,ref,cgpara)
  1478. end;
  1479. end
  1480. else
  1481. internalerror(200402201);
  1482. end;
  1483. end;
  1484. procedure tcg.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1485. var
  1486. tmpreg : tregister;
  1487. begin
  1488. tmpreg:=getintregister(list,size);
  1489. a_load_ref_reg(list,size,size,ref,tmpreg);
  1490. a_op_const_reg(list,op,size,a,tmpreg);
  1491. a_load_reg_ref(list,size,size,tmpreg,ref);
  1492. end;
  1493. procedure tcg.a_op_const_loc(list : TAsmList; Op: TOpCG; a: tcgint; const loc: tlocation);
  1494. begin
  1495. case loc.loc of
  1496. LOC_REGISTER, LOC_CREGISTER:
  1497. a_op_const_reg(list,op,loc.size,a,loc.register);
  1498. LOC_REFERENCE, LOC_CREFERENCE:
  1499. a_op_const_ref(list,op,loc.size,a,loc.reference);
  1500. else
  1501. internalerror(200109061);
  1502. end;
  1503. end;
  1504. procedure tcg.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1505. var
  1506. tmpreg : tregister;
  1507. begin
  1508. tmpreg:=getintregister(list,size);
  1509. a_load_ref_reg(list,size,size,ref,tmpreg);
  1510. a_op_reg_reg(list,op,size,reg,tmpreg);
  1511. a_load_reg_ref(list,size,size,tmpreg,ref);
  1512. end;
  1513. procedure tcg.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1514. var
  1515. tmpreg: tregister;
  1516. begin
  1517. case op of
  1518. OP_NOT,OP_NEG:
  1519. { handle it as "load ref,reg; op reg" }
  1520. begin
  1521. a_load_ref_reg(list,size,size,ref,reg);
  1522. a_op_reg_reg(list,op,size,reg,reg);
  1523. end;
  1524. else
  1525. begin
  1526. tmpreg:=getintregister(list,size);
  1527. a_load_ref_reg(list,size,size,ref,tmpreg);
  1528. a_op_reg_reg(list,op,size,tmpreg,reg);
  1529. end;
  1530. end;
  1531. end;
  1532. procedure tcg.a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  1533. begin
  1534. case loc.loc of
  1535. LOC_REGISTER, LOC_CREGISTER:
  1536. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  1537. LOC_REFERENCE, LOC_CREFERENCE:
  1538. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  1539. else
  1540. internalerror(200109061);
  1541. end;
  1542. end;
  1543. procedure tcg.a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  1544. var
  1545. tmpreg: tregister;
  1546. begin
  1547. case loc.loc of
  1548. LOC_REGISTER,LOC_CREGISTER:
  1549. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  1550. LOC_REFERENCE,LOC_CREFERENCE:
  1551. begin
  1552. tmpreg:=getintregister(list,loc.size);
  1553. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  1554. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  1555. end;
  1556. else
  1557. internalerror(200109061);
  1558. end;
  1559. end;
  1560. procedure Tcg.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1561. a:tcgint;src,dst:Tregister);
  1562. begin
  1563. a_load_reg_reg(list,size,size,src,dst);
  1564. a_op_const_reg(list,op,size,a,dst);
  1565. end;
  1566. procedure tcg.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1567. size: tcgsize; src1, src2, dst: tregister);
  1568. var
  1569. tmpreg: tregister;
  1570. begin
  1571. if (dst<>src1) then
  1572. begin
  1573. a_load_reg_reg(list,size,size,src2,dst);
  1574. a_op_reg_reg(list,op,size,src1,dst);
  1575. end
  1576. else
  1577. begin
  1578. { can we do a direct operation on the target register ? }
  1579. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  1580. a_op_reg_reg(list,op,size,src2,dst)
  1581. else
  1582. begin
  1583. tmpreg:=getintregister(list,size);
  1584. a_load_reg_reg(list,size,size,src2,tmpreg);
  1585. a_op_reg_reg(list,op,size,src1,tmpreg);
  1586. a_load_reg_reg(list,size,size,tmpreg,dst);
  1587. end;
  1588. end;
  1589. end;
  1590. procedure tcg.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1591. begin
  1592. a_op_const_reg_reg(list,op,size,a,src,dst);
  1593. ovloc.loc:=LOC_VOID;
  1594. end;
  1595. procedure tcg.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1596. begin
  1597. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1598. ovloc.loc:=LOC_VOID;
  1599. end;
  1600. procedure tcg.a_cmp_const_reg_label(list: TAsmList; size: tcgsize;
  1601. cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1602. var
  1603. tmpreg: tregister;
  1604. begin
  1605. tmpreg:=getintregister(list,size);
  1606. a_load_const_reg(list,size,a,tmpreg);
  1607. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1608. end;
  1609. procedure tcg.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  1610. l : tasmlabel);
  1611. var
  1612. tmpreg: tregister;
  1613. begin
  1614. tmpreg:=getintregister(list,size);
  1615. a_load_ref_reg(list,size,size,ref,tmpreg);
  1616. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1617. end;
  1618. procedure tcg.a_cmp_const_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const loc : tlocation;
  1619. l : tasmlabel);
  1620. begin
  1621. case loc.loc of
  1622. LOC_REGISTER,LOC_CREGISTER:
  1623. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1624. LOC_REFERENCE,LOC_CREFERENCE:
  1625. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1626. else
  1627. internalerror(200109061);
  1628. end;
  1629. end;
  1630. procedure tcg.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  1631. var
  1632. tmpreg: tregister;
  1633. begin
  1634. tmpreg:=getintregister(list,size);
  1635. a_load_ref_reg(list,size,size,ref,tmpreg);
  1636. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1637. end;
  1638. procedure tcg.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg : tregister; const ref: treference; l : tasmlabel);
  1639. var
  1640. tmpreg: tregister;
  1641. begin
  1642. tmpreg:=getintregister(list,size);
  1643. a_load_ref_reg(list,size,size,ref,tmpreg);
  1644. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  1645. end;
  1646. procedure tcg.a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  1647. begin
  1648. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  1649. end;
  1650. procedure tcg.a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  1651. begin
  1652. case loc.loc of
  1653. LOC_REGISTER,
  1654. LOC_CREGISTER:
  1655. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1656. LOC_REFERENCE,
  1657. LOC_CREFERENCE :
  1658. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1659. LOC_CONSTANT:
  1660. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1661. else
  1662. internalerror(200203231);
  1663. end;
  1664. end;
  1665. procedure tcg.a_cmp_ref_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1666. l : tasmlabel);
  1667. var
  1668. tmpreg: tregister;
  1669. begin
  1670. case loc.loc of
  1671. LOC_REGISTER,LOC_CREGISTER:
  1672. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1673. LOC_REFERENCE,LOC_CREFERENCE:
  1674. begin
  1675. tmpreg:=getintregister(list,size);
  1676. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1677. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1678. end;
  1679. else
  1680. internalerror(200109061);
  1681. end;
  1682. end;
  1683. procedure tcg.a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  1684. begin
  1685. case loc.loc of
  1686. LOC_MMREGISTER,LOC_CMMREGISTER:
  1687. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1688. LOC_REFERENCE,LOC_CREFERENCE:
  1689. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  1690. LOC_REGISTER,LOC_CREGISTER:
  1691. a_loadmm_intreg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1692. else
  1693. internalerror(200310121);
  1694. end;
  1695. end;
  1696. procedure tcg.a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  1697. begin
  1698. case loc.loc of
  1699. LOC_MMREGISTER,LOC_CMMREGISTER:
  1700. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  1701. LOC_REFERENCE,LOC_CREFERENCE:
  1702. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  1703. else
  1704. internalerror(200310122);
  1705. end;
  1706. end;
  1707. procedure tcg.a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle);
  1708. var
  1709. href : treference;
  1710. {$ifndef cpu64bitalu}
  1711. tmpreg : tregister;
  1712. reg64 : tregister64;
  1713. {$endif not cpu64bitalu}
  1714. begin
  1715. {$ifndef cpu64bitalu}
  1716. if not(cgpara.location^.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  1717. (size<>OS_F64) then
  1718. {$endif not cpu64bitalu}
  1719. cgpara.check_simple_location;
  1720. paramanager.alloccgpara(list,cgpara);
  1721. case cgpara.location^.loc of
  1722. LOC_MMREGISTER,LOC_CMMREGISTER:
  1723. a_loadmm_reg_reg(list,size,cgpara.location^.size,reg,cgpara.location^.register,shuffle);
  1724. LOC_REFERENCE,LOC_CREFERENCE:
  1725. begin
  1726. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1727. a_loadmm_reg_ref(list,size,cgpara.location^.size,reg,href,shuffle);
  1728. end;
  1729. LOC_REGISTER,LOC_CREGISTER:
  1730. begin
  1731. if assigned(shuffle) and
  1732. not shufflescalar(shuffle) then
  1733. internalerror(2009112510);
  1734. {$ifndef cpu64bitalu}
  1735. if (size=OS_F64) then
  1736. begin
  1737. if not assigned(cgpara.location^.next) or
  1738. assigned(cgpara.location^.next^.next) then
  1739. internalerror(2009112512);
  1740. case cgpara.location^.next^.loc of
  1741. LOC_REGISTER,LOC_CREGISTER:
  1742. tmpreg:=cgpara.location^.next^.register;
  1743. LOC_REFERENCE,LOC_CREFERENCE:
  1744. tmpreg:=getintregister(list,OS_32);
  1745. else
  1746. internalerror(2009112910);
  1747. end;
  1748. if (target_info.endian=ENDIAN_BIG) then
  1749. begin
  1750. { paraloc^ -> high
  1751. paraloc^.next -> low }
  1752. reg64.reghi:=cgpara.location^.register;
  1753. reg64.reglo:=tmpreg;
  1754. end
  1755. else
  1756. begin
  1757. { paraloc^ -> low
  1758. paraloc^.next -> high }
  1759. reg64.reglo:=cgpara.location^.register;
  1760. reg64.reghi:=tmpreg;
  1761. end;
  1762. cg64.a_loadmm_reg_intreg64(list,size,reg,reg64);
  1763. if (cgpara.location^.next^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1764. begin
  1765. if not(cgpara.location^.next^.size in [OS_32,OS_S32]) then
  1766. internalerror(2009112911);
  1767. reference_reset_base(href,cgpara.location^.next^.reference.index,cgpara.location^.next^.reference.offset,cgpara.alignment);
  1768. a_load_reg_ref(list,OS_32,cgpara.location^.next^.size,tmpreg,href);
  1769. end;
  1770. end
  1771. else
  1772. {$endif not cpu64bitalu}
  1773. a_loadmm_reg_intreg(list,size,cgpara.location^.size,reg,cgpara.location^.register,mms_movescalar);
  1774. end
  1775. else
  1776. internalerror(200310123);
  1777. end;
  1778. end;
  1779. procedure tcg.a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize;const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle);
  1780. var
  1781. hr : tregister;
  1782. hs : tmmshuffle;
  1783. begin
  1784. cgpara.check_simple_location;
  1785. hr:=getmmregister(list,cgpara.location^.size);
  1786. a_loadmm_ref_reg(list,size,cgpara.location^.size,ref,hr,shuffle);
  1787. if realshuffle(shuffle) then
  1788. begin
  1789. hs:=shuffle^;
  1790. removeshuffles(hs);
  1791. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,@hs);
  1792. end
  1793. else
  1794. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,shuffle);
  1795. end;
  1796. procedure tcg.a_loadmm_loc_cgpara(list: TAsmList;const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle);
  1797. begin
  1798. case loc.loc of
  1799. LOC_MMREGISTER,LOC_CMMREGISTER:
  1800. a_loadmm_reg_cgpara(list,loc.size,loc.register,cgpara,shuffle);
  1801. LOC_REFERENCE,LOC_CREFERENCE:
  1802. a_loadmm_ref_cgpara(list,loc.size,loc.reference,cgpara,shuffle);
  1803. else
  1804. internalerror(200310123);
  1805. end;
  1806. end;
  1807. procedure tcg.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1808. var
  1809. hr : tregister;
  1810. hs : tmmshuffle;
  1811. begin
  1812. hr:=getmmregister(list,size);
  1813. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1814. if realshuffle(shuffle) then
  1815. begin
  1816. hs:=shuffle^;
  1817. removeshuffles(hs);
  1818. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  1819. end
  1820. else
  1821. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  1822. end;
  1823. procedure tcg.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  1824. var
  1825. hr : tregister;
  1826. hs : tmmshuffle;
  1827. begin
  1828. hr:=getmmregister(list,size);
  1829. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1830. if realshuffle(shuffle) then
  1831. begin
  1832. hs:=shuffle^;
  1833. removeshuffles(hs);
  1834. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  1835. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  1836. end
  1837. else
  1838. begin
  1839. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  1840. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  1841. end;
  1842. end;
  1843. procedure tcg.a_loadmm_intreg_reg(list: tasmlist; fromsize,tosize: tcgsize; intreg,mmreg: tregister; shuffle: pmmshuffle);
  1844. var
  1845. tmpref: treference;
  1846. begin
  1847. if (tcgsize2size[fromsize]<>4) or
  1848. (tcgsize2size[tosize]<>4) then
  1849. internalerror(2009112503);
  1850. tg.gettemp(list,4,4,tt_normal,tmpref);
  1851. a_load_reg_ref(list,fromsize,fromsize,intreg,tmpref);
  1852. a_loadmm_ref_reg(list,tosize,tosize,tmpref,mmreg,shuffle);
  1853. tg.ungettemp(list,tmpref);
  1854. end;
  1855. procedure tcg.a_loadmm_reg_intreg(list: tasmlist; fromsize,tosize: tcgsize; mmreg,intreg: tregister; shuffle: pmmshuffle);
  1856. var
  1857. tmpref: treference;
  1858. begin
  1859. if (tcgsize2size[fromsize]<>4) or
  1860. (tcgsize2size[tosize]<>4) then
  1861. internalerror(2009112504);
  1862. tg.gettemp(list,8,8,tt_normal,tmpref);
  1863. a_loadmm_reg_ref(list,fromsize,fromsize,mmreg,tmpref,shuffle);
  1864. a_load_ref_reg(list,tosize,tosize,tmpref,intreg);
  1865. tg.ungettemp(list,tmpref);
  1866. end;
  1867. procedure tcg.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  1868. begin
  1869. case loc.loc of
  1870. LOC_CMMREGISTER,LOC_MMREGISTER:
  1871. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  1872. LOC_CREFERENCE,LOC_REFERENCE:
  1873. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  1874. else
  1875. internalerror(200312232);
  1876. end;
  1877. end;
  1878. procedure tcg.a_opmm_loc_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; src,dst: tregister;shuffle : pmmshuffle);
  1879. begin
  1880. case loc.loc of
  1881. LOC_CMMREGISTER,LOC_MMREGISTER:
  1882. a_opmm_reg_reg_reg(list,op,size,loc.register,src,dst,shuffle);
  1883. LOC_CREFERENCE,LOC_REFERENCE:
  1884. a_opmm_ref_reg_reg(list,op,size,loc.reference,src,dst,shuffle);
  1885. else
  1886. internalerror(200312232);
  1887. end;
  1888. end;
  1889. procedure tcg.a_opmm_reg_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;
  1890. src1,src2,dst : tregister;shuffle : pmmshuffle);
  1891. begin
  1892. internalerror(2013061102);
  1893. end;
  1894. procedure tcg.a_opmm_ref_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;
  1895. const ref : treference;src,dst : tregister;shuffle : pmmshuffle);
  1896. begin
  1897. internalerror(2013061101);
  1898. end;
  1899. procedure tcg.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  1900. begin
  1901. g_concatcopy(list,source,dest,len);
  1902. end;
  1903. procedure tcg.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  1904. begin
  1905. g_overflowCheck(list,loc,def);
  1906. end;
  1907. {$ifdef cpuflags}
  1908. procedure tcg.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference);
  1909. var
  1910. tmpreg : tregister;
  1911. begin
  1912. tmpreg:=getintregister(list,size);
  1913. g_flags2reg(list,size,f,tmpreg);
  1914. a_load_reg_ref(list,size,size,tmpreg,ref);
  1915. end;
  1916. {$endif cpuflags}
  1917. procedure tcg.g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  1918. var
  1919. hrefvmt : treference;
  1920. cgpara1,cgpara2 : TCGPara;
  1921. pd: tprocdef;
  1922. begin
  1923. cgpara1.init;
  1924. cgpara2.init;
  1925. if (cs_check_object in current_settings.localswitches) then
  1926. begin
  1927. pd:=search_system_proc('fpc_check_object_ext');
  1928. paramanager.getintparaloc(pd,1,cgpara1);
  1929. paramanager.getintparaloc(pd,2,cgpara2);
  1930. reference_reset_symbol(hrefvmt,current_asmdata.RefAsmSymbol(objdef.vmt_mangledname,AT_DATA),0,sizeof(pint));
  1931. if pd.is_pushleftright then
  1932. begin
  1933. a_load_reg_cgpara(list,OS_ADDR,reg,cgpara1);
  1934. a_loadaddr_ref_cgpara(list,hrefvmt,cgpara2);
  1935. end
  1936. else
  1937. begin
  1938. a_loadaddr_ref_cgpara(list,hrefvmt,cgpara2);
  1939. a_load_reg_cgpara(list,OS_ADDR,reg,cgpara1);
  1940. end;
  1941. paramanager.freecgpara(list,cgpara1);
  1942. paramanager.freecgpara(list,cgpara2);
  1943. allocallcpuregisters(list);
  1944. a_call_name(list,'fpc_check_object_ext',false);
  1945. deallocallcpuregisters(list);
  1946. end
  1947. else
  1948. if (cs_check_range in current_settings.localswitches) then
  1949. begin
  1950. pd:=search_system_proc('fpc_check_object');
  1951. paramanager.getintparaloc(pd,1,cgpara1);
  1952. a_load_reg_cgpara(list,OS_ADDR,reg,cgpara1);
  1953. paramanager.freecgpara(list,cgpara1);
  1954. allocallcpuregisters(list);
  1955. a_call_name(list,'fpc_check_object',false);
  1956. deallocallcpuregisters(list);
  1957. end;
  1958. cgpara1.done;
  1959. cgpara2.done;
  1960. end;
  1961. {*****************************************************************************
  1962. Entry/Exit Code Functions
  1963. *****************************************************************************}
  1964. procedure tcg.g_save_registers(list:TAsmList);
  1965. var
  1966. href : treference;
  1967. size : longint;
  1968. r : integer;
  1969. begin
  1970. { calculate temp. size }
  1971. size:=0;
  1972. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1973. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1974. inc(size,sizeof(aint));
  1975. if uses_registers(R_ADDRESSREGISTER) then
  1976. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1977. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1978. inc(size,sizeof(aint));
  1979. { mm registers }
  1980. if uses_registers(R_MMREGISTER) then
  1981. begin
  1982. { Make sure we reserve enough space to do the alignment based on the offset
  1983. later on. We can't use the size for this, because the alignment of the start
  1984. of the temp is smaller than needed for an OS_VECTOR }
  1985. inc(size,tcgsize2size[OS_VECTOR]);
  1986. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  1987. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  1988. inc(size,tcgsize2size[OS_VECTOR]);
  1989. end;
  1990. if size>0 then
  1991. begin
  1992. tg.GetTemp(list,size,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  1993. include(current_procinfo.flags,pi_has_saved_regs);
  1994. { Copy registers to temp }
  1995. href:=current_procinfo.save_regs_ref;
  1996. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1997. begin
  1998. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1999. begin
  2000. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE),href);
  2001. inc(href.offset,sizeof(aint));
  2002. end;
  2003. include(rg[R_INTREGISTER].preserved_by_proc,saved_standard_registers[r]);
  2004. end;
  2005. if uses_registers(R_ADDRESSREGISTER) then
  2006. for r:=low(saved_address_registers) to high(saved_address_registers) do
  2007. begin
  2008. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2009. begin
  2010. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE),href);
  2011. inc(href.offset,sizeof(aint));
  2012. end;
  2013. include(rg[R_ADDRESSREGISTER].preserved_by_proc,saved_address_registers[r]);
  2014. end;
  2015. if uses_registers(R_MMREGISTER) then
  2016. begin
  2017. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  2018. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  2019. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  2020. begin
  2021. { the array has to be declared even if no MM registers are saved
  2022. (such as with SSE on i386), and since 0-element arrays don't
  2023. exist, they contain a single RS_INVALID element in that case
  2024. }
  2025. if saved_mm_registers[r]<>RS_INVALID then
  2026. begin
  2027. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  2028. begin
  2029. a_loadmm_reg_ref(list,OS_VECTOR,OS_VECTOR,newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBMMWHOLE),href,nil);
  2030. inc(href.offset,tcgsize2size[OS_VECTOR]);
  2031. end;
  2032. include(rg[R_MMREGISTER].preserved_by_proc,saved_mm_registers[r]);
  2033. end;
  2034. end;
  2035. end;
  2036. end;
  2037. end;
  2038. procedure tcg.g_restore_registers(list:TAsmList);
  2039. var
  2040. href : treference;
  2041. r : integer;
  2042. hreg : tregister;
  2043. begin
  2044. if not(pi_has_saved_regs in current_procinfo.flags) then
  2045. exit;
  2046. { Copy registers from temp }
  2047. href:=current_procinfo.save_regs_ref;
  2048. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  2049. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2050. begin
  2051. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  2052. { Allocate register so the optimizer does not remove the load }
  2053. a_reg_alloc(list,hreg);
  2054. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2055. inc(href.offset,sizeof(aint));
  2056. end;
  2057. if uses_registers(R_ADDRESSREGISTER) then
  2058. for r:=low(saved_address_registers) to high(saved_address_registers) do
  2059. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2060. begin
  2061. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  2062. { Allocate register so the optimizer does not remove the load }
  2063. a_reg_alloc(list,hreg);
  2064. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2065. inc(href.offset,sizeof(aint));
  2066. end;
  2067. if uses_registers(R_MMREGISTER) then
  2068. begin
  2069. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  2070. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  2071. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  2072. begin
  2073. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  2074. begin
  2075. hreg:=newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBMMWHOLE);
  2076. { Allocate register so the optimizer does not remove the load }
  2077. a_reg_alloc(list,hreg);
  2078. a_loadmm_ref_reg(list,OS_VECTOR,OS_VECTOR,href,hreg,nil);
  2079. inc(href.offset,tcgsize2size[OS_VECTOR]);
  2080. end;
  2081. end;
  2082. end;
  2083. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  2084. end;
  2085. procedure tcg.g_profilecode(list : TAsmList);
  2086. begin
  2087. end;
  2088. procedure tcg.g_exception_reason_save(list : TAsmList; const href : treference);
  2089. begin
  2090. a_load_reg_ref(list, OS_INT, OS_INT, NR_FUNCTION_RESULT_REG, href);
  2091. end;
  2092. procedure tcg.g_exception_reason_save_const(list : TAsmList; const href : treference; a: tcgint);
  2093. begin
  2094. a_load_const_ref(list, OS_INT, a, href);
  2095. end;
  2096. procedure tcg.g_exception_reason_load(list : TAsmList; const href : treference);
  2097. begin
  2098. a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  2099. a_load_ref_reg(list, OS_INT, OS_INT, href, NR_FUNCTION_RESULT_REG);
  2100. end;
  2101. procedure tcg.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  2102. var
  2103. hsym : tsym;
  2104. href : treference;
  2105. paraloc : Pcgparalocation;
  2106. begin
  2107. { calculate the parameter info for the procdef }
  2108. procdef.init_paraloc_info(callerside);
  2109. hsym:=tsym(procdef.parast.Find('self'));
  2110. if not(assigned(hsym) and
  2111. (hsym.typ=paravarsym)) then
  2112. internalerror(200305251);
  2113. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  2114. while paraloc<>nil do
  2115. with paraloc^ do
  2116. begin
  2117. case loc of
  2118. LOC_REGISTER:
  2119. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  2120. LOC_REFERENCE:
  2121. begin
  2122. { offset in the wrapper needs to be adjusted for the stored
  2123. return address }
  2124. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),sizeof(pint));
  2125. a_op_const_ref(list,OP_SUB,size,ioffset,href);
  2126. end
  2127. else
  2128. internalerror(200309189);
  2129. end;
  2130. paraloc:=next;
  2131. end;
  2132. end;
  2133. procedure tcg.g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string);
  2134. begin
  2135. a_jmp_name(list,externalname);
  2136. end;
  2137. procedure tcg.a_call_name_static(list : TAsmList;const s : string);
  2138. begin
  2139. a_call_name(list,s,false);
  2140. end;
  2141. function tcg.g_indirect_sym_load(list:TAsmList;const symname: string; const flags: tindsymflags): tregister;
  2142. var
  2143. l: tasmsymbol;
  2144. ref: treference;
  2145. nlsymname: string;
  2146. begin
  2147. result := NR_NO;
  2148. case target_info.system of
  2149. system_powerpc_darwin,
  2150. system_i386_darwin,
  2151. system_i386_iphonesim,
  2152. system_powerpc64_darwin,
  2153. system_arm_darwin:
  2154. begin
  2155. nlsymname:='L'+symname+'$non_lazy_ptr';
  2156. l:=current_asmdata.getasmsymbol(nlsymname);
  2157. if not(assigned(l)) then
  2158. begin
  2159. new_section(current_asmdata.asmlists[al_picdata],sec_data_nonlazy,'',sizeof(pint));
  2160. l:=current_asmdata.DefineAsmSymbol(nlsymname,AB_LOCAL,AT_DATA);
  2161. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create(l,0));
  2162. if not(is_weak in flags) then
  2163. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.RefAsmSymbol(symname).Name))
  2164. else
  2165. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.WeakRefAsmSymbol(symname).Name));
  2166. {$ifdef cpu64bitaddr}
  2167. current_asmdata.asmlists[al_picdata].concat(tai_const.create_64bit(0));
  2168. {$else cpu64bitaddr}
  2169. current_asmdata.asmlists[al_picdata].concat(tai_const.create_32bit(0));
  2170. {$endif cpu64bitaddr}
  2171. end;
  2172. result := getaddressregister(list);
  2173. reference_reset_symbol(ref,l,0,sizeof(pint));
  2174. { a_load_ref_reg will turn this into a pic-load if needed }
  2175. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,result);
  2176. end;
  2177. end;
  2178. end;
  2179. procedure tcg.g_maybe_got_init(list: TAsmList);
  2180. begin
  2181. end;
  2182. procedure tcg.g_call(list: TAsmList;const s: string);
  2183. begin
  2184. allocallcpuregisters(list);
  2185. a_call_name(list,s,false);
  2186. deallocallcpuregisters(list);
  2187. end;
  2188. procedure tcg.g_local_unwind(list: TAsmList; l: TAsmLabel);
  2189. begin
  2190. a_jmp_always(list,l);
  2191. end;
  2192. procedure tcg.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  2193. begin
  2194. internalerror(200807231);
  2195. end;
  2196. procedure tcg.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2197. begin
  2198. internalerror(200807232);
  2199. end;
  2200. procedure tcg.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2201. begin
  2202. internalerror(200807233);
  2203. end;
  2204. procedure tcg.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  2205. begin
  2206. internalerror(200807234);
  2207. end;
  2208. function tcg.getflagregister(list: TAsmList; size: Tcgsize): Tregister;
  2209. begin
  2210. Result:=TRegister(0);
  2211. internalerror(200807238);
  2212. end;
  2213. procedure tcg.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: tcgsize; src, dst: TRegister);
  2214. begin
  2215. internalerror(2014070601);
  2216. end;
  2217. procedure tcg.g_stackpointer_alloc(list: TAsmList; size: longint);
  2218. begin
  2219. internalerror(2014070602);
  2220. end;
  2221. procedure tcg.a_mul_reg_reg_pair(list: TAsmList; size: TCgSize; src1,src2,dstlo,dsthi: TRegister);
  2222. begin
  2223. internalerror(2014060801);
  2224. end;
  2225. procedure tcg.g_div_const_reg_reg(list:tasmlist; size: TCgSize; a: tcgint; src,dst: tregister);
  2226. var
  2227. divreg: tregister;
  2228. magic: aInt;
  2229. u_magic: aWord;
  2230. u_shift: byte;
  2231. u_add: boolean;
  2232. begin
  2233. divreg:=getintregister(list,OS_INT);
  2234. if (size in [OS_S32,OS_S64]) then
  2235. begin
  2236. calc_divconst_magic_signed(tcgsize2size[size]*8,a,magic,u_shift);
  2237. { load magic value }
  2238. a_load_const_reg(list,OS_INT,magic,divreg);
  2239. { multiply, discarding low bits }
  2240. a_mul_reg_reg_pair(list,size,src,divreg,NR_NO,dst);
  2241. { add/subtract numerator }
  2242. if (a>0) and (magic<0) then
  2243. a_op_reg_reg_reg(list,OP_ADD,OS_INT,src,dst,dst)
  2244. else if (a<0) and (magic>0) then
  2245. a_op_reg_reg_reg(list,OP_SUB,OS_INT,src,dst,dst);
  2246. { shift shift places to the right (arithmetic) }
  2247. a_op_const_reg_reg(list,OP_SAR,OS_INT,u_shift,dst,dst);
  2248. { extract and add sign bit }
  2249. if (a>=0) then
  2250. a_op_const_reg_reg(list,OP_SHR,OS_INT,tcgsize2size[size]*8-1,src,divreg)
  2251. else
  2252. a_op_const_reg_reg(list,OP_SHR,OS_INT,tcgsize2size[size]*8-1,dst,divreg);
  2253. a_op_reg_reg_reg(list,OP_ADD,OS_INT,dst,divreg,dst);
  2254. end
  2255. else if (size in [OS_32,OS_64]) then
  2256. begin
  2257. calc_divconst_magic_unsigned(tcgsize2size[size]*8,a,u_magic,u_add,u_shift);
  2258. { load magic in divreg }
  2259. a_load_const_reg(list,OS_INT,tcgint(u_magic),divreg);
  2260. { multiply, discarding low bits }
  2261. a_mul_reg_reg_pair(list,size,src,divreg,NR_NO,dst);
  2262. if (u_add) then
  2263. begin
  2264. { Calculate "(numerator+result) shr u_shift", avoiding possible overflow }
  2265. a_op_reg_reg_reg(list,OP_SUB,OS_INT,dst,src,divreg);
  2266. { divreg=(numerator-result) }
  2267. a_op_const_reg_reg(list,OP_SHR,OS_INT,1,divreg,divreg);
  2268. { divreg=(numerator-result)/2 }
  2269. a_op_reg_reg_reg(list,OP_ADD,OS_INT,divreg,dst,divreg);
  2270. { divreg=(numerator+result)/2, already shifted by 1, so decrease u_shift. }
  2271. a_op_const_reg_reg(list,OP_SHR,OS_INT,u_shift-1,divreg,dst);
  2272. end
  2273. else
  2274. a_op_const_reg_reg(list,OP_SHR,OS_INT,u_shift,dst,dst);
  2275. end
  2276. else
  2277. InternalError(2014060601);
  2278. end;
  2279. {*****************************************************************************
  2280. TCG64
  2281. *****************************************************************************}
  2282. {$ifndef cpu64bitalu}
  2283. function joinreg64(reglo,reghi : tregister) : tregister64;
  2284. begin
  2285. result.reglo:=reglo;
  2286. result.reghi:=reghi;
  2287. end;
  2288. procedure tcg64.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64; regsrc,regdst : tregister64);
  2289. begin
  2290. a_load64_reg_reg(list,regsrc,regdst);
  2291. a_op64_const_reg(list,op,size,value,regdst);
  2292. end;
  2293. procedure tcg64.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  2294. var
  2295. tmpreg64 : tregister64;
  2296. begin
  2297. { when src1=dst then we need to first create a temp to prevent
  2298. overwriting src1 with src2 }
  2299. if (regsrc1.reghi=regdst.reghi) or
  2300. (regsrc1.reglo=regdst.reghi) or
  2301. (regsrc1.reghi=regdst.reglo) or
  2302. (regsrc1.reglo=regdst.reglo) then
  2303. begin
  2304. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2305. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2306. a_load64_reg_reg(list,regsrc2,tmpreg64);
  2307. a_op64_reg_reg(list,op,size,regsrc1,tmpreg64);
  2308. a_load64_reg_reg(list,tmpreg64,regdst);
  2309. end
  2310. else
  2311. begin
  2312. a_load64_reg_reg(list,regsrc2,regdst);
  2313. a_op64_reg_reg(list,op,size,regsrc1,regdst);
  2314. end;
  2315. end;
  2316. procedure tcg64.a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  2317. var
  2318. tmpreg64 : tregister64;
  2319. begin
  2320. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2321. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2322. a_load64_subsetref_reg(list,sref,tmpreg64);
  2323. a_op64_const_reg(list,op,size,a,tmpreg64);
  2324. a_load64_reg_subsetref(list,tmpreg64,sref);
  2325. end;
  2326. procedure tcg64.a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  2327. var
  2328. tmpreg64 : tregister64;
  2329. begin
  2330. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2331. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2332. a_load64_subsetref_reg(list,sref,tmpreg64);
  2333. a_op64_reg_reg(list,op,size,reg,tmpreg64);
  2334. a_load64_reg_subsetref(list,tmpreg64,sref);
  2335. end;
  2336. procedure tcg64.a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  2337. var
  2338. tmpreg64 : tregister64;
  2339. begin
  2340. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2341. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2342. a_load64_subsetref_reg(list,sref,tmpreg64);
  2343. a_op64_ref_reg(list,op,size,ref,tmpreg64);
  2344. a_load64_reg_subsetref(list,tmpreg64,sref);
  2345. end;
  2346. procedure tcg64.a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  2347. var
  2348. tmpreg64 : tregister64;
  2349. begin
  2350. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2351. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2352. a_load64_subsetref_reg(list,ssref,tmpreg64);
  2353. a_op64_reg_subsetref(list,op,size,tmpreg64,dsref);
  2354. end;
  2355. procedure tcg64.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2356. begin
  2357. a_op64_const_reg_reg(list,op,size,value,regsrc,regdst);
  2358. ovloc.loc:=LOC_VOID;
  2359. end;
  2360. procedure tcg64.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2361. begin
  2362. a_op64_reg_reg_reg(list,op,size,regsrc1,regsrc2,regdst);
  2363. ovloc.loc:=LOC_VOID;
  2364. end;
  2365. procedure tcg64.a_load64_loc_subsetref(list : TAsmList;const l: tlocation; const sref : tsubsetreference);
  2366. begin
  2367. case l.loc of
  2368. LOC_REFERENCE, LOC_CREFERENCE:
  2369. a_load64_ref_subsetref(list,l.reference,sref);
  2370. LOC_REGISTER,LOC_CREGISTER:
  2371. a_load64_reg_subsetref(list,l.register64,sref);
  2372. LOC_CONSTANT :
  2373. a_load64_const_subsetref(list,l.value64,sref);
  2374. LOC_SUBSETREF,LOC_CSUBSETREF:
  2375. a_load64_subsetref_subsetref(list,l.sref,sref);
  2376. else
  2377. internalerror(2006082210);
  2378. end;
  2379. end;
  2380. procedure tcg64.a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  2381. begin
  2382. case l.loc of
  2383. LOC_REFERENCE, LOC_CREFERENCE:
  2384. a_load64_subsetref_ref(list,sref,l.reference);
  2385. LOC_REGISTER,LOC_CREGISTER:
  2386. a_load64_subsetref_reg(list,sref,l.register64);
  2387. LOC_SUBSETREF,LOC_CSUBSETREF:
  2388. a_load64_subsetref_subsetref(list,sref,l.sref);
  2389. else
  2390. internalerror(2006082211);
  2391. end;
  2392. end;
  2393. {$else cpu64bitalu}
  2394. function joinreg128(reglo, reghi: tregister): tregister128;
  2395. begin
  2396. result.reglo:=reglo;
  2397. result.reghi:=reghi;
  2398. end;
  2399. procedure splitparaloc128(const cgpara:tcgpara;var cgparalo,cgparahi:tcgpara);
  2400. var
  2401. paraloclo,
  2402. paralochi : pcgparalocation;
  2403. begin
  2404. if not(cgpara.size in [OS_128,OS_S128]) then
  2405. internalerror(2012090604);
  2406. if not assigned(cgpara.location) then
  2407. internalerror(2012090605);
  2408. { init lo/hi para }
  2409. cgparahi.reset;
  2410. if cgpara.size=OS_S128 then
  2411. cgparahi.size:=OS_S64
  2412. else
  2413. cgparahi.size:=OS_64;
  2414. cgparahi.intsize:=8;
  2415. cgparahi.alignment:=cgpara.alignment;
  2416. paralochi:=cgparahi.add_location;
  2417. cgparalo.reset;
  2418. cgparalo.size:=OS_64;
  2419. cgparalo.intsize:=8;
  2420. cgparalo.alignment:=cgpara.alignment;
  2421. paraloclo:=cgparalo.add_location;
  2422. { 2 parameter fields? }
  2423. if assigned(cgpara.location^.next) then
  2424. begin
  2425. { Order for multiple locations is always
  2426. paraloc^ -> high
  2427. paraloc^.next -> low }
  2428. if (target_info.endian=ENDIAN_BIG) then
  2429. begin
  2430. { paraloc^ -> high
  2431. paraloc^.next -> low }
  2432. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2433. move(cgpara.location^.next^,paraloclo^,sizeof(paraloclo^));
  2434. end
  2435. else
  2436. begin
  2437. { paraloc^ -> low
  2438. paraloc^.next -> high }
  2439. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2440. move(cgpara.location^.next^,paralochi^,sizeof(paralochi^));
  2441. end;
  2442. end
  2443. else
  2444. begin
  2445. { single parameter, this can only be in memory }
  2446. if cgpara.location^.loc<>LOC_REFERENCE then
  2447. internalerror(2012090606);
  2448. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2449. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2450. { for big endian low is at +8, for little endian high }
  2451. if target_info.endian = endian_big then
  2452. begin
  2453. inc(cgparalo.location^.reference.offset,8);
  2454. cgparalo.alignment:=newalignment(cgparalo.alignment,8);
  2455. end
  2456. else
  2457. begin
  2458. inc(cgparahi.location^.reference.offset,8);
  2459. cgparahi.alignment:=newalignment(cgparahi.alignment,8);
  2460. end;
  2461. end;
  2462. { fix size }
  2463. paraloclo^.size:=cgparalo.size;
  2464. paraloclo^.next:=nil;
  2465. paralochi^.size:=cgparahi.size;
  2466. paralochi^.next:=nil;
  2467. end;
  2468. procedure tcg128.a_load128_reg_reg(list: TAsmList; regsrc,
  2469. regdst: tregister128);
  2470. begin
  2471. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reglo,regdst.reglo);
  2472. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reghi,regdst.reghi);
  2473. end;
  2474. procedure tcg128.a_load128_reg_ref(list: TAsmList; reg: tregister128;
  2475. const ref: treference);
  2476. var
  2477. tmpreg: tregister;
  2478. tmpref: treference;
  2479. begin
  2480. if target_info.endian = endian_big then
  2481. begin
  2482. tmpreg:=reg.reglo;
  2483. reg.reglo:=reg.reghi;
  2484. reg.reghi:=tmpreg;
  2485. end;
  2486. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reglo,ref);
  2487. tmpref := ref;
  2488. inc(tmpref.offset,8);
  2489. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reghi,tmpref);
  2490. end;
  2491. procedure tcg128.a_load128_ref_reg(list: TAsmList; const ref: treference;
  2492. reg: tregister128);
  2493. var
  2494. tmpreg: tregister;
  2495. tmpref: treference;
  2496. begin
  2497. if target_info.endian = endian_big then
  2498. begin
  2499. tmpreg := reg.reglo;
  2500. reg.reglo := reg.reghi;
  2501. reg.reghi := tmpreg;
  2502. end;
  2503. tmpref := ref;
  2504. if (tmpref.base=reg.reglo) then
  2505. begin
  2506. tmpreg:=cg.getaddressregister(list);
  2507. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.base,tmpreg);
  2508. tmpref.base:=tmpreg;
  2509. end
  2510. else
  2511. { this works only for the i386, thus the i386 needs to override }
  2512. { this method and this method must be replaced by a more generic }
  2513. { implementation FK }
  2514. if (tmpref.index=reg.reglo) then
  2515. begin
  2516. tmpreg:=cg.getaddressregister(list);
  2517. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.index,tmpreg);
  2518. tmpref.index:=tmpreg;
  2519. end;
  2520. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reglo);
  2521. inc(tmpref.offset,8);
  2522. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reghi);
  2523. end;
  2524. procedure tcg128.a_load128_loc_ref(list: TAsmList; const l: tlocation;
  2525. const ref: treference);
  2526. begin
  2527. case l.loc of
  2528. LOC_REGISTER,LOC_CREGISTER:
  2529. a_load128_reg_ref(list,l.register128,ref);
  2530. { not yet implemented:
  2531. LOC_CONSTANT :
  2532. a_load128_const_ref(list,l.value128,ref);
  2533. LOC_SUBSETREF, LOC_CSUBSETREF:
  2534. a_load64_subsetref_ref(list,l.sref,ref); }
  2535. else
  2536. internalerror(201209061);
  2537. end;
  2538. end;
  2539. procedure tcg128.a_load128_reg_loc(list: TAsmList; reg: tregister128;
  2540. const l: tlocation);
  2541. begin
  2542. case l.loc of
  2543. LOC_REFERENCE, LOC_CREFERENCE:
  2544. a_load128_reg_ref(list,reg,l.reference);
  2545. LOC_REGISTER,LOC_CREGISTER:
  2546. a_load128_reg_reg(list,reg,l.register128);
  2547. { not yet implemented:
  2548. LOC_SUBSETREF, LOC_CSUBSETREF:
  2549. a_load64_reg_subsetref(list,reg,l.sref);
  2550. LOC_MMREGISTER, LOC_CMMREGISTER:
  2551. a_loadmm_intreg64_reg(list,l.size,reg,l.register); }
  2552. else
  2553. internalerror(201209062);
  2554. end;
  2555. end;
  2556. procedure tcg128.a_load128_const_reg(list: TAsmList; valuelo,
  2557. valuehi: int64; reg: tregister128);
  2558. begin
  2559. cg.a_load_const_reg(list,OS_64,aint(valuelo),reg.reglo);
  2560. cg.a_load_const_reg(list,OS_64,aint(valuehi),reg.reghi);
  2561. end;
  2562. procedure tcg128.a_load128_loc_cgpara(list: TAsmList; const l: tlocation;
  2563. const paraloc: TCGPara);
  2564. begin
  2565. case l.loc of
  2566. LOC_REGISTER,
  2567. LOC_CREGISTER :
  2568. a_load128_reg_cgpara(list,l.register128,paraloc);
  2569. {not yet implemented:
  2570. LOC_CONSTANT :
  2571. a_load128_const_cgpara(list,l.value64,paraloc);
  2572. }
  2573. LOC_CREFERENCE,
  2574. LOC_REFERENCE :
  2575. a_load128_ref_cgpara(list,l.reference,paraloc);
  2576. else
  2577. internalerror(2012090603);
  2578. end;
  2579. end;
  2580. procedure tcg128.a_load128_reg_cgpara(list : TAsmList;reg : tregister128;const paraloc : tcgpara);
  2581. var
  2582. tmplochi,tmploclo: tcgpara;
  2583. begin
  2584. tmploclo.init;
  2585. tmplochi.init;
  2586. splitparaloc128(paraloc,tmploclo,tmplochi);
  2587. cg.a_load_reg_cgpara(list,OS_64,reg.reghi,tmplochi);
  2588. cg.a_load_reg_cgpara(list,OS_64,reg.reglo,tmploclo);
  2589. tmploclo.done;
  2590. tmplochi.done;
  2591. end;
  2592. procedure tcg128.a_load128_ref_cgpara(list : TAsmList;const r : treference;const paraloc : tcgpara);
  2593. var
  2594. tmprefhi,tmpreflo : treference;
  2595. tmploclo,tmplochi : tcgpara;
  2596. begin
  2597. tmploclo.init;
  2598. tmplochi.init;
  2599. splitparaloc128(paraloc,tmploclo,tmplochi);
  2600. tmprefhi:=r;
  2601. tmpreflo:=r;
  2602. if target_info.endian=endian_big then
  2603. inc(tmpreflo.offset,8)
  2604. else
  2605. inc(tmprefhi.offset,8);
  2606. cg.a_load_ref_cgpara(list,OS_64,tmprefhi,tmplochi);
  2607. cg.a_load_ref_cgpara(list,OS_64,tmpreflo,tmploclo);
  2608. tmploclo.done;
  2609. tmplochi.done;
  2610. end;
  2611. {$endif cpu64bitalu}
  2612. function asmsym2indsymflags(sym: TAsmSymbol): tindsymflags;
  2613. begin
  2614. result:=[];
  2615. if sym.typ<>AT_FUNCTION then
  2616. include(result,is_data);
  2617. if sym.bind=AB_WEAK_EXTERNAL then
  2618. include(result,is_weak);
  2619. end;
  2620. procedure destroy_codegen;
  2621. begin
  2622. cg.free;
  2623. cg:=nil;
  2624. {$ifdef cpu64bitalu}
  2625. cg128.free;
  2626. cg128:=nil;
  2627. {$else cpu64bitalu}
  2628. cg64.free;
  2629. cg64:=nil;
  2630. {$endif cpu64bitalu}
  2631. end;
  2632. end.