cgobj.pas 119 KB

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