cgobj.pas 136 KB

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