cgobj.pas 135 KB

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