cgobj.pas 127 KB

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