cgobj.pas 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998
  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. if assigned(cgpara.location^.next) then
  718. begin
  719. tg.gethltemp(list,cgpara.def,cgpara.def.size,tt_persistent,ref);
  720. a_load_reg_ref(list,size,size,r,ref);
  721. a_load_ref_cgpara(list,size,ref,cgpara);
  722. tg.ungettemp(list,ref);
  723. exit;
  724. end;
  725. paramanager.alloccgpara(list,cgpara);
  726. if cgpara.location^.shiftval<0 then
  727. begin
  728. tmpreg:=getintregister(list,cgpara.location^.size);
  729. a_op_const_reg_reg(list,OP_SHL,cgpara.location^.size,-cgpara.location^.shiftval,r,tmpreg);
  730. r:=tmpreg;
  731. end;
  732. case cgpara.location^.loc of
  733. LOC_REGISTER,LOC_CREGISTER:
  734. a_load_reg_reg(list,size,cgpara.location^.size,r,cgpara.location^.register);
  735. LOC_REFERENCE,LOC_CREFERENCE:
  736. begin
  737. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  738. a_load_reg_ref(list,size,cgpara.location^.size,r,ref);
  739. end;
  740. LOC_MMREGISTER,LOC_CMMREGISTER:
  741. a_loadmm_intreg_reg(list,size,cgpara.location^.size,r,cgpara.location^.register,mms_movescalar);
  742. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  743. begin
  744. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,ref);
  745. a_load_reg_ref(list,size,size,r,ref);
  746. a_loadfpu_ref_cgpara(list,cgpara.location^.size,ref,cgpara);
  747. tg.Ungettemp(list,ref);
  748. end
  749. else
  750. internalerror(2002071004);
  751. end;
  752. end;
  753. procedure tcg.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : TCGPara);
  754. var
  755. ref : treference;
  756. begin
  757. cgpara.check_simple_location;
  758. paramanager.alloccgpara(list,cgpara);
  759. if cgpara.location^.shiftval<0 then
  760. a:=a shl -cgpara.location^.shiftval;
  761. case cgpara.location^.loc of
  762. LOC_REGISTER,LOC_CREGISTER:
  763. a_load_const_reg(list,cgpara.location^.size,a,cgpara.location^.register);
  764. LOC_REFERENCE,LOC_CREFERENCE:
  765. begin
  766. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  767. a_load_const_ref(list,cgpara.location^.size,a,ref);
  768. end
  769. else
  770. internalerror(2010053109);
  771. end;
  772. end;
  773. procedure tcg.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : TCGPara);
  774. var
  775. tmpref, ref: treference;
  776. tmpreg: tregister;
  777. location: pcgparalocation;
  778. orgsizeleft,
  779. sizeleft: tcgint;
  780. reghasvalue: boolean;
  781. begin
  782. location:=cgpara.location;
  783. tmpref:=r;
  784. sizeleft:=cgpara.intsize;
  785. while assigned(location) do
  786. begin
  787. paramanager.allocparaloc(list,location);
  788. case location^.loc of
  789. LOC_REGISTER,LOC_CREGISTER:
  790. begin
  791. { Parameter locations are often allocated in multiples of
  792. entire registers. If a parameter only occupies a part of
  793. such a register (e.g. a 16 bit int on a 32 bit
  794. architecture), the size of this parameter can only be
  795. determined by looking at the "size" parameter of this
  796. method -> if the size parameter is <= sizeof(aint), then
  797. we check that there is only one parameter location and
  798. then use this "size" to load the value into the parameter
  799. location }
  800. if (size<>OS_NO) and
  801. (tcgsize2size[size]<=sizeof(aint)) then
  802. begin
  803. cgpara.check_simple_location;
  804. a_load_ref_reg(list,size,location^.size,tmpref,location^.register);
  805. if location^.shiftval<0 then
  806. a_op_const_reg(list,OP_SHL,location^.size,-location^.shiftval,location^.register);
  807. end
  808. { there's a lot more data left, and the current paraloc's
  809. register is entirely filled with part of that data }
  810. else if (sizeleft>sizeof(aint)) then
  811. begin
  812. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  813. end
  814. { we're at the end of the data, and it can be loaded into
  815. the current location's register with a single regular
  816. load }
  817. else if sizeleft in [1,2,4,8] then
  818. begin
  819. a_load_ref_reg(list,int_cgsize(sizeleft),location^.size,tmpref,location^.register);
  820. if location^.shiftval<0 then
  821. a_op_const_reg(list,OP_SHL,location^.size,-location^.shiftval,location^.register);
  822. end
  823. { we're at the end of the data, and we need multiple loads
  824. to get it in the register because it's an irregular size }
  825. else
  826. begin
  827. { should be the last part }
  828. if assigned(location^.next) then
  829. internalerror(2010052907);
  830. { load the value piecewise to get it into the register }
  831. orgsizeleft:=sizeleft;
  832. reghasvalue:=false;
  833. {$ifdef cpu64bitalu}
  834. if sizeleft>=4 then
  835. begin
  836. a_load_ref_reg(list,OS_32,location^.size,tmpref,location^.register);
  837. dec(sizeleft,4);
  838. if target_info.endian=endian_big then
  839. a_op_const_reg(list,OP_SHL,location^.size,sizeleft*8,location^.register);
  840. inc(tmpref.offset,4);
  841. reghasvalue:=true;
  842. end;
  843. {$endif cpu64bitalu}
  844. if sizeleft>=2 then
  845. begin
  846. tmpreg:=getintregister(list,location^.size);
  847. a_load_ref_reg(list,OS_16,location^.size,tmpref,tmpreg);
  848. dec(sizeleft,2);
  849. if reghasvalue then
  850. begin
  851. if target_info.endian=endian_big then
  852. a_op_const_reg(list,OP_SHL,location^.size,sizeleft*8,tmpreg)
  853. else
  854. a_op_const_reg(list,OP_SHL,location^.size,(orgsizeleft-(sizeleft+2))*8,tmpreg);
  855. a_op_reg_reg(list,OP_OR,location^.size,tmpreg,location^.register);
  856. end
  857. else
  858. begin
  859. if target_info.endian=endian_big then
  860. a_op_const_reg_reg(list,OP_SHL,location^.size,sizeleft*8,tmpreg,location^.register)
  861. else
  862. a_load_reg_reg(list,location^.size,location^.size,tmpreg,location^.register);
  863. end;
  864. inc(tmpref.offset,2);
  865. reghasvalue:=true;
  866. end;
  867. if sizeleft=1 then
  868. begin
  869. tmpreg:=getintregister(list,location^.size);
  870. a_load_ref_reg(list,OS_8,location^.size,tmpref,tmpreg);
  871. dec(sizeleft,1);
  872. if reghasvalue then
  873. begin
  874. if target_info.endian=endian_little then
  875. a_op_const_reg(list,OP_SHL,location^.size,(orgsizeleft-(sizeleft+1))*8,tmpreg);
  876. a_op_reg_reg(list,OP_OR,location^.size,tmpreg,location^.register)
  877. end
  878. else
  879. a_load_reg_reg(list,location^.size,location^.size,tmpreg,location^.register);
  880. inc(tmpref.offset);
  881. end;
  882. if location^.shiftval<0 then
  883. a_op_const_reg(list,OP_SHL,location^.size,-location^.shiftval,location^.register);
  884. { the loop will already adjust the offset and sizeleft }
  885. dec(tmpref.offset,orgsizeleft);
  886. sizeleft:=orgsizeleft;
  887. end;
  888. end;
  889. LOC_REFERENCE,LOC_CREFERENCE:
  890. begin
  891. if assigned(location^.next) then
  892. internalerror(2010052906);
  893. reference_reset_base(ref,location^.reference.index,location^.reference.offset,newalignment(cgpara.alignment,cgpara.intsize-sizeleft));
  894. if (size <> OS_NO) and
  895. (tcgsize2size[size] <= sizeof(aint)) then
  896. a_load_ref_ref(list,size,location^.size,tmpref,ref)
  897. else
  898. { use concatcopy, because the parameter can be larger than }
  899. { what the OS_* constants can handle }
  900. g_concatcopy(list,tmpref,ref,sizeleft);
  901. end;
  902. LOC_MMREGISTER,LOC_CMMREGISTER:
  903. begin
  904. case location^.size of
  905. OS_F32,
  906. OS_F64,
  907. OS_F128:
  908. a_loadmm_ref_reg(list,location^.size,location^.size,tmpref,location^.register,mms_movescalar);
  909. OS_M8..OS_M128,
  910. OS_MS8..OS_MS128:
  911. a_loadmm_ref_reg(list,location^.size,location^.size,tmpref,location^.register,nil);
  912. else
  913. internalerror(2010053101);
  914. end;
  915. end
  916. else
  917. internalerror(2010053111);
  918. end;
  919. inc(tmpref.offset,tcgsize2size[location^.size]);
  920. dec(sizeleft,tcgsize2size[location^.size]);
  921. location:=location^.next;
  922. end;
  923. end;
  924. procedure tcg.a_load_loc_cgpara(list : TAsmList;const l:tlocation;const cgpara : TCGPara);
  925. begin
  926. case l.loc of
  927. LOC_REGISTER,
  928. LOC_CREGISTER :
  929. a_load_reg_cgpara(list,l.size,l.register,cgpara);
  930. LOC_CONSTANT :
  931. a_load_const_cgpara(list,l.size,l.value,cgpara);
  932. LOC_CREFERENCE,
  933. LOC_REFERENCE :
  934. a_load_ref_cgpara(list,l.size,l.reference,cgpara);
  935. else
  936. internalerror(2002032211);
  937. end;
  938. end;
  939. procedure tcg.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : TCGPara);
  940. var
  941. hr : tregister;
  942. begin
  943. cgpara.check_simple_location;
  944. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  945. begin
  946. paramanager.allocparaloc(list,cgpara.location);
  947. a_loadaddr_ref_reg(list,r,cgpara.location^.register)
  948. end
  949. else
  950. begin
  951. hr:=getaddressregister(list);
  952. a_loadaddr_ref_reg(list,r,hr);
  953. a_load_reg_cgpara(list,OS_ADDR,hr,cgpara);
  954. end;
  955. end;
  956. procedure tcg.a_load_cgparaloc_ref(list : TAsmList;const paraloc : TCGParaLocation;const ref : treference;sizeleft : tcgint;align : longint);
  957. var
  958. href : treference;
  959. hreg : tregister;
  960. cgsize: tcgsize;
  961. begin
  962. case paraloc.loc of
  963. LOC_REGISTER :
  964. begin
  965. hreg:=paraloc.register;
  966. cgsize:=paraloc.size;
  967. if paraloc.shiftval>0 then
  968. a_op_const_reg_reg(list,OP_SHL,OS_INT,paraloc.shiftval,paraloc.register,paraloc.register)
  969. else if (paraloc.shiftval<0) and
  970. (sizeleft in [1,2,4]) then
  971. begin
  972. a_op_const_reg_reg(list,OP_SHR,OS_INT,-paraloc.shiftval,paraloc.register,paraloc.register);
  973. { convert to a register of 1/2/4 bytes in size, since the
  974. original register had to be made larger to be able to hold
  975. the shifted value }
  976. cgsize:=int_cgsize(tcgsize2size[OS_INT]-(-paraloc.shiftval div 8));
  977. hreg:=getintregister(list,cgsize);
  978. a_load_reg_reg(list,OS_INT,cgsize,paraloc.register,hreg);
  979. end;
  980. a_load_reg_ref(list,paraloc.size,cgsize,hreg,ref);
  981. end;
  982. LOC_MMREGISTER :
  983. begin
  984. case paraloc.size of
  985. OS_F32,
  986. OS_F64,
  987. OS_F128:
  988. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  989. OS_M8..OS_M128,
  990. OS_MS8..OS_MS128:
  991. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,nil);
  992. else
  993. internalerror(2010053102);
  994. end;
  995. end;
  996. LOC_FPUREGISTER :
  997. a_loadfpu_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  998. LOC_REFERENCE :
  999. begin
  1000. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,align);
  1001. { use concatcopy, because it can also be a float which fails when
  1002. load_ref_ref is used. Don't copy data when the references are equal }
  1003. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1004. g_concatcopy(list,href,ref,sizeleft);
  1005. end;
  1006. else
  1007. internalerror(2002081302);
  1008. end;
  1009. end;
  1010. procedure tcg.a_load_cgparaloc_anyreg(list: TAsmList;regsize: tcgsize;const paraloc: TCGParaLocation;reg: tregister;align: longint);
  1011. var
  1012. href : treference;
  1013. begin
  1014. case paraloc.loc of
  1015. LOC_REGISTER :
  1016. begin
  1017. if paraloc.shiftval<0 then
  1018. a_op_const_reg_reg(list,OP_SHR,OS_INT,-paraloc.shiftval,paraloc.register,paraloc.register);
  1019. case getregtype(reg) of
  1020. R_ADDRESSREGISTER,
  1021. R_INTREGISTER:
  1022. a_load_reg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1023. R_MMREGISTER:
  1024. a_loadmm_intreg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1025. else
  1026. internalerror(2009112422);
  1027. end;
  1028. end;
  1029. LOC_MMREGISTER :
  1030. begin
  1031. case getregtype(reg) of
  1032. R_ADDRESSREGISTER,
  1033. R_INTREGISTER:
  1034. a_loadmm_reg_intreg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1035. R_MMREGISTER:
  1036. begin
  1037. case paraloc.size of
  1038. OS_F32,
  1039. OS_F64,
  1040. OS_F128:
  1041. a_loadmm_reg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1042. OS_M8..OS_M128,
  1043. OS_MS8..OS_MS128:
  1044. a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,nil);
  1045. else
  1046. internalerror(2010053102);
  1047. end;
  1048. end;
  1049. else
  1050. internalerror(2010053104);
  1051. end;
  1052. end;
  1053. LOC_FPUREGISTER :
  1054. a_loadfpu_reg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1055. LOC_REFERENCE :
  1056. begin
  1057. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,align);
  1058. case getregtype(reg) of
  1059. R_ADDRESSREGISTER,
  1060. R_INTREGISTER :
  1061. a_load_ref_reg(list,paraloc.size,regsize,href,reg);
  1062. R_FPUREGISTER :
  1063. a_loadfpu_ref_reg(list,paraloc.size,regsize,href,reg);
  1064. R_MMREGISTER :
  1065. { not paraloc.size, because it may be OS_64 instead of
  1066. OS_F64 in case the parameter is passed using integer
  1067. conventions (e.g., on ARM) }
  1068. a_loadmm_ref_reg(list,regsize,regsize,href,reg,mms_movescalar);
  1069. else
  1070. internalerror(2004101012);
  1071. end;
  1072. end;
  1073. else
  1074. internalerror(2002081302);
  1075. end;
  1076. end;
  1077. {****************************************************************************
  1078. some generic implementations
  1079. ****************************************************************************}
  1080. { memory/register loading }
  1081. procedure tcg.a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  1082. var
  1083. tmpref : treference;
  1084. tmpreg : tregister;
  1085. i : longint;
  1086. begin
  1087. if ref.alignment<tcgsize2size[fromsize] then
  1088. begin
  1089. tmpref:=ref;
  1090. { we take care of the alignment now }
  1091. tmpref.alignment:=0;
  1092. case FromSize of
  1093. OS_16,OS_S16:
  1094. begin
  1095. tmpreg:=getintregister(list,OS_16);
  1096. a_load_reg_reg(list,fromsize,OS_16,register,tmpreg);
  1097. if target_info.endian=endian_big then
  1098. inc(tmpref.offset);
  1099. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1100. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1101. tmpreg:=makeregsize(list,tmpreg,OS_16);
  1102. a_op_const_reg(list,OP_SHR,OS_16,8,tmpreg);
  1103. if target_info.endian=endian_big then
  1104. dec(tmpref.offset)
  1105. else
  1106. inc(tmpref.offset);
  1107. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1108. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1109. end;
  1110. OS_32,OS_S32:
  1111. begin
  1112. { could add an optimised case for ref.alignment=2 }
  1113. tmpreg:=getintregister(list,OS_32);
  1114. a_load_reg_reg(list,fromsize,OS_32,register,tmpreg);
  1115. if target_info.endian=endian_big then
  1116. inc(tmpref.offset,3);
  1117. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1118. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1119. tmpreg:=makeregsize(list,tmpreg,OS_32);
  1120. for i:=1 to 3 do
  1121. begin
  1122. a_op_const_reg(list,OP_SHR,OS_32,8,tmpreg);
  1123. if target_info.endian=endian_big then
  1124. dec(tmpref.offset)
  1125. else
  1126. inc(tmpref.offset);
  1127. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1128. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1129. tmpreg:=makeregsize(list,tmpreg,OS_32);
  1130. end;
  1131. end
  1132. else
  1133. a_load_reg_ref(list,fromsize,tosize,register,tmpref);
  1134. end;
  1135. end
  1136. else
  1137. a_load_reg_ref(list,fromsize,tosize,register,ref);
  1138. end;
  1139. procedure tcg.a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  1140. var
  1141. tmpref : treference;
  1142. tmpreg,
  1143. tmpreg2 : tregister;
  1144. i : longint;
  1145. hisize : tcgsize;
  1146. begin
  1147. if ref.alignment in [1,2] then
  1148. begin
  1149. tmpref:=ref;
  1150. { we take care of the alignment now }
  1151. tmpref.alignment:=0;
  1152. case FromSize of
  1153. OS_16,OS_S16:
  1154. if ref.alignment=2 then
  1155. a_load_ref_reg(list,fromsize,tosize,tmpref,register)
  1156. else
  1157. begin
  1158. if FromSize=OS_16 then
  1159. hisize:=OS_8
  1160. else
  1161. hisize:=OS_S8;
  1162. { first load in tmpreg, because the target register }
  1163. { may be used in ref as well }
  1164. if target_info.endian=endian_little then
  1165. inc(tmpref.offset);
  1166. tmpreg:=getintregister(list,OS_8);
  1167. a_load_ref_reg(list,hisize,hisize,tmpref,tmpreg);
  1168. tmpreg:=makeregsize(list,tmpreg,FromSize);
  1169. a_op_const_reg(list,OP_SHL,FromSize,8,tmpreg);
  1170. if target_info.endian=endian_little then
  1171. dec(tmpref.offset)
  1172. else
  1173. inc(tmpref.offset);
  1174. a_load_ref_reg(list,OS_8,OS_16,tmpref,register);
  1175. a_op_reg_reg(list,OP_OR,OS_16,tmpreg,register);
  1176. end;
  1177. OS_32,OS_S32:
  1178. if ref.alignment=2 then
  1179. begin
  1180. if target_info.endian=endian_little then
  1181. inc(tmpref.offset,2);
  1182. tmpreg:=getintregister(list,OS_32);
  1183. a_load_ref_reg(list,OS_16,OS_32,tmpref,tmpreg);
  1184. a_op_const_reg(list,OP_SHL,OS_32,16,tmpreg);
  1185. if target_info.endian=endian_little then
  1186. dec(tmpref.offset,2)
  1187. else
  1188. inc(tmpref.offset,2);
  1189. a_load_ref_reg(list,OS_16,OS_32,tmpref,register);
  1190. a_op_reg_reg(list,OP_OR,OS_32,tmpreg,register);
  1191. end
  1192. else
  1193. begin
  1194. if target_info.endian=endian_little then
  1195. inc(tmpref.offset,3);
  1196. tmpreg:=getintregister(list,OS_32);
  1197. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg);
  1198. tmpreg2:=getintregister(list,OS_32);
  1199. for i:=1 to 3 do
  1200. begin
  1201. a_op_const_reg(list,OP_SHL,OS_32,8,tmpreg);
  1202. if target_info.endian=endian_little then
  1203. dec(tmpref.offset)
  1204. else
  1205. inc(tmpref.offset);
  1206. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg2);
  1207. a_op_reg_reg(list,OP_OR,OS_32,tmpreg2,tmpreg);
  1208. end;
  1209. a_load_reg_reg(list,OS_32,OS_32,tmpreg,register);
  1210. end
  1211. else
  1212. a_load_ref_reg(list,fromsize,tosize,tmpref,register);
  1213. end;
  1214. end
  1215. else
  1216. a_load_ref_reg(list,fromsize,tosize,ref,register);
  1217. end;
  1218. procedure tcg.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  1219. var
  1220. tmpreg: tregister;
  1221. begin
  1222. { verify if we have the same reference }
  1223. if references_equal(sref,dref) then
  1224. exit;
  1225. tmpreg:=getintregister(list,tosize);
  1226. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  1227. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  1228. end;
  1229. procedure tcg.a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : treference);
  1230. var
  1231. tmpreg: tregister;
  1232. begin
  1233. tmpreg:=getintregister(list,size);
  1234. a_load_const_reg(list,size,a,tmpreg);
  1235. a_load_reg_ref(list,size,size,tmpreg,ref);
  1236. end;
  1237. procedure tcg.a_load_const_loc(list : TAsmList;a : tcgint;const loc: tlocation);
  1238. begin
  1239. case loc.loc of
  1240. LOC_REFERENCE,LOC_CREFERENCE:
  1241. a_load_const_ref(list,loc.size,a,loc.reference);
  1242. LOC_REGISTER,LOC_CREGISTER:
  1243. a_load_const_reg(list,loc.size,a,loc.register);
  1244. else
  1245. internalerror(200203272);
  1246. end;
  1247. end;
  1248. procedure tcg.a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  1249. begin
  1250. case loc.loc of
  1251. LOC_REFERENCE,LOC_CREFERENCE:
  1252. a_load_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  1253. LOC_REGISTER,LOC_CREGISTER:
  1254. a_load_reg_reg(list,fromsize,loc.size,reg,loc.register);
  1255. LOC_MMREGISTER,LOC_CMMREGISTER:
  1256. a_loadmm_intreg_reg(list,fromsize,loc.size,reg,loc.register,mms_movescalar);
  1257. else
  1258. internalerror(200203271);
  1259. end;
  1260. end;
  1261. procedure tcg.a_load_loc_reg(list : TAsmList; tosize: tcgsize; const loc: tlocation; reg : tregister);
  1262. begin
  1263. case loc.loc of
  1264. LOC_REFERENCE,LOC_CREFERENCE:
  1265. a_load_ref_reg(list,loc.size,tosize,loc.reference,reg);
  1266. LOC_REGISTER,LOC_CREGISTER:
  1267. a_load_reg_reg(list,loc.size,tosize,loc.register,reg);
  1268. LOC_CONSTANT:
  1269. a_load_const_reg(list,tosize,loc.value,reg);
  1270. else
  1271. internalerror(200109092);
  1272. end;
  1273. end;
  1274. procedure tcg.a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  1275. begin
  1276. case loc.loc of
  1277. LOC_REFERENCE,LOC_CREFERENCE:
  1278. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  1279. LOC_REGISTER,LOC_CREGISTER:
  1280. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  1281. LOC_CONSTANT:
  1282. a_load_const_ref(list,tosize,loc.value,ref);
  1283. else
  1284. internalerror(200109302);
  1285. end;
  1286. end;
  1287. procedure tcg.optimize_op_const(size: TCGSize; var op: topcg; var a : tcgint);
  1288. var
  1289. powerval : longint;
  1290. signext_a, zeroext_a: tcgint;
  1291. begin
  1292. case size of
  1293. OS_64,OS_S64:
  1294. begin
  1295. signext_a:=int64(a);
  1296. zeroext_a:=int64(a);
  1297. end;
  1298. OS_32,OS_S32:
  1299. begin
  1300. signext_a:=longint(a);
  1301. zeroext_a:=dword(a);
  1302. end;
  1303. OS_16,OS_S16:
  1304. begin
  1305. signext_a:=smallint(a);
  1306. zeroext_a:=word(a);
  1307. end;
  1308. OS_8,OS_S8:
  1309. begin
  1310. signext_a:=shortint(a);
  1311. zeroext_a:=byte(a);
  1312. end
  1313. else
  1314. begin
  1315. { Should we internalerror() here instead? }
  1316. signext_a:=a;
  1317. zeroext_a:=a;
  1318. end;
  1319. end;
  1320. case op of
  1321. OP_OR :
  1322. begin
  1323. { or with zero returns same result }
  1324. if a = 0 then
  1325. op:=OP_NONE
  1326. else
  1327. { or with max returns max }
  1328. if signext_a = -1 then
  1329. op:=OP_MOVE;
  1330. end;
  1331. OP_AND :
  1332. begin
  1333. { and with max returns same result }
  1334. if (signext_a = -1) then
  1335. op:=OP_NONE
  1336. else
  1337. { and with 0 returns 0 }
  1338. if a=0 then
  1339. op:=OP_MOVE;
  1340. end;
  1341. OP_DIV :
  1342. begin
  1343. { division by 1 returns result }
  1344. if a = 1 then
  1345. op:=OP_NONE
  1346. else if ispowerof2(int64(zeroext_a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  1347. begin
  1348. a := powerval;
  1349. op:= OP_SHR;
  1350. end;
  1351. end;
  1352. OP_IDIV:
  1353. begin
  1354. if a = 1 then
  1355. op:=OP_NONE;
  1356. end;
  1357. OP_MUL,OP_IMUL:
  1358. begin
  1359. if a = 1 then
  1360. op:=OP_NONE
  1361. else
  1362. if a=0 then
  1363. op:=OP_MOVE
  1364. else if ispowerof2(int64(zeroext_a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  1365. begin
  1366. a := powerval;
  1367. op:= OP_SHL;
  1368. end;
  1369. end;
  1370. OP_ADD,OP_SUB:
  1371. begin
  1372. if a = 0 then
  1373. op:=OP_NONE;
  1374. end;
  1375. OP_SAR,OP_SHL,OP_SHR,OP_ROL,OP_ROR:
  1376. begin
  1377. if a = 0 then
  1378. op:=OP_NONE;
  1379. end;
  1380. end;
  1381. end;
  1382. procedure tcg.a_loadfpu_loc_reg(list: TAsmList; tosize: tcgsize; const loc: tlocation; const reg: tregister);
  1383. begin
  1384. case loc.loc of
  1385. LOC_REFERENCE, LOC_CREFERENCE:
  1386. a_loadfpu_ref_reg(list,loc.size,tosize,loc.reference,reg);
  1387. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1388. a_loadfpu_reg_reg(list,loc.size,tosize,loc.register,reg);
  1389. else
  1390. internalerror(200203301);
  1391. end;
  1392. end;
  1393. procedure tcg.a_loadfpu_reg_loc(list: TAsmList; fromsize: tcgsize; const reg: tregister; const loc: tlocation);
  1394. begin
  1395. case loc.loc of
  1396. LOC_REFERENCE, LOC_CREFERENCE:
  1397. a_loadfpu_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  1398. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1399. a_loadfpu_reg_reg(list,fromsize,loc.size,reg,loc.register);
  1400. else
  1401. internalerror(48991);
  1402. end;
  1403. end;
  1404. procedure tcg.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tcgsize; const ref1,ref2: treference);
  1405. var
  1406. reg: tregister;
  1407. regsize: tcgsize;
  1408. begin
  1409. if (fromsize>=tosize) then
  1410. regsize:=fromsize
  1411. else
  1412. regsize:=tosize;
  1413. reg:=getfpuregister(list,regsize);
  1414. a_loadfpu_ref_reg(list,fromsize,regsize,ref1,reg);
  1415. a_loadfpu_reg_ref(list,regsize,tosize,reg,ref2);
  1416. end;
  1417. procedure tcg.a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);
  1418. var
  1419. ref : treference;
  1420. begin
  1421. paramanager.alloccgpara(list,cgpara);
  1422. case cgpara.location^.loc of
  1423. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1424. begin
  1425. cgpara.check_simple_location;
  1426. a_loadfpu_reg_reg(list,size,size,r,cgpara.location^.register);
  1427. end;
  1428. LOC_REFERENCE,LOC_CREFERENCE:
  1429. begin
  1430. cgpara.check_simple_location;
  1431. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1432. a_loadfpu_reg_ref(list,size,size,r,ref);
  1433. end;
  1434. LOC_REGISTER,LOC_CREGISTER:
  1435. begin
  1436. { paramfpu_ref does the check_simpe_location check here if necessary }
  1437. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,ref);
  1438. a_loadfpu_reg_ref(list,size,size,r,ref);
  1439. a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  1440. tg.Ungettemp(list,ref);
  1441. end;
  1442. else
  1443. internalerror(2010053112);
  1444. end;
  1445. end;
  1446. procedure tcg.a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);
  1447. var
  1448. href : treference;
  1449. hsize: tcgsize;
  1450. begin
  1451. case cgpara.location^.loc of
  1452. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1453. begin
  1454. cgpara.check_simple_location;
  1455. paramanager.alloccgpara(list,cgpara);
  1456. a_loadfpu_ref_reg(list,size,size,ref,cgpara.location^.register);
  1457. end;
  1458. LOC_REFERENCE,LOC_CREFERENCE:
  1459. begin
  1460. cgpara.check_simple_location;
  1461. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1462. { concatcopy should choose the best way to copy the data }
  1463. g_concatcopy(list,ref,href,tcgsize2size[size]);
  1464. end;
  1465. LOC_REGISTER,LOC_CREGISTER:
  1466. begin
  1467. { force integer size }
  1468. hsize:=int_cgsize(tcgsize2size[size]);
  1469. {$ifndef cpu64bitalu}
  1470. if (hsize in [OS_S64,OS_64]) then
  1471. cg64.a_load64_ref_cgpara(list,ref,cgpara)
  1472. else
  1473. {$endif not cpu64bitalu}
  1474. begin
  1475. cgpara.check_simple_location;
  1476. a_load_ref_cgpara(list,hsize,ref,cgpara)
  1477. end;
  1478. end
  1479. else
  1480. internalerror(200402201);
  1481. end;
  1482. end;
  1483. procedure tcg.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1484. var
  1485. tmpreg : tregister;
  1486. begin
  1487. tmpreg:=getintregister(list,size);
  1488. a_load_ref_reg(list,size,size,ref,tmpreg);
  1489. a_op_const_reg(list,op,size,a,tmpreg);
  1490. a_load_reg_ref(list,size,size,tmpreg,ref);
  1491. end;
  1492. procedure tcg.a_op_const_loc(list : TAsmList; Op: TOpCG; a: tcgint; const loc: tlocation);
  1493. begin
  1494. case loc.loc of
  1495. LOC_REGISTER, LOC_CREGISTER:
  1496. a_op_const_reg(list,op,loc.size,a,loc.register);
  1497. LOC_REFERENCE, LOC_CREFERENCE:
  1498. a_op_const_ref(list,op,loc.size,a,loc.reference);
  1499. else
  1500. internalerror(200109061);
  1501. end;
  1502. end;
  1503. procedure tcg.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1504. var
  1505. tmpreg : tregister;
  1506. begin
  1507. tmpreg:=getintregister(list,size);
  1508. a_load_ref_reg(list,size,size,ref,tmpreg);
  1509. a_op_reg_reg(list,op,size,reg,tmpreg);
  1510. a_load_reg_ref(list,size,size,tmpreg,ref);
  1511. end;
  1512. procedure tcg.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1513. var
  1514. tmpreg: tregister;
  1515. begin
  1516. case op of
  1517. OP_NOT,OP_NEG:
  1518. { handle it as "load ref,reg; op reg" }
  1519. begin
  1520. a_load_ref_reg(list,size,size,ref,reg);
  1521. a_op_reg_reg(list,op,size,reg,reg);
  1522. end;
  1523. else
  1524. begin
  1525. tmpreg:=getintregister(list,size);
  1526. a_load_ref_reg(list,size,size,ref,tmpreg);
  1527. a_op_reg_reg(list,op,size,tmpreg,reg);
  1528. end;
  1529. end;
  1530. end;
  1531. procedure tcg.a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  1532. begin
  1533. case loc.loc of
  1534. LOC_REGISTER, LOC_CREGISTER:
  1535. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  1536. LOC_REFERENCE, LOC_CREFERENCE:
  1537. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  1538. else
  1539. internalerror(200109061);
  1540. end;
  1541. end;
  1542. procedure tcg.a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  1543. var
  1544. tmpreg: tregister;
  1545. begin
  1546. case loc.loc of
  1547. LOC_REGISTER,LOC_CREGISTER:
  1548. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  1549. LOC_REFERENCE,LOC_CREFERENCE:
  1550. begin
  1551. tmpreg:=getintregister(list,loc.size);
  1552. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  1553. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  1554. end;
  1555. else
  1556. internalerror(200109061);
  1557. end;
  1558. end;
  1559. procedure Tcg.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1560. a:tcgint;src,dst:Tregister);
  1561. begin
  1562. a_load_reg_reg(list,size,size,src,dst);
  1563. a_op_const_reg(list,op,size,a,dst);
  1564. end;
  1565. procedure tcg.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1566. size: tcgsize; src1, src2, dst: tregister);
  1567. var
  1568. tmpreg: tregister;
  1569. begin
  1570. if (dst<>src1) then
  1571. begin
  1572. a_load_reg_reg(list,size,size,src2,dst);
  1573. a_op_reg_reg(list,op,size,src1,dst);
  1574. end
  1575. else
  1576. begin
  1577. { can we do a direct operation on the target register ? }
  1578. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  1579. a_op_reg_reg(list,op,size,src2,dst)
  1580. else
  1581. begin
  1582. tmpreg:=getintregister(list,size);
  1583. a_load_reg_reg(list,size,size,src2,tmpreg);
  1584. a_op_reg_reg(list,op,size,src1,tmpreg);
  1585. a_load_reg_reg(list,size,size,tmpreg,dst);
  1586. end;
  1587. end;
  1588. end;
  1589. procedure tcg.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1590. begin
  1591. a_op_const_reg_reg(list,op,size,a,src,dst);
  1592. ovloc.loc:=LOC_VOID;
  1593. end;
  1594. procedure tcg.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1595. begin
  1596. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1597. ovloc.loc:=LOC_VOID;
  1598. end;
  1599. procedure tcg.a_cmp_const_reg_label(list: TAsmList; size: tcgsize;
  1600. cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1601. var
  1602. tmpreg: tregister;
  1603. begin
  1604. tmpreg:=getintregister(list,size);
  1605. a_load_const_reg(list,size,a,tmpreg);
  1606. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1607. end;
  1608. procedure tcg.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  1609. l : tasmlabel);
  1610. var
  1611. tmpreg: tregister;
  1612. begin
  1613. tmpreg:=getintregister(list,size);
  1614. a_load_ref_reg(list,size,size,ref,tmpreg);
  1615. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1616. end;
  1617. procedure tcg.a_cmp_const_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const loc : tlocation;
  1618. l : tasmlabel);
  1619. begin
  1620. case loc.loc of
  1621. LOC_REGISTER,LOC_CREGISTER:
  1622. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1623. LOC_REFERENCE,LOC_CREFERENCE:
  1624. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1625. else
  1626. internalerror(200109061);
  1627. end;
  1628. end;
  1629. procedure tcg.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  1630. var
  1631. tmpreg: tregister;
  1632. begin
  1633. tmpreg:=getintregister(list,size);
  1634. a_load_ref_reg(list,size,size,ref,tmpreg);
  1635. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1636. end;
  1637. procedure tcg.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg : tregister; const ref: treference; l : tasmlabel);
  1638. var
  1639. tmpreg: tregister;
  1640. begin
  1641. tmpreg:=getintregister(list,size);
  1642. a_load_ref_reg(list,size,size,ref,tmpreg);
  1643. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  1644. end;
  1645. procedure tcg.a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  1646. begin
  1647. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  1648. end;
  1649. procedure tcg.a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  1650. begin
  1651. case loc.loc of
  1652. LOC_REGISTER,
  1653. LOC_CREGISTER:
  1654. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1655. LOC_REFERENCE,
  1656. LOC_CREFERENCE :
  1657. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1658. LOC_CONSTANT:
  1659. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1660. else
  1661. internalerror(200203231);
  1662. end;
  1663. end;
  1664. procedure tcg.a_cmp_ref_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1665. l : tasmlabel);
  1666. var
  1667. tmpreg: tregister;
  1668. begin
  1669. case loc.loc of
  1670. LOC_REGISTER,LOC_CREGISTER:
  1671. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1672. LOC_REFERENCE,LOC_CREFERENCE:
  1673. begin
  1674. tmpreg:=getintregister(list,size);
  1675. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1676. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1677. end;
  1678. else
  1679. internalerror(200109061);
  1680. end;
  1681. end;
  1682. procedure tcg.a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  1683. begin
  1684. case loc.loc of
  1685. LOC_MMREGISTER,LOC_CMMREGISTER:
  1686. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1687. LOC_REFERENCE,LOC_CREFERENCE:
  1688. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  1689. LOC_REGISTER,LOC_CREGISTER:
  1690. a_loadmm_intreg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1691. else
  1692. internalerror(200310121);
  1693. end;
  1694. end;
  1695. procedure tcg.a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  1696. begin
  1697. case loc.loc of
  1698. LOC_MMREGISTER,LOC_CMMREGISTER:
  1699. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  1700. LOC_REFERENCE,LOC_CREFERENCE:
  1701. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  1702. else
  1703. internalerror(200310122);
  1704. end;
  1705. end;
  1706. procedure tcg.a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle);
  1707. var
  1708. href : treference;
  1709. {$ifndef cpu64bitalu}
  1710. tmpreg : tregister;
  1711. reg64 : tregister64;
  1712. {$endif not cpu64bitalu}
  1713. begin
  1714. {$ifndef cpu64bitalu}
  1715. if not(cgpara.location^.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  1716. (size<>OS_F64) then
  1717. {$endif not cpu64bitalu}
  1718. cgpara.check_simple_location;
  1719. paramanager.alloccgpara(list,cgpara);
  1720. case cgpara.location^.loc of
  1721. LOC_MMREGISTER,LOC_CMMREGISTER:
  1722. a_loadmm_reg_reg(list,size,cgpara.location^.size,reg,cgpara.location^.register,shuffle);
  1723. LOC_REFERENCE,LOC_CREFERENCE:
  1724. begin
  1725. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1726. a_loadmm_reg_ref(list,size,cgpara.location^.size,reg,href,shuffle);
  1727. end;
  1728. LOC_REGISTER,LOC_CREGISTER:
  1729. begin
  1730. if assigned(shuffle) and
  1731. not shufflescalar(shuffle) then
  1732. internalerror(2009112510);
  1733. {$ifndef cpu64bitalu}
  1734. if (size=OS_F64) then
  1735. begin
  1736. if not assigned(cgpara.location^.next) or
  1737. assigned(cgpara.location^.next^.next) then
  1738. internalerror(2009112512);
  1739. case cgpara.location^.next^.loc of
  1740. LOC_REGISTER,LOC_CREGISTER:
  1741. tmpreg:=cgpara.location^.next^.register;
  1742. LOC_REFERENCE,LOC_CREFERENCE:
  1743. tmpreg:=getintregister(list,OS_32);
  1744. else
  1745. internalerror(2009112910);
  1746. end;
  1747. if (target_info.endian=ENDIAN_BIG) then
  1748. begin
  1749. { paraloc^ -> high
  1750. paraloc^.next -> low }
  1751. reg64.reghi:=cgpara.location^.register;
  1752. reg64.reglo:=tmpreg;
  1753. end
  1754. else
  1755. begin
  1756. { paraloc^ -> low
  1757. paraloc^.next -> high }
  1758. reg64.reglo:=cgpara.location^.register;
  1759. reg64.reghi:=tmpreg;
  1760. end;
  1761. cg64.a_loadmm_reg_intreg64(list,size,reg,reg64);
  1762. if (cgpara.location^.next^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1763. begin
  1764. if not(cgpara.location^.next^.size in [OS_32,OS_S32]) then
  1765. internalerror(2009112911);
  1766. reference_reset_base(href,cgpara.location^.next^.reference.index,cgpara.location^.next^.reference.offset,cgpara.alignment);
  1767. a_load_reg_ref(list,OS_32,cgpara.location^.next^.size,tmpreg,href);
  1768. end;
  1769. end
  1770. else
  1771. {$endif not cpu64bitalu}
  1772. a_loadmm_reg_intreg(list,size,cgpara.location^.size,reg,cgpara.location^.register,mms_movescalar);
  1773. end
  1774. else
  1775. internalerror(200310123);
  1776. end;
  1777. end;
  1778. procedure tcg.a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize;const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle);
  1779. var
  1780. hr : tregister;
  1781. hs : tmmshuffle;
  1782. begin
  1783. cgpara.check_simple_location;
  1784. hr:=getmmregister(list,cgpara.location^.size);
  1785. a_loadmm_ref_reg(list,size,cgpara.location^.size,ref,hr,shuffle);
  1786. if realshuffle(shuffle) then
  1787. begin
  1788. hs:=shuffle^;
  1789. removeshuffles(hs);
  1790. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,@hs);
  1791. end
  1792. else
  1793. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,shuffle);
  1794. end;
  1795. procedure tcg.a_loadmm_loc_cgpara(list: TAsmList;const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle);
  1796. begin
  1797. case loc.loc of
  1798. LOC_MMREGISTER,LOC_CMMREGISTER:
  1799. a_loadmm_reg_cgpara(list,loc.size,loc.register,cgpara,shuffle);
  1800. LOC_REFERENCE,LOC_CREFERENCE:
  1801. a_loadmm_ref_cgpara(list,loc.size,loc.reference,cgpara,shuffle);
  1802. else
  1803. internalerror(200310123);
  1804. end;
  1805. end;
  1806. procedure tcg.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1807. var
  1808. hr : tregister;
  1809. hs : tmmshuffle;
  1810. begin
  1811. hr:=getmmregister(list,size);
  1812. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1813. if realshuffle(shuffle) then
  1814. begin
  1815. hs:=shuffle^;
  1816. removeshuffles(hs);
  1817. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  1818. end
  1819. else
  1820. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  1821. end;
  1822. procedure tcg.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  1823. var
  1824. hr : tregister;
  1825. hs : tmmshuffle;
  1826. begin
  1827. hr:=getmmregister(list,size);
  1828. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1829. if realshuffle(shuffle) then
  1830. begin
  1831. hs:=shuffle^;
  1832. removeshuffles(hs);
  1833. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  1834. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  1835. end
  1836. else
  1837. begin
  1838. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  1839. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  1840. end;
  1841. end;
  1842. procedure tcg.a_loadmm_intreg_reg(list: tasmlist; fromsize,tosize: tcgsize; intreg,mmreg: tregister; shuffle: pmmshuffle);
  1843. var
  1844. tmpref: treference;
  1845. begin
  1846. if (tcgsize2size[fromsize]<>4) or
  1847. (tcgsize2size[tosize]<>4) then
  1848. internalerror(2009112503);
  1849. tg.gettemp(list,4,4,tt_normal,tmpref);
  1850. a_load_reg_ref(list,fromsize,fromsize,intreg,tmpref);
  1851. a_loadmm_ref_reg(list,tosize,tosize,tmpref,mmreg,shuffle);
  1852. tg.ungettemp(list,tmpref);
  1853. end;
  1854. procedure tcg.a_loadmm_reg_intreg(list: tasmlist; fromsize,tosize: tcgsize; mmreg,intreg: tregister; shuffle: pmmshuffle);
  1855. var
  1856. tmpref: treference;
  1857. begin
  1858. if (tcgsize2size[fromsize]<>4) or
  1859. (tcgsize2size[tosize]<>4) then
  1860. internalerror(2009112504);
  1861. tg.gettemp(list,8,8,tt_normal,tmpref);
  1862. a_loadmm_reg_ref(list,fromsize,fromsize,mmreg,tmpref,shuffle);
  1863. a_load_ref_reg(list,tosize,tosize,tmpref,intreg);
  1864. tg.ungettemp(list,tmpref);
  1865. end;
  1866. procedure tcg.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  1867. begin
  1868. case loc.loc of
  1869. LOC_CMMREGISTER,LOC_MMREGISTER:
  1870. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  1871. LOC_CREFERENCE,LOC_REFERENCE:
  1872. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  1873. else
  1874. internalerror(200312232);
  1875. end;
  1876. end;
  1877. procedure tcg.a_opmm_loc_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; src,dst: tregister;shuffle : pmmshuffle);
  1878. begin
  1879. case loc.loc of
  1880. LOC_CMMREGISTER,LOC_MMREGISTER:
  1881. a_opmm_reg_reg_reg(list,op,size,loc.register,src,dst,shuffle);
  1882. LOC_CREFERENCE,LOC_REFERENCE:
  1883. a_opmm_ref_reg_reg(list,op,size,loc.reference,src,dst,shuffle);
  1884. else
  1885. internalerror(200312232);
  1886. end;
  1887. end;
  1888. procedure tcg.a_opmm_reg_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;
  1889. src1,src2,dst : tregister;shuffle : pmmshuffle);
  1890. begin
  1891. internalerror(2013061102);
  1892. end;
  1893. procedure tcg.a_opmm_ref_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;
  1894. const ref : treference;src,dst : tregister;shuffle : pmmshuffle);
  1895. begin
  1896. internalerror(2013061101);
  1897. end;
  1898. procedure tcg.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  1899. begin
  1900. g_concatcopy(list,source,dest,len);
  1901. end;
  1902. procedure tcg.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  1903. begin
  1904. g_overflowCheck(list,loc,def);
  1905. end;
  1906. {$ifdef cpuflags}
  1907. procedure tcg.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference);
  1908. var
  1909. tmpreg : tregister;
  1910. begin
  1911. tmpreg:=getintregister(list,size);
  1912. g_flags2reg(list,size,f,tmpreg);
  1913. a_load_reg_ref(list,size,size,tmpreg,ref);
  1914. end;
  1915. {$endif cpuflags}
  1916. procedure tcg.g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  1917. var
  1918. hrefvmt : treference;
  1919. cgpara1,cgpara2 : TCGPara;
  1920. pd: tprocdef;
  1921. begin
  1922. cgpara1.init;
  1923. cgpara2.init;
  1924. if (cs_check_object in current_settings.localswitches) then
  1925. begin
  1926. pd:=search_system_proc('fpc_check_object_ext');
  1927. paramanager.getintparaloc(pd,1,cgpara1);
  1928. paramanager.getintparaloc(pd,2,cgpara2);
  1929. reference_reset_symbol(hrefvmt,current_asmdata.RefAsmSymbol(objdef.vmt_mangledname,AT_DATA),0,sizeof(pint));
  1930. if pd.is_pushleftright then
  1931. begin
  1932. a_load_reg_cgpara(list,OS_ADDR,reg,cgpara1);
  1933. a_loadaddr_ref_cgpara(list,hrefvmt,cgpara2);
  1934. end
  1935. else
  1936. begin
  1937. a_loadaddr_ref_cgpara(list,hrefvmt,cgpara2);
  1938. a_load_reg_cgpara(list,OS_ADDR,reg,cgpara1);
  1939. end;
  1940. paramanager.freecgpara(list,cgpara1);
  1941. paramanager.freecgpara(list,cgpara2);
  1942. allocallcpuregisters(list);
  1943. a_call_name(list,'fpc_check_object_ext',false);
  1944. deallocallcpuregisters(list);
  1945. end
  1946. else
  1947. if (cs_check_range in current_settings.localswitches) then
  1948. begin
  1949. pd:=search_system_proc('fpc_check_object');
  1950. paramanager.getintparaloc(pd,1,cgpara1);
  1951. a_load_reg_cgpara(list,OS_ADDR,reg,cgpara1);
  1952. paramanager.freecgpara(list,cgpara1);
  1953. allocallcpuregisters(list);
  1954. a_call_name(list,'fpc_check_object',false);
  1955. deallocallcpuregisters(list);
  1956. end;
  1957. cgpara1.done;
  1958. cgpara2.done;
  1959. end;
  1960. {*****************************************************************************
  1961. Entry/Exit Code Functions
  1962. *****************************************************************************}
  1963. procedure tcg.g_save_registers(list:TAsmList);
  1964. var
  1965. href : treference;
  1966. size : longint;
  1967. r : integer;
  1968. begin
  1969. { calculate temp. size }
  1970. size:=0;
  1971. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1972. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1973. inc(size,sizeof(aint));
  1974. if uses_registers(R_ADDRESSREGISTER) then
  1975. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1976. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1977. inc(size,sizeof(aint));
  1978. { mm registers }
  1979. if uses_registers(R_MMREGISTER) then
  1980. begin
  1981. { Make sure we reserve enough space to do the alignment based on the offset
  1982. later on. We can't use the size for this, because the alignment of the start
  1983. of the temp is smaller than needed for an OS_VECTOR }
  1984. inc(size,tcgsize2size[OS_VECTOR]);
  1985. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  1986. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  1987. inc(size,tcgsize2size[OS_VECTOR]);
  1988. end;
  1989. if size>0 then
  1990. begin
  1991. tg.GetTemp(list,size,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  1992. include(current_procinfo.flags,pi_has_saved_regs);
  1993. { Copy registers to temp }
  1994. href:=current_procinfo.save_regs_ref;
  1995. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1996. begin
  1997. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1998. begin
  1999. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE),href);
  2000. inc(href.offset,sizeof(aint));
  2001. end;
  2002. include(rg[R_INTREGISTER].preserved_by_proc,saved_standard_registers[r]);
  2003. end;
  2004. if uses_registers(R_ADDRESSREGISTER) then
  2005. for r:=low(saved_address_registers) to high(saved_address_registers) do
  2006. begin
  2007. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2008. begin
  2009. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE),href);
  2010. inc(href.offset,sizeof(aint));
  2011. end;
  2012. include(rg[R_ADDRESSREGISTER].preserved_by_proc,saved_address_registers[r]);
  2013. end;
  2014. if uses_registers(R_MMREGISTER) then
  2015. begin
  2016. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  2017. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  2018. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  2019. begin
  2020. { the array has to be declared even if no MM registers are saved
  2021. (such as with SSE on i386), and since 0-element arrays don't
  2022. exist, they contain a single RS_INVALID element in that case
  2023. }
  2024. if saved_mm_registers[r]<>RS_INVALID then
  2025. begin
  2026. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  2027. begin
  2028. a_loadmm_reg_ref(list,OS_VECTOR,OS_VECTOR,newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBMMWHOLE),href,nil);
  2029. inc(href.offset,tcgsize2size[OS_VECTOR]);
  2030. end;
  2031. include(rg[R_MMREGISTER].preserved_by_proc,saved_mm_registers[r]);
  2032. end;
  2033. end;
  2034. end;
  2035. end;
  2036. end;
  2037. procedure tcg.g_restore_registers(list:TAsmList);
  2038. var
  2039. href : treference;
  2040. r : integer;
  2041. hreg : tregister;
  2042. begin
  2043. if not(pi_has_saved_regs in current_procinfo.flags) then
  2044. exit;
  2045. { Copy registers from temp }
  2046. href:=current_procinfo.save_regs_ref;
  2047. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  2048. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2049. begin
  2050. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  2051. { Allocate register so the optimizer does not remove the load }
  2052. a_reg_alloc(list,hreg);
  2053. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2054. inc(href.offset,sizeof(aint));
  2055. end;
  2056. if uses_registers(R_ADDRESSREGISTER) then
  2057. for r:=low(saved_address_registers) to high(saved_address_registers) do
  2058. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2059. begin
  2060. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  2061. { Allocate register so the optimizer does not remove the load }
  2062. a_reg_alloc(list,hreg);
  2063. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2064. inc(href.offset,sizeof(aint));
  2065. end;
  2066. if uses_registers(R_MMREGISTER) then
  2067. begin
  2068. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  2069. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  2070. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  2071. begin
  2072. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  2073. begin
  2074. hreg:=newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBMMWHOLE);
  2075. { Allocate register so the optimizer does not remove the load }
  2076. a_reg_alloc(list,hreg);
  2077. a_loadmm_ref_reg(list,OS_VECTOR,OS_VECTOR,href,hreg,nil);
  2078. inc(href.offset,tcgsize2size[OS_VECTOR]);
  2079. end;
  2080. end;
  2081. end;
  2082. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  2083. end;
  2084. procedure tcg.g_profilecode(list : TAsmList);
  2085. begin
  2086. end;
  2087. procedure tcg.g_exception_reason_save(list : TAsmList; const href : treference);
  2088. begin
  2089. a_load_reg_ref(list, OS_INT, OS_INT, NR_FUNCTION_RESULT_REG, href);
  2090. end;
  2091. procedure tcg.g_exception_reason_save_const(list : TAsmList; const href : treference; a: tcgint);
  2092. begin
  2093. a_load_const_ref(list, OS_INT, a, href);
  2094. end;
  2095. procedure tcg.g_exception_reason_load(list : TAsmList; const href : treference);
  2096. begin
  2097. a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  2098. a_load_ref_reg(list, OS_INT, OS_INT, href, NR_FUNCTION_RESULT_REG);
  2099. end;
  2100. procedure tcg.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  2101. var
  2102. hsym : tsym;
  2103. href : treference;
  2104. paraloc : Pcgparalocation;
  2105. begin
  2106. { calculate the parameter info for the procdef }
  2107. procdef.init_paraloc_info(callerside);
  2108. hsym:=tsym(procdef.parast.Find('self'));
  2109. if not(assigned(hsym) and
  2110. (hsym.typ=paravarsym)) then
  2111. internalerror(200305251);
  2112. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  2113. while paraloc<>nil do
  2114. with paraloc^ do
  2115. begin
  2116. case loc of
  2117. LOC_REGISTER:
  2118. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  2119. LOC_REFERENCE:
  2120. begin
  2121. { offset in the wrapper needs to be adjusted for the stored
  2122. return address }
  2123. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),sizeof(pint));
  2124. a_op_const_ref(list,OP_SUB,size,ioffset,href);
  2125. end
  2126. else
  2127. internalerror(200309189);
  2128. end;
  2129. paraloc:=next;
  2130. end;
  2131. end;
  2132. procedure tcg.g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string);
  2133. begin
  2134. a_jmp_name(list,externalname);
  2135. end;
  2136. procedure tcg.a_call_name_static(list : TAsmList;const s : string);
  2137. begin
  2138. a_call_name(list,s,false);
  2139. end;
  2140. function tcg.g_indirect_sym_load(list:TAsmList;const symname: string; const flags: tindsymflags): tregister;
  2141. var
  2142. l: tasmsymbol;
  2143. ref: treference;
  2144. nlsymname: string;
  2145. begin
  2146. result := NR_NO;
  2147. case target_info.system of
  2148. system_powerpc_darwin,
  2149. system_i386_darwin,
  2150. system_i386_iphonesim,
  2151. system_powerpc64_darwin,
  2152. system_arm_darwin:
  2153. begin
  2154. nlsymname:='L'+symname+'$non_lazy_ptr';
  2155. l:=current_asmdata.getasmsymbol(nlsymname);
  2156. if not(assigned(l)) then
  2157. begin
  2158. new_section(current_asmdata.asmlists[al_picdata],sec_data_nonlazy,'',sizeof(pint));
  2159. l:=current_asmdata.DefineAsmSymbol(nlsymname,AB_LOCAL,AT_DATA);
  2160. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create(l,0));
  2161. if not(is_weak in flags) then
  2162. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.RefAsmSymbol(symname).Name))
  2163. else
  2164. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.WeakRefAsmSymbol(symname).Name));
  2165. {$ifdef cpu64bitaddr}
  2166. current_asmdata.asmlists[al_picdata].concat(tai_const.create_64bit(0));
  2167. {$else cpu64bitaddr}
  2168. current_asmdata.asmlists[al_picdata].concat(tai_const.create_32bit(0));
  2169. {$endif cpu64bitaddr}
  2170. end;
  2171. result := getaddressregister(list);
  2172. reference_reset_symbol(ref,l,0,sizeof(pint));
  2173. { a_load_ref_reg will turn this into a pic-load if needed }
  2174. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,result);
  2175. end;
  2176. end;
  2177. end;
  2178. procedure tcg.g_maybe_got_init(list: TAsmList);
  2179. begin
  2180. end;
  2181. procedure tcg.g_call(list: TAsmList;const s: string);
  2182. begin
  2183. allocallcpuregisters(list);
  2184. a_call_name(list,s,false);
  2185. deallocallcpuregisters(list);
  2186. end;
  2187. procedure tcg.g_local_unwind(list: TAsmList; l: TAsmLabel);
  2188. begin
  2189. a_jmp_always(list,l);
  2190. end;
  2191. procedure tcg.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  2192. begin
  2193. internalerror(200807231);
  2194. end;
  2195. procedure tcg.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2196. begin
  2197. internalerror(200807232);
  2198. end;
  2199. procedure tcg.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2200. begin
  2201. internalerror(200807233);
  2202. end;
  2203. procedure tcg.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  2204. begin
  2205. internalerror(200807234);
  2206. end;
  2207. function tcg.getflagregister(list: TAsmList; size: Tcgsize): Tregister;
  2208. begin
  2209. Result:=TRegister(0);
  2210. internalerror(200807238);
  2211. end;
  2212. procedure tcg.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: tcgsize; src, dst: TRegister);
  2213. begin
  2214. internalerror(2014070601);
  2215. end;
  2216. procedure tcg.g_stackpointer_alloc(list: TAsmList; size: longint);
  2217. begin
  2218. internalerror(2014070602);
  2219. end;
  2220. procedure tcg.a_mul_reg_reg_pair(list: TAsmList; size: TCgSize; src1,src2,dstlo,dsthi: TRegister);
  2221. begin
  2222. internalerror(2014060801);
  2223. end;
  2224. procedure tcg.g_div_const_reg_reg(list:tasmlist; size: TCgSize; a: tcgint; src,dst: tregister);
  2225. var
  2226. divreg: tregister;
  2227. magic: aInt;
  2228. u_magic: aWord;
  2229. u_shift: byte;
  2230. u_add: boolean;
  2231. begin
  2232. divreg:=getintregister(list,OS_INT);
  2233. if (size in [OS_S32,OS_S64]) then
  2234. begin
  2235. calc_divconst_magic_signed(tcgsize2size[size]*8,a,magic,u_shift);
  2236. { load magic value }
  2237. a_load_const_reg(list,OS_INT,magic,divreg);
  2238. { multiply, discarding low bits }
  2239. a_mul_reg_reg_pair(list,size,src,divreg,NR_NO,dst);
  2240. { add/subtract numerator }
  2241. if (a>0) and (magic<0) then
  2242. a_op_reg_reg_reg(list,OP_ADD,OS_INT,src,dst,dst)
  2243. else if (a<0) and (magic>0) then
  2244. a_op_reg_reg_reg(list,OP_SUB,OS_INT,src,dst,dst);
  2245. { shift shift places to the right (arithmetic) }
  2246. a_op_const_reg_reg(list,OP_SAR,OS_INT,u_shift,dst,dst);
  2247. { extract and add sign bit }
  2248. if (a>=0) then
  2249. a_op_const_reg_reg(list,OP_SHR,OS_INT,tcgsize2size[size]*8-1,src,divreg)
  2250. else
  2251. a_op_const_reg_reg(list,OP_SHR,OS_INT,tcgsize2size[size]*8-1,dst,divreg);
  2252. a_op_reg_reg_reg(list,OP_ADD,OS_INT,dst,divreg,dst);
  2253. end
  2254. else if (size in [OS_32,OS_64]) then
  2255. begin
  2256. calc_divconst_magic_unsigned(tcgsize2size[size]*8,a,u_magic,u_add,u_shift);
  2257. { load magic in divreg }
  2258. a_load_const_reg(list,OS_INT,tcgint(u_magic),divreg);
  2259. { multiply, discarding low bits }
  2260. a_mul_reg_reg_pair(list,size,src,divreg,NR_NO,dst);
  2261. if (u_add) then
  2262. begin
  2263. { Calculate "(numerator+result) shr u_shift", avoiding possible overflow }
  2264. a_op_reg_reg_reg(list,OP_SUB,OS_INT,dst,src,divreg);
  2265. { divreg=(numerator-result) }
  2266. a_op_const_reg_reg(list,OP_SHR,OS_INT,1,divreg,divreg);
  2267. { divreg=(numerator-result)/2 }
  2268. a_op_reg_reg_reg(list,OP_ADD,OS_INT,divreg,dst,divreg);
  2269. { divreg=(numerator+result)/2, already shifted by 1, so decrease u_shift. }
  2270. a_op_const_reg_reg(list,OP_SHR,OS_INT,u_shift-1,divreg,dst);
  2271. end
  2272. else
  2273. a_op_const_reg_reg(list,OP_SHR,OS_INT,u_shift,dst,dst);
  2274. end
  2275. else
  2276. InternalError(2014060601);
  2277. end;
  2278. {*****************************************************************************
  2279. TCG64
  2280. *****************************************************************************}
  2281. {$ifndef cpu64bitalu}
  2282. function joinreg64(reglo,reghi : tregister) : tregister64;
  2283. begin
  2284. result.reglo:=reglo;
  2285. result.reghi:=reghi;
  2286. end;
  2287. procedure tcg64.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64; regsrc,regdst : tregister64);
  2288. begin
  2289. a_load64_reg_reg(list,regsrc,regdst);
  2290. a_op64_const_reg(list,op,size,value,regdst);
  2291. end;
  2292. procedure tcg64.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  2293. var
  2294. tmpreg64 : tregister64;
  2295. begin
  2296. { when src1=dst then we need to first create a temp to prevent
  2297. overwriting src1 with src2 }
  2298. if (regsrc1.reghi=regdst.reghi) or
  2299. (regsrc1.reglo=regdst.reghi) or
  2300. (regsrc1.reghi=regdst.reglo) or
  2301. (regsrc1.reglo=regdst.reglo) then
  2302. begin
  2303. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2304. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2305. a_load64_reg_reg(list,regsrc2,tmpreg64);
  2306. a_op64_reg_reg(list,op,size,regsrc1,tmpreg64);
  2307. a_load64_reg_reg(list,tmpreg64,regdst);
  2308. end
  2309. else
  2310. begin
  2311. a_load64_reg_reg(list,regsrc2,regdst);
  2312. a_op64_reg_reg(list,op,size,regsrc1,regdst);
  2313. end;
  2314. end;
  2315. procedure tcg64.a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  2316. var
  2317. tmpreg64 : tregister64;
  2318. begin
  2319. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2320. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2321. a_load64_subsetref_reg(list,sref,tmpreg64);
  2322. a_op64_const_reg(list,op,size,a,tmpreg64);
  2323. a_load64_reg_subsetref(list,tmpreg64,sref);
  2324. end;
  2325. procedure tcg64.a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  2326. var
  2327. tmpreg64 : tregister64;
  2328. begin
  2329. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2330. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2331. a_load64_subsetref_reg(list,sref,tmpreg64);
  2332. a_op64_reg_reg(list,op,size,reg,tmpreg64);
  2333. a_load64_reg_subsetref(list,tmpreg64,sref);
  2334. end;
  2335. procedure tcg64.a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  2336. var
  2337. tmpreg64 : tregister64;
  2338. begin
  2339. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2340. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2341. a_load64_subsetref_reg(list,sref,tmpreg64);
  2342. a_op64_ref_reg(list,op,size,ref,tmpreg64);
  2343. a_load64_reg_subsetref(list,tmpreg64,sref);
  2344. end;
  2345. procedure tcg64.a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  2346. var
  2347. tmpreg64 : tregister64;
  2348. begin
  2349. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2350. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2351. a_load64_subsetref_reg(list,ssref,tmpreg64);
  2352. a_op64_reg_subsetref(list,op,size,tmpreg64,dsref);
  2353. end;
  2354. procedure tcg64.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2355. begin
  2356. a_op64_const_reg_reg(list,op,size,value,regsrc,regdst);
  2357. ovloc.loc:=LOC_VOID;
  2358. end;
  2359. procedure tcg64.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2360. begin
  2361. a_op64_reg_reg_reg(list,op,size,regsrc1,regsrc2,regdst);
  2362. ovloc.loc:=LOC_VOID;
  2363. end;
  2364. procedure tcg64.a_load64_loc_subsetref(list : TAsmList;const l: tlocation; const sref : tsubsetreference);
  2365. begin
  2366. case l.loc of
  2367. LOC_REFERENCE, LOC_CREFERENCE:
  2368. a_load64_ref_subsetref(list,l.reference,sref);
  2369. LOC_REGISTER,LOC_CREGISTER:
  2370. a_load64_reg_subsetref(list,l.register64,sref);
  2371. LOC_CONSTANT :
  2372. a_load64_const_subsetref(list,l.value64,sref);
  2373. LOC_SUBSETREF,LOC_CSUBSETREF:
  2374. a_load64_subsetref_subsetref(list,l.sref,sref);
  2375. else
  2376. internalerror(2006082210);
  2377. end;
  2378. end;
  2379. procedure tcg64.a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  2380. begin
  2381. case l.loc of
  2382. LOC_REFERENCE, LOC_CREFERENCE:
  2383. a_load64_subsetref_ref(list,sref,l.reference);
  2384. LOC_REGISTER,LOC_CREGISTER:
  2385. a_load64_subsetref_reg(list,sref,l.register64);
  2386. LOC_SUBSETREF,LOC_CSUBSETREF:
  2387. a_load64_subsetref_subsetref(list,sref,l.sref);
  2388. else
  2389. internalerror(2006082211);
  2390. end;
  2391. end;
  2392. {$else cpu64bitalu}
  2393. function joinreg128(reglo, reghi: tregister): tregister128;
  2394. begin
  2395. result.reglo:=reglo;
  2396. result.reghi:=reghi;
  2397. end;
  2398. procedure splitparaloc128(const cgpara:tcgpara;var cgparalo,cgparahi:tcgpara);
  2399. var
  2400. paraloclo,
  2401. paralochi : pcgparalocation;
  2402. begin
  2403. if not(cgpara.size in [OS_128,OS_S128]) then
  2404. internalerror(2012090604);
  2405. if not assigned(cgpara.location) then
  2406. internalerror(2012090605);
  2407. { init lo/hi para }
  2408. cgparahi.reset;
  2409. if cgpara.size=OS_S128 then
  2410. cgparahi.size:=OS_S64
  2411. else
  2412. cgparahi.size:=OS_64;
  2413. cgparahi.intsize:=8;
  2414. cgparahi.alignment:=cgpara.alignment;
  2415. paralochi:=cgparahi.add_location;
  2416. cgparalo.reset;
  2417. cgparalo.size:=OS_64;
  2418. cgparalo.intsize:=8;
  2419. cgparalo.alignment:=cgpara.alignment;
  2420. paraloclo:=cgparalo.add_location;
  2421. { 2 parameter fields? }
  2422. if assigned(cgpara.location^.next) then
  2423. begin
  2424. { Order for multiple locations is always
  2425. paraloc^ -> high
  2426. paraloc^.next -> low }
  2427. if (target_info.endian=ENDIAN_BIG) then
  2428. begin
  2429. { paraloc^ -> high
  2430. paraloc^.next -> low }
  2431. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2432. move(cgpara.location^.next^,paraloclo^,sizeof(paraloclo^));
  2433. end
  2434. else
  2435. begin
  2436. { paraloc^ -> low
  2437. paraloc^.next -> high }
  2438. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2439. move(cgpara.location^.next^,paralochi^,sizeof(paralochi^));
  2440. end;
  2441. end
  2442. else
  2443. begin
  2444. { single parameter, this can only be in memory }
  2445. if cgpara.location^.loc<>LOC_REFERENCE then
  2446. internalerror(2012090606);
  2447. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2448. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2449. { for big endian low is at +8, for little endian high }
  2450. if target_info.endian = endian_big then
  2451. begin
  2452. inc(cgparalo.location^.reference.offset,8);
  2453. cgparalo.alignment:=newalignment(cgparalo.alignment,8);
  2454. end
  2455. else
  2456. begin
  2457. inc(cgparahi.location^.reference.offset,8);
  2458. cgparahi.alignment:=newalignment(cgparahi.alignment,8);
  2459. end;
  2460. end;
  2461. { fix size }
  2462. paraloclo^.size:=cgparalo.size;
  2463. paraloclo^.next:=nil;
  2464. paralochi^.size:=cgparahi.size;
  2465. paralochi^.next:=nil;
  2466. end;
  2467. procedure tcg128.a_load128_reg_reg(list: TAsmList; regsrc,
  2468. regdst: tregister128);
  2469. begin
  2470. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reglo,regdst.reglo);
  2471. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reghi,regdst.reghi);
  2472. end;
  2473. procedure tcg128.a_load128_reg_ref(list: TAsmList; reg: tregister128;
  2474. const ref: treference);
  2475. var
  2476. tmpreg: tregister;
  2477. tmpref: treference;
  2478. begin
  2479. if target_info.endian = endian_big then
  2480. begin
  2481. tmpreg:=reg.reglo;
  2482. reg.reglo:=reg.reghi;
  2483. reg.reghi:=tmpreg;
  2484. end;
  2485. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reglo,ref);
  2486. tmpref := ref;
  2487. inc(tmpref.offset,8);
  2488. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reghi,tmpref);
  2489. end;
  2490. procedure tcg128.a_load128_ref_reg(list: TAsmList; const ref: treference;
  2491. reg: tregister128);
  2492. var
  2493. tmpreg: tregister;
  2494. tmpref: treference;
  2495. begin
  2496. if target_info.endian = endian_big then
  2497. begin
  2498. tmpreg := reg.reglo;
  2499. reg.reglo := reg.reghi;
  2500. reg.reghi := tmpreg;
  2501. end;
  2502. tmpref := ref;
  2503. if (tmpref.base=reg.reglo) then
  2504. begin
  2505. tmpreg:=cg.getaddressregister(list);
  2506. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.base,tmpreg);
  2507. tmpref.base:=tmpreg;
  2508. end
  2509. else
  2510. { this works only for the i386, thus the i386 needs to override }
  2511. { this method and this method must be replaced by a more generic }
  2512. { implementation FK }
  2513. if (tmpref.index=reg.reglo) then
  2514. begin
  2515. tmpreg:=cg.getaddressregister(list);
  2516. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.index,tmpreg);
  2517. tmpref.index:=tmpreg;
  2518. end;
  2519. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reglo);
  2520. inc(tmpref.offset,8);
  2521. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reghi);
  2522. end;
  2523. procedure tcg128.a_load128_loc_ref(list: TAsmList; const l: tlocation;
  2524. const ref: treference);
  2525. begin
  2526. case l.loc of
  2527. LOC_REGISTER,LOC_CREGISTER:
  2528. a_load128_reg_ref(list,l.register128,ref);
  2529. { not yet implemented:
  2530. LOC_CONSTANT :
  2531. a_load128_const_ref(list,l.value128,ref);
  2532. LOC_SUBSETREF, LOC_CSUBSETREF:
  2533. a_load64_subsetref_ref(list,l.sref,ref); }
  2534. else
  2535. internalerror(201209061);
  2536. end;
  2537. end;
  2538. procedure tcg128.a_load128_reg_loc(list: TAsmList; reg: tregister128;
  2539. const l: tlocation);
  2540. begin
  2541. case l.loc of
  2542. LOC_REFERENCE, LOC_CREFERENCE:
  2543. a_load128_reg_ref(list,reg,l.reference);
  2544. LOC_REGISTER,LOC_CREGISTER:
  2545. a_load128_reg_reg(list,reg,l.register128);
  2546. { not yet implemented:
  2547. LOC_SUBSETREF, LOC_CSUBSETREF:
  2548. a_load64_reg_subsetref(list,reg,l.sref);
  2549. LOC_MMREGISTER, LOC_CMMREGISTER:
  2550. a_loadmm_intreg64_reg(list,l.size,reg,l.register); }
  2551. else
  2552. internalerror(201209062);
  2553. end;
  2554. end;
  2555. procedure tcg128.a_load128_const_reg(list: TAsmList; valuelo,
  2556. valuehi: int64; reg: tregister128);
  2557. begin
  2558. cg.a_load_const_reg(list,OS_64,aint(valuelo),reg.reglo);
  2559. cg.a_load_const_reg(list,OS_64,aint(valuehi),reg.reghi);
  2560. end;
  2561. procedure tcg128.a_load128_loc_cgpara(list: TAsmList; const l: tlocation;
  2562. const paraloc: TCGPara);
  2563. begin
  2564. case l.loc of
  2565. LOC_REGISTER,
  2566. LOC_CREGISTER :
  2567. a_load128_reg_cgpara(list,l.register128,paraloc);
  2568. {not yet implemented:
  2569. LOC_CONSTANT :
  2570. a_load128_const_cgpara(list,l.value64,paraloc);
  2571. }
  2572. LOC_CREFERENCE,
  2573. LOC_REFERENCE :
  2574. a_load128_ref_cgpara(list,l.reference,paraloc);
  2575. else
  2576. internalerror(2012090603);
  2577. end;
  2578. end;
  2579. procedure tcg128.a_load128_reg_cgpara(list : TAsmList;reg : tregister128;const paraloc : tcgpara);
  2580. var
  2581. tmplochi,tmploclo: tcgpara;
  2582. begin
  2583. tmploclo.init;
  2584. tmplochi.init;
  2585. splitparaloc128(paraloc,tmploclo,tmplochi);
  2586. cg.a_load_reg_cgpara(list,OS_64,reg.reghi,tmplochi);
  2587. cg.a_load_reg_cgpara(list,OS_64,reg.reglo,tmploclo);
  2588. tmploclo.done;
  2589. tmplochi.done;
  2590. end;
  2591. procedure tcg128.a_load128_ref_cgpara(list : TAsmList;const r : treference;const paraloc : tcgpara);
  2592. var
  2593. tmprefhi,tmpreflo : treference;
  2594. tmploclo,tmplochi : tcgpara;
  2595. begin
  2596. tmploclo.init;
  2597. tmplochi.init;
  2598. splitparaloc128(paraloc,tmploclo,tmplochi);
  2599. tmprefhi:=r;
  2600. tmpreflo:=r;
  2601. if target_info.endian=endian_big then
  2602. inc(tmpreflo.offset,8)
  2603. else
  2604. inc(tmprefhi.offset,8);
  2605. cg.a_load_ref_cgpara(list,OS_64,tmprefhi,tmplochi);
  2606. cg.a_load_ref_cgpara(list,OS_64,tmpreflo,tmploclo);
  2607. tmploclo.done;
  2608. tmplochi.done;
  2609. end;
  2610. {$endif cpu64bitalu}
  2611. function asmsym2indsymflags(sym: TAsmSymbol): tindsymflags;
  2612. begin
  2613. result:=[];
  2614. if sym.typ<>AT_FUNCTION then
  2615. include(result,is_data);
  2616. if sym.bind=AB_WEAK_EXTERNAL then
  2617. include(result,is_weak);
  2618. end;
  2619. procedure destroy_codegen;
  2620. begin
  2621. cg.free;
  2622. cg:=nil;
  2623. {$ifdef cpu64bitalu}
  2624. cg128.free;
  2625. cg128:=nil;
  2626. {$else cpu64bitalu}
  2627. cg64.free;
  2628. cg64:=nil;
  2629. {$endif cpu64bitalu}
  2630. end;
  2631. end.