cgobj.pas 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193
  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. else
  1461. internalerror(200109092);
  1462. end;
  1463. end;
  1464. procedure tcg.a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  1465. begin
  1466. case loc.loc of
  1467. LOC_REFERENCE,LOC_CREFERENCE:
  1468. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  1469. LOC_REGISTER,LOC_CREGISTER:
  1470. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  1471. LOC_CONSTANT:
  1472. a_load_const_ref(list,tosize,loc.value,ref);
  1473. else
  1474. internalerror(200109302);
  1475. end;
  1476. end;
  1477. procedure tcg.optimize_op_const(size: TCGSize; var op: topcg; var a : tcgint);
  1478. var
  1479. powerval : longint;
  1480. signext_a, zeroext_a: tcgint;
  1481. begin
  1482. case size of
  1483. OS_64,OS_S64:
  1484. begin
  1485. signext_a:=int64(a);
  1486. zeroext_a:=int64(a);
  1487. end;
  1488. OS_32,OS_S32:
  1489. begin
  1490. signext_a:=longint(a);
  1491. zeroext_a:=dword(a);
  1492. end;
  1493. OS_16,OS_S16:
  1494. begin
  1495. signext_a:=smallint(a);
  1496. zeroext_a:=word(a);
  1497. end;
  1498. OS_8,OS_S8:
  1499. begin
  1500. signext_a:=shortint(a);
  1501. zeroext_a:=byte(a);
  1502. end
  1503. else
  1504. begin
  1505. { Should we internalerror() here instead? }
  1506. signext_a:=a;
  1507. zeroext_a:=a;
  1508. end;
  1509. end;
  1510. case op of
  1511. OP_OR :
  1512. begin
  1513. { or with zero returns same result }
  1514. if a = 0 then
  1515. op:=OP_NONE
  1516. else
  1517. { or with max returns max }
  1518. if signext_a = -1 then
  1519. op:=OP_MOVE;
  1520. end;
  1521. OP_AND :
  1522. begin
  1523. { and with max returns same result }
  1524. if (signext_a = -1) then
  1525. op:=OP_NONE
  1526. else
  1527. { and with 0 returns 0 }
  1528. if a=0 then
  1529. op:=OP_MOVE;
  1530. end;
  1531. OP_XOR :
  1532. begin
  1533. { xor with zero returns same result }
  1534. if a = 0 then
  1535. op:=OP_NONE;
  1536. end;
  1537. OP_DIV :
  1538. begin
  1539. { division by 1 returns result }
  1540. if a = 1 then
  1541. op:=OP_NONE
  1542. else if ispowerof2(int64(zeroext_a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  1543. begin
  1544. a := powerval;
  1545. op:= OP_SHR;
  1546. end;
  1547. end;
  1548. OP_IDIV:
  1549. begin
  1550. if a = 1 then
  1551. op:=OP_NONE;
  1552. end;
  1553. OP_MUL,OP_IMUL:
  1554. begin
  1555. if a = 1 then
  1556. op:=OP_NONE
  1557. else
  1558. if a=0 then
  1559. op:=OP_MOVE
  1560. else if ispowerof2(int64(zeroext_a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  1561. begin
  1562. a := powerval;
  1563. op:= OP_SHL;
  1564. end;
  1565. end;
  1566. OP_ADD,OP_SUB:
  1567. begin
  1568. if a = 0 then
  1569. op:=OP_NONE;
  1570. end;
  1571. OP_SAR,OP_SHL,OP_SHR:
  1572. begin
  1573. if a = 0 then
  1574. op:=OP_NONE;
  1575. end;
  1576. OP_ROL,OP_ROR:
  1577. begin
  1578. case size of
  1579. OS_64,OS_S64:
  1580. a:=a and 63;
  1581. OS_32,OS_S32:
  1582. a:=a and 31;
  1583. OS_16,OS_S16:
  1584. a:=a and 15;
  1585. OS_8,OS_S8:
  1586. a:=a and 7;
  1587. end;
  1588. if a = 0 then
  1589. op:=OP_NONE;
  1590. end;
  1591. end;
  1592. end;
  1593. procedure tcg.a_loadfpu_loc_reg(list: TAsmList; tosize: tcgsize; const loc: tlocation; const reg: tregister);
  1594. begin
  1595. case loc.loc of
  1596. LOC_REFERENCE, LOC_CREFERENCE:
  1597. a_loadfpu_ref_reg(list,loc.size,tosize,loc.reference,reg);
  1598. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1599. a_loadfpu_reg_reg(list,loc.size,tosize,loc.register,reg);
  1600. else
  1601. internalerror(200203301);
  1602. end;
  1603. end;
  1604. procedure tcg.a_loadfpu_reg_loc(list: TAsmList; fromsize: tcgsize; const reg: tregister; const loc: tlocation);
  1605. begin
  1606. case loc.loc of
  1607. LOC_REFERENCE, LOC_CREFERENCE:
  1608. a_loadfpu_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  1609. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1610. a_loadfpu_reg_reg(list,fromsize,loc.size,reg,loc.register);
  1611. else
  1612. internalerror(48991);
  1613. end;
  1614. end;
  1615. procedure tcg.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tcgsize; const ref1,ref2: treference);
  1616. var
  1617. reg: tregister;
  1618. regsize: tcgsize;
  1619. begin
  1620. if (fromsize>=tosize) then
  1621. regsize:=fromsize
  1622. else
  1623. regsize:=tosize;
  1624. reg:=getfpuregister(list,regsize);
  1625. a_loadfpu_ref_reg(list,fromsize,regsize,ref1,reg);
  1626. a_loadfpu_reg_ref(list,regsize,tosize,reg,ref2);
  1627. end;
  1628. procedure tcg.a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);
  1629. var
  1630. ref : treference;
  1631. begin
  1632. paramanager.alloccgpara(list,cgpara);
  1633. case cgpara.location^.loc of
  1634. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1635. begin
  1636. cgpara.check_simple_location;
  1637. a_loadfpu_reg_reg(list,size,size,r,cgpara.location^.register);
  1638. end;
  1639. LOC_REFERENCE,LOC_CREFERENCE:
  1640. begin
  1641. cgpara.check_simple_location;
  1642. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment,[]);
  1643. a_loadfpu_reg_ref(list,size,size,r,ref);
  1644. end;
  1645. LOC_REGISTER,LOC_CREGISTER:
  1646. begin
  1647. { paramfpu_ref does the check_simpe_location check here if necessary }
  1648. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,ref);
  1649. a_loadfpu_reg_ref(list,size,size,r,ref);
  1650. a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  1651. tg.Ungettemp(list,ref);
  1652. end;
  1653. else
  1654. internalerror(2010053112);
  1655. end;
  1656. end;
  1657. procedure tcg.a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);
  1658. var
  1659. href : treference;
  1660. hsize: tcgsize;
  1661. paraloc: PCGParaLocation;
  1662. begin
  1663. case cgpara.location^.loc of
  1664. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1665. begin
  1666. paramanager.alloccgpara(list,cgpara);
  1667. paraloc:=cgpara.location;
  1668. href:=ref;
  1669. while assigned(paraloc) do
  1670. begin
  1671. if not(paraloc^.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  1672. internalerror(2015031501);
  1673. a_loadfpu_ref_reg(list,paraloc^.size,paraloc^.size,href,paraloc^.register);
  1674. inc(href.offset,tcgsize2size[paraloc^.size]);
  1675. paraloc:=paraloc^.next;
  1676. end;
  1677. end;
  1678. LOC_REFERENCE,LOC_CREFERENCE:
  1679. begin
  1680. cgpara.check_simple_location;
  1681. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment,[]);
  1682. { concatcopy should choose the best way to copy the data }
  1683. g_concatcopy(list,ref,href,tcgsize2size[size]);
  1684. end;
  1685. LOC_REGISTER,LOC_CREGISTER:
  1686. begin
  1687. { force integer size }
  1688. hsize:=int_cgsize(tcgsize2size[size]);
  1689. {$ifndef cpu64bitalu}
  1690. if (hsize in [OS_S64,OS_64]) then
  1691. cg64.a_load64_ref_cgpara(list,ref,cgpara)
  1692. else
  1693. {$endif not cpu64bitalu}
  1694. begin
  1695. cgpara.check_simple_location;
  1696. a_load_ref_cgpara(list,hsize,ref,cgpara)
  1697. end;
  1698. end
  1699. else
  1700. internalerror(200402201);
  1701. end;
  1702. end;
  1703. procedure tcg.a_loadfpu_intreg_reg(list : TAsmList; fromsize,tosize : tcgsize; intreg,fpureg : tregister);
  1704. var
  1705. tmpref: treference;
  1706. begin
  1707. if not(tcgsize2size[fromsize] in [4,8]) or
  1708. not(tcgsize2size[tosize] in [4,8]) or
  1709. (tcgsize2size[fromsize]<>tcgsize2size[tosize]) then
  1710. internalerror(2017070902);
  1711. tg.gettemp(list,tcgsize2size[fromsize],tcgsize2size[fromsize],tt_normal,tmpref);
  1712. a_load_reg_ref(list,fromsize,fromsize,intreg,tmpref);
  1713. a_loadfpu_ref_reg(list,tosize,tosize,tmpref,fpureg);
  1714. tg.ungettemp(list,tmpref);
  1715. end;
  1716. procedure tcg.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1717. var
  1718. tmpreg : tregister;
  1719. begin
  1720. tmpreg:=getintregister(list,size);
  1721. a_load_ref_reg(list,size,size,ref,tmpreg);
  1722. a_op_const_reg(list,op,size,a,tmpreg);
  1723. a_load_reg_ref(list,size,size,tmpreg,ref);
  1724. end;
  1725. procedure tcg.a_op_const_loc(list : TAsmList; Op: TOpCG; a: tcgint; const loc: tlocation);
  1726. begin
  1727. case loc.loc of
  1728. LOC_REGISTER, LOC_CREGISTER:
  1729. a_op_const_reg(list,op,loc.size,a,loc.register);
  1730. LOC_REFERENCE, LOC_CREFERENCE:
  1731. a_op_const_ref(list,op,loc.size,a,loc.reference);
  1732. else
  1733. internalerror(200109061);
  1734. end;
  1735. end;
  1736. procedure tcg.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1737. var
  1738. tmpreg : tregister;
  1739. begin
  1740. tmpreg:=getintregister(list,size);
  1741. a_load_ref_reg(list,size,size,ref,tmpreg);
  1742. if op in [OP_NEG,OP_NOT] then
  1743. begin
  1744. if reg<>NR_NO then
  1745. internalerror(2017040901);
  1746. a_op_reg_reg(list,op,size,tmpreg,tmpreg);
  1747. end
  1748. else
  1749. a_op_reg_reg(list,op,size,reg,tmpreg);
  1750. a_load_reg_ref(list,size,size,tmpreg,ref);
  1751. end;
  1752. procedure tcg.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1753. var
  1754. tmpreg: tregister;
  1755. begin
  1756. case op of
  1757. OP_NOT,OP_NEG:
  1758. { handle it as "load ref,reg; op reg" }
  1759. begin
  1760. a_load_ref_reg(list,size,size,ref,reg);
  1761. a_op_reg_reg(list,op,size,reg,reg);
  1762. end;
  1763. else
  1764. begin
  1765. tmpreg:=getintregister(list,size);
  1766. a_load_ref_reg(list,size,size,ref,tmpreg);
  1767. a_op_reg_reg(list,op,size,tmpreg,reg);
  1768. end;
  1769. end;
  1770. end;
  1771. procedure tcg.a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  1772. begin
  1773. case loc.loc of
  1774. LOC_REGISTER, LOC_CREGISTER:
  1775. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  1776. LOC_REFERENCE, LOC_CREFERENCE:
  1777. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  1778. else
  1779. internalerror(200109061);
  1780. end;
  1781. end;
  1782. procedure tcg.a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  1783. var
  1784. tmpreg: tregister;
  1785. begin
  1786. case loc.loc of
  1787. LOC_REGISTER,LOC_CREGISTER:
  1788. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  1789. LOC_REFERENCE,LOC_CREFERENCE:
  1790. begin
  1791. tmpreg:=getintregister(list,loc.size);
  1792. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  1793. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  1794. end;
  1795. else
  1796. internalerror(200109061);
  1797. end;
  1798. end;
  1799. procedure Tcg.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1800. a:tcgint;src,dst:Tregister);
  1801. begin
  1802. optimize_op_const(size, op, a);
  1803. case op of
  1804. OP_NONE:
  1805. begin
  1806. if src <> dst then
  1807. a_load_reg_reg(list, size, size, src, dst);
  1808. exit;
  1809. end;
  1810. OP_MOVE:
  1811. begin
  1812. a_load_const_reg(list, size, a, dst);
  1813. exit;
  1814. end;
  1815. end;
  1816. a_load_reg_reg(list,size,size,src,dst);
  1817. a_op_const_reg(list,op,size,a,dst);
  1818. end;
  1819. procedure tcg.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1820. size: tcgsize; src1, src2, dst: tregister);
  1821. var
  1822. tmpreg: tregister;
  1823. begin
  1824. if (dst<>src1) then
  1825. begin
  1826. a_load_reg_reg(list,size,size,src2,dst);
  1827. a_op_reg_reg(list,op,size,src1,dst);
  1828. end
  1829. else
  1830. begin
  1831. { can we do a direct operation on the target register ? }
  1832. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  1833. a_op_reg_reg(list,op,size,src2,dst)
  1834. else
  1835. begin
  1836. tmpreg:=getintregister(list,size);
  1837. a_load_reg_reg(list,size,size,src2,tmpreg);
  1838. a_op_reg_reg(list,op,size,src1,tmpreg);
  1839. a_load_reg_reg(list,size,size,tmpreg,dst);
  1840. end;
  1841. end;
  1842. end;
  1843. procedure tcg.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1844. begin
  1845. a_op_const_reg_reg(list,op,size,a,src,dst);
  1846. ovloc.loc:=LOC_VOID;
  1847. end;
  1848. procedure tcg.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1849. begin
  1850. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1851. ovloc.loc:=LOC_VOID;
  1852. end;
  1853. procedure tcg.a_cmp_const_reg_label(list: TAsmList; size: tcgsize;
  1854. cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1855. var
  1856. tmpreg: tregister;
  1857. begin
  1858. tmpreg:=getintregister(list,size);
  1859. a_load_const_reg(list,size,a,tmpreg);
  1860. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1861. end;
  1862. procedure tcg.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  1863. l : tasmlabel);
  1864. var
  1865. tmpreg: tregister;
  1866. begin
  1867. tmpreg:=getintregister(list,size);
  1868. a_load_ref_reg(list,size,size,ref,tmpreg);
  1869. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1870. end;
  1871. procedure tcg.a_cmp_const_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const loc : tlocation;
  1872. l : tasmlabel);
  1873. begin
  1874. case loc.loc of
  1875. LOC_REGISTER,LOC_CREGISTER:
  1876. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1877. LOC_REFERENCE,LOC_CREFERENCE:
  1878. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1879. else
  1880. internalerror(200109061);
  1881. end;
  1882. end;
  1883. procedure tcg.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  1884. var
  1885. tmpreg: tregister;
  1886. begin
  1887. tmpreg:=getintregister(list,size);
  1888. a_load_ref_reg(list,size,size,ref,tmpreg);
  1889. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1890. end;
  1891. procedure tcg.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg : tregister; const ref: treference; l : tasmlabel);
  1892. var
  1893. tmpreg: tregister;
  1894. begin
  1895. tmpreg:=getintregister(list,size);
  1896. a_load_ref_reg(list,size,size,ref,tmpreg);
  1897. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  1898. end;
  1899. procedure tcg.a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  1900. begin
  1901. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  1902. end;
  1903. procedure tcg.a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  1904. begin
  1905. case loc.loc of
  1906. LOC_REGISTER,
  1907. LOC_CREGISTER:
  1908. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1909. LOC_REFERENCE,
  1910. LOC_CREFERENCE :
  1911. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1912. LOC_CONSTANT:
  1913. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1914. else
  1915. internalerror(200203231);
  1916. end;
  1917. end;
  1918. procedure tcg.a_cmp_ref_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1919. l : tasmlabel);
  1920. var
  1921. tmpreg: tregister;
  1922. begin
  1923. case loc.loc of
  1924. LOC_REGISTER,LOC_CREGISTER:
  1925. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1926. LOC_REFERENCE,LOC_CREFERENCE:
  1927. begin
  1928. tmpreg:=getintregister(list,size);
  1929. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1930. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1931. end;
  1932. else
  1933. internalerror(200109061);
  1934. end;
  1935. end;
  1936. procedure tcg.a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  1937. begin
  1938. case loc.loc of
  1939. LOC_MMREGISTER,LOC_CMMREGISTER:
  1940. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1941. LOC_REFERENCE,LOC_CREFERENCE:
  1942. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  1943. LOC_REGISTER,LOC_CREGISTER:
  1944. a_loadmm_intreg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1945. else
  1946. internalerror(200310121);
  1947. end;
  1948. end;
  1949. procedure tcg.a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  1950. begin
  1951. case loc.loc of
  1952. LOC_MMREGISTER,LOC_CMMREGISTER:
  1953. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  1954. LOC_REFERENCE,LOC_CREFERENCE:
  1955. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  1956. else
  1957. internalerror(200310122);
  1958. end;
  1959. end;
  1960. procedure tcg.a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle);
  1961. var
  1962. href : treference;
  1963. {$ifndef cpu64bitalu}
  1964. tmpreg : tregister;
  1965. reg64 : tregister64;
  1966. {$endif not cpu64bitalu}
  1967. begin
  1968. {$ifndef cpu64bitalu}
  1969. if not(cgpara.location^.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  1970. (size<>OS_F64) then
  1971. {$endif not cpu64bitalu}
  1972. cgpara.check_simple_location;
  1973. paramanager.alloccgpara(list,cgpara);
  1974. case cgpara.location^.loc of
  1975. LOC_MMREGISTER,LOC_CMMREGISTER:
  1976. a_loadmm_reg_reg(list,size,cgpara.location^.size,reg,cgpara.location^.register,shuffle);
  1977. LOC_REFERENCE,LOC_CREFERENCE:
  1978. begin
  1979. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment,[]);
  1980. a_loadmm_reg_ref(list,size,cgpara.location^.size,reg,href,shuffle);
  1981. end;
  1982. LOC_REGISTER,LOC_CREGISTER:
  1983. begin
  1984. if assigned(shuffle) and
  1985. not shufflescalar(shuffle) then
  1986. internalerror(2009112510);
  1987. {$ifndef cpu64bitalu}
  1988. if (size=OS_F64) then
  1989. begin
  1990. if not assigned(cgpara.location^.next) or
  1991. assigned(cgpara.location^.next^.next) then
  1992. internalerror(2009112512);
  1993. case cgpara.location^.next^.loc of
  1994. LOC_REGISTER,LOC_CREGISTER:
  1995. tmpreg:=cgpara.location^.next^.register;
  1996. LOC_REFERENCE,LOC_CREFERENCE:
  1997. tmpreg:=getintregister(list,OS_32);
  1998. else
  1999. internalerror(2009112910);
  2000. end;
  2001. if (target_info.endian=ENDIAN_BIG) then
  2002. begin
  2003. { paraloc^ -> high
  2004. paraloc^.next -> low }
  2005. reg64.reghi:=cgpara.location^.register;
  2006. reg64.reglo:=tmpreg;
  2007. end
  2008. else
  2009. begin
  2010. { paraloc^ -> low
  2011. paraloc^.next -> high }
  2012. reg64.reglo:=cgpara.location^.register;
  2013. reg64.reghi:=tmpreg;
  2014. end;
  2015. cg64.a_loadmm_reg_intreg64(list,size,reg,reg64);
  2016. if (cgpara.location^.next^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2017. begin
  2018. if not(cgpara.location^.next^.size in [OS_32,OS_S32]) then
  2019. internalerror(2009112911);
  2020. reference_reset_base(href,cgpara.location^.next^.reference.index,cgpara.location^.next^.reference.offset,cgpara.alignment,[]);
  2021. a_load_reg_ref(list,OS_32,cgpara.location^.next^.size,tmpreg,href);
  2022. end;
  2023. end
  2024. else
  2025. {$endif not cpu64bitalu}
  2026. a_loadmm_reg_intreg(list,size,cgpara.location^.size,reg,cgpara.location^.register,mms_movescalar);
  2027. end
  2028. else
  2029. internalerror(200310123);
  2030. end;
  2031. end;
  2032. procedure tcg.a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize;const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle);
  2033. var
  2034. hr : tregister;
  2035. hs : tmmshuffle;
  2036. begin
  2037. cgpara.check_simple_location;
  2038. hr:=getmmregister(list,cgpara.location^.size);
  2039. a_loadmm_ref_reg(list,size,cgpara.location^.size,ref,hr,shuffle);
  2040. if realshuffle(shuffle) then
  2041. begin
  2042. hs:=shuffle^;
  2043. removeshuffles(hs);
  2044. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,@hs);
  2045. end
  2046. else
  2047. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,shuffle);
  2048. end;
  2049. procedure tcg.a_loadmm_loc_cgpara(list: TAsmList;const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle);
  2050. begin
  2051. case loc.loc of
  2052. LOC_MMREGISTER,LOC_CMMREGISTER:
  2053. a_loadmm_reg_cgpara(list,loc.size,loc.register,cgpara,shuffle);
  2054. LOC_REFERENCE,LOC_CREFERENCE:
  2055. a_loadmm_ref_cgpara(list,loc.size,loc.reference,cgpara,shuffle);
  2056. else
  2057. internalerror(200310123);
  2058. end;
  2059. end;
  2060. procedure tcg.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  2061. var
  2062. hr : tregister;
  2063. hs : tmmshuffle;
  2064. begin
  2065. hr:=getmmregister(list,size);
  2066. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  2067. if realshuffle(shuffle) then
  2068. begin
  2069. hs:=shuffle^;
  2070. removeshuffles(hs);
  2071. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  2072. end
  2073. else
  2074. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  2075. end;
  2076. procedure tcg.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  2077. var
  2078. hr : tregister;
  2079. hs : tmmshuffle;
  2080. begin
  2081. hr:=getmmregister(list,size);
  2082. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  2083. if realshuffle(shuffle) then
  2084. begin
  2085. hs:=shuffle^;
  2086. removeshuffles(hs);
  2087. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  2088. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  2089. end
  2090. else
  2091. begin
  2092. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  2093. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  2094. end;
  2095. end;
  2096. procedure tcg.a_loadmm_intreg_reg(list: tasmlist; fromsize,tosize: tcgsize; intreg,mmreg: tregister; shuffle: pmmshuffle);
  2097. var
  2098. tmpref: treference;
  2099. begin
  2100. if (tcgsize2size[fromsize]<>4) or
  2101. (tcgsize2size[tosize]<>4) then
  2102. internalerror(2009112503);
  2103. tg.gettemp(list,4,4,tt_normal,tmpref);
  2104. a_load_reg_ref(list,fromsize,fromsize,intreg,tmpref);
  2105. a_loadmm_ref_reg(list,tosize,tosize,tmpref,mmreg,shuffle);
  2106. tg.ungettemp(list,tmpref);
  2107. end;
  2108. procedure tcg.a_loadmm_reg_intreg(list: tasmlist; fromsize,tosize: tcgsize; mmreg,intreg: tregister; shuffle: pmmshuffle);
  2109. var
  2110. tmpref: treference;
  2111. begin
  2112. if (tcgsize2size[fromsize]<>4) or
  2113. (tcgsize2size[tosize]<>4) then
  2114. internalerror(2009112504);
  2115. tg.gettemp(list,8,8,tt_normal,tmpref);
  2116. a_loadmm_reg_ref(list,fromsize,fromsize,mmreg,tmpref,shuffle);
  2117. a_load_ref_reg(list,tosize,tosize,tmpref,intreg);
  2118. tg.ungettemp(list,tmpref);
  2119. end;
  2120. procedure tcg.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  2121. begin
  2122. case loc.loc of
  2123. LOC_CMMREGISTER,LOC_MMREGISTER:
  2124. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  2125. LOC_CREFERENCE,LOC_REFERENCE:
  2126. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  2127. else
  2128. internalerror(200312232);
  2129. end;
  2130. end;
  2131. procedure tcg.a_opmm_loc_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; src,dst: tregister;shuffle : pmmshuffle);
  2132. begin
  2133. case loc.loc of
  2134. LOC_CMMREGISTER,LOC_MMREGISTER:
  2135. a_opmm_reg_reg_reg(list,op,size,loc.register,src,dst,shuffle);
  2136. LOC_CREFERENCE,LOC_REFERENCE:
  2137. a_opmm_ref_reg_reg(list,op,size,loc.reference,src,dst,shuffle);
  2138. else
  2139. internalerror(200312232);
  2140. end;
  2141. end;
  2142. procedure tcg.a_opmm_reg_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;
  2143. src1,src2,dst : tregister;shuffle : pmmshuffle);
  2144. begin
  2145. internalerror(2013061102);
  2146. end;
  2147. procedure tcg.a_opmm_ref_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;
  2148. const ref : treference;src,dst : tregister;shuffle : pmmshuffle);
  2149. begin
  2150. internalerror(2013061101);
  2151. end;
  2152. procedure tcg.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  2153. begin
  2154. g_concatcopy(list,source,dest,len);
  2155. end;
  2156. procedure tcg.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  2157. begin
  2158. g_overflowCheck(list,loc,def);
  2159. end;
  2160. {$ifdef cpuflags}
  2161. procedure tcg.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference);
  2162. var
  2163. tmpreg : tregister;
  2164. begin
  2165. tmpreg:=getintregister(list,size);
  2166. g_flags2reg(list,size,f,tmpreg);
  2167. a_load_reg_ref(list,size,size,tmpreg,ref);
  2168. end;
  2169. {$endif cpuflags}
  2170. {*****************************************************************************
  2171. Entry/Exit Code Functions
  2172. *****************************************************************************}
  2173. procedure tcg.g_save_registers(list:TAsmList);
  2174. var
  2175. href : treference;
  2176. size : longint;
  2177. r : integer;
  2178. begin
  2179. { calculate temp. size }
  2180. size:=0;
  2181. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  2182. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2183. inc(size,sizeof(aint));
  2184. if uses_registers(R_ADDRESSREGISTER) then
  2185. for r:=low(saved_address_registers) to high(saved_address_registers) do
  2186. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2187. inc(size,sizeof(aint));
  2188. { mm registers }
  2189. if uses_registers(R_MMREGISTER) then
  2190. begin
  2191. { Make sure we reserve enough space to do the alignment based on the offset
  2192. later on. We can't use the size for this, because the alignment of the start
  2193. of the temp is smaller than needed for an OS_VECTOR }
  2194. inc(size,tcgsize2size[OS_VECTOR]);
  2195. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  2196. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  2197. inc(size,tcgsize2size[OS_VECTOR]);
  2198. end;
  2199. if size>0 then
  2200. begin
  2201. tg.GetTemp(list,size,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  2202. include(current_procinfo.flags,pi_has_saved_regs);
  2203. { Copy registers to temp }
  2204. href:=current_procinfo.save_regs_ref;
  2205. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  2206. begin
  2207. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2208. begin
  2209. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE),href);
  2210. inc(href.offset,sizeof(aint));
  2211. end;
  2212. include(rg[R_INTREGISTER].preserved_by_proc,saved_standard_registers[r]);
  2213. end;
  2214. if uses_registers(R_ADDRESSREGISTER) then
  2215. for r:=low(saved_address_registers) to high(saved_address_registers) do
  2216. begin
  2217. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2218. begin
  2219. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE),href);
  2220. inc(href.offset,sizeof(aint));
  2221. end;
  2222. include(rg[R_ADDRESSREGISTER].preserved_by_proc,saved_address_registers[r]);
  2223. end;
  2224. if uses_registers(R_MMREGISTER) then
  2225. begin
  2226. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  2227. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  2228. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  2229. begin
  2230. { the array has to be declared even if no MM registers are saved
  2231. (such as with SSE on i386), and since 0-element arrays don't
  2232. exist, they contain a single RS_INVALID element in that case
  2233. }
  2234. if saved_mm_registers[r]<>RS_INVALID then
  2235. begin
  2236. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  2237. begin
  2238. a_loadmm_reg_ref(list,OS_VECTOR,OS_VECTOR,newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBMMWHOLE),href,nil);
  2239. inc(href.offset,tcgsize2size[OS_VECTOR]);
  2240. end;
  2241. include(rg[R_MMREGISTER].preserved_by_proc,saved_mm_registers[r]);
  2242. end;
  2243. end;
  2244. end;
  2245. end;
  2246. end;
  2247. procedure tcg.g_restore_registers(list:TAsmList);
  2248. var
  2249. href : treference;
  2250. r : integer;
  2251. hreg : tregister;
  2252. begin
  2253. if not(pi_has_saved_regs in current_procinfo.flags) then
  2254. exit;
  2255. { Copy registers from temp }
  2256. href:=current_procinfo.save_regs_ref;
  2257. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  2258. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2259. begin
  2260. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  2261. { Allocate register so the optimizer does not remove the load }
  2262. a_reg_alloc(list,hreg);
  2263. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2264. inc(href.offset,sizeof(aint));
  2265. end;
  2266. if uses_registers(R_ADDRESSREGISTER) then
  2267. for r:=low(saved_address_registers) to high(saved_address_registers) do
  2268. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2269. begin
  2270. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  2271. { Allocate register so the optimizer does not remove the load }
  2272. a_reg_alloc(list,hreg);
  2273. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2274. inc(href.offset,sizeof(aint));
  2275. end;
  2276. if uses_registers(R_MMREGISTER) then
  2277. begin
  2278. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  2279. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  2280. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  2281. begin
  2282. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  2283. begin
  2284. hreg:=newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBMMWHOLE);
  2285. { Allocate register so the optimizer does not remove the load }
  2286. a_reg_alloc(list,hreg);
  2287. a_loadmm_ref_reg(list,OS_VECTOR,OS_VECTOR,href,hreg,nil);
  2288. inc(href.offset,tcgsize2size[OS_VECTOR]);
  2289. end;
  2290. end;
  2291. end;
  2292. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  2293. end;
  2294. procedure tcg.g_profilecode(list : TAsmList);
  2295. begin
  2296. end;
  2297. procedure tcg.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  2298. var
  2299. hsym : tsym;
  2300. href : treference;
  2301. paraloc : Pcgparalocation;
  2302. begin
  2303. { calculate the parameter info for the procdef }
  2304. procdef.init_paraloc_info(callerside);
  2305. hsym:=tsym(procdef.parast.Find('self'));
  2306. if not(assigned(hsym) and
  2307. (hsym.typ=paravarsym)) then
  2308. internalerror(200305251);
  2309. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  2310. while paraloc<>nil do
  2311. with paraloc^ do
  2312. begin
  2313. case loc of
  2314. LOC_REGISTER:
  2315. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  2316. LOC_REFERENCE:
  2317. begin
  2318. { offset in the wrapper needs to be adjusted for the stored
  2319. return address }
  2320. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),sizeof(pint),[]);
  2321. a_op_const_ref(list,OP_SUB,size,ioffset,href);
  2322. end
  2323. else
  2324. internalerror(200309189);
  2325. end;
  2326. paraloc:=next;
  2327. end;
  2328. end;
  2329. procedure tcg.a_call_name_static(list : TAsmList;const s : string);
  2330. begin
  2331. a_call_name(list,s,false);
  2332. end;
  2333. function tcg.g_indirect_sym_load(list:TAsmList;const symname: string; const flags: tindsymflags): tregister;
  2334. var
  2335. l: tasmsymbol;
  2336. ref: treference;
  2337. nlsymname: string;
  2338. symtyp: TAsmsymtype;
  2339. begin
  2340. result := NR_NO;
  2341. case target_info.system of
  2342. system_powerpc_darwin,
  2343. system_i386_darwin,
  2344. system_i386_iphonesim,
  2345. system_powerpc64_darwin,
  2346. system_arm_darwin:
  2347. begin
  2348. nlsymname:='L'+symname+'$non_lazy_ptr';
  2349. l:=current_asmdata.getasmsymbol(nlsymname);
  2350. if not(assigned(l)) then
  2351. begin
  2352. if is_data in flags then
  2353. symtyp:=AT_DATA
  2354. else
  2355. symtyp:=AT_FUNCTION;
  2356. new_section(current_asmdata.asmlists[al_picdata],sec_data_nonlazy,'',sizeof(pint));
  2357. l:=current_asmdata.DefineAsmSymbol(nlsymname,AB_LOCAL,AT_DATA,voidpointertype);
  2358. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create(l,0));
  2359. if not(is_weak in flags) then
  2360. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.RefAsmSymbol(symname,symtyp).Name))
  2361. else
  2362. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.WeakRefAsmSymbol(symname,symtyp).Name));
  2363. {$ifdef cpu64bitaddr}
  2364. current_asmdata.asmlists[al_picdata].concat(tai_const.create_64bit(0));
  2365. {$else cpu64bitaddr}
  2366. current_asmdata.asmlists[al_picdata].concat(tai_const.create_32bit(0));
  2367. {$endif cpu64bitaddr}
  2368. end;
  2369. result := getaddressregister(list);
  2370. reference_reset_symbol(ref,l,0,sizeof(pint),[]);
  2371. { a_load_ref_reg will turn this into a pic-load if needed }
  2372. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,result);
  2373. end;
  2374. end;
  2375. end;
  2376. procedure tcg.g_maybe_got_init(list: TAsmList);
  2377. begin
  2378. end;
  2379. procedure tcg.g_call(list: TAsmList;const s: string);
  2380. begin
  2381. allocallcpuregisters(list);
  2382. a_call_name(list,s,false);
  2383. deallocallcpuregisters(list);
  2384. end;
  2385. procedure tcg.g_local_unwind(list: TAsmList; l: TAsmLabel);
  2386. begin
  2387. a_jmp_always(list,l);
  2388. end;
  2389. procedure tcg.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  2390. begin
  2391. internalerror(200807231);
  2392. end;
  2393. procedure tcg.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2394. begin
  2395. internalerror(200807232);
  2396. end;
  2397. procedure tcg.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2398. begin
  2399. internalerror(200807233);
  2400. end;
  2401. procedure tcg.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  2402. begin
  2403. internalerror(200807234);
  2404. end;
  2405. function tcg.getflagregister(list: TAsmList; size: Tcgsize): Tregister;
  2406. begin
  2407. Result:=TRegister(0);
  2408. internalerror(200807238);
  2409. end;
  2410. procedure tcg.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tcgsize; src, dst: TRegister);
  2411. begin
  2412. internalerror(2014070601);
  2413. end;
  2414. procedure tcg.g_stackpointer_alloc(list: TAsmList; size: longint);
  2415. begin
  2416. internalerror(2014070602);
  2417. end;
  2418. procedure tcg.a_mul_reg_reg_pair(list: TAsmList; size: TCgSize; src1,src2,dstlo,dsthi: TRegister);
  2419. begin
  2420. internalerror(2014060801);
  2421. end;
  2422. procedure tcg.g_div_const_reg_reg(list:tasmlist; size: TCgSize; a: tcgint; src,dst: tregister);
  2423. var
  2424. divreg: tregister;
  2425. magic: aInt;
  2426. u_magic: aWord;
  2427. u_shift: byte;
  2428. u_add: boolean;
  2429. begin
  2430. divreg:=getintregister(list,OS_INT);
  2431. if (size in [OS_S32,OS_S64]) then
  2432. begin
  2433. calc_divconst_magic_signed(tcgsize2size[size]*8,a,magic,u_shift);
  2434. { load magic value }
  2435. a_load_const_reg(list,OS_INT,magic,divreg);
  2436. { multiply, discarding low bits }
  2437. a_mul_reg_reg_pair(list,size,src,divreg,NR_NO,dst);
  2438. { add/subtract numerator }
  2439. if (a>0) and (magic<0) then
  2440. a_op_reg_reg_reg(list,OP_ADD,OS_INT,src,dst,dst)
  2441. else if (a<0) and (magic>0) then
  2442. a_op_reg_reg_reg(list,OP_SUB,OS_INT,src,dst,dst);
  2443. { shift shift places to the right (arithmetic) }
  2444. a_op_const_reg_reg(list,OP_SAR,OS_INT,u_shift,dst,dst);
  2445. { extract and add sign bit }
  2446. if (a>=0) then
  2447. a_op_const_reg_reg(list,OP_SHR,OS_INT,tcgsize2size[size]*8-1,src,divreg)
  2448. else
  2449. a_op_const_reg_reg(list,OP_SHR,OS_INT,tcgsize2size[size]*8-1,dst,divreg);
  2450. a_op_reg_reg_reg(list,OP_ADD,OS_INT,dst,divreg,dst);
  2451. end
  2452. else if (size in [OS_32,OS_64]) then
  2453. begin
  2454. calc_divconst_magic_unsigned(tcgsize2size[size]*8,a,u_magic,u_add,u_shift);
  2455. { load magic in divreg }
  2456. a_load_const_reg(list,OS_INT,tcgint(u_magic),divreg);
  2457. { multiply, discarding low bits }
  2458. a_mul_reg_reg_pair(list,size,src,divreg,NR_NO,dst);
  2459. if (u_add) then
  2460. begin
  2461. { Calculate "(numerator+result) shr u_shift", avoiding possible overflow }
  2462. a_op_reg_reg_reg(list,OP_SUB,OS_INT,dst,src,divreg);
  2463. { divreg=(numerator-result) }
  2464. a_op_const_reg_reg(list,OP_SHR,OS_INT,1,divreg,divreg);
  2465. { divreg=(numerator-result)/2 }
  2466. a_op_reg_reg_reg(list,OP_ADD,OS_INT,divreg,dst,divreg);
  2467. { divreg=(numerator+result)/2, already shifted by 1, so decrease u_shift. }
  2468. a_op_const_reg_reg(list,OP_SHR,OS_INT,u_shift-1,divreg,dst);
  2469. end
  2470. else
  2471. a_op_const_reg_reg(list,OP_SHR,OS_INT,u_shift,dst,dst);
  2472. end
  2473. else
  2474. InternalError(2014060601);
  2475. end;
  2476. {*****************************************************************************
  2477. TCG64
  2478. *****************************************************************************}
  2479. {$ifndef cpu64bitalu}
  2480. function joinreg64(reglo,reghi : tregister) : tregister64;
  2481. begin
  2482. result.reglo:=reglo;
  2483. result.reghi:=reghi;
  2484. end;
  2485. procedure tcg64.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64; regsrc,regdst : tregister64);
  2486. begin
  2487. a_load64_reg_reg(list,regsrc,regdst);
  2488. a_op64_const_reg(list,op,size,value,regdst);
  2489. end;
  2490. procedure tcg64.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  2491. var
  2492. tmpreg64 : tregister64;
  2493. begin
  2494. { when src1=dst then we need to first create a temp to prevent
  2495. overwriting src1 with src2 }
  2496. if (regsrc1.reghi=regdst.reghi) or
  2497. (regsrc1.reglo=regdst.reghi) or
  2498. (regsrc1.reghi=regdst.reglo) or
  2499. (regsrc1.reglo=regdst.reglo) then
  2500. begin
  2501. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2502. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2503. a_load64_reg_reg(list,regsrc2,tmpreg64);
  2504. a_op64_reg_reg(list,op,size,regsrc1,tmpreg64);
  2505. a_load64_reg_reg(list,tmpreg64,regdst);
  2506. end
  2507. else
  2508. begin
  2509. a_load64_reg_reg(list,regsrc2,regdst);
  2510. a_op64_reg_reg(list,op,size,regsrc1,regdst);
  2511. end;
  2512. end;
  2513. procedure tcg64.a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  2514. var
  2515. tmpreg64 : tregister64;
  2516. begin
  2517. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2518. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2519. a_load64_subsetref_reg(list,sref,tmpreg64);
  2520. a_op64_const_reg(list,op,size,a,tmpreg64);
  2521. a_load64_reg_subsetref(list,tmpreg64,sref);
  2522. end;
  2523. procedure tcg64.a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  2524. var
  2525. tmpreg64 : tregister64;
  2526. begin
  2527. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2528. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2529. a_load64_subsetref_reg(list,sref,tmpreg64);
  2530. a_op64_reg_reg(list,op,size,reg,tmpreg64);
  2531. a_load64_reg_subsetref(list,tmpreg64,sref);
  2532. end;
  2533. procedure tcg64.a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  2534. var
  2535. tmpreg64 : tregister64;
  2536. begin
  2537. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2538. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2539. a_load64_subsetref_reg(list,sref,tmpreg64);
  2540. a_op64_ref_reg(list,op,size,ref,tmpreg64);
  2541. a_load64_reg_subsetref(list,tmpreg64,sref);
  2542. end;
  2543. procedure tcg64.a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  2544. var
  2545. tmpreg64 : tregister64;
  2546. begin
  2547. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2548. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2549. a_load64_subsetref_reg(list,ssref,tmpreg64);
  2550. a_op64_reg_subsetref(list,op,size,tmpreg64,dsref);
  2551. end;
  2552. procedure tcg64.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2553. begin
  2554. a_op64_const_reg_reg(list,op,size,value,regsrc,regdst);
  2555. ovloc.loc:=LOC_VOID;
  2556. end;
  2557. procedure tcg64.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2558. begin
  2559. a_op64_reg_reg_reg(list,op,size,regsrc1,regsrc2,regdst);
  2560. ovloc.loc:=LOC_VOID;
  2561. end;
  2562. procedure tcg64.a_load64_loc_subsetref(list : TAsmList;const l: tlocation; const sref : tsubsetreference);
  2563. begin
  2564. case l.loc of
  2565. LOC_REFERENCE, LOC_CREFERENCE:
  2566. a_load64_ref_subsetref(list,l.reference,sref);
  2567. LOC_REGISTER,LOC_CREGISTER:
  2568. a_load64_reg_subsetref(list,l.register64,sref);
  2569. LOC_CONSTANT :
  2570. a_load64_const_subsetref(list,l.value64,sref);
  2571. LOC_SUBSETREF,LOC_CSUBSETREF:
  2572. a_load64_subsetref_subsetref(list,l.sref,sref);
  2573. else
  2574. internalerror(2006082210);
  2575. end;
  2576. end;
  2577. procedure tcg64.a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  2578. begin
  2579. case l.loc of
  2580. LOC_REFERENCE, LOC_CREFERENCE:
  2581. a_load64_subsetref_ref(list,sref,l.reference);
  2582. LOC_REGISTER,LOC_CREGISTER:
  2583. a_load64_subsetref_reg(list,sref,l.register64);
  2584. LOC_SUBSETREF,LOC_CSUBSETREF:
  2585. a_load64_subsetref_subsetref(list,sref,l.sref);
  2586. else
  2587. internalerror(2006082211);
  2588. end;
  2589. end;
  2590. {$else cpu64bitalu}
  2591. function joinreg128(reglo, reghi: tregister): tregister128;
  2592. begin
  2593. result.reglo:=reglo;
  2594. result.reghi:=reghi;
  2595. end;
  2596. procedure splitparaloc128(const cgpara:tcgpara;var cgparalo,cgparahi:tcgpara);
  2597. var
  2598. paraloclo,
  2599. paralochi : pcgparalocation;
  2600. begin
  2601. if not(cgpara.size in [OS_128,OS_S128]) then
  2602. internalerror(2012090604);
  2603. if not assigned(cgpara.location) then
  2604. internalerror(2012090605);
  2605. { init lo/hi para }
  2606. cgparahi.reset;
  2607. if cgpara.size=OS_S128 then
  2608. cgparahi.size:=OS_S64
  2609. else
  2610. cgparahi.size:=OS_64;
  2611. cgparahi.intsize:=8;
  2612. cgparahi.alignment:=cgpara.alignment;
  2613. paralochi:=cgparahi.add_location;
  2614. cgparalo.reset;
  2615. cgparalo.size:=OS_64;
  2616. cgparalo.intsize:=8;
  2617. cgparalo.alignment:=cgpara.alignment;
  2618. paraloclo:=cgparalo.add_location;
  2619. { 2 parameter fields? }
  2620. if assigned(cgpara.location^.next) then
  2621. begin
  2622. { Order for multiple locations is always
  2623. paraloc^ -> high
  2624. paraloc^.next -> low }
  2625. if (target_info.endian=ENDIAN_BIG) then
  2626. begin
  2627. { paraloc^ -> high
  2628. paraloc^.next -> low }
  2629. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2630. move(cgpara.location^.next^,paraloclo^,sizeof(paraloclo^));
  2631. end
  2632. else
  2633. begin
  2634. { paraloc^ -> low
  2635. paraloc^.next -> high }
  2636. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2637. move(cgpara.location^.next^,paralochi^,sizeof(paralochi^));
  2638. end;
  2639. end
  2640. else
  2641. begin
  2642. { single parameter, this can only be in memory }
  2643. if cgpara.location^.loc<>LOC_REFERENCE then
  2644. internalerror(2012090606);
  2645. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2646. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2647. { for big endian low is at +8, for little endian high }
  2648. if target_info.endian = endian_big then
  2649. begin
  2650. inc(cgparalo.location^.reference.offset,8);
  2651. cgparalo.alignment:=newalignment(cgparalo.alignment,8);
  2652. end
  2653. else
  2654. begin
  2655. inc(cgparahi.location^.reference.offset,8);
  2656. cgparahi.alignment:=newalignment(cgparahi.alignment,8);
  2657. end;
  2658. end;
  2659. { fix size }
  2660. paraloclo^.size:=cgparalo.size;
  2661. paraloclo^.next:=nil;
  2662. paralochi^.size:=cgparahi.size;
  2663. paralochi^.next:=nil;
  2664. end;
  2665. procedure tcg128.a_load128_reg_reg(list: TAsmList; regsrc,
  2666. regdst: tregister128);
  2667. begin
  2668. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reglo,regdst.reglo);
  2669. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reghi,regdst.reghi);
  2670. end;
  2671. procedure tcg128.a_load128_reg_ref(list: TAsmList; reg: tregister128;
  2672. const ref: treference);
  2673. var
  2674. tmpreg: tregister;
  2675. tmpref: treference;
  2676. begin
  2677. if target_info.endian = endian_big then
  2678. begin
  2679. tmpreg:=reg.reglo;
  2680. reg.reglo:=reg.reghi;
  2681. reg.reghi:=tmpreg;
  2682. end;
  2683. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reglo,ref);
  2684. tmpref := ref;
  2685. inc(tmpref.offset,8);
  2686. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reghi,tmpref);
  2687. end;
  2688. procedure tcg128.a_load128_ref_reg(list: TAsmList; const ref: treference;
  2689. reg: tregister128);
  2690. var
  2691. tmpreg: tregister;
  2692. tmpref: treference;
  2693. begin
  2694. if target_info.endian = endian_big then
  2695. begin
  2696. tmpreg := reg.reglo;
  2697. reg.reglo := reg.reghi;
  2698. reg.reghi := tmpreg;
  2699. end;
  2700. tmpref := ref;
  2701. if (tmpref.base=reg.reglo) then
  2702. begin
  2703. tmpreg:=cg.getaddressregister(list);
  2704. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.base,tmpreg);
  2705. tmpref.base:=tmpreg;
  2706. end
  2707. else
  2708. { this works only for the i386, thus the i386 needs to override }
  2709. { this method and this method must be replaced by a more generic }
  2710. { implementation FK }
  2711. if (tmpref.index=reg.reglo) then
  2712. begin
  2713. tmpreg:=cg.getaddressregister(list);
  2714. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.index,tmpreg);
  2715. tmpref.index:=tmpreg;
  2716. end;
  2717. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reglo);
  2718. inc(tmpref.offset,8);
  2719. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reghi);
  2720. end;
  2721. procedure tcg128.a_load128_loc_ref(list: TAsmList; const l: tlocation;
  2722. const ref: treference);
  2723. begin
  2724. case l.loc of
  2725. LOC_REGISTER,LOC_CREGISTER:
  2726. a_load128_reg_ref(list,l.register128,ref);
  2727. { not yet implemented:
  2728. LOC_CONSTANT :
  2729. a_load128_const_ref(list,l.value128,ref);
  2730. LOC_SUBSETREF, LOC_CSUBSETREF:
  2731. a_load64_subsetref_ref(list,l.sref,ref); }
  2732. else
  2733. internalerror(201209061);
  2734. end;
  2735. end;
  2736. procedure tcg128.a_load128_reg_loc(list: TAsmList; reg: tregister128;
  2737. const l: tlocation);
  2738. begin
  2739. case l.loc of
  2740. LOC_REFERENCE, LOC_CREFERENCE:
  2741. a_load128_reg_ref(list,reg,l.reference);
  2742. LOC_REGISTER,LOC_CREGISTER:
  2743. a_load128_reg_reg(list,reg,l.register128);
  2744. { not yet implemented:
  2745. LOC_SUBSETREF, LOC_CSUBSETREF:
  2746. a_load64_reg_subsetref(list,reg,l.sref);
  2747. LOC_MMREGISTER, LOC_CMMREGISTER:
  2748. a_loadmm_intreg64_reg(list,l.size,reg,l.register); }
  2749. else
  2750. internalerror(201209062);
  2751. end;
  2752. end;
  2753. procedure tcg128.a_load128_const_reg(list: TAsmList; valuelo,
  2754. valuehi: int64; reg: tregister128);
  2755. begin
  2756. cg.a_load_const_reg(list,OS_64,aint(valuelo),reg.reglo);
  2757. cg.a_load_const_reg(list,OS_64,aint(valuehi),reg.reghi);
  2758. end;
  2759. procedure tcg128.a_load128_loc_cgpara(list: TAsmList; const l: tlocation;
  2760. const paraloc: TCGPara);
  2761. begin
  2762. case l.loc of
  2763. LOC_REGISTER,
  2764. LOC_CREGISTER :
  2765. a_load128_reg_cgpara(list,l.register128,paraloc);
  2766. {not yet implemented:
  2767. LOC_CONSTANT :
  2768. a_load128_const_cgpara(list,l.value64,paraloc);
  2769. }
  2770. LOC_CREFERENCE,
  2771. LOC_REFERENCE :
  2772. a_load128_ref_cgpara(list,l.reference,paraloc);
  2773. else
  2774. internalerror(2012090603);
  2775. end;
  2776. end;
  2777. procedure tcg128.a_load128_reg_cgpara(list : TAsmList;reg : tregister128;const paraloc : tcgpara);
  2778. var
  2779. tmplochi,tmploclo: tcgpara;
  2780. begin
  2781. tmploclo.init;
  2782. tmplochi.init;
  2783. splitparaloc128(paraloc,tmploclo,tmplochi);
  2784. cg.a_load_reg_cgpara(list,OS_64,reg.reghi,tmplochi);
  2785. cg.a_load_reg_cgpara(list,OS_64,reg.reglo,tmploclo);
  2786. tmploclo.done;
  2787. tmplochi.done;
  2788. end;
  2789. procedure tcg128.a_load128_ref_cgpara(list : TAsmList;const r : treference;const paraloc : tcgpara);
  2790. var
  2791. tmprefhi,tmpreflo : treference;
  2792. tmploclo,tmplochi : tcgpara;
  2793. begin
  2794. tmploclo.init;
  2795. tmplochi.init;
  2796. splitparaloc128(paraloc,tmploclo,tmplochi);
  2797. tmprefhi:=r;
  2798. tmpreflo:=r;
  2799. if target_info.endian=endian_big then
  2800. inc(tmpreflo.offset,8)
  2801. else
  2802. inc(tmprefhi.offset,8);
  2803. cg.a_load_ref_cgpara(list,OS_64,tmprefhi,tmplochi);
  2804. cg.a_load_ref_cgpara(list,OS_64,tmpreflo,tmploclo);
  2805. tmploclo.done;
  2806. tmplochi.done;
  2807. end;
  2808. {$endif cpu64bitalu}
  2809. function asmsym2indsymflags(sym: TAsmSymbol): tindsymflags;
  2810. begin
  2811. result:=[];
  2812. if sym.typ<>AT_FUNCTION then
  2813. include(result,is_data);
  2814. if sym.bind=AB_WEAK_EXTERNAL then
  2815. include(result,is_weak);
  2816. end;
  2817. procedure destroy_codegen;
  2818. begin
  2819. cg.free;
  2820. cg:=nil;
  2821. {$ifdef cpu64bitalu}
  2822. cg128.free;
  2823. cg128:=nil;
  2824. {$else cpu64bitalu}
  2825. cg64.free;
  2826. cg64:=nil;
  2827. {$endif cpu64bitalu}
  2828. end;
  2829. end.