cgobj.pas 121 KB

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