cgobj.pas 120 KB

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