cgobj.pas 115 KB

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