cgobj.pas 142 KB

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