cgobj.pas 144 KB

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