cgobj.pas 123 KB

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