cgobj.pas 135 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304
  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. {# Pass the value of a parameter, which can be located either in a register or memory location,
  145. to a routine.
  146. A generic version is provided.
  147. @param(l location of the operand to send)
  148. @param(nr parameter number (starting from one) of routine (from left to right))
  149. @param(cgpara where the parameter will be stored)
  150. }
  151. procedure a_load_loc_cgpara(list : TAsmList;const l : tlocation;const cgpara : TCGPara);
  152. {# Pass the address of a reference to a routine. This routine
  153. will calculate the address of the reference, and pass this
  154. calculated address as a parameter.
  155. It must generate register allocation information for the cgpara in
  156. case it consists of cpuregisters.
  157. A generic version is provided. This routine should
  158. be overridden for optimization purposes if the cpu
  159. permits directly sending this type of parameter.
  160. @param(r reference to get address from)
  161. @param(nr parameter number (starting from one) of routine (from left to right))
  162. }
  163. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : TCGPara);virtual;
  164. {# Load a cgparaloc into a memory reference.
  165. It must generate register allocation information for the cgpara in
  166. case it consists of cpuregisters.
  167. @param(paraloc the source parameter sublocation)
  168. @param(ref the destination reference)
  169. @param(sizeleft indicates the total number of bytes left in all of
  170. the remaining sublocations of this parameter (the current
  171. sublocation and all of the sublocations coming after it).
  172. In case this location is also a reference, it is assumed
  173. to be the final part sublocation of the parameter and that it
  174. contains all of the "sizeleft" bytes).)
  175. @param(align the alignment of the paraloc in case it's a reference)
  176. }
  177. procedure a_load_cgparaloc_ref(list : TAsmList;const paraloc : TCGParaLocation;const ref : treference;sizeleft : tcgint;align : longint);
  178. {# Load a cgparaloc into any kind of register (int, fp, mm).
  179. @param(regsize the size of the destination register)
  180. @param(paraloc the source parameter sublocation)
  181. @param(reg the destination register)
  182. @param(align the alignment of the paraloc in case it's a reference)
  183. }
  184. procedure a_load_cgparaloc_anyreg(list : TAsmList;regsize : tcgsize;const paraloc : TCGParaLocation;reg : tregister;align : longint);
  185. { Remarks:
  186. * If a method specifies a size you have only to take care
  187. of that number of bits, i.e. load_const_reg with OP_8 must
  188. only load the lower 8 bit of the specified register
  189. the rest of the register can be undefined
  190. if necessary the compiler will call a method
  191. to zero or sign extend the register
  192. * The a_load_XX_XX with OP_64 needn't to be
  193. implemented for 32 bit
  194. processors, the code generator takes care of that
  195. * the addr size is for work with the natural pointer
  196. size
  197. * the procedures without fpu/mm are only for integer usage
  198. * normally the first location is the source and the
  199. second the destination
  200. }
  201. {# Emits instruction to call the method specified by symbol name.
  202. This routine must be overridden for each new target cpu.
  203. }
  204. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);virtual; abstract;
  205. procedure a_call_reg(list : TAsmList;reg : tregister);virtual; abstract;
  206. { same as a_call_name, might be overridden on certain architectures to emit
  207. static calls without usage of a got trampoline }
  208. procedure a_call_name_static(list : TAsmList;const s : string);virtual;
  209. { move instructions }
  210. procedure a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);virtual; abstract;
  211. procedure a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : treference);virtual;
  212. procedure a_load_const_loc(list : TAsmList;a : tcgint;const loc : tlocation);
  213. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  214. procedure a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual;
  215. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  216. procedure a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  217. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  218. procedure a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual;
  219. procedure a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);virtual;
  220. procedure a_load_loc_reg(list : TAsmList;tosize: tcgsize; const loc: tlocation; reg : tregister);
  221. procedure a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  222. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);virtual; abstract;
  223. { bit scan instructions }
  224. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tcgsize; src, dst: TRegister); virtual;
  225. { Multiplication with doubling result size.
  226. dstlo or dsthi may be NR_NO, in which case corresponding half of result is discarded. }
  227. procedure a_mul_reg_reg_pair(list: TAsmList; size: tcgsize; src1,src2,dstlo,dsthi: TRegister);virtual;
  228. { fpu move instructions }
  229. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize:tcgsize; reg1, reg2: tregister); virtual; abstract;
  230. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  231. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  232. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tcgsize; const ref1,ref2: treference);
  233. procedure a_loadfpu_loc_reg(list: TAsmList; tosize: tcgsize; const loc: tlocation; const reg: tregister);
  234. procedure a_loadfpu_reg_loc(list: TAsmList; fromsize: tcgsize; const reg: tregister; const loc: tlocation);
  235. procedure a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);virtual;
  236. procedure a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);virtual;
  237. procedure a_loadfpu_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, fpureg: tregister); virtual;
  238. { vector register move instructions }
  239. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); virtual;
  240. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  241. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual;
  242. procedure a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  243. procedure a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  244. procedure a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  245. procedure a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  246. procedure a_loadmm_loc_cgpara(list: TAsmList; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  247. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle); virtual;
  248. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  249. procedure a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  250. procedure a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  251. procedure a_opmm_loc_reg_reg(list: TAsmList;Op : TOpCG;size : tcgsize;const loc : tlocation;src,dst : tregister;shuffle : pmmshuffle); virtual;
  252. procedure a_opmm_reg_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src1,src2,dst: tregister;shuffle : pmmshuffle); virtual;
  253. procedure a_opmm_ref_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; src,dst: tregister;shuffle : pmmshuffle); virtual;
  254. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle); virtual;
  255. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize; mmreg, intreg: tregister; shuffle : pmmshuffle); virtual;
  256. { basic arithmetic operations }
  257. { note: for operators which require only one argument (not, neg), use }
  258. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  259. { that in this case the *second* operand is used as both source and }
  260. { destination (JM) }
  261. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); virtual; abstract;
  262. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); virtual;
  263. procedure a_op_const_loc(list : TAsmList; Op: TOpCG; a: tcgint; const loc: tlocation);
  264. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  265. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  266. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  267. procedure a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  268. procedure a_op_loc_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const loc: tlocation; reg: tregister);
  269. procedure a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  270. { trinary operations for processors that support them, 'emulated' }
  271. { on others. None with "ref" arguments since I don't think there }
  272. { are any processors that support it (JM) }
  273. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister); virtual;
  274. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister); virtual;
  275. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  276. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  277. { comparison operations }
  278. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  279. l : tasmlabel); virtual;
  280. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  281. l : tasmlabel); virtual;
  282. procedure a_cmp_const_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; a: tcgint; const loc: tlocation;
  283. l : tasmlabel);
  284. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  285. procedure a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  286. procedure a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  287. procedure a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  288. procedure a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  289. procedure a_cmp_ref_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  290. l : tasmlabel);
  291. procedure a_jmp_name(list : TAsmList;const s : string); virtual; abstract;
  292. procedure a_jmp_always(list : TAsmList;l: tasmlabel); virtual; abstract;
  293. {$ifdef cpuflags}
  294. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); virtual; abstract;
  295. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  296. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  297. }
  298. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  299. procedure g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  300. {$endif cpuflags}
  301. {
  302. This routine tries to optimize the op_const_reg/ref opcode, and should be
  303. called at the start of a_op_const_reg/ref. It returns the actual opcode
  304. to emit, and the constant value to emit. This function can opcode OP_NONE to
  305. remove the opcode and OP_MOVE to replace it with a simple load
  306. @param(size Size of the operand in constant)
  307. @param(op The opcode to emit, returns the opcode which must be emitted)
  308. @param(a The constant which should be emitted, returns the constant which must
  309. be emitted)
  310. }
  311. procedure optimize_op_const(size: TCGSize; var op: topcg; var a : tcgint);virtual;
  312. {# This should emit the opcode to copy len bytes from the source
  313. to destination.
  314. It must be overridden for each new target processor.
  315. @param(source Source reference of copy)
  316. @param(dest Destination reference of copy)
  317. }
  318. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);virtual; abstract;
  319. {# This should emit the opcode to copy len bytes from the an unaligned source
  320. to destination.
  321. It must be overridden for each new target processor.
  322. @param(source Source reference of copy)
  323. @param(dest Destination reference of copy)
  324. }
  325. procedure g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);virtual;
  326. {# Generates overflow checking code for a node }
  327. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef); virtual;abstract;
  328. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);virtual;
  329. {# Emits instructions when compilation is done in profile
  330. mode (this is set as a command line option). The default
  331. behavior does nothing, should be overridden as required.
  332. }
  333. procedure g_profilecode(list : TAsmList);virtual;
  334. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  335. @param(size Number of bytes to allocate)
  336. }
  337. procedure g_stackpointer_alloc(list : TAsmList;size : longint);virtual;
  338. {# Emits instruction for allocating the locals in entry
  339. code of a routine. This is one of the first
  340. routine called in @var(genentrycode).
  341. @param(localsize Number of bytes to allocate as locals)
  342. }
  343. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);virtual; abstract;
  344. {# Emits instructions for returning from a subroutine.
  345. Should also restore the framepointer and stack.
  346. @param(parasize Number of bytes of parameters to deallocate from stack)
  347. }
  348. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);virtual;abstract;
  349. {# This routine is called when generating the code for the entry point
  350. of a routine. It should save all registers which are not used in this
  351. routine, and which should be declared as saved in the std_saved_registers
  352. set.
  353. This routine is mainly used when linking to code which is generated
  354. by ABI-compliant compilers (like GCC), to make sure that the reserved
  355. registers of that ABI are not clobbered.
  356. @param(usedinproc Registers which are used in the code of this routine)
  357. }
  358. procedure g_save_registers(list:TAsmList);virtual;
  359. {# This routine is called when generating the code for the exit point
  360. of a routine. It should restore all registers which were previously
  361. saved in @var(g_save_standard_registers).
  362. @param(usedinproc Registers which are used in the code of this routine)
  363. }
  364. procedure g_restore_registers(list:TAsmList);virtual;
  365. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);virtual;
  366. { initialize the pic/got register }
  367. procedure g_maybe_got_init(list: TAsmList); virtual;
  368. { allocallcpuregisters, a_call_name, deallocallcpuregisters sequence }
  369. procedure g_call(list: TAsmList; const s: string);
  370. { Generate code to exit an unwind-protected region. The default implementation
  371. produces a simple jump to destination label. }
  372. procedure g_local_unwind(list: TAsmList; l: TAsmLabel);virtual;
  373. { Generate code for integer division by constant,
  374. generic version is suitable for 3-address CPUs }
  375. procedure g_div_const_reg_reg(list:tasmlist; size: TCgSize; a: tcgint; src,dst: tregister); virtual;
  376. { some CPUs do not support hardware fpu exceptions, this procedure is called after instructions which
  377. might set FPU exception related flags, so it has to check these flags if needed and throw an exeception }
  378. procedure g_check_for_fpu_exception(list: TAsmList); 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. if assigned(location^.next) then
  974. internalerror(2010052906);
  975. reference_reset_base(ref,location^.reference.index,location^.reference.offset,ctempposinvalid,newalignment(cgpara.alignment,cgpara.intsize-sizeleft),[]);
  976. if (size <> OS_NO) and
  977. (tcgsize2size[size] <= sizeof(aint)) then
  978. a_load_ref_ref(list,size,location^.size,tmpref,ref)
  979. else
  980. { use concatcopy, because the parameter can be larger than }
  981. { what the OS_* constants can handle }
  982. g_concatcopy(list,tmpref,ref,sizeleft);
  983. end;
  984. LOC_MMREGISTER,LOC_CMMREGISTER:
  985. begin
  986. case location^.size of
  987. OS_F32,
  988. OS_F64,
  989. OS_F128:
  990. a_loadmm_ref_reg(list,location^.size,location^.size,tmpref,location^.register,mms_movescalar);
  991. OS_M8..OS_M128,
  992. OS_MS8..OS_MS128:
  993. a_loadmm_ref_reg(list,location^.size,location^.size,tmpref,location^.register,nil);
  994. else
  995. internalerror(2010053101);
  996. end;
  997. end
  998. else
  999. internalerror(2010053111);
  1000. end;
  1001. inc(tmpref.offset,tcgsize2size[location^.size]);
  1002. dec(sizeleft,tcgsize2size[location^.size]);
  1003. location:=location^.next;
  1004. end;
  1005. end;
  1006. procedure tcg.a_load_loc_cgpara(list : TAsmList;const l:tlocation;const cgpara : TCGPara);
  1007. begin
  1008. case l.loc of
  1009. LOC_REGISTER,
  1010. LOC_CREGISTER :
  1011. a_load_reg_cgpara(list,l.size,l.register,cgpara);
  1012. LOC_CONSTANT :
  1013. a_load_const_cgpara(list,l.size,l.value,cgpara);
  1014. LOC_CREFERENCE,
  1015. LOC_REFERENCE :
  1016. a_load_ref_cgpara(list,l.size,l.reference,cgpara);
  1017. else
  1018. internalerror(2002032211);
  1019. end;
  1020. end;
  1021. procedure tcg.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : TCGPara);
  1022. var
  1023. hr : tregister;
  1024. begin
  1025. cgpara.check_simple_location;
  1026. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  1027. begin
  1028. paramanager.allocparaloc(list,cgpara.location);
  1029. a_loadaddr_ref_reg(list,r,cgpara.location^.register)
  1030. end
  1031. else
  1032. begin
  1033. hr:=getaddressregister(list);
  1034. a_loadaddr_ref_reg(list,r,hr);
  1035. a_load_reg_cgpara(list,OS_ADDR,hr,cgpara);
  1036. end;
  1037. end;
  1038. procedure tcg.a_load_cgparaloc_ref(list : TAsmList;const paraloc : TCGParaLocation;const ref : treference;sizeleft : tcgint;align : longint);
  1039. var
  1040. href : treference;
  1041. hreg : tregister;
  1042. cgsize: tcgsize;
  1043. begin
  1044. case paraloc.loc of
  1045. LOC_REGISTER :
  1046. begin
  1047. hreg:=paraloc.register;
  1048. cgsize:=paraloc.size;
  1049. if paraloc.shiftval>0 then
  1050. a_op_const_reg_reg(list,OP_SHL,OS_INT,paraloc.shiftval,paraloc.register,paraloc.register)
  1051. { in case the original size was 3 or 5/6/7 bytes, the value was
  1052. shifted to the top of the to 4 resp. 8 byte register on the
  1053. caller side and needs to be stored with those bytes at the
  1054. start of the reference -> don't shift right }
  1055. else if (paraloc.shiftval<0) and
  1056. ((-paraloc.shiftval) in [8,16,32]) then
  1057. begin
  1058. a_op_const_reg_reg(list,OP_SHR,OS_INT,-paraloc.shiftval,paraloc.register,paraloc.register);
  1059. { convert to a register of 1/2/4 bytes in size, since the
  1060. original register had to be made larger to be able to hold
  1061. the shifted value }
  1062. cgsize:=int_cgsize(tcgsize2size[OS_INT]-(-paraloc.shiftval div 8));
  1063. if cgsize=OS_NO then
  1064. cgsize:=OS_INT;
  1065. hreg:=getintregister(list,cgsize);
  1066. a_load_reg_reg(list,OS_INT,cgsize,paraloc.register,hreg);
  1067. end;
  1068. { use the exact size to avoid overwriting of adjacent data }
  1069. if tcgsize2size[cgsize]<=sizeleft then
  1070. a_load_reg_ref(list,paraloc.size,cgsize,hreg,ref)
  1071. else
  1072. case sizeleft of
  1073. 1,2,4,8:
  1074. a_load_reg_ref(list,paraloc.size,int_cgsize(sizeleft),hreg,ref);
  1075. 3:
  1076. begin
  1077. if target_info.endian=endian_big then
  1078. begin
  1079. href:=ref;
  1080. inc(href.offset,2);
  1081. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1082. a_op_const_reg_reg(list,OP_SHR,OS_INT,8,hreg,hreg);
  1083. a_load_reg_ref(list,paraloc.size,OS_16,hreg,ref);
  1084. end
  1085. else
  1086. begin
  1087. a_load_reg_ref(list,paraloc.size,OS_16,hreg,ref);
  1088. href:=ref;
  1089. inc(href.offset,2);
  1090. a_op_const_reg_reg(list,OP_SHR,cgsize,16,hreg,hreg);
  1091. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1092. end
  1093. end;
  1094. 5:
  1095. begin
  1096. if target_info.endian=endian_big then
  1097. begin
  1098. href:=ref;
  1099. inc(href.offset,4);
  1100. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1101. a_op_const_reg_reg(list,OP_SHR,OS_INT,8,hreg,hreg);
  1102. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1103. end
  1104. else
  1105. begin
  1106. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1107. href:=ref;
  1108. inc(href.offset,4);
  1109. a_op_const_reg_reg(list,OP_SHR,cgsize,32,hreg,hreg);
  1110. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1111. end
  1112. end;
  1113. 6:
  1114. begin
  1115. if target_info.endian=endian_big then
  1116. begin
  1117. href:=ref;
  1118. inc(href.offset,4);
  1119. a_load_reg_ref(list,paraloc.size,OS_16,hreg,href);
  1120. a_op_const_reg_reg(list,OP_SHR,OS_INT,16,hreg,hreg);
  1121. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1122. end
  1123. else
  1124. begin
  1125. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1126. href:=ref;
  1127. inc(href.offset,4);
  1128. a_op_const_reg_reg(list,OP_SHR,cgsize,32,hreg,hreg);
  1129. a_load_reg_ref(list,paraloc.size,OS_16,hreg,href);
  1130. end
  1131. end;
  1132. 7:
  1133. begin
  1134. if target_info.endian=endian_big then
  1135. begin
  1136. href:=ref;
  1137. inc(href.offset,6);
  1138. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1139. a_op_const_reg_reg(list,OP_SHR,OS_INT,8,hreg,hreg);
  1140. href:=ref;
  1141. inc(href.offset,4);
  1142. a_load_reg_ref(list,paraloc.size,OS_16,hreg,href);
  1143. a_op_const_reg_reg(list,OP_SHR,OS_INT,16,hreg,hreg);
  1144. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1145. end
  1146. else
  1147. begin
  1148. a_load_reg_ref(list,paraloc.size,OS_32,hreg,ref);
  1149. href:=ref;
  1150. inc(href.offset,4);
  1151. a_op_const_reg_reg(list,OP_SHR,cgsize,32,hreg,hreg);
  1152. a_load_reg_ref(list,paraloc.size,OS_16,hreg,href);
  1153. inc(href.offset,2);
  1154. a_op_const_reg_reg(list,OP_SHR,cgsize,16,hreg,hreg);
  1155. a_load_reg_ref(list,paraloc.size,OS_8,hreg,href);
  1156. end
  1157. end;
  1158. else
  1159. { other sizes not allowed }
  1160. Internalerror(2017080901);
  1161. end;
  1162. end;
  1163. LOC_MMREGISTER :
  1164. begin
  1165. case paraloc.size of
  1166. OS_F32,
  1167. OS_F64,
  1168. OS_F128:
  1169. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  1170. OS_M8..OS_M128,
  1171. OS_MS8..OS_MS128:
  1172. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,nil);
  1173. else
  1174. internalerror(2010053102);
  1175. end;
  1176. end;
  1177. LOC_FPUREGISTER :
  1178. a_loadfpu_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1179. LOC_REFERENCE :
  1180. begin
  1181. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,ctempposinvalid,align,[]);
  1182. { use concatcopy, because it can also be a float which fails when
  1183. load_ref_ref is used. Don't copy data when the references are equal }
  1184. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1185. g_concatcopy(list,href,ref,sizeleft);
  1186. end;
  1187. else
  1188. internalerror(2002081302);
  1189. end;
  1190. end;
  1191. procedure tcg.a_load_cgparaloc_anyreg(list: TAsmList;regsize: tcgsize;const paraloc: TCGParaLocation;reg: tregister;align: longint);
  1192. var
  1193. href : treference;
  1194. begin
  1195. case paraloc.loc of
  1196. LOC_REGISTER :
  1197. begin
  1198. if paraloc.shiftval<0 then
  1199. a_op_const_reg_reg(list,OP_SHR,OS_INT,-paraloc.shiftval,paraloc.register,paraloc.register);
  1200. case getregtype(reg) of
  1201. R_ADDRESSREGISTER,
  1202. R_INTREGISTER:
  1203. a_load_reg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1204. R_MMREGISTER:
  1205. a_loadmm_intreg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1206. R_FPUREGISTER:
  1207. a_loadfpu_intreg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1208. else
  1209. internalerror(2009112422);
  1210. end;
  1211. end;
  1212. LOC_MMREGISTER :
  1213. begin
  1214. case getregtype(reg) of
  1215. R_ADDRESSREGISTER,
  1216. R_INTREGISTER:
  1217. a_loadmm_reg_intreg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1218. R_MMREGISTER:
  1219. begin
  1220. case paraloc.size of
  1221. OS_F32,
  1222. OS_F64,
  1223. OS_F128:
  1224. a_loadmm_reg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1225. OS_M8..OS_M128,
  1226. OS_MS8..OS_MS128:
  1227. a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,nil);
  1228. else
  1229. internalerror(2010053102);
  1230. end;
  1231. end;
  1232. else
  1233. internalerror(2010053104);
  1234. end;
  1235. end;
  1236. LOC_FPUREGISTER :
  1237. begin
  1238. case getregtype(reg) of
  1239. R_FPUREGISTER:
  1240. a_loadfpu_reg_reg(list,paraloc.size,regsize,paraloc.register,reg)
  1241. else
  1242. internalerror(2015031401);
  1243. end;
  1244. end;
  1245. LOC_REFERENCE :
  1246. begin
  1247. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,ctempposinvalid,align,[]);
  1248. case getregtype(reg) of
  1249. R_ADDRESSREGISTER,
  1250. R_INTREGISTER :
  1251. a_load_ref_reg(list,paraloc.size,regsize,href,reg);
  1252. R_FPUREGISTER :
  1253. a_loadfpu_ref_reg(list,paraloc.size,regsize,href,reg);
  1254. R_MMREGISTER :
  1255. { not paraloc.size, because it may be OS_64 instead of
  1256. OS_F64 in case the parameter is passed using integer
  1257. conventions (e.g., on ARM) }
  1258. a_loadmm_ref_reg(list,regsize,regsize,href,reg,mms_movescalar);
  1259. else
  1260. internalerror(2004101012);
  1261. end;
  1262. end;
  1263. else
  1264. internalerror(2002081302);
  1265. end;
  1266. end;
  1267. {****************************************************************************
  1268. some generic implementations
  1269. ****************************************************************************}
  1270. { memory/register loading }
  1271. procedure tcg.a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  1272. var
  1273. tmpref : treference;
  1274. tmpreg : tregister;
  1275. i : longint;
  1276. begin
  1277. if ref.alignment<tcgsize2size[fromsize] then
  1278. begin
  1279. tmpref:=ref;
  1280. { we take care of the alignment now }
  1281. tmpref.alignment:=0;
  1282. case FromSize of
  1283. OS_16,OS_S16:
  1284. begin
  1285. tmpreg:=getintregister(list,OS_16);
  1286. a_load_reg_reg(list,fromsize,OS_16,register,tmpreg);
  1287. if target_info.endian=endian_big then
  1288. inc(tmpref.offset);
  1289. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1290. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1291. tmpreg:=makeregsize(list,tmpreg,OS_16);
  1292. a_op_const_reg(list,OP_SHR,OS_16,8,tmpreg);
  1293. if target_info.endian=endian_big then
  1294. dec(tmpref.offset)
  1295. else
  1296. inc(tmpref.offset);
  1297. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1298. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1299. end;
  1300. OS_32,OS_S32:
  1301. begin
  1302. { could add an optimised case for ref.alignment=2 }
  1303. tmpreg:=getintregister(list,OS_32);
  1304. a_load_reg_reg(list,fromsize,OS_32,register,tmpreg);
  1305. if target_info.endian=endian_big then
  1306. inc(tmpref.offset,3);
  1307. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1308. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1309. tmpreg:=makeregsize(list,tmpreg,OS_32);
  1310. for i:=1 to 3 do
  1311. begin
  1312. a_op_const_reg(list,OP_SHR,OS_32,8,tmpreg);
  1313. if target_info.endian=endian_big then
  1314. dec(tmpref.offset)
  1315. else
  1316. inc(tmpref.offset);
  1317. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1318. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1319. tmpreg:=makeregsize(list,tmpreg,OS_32);
  1320. end;
  1321. end
  1322. else
  1323. a_load_reg_ref(list,fromsize,tosize,register,tmpref);
  1324. end;
  1325. end
  1326. else
  1327. a_load_reg_ref(list,fromsize,tosize,register,ref);
  1328. end;
  1329. procedure tcg.a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  1330. var
  1331. tmpref : treference;
  1332. tmpreg,
  1333. tmpreg2 : tregister;
  1334. i : longint;
  1335. hisize : tcgsize;
  1336. begin
  1337. if ref.alignment in [1,2] then
  1338. begin
  1339. tmpref:=ref;
  1340. { we take care of the alignment now }
  1341. tmpref.alignment:=0;
  1342. case FromSize of
  1343. OS_16,OS_S16:
  1344. if ref.alignment=2 then
  1345. a_load_ref_reg(list,fromsize,tosize,tmpref,register)
  1346. else
  1347. begin
  1348. if FromSize=OS_16 then
  1349. hisize:=OS_8
  1350. else
  1351. hisize:=OS_S8;
  1352. { first load in tmpreg, because the target register }
  1353. { may be used in ref as well }
  1354. if target_info.endian=endian_little then
  1355. inc(tmpref.offset);
  1356. tmpreg:=getintregister(list,OS_8);
  1357. a_load_ref_reg(list,hisize,hisize,tmpref,tmpreg);
  1358. tmpreg:=makeregsize(list,tmpreg,FromSize);
  1359. a_op_const_reg(list,OP_SHL,FromSize,8,tmpreg);
  1360. if target_info.endian=endian_little then
  1361. dec(tmpref.offset)
  1362. else
  1363. inc(tmpref.offset);
  1364. tmpreg2:=makeregsize(list,register,OS_16);
  1365. a_load_ref_reg(list,OS_8,OS_16,tmpref,tmpreg2);
  1366. a_op_reg_reg(list,OP_OR,OS_16,tmpreg,tmpreg2);
  1367. a_load_reg_reg(list,fromsize,tosize,tmpreg2,register);
  1368. end;
  1369. OS_32,OS_S32:
  1370. if ref.alignment=2 then
  1371. begin
  1372. if target_info.endian=endian_little then
  1373. inc(tmpref.offset,2);
  1374. tmpreg:=getintregister(list,OS_32);
  1375. a_load_ref_reg(list,OS_16,OS_32,tmpref,tmpreg);
  1376. a_op_const_reg(list,OP_SHL,OS_32,16,tmpreg);
  1377. if target_info.endian=endian_little then
  1378. dec(tmpref.offset,2)
  1379. else
  1380. inc(tmpref.offset,2);
  1381. tmpreg2:=makeregsize(list,register,OS_32);
  1382. a_load_ref_reg(list,OS_16,OS_32,tmpref,tmpreg2);
  1383. a_op_reg_reg(list,OP_OR,OS_32,tmpreg,tmpreg2);
  1384. a_load_reg_reg(list,fromsize,tosize,tmpreg2,register);
  1385. end
  1386. else
  1387. begin
  1388. if target_info.endian=endian_little then
  1389. inc(tmpref.offset,3);
  1390. tmpreg:=getintregister(list,OS_32);
  1391. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg);
  1392. tmpreg2:=getintregister(list,OS_32);
  1393. for i:=1 to 3 do
  1394. begin
  1395. a_op_const_reg(list,OP_SHL,OS_32,8,tmpreg);
  1396. if target_info.endian=endian_little then
  1397. dec(tmpref.offset)
  1398. else
  1399. inc(tmpref.offset);
  1400. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg2);
  1401. a_op_reg_reg(list,OP_OR,OS_32,tmpreg2,tmpreg);
  1402. end;
  1403. a_load_reg_reg(list,fromsize,tosize,tmpreg,register);
  1404. end
  1405. else
  1406. a_load_ref_reg(list,fromsize,tosize,tmpref,register);
  1407. end;
  1408. end
  1409. else
  1410. a_load_ref_reg(list,fromsize,tosize,ref,register);
  1411. end;
  1412. procedure tcg.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  1413. var
  1414. tmpreg: tregister;
  1415. begin
  1416. { verify if we have the same reference }
  1417. if references_equal(sref,dref) then
  1418. exit;
  1419. tmpreg:=getintregister(list,tosize);
  1420. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  1421. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  1422. end;
  1423. procedure tcg.a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : treference);
  1424. var
  1425. tmpreg: tregister;
  1426. begin
  1427. tmpreg:=getintregister(list,size);
  1428. a_load_const_reg(list,size,a,tmpreg);
  1429. a_load_reg_ref(list,size,size,tmpreg,ref);
  1430. end;
  1431. procedure tcg.a_load_const_loc(list : TAsmList;a : tcgint;const loc: tlocation);
  1432. begin
  1433. case loc.loc of
  1434. LOC_REFERENCE,LOC_CREFERENCE:
  1435. a_load_const_ref(list,loc.size,a,loc.reference);
  1436. LOC_REGISTER,LOC_CREGISTER:
  1437. a_load_const_reg(list,loc.size,a,loc.register);
  1438. else
  1439. internalerror(200203272);
  1440. end;
  1441. end;
  1442. procedure tcg.a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  1443. begin
  1444. case loc.loc of
  1445. LOC_REFERENCE,LOC_CREFERENCE:
  1446. a_load_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  1447. LOC_REGISTER,LOC_CREGISTER:
  1448. a_load_reg_reg(list,fromsize,loc.size,reg,loc.register);
  1449. LOC_MMREGISTER,LOC_CMMREGISTER:
  1450. a_loadmm_intreg_reg(list,fromsize,loc.size,reg,loc.register,mms_movescalar);
  1451. else
  1452. internalerror(200203271);
  1453. end;
  1454. end;
  1455. procedure tcg.a_load_loc_reg(list : TAsmList; tosize: tcgsize; const loc: tlocation; reg : tregister);
  1456. begin
  1457. case loc.loc of
  1458. LOC_REFERENCE,LOC_CREFERENCE:
  1459. a_load_ref_reg(list,loc.size,tosize,loc.reference,reg);
  1460. LOC_REGISTER,LOC_CREGISTER:
  1461. a_load_reg_reg(list,loc.size,tosize,loc.register,reg);
  1462. LOC_CONSTANT:
  1463. a_load_const_reg(list,tosize,loc.value,reg);
  1464. LOC_MMREGISTER,LOC_CMMREGISTER:
  1465. a_loadmm_reg_intreg(list,loc.size,tosize,loc.register,reg,mms_movescalar);
  1466. else
  1467. internalerror(200109092);
  1468. end;
  1469. end;
  1470. procedure tcg.a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  1471. begin
  1472. case loc.loc of
  1473. LOC_REFERENCE,LOC_CREFERENCE:
  1474. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  1475. LOC_REGISTER,LOC_CREGISTER:
  1476. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  1477. LOC_CONSTANT:
  1478. a_load_const_ref(list,tosize,loc.value,ref);
  1479. else
  1480. internalerror(200109302);
  1481. end;
  1482. end;
  1483. procedure tcg.optimize_op_const(size: TCGSize; var op: topcg; var a : tcgint);
  1484. var
  1485. powerval : longint;
  1486. signext_a, zeroext_a: tcgint;
  1487. begin
  1488. case size of
  1489. OS_64,OS_S64:
  1490. begin
  1491. signext_a:=int64(a);
  1492. zeroext_a:=int64(a);
  1493. end;
  1494. OS_32,OS_S32:
  1495. begin
  1496. signext_a:=longint(a);
  1497. zeroext_a:=dword(a);
  1498. end;
  1499. OS_16,OS_S16:
  1500. begin
  1501. signext_a:=smallint(a);
  1502. zeroext_a:=word(a);
  1503. end;
  1504. OS_8,OS_S8:
  1505. begin
  1506. signext_a:=shortint(a);
  1507. zeroext_a:=byte(a);
  1508. end
  1509. else
  1510. begin
  1511. { Should we internalerror() here instead? }
  1512. signext_a:=a;
  1513. zeroext_a:=a;
  1514. end;
  1515. end;
  1516. case op of
  1517. OP_OR :
  1518. begin
  1519. { or with zero returns same result }
  1520. if a = 0 then
  1521. op:=OP_NONE
  1522. else
  1523. { or with max returns max }
  1524. if signext_a = -1 then
  1525. op:=OP_MOVE;
  1526. end;
  1527. OP_AND :
  1528. begin
  1529. { and with max returns same result }
  1530. if (signext_a = -1) then
  1531. op:=OP_NONE
  1532. else
  1533. { and with 0 returns 0 }
  1534. if a=0 then
  1535. op:=OP_MOVE;
  1536. end;
  1537. OP_XOR :
  1538. begin
  1539. { xor with zero returns same result }
  1540. if a = 0 then
  1541. op:=OP_NONE;
  1542. end;
  1543. OP_DIV :
  1544. begin
  1545. { division by 1 returns result }
  1546. if a = 1 then
  1547. op:=OP_NONE
  1548. else if ispowerof2(int64(zeroext_a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  1549. begin
  1550. a := powerval;
  1551. op:= OP_SHR;
  1552. end;
  1553. end;
  1554. OP_IDIV:
  1555. begin
  1556. if a = 1 then
  1557. op:=OP_NONE;
  1558. end;
  1559. OP_MUL,OP_IMUL:
  1560. begin
  1561. if a = 1 then
  1562. op:=OP_NONE
  1563. else
  1564. if a=0 then
  1565. op:=OP_MOVE
  1566. else if ispowerof2(int64(zeroext_a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  1567. begin
  1568. a := powerval;
  1569. op:= OP_SHL;
  1570. end;
  1571. end;
  1572. OP_ADD,OP_SUB:
  1573. begin
  1574. if a = 0 then
  1575. op:=OP_NONE;
  1576. end;
  1577. OP_SAR,OP_SHL,OP_SHR:
  1578. begin
  1579. if a = 0 then
  1580. op:=OP_NONE;
  1581. end;
  1582. OP_ROL,OP_ROR:
  1583. begin
  1584. case size of
  1585. OS_64,OS_S64:
  1586. a:=a and 63;
  1587. OS_32,OS_S32:
  1588. a:=a and 31;
  1589. OS_16,OS_S16:
  1590. a:=a and 15;
  1591. OS_8,OS_S8:
  1592. a:=a and 7;
  1593. end;
  1594. if a = 0 then
  1595. op:=OP_NONE;
  1596. end;
  1597. end;
  1598. end;
  1599. procedure tcg.a_loadfpu_loc_reg(list: TAsmList; tosize: tcgsize; const loc: tlocation; const reg: tregister);
  1600. begin
  1601. case loc.loc of
  1602. LOC_REFERENCE, LOC_CREFERENCE:
  1603. a_loadfpu_ref_reg(list,loc.size,tosize,loc.reference,reg);
  1604. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1605. a_loadfpu_reg_reg(list,loc.size,tosize,loc.register,reg);
  1606. else
  1607. internalerror(200203301);
  1608. end;
  1609. end;
  1610. procedure tcg.a_loadfpu_reg_loc(list: TAsmList; fromsize: tcgsize; const reg: tregister; const loc: tlocation);
  1611. begin
  1612. case loc.loc of
  1613. LOC_REFERENCE, LOC_CREFERENCE:
  1614. a_loadfpu_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  1615. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1616. a_loadfpu_reg_reg(list,fromsize,loc.size,reg,loc.register);
  1617. else
  1618. internalerror(48991);
  1619. end;
  1620. end;
  1621. procedure tcg.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tcgsize; const ref1,ref2: treference);
  1622. var
  1623. reg: tregister;
  1624. regsize: tcgsize;
  1625. begin
  1626. if (fromsize>=tosize) then
  1627. regsize:=fromsize
  1628. else
  1629. regsize:=tosize;
  1630. reg:=getfpuregister(list,regsize);
  1631. a_loadfpu_ref_reg(list,fromsize,regsize,ref1,reg);
  1632. a_loadfpu_reg_ref(list,regsize,tosize,reg,ref2);
  1633. end;
  1634. procedure tcg.a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);
  1635. var
  1636. ref : treference;
  1637. begin
  1638. paramanager.alloccgpara(list,cgpara);
  1639. case cgpara.location^.loc of
  1640. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1641. begin
  1642. cgpara.check_simple_location;
  1643. a_loadfpu_reg_reg(list,size,size,r,cgpara.location^.register);
  1644. end;
  1645. LOC_REFERENCE,LOC_CREFERENCE:
  1646. begin
  1647. cgpara.check_simple_location;
  1648. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  1649. a_loadfpu_reg_ref(list,size,size,r,ref);
  1650. end;
  1651. LOC_REGISTER,LOC_CREGISTER:
  1652. begin
  1653. { paramfpu_ref does the check_simpe_location check here if necessary }
  1654. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,ref);
  1655. a_loadfpu_reg_ref(list,size,size,r,ref);
  1656. a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  1657. tg.Ungettemp(list,ref);
  1658. end;
  1659. else
  1660. internalerror(2010053112);
  1661. end;
  1662. end;
  1663. procedure tcg.a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);
  1664. var
  1665. href : treference;
  1666. hsize: tcgsize;
  1667. paraloc: PCGParaLocation;
  1668. begin
  1669. case cgpara.location^.loc of
  1670. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1671. begin
  1672. paramanager.alloccgpara(list,cgpara);
  1673. paraloc:=cgpara.location;
  1674. href:=ref;
  1675. while assigned(paraloc) do
  1676. begin
  1677. if not(paraloc^.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  1678. internalerror(2015031501);
  1679. a_loadfpu_ref_reg(list,paraloc^.size,paraloc^.size,href,paraloc^.register);
  1680. inc(href.offset,tcgsize2size[paraloc^.size]);
  1681. paraloc:=paraloc^.next;
  1682. end;
  1683. end;
  1684. LOC_REFERENCE,LOC_CREFERENCE:
  1685. begin
  1686. cgpara.check_simple_location;
  1687. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  1688. { concatcopy should choose the best way to copy the data }
  1689. g_concatcopy(list,ref,href,tcgsize2size[size]);
  1690. end;
  1691. LOC_REGISTER,LOC_CREGISTER:
  1692. begin
  1693. { force integer size }
  1694. hsize:=int_cgsize(tcgsize2size[size]);
  1695. {$ifndef cpu64bitalu}
  1696. if (hsize in [OS_S64,OS_64]) then
  1697. cg64.a_load64_ref_cgpara(list,ref,cgpara)
  1698. else
  1699. {$endif not cpu64bitalu}
  1700. begin
  1701. cgpara.check_simple_location;
  1702. a_load_ref_cgpara(list,hsize,ref,cgpara)
  1703. end;
  1704. end
  1705. else
  1706. internalerror(200402201);
  1707. end;
  1708. end;
  1709. procedure tcg.a_loadfpu_intreg_reg(list : TAsmList; fromsize,tosize : tcgsize; intreg,fpureg : tregister);
  1710. var
  1711. tmpref: treference;
  1712. begin
  1713. if not(tcgsize2size[fromsize] in [4,8]) or
  1714. not(tcgsize2size[tosize] in [4,8]) or
  1715. (tcgsize2size[fromsize]<>tcgsize2size[tosize]) then
  1716. internalerror(2017070902);
  1717. tg.gettemp(list,tcgsize2size[fromsize],tcgsize2size[fromsize],tt_normal,tmpref);
  1718. a_load_reg_ref(list,fromsize,fromsize,intreg,tmpref);
  1719. a_loadfpu_ref_reg(list,tosize,tosize,tmpref,fpureg);
  1720. tg.ungettemp(list,tmpref);
  1721. end;
  1722. procedure tcg.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1723. var
  1724. tmpreg : tregister;
  1725. tmpref : treference;
  1726. begin
  1727. if assigned(ref.symbol) then
  1728. begin
  1729. tmpreg:=getaddressregister(list);
  1730. a_loadaddr_ref_reg(list,ref,tmpreg);
  1731. reference_reset_base(tmpref,tmpreg,0,ref.temppos,ref.alignment,[]);
  1732. end
  1733. else
  1734. tmpref:=ref;
  1735. tmpreg:=getintregister(list,size);
  1736. a_load_ref_reg(list,size,size,tmpref,tmpreg);
  1737. a_op_const_reg(list,op,size,a,tmpreg);
  1738. a_load_reg_ref(list,size,size,tmpreg,tmpref);
  1739. end;
  1740. procedure tcg.a_op_const_loc(list : TAsmList; Op: TOpCG; a: tcgint; const loc: tlocation);
  1741. begin
  1742. case loc.loc of
  1743. LOC_REGISTER, LOC_CREGISTER:
  1744. a_op_const_reg(list,op,loc.size,a,loc.register);
  1745. LOC_REFERENCE, LOC_CREFERENCE:
  1746. a_op_const_ref(list,op,loc.size,a,loc.reference);
  1747. else
  1748. internalerror(200109061);
  1749. end;
  1750. end;
  1751. procedure tcg.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1752. var
  1753. tmpreg : tregister;
  1754. tmpref : treference;
  1755. begin
  1756. if assigned(ref.symbol) then
  1757. begin
  1758. tmpreg:=getaddressregister(list);
  1759. a_loadaddr_ref_reg(list,ref,tmpreg);
  1760. reference_reset_base(tmpref,tmpreg,0,ref.temppos,ref.alignment,[]);
  1761. end
  1762. else
  1763. tmpref:=ref;
  1764. tmpreg:=getintregister(list,size);
  1765. a_load_ref_reg(list,size,size,tmpref,tmpreg);
  1766. if op in [OP_NEG,OP_NOT] then
  1767. begin
  1768. if reg<>NR_NO then
  1769. internalerror(2017040901);
  1770. a_op_reg_reg(list,op,size,tmpreg,tmpreg);
  1771. end
  1772. else
  1773. a_op_reg_reg(list,op,size,reg,tmpreg);
  1774. a_load_reg_ref(list,size,size,tmpreg,tmpref);
  1775. end;
  1776. procedure tcg.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1777. var
  1778. tmpreg: tregister;
  1779. begin
  1780. case op of
  1781. OP_NOT,OP_NEG:
  1782. { handle it as "load ref,reg; op reg" }
  1783. begin
  1784. a_load_ref_reg(list,size,size,ref,reg);
  1785. a_op_reg_reg(list,op,size,reg,reg);
  1786. end;
  1787. else
  1788. begin
  1789. tmpreg:=getintregister(list,size);
  1790. a_load_ref_reg(list,size,size,ref,tmpreg);
  1791. a_op_reg_reg(list,op,size,tmpreg,reg);
  1792. end;
  1793. end;
  1794. end;
  1795. procedure tcg.a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  1796. begin
  1797. case loc.loc of
  1798. LOC_REGISTER, LOC_CREGISTER:
  1799. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  1800. LOC_REFERENCE, LOC_CREFERENCE:
  1801. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  1802. else
  1803. internalerror(200109061);
  1804. end;
  1805. end;
  1806. procedure tcg.a_op_loc_reg(list : TAsmList; Op : TOpCG; size: TCGSize; const loc : tlocation; reg : tregister);
  1807. begin
  1808. case loc.loc of
  1809. LOC_REGISTER, LOC_CREGISTER:
  1810. a_op_reg_reg(list,op,size,loc.register,reg);
  1811. LOC_REFERENCE, LOC_CREFERENCE:
  1812. a_op_ref_reg(list,op,size,loc.reference,reg);
  1813. LOC_CONSTANT:
  1814. a_op_const_reg(list,op,size,loc.value,reg);
  1815. else
  1816. internalerror(2018031101);
  1817. end;
  1818. end;
  1819. procedure tcg.a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  1820. var
  1821. tmpreg: tregister;
  1822. begin
  1823. case loc.loc of
  1824. LOC_REGISTER,LOC_CREGISTER:
  1825. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  1826. LOC_REFERENCE,LOC_CREFERENCE:
  1827. begin
  1828. tmpreg:=getintregister(list,loc.size);
  1829. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  1830. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  1831. end;
  1832. else
  1833. internalerror(200109061);
  1834. end;
  1835. end;
  1836. procedure Tcg.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1837. a:tcgint;src,dst:Tregister);
  1838. begin
  1839. optimize_op_const(size, op, a);
  1840. case op of
  1841. OP_NONE:
  1842. begin
  1843. if src <> dst then
  1844. a_load_reg_reg(list, size, size, src, dst);
  1845. exit;
  1846. end;
  1847. OP_MOVE:
  1848. begin
  1849. a_load_const_reg(list, size, a, dst);
  1850. exit;
  1851. end;
  1852. {$ifdef cpu8bitalu}
  1853. OP_SHL:
  1854. begin
  1855. if a=8 then
  1856. case size of
  1857. OS_S16,OS_16:
  1858. begin
  1859. a_load_reg_reg(list,OS_8,OS_8,src,GetNextReg(dst));
  1860. a_load_const_reg(list,OS_8,0,dst);
  1861. exit;
  1862. end;
  1863. end;
  1864. end;
  1865. OP_SHR:
  1866. begin
  1867. if a=8 then
  1868. case size of
  1869. OS_S16,OS_16:
  1870. begin
  1871. a_load_reg_reg(list,OS_8,OS_8,GetNextReg(src),dst);
  1872. a_load_const_reg(list,OS_8,0,GetNextReg(dst));
  1873. exit;
  1874. end;
  1875. end;
  1876. end;
  1877. {$endif cpu8bitalu}
  1878. {$ifdef cpu16bitalu}
  1879. OP_SHL:
  1880. begin
  1881. if a=16 then
  1882. case size of
  1883. OS_S32,OS_32:
  1884. begin
  1885. a_load_reg_reg(list,OS_16,OS_16,src,GetNextReg(dst));
  1886. a_load_const_reg(list,OS_16,0,dst);
  1887. exit;
  1888. end;
  1889. end;
  1890. end;
  1891. OP_SHR:
  1892. begin
  1893. if a=16 then
  1894. case size of
  1895. OS_S32,OS_32:
  1896. begin
  1897. a_load_reg_reg(list,OS_16,OS_16,GetNextReg(src),dst);
  1898. a_load_const_reg(list,OS_16,0,GetNextReg(dst));
  1899. exit;
  1900. end;
  1901. end;
  1902. end;
  1903. {$endif cpu16bitalu}
  1904. end;
  1905. a_load_reg_reg(list,size,size,src,dst);
  1906. a_op_const_reg(list,op,size,a,dst);
  1907. end;
  1908. procedure tcg.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1909. size: tcgsize; src1, src2, dst: tregister);
  1910. var
  1911. tmpreg: tregister;
  1912. begin
  1913. if (dst<>src1) then
  1914. begin
  1915. a_load_reg_reg(list,size,size,src2,dst);
  1916. a_op_reg_reg(list,op,size,src1,dst);
  1917. end
  1918. else
  1919. begin
  1920. { can we do a direct operation on the target register ? }
  1921. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  1922. a_op_reg_reg(list,op,size,src2,dst)
  1923. else
  1924. begin
  1925. tmpreg:=getintregister(list,size);
  1926. a_load_reg_reg(list,size,size,src2,tmpreg);
  1927. a_op_reg_reg(list,op,size,src1,tmpreg);
  1928. a_load_reg_reg(list,size,size,tmpreg,dst);
  1929. end;
  1930. end;
  1931. end;
  1932. procedure tcg.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1933. begin
  1934. a_op_const_reg_reg(list,op,size,a,src,dst);
  1935. ovloc.loc:=LOC_VOID;
  1936. end;
  1937. procedure tcg.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1938. begin
  1939. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1940. ovloc.loc:=LOC_VOID;
  1941. end;
  1942. procedure tcg.a_cmp_const_reg_label(list: TAsmList; size: tcgsize;
  1943. cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1944. var
  1945. tmpreg: tregister;
  1946. begin
  1947. tmpreg:=getintregister(list,size);
  1948. a_load_const_reg(list,size,a,tmpreg);
  1949. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1950. end;
  1951. procedure tcg.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  1952. l : tasmlabel);
  1953. var
  1954. tmpreg: tregister;
  1955. begin
  1956. tmpreg:=getintregister(list,size);
  1957. a_load_ref_reg(list,size,size,ref,tmpreg);
  1958. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1959. end;
  1960. procedure tcg.a_cmp_const_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const loc : tlocation;
  1961. l : tasmlabel);
  1962. begin
  1963. case loc.loc of
  1964. LOC_REGISTER,LOC_CREGISTER:
  1965. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1966. LOC_REFERENCE,LOC_CREFERENCE:
  1967. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1968. else
  1969. internalerror(200109061);
  1970. end;
  1971. end;
  1972. procedure tcg.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  1973. var
  1974. tmpreg: tregister;
  1975. begin
  1976. tmpreg:=getintregister(list,size);
  1977. a_load_ref_reg(list,size,size,ref,tmpreg);
  1978. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1979. end;
  1980. procedure tcg.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg : tregister; const ref: treference; l : tasmlabel);
  1981. var
  1982. tmpreg: tregister;
  1983. begin
  1984. tmpreg:=getintregister(list,size);
  1985. a_load_ref_reg(list,size,size,ref,tmpreg);
  1986. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  1987. end;
  1988. procedure tcg.a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  1989. begin
  1990. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  1991. end;
  1992. procedure tcg.a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  1993. begin
  1994. case loc.loc of
  1995. LOC_REGISTER,
  1996. LOC_CREGISTER:
  1997. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1998. LOC_REFERENCE,
  1999. LOC_CREFERENCE :
  2000. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  2001. LOC_CONSTANT:
  2002. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  2003. else
  2004. internalerror(200203231);
  2005. end;
  2006. end;
  2007. procedure tcg.a_cmp_ref_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  2008. l : tasmlabel);
  2009. var
  2010. tmpreg: tregister;
  2011. begin
  2012. case loc.loc of
  2013. LOC_REGISTER,LOC_CREGISTER:
  2014. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  2015. LOC_REFERENCE,LOC_CREFERENCE:
  2016. begin
  2017. tmpreg:=getintregister(list,size);
  2018. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  2019. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  2020. end;
  2021. else
  2022. internalerror(200109061);
  2023. end;
  2024. end;
  2025. procedure tcg.a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  2026. begin
  2027. case loc.loc of
  2028. LOC_MMREGISTER,LOC_CMMREGISTER:
  2029. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  2030. LOC_REFERENCE,LOC_CREFERENCE:
  2031. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  2032. LOC_REGISTER,LOC_CREGISTER:
  2033. a_loadmm_intreg_reg(list,loc.size,size,loc.register,reg,shuffle);
  2034. else
  2035. internalerror(200310121);
  2036. end;
  2037. end;
  2038. procedure tcg.a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  2039. begin
  2040. case loc.loc of
  2041. LOC_MMREGISTER,LOC_CMMREGISTER:
  2042. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  2043. LOC_REFERENCE,LOC_CREFERENCE:
  2044. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  2045. else
  2046. internalerror(200310122);
  2047. end;
  2048. end;
  2049. procedure tcg.a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle);
  2050. var
  2051. href : treference;
  2052. {$ifndef cpu64bitalu}
  2053. tmpreg : tregister;
  2054. reg64 : tregister64;
  2055. {$endif not cpu64bitalu}
  2056. begin
  2057. {$ifndef cpu64bitalu}
  2058. if not(cgpara.location^.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  2059. (size<>OS_F64) then
  2060. {$endif not cpu64bitalu}
  2061. cgpara.check_simple_location;
  2062. paramanager.alloccgpara(list,cgpara);
  2063. case cgpara.location^.loc of
  2064. LOC_MMREGISTER,LOC_CMMREGISTER:
  2065. a_loadmm_reg_reg(list,size,cgpara.location^.size,reg,cgpara.location^.register,shuffle);
  2066. LOC_REFERENCE,LOC_CREFERENCE:
  2067. begin
  2068. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  2069. a_loadmm_reg_ref(list,size,cgpara.location^.size,reg,href,shuffle);
  2070. end;
  2071. LOC_REGISTER,LOC_CREGISTER:
  2072. begin
  2073. if assigned(shuffle) and
  2074. not shufflescalar(shuffle) then
  2075. internalerror(2009112510);
  2076. {$ifndef cpu64bitalu}
  2077. if (size=OS_F64) then
  2078. begin
  2079. if not assigned(cgpara.location^.next) or
  2080. assigned(cgpara.location^.next^.next) then
  2081. internalerror(2009112512);
  2082. case cgpara.location^.next^.loc of
  2083. LOC_REGISTER,LOC_CREGISTER:
  2084. tmpreg:=cgpara.location^.next^.register;
  2085. LOC_REFERENCE,LOC_CREFERENCE:
  2086. tmpreg:=getintregister(list,OS_32);
  2087. else
  2088. internalerror(2009112910);
  2089. end;
  2090. if (target_info.endian=ENDIAN_BIG) then
  2091. begin
  2092. { paraloc^ -> high
  2093. paraloc^.next -> low }
  2094. reg64.reghi:=cgpara.location^.register;
  2095. reg64.reglo:=tmpreg;
  2096. end
  2097. else
  2098. begin
  2099. { paraloc^ -> low
  2100. paraloc^.next -> high }
  2101. reg64.reglo:=cgpara.location^.register;
  2102. reg64.reghi:=tmpreg;
  2103. end;
  2104. cg64.a_loadmm_reg_intreg64(list,size,reg,reg64);
  2105. if (cgpara.location^.next^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2106. begin
  2107. if not(cgpara.location^.next^.size in [OS_32,OS_S32]) then
  2108. internalerror(2009112911);
  2109. reference_reset_base(href,cgpara.location^.next^.reference.index,cgpara.location^.next^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  2110. a_load_reg_ref(list,OS_32,cgpara.location^.next^.size,tmpreg,href);
  2111. end;
  2112. end
  2113. else
  2114. {$endif not cpu64bitalu}
  2115. a_loadmm_reg_intreg(list,size,cgpara.location^.size,reg,cgpara.location^.register,mms_movescalar);
  2116. end
  2117. else
  2118. internalerror(200310123);
  2119. end;
  2120. end;
  2121. procedure tcg.a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize;const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle);
  2122. var
  2123. hr : tregister;
  2124. hs : tmmshuffle;
  2125. begin
  2126. cgpara.check_simple_location;
  2127. hr:=getmmregister(list,cgpara.location^.size);
  2128. a_loadmm_ref_reg(list,size,cgpara.location^.size,ref,hr,shuffle);
  2129. if realshuffle(shuffle) then
  2130. begin
  2131. hs:=shuffle^;
  2132. removeshuffles(hs);
  2133. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,@hs);
  2134. end
  2135. else
  2136. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,shuffle);
  2137. end;
  2138. procedure tcg.a_loadmm_loc_cgpara(list: TAsmList;const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle);
  2139. begin
  2140. case loc.loc of
  2141. LOC_MMREGISTER,LOC_CMMREGISTER:
  2142. a_loadmm_reg_cgpara(list,loc.size,loc.register,cgpara,shuffle);
  2143. LOC_REFERENCE,LOC_CREFERENCE:
  2144. a_loadmm_ref_cgpara(list,loc.size,loc.reference,cgpara,shuffle);
  2145. else
  2146. internalerror(200310123);
  2147. end;
  2148. end;
  2149. procedure tcg.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  2150. var
  2151. hr : tregister;
  2152. hs : tmmshuffle;
  2153. begin
  2154. hr:=getmmregister(list,size);
  2155. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  2156. if realshuffle(shuffle) then
  2157. begin
  2158. hs:=shuffle^;
  2159. removeshuffles(hs);
  2160. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  2161. end
  2162. else
  2163. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  2164. end;
  2165. procedure tcg.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  2166. var
  2167. hr : tregister;
  2168. hs : tmmshuffle;
  2169. begin
  2170. hr:=getmmregister(list,size);
  2171. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  2172. if realshuffle(shuffle) then
  2173. begin
  2174. hs:=shuffle^;
  2175. removeshuffles(hs);
  2176. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  2177. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  2178. end
  2179. else
  2180. begin
  2181. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  2182. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  2183. end;
  2184. end;
  2185. procedure tcg.a_loadmm_intreg_reg(list: tasmlist; fromsize,tosize: tcgsize; intreg,mmreg: tregister; shuffle: pmmshuffle);
  2186. var
  2187. tmpref: treference;
  2188. begin
  2189. if (tcgsize2size[fromsize]<>4) or
  2190. (tcgsize2size[tosize]<>4) then
  2191. internalerror(2009112503);
  2192. tg.gettemp(list,4,4,tt_normal,tmpref);
  2193. a_load_reg_ref(list,fromsize,fromsize,intreg,tmpref);
  2194. a_loadmm_ref_reg(list,tosize,tosize,tmpref,mmreg,shuffle);
  2195. tg.ungettemp(list,tmpref);
  2196. end;
  2197. procedure tcg.a_loadmm_reg_intreg(list: tasmlist; fromsize,tosize: tcgsize; mmreg,intreg: tregister; shuffle: pmmshuffle);
  2198. var
  2199. tmpref: treference;
  2200. begin
  2201. if (tcgsize2size[fromsize]<>4) or
  2202. (tcgsize2size[tosize]<>4) then
  2203. internalerror(2009112504);
  2204. tg.gettemp(list,8,8,tt_normal,tmpref);
  2205. a_loadmm_reg_ref(list,fromsize,fromsize,mmreg,tmpref,shuffle);
  2206. a_load_ref_reg(list,tosize,tosize,tmpref,intreg);
  2207. tg.ungettemp(list,tmpref);
  2208. end;
  2209. procedure tcg.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  2210. begin
  2211. case loc.loc of
  2212. LOC_CMMREGISTER,LOC_MMREGISTER:
  2213. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  2214. LOC_CREFERENCE,LOC_REFERENCE:
  2215. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  2216. else
  2217. internalerror(200312232);
  2218. end;
  2219. end;
  2220. procedure tcg.a_opmm_loc_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; src,dst: tregister;shuffle : pmmshuffle);
  2221. begin
  2222. case loc.loc of
  2223. LOC_CMMREGISTER,LOC_MMREGISTER:
  2224. a_opmm_reg_reg_reg(list,op,size,loc.register,src,dst,shuffle);
  2225. LOC_CREFERENCE,LOC_REFERENCE:
  2226. a_opmm_ref_reg_reg(list,op,size,loc.reference,src,dst,shuffle);
  2227. else
  2228. internalerror(200312232);
  2229. end;
  2230. end;
  2231. procedure tcg.a_opmm_reg_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;
  2232. src1,src2,dst : tregister;shuffle : pmmshuffle);
  2233. begin
  2234. internalerror(2013061102);
  2235. end;
  2236. procedure tcg.a_opmm_ref_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;
  2237. const ref : treference;src,dst : tregister;shuffle : pmmshuffle);
  2238. begin
  2239. internalerror(2013061101);
  2240. end;
  2241. procedure tcg.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  2242. begin
  2243. g_concatcopy(list,source,dest,len);
  2244. end;
  2245. procedure tcg.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  2246. begin
  2247. g_overflowCheck(list,loc,def);
  2248. end;
  2249. {$ifdef cpuflags}
  2250. procedure tcg.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference);
  2251. var
  2252. tmpreg : tregister;
  2253. begin
  2254. tmpreg:=getintregister(list,size);
  2255. g_flags2reg(list,size,f,tmpreg);
  2256. a_load_reg_ref(list,size,size,tmpreg,ref);
  2257. end;
  2258. {$endif cpuflags}
  2259. {*****************************************************************************
  2260. Entry/Exit Code Functions
  2261. *****************************************************************************}
  2262. procedure tcg.g_save_registers(list:TAsmList);
  2263. var
  2264. href : treference;
  2265. size : longint;
  2266. r : integer;
  2267. regs_to_save_int,
  2268. regs_to_save_address,
  2269. regs_to_save_mm : tcpuregisterarray;
  2270. begin
  2271. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  2272. regs_to_save_address:=paramanager.get_saved_registers_address(current_procinfo.procdef.proccalloption);
  2273. regs_to_save_mm:=paramanager.get_saved_registers_mm(current_procinfo.procdef.proccalloption);
  2274. { calculate temp. size }
  2275. size:=0;
  2276. for r:=low(regs_to_save_int) to high(regs_to_save_int) do
  2277. if regs_to_save_int[r] in rg[R_INTREGISTER].used_in_proc then
  2278. inc(size,sizeof(aint));
  2279. if uses_registers(R_ADDRESSREGISTER) then
  2280. for r:=low(regs_to_save_int) to high(regs_to_save_int) do
  2281. if regs_to_save_int[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2282. inc(size,sizeof(aint));
  2283. { mm registers }
  2284. if uses_registers(R_MMREGISTER) then
  2285. begin
  2286. { Make sure we reserve enough space to do the alignment based on the offset
  2287. later on. We can't use the size for this, because the alignment of the start
  2288. of the temp is smaller than needed for an OS_VECTOR }
  2289. inc(size,tcgsize2size[OS_VECTOR]);
  2290. for r:=low(regs_to_save_mm) to high(regs_to_save_mm) do
  2291. if regs_to_save_mm[r] in rg[R_MMREGISTER].used_in_proc then
  2292. inc(size,tcgsize2size[OS_VECTOR]);
  2293. end;
  2294. if size>0 then
  2295. begin
  2296. tg.GetTemp(list,size,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  2297. include(current_procinfo.flags,pi_has_saved_regs);
  2298. { Copy registers to temp }
  2299. href:=current_procinfo.save_regs_ref;
  2300. for r:=low(regs_to_save_int) to high(regs_to_save_int) do
  2301. begin
  2302. if regs_to_save_int[r] in rg[R_INTREGISTER].used_in_proc then
  2303. begin
  2304. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE),href);
  2305. inc(href.offset,sizeof(aint));
  2306. end;
  2307. include(rg[R_INTREGISTER].preserved_by_proc,regs_to_save_int[r]);
  2308. end;
  2309. if uses_registers(R_ADDRESSREGISTER) then
  2310. for r:=low(regs_to_save_address) to high(regs_to_save_address) do
  2311. begin
  2312. if regs_to_save_address[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2313. begin
  2314. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_ADDRESSREGISTER,regs_to_save_address[r],R_SUBWHOLE),href);
  2315. inc(href.offset,sizeof(aint));
  2316. end;
  2317. include(rg[R_ADDRESSREGISTER].preserved_by_proc,regs_to_save_address[r]);
  2318. end;
  2319. if uses_registers(R_MMREGISTER) then
  2320. begin
  2321. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  2322. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  2323. for r:=low(regs_to_save_mm) to high(regs_to_save_mm) do
  2324. begin
  2325. { the array has to be declared even if no MM registers are saved
  2326. (such as with SSE on i386), and since 0-element arrays don't
  2327. exist, they contain a single RS_INVALID element in that case
  2328. }
  2329. if regs_to_save_mm[r]<>RS_INVALID then
  2330. begin
  2331. if regs_to_save_mm[r] in rg[R_MMREGISTER].used_in_proc then
  2332. begin
  2333. a_loadmm_reg_ref(list,OS_VECTOR,OS_VECTOR,newreg(R_MMREGISTER,regs_to_save_mm[r],R_SUBMMWHOLE),href,nil);
  2334. inc(href.offset,tcgsize2size[OS_VECTOR]);
  2335. end;
  2336. include(rg[R_MMREGISTER].preserved_by_proc,regs_to_save_mm[r]);
  2337. end;
  2338. end;
  2339. end;
  2340. end;
  2341. end;
  2342. procedure tcg.g_restore_registers(list:TAsmList);
  2343. var
  2344. href : treference;
  2345. r : integer;
  2346. hreg : tregister;
  2347. regs_to_save_int,
  2348. regs_to_save_address,
  2349. regs_to_save_mm : tcpuregisterarray;
  2350. begin
  2351. if not(pi_has_saved_regs in current_procinfo.flags) then
  2352. exit;
  2353. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  2354. regs_to_save_address:=paramanager.get_saved_registers_address(current_procinfo.procdef.proccalloption);
  2355. regs_to_save_mm:=paramanager.get_saved_registers_mm(current_procinfo.procdef.proccalloption);
  2356. { Copy registers from temp }
  2357. href:=current_procinfo.save_regs_ref;
  2358. for r:=low(regs_to_save_int) to high(regs_to_save_int) do
  2359. if regs_to_save_int[r] in rg[R_INTREGISTER].used_in_proc then
  2360. begin
  2361. hreg:=newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE);
  2362. { Allocate register so the optimizer does not remove the load }
  2363. a_reg_alloc(list,hreg);
  2364. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2365. inc(href.offset,sizeof(aint));
  2366. end;
  2367. if uses_registers(R_ADDRESSREGISTER) then
  2368. for r:=low(regs_to_save_address) to high(regs_to_save_address) do
  2369. if regs_to_save_address[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2370. begin
  2371. hreg:=newreg(R_ADDRESSREGISTER,regs_to_save_address[r],R_SUBWHOLE);
  2372. { Allocate register so the optimizer does not remove the load }
  2373. a_reg_alloc(list,hreg);
  2374. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2375. inc(href.offset,sizeof(aint));
  2376. end;
  2377. if uses_registers(R_MMREGISTER) then
  2378. begin
  2379. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  2380. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  2381. for r:=low(regs_to_save_mm) to high(regs_to_save_mm) do
  2382. begin
  2383. if regs_to_save_mm[r] in rg[R_MMREGISTER].used_in_proc then
  2384. begin
  2385. hreg:=newreg(R_MMREGISTER,regs_to_save_mm[r],R_SUBMMWHOLE);
  2386. { Allocate register so the optimizer does not remove the load }
  2387. a_reg_alloc(list,hreg);
  2388. a_loadmm_ref_reg(list,OS_VECTOR,OS_VECTOR,href,hreg,nil);
  2389. inc(href.offset,tcgsize2size[OS_VECTOR]);
  2390. end;
  2391. end;
  2392. end;
  2393. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  2394. end;
  2395. procedure tcg.g_profilecode(list : TAsmList);
  2396. begin
  2397. end;
  2398. procedure tcg.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  2399. var
  2400. hsym : tsym;
  2401. href : treference;
  2402. paraloc : Pcgparalocation;
  2403. begin
  2404. { calculate the parameter info for the procdef }
  2405. procdef.init_paraloc_info(callerside);
  2406. hsym:=tsym(procdef.parast.Find('self'));
  2407. if not(assigned(hsym) and
  2408. (hsym.typ=paravarsym)) then
  2409. internalerror(200305251);
  2410. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  2411. while paraloc<>nil do
  2412. with paraloc^ do
  2413. begin
  2414. case loc of
  2415. LOC_REGISTER:
  2416. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  2417. LOC_REFERENCE:
  2418. begin
  2419. { offset in the wrapper needs to be adjusted for the stored
  2420. return address }
  2421. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),ctempposinvalid,sizeof(pint),[]);
  2422. a_op_const_ref(list,OP_SUB,size,ioffset,href);
  2423. end
  2424. else
  2425. internalerror(200309189);
  2426. end;
  2427. paraloc:=next;
  2428. end;
  2429. end;
  2430. procedure tcg.a_call_name_static(list : TAsmList;const s : string);
  2431. begin
  2432. a_call_name(list,s,false);
  2433. end;
  2434. function tcg.g_indirect_sym_load(list:TAsmList;const symname: string; const flags: tindsymflags): tregister;
  2435. var
  2436. l: tasmsymbol;
  2437. ref: treference;
  2438. nlsymname: string;
  2439. symtyp: TAsmsymtype;
  2440. begin
  2441. result := NR_NO;
  2442. case target_info.system of
  2443. system_powerpc_darwin,
  2444. system_i386_darwin,
  2445. system_i386_iphonesim,
  2446. system_powerpc64_darwin,
  2447. system_arm_darwin:
  2448. begin
  2449. nlsymname:='L'+symname+'$non_lazy_ptr';
  2450. l:=current_asmdata.getasmsymbol(nlsymname);
  2451. if not(assigned(l)) then
  2452. begin
  2453. if is_data in flags then
  2454. symtyp:=AT_DATA
  2455. else
  2456. symtyp:=AT_FUNCTION;
  2457. new_section(current_asmdata.asmlists[al_picdata],sec_data_nonlazy,'',sizeof(pint));
  2458. l:=current_asmdata.DefineAsmSymbol(nlsymname,AB_LOCAL,AT_DATA,voidpointertype);
  2459. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create(l,0));
  2460. if not(is_weak in flags) then
  2461. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.RefAsmSymbol(symname,symtyp).Name))
  2462. else
  2463. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.WeakRefAsmSymbol(symname,symtyp).Name));
  2464. {$ifdef cpu64bitaddr}
  2465. current_asmdata.asmlists[al_picdata].concat(tai_const.create_64bit(0));
  2466. {$else cpu64bitaddr}
  2467. current_asmdata.asmlists[al_picdata].concat(tai_const.create_32bit(0));
  2468. {$endif cpu64bitaddr}
  2469. end;
  2470. result := getaddressregister(list);
  2471. reference_reset_symbol(ref,l,0,sizeof(pint),[]);
  2472. { a_load_ref_reg will turn this into a pic-load if needed }
  2473. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,result);
  2474. end;
  2475. end;
  2476. end;
  2477. procedure tcg.g_maybe_got_init(list: TAsmList);
  2478. begin
  2479. end;
  2480. procedure tcg.g_call(list: TAsmList;const s: string);
  2481. begin
  2482. allocallcpuregisters(list);
  2483. a_call_name(list,s,false);
  2484. deallocallcpuregisters(list);
  2485. end;
  2486. procedure tcg.g_local_unwind(list: TAsmList; l: TAsmLabel);
  2487. begin
  2488. a_jmp_always(list,l);
  2489. end;
  2490. procedure tcg.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  2491. begin
  2492. internalerror(200807231);
  2493. end;
  2494. procedure tcg.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2495. begin
  2496. internalerror(200807232);
  2497. end;
  2498. procedure tcg.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2499. begin
  2500. internalerror(200807233);
  2501. end;
  2502. procedure tcg.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  2503. begin
  2504. internalerror(200807234);
  2505. end;
  2506. function tcg.getflagregister(list: TAsmList; size: Tcgsize): Tregister;
  2507. begin
  2508. Result:=TRegister(0);
  2509. internalerror(200807238);
  2510. end;
  2511. procedure tcg.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tcgsize; src, dst: TRegister);
  2512. begin
  2513. internalerror(2014070601);
  2514. end;
  2515. procedure tcg.g_stackpointer_alloc(list: TAsmList; size: longint);
  2516. begin
  2517. internalerror(2014070602);
  2518. end;
  2519. procedure tcg.a_mul_reg_reg_pair(list: TAsmList; size: TCgSize; src1,src2,dstlo,dsthi: TRegister);
  2520. begin
  2521. internalerror(2014060801);
  2522. end;
  2523. procedure tcg.g_div_const_reg_reg(list:tasmlist; size: TCgSize; a: tcgint; src,dst: tregister);
  2524. var
  2525. divreg: tregister;
  2526. magic: aInt;
  2527. u_magic: aWord;
  2528. u_shift: byte;
  2529. u_add: boolean;
  2530. begin
  2531. divreg:=getintregister(list,OS_INT);
  2532. if (size in [OS_S32,OS_S64]) then
  2533. begin
  2534. calc_divconst_magic_signed(tcgsize2size[size]*8,a,magic,u_shift);
  2535. { load magic value }
  2536. a_load_const_reg(list,OS_INT,magic,divreg);
  2537. { multiply, discarding low bits }
  2538. a_mul_reg_reg_pair(list,size,src,divreg,NR_NO,dst);
  2539. { add/subtract numerator }
  2540. if (a>0) and (magic<0) then
  2541. a_op_reg_reg_reg(list,OP_ADD,OS_INT,src,dst,dst)
  2542. else if (a<0) and (magic>0) then
  2543. a_op_reg_reg_reg(list,OP_SUB,OS_INT,src,dst,dst);
  2544. { shift shift places to the right (arithmetic) }
  2545. a_op_const_reg_reg(list,OP_SAR,OS_INT,u_shift,dst,dst);
  2546. { extract and add sign bit }
  2547. if (a>=0) then
  2548. a_op_const_reg_reg(list,OP_SHR,OS_INT,tcgsize2size[size]*8-1,src,divreg)
  2549. else
  2550. a_op_const_reg_reg(list,OP_SHR,OS_INT,tcgsize2size[size]*8-1,dst,divreg);
  2551. a_op_reg_reg_reg(list,OP_ADD,OS_INT,dst,divreg,dst);
  2552. end
  2553. else if (size in [OS_32,OS_64]) then
  2554. begin
  2555. calc_divconst_magic_unsigned(tcgsize2size[size]*8,a,u_magic,u_add,u_shift);
  2556. { load magic in divreg }
  2557. a_load_const_reg(list,OS_INT,tcgint(u_magic),divreg);
  2558. { multiply, discarding low bits }
  2559. a_mul_reg_reg_pair(list,size,src,divreg,NR_NO,dst);
  2560. if (u_add) then
  2561. begin
  2562. { Calculate "(numerator+result) shr u_shift", avoiding possible overflow }
  2563. a_op_reg_reg_reg(list,OP_SUB,OS_INT,dst,src,divreg);
  2564. { divreg=(numerator-result) }
  2565. a_op_const_reg_reg(list,OP_SHR,OS_INT,1,divreg,divreg);
  2566. { divreg=(numerator-result)/2 }
  2567. a_op_reg_reg_reg(list,OP_ADD,OS_INT,divreg,dst,divreg);
  2568. { divreg=(numerator+result)/2, already shifted by 1, so decrease u_shift. }
  2569. a_op_const_reg_reg(list,OP_SHR,OS_INT,u_shift-1,divreg,dst);
  2570. end
  2571. else
  2572. a_op_const_reg_reg(list,OP_SHR,OS_INT,u_shift,dst,dst);
  2573. end
  2574. else
  2575. InternalError(2014060601);
  2576. end;
  2577. procedure tcg.g_check_for_fpu_exception(list: TAsmList);
  2578. begin
  2579. { empty by default }
  2580. end;
  2581. {*****************************************************************************
  2582. TCG64
  2583. *****************************************************************************}
  2584. {$ifndef cpu64bitalu}
  2585. function joinreg64(reglo,reghi : tregister) : tregister64;
  2586. begin
  2587. result.reglo:=reglo;
  2588. result.reghi:=reghi;
  2589. end;
  2590. procedure tcg64.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64; regsrc,regdst : tregister64);
  2591. begin
  2592. a_load64_reg_reg(list,regsrc,regdst);
  2593. a_op64_const_reg(list,op,size,value,regdst);
  2594. end;
  2595. procedure tcg64.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  2596. var
  2597. tmpreg64 : tregister64;
  2598. begin
  2599. { when src1=dst then we need to first create a temp to prevent
  2600. overwriting src1 with src2 }
  2601. if (regsrc1.reghi=regdst.reghi) or
  2602. (regsrc1.reglo=regdst.reghi) or
  2603. (regsrc1.reghi=regdst.reglo) or
  2604. (regsrc1.reglo=regdst.reglo) then
  2605. begin
  2606. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2607. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2608. a_load64_reg_reg(list,regsrc2,tmpreg64);
  2609. a_op64_reg_reg(list,op,size,regsrc1,tmpreg64);
  2610. a_load64_reg_reg(list,tmpreg64,regdst);
  2611. end
  2612. else
  2613. begin
  2614. a_load64_reg_reg(list,regsrc2,regdst);
  2615. a_op64_reg_reg(list,op,size,regsrc1,regdst);
  2616. end;
  2617. end;
  2618. procedure tcg64.a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; 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_const_reg(list,op,size,a,tmpreg64);
  2626. a_load64_reg_subsetref(list,tmpreg64,sref);
  2627. end;
  2628. procedure tcg64.a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; 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_reg_reg(list,op,size,reg,tmpreg64);
  2636. a_load64_reg_subsetref(list,tmpreg64,sref);
  2637. end;
  2638. procedure tcg64.a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: 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,sref,tmpreg64);
  2645. a_op64_ref_reg(list,op,size,ref,tmpreg64);
  2646. a_load64_reg_subsetref(list,tmpreg64,sref);
  2647. end;
  2648. procedure tcg64.a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  2649. var
  2650. tmpreg64 : tregister64;
  2651. begin
  2652. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2653. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2654. a_load64_subsetref_reg(list,ssref,tmpreg64);
  2655. a_op64_reg_subsetref(list,op,size,tmpreg64,dsref);
  2656. end;
  2657. procedure tcg64.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2658. begin
  2659. a_op64_const_reg_reg(list,op,size,value,regsrc,regdst);
  2660. ovloc.loc:=LOC_VOID;
  2661. end;
  2662. procedure tcg64.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2663. begin
  2664. a_op64_reg_reg_reg(list,op,size,regsrc1,regsrc2,regdst);
  2665. ovloc.loc:=LOC_VOID;
  2666. end;
  2667. procedure tcg64.a_load64_loc_subsetref(list : TAsmList;const l: tlocation; const sref : tsubsetreference);
  2668. begin
  2669. case l.loc of
  2670. LOC_REFERENCE, LOC_CREFERENCE:
  2671. a_load64_ref_subsetref(list,l.reference,sref);
  2672. LOC_REGISTER,LOC_CREGISTER:
  2673. a_load64_reg_subsetref(list,l.register64,sref);
  2674. LOC_CONSTANT :
  2675. a_load64_const_subsetref(list,l.value64,sref);
  2676. LOC_SUBSETREF,LOC_CSUBSETREF:
  2677. a_load64_subsetref_subsetref(list,l.sref,sref);
  2678. else
  2679. internalerror(2006082210);
  2680. end;
  2681. end;
  2682. procedure tcg64.a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  2683. begin
  2684. case l.loc of
  2685. LOC_REFERENCE, LOC_CREFERENCE:
  2686. a_load64_subsetref_ref(list,sref,l.reference);
  2687. LOC_REGISTER,LOC_CREGISTER:
  2688. a_load64_subsetref_reg(list,sref,l.register64);
  2689. LOC_SUBSETREF,LOC_CSUBSETREF:
  2690. a_load64_subsetref_subsetref(list,sref,l.sref);
  2691. else
  2692. internalerror(2006082211);
  2693. end;
  2694. end;
  2695. {$else cpu64bitalu}
  2696. function joinreg128(reglo, reghi: tregister): tregister128;
  2697. begin
  2698. result.reglo:=reglo;
  2699. result.reghi:=reghi;
  2700. end;
  2701. procedure splitparaloc128(const cgpara:tcgpara;var cgparalo,cgparahi:tcgpara);
  2702. var
  2703. paraloclo,
  2704. paralochi : pcgparalocation;
  2705. begin
  2706. if not(cgpara.size in [OS_128,OS_S128]) then
  2707. internalerror(2012090604);
  2708. if not assigned(cgpara.location) then
  2709. internalerror(2012090605);
  2710. { init lo/hi para }
  2711. cgparahi.reset;
  2712. if cgpara.size=OS_S128 then
  2713. cgparahi.size:=OS_S64
  2714. else
  2715. cgparahi.size:=OS_64;
  2716. cgparahi.intsize:=8;
  2717. cgparahi.alignment:=cgpara.alignment;
  2718. paralochi:=cgparahi.add_location;
  2719. cgparalo.reset;
  2720. cgparalo.size:=OS_64;
  2721. cgparalo.intsize:=8;
  2722. cgparalo.alignment:=cgpara.alignment;
  2723. paraloclo:=cgparalo.add_location;
  2724. { 2 parameter fields? }
  2725. if assigned(cgpara.location^.next) then
  2726. begin
  2727. { Order for multiple locations is always
  2728. paraloc^ -> high
  2729. paraloc^.next -> low }
  2730. if (target_info.endian=ENDIAN_BIG) then
  2731. begin
  2732. { paraloc^ -> high
  2733. paraloc^.next -> low }
  2734. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2735. move(cgpara.location^.next^,paraloclo^,sizeof(paraloclo^));
  2736. end
  2737. else
  2738. begin
  2739. { paraloc^ -> low
  2740. paraloc^.next -> high }
  2741. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2742. move(cgpara.location^.next^,paralochi^,sizeof(paralochi^));
  2743. end;
  2744. end
  2745. else
  2746. begin
  2747. { single parameter, this can only be in memory }
  2748. if cgpara.location^.loc<>LOC_REFERENCE then
  2749. internalerror(2012090606);
  2750. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2751. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2752. { for big endian low is at +8, for little endian high }
  2753. if target_info.endian = endian_big then
  2754. begin
  2755. inc(cgparalo.location^.reference.offset,8);
  2756. cgparalo.alignment:=newalignment(cgparalo.alignment,8);
  2757. end
  2758. else
  2759. begin
  2760. inc(cgparahi.location^.reference.offset,8);
  2761. cgparahi.alignment:=newalignment(cgparahi.alignment,8);
  2762. end;
  2763. end;
  2764. { fix size }
  2765. paraloclo^.size:=cgparalo.size;
  2766. paraloclo^.next:=nil;
  2767. paralochi^.size:=cgparahi.size;
  2768. paralochi^.next:=nil;
  2769. end;
  2770. procedure tcg128.a_load128_reg_reg(list: TAsmList; regsrc,
  2771. regdst: tregister128);
  2772. begin
  2773. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reglo,regdst.reglo);
  2774. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reghi,regdst.reghi);
  2775. end;
  2776. procedure tcg128.a_load128_reg_ref(list: TAsmList; reg: tregister128;
  2777. const ref: treference);
  2778. var
  2779. tmpreg: tregister;
  2780. tmpref: treference;
  2781. begin
  2782. if target_info.endian = endian_big then
  2783. begin
  2784. tmpreg:=reg.reglo;
  2785. reg.reglo:=reg.reghi;
  2786. reg.reghi:=tmpreg;
  2787. end;
  2788. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reglo,ref);
  2789. tmpref := ref;
  2790. inc(tmpref.offset,8);
  2791. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reghi,tmpref);
  2792. end;
  2793. procedure tcg128.a_load128_ref_reg(list: TAsmList; const ref: treference;
  2794. reg: tregister128);
  2795. var
  2796. tmpreg: tregister;
  2797. tmpref: treference;
  2798. begin
  2799. if target_info.endian = endian_big then
  2800. begin
  2801. tmpreg := reg.reglo;
  2802. reg.reglo := reg.reghi;
  2803. reg.reghi := tmpreg;
  2804. end;
  2805. tmpref := ref;
  2806. if (tmpref.base=reg.reglo) then
  2807. begin
  2808. tmpreg:=cg.getaddressregister(list);
  2809. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.base,tmpreg);
  2810. tmpref.base:=tmpreg;
  2811. end
  2812. else
  2813. { this works only for the i386, thus the i386 needs to override }
  2814. { this method and this method must be replaced by a more generic }
  2815. { implementation FK }
  2816. if (tmpref.index=reg.reglo) then
  2817. begin
  2818. tmpreg:=cg.getaddressregister(list);
  2819. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.index,tmpreg);
  2820. tmpref.index:=tmpreg;
  2821. end;
  2822. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reglo);
  2823. inc(tmpref.offset,8);
  2824. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reghi);
  2825. end;
  2826. procedure tcg128.a_load128_loc_ref(list: TAsmList; const l: tlocation;
  2827. const ref: treference);
  2828. begin
  2829. case l.loc of
  2830. LOC_REGISTER,LOC_CREGISTER:
  2831. a_load128_reg_ref(list,l.register128,ref);
  2832. { not yet implemented:
  2833. LOC_CONSTANT :
  2834. a_load128_const_ref(list,l.value128,ref);
  2835. LOC_SUBSETREF, LOC_CSUBSETREF:
  2836. a_load64_subsetref_ref(list,l.sref,ref); }
  2837. else
  2838. internalerror(201209061);
  2839. end;
  2840. end;
  2841. procedure tcg128.a_load128_reg_loc(list: TAsmList; reg: tregister128;
  2842. const l: tlocation);
  2843. begin
  2844. case l.loc of
  2845. LOC_REFERENCE, LOC_CREFERENCE:
  2846. a_load128_reg_ref(list,reg,l.reference);
  2847. LOC_REGISTER,LOC_CREGISTER:
  2848. a_load128_reg_reg(list,reg,l.register128);
  2849. { not yet implemented:
  2850. LOC_SUBSETREF, LOC_CSUBSETREF:
  2851. a_load64_reg_subsetref(list,reg,l.sref);
  2852. LOC_MMREGISTER, LOC_CMMREGISTER:
  2853. a_loadmm_intreg64_reg(list,l.size,reg,l.register); }
  2854. else
  2855. internalerror(201209062);
  2856. end;
  2857. end;
  2858. procedure tcg128.a_load128_const_reg(list: TAsmList; valuelo,
  2859. valuehi: int64; reg: tregister128);
  2860. begin
  2861. cg.a_load_const_reg(list,OS_64,aint(valuelo),reg.reglo);
  2862. cg.a_load_const_reg(list,OS_64,aint(valuehi),reg.reghi);
  2863. end;
  2864. procedure tcg128.a_load128_loc_cgpara(list: TAsmList; const l: tlocation;
  2865. const paraloc: TCGPara);
  2866. begin
  2867. case l.loc of
  2868. LOC_REGISTER,
  2869. LOC_CREGISTER :
  2870. a_load128_reg_cgpara(list,l.register128,paraloc);
  2871. {not yet implemented:
  2872. LOC_CONSTANT :
  2873. a_load128_const_cgpara(list,l.value64,paraloc);
  2874. }
  2875. LOC_CREFERENCE,
  2876. LOC_REFERENCE :
  2877. a_load128_ref_cgpara(list,l.reference,paraloc);
  2878. else
  2879. internalerror(2012090603);
  2880. end;
  2881. end;
  2882. procedure tcg128.a_load128_reg_cgpara(list : TAsmList;reg : tregister128;const paraloc : tcgpara);
  2883. var
  2884. tmplochi,tmploclo: tcgpara;
  2885. begin
  2886. tmploclo.init;
  2887. tmplochi.init;
  2888. splitparaloc128(paraloc,tmploclo,tmplochi);
  2889. cg.a_load_reg_cgpara(list,OS_64,reg.reghi,tmplochi);
  2890. cg.a_load_reg_cgpara(list,OS_64,reg.reglo,tmploclo);
  2891. tmploclo.done;
  2892. tmplochi.done;
  2893. end;
  2894. procedure tcg128.a_load128_ref_cgpara(list : TAsmList;const r : treference;const paraloc : tcgpara);
  2895. var
  2896. tmprefhi,tmpreflo : treference;
  2897. tmploclo,tmplochi : tcgpara;
  2898. begin
  2899. tmploclo.init;
  2900. tmplochi.init;
  2901. splitparaloc128(paraloc,tmploclo,tmplochi);
  2902. tmprefhi:=r;
  2903. tmpreflo:=r;
  2904. if target_info.endian=endian_big then
  2905. inc(tmpreflo.offset,8)
  2906. else
  2907. inc(tmprefhi.offset,8);
  2908. cg.a_load_ref_cgpara(list,OS_64,tmprefhi,tmplochi);
  2909. cg.a_load_ref_cgpara(list,OS_64,tmpreflo,tmploclo);
  2910. tmploclo.done;
  2911. tmplochi.done;
  2912. end;
  2913. {$endif cpu64bitalu}
  2914. function asmsym2indsymflags(sym: TAsmSymbol): tindsymflags;
  2915. begin
  2916. result:=[];
  2917. if sym.typ<>AT_FUNCTION then
  2918. include(result,is_data);
  2919. if sym.bind=AB_WEAK_EXTERNAL then
  2920. include(result,is_weak);
  2921. end;
  2922. procedure destroy_codegen;
  2923. begin
  2924. cg.free;
  2925. cg:=nil;
  2926. {$ifdef cpu64bitalu}
  2927. cg128.free;
  2928. cg128:=nil;
  2929. {$else cpu64bitalu}
  2930. cg64.free;
  2931. cg64:=nil;
  2932. {$endif cpu64bitalu}
  2933. end;
  2934. end.