cgobj.pas 130 KB

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