cgobj.pas 136 KB

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