cgobj.pas 132 KB

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