cgobj.pas 143 KB

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