cgobj.pas 129 KB

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