cgobj.pas 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139
  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. cclasses,globtype,
  31. cpubase,cgbase,cgutils,parabase,
  32. aasmbase,aasmtai,aasmdata,aasmcpu,
  33. symconst,symbase,symtype,symdef,symtable,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 overriden for all new processors which will be supported
  41. by Free Pascal. For 32-bit processors, the base class
  42. sould be @link(tcg64f32) and not @var(tcg).
  43. }
  44. tcg = class
  45. public
  46. alignment : talignment;
  47. rg : array[tregistertype] of trgobj;
  48. t_times : longint;
  49. {$ifdef flowgraph}
  50. aktflownode:word;
  51. {$endif}
  52. {************************************************}
  53. { basic routines }
  54. constructor create;
  55. {# Initialize the register allocators needed for the codegenerator.}
  56. procedure init_register_allocators;virtual;
  57. {# Clean up the register allocators needed for the codegenerator.}
  58. procedure done_register_allocators;virtual;
  59. {# 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. }
  60. procedure set_regalloc_extend_backwards(b: boolean);
  61. {$ifdef flowgraph}
  62. procedure init_flowgraph;
  63. procedure done_flowgraph;
  64. {$endif}
  65. {# Gets a register suitable to do integer operations on.}
  66. function getintregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  67. {# Gets a register suitable to do integer operations on.}
  68. function getaddressregister(list:TAsmList):Tregister;virtual;
  69. function getfpuregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  70. function getmmregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  71. function getflagregister(list:TAsmList;size:Tcgsize):Tregister;virtual;abstract;
  72. {Does the generic cg need SIMD registers, like getmmxregister? Or should
  73. the cpu specific child cg object have such a method?}
  74. procedure add_reg_instruction(instr:Tai;r:tregister);virtual;
  75. procedure add_move_instruction(instr:Taicpu);virtual;
  76. function uses_registers(rt:Tregistertype):boolean;virtual;
  77. {# Get a specific register.}
  78. procedure getcpuregister(list:TAsmList;r:Tregister);virtual;
  79. procedure ungetcpuregister(list:TAsmList;r:Tregister);virtual;
  80. {# Get multiple registers specified.}
  81. procedure alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  82. {# Free multiple registers specified.}
  83. procedure dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  84. procedure allocallcpuregisters(list:TAsmList);virtual;
  85. procedure deallocallcpuregisters(list:TAsmList);virtual;
  86. procedure do_register_allocation(list:TAsmList;headertai:tai);virtual;
  87. procedure translate_register(var reg : tregister);
  88. function makeregsize(list:TAsmList;reg:Tregister;size:Tcgsize):Tregister;
  89. {# Emit a label to the instruction stream. }
  90. procedure a_label(list : TAsmList;l : tasmlabel);virtual;
  91. {# Allocates register r by inserting a pai_realloc record }
  92. procedure a_reg_alloc(list : TAsmList;r : tregister);
  93. {# Deallocates register r by inserting a pa_regdealloc record}
  94. procedure a_reg_dealloc(list : TAsmList;r : tregister);
  95. { Synchronize register, make sure it is still valid }
  96. procedure a_reg_sync(list : TAsmList;r : tregister);
  97. {# Pass a parameter, which is located in a register, to a routine.
  98. This routine should push/send the parameter to the routine, as
  99. required by the specific processor ABI and routine modifiers.
  100. This must be overriden for each CPU target.
  101. @param(size size of the operand in the register)
  102. @param(r register source of the operand)
  103. @param(cgpara where the parameter will be stored)
  104. }
  105. procedure a_param_reg(list : TAsmList;size : tcgsize;r : tregister;const cgpara : TCGPara);virtual;
  106. {# Pass a parameter, which is a constant, to a routine.
  107. A generic version is provided. This routine should
  108. be overriden for optimization purposes if the cpu
  109. permits directly sending this type of parameter.
  110. @param(size size of the operand in constant)
  111. @param(a value of constant to send)
  112. @param(cgpara where the parameter will be stored)
  113. }
  114. procedure a_param_const(list : TAsmList;size : tcgsize;a : aint;const cgpara : TCGPara);virtual;
  115. {# Pass the value of a parameter, which is located in memory, to a routine.
  116. A generic version is provided. This routine should
  117. be overriden for optimization purposes if the cpu
  118. permits directly sending this type of parameter.
  119. @param(size size of the operand in constant)
  120. @param(r Memory reference of value to send)
  121. @param(cgpara where the parameter will be stored)
  122. }
  123. procedure a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const cgpara : TCGPara);virtual;
  124. {# Pass the value of a parameter, which can be located either in a register or memory location,
  125. to a routine.
  126. A generic version is provided.
  127. @param(l location of the operand to send)
  128. @param(nr parameter number (starting from one) of routine (from left to right))
  129. @param(cgpara where the parameter will be stored)
  130. }
  131. procedure a_param_loc(list : TAsmList;const l : tlocation;const cgpara : TCGPara);
  132. {# Pass the address of a reference to a routine. This routine
  133. will calculate the address of the reference, and pass this
  134. calculated address as a parameter.
  135. A generic version is provided. This routine should
  136. be overriden for optimization purposes if the cpu
  137. permits directly sending this type of parameter.
  138. @param(r reference to get address from)
  139. @param(nr parameter number (starting from one) of routine (from left to right))
  140. }
  141. procedure a_paramaddr_ref(list : TAsmList;const r : treference;const cgpara : TCGPara);virtual;
  142. { Remarks:
  143. * If a method specifies a size you have only to take care
  144. of that number of bits, i.e. load_const_reg with OP_8 must
  145. only load the lower 8 bit of the specified register
  146. the rest of the register can be undefined
  147. if necessary the compiler will call a method
  148. to zero or sign extend the register
  149. * The a_load_XX_XX with OP_64 needn't to be
  150. implemented for 32 bit
  151. processors, the code generator takes care of that
  152. * the addr size is for work with the natural pointer
  153. size
  154. * the procedures without fpu/mm are only for integer usage
  155. * normally the first location is the source and the
  156. second the destination
  157. }
  158. {# Emits instruction to call the method specified by symbol name.
  159. This routine must be overriden for each new target cpu.
  160. There is no a_call_ref because loading the reference will use
  161. a temp register on most cpu's resulting in conflicts with the
  162. registers used for the parameters (PFV)
  163. }
  164. procedure a_call_name(list : TAsmList;const s : string);virtual; abstract;
  165. procedure a_call_reg(list : TAsmList;reg : tregister);virtual; abstract;
  166. procedure a_call_ref(list : TAsmList;ref : treference);virtual; abstract;
  167. { same as a_call_name, might be overriden on certain architectures to emit
  168. static calls without usage of a got trampoline }
  169. procedure a_call_name_static(list : TAsmList;const s : string);virtual;
  170. { move instructions }
  171. procedure a_load_const_reg(list : TAsmList;size : tcgsize;a : aint;register : tregister);virtual; abstract;
  172. procedure a_load_const_ref(list : TAsmList;size : tcgsize;a : aint;const ref : treference);virtual;
  173. procedure a_load_const_loc(list : TAsmList;a : aint;const loc : tlocation);
  174. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  175. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  176. procedure a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  177. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  178. procedure a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);virtual;
  179. procedure a_load_loc_reg(list : TAsmList;tosize: tcgsize; const loc: tlocation; reg : tregister);
  180. procedure a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  181. procedure a_load_loc_subsetreg(list : TAsmList;subsetsize: tcgsize; const loc: tlocation; const sreg : tsubsetregister);
  182. procedure a_load_loc_subsetref(list : TAsmList;subsetsize: tcgsize; const loc: tlocation; const sref : tsubsetreference);
  183. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);virtual; abstract;
  184. procedure a_load_subsetreg_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; destreg: tregister); virtual;
  185. procedure a_load_reg_subsetreg(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister); virtual;
  186. procedure a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsreg, tosreg: tsubsetregister); virtual;
  187. procedure a_load_subsetreg_ref(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; const destref: treference); virtual;
  188. procedure a_load_ref_subsetreg(list : TAsmList; fromsize, subsetsize: tcgsize; const fromref: treference; const sreg: tsubsetregister); virtual;
  189. procedure a_load_const_subsetreg(list: TAsmlist; subsetsize: tcgsize; a: aint; const sreg: tsubsetregister); virtual;
  190. procedure a_load_subsetreg_loc(list: TAsmlist; subsetsize: tcgsize; const sreg: tsubsetregister; const loc: tlocation); virtual;
  191. procedure a_load_subsetref_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sref: tsubsetreference; destreg: tregister); virtual;
  192. procedure a_load_reg_subsetref(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sref: tsubsetreference); virtual;
  193. procedure a_load_subsetref_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsref, tosref: tsubsetreference); virtual;
  194. procedure a_load_subsetref_ref(list : TAsmList; subsetsize, tosize: tcgsize; const sref: tsubsetreference; const destref: treference); virtual;
  195. procedure a_load_ref_subsetref(list : TAsmList; fromsize, subsetsize: tcgsize; const fromref: treference; const sref: tsubsetreference); virtual;
  196. procedure a_load_const_subsetref(list: TAsmlist; subsetsize: tcgsize; a: aint; const sref: tsubsetreference); virtual;
  197. procedure a_load_subsetref_loc(list: TAsmlist; subsetsize: tcgsize; const sref: tsubsetreference; const loc: tlocation); virtual;
  198. procedure a_load_subsetref_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsref: tsubsetreference; const tosreg: tsubsetregister); virtual;
  199. procedure a_load_subsetreg_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsreg: tsubsetregister; const tosref: tsubsetreference); virtual;
  200. { fpu move instructions }
  201. procedure a_loadfpu_reg_reg(list: TAsmList; size:tcgsize; reg1, reg2: tregister); virtual; abstract;
  202. procedure a_loadfpu_ref_reg(list: TAsmList; size: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  203. procedure a_loadfpu_reg_ref(list: TAsmList; size: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  204. procedure a_loadfpu_loc_reg(list: TAsmList; const loc: tlocation; const reg: tregister);
  205. procedure a_loadfpu_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation);
  206. procedure a_paramfpu_reg(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);virtual;
  207. procedure a_paramfpu_ref(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);virtual;
  208. { vector register move instructions }
  209. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); virtual; abstract;
  210. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual; abstract;
  211. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual; abstract;
  212. procedure a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  213. procedure a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  214. procedure a_parammm_reg(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  215. procedure a_parammm_ref(list: TAsmList; size: tcgsize; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  216. procedure a_parammm_loc(list: TAsmList; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  217. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle); virtual;abstract;
  218. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  219. procedure a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  220. procedure a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  221. { basic arithmetic operations }
  222. { note: for operators which require only one argument (not, neg), use }
  223. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  224. { that in this case the *second* operand is used as both source and }
  225. { destination (JM) }
  226. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: Aint; reg: TRegister); virtual; abstract;
  227. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: Aint; const ref: TReference); virtual;
  228. procedure a_op_const_subsetreg(list : TAsmList; Op : TOpCG; size, subsetsize : TCGSize; a : aint; const sreg: tsubsetregister); virtual;
  229. procedure a_op_const_subsetref(list : TAsmList; Op : TOpCG; size, subsetsize : TCGSize; a : aint; const sref: tsubsetreference); virtual;
  230. procedure a_op_const_loc(list : TAsmList; Op: TOpCG; a: Aint; const loc: tlocation);
  231. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  232. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  233. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  234. procedure a_op_reg_subsetreg(list : TAsmList; Op : TOpCG; opsize, subsetsize : TCGSize; reg: TRegister; const sreg: tsubsetregister); virtual;
  235. procedure a_op_reg_subsetref(list : TAsmList; Op : TOpCG; opsize, subsetsize : TCGSize; reg: TRegister; const sref: tsubsetreference); virtual;
  236. procedure a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  237. procedure a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  238. { trinary operations for processors that support them, 'emulated' }
  239. { on others. None with "ref" arguments since I don't think there }
  240. { are any processors that support it (JM) }
  241. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister); virtual;
  242. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister); virtual;
  243. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  244. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  245. { comparison operations }
  246. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  247. l : tasmlabel);virtual; abstract;
  248. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  249. l : tasmlabel); virtual;
  250. procedure a_cmp_const_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; a: aint; const loc: tlocation;
  251. l : tasmlabel);
  252. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  253. procedure a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  254. procedure a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  255. procedure a_cmp_subsetreg_reg_label(list : TAsmList; subsetsize, cmpsize : tcgsize; cmp_op : topcmp; const sreg: tsubsetregister; reg : tregister; l : tasmlabel); virtual;
  256. procedure a_cmp_subsetref_reg_label(list : TAsmList; subsetsize, cmpsize : tcgsize; cmp_op : topcmp; const sref: tsubsetreference; reg : tregister; l : tasmlabel); virtual;
  257. procedure a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  258. procedure a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  259. procedure a_cmp_ref_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  260. l : tasmlabel);
  261. procedure a_jmp_name(list : TAsmList;const s : string); virtual; abstract;
  262. procedure a_jmp_always(list : TAsmList;l: tasmlabel); virtual; abstract;
  263. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); virtual; abstract;
  264. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  265. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  266. }
  267. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  268. procedure g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  269. {
  270. This routine tries to optimize the op_const_reg/ref opcode, and should be
  271. called at the start of a_op_const_reg/ref. It returns the actual opcode
  272. to emit, and the constant value to emit. This function can opcode OP_NONE to
  273. remove the opcode and OP_MOVE to replace it with a simple load
  274. @param(op The opcode to emit, returns the opcode which must be emitted)
  275. @param(a The constant which should be emitted, returns the constant which must
  276. be emitted)
  277. }
  278. procedure optimize_op_const(var op: topcg; var a : aint);virtual;
  279. {#
  280. This routine is used in exception management nodes. It should
  281. save the exception reason currently in the FUNCTION_RETURN_REG. The
  282. save should be done either to a temp (pointed to by href).
  283. or on the stack (pushing the value on the stack).
  284. The size of the value to save is OS_S32. The default version
  285. saves the exception reason to a temp. memory area.
  286. }
  287. procedure g_exception_reason_save(list : TAsmList; const href : treference);virtual;
  288. {#
  289. This routine is used in exception management nodes. It should
  290. save the exception reason constant. The
  291. save should be done either to a temp (pointed to by href).
  292. or on the stack (pushing the value on the stack).
  293. The size of the value to save is OS_S32. The default version
  294. saves the exception reason to a temp. memory area.
  295. }
  296. procedure g_exception_reason_save_const(list : TAsmList; const href : treference; a: aint);virtual;
  297. {#
  298. This routine is used in exception management nodes. It should
  299. load the exception reason to the FUNCTION_RETURN_REG. The saved value
  300. should either be in the temp. area (pointed to by href , href should
  301. *NOT* be freed) or on the stack (the value should be popped).
  302. The size of the value to save is OS_S32. The default version
  303. saves the exception reason to a temp. memory area.
  304. }
  305. procedure g_exception_reason_load(list : TAsmList; const href : treference);virtual;
  306. procedure g_maybe_testself(list : TAsmList;reg:tregister);
  307. procedure g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  308. {# This should emit the opcode to copy len bytes from the source
  309. to destination.
  310. It must be overriden for each new target processor.
  311. @param(source Source reference of copy)
  312. @param(dest Destination reference of copy)
  313. }
  314. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);virtual; abstract;
  315. {# This should emit the opcode to copy len bytes from the an unaligned source
  316. to destination.
  317. It must be overriden for each new target processor.
  318. @param(source Source reference of copy)
  319. @param(dest Destination reference of copy)
  320. }
  321. procedure g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : aint);virtual;
  322. {# This should emit the opcode to a shortrstring from the source
  323. to destination.
  324. @param(source Source reference of copy)
  325. @param(dest Destination reference of copy)
  326. }
  327. procedure g_copyshortstring(list : TAsmList;const source,dest : treference;len:byte);
  328. procedure g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);
  329. procedure g_decrrefcount(list : TAsmList;t: tdef; const ref: treference);
  330. procedure g_initialize(list : TAsmList;t : tdef;const ref : treference);
  331. procedure g_finalize(list : TAsmList;t : tdef;const ref : treference);
  332. {# Generates range checking code. It is to note
  333. that this routine does not need to be overriden,
  334. as it takes care of everything.
  335. @param(p Node which contains the value to check)
  336. @param(todef Type definition of node to range check)
  337. }
  338. procedure g_rangecheck(list: TAsmList; const l:tlocation; fromdef,todef: tdef); virtual;
  339. {# Generates overflow checking code for a node }
  340. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef); virtual;abstract;
  341. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);virtual;
  342. procedure g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:aint;destreg:tregister);virtual;
  343. procedure g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);virtual;
  344. {# Emits instructions when compilation is done in profile
  345. mode (this is set as a command line option). The default
  346. behavior does nothing, should be overriden as required.
  347. }
  348. procedure g_profilecode(list : TAsmList);virtual;
  349. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  350. @param(size Number of bytes to allocate)
  351. }
  352. procedure g_stackpointer_alloc(list : TAsmList;size : longint);virtual; abstract;
  353. {# Emits instruction for allocating the locals in entry
  354. code of a routine. This is one of the first
  355. routine called in @var(genentrycode).
  356. @param(localsize Number of bytes to allocate as locals)
  357. }
  358. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);virtual; abstract;
  359. {# Emits instructions for returning from a subroutine.
  360. Should also restore the framepointer and stack.
  361. @param(parasize Number of bytes of parameters to deallocate from stack)
  362. }
  363. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);virtual;abstract;
  364. {# This routine is called when generating the code for the entry point
  365. of a routine. It should save all registers which are not used in this
  366. routine, and which should be declared as saved in the std_saved_registers
  367. set.
  368. This routine is mainly used when linking to code which is generated
  369. by ABI-compliant compilers (like GCC), to make sure that the reserved
  370. registers of that ABI are not clobbered.
  371. @param(usedinproc Registers which are used in the code of this routine)
  372. }
  373. procedure g_save_standard_registers(list:TAsmList);virtual;
  374. {# This routine is called when generating the code for the exit point
  375. of a routine. It should restore all registers which were previously
  376. saved in @var(g_save_standard_registers).
  377. @param(usedinproc Registers which are used in the code of this routine)
  378. }
  379. procedure g_restore_standard_registers(list:TAsmList);virtual;
  380. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);virtual;abstract;
  381. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);virtual;
  382. function g_indirect_sym_load(list:TAsmList;const symname: string): tregister;virtual;
  383. protected
  384. procedure get_subsetref_load_info(const sref: tsubsetreference; out loadsize: tcgsize; out extra_load: boolean);
  385. procedure a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister); virtual;
  386. procedure a_load_subsetref_regs_index(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister); virtual;
  387. end;
  388. {$ifndef cpu64bit}
  389. {# @abstract(Abstract code generator for 64 Bit operations)
  390. This class implements an abstract code generator class
  391. for 64 Bit operations.
  392. }
  393. tcg64 = class
  394. procedure a_load64_const_ref(list : TAsmList;value : int64;const ref : treference);virtual;abstract;
  395. procedure a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference);virtual;abstract;
  396. procedure a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64);virtual;abstract;
  397. procedure a_load64_reg_reg(list : TAsmList;regsrc,regdst : tregister64);virtual;abstract;
  398. procedure a_load64_const_reg(list : TAsmList;value : int64;reg : tregister64);virtual;abstract;
  399. procedure a_load64_loc_reg(list : TAsmList;const l : tlocation;reg : tregister64);virtual;abstract;
  400. procedure a_load64_loc_ref(list : TAsmList;const l : tlocation;const ref : treference);virtual;abstract;
  401. procedure a_load64_const_loc(list : TAsmList;value : int64;const l : tlocation);virtual;abstract;
  402. procedure a_load64_reg_loc(list : TAsmList;reg : tregister64;const l : tlocation);virtual;abstract;
  403. procedure a_load64high_reg_ref(list : TAsmList;reg : tregister;const ref : treference);virtual;abstract;
  404. procedure a_load64low_reg_ref(list : TAsmList;reg : tregister;const ref : treference);virtual;abstract;
  405. procedure a_load64high_ref_reg(list : TAsmList;const ref : treference;reg : tregister);virtual;abstract;
  406. procedure a_load64low_ref_reg(list : TAsmList;const ref : treference;reg : tregister);virtual;abstract;
  407. procedure a_load64high_loc_reg(list : TAsmList;const l : tlocation;reg : tregister);virtual;abstract;
  408. procedure a_load64low_loc_reg(list : TAsmList;const l : tlocation;reg : tregister);virtual;abstract;
  409. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);virtual;abstract;
  410. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);virtual;abstract;
  411. procedure a_op64_reg_ref(list : TAsmList;op:TOpCG;size : tcgsize;regsrc : tregister64;const ref : treference);virtual;abstract;
  412. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;regdst : tregister64);virtual;abstract;
  413. procedure a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);virtual;abstract;
  414. procedure a_op64_const_loc(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const l: tlocation);virtual;abstract;
  415. procedure a_op64_reg_loc(list : TAsmList;op:TOpCG;size : tcgsize;reg : tregister64;const l : tlocation);virtual;abstract;
  416. procedure a_op64_loc_reg(list : TAsmList;op:TOpCG;size : tcgsize;const l : tlocation;reg64 : tregister64);virtual;abstract;
  417. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);virtual;
  418. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);virtual;
  419. procedure a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  420. procedure a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  421. procedure a_param64_reg(list : TAsmList;reg64 : tregister64;const loc : TCGPara);virtual;abstract;
  422. procedure a_param64_const(list : TAsmList;value : int64;const loc : TCGPara);virtual;abstract;
  423. procedure a_param64_ref(list : TAsmList;const r : treference;const loc : TCGPara);virtual;abstract;
  424. procedure a_param64_loc(list : TAsmList;const l : tlocation;const loc : TCGPara);virtual;abstract;
  425. {
  426. This routine tries to optimize the const_reg opcode, and should be
  427. called at the start of a_op64_const_reg. It returns the actual opcode
  428. to emit, and the constant value to emit. If this routine returns
  429. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  430. @param(op The opcode to emit, returns the opcode which must be emitted)
  431. @param(a The constant which should be emitted, returns the constant which must
  432. be emitted)
  433. @param(reg The register to emit the opcode with, returns the register with
  434. which the opcode will be emitted)
  435. }
  436. function optimize64_op_const_reg(list: TAsmList; var op: topcg; var a : int64; var reg: tregister64): boolean;virtual;abstract;
  437. { override to catch 64bit rangechecks }
  438. procedure g_rangecheck64(list: TAsmList; const l:tlocation; fromdef,todef: tdef);virtual;abstract;
  439. end;
  440. {$endif cpu64bit}
  441. var
  442. {# Main code generator class }
  443. cg : tcg;
  444. {$ifndef cpu64bit}
  445. {# Code generator class for all operations working with 64-Bit operands }
  446. cg64 : tcg64;
  447. {$endif cpu64bit}
  448. implementation
  449. uses
  450. globals,options,systems,
  451. verbose,defutil,paramgr,symsym,
  452. tgobj,cutils,procinfo;
  453. {*****************************************************************************
  454. basic functionallity
  455. ******************************************************************************}
  456. constructor tcg.create;
  457. begin
  458. end;
  459. {*****************************************************************************
  460. register allocation
  461. ******************************************************************************}
  462. procedure tcg.init_register_allocators;
  463. begin
  464. fillchar(rg,sizeof(rg),0);
  465. add_reg_instruction_hook:=@add_reg_instruction;
  466. end;
  467. procedure tcg.done_register_allocators;
  468. begin
  469. { Safety }
  470. fillchar(rg,sizeof(rg),0);
  471. add_reg_instruction_hook:=nil;
  472. end;
  473. {$ifdef flowgraph}
  474. procedure Tcg.init_flowgraph;
  475. begin
  476. aktflownode:=0;
  477. end;
  478. procedure Tcg.done_flowgraph;
  479. begin
  480. end;
  481. {$endif}
  482. function tcg.getintregister(list:TAsmList;size:Tcgsize):Tregister;
  483. begin
  484. if not assigned(rg[R_INTREGISTER]) then
  485. internalerror(200312122);
  486. result:=rg[R_INTREGISTER].getregister(list,cgsize2subreg(size));
  487. end;
  488. function tcg.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  489. begin
  490. if not assigned(rg[R_FPUREGISTER]) then
  491. internalerror(200312123);
  492. result:=rg[R_FPUREGISTER].getregister(list,cgsize2subreg(size));
  493. end;
  494. function tcg.getmmregister(list:TAsmList;size:Tcgsize):Tregister;
  495. begin
  496. if not assigned(rg[R_MMREGISTER]) then
  497. internalerror(2003121214);
  498. result:=rg[R_MMREGISTER].getregister(list,cgsize2subreg(size));
  499. end;
  500. function tcg.getaddressregister(list:TAsmList):Tregister;
  501. begin
  502. if assigned(rg[R_ADDRESSREGISTER]) then
  503. result:=rg[R_ADDRESSREGISTER].getregister(list,R_SUBWHOLE)
  504. else
  505. begin
  506. if not assigned(rg[R_INTREGISTER]) then
  507. internalerror(200312121);
  508. result:=rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  509. end;
  510. end;
  511. function Tcg.makeregsize(list:TAsmList;reg:Tregister;size:Tcgsize):Tregister;
  512. var
  513. subreg:Tsubregister;
  514. begin
  515. subreg:=cgsize2subreg(size);
  516. result:=reg;
  517. setsubreg(result,subreg);
  518. { notify RA }
  519. if result<>reg then
  520. list.concat(tai_regalloc.resize(result));
  521. end;
  522. procedure tcg.getcpuregister(list:TAsmList;r:Tregister);
  523. begin
  524. if not assigned(rg[getregtype(r)]) then
  525. internalerror(200312125);
  526. rg[getregtype(r)].getcpuregister(list,r);
  527. end;
  528. procedure tcg.ungetcpuregister(list:TAsmList;r:Tregister);
  529. begin
  530. if not assigned(rg[getregtype(r)]) then
  531. internalerror(200312126);
  532. rg[getregtype(r)].ungetcpuregister(list,r);
  533. end;
  534. procedure tcg.alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  535. begin
  536. if assigned(rg[rt]) then
  537. rg[rt].alloccpuregisters(list,r)
  538. else
  539. internalerror(200310092);
  540. end;
  541. procedure tcg.allocallcpuregisters(list:TAsmList);
  542. begin
  543. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  544. {$ifndef i386}
  545. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  546. {$ifdef cpumm}
  547. alloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  548. {$endif cpumm}
  549. {$endif i386}
  550. end;
  551. procedure tcg.dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  552. begin
  553. if assigned(rg[rt]) then
  554. rg[rt].dealloccpuregisters(list,r)
  555. else
  556. internalerror(200310093);
  557. end;
  558. procedure tcg.deallocallcpuregisters(list:TAsmList);
  559. begin
  560. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  561. {$ifndef i386}
  562. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  563. {$ifdef cpumm}
  564. dealloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  565. {$endif cpumm}
  566. {$endif i386}
  567. end;
  568. function tcg.uses_registers(rt:Tregistertype):boolean;
  569. begin
  570. if assigned(rg[rt]) then
  571. result:=rg[rt].uses_registers
  572. else
  573. result:=false;
  574. end;
  575. procedure tcg.add_reg_instruction(instr:Tai;r:tregister);
  576. var
  577. rt : tregistertype;
  578. begin
  579. rt:=getregtype(r);
  580. { Only add it when a register allocator is configured.
  581. No IE can be generated, because the VMT is written
  582. without a valid rg[] }
  583. if assigned(rg[rt]) then
  584. rg[rt].add_reg_instruction(instr,r);
  585. end;
  586. procedure tcg.add_move_instruction(instr:Taicpu);
  587. var
  588. rt : tregistertype;
  589. begin
  590. rt:=getregtype(instr.oper[O_MOV_SOURCE]^.reg);
  591. if assigned(rg[rt]) then
  592. rg[rt].add_move_instruction(instr)
  593. else
  594. internalerror(200310095);
  595. end;
  596. procedure tcg.set_regalloc_extend_backwards(b: boolean);
  597. var
  598. rt : tregistertype;
  599. begin
  600. for rt:=low(rg) to high(rg) do
  601. begin
  602. if assigned(rg[rt]) then
  603. rg[rt].extend_live_range_backwards := b;;
  604. end;
  605. end;
  606. procedure tcg.do_register_allocation(list:TAsmList;headertai:tai);
  607. var
  608. rt : tregistertype;
  609. begin
  610. for rt:=R_FPUREGISTER to R_SPECIALREGISTER do
  611. begin
  612. if assigned(rg[rt]) then
  613. rg[rt].do_register_allocation(list,headertai);
  614. end;
  615. { running the other register allocator passes could require addition int/addr. registers
  616. when spilling so run int/addr register allocation at the end }
  617. if assigned(rg[R_INTREGISTER]) then
  618. rg[R_INTREGISTER].do_register_allocation(list,headertai);
  619. if assigned(rg[R_ADDRESSREGISTER]) then
  620. rg[R_ADDRESSREGISTER].do_register_allocation(list,headertai);
  621. end;
  622. procedure tcg.translate_register(var reg : tregister);
  623. begin
  624. rg[getregtype(reg)].translate_register(reg);
  625. end;
  626. procedure tcg.a_reg_alloc(list : TAsmList;r : tregister);
  627. begin
  628. list.concat(tai_regalloc.alloc(r,nil));
  629. end;
  630. procedure tcg.a_reg_dealloc(list : TAsmList;r : tregister);
  631. begin
  632. list.concat(tai_regalloc.dealloc(r,nil));
  633. end;
  634. procedure tcg.a_reg_sync(list : TAsmList;r : tregister);
  635. var
  636. instr : tai;
  637. begin
  638. instr:=tai_regalloc.sync(r);
  639. list.concat(instr);
  640. add_reg_instruction(instr,r);
  641. end;
  642. procedure tcg.a_label(list : TAsmList;l : tasmlabel);
  643. begin
  644. list.concat(tai_label.create(l));
  645. end;
  646. {*****************************************************************************
  647. for better code generation these methods should be overridden
  648. ******************************************************************************}
  649. procedure tcg.a_param_reg(list : TAsmList;size : tcgsize;r : tregister;const cgpara : TCGPara);
  650. var
  651. ref : treference;
  652. begin
  653. cgpara.check_simple_location;
  654. case cgpara.location^.loc of
  655. LOC_REGISTER,LOC_CREGISTER:
  656. a_load_reg_reg(list,size,cgpara.location^.size,r,cgpara.location^.register);
  657. LOC_REFERENCE,LOC_CREFERENCE:
  658. begin
  659. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  660. a_load_reg_ref(list,size,cgpara.location^.size,r,ref);
  661. end
  662. else
  663. internalerror(2002071004);
  664. end;
  665. end;
  666. procedure tcg.a_param_const(list : TAsmList;size : tcgsize;a : aint;const cgpara : TCGPara);
  667. var
  668. ref : treference;
  669. begin
  670. cgpara.check_simple_location;
  671. case cgpara.location^.loc of
  672. LOC_REGISTER,LOC_CREGISTER:
  673. a_load_const_reg(list,cgpara.location^.size,a,cgpara.location^.register);
  674. LOC_REFERENCE,LOC_CREFERENCE:
  675. begin
  676. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  677. a_load_const_ref(list,cgpara.location^.size,a,ref);
  678. end
  679. else
  680. internalerror(2002071004);
  681. end;
  682. end;
  683. procedure tcg.a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const cgpara : TCGPara);
  684. var
  685. ref : treference;
  686. begin
  687. cgpara.check_simple_location;
  688. case cgpara.location^.loc of
  689. LOC_REGISTER,LOC_CREGISTER:
  690. a_load_ref_reg(list,size,cgpara.location^.size,r,cgpara.location^.register);
  691. LOC_REFERENCE,LOC_CREFERENCE:
  692. begin
  693. reference_reset(ref);
  694. ref.base:=cgpara.location^.reference.index;
  695. ref.offset:=cgpara.location^.reference.offset;
  696. if (size <> OS_NO) and
  697. (tcgsize2size[size] < sizeof(aint)) then
  698. begin
  699. if (cgpara.size = OS_NO) or
  700. assigned(cgpara.location^.next) then
  701. internalerror(2006052401);
  702. a_load_ref_ref(list,size,cgpara.size,r,ref);
  703. end
  704. else
  705. { use concatcopy, because the parameter can be larger than }
  706. { what the OS_* constants can handle }
  707. g_concatcopy(list,r,ref,cgpara.intsize);
  708. end
  709. else
  710. internalerror(2002071004);
  711. end;
  712. end;
  713. procedure tcg.a_param_loc(list : TAsmList;const l:tlocation;const cgpara : TCGPara);
  714. begin
  715. case l.loc of
  716. LOC_REGISTER,
  717. LOC_CREGISTER :
  718. a_param_reg(list,l.size,l.register,cgpara);
  719. LOC_CONSTANT :
  720. a_param_const(list,l.size,l.value,cgpara);
  721. LOC_CREFERENCE,
  722. LOC_REFERENCE :
  723. a_param_ref(list,l.size,l.reference,cgpara);
  724. else
  725. internalerror(2002032211);
  726. end;
  727. end;
  728. procedure tcg.a_paramaddr_ref(list : TAsmList;const r : treference;const cgpara : TCGPara);
  729. var
  730. hr : tregister;
  731. begin
  732. cgpara.check_simple_location;
  733. hr:=getaddressregister(list);
  734. a_loadaddr_ref_reg(list,r,hr);
  735. a_param_reg(list,OS_ADDR,hr,cgpara);
  736. end;
  737. {****************************************************************************
  738. some generic implementations
  739. ****************************************************************************}
  740. {$ifopt r+}
  741. {$define rangeon}
  742. {$endif}
  743. {$ifopt q+}
  744. {$define overflowon}
  745. {$endif}
  746. procedure tcg.a_load_subsetreg_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; destreg: tregister);
  747. var
  748. bitmask: aint;
  749. tmpreg: tregister;
  750. stopbit: byte;
  751. begin
  752. tmpreg:=getintregister(list,sreg.subsetregsize);
  753. a_op_const_reg_reg(list,OP_SHR,sreg.subsetregsize,sreg.startbit,sreg.subsetreg,tmpreg);
  754. stopbit := sreg.startbit + sreg.bitlen;
  755. // on x86(64), 1 shl 32(64) = 1 instead of 0
  756. if (stopbit - sreg.startbit <> AIntBits) then
  757. bitmask := (1 shl (stopbit - sreg.startbit)) - 1
  758. else
  759. bitmask := -1;
  760. a_op_const_reg(list,OP_AND,sreg.subsetregsize,bitmask,tmpreg);
  761. tmpreg := makeregsize(list,tmpreg,subsetsize);
  762. a_load_reg_reg(list,tcgsize2unsigned[subsetsize],subsetsize,tmpreg,tmpreg);
  763. a_load_reg_reg(list,subsetsize,tosize,tmpreg,destreg);
  764. end;
  765. procedure tcg.a_load_reg_subsetreg(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister);
  766. var
  767. bitmask: aint;
  768. tmpreg: tregister;
  769. stopbit: byte;
  770. begin
  771. stopbit := sreg.startbit + sreg.bitlen;
  772. // on x86(64), 1 shl 32(64) = 1 instead of 0
  773. if (stopbit <> AIntBits) then
  774. bitmask := not(((1 shl stopbit)-1) xor ((1 shl sreg.startbit)-1))
  775. else
  776. bitmask := not(-1 xor ((1 shl sreg.startbit)-1));
  777. tmpreg:=getintregister(list,sreg.subsetregsize);
  778. a_load_reg_reg(list,fromsize,sreg.subsetregsize,fromreg,tmpreg);
  779. a_op_const_reg(list,OP_SHL,sreg.subsetregsize,sreg.startbit,tmpreg);
  780. a_op_const_reg(list,OP_AND,sreg.subsetregsize,not(bitmask),tmpreg);
  781. a_op_const_reg(list,OP_AND,sreg.subsetregsize,bitmask,sreg.subsetreg);
  782. a_op_reg_reg(list,OP_OR,sreg.subsetregsize,tmpreg,sreg.subsetreg);
  783. end;
  784. procedure tcg.a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsreg, tosreg: tsubsetregister);
  785. var
  786. tmpreg: tregister;
  787. bitmask: aint;
  788. stopbit: byte;
  789. begin
  790. if (fromsreg.bitlen >= tosreg.bitlen) then
  791. begin
  792. tmpreg := getintregister(list,tosreg.subsetregsize);
  793. a_load_reg_reg(list,fromsreg.subsetregsize,tosreg.subsetregsize,fromsreg.subsetreg,tmpreg);
  794. if (fromsreg.startbit <= tosreg.startbit) then
  795. a_op_const_reg(list,OP_SHL,tosreg.subsetregsize,tosreg.startbit-fromsreg.startbit,tmpreg)
  796. else
  797. a_op_const_reg(list,OP_SHR,tosreg.subsetregsize,fromsreg.startbit-tosreg.startbit,tmpreg);
  798. stopbit := tosreg.startbit + tosreg.bitlen;
  799. // on x86(64), 1 shl 32(64) = 1 instead of 0
  800. if (stopbit <> AIntBits) then
  801. bitmask := not(((1 shl stopbit)-1) xor ((1 shl tosreg.startbit)-1))
  802. else
  803. bitmask := (1 shl tosreg.startbit) - 1;
  804. a_op_const_reg(list,OP_AND,tosreg.subsetregsize,bitmask,tosreg.subsetreg);
  805. a_op_const_reg(list,OP_AND,tosreg.subsetregsize,not(bitmask),tmpreg);
  806. a_op_reg_reg(list,OP_OR,tosreg.subsetregsize,tmpreg,tosreg.subsetreg);
  807. end
  808. else
  809. begin
  810. tmpreg := getintregister(list,tosubsetsize);
  811. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  812. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  813. end;
  814. end;
  815. procedure tcg.a_load_subsetreg_ref(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; const destref: treference);
  816. var
  817. tmpreg: tregister;
  818. begin
  819. tmpreg := getintregister(list,tosize);
  820. a_load_subsetreg_reg(list,subsetsize,tosize,sreg,tmpreg);
  821. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  822. end;
  823. procedure tcg.a_load_ref_subsetreg(list : TAsmList; fromsize, subsetsize: tcgsize; const fromref: treference; const sreg: tsubsetregister);
  824. var
  825. tmpreg: tregister;
  826. begin
  827. tmpreg := getintregister(list,subsetsize);
  828. a_load_ref_reg(list,fromsize,subsetsize,fromref,tmpreg);
  829. a_load_reg_subsetreg(list,subsetsize,subsetsize,tmpreg,sreg);
  830. end;
  831. procedure tcg.a_load_const_subsetreg(list: TAsmlist; subsetsize: tcgsize; a: aint; const sreg: tsubsetregister);
  832. var
  833. bitmask: aint;
  834. stopbit: byte;
  835. begin
  836. stopbit := sreg.startbit + sreg.bitlen;
  837. // on x86(64), 1 shl 32(64) = 1 instead of 0
  838. if (stopbit <> AIntBits) then
  839. bitmask := not(((1 shl stopbit)-1) xor ((1 shl sreg.startbit)-1))
  840. else
  841. bitmask := (1 shl sreg.startbit) - 1;
  842. a_op_const_reg(list,OP_AND,sreg.subsetregsize,bitmask,sreg.subsetreg);
  843. a_op_const_reg(list,OP_OR,sreg.subsetregsize,(a shl sreg.startbit) and not(bitmask),sreg.subsetreg);
  844. end;
  845. procedure tcg.a_load_loc_subsetref(list : TAsmList;subsetsize: tcgsize; const loc: tlocation; const sref : tsubsetreference);
  846. begin
  847. case loc.loc of
  848. LOC_REFERENCE,LOC_CREFERENCE:
  849. a_load_ref_subsetref(list,loc.size,subsetsize,loc.reference,sref);
  850. LOC_REGISTER,LOC_CREGISTER:
  851. a_load_reg_subsetref(list,loc.size,subsetsize,loc.register,sref);
  852. LOC_CONSTANT:
  853. a_load_const_subsetref(list,subsetsize,loc.value,sref);
  854. LOC_SUBSETREG,LOC_CSUBSETREG:
  855. a_load_subsetreg_subsetref(list,loc.size,subsetsize,loc.sreg,sref);
  856. LOC_SUBSETREF,LOC_CSUBSETREF:
  857. a_load_subsetref_subsetref(list,loc.size,subsetsize,loc.sref,sref);
  858. else
  859. internalerror(200608053);
  860. end;
  861. end;
  862. (*
  863. Subsetrefs are used for (bit)packed arrays and (bit)packed records stored
  864. in memory. They are like a regular reference, but contain an extra bit
  865. offset (either constant -startbit- or variable -bitindexreg, always OS_INT)
  866. and a bit length (always constant).
  867. Bit packed values are stored differently in memory depending on whether we
  868. are on a big or a little endian system (compatible with at least GPC). The
  869. size of the basic working unit is always the smallest power-of-2 byte size
  870. which can contain the bit value (so 1..8 bits -> 1 byte, 9..16 bits -> 2
  871. bytes, 17..32 bits -> 4 bytes etc).
  872. On a big endian, 5-bit: values are stored like this:
  873. 11111222 22333334 44445555 56666677 77788888
  874. The leftmost bit of each 5-bit value corresponds to the most significant
  875. bit.
  876. On little endian, it goes like this:
  877. 22211111 43333322 55554444 77666665 88888777
  878. In this case, per byte the left-most bit is more significant than those on
  879. the right, but the bits in the next byte are all more significant than
  880. those in the previous byte (e.g., the 222 in the first byte are the low
  881. three bits of that value, while the 22 in the second byte are the upper
  882. three bits.
  883. Big endian, 9 bit values:
  884. 11111111 12222222 22333333 33344444 ...
  885. Little endian, 9 bit values:
  886. 11111111 22222221 33333322 44444333 ...
  887. This is memory representation and the 16 bit values are byteswapped.
  888. Similarly as in the previous case, the 2222222 string contains the lower
  889. bits of value 2 and the 22 string contains the upper bits. Once loaded into
  890. registers (two 16 bit registers in the current implementation, although a
  891. single 32 bit register would be possible too, in particular if 32 bit
  892. alignment can be guaranteed), this becomes:
  893. 22222221 11111111 44444333 33333322 ...
  894. (l)ow u l l u l u
  895. The startbit/bitindex in a subsetreference always refers to
  896. a) on big endian: the most significant bit of the value
  897. (bits counted from left to right, both memory an registers)
  898. b) on little endia: the least significant bit when the value
  899. is loaded in a register (bit counted from right to left)
  900. Although a) results in more complex code for big endian systems, it's
  901. needed for compatibility both with GPC and with e.g. bitpacked arrays in
  902. Apple's universal interfaces which depend on these layout differences).
  903. Note: when changing the loadsize calculated in get_subsetref_load_info,
  904. make sure the appropriate alignment is guaranteed, at least in case of
  905. {$defined cpurequiresproperalignment}.
  906. *)
  907. procedure tcg.get_subsetref_load_info(const sref: tsubsetreference; out loadsize: tcgsize; out extra_load: boolean);
  908. var
  909. intloadsize: aint;
  910. begin
  911. intloadsize := sref.ref.alignment;
  912. if (intloadsize = 0) then
  913. internalerror(2006081310);
  914. if (intloadsize > sizeof(aint)) then
  915. intloadsize := sizeof(aint);
  916. loadsize := int_cgsize(intloadsize);
  917. if (loadsize = OS_NO) then
  918. internalerror(2006081311);
  919. if (sref.bitlen > sizeof(aint)*8) then
  920. internalerror(2006081312);
  921. extra_load :=
  922. (intloadsize <> 1) and
  923. ((sref.bitindexreg <> NR_NO) or
  924. (byte(sref.startbit+sref.bitlen) > byte(intloadsize*8)));
  925. end;
  926. procedure tcg.a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister);
  927. var
  928. restbits: byte;
  929. begin
  930. if (target_info.endian = endian_big) then
  931. begin
  932. { valuereg contains the upper bits, extra_value_reg the lower }
  933. restbits := (sref.bitlen - (loadbitsize - sref.startbit));
  934. a_op_const_reg(list,OP_SHL,OS_INT,restbits,valuereg);
  935. { mask other bits }
  936. a_op_const_reg(list,OP_AND,OS_INT,(1 shl sref.bitlen)-1,valuereg);
  937. a_op_const_reg(list,OP_SHR,OS_INT,loadbitsize-restbits,extra_value_reg)
  938. end
  939. else
  940. begin
  941. { valuereg contains the lower bits, extra_value_reg the upper }
  942. a_op_const_reg(list,OP_SHR,OS_INT,sref.startbit,valuereg);
  943. a_op_const_reg(list,OP_SHL,OS_INT,loadbitsize-sref.startbit,extra_value_reg);
  944. { mask other bits }
  945. a_op_const_reg(list,OP_AND,OS_INT,(1 shl sref.bitlen)-1,extra_value_reg);
  946. end;
  947. { merge }
  948. a_op_reg_reg(list,OP_OR,OS_INT,extra_value_reg,valuereg);
  949. end;
  950. procedure tcg.a_load_subsetref_regs_index(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister);
  951. var
  952. tmpreg, maskreg: tregister;
  953. begin
  954. tmpreg := getintregister(list,OS_INT);
  955. if (target_info.endian = endian_big) then
  956. begin
  957. { since this is a dynamic index, it's possible that the value }
  958. { is entirely in valuereg. }
  959. { get the data in valuereg in the right place }
  960. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,valuereg);
  961. a_op_const_reg(list,OP_SHR,OS_INT,loadbitsize-sref.bitlen,valuereg);
  962. if (loadbitsize <> AIntBits) then
  963. { mask left over bits }
  964. a_op_const_reg(list,OP_AND,OS_INT,(1 shl sref.bitlen)-1,valuereg);
  965. tmpreg := getintregister(list,OS_INT);
  966. { the bits in extra_value_reg (if any) start at the most significant bit => }
  967. { extra_value_reg must be shr by (loadbitsize-sref.bitlen)+(loadsize-sref.bitindex) }
  968. { => = -(sref.bitindex+(sref.bitlen-2*loadbitsize)) }
  969. a_op_const_reg_reg(list,OP_ADD,OS_INT,sref.bitlen-2*loadbitsize,sref.bitindexreg,tmpreg);
  970. a_op_reg_reg(list,OP_NEG,OS_INT,tmpreg,tmpreg);
  971. a_op_reg_reg(list,OP_SHR,OS_INT,tmpreg,extra_value_reg);
  972. { if there are no bits in extra_value_reg, then sref.bitindex was }
  973. { < loadsize-sref.bitlen, and therefore tmpreg will now be >= loadsize }
  974. { => extra_value_reg is now 0 }
  975. { merge }
  976. a_op_reg_reg(list,OP_OR,OS_INT,extra_value_reg,valuereg);
  977. { no need to mask, necessary masking happened earlier on }
  978. end
  979. else
  980. begin
  981. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,valuereg);
  982. { Y-x = -(Y-x) }
  983. a_op_const_reg_reg(list,OP_SUB,OS_INT,loadbitsize,sref.bitindexreg,tmpreg);
  984. a_op_reg_reg(list,OP_NEG,OS_INT,tmpreg,tmpreg);
  985. { tmpreg is in the range 1..<cpu_bitsize> -> will zero extra_value_reg }
  986. { if all bits are in valuereg }
  987. a_op_reg_reg(list,OP_SHL,OS_INT,tmpreg,extra_value_reg);
  988. {$ifdef x86}
  989. { on i386 "x shl 32 = x shl 0", on x86/64 "x shl 64 = x shl 0". Fix so it's 0. }
  990. if (loadbitsize = AIntBits) then
  991. begin
  992. { if (tmpreg >= cpu_bit_size) then tmpreg := 1 else tmpreg := 0 }
  993. a_op_const_reg(list,OP_SHR,OS_INT,{$ifdef cpu64bit}6{$else}5{$endif},tmpreg);
  994. { if (tmpreg = cpu_bit_size) then tmpreg := 0 else tmpreg := -1 }
  995. a_op_const_reg(list,OP_SUB,OS_INT,1,tmpreg);
  996. { if (tmpreg = cpu_bit_size) then extra_value_reg := 0 }
  997. a_op_reg_reg(list,OP_AND,OS_INT,tmpreg,extra_value_reg);
  998. end;
  999. {$endif x86}
  1000. { merge }
  1001. a_op_reg_reg(list,OP_OR,OS_INT,extra_value_reg,valuereg);
  1002. { mask other bits }
  1003. a_op_const_reg(list,OP_AND,OS_INT,(1 shl sref.bitlen)-1,valuereg);
  1004. end;
  1005. end;
  1006. procedure tcg.a_load_subsetref_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sref: tsubsetreference; destreg: tregister);
  1007. var
  1008. tmpref: treference;
  1009. valuereg,tmpreg,maskreg,extra_value_reg: tregister;
  1010. tosreg: tsubsetregister;
  1011. loadsize: tcgsize;
  1012. loadbitsize, restbits: byte;
  1013. extra_load: boolean;
  1014. begin
  1015. get_subsetref_load_info(sref,loadsize,extra_load);
  1016. loadbitsize := tcgsize2size[loadsize]*8;
  1017. { load the (first part) of the bit sequence }
  1018. valuereg := cg.getintregister(list,OS_INT);
  1019. a_load_ref_reg(list,loadsize,OS_INT,sref.ref,valuereg);
  1020. if not extra_load then
  1021. begin
  1022. { everything is guaranteed to be in a single register of loadsize }
  1023. if (sref.bitindexreg = NR_NO) then
  1024. begin
  1025. { use subsetreg routine, it may have been overridden with an optimized version }
  1026. tosreg.subsetreg := valuereg;
  1027. tosreg.subsetregsize := OS_INT;
  1028. { subsetregs always count bits from right to left }
  1029. if (target_info.endian = endian_big) then
  1030. tosreg.startbit := loadbitsize - (sref.startbit+sref.bitlen)
  1031. else
  1032. tosreg.startbit := sref.startbit;
  1033. tosreg.bitlen := sref.bitlen;
  1034. a_load_subsetreg_reg(list,subsetsize,tosize,tosreg,destreg);
  1035. exit;
  1036. end
  1037. else
  1038. begin
  1039. if (sref.startbit <> 0) then
  1040. internalerror(2006081510);
  1041. if (target_info.endian = endian_big) then
  1042. begin
  1043. tmpreg := cg.getintregister(list,OS_INT);
  1044. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,valuereg);
  1045. a_op_const_reg(list,OP_SHR,OS_INT,loadbitsize-sref.bitlen,valuereg);
  1046. end
  1047. else
  1048. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,valuereg);
  1049. { mask other bits }
  1050. a_op_const_reg(list,OP_AND,OS_INT,(1 shl sref.bitlen)-1,valuereg);
  1051. end
  1052. end
  1053. else
  1054. begin
  1055. { load next value as well }
  1056. extra_value_reg := getintregister(list,OS_INT);
  1057. tmpref := sref.ref;
  1058. inc(tmpref.offset,loadbitsize div 8);
  1059. a_load_ref_reg(list,loadsize,OS_INT,tmpref,extra_value_reg);
  1060. if (sref.bitindexreg = NR_NO) then
  1061. { can be overridden to optimize }
  1062. a_load_subsetref_regs_noindex(list,subsetsize,loadbitsize,sref,valuereg,extra_value_reg)
  1063. else
  1064. begin
  1065. if (sref.startbit <> 0) then
  1066. internalerror(2006080610);
  1067. a_load_subsetref_regs_index(list,subsetsize,loadbitsize,sref,valuereg,extra_value_reg);
  1068. end;
  1069. end;
  1070. { store in destination }
  1071. { (types with a negative lower bound are always a base type (8, 16, 32, 64 bits) }
  1072. if ((sref.bitlen mod 8) = 0) then
  1073. begin
  1074. { since we know all necessary bits are already masked, avoid unnecessary }
  1075. { zero-extensions }
  1076. valuereg := makeregsize(list,valuereg,tosize);
  1077. a_load_reg_reg(list,tcgsize2unsigned[tosize],tosize,valuereg,destreg)
  1078. end
  1079. else
  1080. begin
  1081. { avoid unnecessary sign extension and zeroing }
  1082. valuereg := makeregsize(list,valuereg,OS_INT);
  1083. destreg := makeregsize(list,destreg,OS_INT);
  1084. a_load_reg_reg(list,OS_INT,OS_INT,valuereg,destreg);
  1085. destreg := makeregsize(list,destreg,tosize);
  1086. end
  1087. end;
  1088. procedure tcg.a_load_reg_subsetref(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sref: tsubsetreference);
  1089. var
  1090. tmpreg, tmpindexreg, valuereg, extra_value_reg, maskreg: tregister;
  1091. tosreg, fromsreg: tsubsetregister;
  1092. tmpref: treference;
  1093. loadsize: tcgsize;
  1094. bitmask: aint;
  1095. loadbitsize: byte;
  1096. extra_load: boolean;
  1097. begin
  1098. { the register must be able to contain the requested value }
  1099. if (tcgsize2size[fromsize]*8 < sref.bitlen) then
  1100. internalerror(2006081613);
  1101. get_subsetref_load_info(sref,loadsize,extra_load);
  1102. loadbitsize := tcgsize2size[loadsize]*8;
  1103. { load the (first part) of the bit sequence }
  1104. valuereg := cg.getintregister(list,OS_INT);
  1105. a_load_ref_reg(list,loadsize,OS_INT,sref.ref,valuereg);
  1106. { constant offset of bit sequence? }
  1107. if not extra_load then
  1108. begin
  1109. if (sref.bitindexreg = NR_NO) then
  1110. begin
  1111. { use subsetreg routine, it may have been overridden with an optimized version }
  1112. tosreg.subsetreg := valuereg;
  1113. tosreg.subsetregsize := OS_INT;
  1114. { subsetregs always count bits from right to left }
  1115. if (target_info.endian = endian_big) then
  1116. tosreg.startbit := loadbitsize - (sref.startbit+sref.bitlen)
  1117. else
  1118. tosreg.startbit := sref.startbit;
  1119. tosreg.bitlen := sref.bitlen;
  1120. a_load_reg_subsetreg(list,fromsize,subsetsize,fromreg,tosreg);
  1121. end
  1122. else
  1123. begin
  1124. if (sref.startbit <> 0) then
  1125. internalerror(2006081710);
  1126. { should be handled by normal code and will give wrong result }
  1127. { on x86 for the '1 shl bitlen' below }
  1128. if (sref.bitlen = AIntBits) then
  1129. internalerror(2006081711);
  1130. { calculated correct shiftcount for big endian }
  1131. tmpindexreg := getintregister(list,OS_INT);
  1132. a_load_reg_reg(list,OS_INT,OS_INT,sref.bitindexreg,tmpindexreg);
  1133. if (target_info.endian = endian_big) then
  1134. begin
  1135. a_op_const_reg(list,OP_SUB,OS_INT,loadbitsize-sref.bitlen,tmpindexreg);
  1136. a_op_reg_reg(list,OP_NEG,OS_INT,tmpindexreg,tmpindexreg);
  1137. end;
  1138. { zero the bits we have to insert }
  1139. maskreg := getintregister(list,OS_INT);
  1140. a_load_const_reg(list,OS_INT,(1 shl sref.bitlen)-1,maskreg);
  1141. a_op_reg_reg(list,OP_SHL,OS_INT,tmpindexreg,maskreg);
  1142. a_op_reg_reg(list,OP_NOT,OS_INT,maskreg,maskreg);
  1143. a_op_reg_reg(list,OP_AND,OS_INT,maskreg,valuereg);
  1144. { insert the value }
  1145. tmpreg := getintregister(list,OS_INT);
  1146. a_load_reg_reg(list,fromsize,OS_INT,fromreg,tmpreg);
  1147. a_op_const_reg(list,OP_AND,OS_INT,(1 shl sref.bitlen)-1,tmpreg);
  1148. a_op_reg_reg(list,OP_SHL,OS_INT,tmpindexreg,tmpreg);
  1149. a_op_reg_reg(list,OP_OR,OS_INT,tmpreg,valuereg);
  1150. end;
  1151. { store back to memory }
  1152. valuereg := makeregsize(list,valuereg,loadsize);
  1153. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1154. exit;
  1155. end
  1156. else
  1157. begin
  1158. { load next value }
  1159. extra_value_reg := getintregister(list,OS_INT);
  1160. tmpref := sref.ref;
  1161. inc(tmpref.offset,loadbitsize div 8);
  1162. { should maybe be taken out too, can be done more efficiently }
  1163. { on e.g. i386 with shld/shrd }
  1164. if (sref.bitindexreg = NR_NO) then
  1165. begin
  1166. a_load_ref_reg(list,loadsize,OS_INT,tmpref,extra_value_reg);
  1167. fromsreg.subsetreg := fromreg;
  1168. fromsreg.subsetregsize := fromsize;
  1169. tosreg.subsetreg := valuereg;
  1170. tosreg.subsetregsize := OS_INT;
  1171. { transfer first part }
  1172. fromsreg.bitlen := loadbitsize-sref.startbit;
  1173. tosreg.bitlen := fromsreg.bitlen;
  1174. if (target_info.endian = endian_big) then
  1175. begin
  1176. { valuereg must contain the upper bits of the value at bits [0..loadbitsize-startbit] }
  1177. { upper bits of the value ... }
  1178. fromsreg.startbit := sref.bitlen-(loadbitsize-sref.startbit);
  1179. { ... to bit 0 }
  1180. tosreg.startbit := 0
  1181. end
  1182. else
  1183. begin
  1184. { valuereg must contain the lower bits of the value at bits [startbit..loadbitsize] }
  1185. { lower bits of the value ... }
  1186. fromsreg.startbit := 0;
  1187. { ... to startbit }
  1188. tosreg.startbit := sref.startbit;
  1189. end;
  1190. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  1191. valuereg := makeregsize(list,valuereg,loadsize);
  1192. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1193. { transfer second part }
  1194. if (target_info.endian = endian_big) then
  1195. begin
  1196. { extra_value_reg must contain the lower bits of the value at bits }
  1197. { [(loadbitsize-(bitlen-(loadbitsize-startbit)))..loadbitsize] }
  1198. { (loadbitsize-(bitlen-(loadbitsize-startbit))) = 2*loadbitsize }
  1199. { - bitlen - startbit }
  1200. fromsreg.startbit := 0;
  1201. tosreg.startbit := 2*loadbitsize - sref.bitlen - sref.startbit
  1202. end
  1203. else
  1204. begin
  1205. { extra_value_reg must contain the upper bits of the value at bits [0..bitlen-(loadbitsize-startbit)] }
  1206. fromsreg.startbit := fromsreg.bitlen;
  1207. tosreg.startbit := 0;
  1208. end;
  1209. tosreg.subsetreg := extra_value_reg;
  1210. fromsreg.bitlen := sref.bitlen-fromsreg.bitlen;
  1211. tosreg.bitlen := fromsreg.bitlen;
  1212. a_load_subsetreg_subsetreg(list,fromsize,subsetsize,fromsreg,tosreg);
  1213. extra_value_reg := makeregsize(list,extra_value_reg,loadsize);
  1214. a_load_reg_ref(list,loadsize,loadsize,extra_value_reg,tmpref);
  1215. exit;
  1216. end
  1217. else
  1218. begin
  1219. if (sref.startbit <> 0) then
  1220. internalerror(2006081812);
  1221. { should be handled by normal code and will give wrong result }
  1222. { on x86 for the '1 shl bitlen' below }
  1223. if (sref.bitlen = AIntBits) then
  1224. internalerror(2006081713);
  1225. { generate mask to zero the bits we have to insert }
  1226. maskreg := getintregister(list,OS_INT);
  1227. if (target_info.endian = endian_big) then
  1228. begin
  1229. a_load_const_reg(list,OS_INT,((1 shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen),maskreg);
  1230. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,maskreg);
  1231. end
  1232. else
  1233. begin
  1234. a_load_const_reg(list,OS_INT,(1 shl sref.bitlen)-1,maskreg);
  1235. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,maskreg);
  1236. end;
  1237. a_op_reg_reg(list,OP_NOT,OS_INT,maskreg,maskreg);
  1238. a_op_reg_reg(list,OP_AND,OS_INT,maskreg,valuereg);
  1239. { insert the value }
  1240. tmpreg := getintregister(list,OS_INT);
  1241. a_load_reg_reg(list,fromsize,OS_INT,fromreg,tmpreg);
  1242. if (target_info.endian = endian_big) then
  1243. begin
  1244. a_op_const_reg(list,OP_SHL,OS_INT,loadbitsize-sref.bitlen,tmpreg);
  1245. if (loadbitsize <> AIntBits) then
  1246. { mask left over bits }
  1247. a_op_const_reg(list,OP_AND,OS_INT,((1 shl sref.bitlen)-1) shl (loadbitsize-sref.bitlen),tmpreg);
  1248. a_op_reg_reg(list,OP_SHR,OS_INT,sref.bitindexreg,tmpreg);
  1249. end
  1250. else
  1251. begin
  1252. a_op_const_reg(list,OP_AND,OS_INT,(1 shl sref.bitlen)-1,tmpreg);
  1253. a_op_reg_reg(list,OP_SHL,OS_INT,sref.bitindexreg,tmpreg);
  1254. end;
  1255. a_op_reg_reg(list,OP_OR,OS_INT,tmpreg,valuereg);
  1256. valuereg := makeregsize(list,valuereg,loadsize);
  1257. a_load_reg_ref(list,loadsize,loadsize,valuereg,sref.ref);
  1258. a_load_ref_reg(list,loadsize,OS_INT,tmpref,extra_value_reg);
  1259. tmpindexreg := getintregister(list,OS_INT);
  1260. { load current array value }
  1261. tmpreg := getintregister(list,OS_INT);
  1262. a_load_reg_reg(list,fromsize,OS_INT,fromreg,tmpreg);
  1263. { generate mask to zero the bits we have to insert }
  1264. maskreg := getintregister(list,OS_INT);
  1265. if (target_info.endian = endian_big) then
  1266. begin
  1267. a_op_const_reg_reg(list,OP_ADD,OS_INT,sref.bitlen-2*loadbitsize,sref.bitindexreg,tmpindexreg);
  1268. a_op_reg_reg(list,OP_NEG,OS_INT,tmpindexreg,tmpindexreg);
  1269. a_load_const_reg(list,OS_INT,(1 shl sref.bitlen)-1,maskreg);
  1270. a_op_reg_reg(list,OP_SHL,OS_INT,tmpindexreg,maskreg);
  1271. end
  1272. else
  1273. begin
  1274. { Y-x = -(Y-x) }
  1275. a_op_const_reg_reg(list,OP_SUB,OS_INT,loadbitsize,sref.bitindexreg,tmpindexreg);
  1276. a_op_reg_reg(list,OP_NEG,OS_INT,tmpindexreg,tmpindexreg);
  1277. a_load_const_reg(list,OS_INT,(1 shl sref.bitlen)-1,maskreg);
  1278. a_op_reg_reg(list,OP_SHR,OS_INT,tmpindexreg,maskreg);
  1279. {$ifdef x86}
  1280. { on i386 "x shl 32 = x shl 0", on x86/64 "x shl 64 = x shl 0". Fix so it's 0. }
  1281. if (loadbitsize = AIntBits) then
  1282. begin
  1283. valuereg := getintregister(list,OS_INT);
  1284. { if (tmpreg >= cpu_bit_size) then valuereg := 1 else valuereg := 0 }
  1285. a_op_const_reg_reg(list,OP_SHR,OS_INT,{$ifdef cpu64bit}6{$else}5{$endif},tmpindexreg,valuereg);
  1286. { if (tmpreg = cpu_bit_size) then valuereg := 0 else valuereg := -1 }
  1287. a_op_const_reg(list,OP_SUB,OS_INT,1,valuereg);
  1288. { if (tmpreg = cpu_bit_size) then tmpreg := maskreg := 0 }
  1289. a_op_reg_reg(list,OP_AND,OS_INT,valuereg,tmpreg);
  1290. a_op_reg_reg(list,OP_AND,OS_INT,valuereg,maskreg);
  1291. end;
  1292. {$endif x86}
  1293. end;
  1294. a_op_reg_reg(list,OP_NOT,OS_INT,maskreg,maskreg);
  1295. a_op_reg_reg(list,OP_AND,OS_INT,maskreg,extra_value_reg);
  1296. if (target_info.endian = endian_big) then
  1297. a_op_reg_reg(list,OP_SHL,OS_INT,tmpindexreg,tmpreg)
  1298. else
  1299. begin
  1300. a_op_const_reg(list,OP_AND,OS_INT,(1 shl sref.bitlen)-1,tmpreg);
  1301. a_op_reg_reg(list,OP_SHR,OS_INT,tmpindexreg,tmpreg);
  1302. end;
  1303. a_op_reg_reg(list,OP_OR,OS_INT,tmpreg,extra_value_reg);
  1304. extra_value_reg := makeregsize(list,extra_value_reg,loadsize);
  1305. a_load_reg_ref(list,loadsize,loadsize,extra_value_reg,tmpref);
  1306. end;
  1307. end;
  1308. end;
  1309. procedure tcg.a_load_subsetref_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsref, tosref: tsubsetreference);
  1310. var
  1311. tmpreg: tregister;
  1312. begin
  1313. tmpreg := getintregister(list,tosubsetsize);
  1314. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1315. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1316. end;
  1317. procedure tcg.a_load_subsetref_ref(list : TAsmList; subsetsize, tosize: tcgsize; const sref: tsubsetreference; const destref: treference);
  1318. var
  1319. tmpreg: tregister;
  1320. begin
  1321. tmpreg := getintregister(list,tosize);
  1322. a_load_subsetref_reg(list,subsetsize,tosize,sref,tmpreg);
  1323. a_load_reg_ref(list,tosize,tosize,tmpreg,destref);
  1324. end;
  1325. procedure tcg.a_load_ref_subsetref(list : TAsmList; fromsize, subsetsize: tcgsize; const fromref: treference; const sref: tsubsetreference);
  1326. var
  1327. tmpreg: tregister;
  1328. begin
  1329. tmpreg := getintregister(list,subsetsize);
  1330. a_load_ref_reg(list,fromsize,subsetsize,fromref,tmpreg);
  1331. a_load_reg_subsetref(list,subsetsize,subsetsize,tmpreg,sref);
  1332. end;
  1333. procedure tcg.a_load_const_subsetref(list: TAsmlist; subsetsize: tcgsize; a: aint; const sref: tsubsetreference);
  1334. var
  1335. tmpreg: tregister;
  1336. begin
  1337. tmpreg := getintregister(list,subsetsize);
  1338. a_load_const_reg(list,subsetsize,a,tmpreg);
  1339. a_load_reg_subsetref(list,subsetsize,subsetsize,tmpreg,sref);
  1340. end;
  1341. procedure tcg.a_load_subsetref_loc(list: TAsmlist; subsetsize: tcgsize; const sref: tsubsetreference; const loc: tlocation);
  1342. begin
  1343. case loc.loc of
  1344. LOC_REFERENCE,LOC_CREFERENCE:
  1345. a_load_subsetref_ref(list,subsetsize,loc.size,sref,loc.reference);
  1346. LOC_REGISTER,LOC_CREGISTER:
  1347. a_load_subsetref_reg(list,subsetsize,loc.size,sref,loc.register);
  1348. LOC_SUBSETREG,LOC_CSUBSETREG:
  1349. a_load_subsetref_subsetreg(list,subsetsize,loc.size,sref,loc.sreg);
  1350. LOC_SUBSETREF,LOC_CSUBSETREF:
  1351. a_load_subsetref_subsetref(list,subsetsize,loc.size,sref,loc.sref);
  1352. else
  1353. internalerror(200608054);
  1354. end;
  1355. end;
  1356. procedure tcg.a_load_subsetref_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsref: tsubsetreference; const tosreg: tsubsetregister);
  1357. var
  1358. tmpreg: tregister;
  1359. begin
  1360. tmpreg := getintregister(list,tosubsetsize);
  1361. a_load_subsetref_reg(list,fromsubsetsize,tosubsetsize,fromsref,tmpreg);
  1362. a_load_reg_subsetreg(list,tosubsetsize,tosubsetsize,tmpreg,tosreg);
  1363. end;
  1364. procedure tcg.a_load_subsetreg_subsetref(list: TAsmlist; fromsubsetsize, tosubsetsize : tcgsize; const fromsreg: tsubsetregister; const tosref: tsubsetreference);
  1365. var
  1366. tmpreg: tregister;
  1367. begin
  1368. tmpreg := getintregister(list,tosubsetsize);
  1369. a_load_subsetreg_reg(list,fromsubsetsize,tosubsetsize,fromsreg,tmpreg);
  1370. a_load_reg_subsetref(list,tosubsetsize,tosubsetsize,tmpreg,tosref);
  1371. end;
  1372. {$ifdef rangeon}
  1373. {$r+}
  1374. {$undef rangeon}
  1375. {$endif}
  1376. {$ifdef overflowon}
  1377. {$q+}
  1378. {$undef overflowon}
  1379. {$endif}
  1380. procedure tcg.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  1381. var
  1382. tmpreg: tregister;
  1383. begin
  1384. { verify if we have the same reference }
  1385. if references_equal(sref,dref) then
  1386. exit;
  1387. tmpreg:=getintregister(list,tosize);
  1388. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  1389. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  1390. end;
  1391. procedure tcg.a_load_const_ref(list : TAsmList;size : tcgsize;a : aint;const ref : treference);
  1392. var
  1393. tmpreg: tregister;
  1394. begin
  1395. tmpreg:=getintregister(list,size);
  1396. a_load_const_reg(list,size,a,tmpreg);
  1397. a_load_reg_ref(list,size,size,tmpreg,ref);
  1398. end;
  1399. procedure tcg.a_load_const_loc(list : TAsmList;a : aint;const loc: tlocation);
  1400. begin
  1401. case loc.loc of
  1402. LOC_REFERENCE,LOC_CREFERENCE:
  1403. a_load_const_ref(list,loc.size,a,loc.reference);
  1404. LOC_REGISTER,LOC_CREGISTER:
  1405. a_load_const_reg(list,loc.size,a,loc.register);
  1406. LOC_SUBSETREG,LOC_CSUBSETREG:
  1407. a_load_const_subsetreg(list,loc.size,a,loc.sreg);
  1408. LOC_SUBSETREF,LOC_CSUBSETREF:
  1409. a_load_const_subsetref(list,loc.size,a,loc.sref);
  1410. else
  1411. internalerror(200203272);
  1412. end;
  1413. end;
  1414. procedure tcg.a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  1415. begin
  1416. case loc.loc of
  1417. LOC_REFERENCE,LOC_CREFERENCE:
  1418. a_load_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  1419. LOC_REGISTER,LOC_CREGISTER:
  1420. a_load_reg_reg(list,fromsize,loc.size,reg,loc.register);
  1421. LOC_SUBSETREG,LOC_CSUBSETREG:
  1422. a_load_reg_subsetreg(list,fromsize,loc.size,reg,loc.sreg);
  1423. LOC_SUBSETREF,LOC_CSUBSETREF:
  1424. a_load_reg_subsetref(list,fromsize,loc.size,reg,loc.sref);
  1425. else
  1426. internalerror(200203271);
  1427. end;
  1428. end;
  1429. procedure tcg.a_load_loc_reg(list : TAsmList; tosize: tcgsize; const loc: tlocation; reg : tregister);
  1430. begin
  1431. case loc.loc of
  1432. LOC_REFERENCE,LOC_CREFERENCE:
  1433. a_load_ref_reg(list,loc.size,tosize,loc.reference,reg);
  1434. LOC_REGISTER,LOC_CREGISTER:
  1435. a_load_reg_reg(list,loc.size,tosize,loc.register,reg);
  1436. LOC_CONSTANT:
  1437. a_load_const_reg(list,tosize,loc.value,reg);
  1438. LOC_SUBSETREG,LOC_CSUBSETREG:
  1439. a_load_subsetreg_reg(list,loc.size,tosize,loc.sreg,reg);
  1440. LOC_SUBSETREF,LOC_CSUBSETREF:
  1441. a_load_subsetref_reg(list,loc.size,tosize,loc.sref,reg);
  1442. else
  1443. internalerror(200109092);
  1444. end;
  1445. end;
  1446. procedure tcg.a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  1447. begin
  1448. case loc.loc of
  1449. LOC_REFERENCE,LOC_CREFERENCE:
  1450. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  1451. LOC_REGISTER,LOC_CREGISTER:
  1452. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  1453. LOC_CONSTANT:
  1454. a_load_const_ref(list,tosize,loc.value,ref);
  1455. LOC_SUBSETREG,LOC_CSUBSETREG:
  1456. a_load_subsetreg_ref(list,loc.size,tosize,loc.sreg,ref);
  1457. LOC_SUBSETREF,LOC_CSUBSETREF:
  1458. a_load_subsetref_ref(list,loc.size,tosize,loc.sref,ref);
  1459. else
  1460. internalerror(200109302);
  1461. end;
  1462. end;
  1463. procedure tcg.a_load_loc_subsetreg(list : TAsmList; subsetsize: tcgsize; const loc: tlocation; const sreg : tsubsetregister);
  1464. begin
  1465. case loc.loc of
  1466. LOC_REFERENCE,LOC_CREFERENCE:
  1467. a_load_ref_subsetreg(list,loc.size,subsetsize,loc.reference,sreg);
  1468. LOC_REGISTER,LOC_CREGISTER:
  1469. a_load_reg_subsetreg(list,loc.size,subsetsize,loc.register,sreg);
  1470. LOC_CONSTANT:
  1471. a_load_const_subsetreg(list,subsetsize,loc.value,sreg);
  1472. LOC_SUBSETREG,LOC_CSUBSETREG:
  1473. a_load_subsetreg_subsetreg(list,loc.size,subsetsize,loc.sreg,sreg);
  1474. LOC_SUBSETREF,LOC_CSUBSETREF:
  1475. a_load_subsetref_subsetreg(list,loc.size,subsetsize,loc.sref,sreg);
  1476. else
  1477. internalerror(2006052310);
  1478. end;
  1479. end;
  1480. procedure tcg.a_load_subsetreg_loc(list: TAsmlist; subsetsize: tcgsize; const sreg: tsubsetregister; const loc: tlocation);
  1481. begin
  1482. case loc.loc of
  1483. LOC_REFERENCE,LOC_CREFERENCE:
  1484. a_load_subsetreg_ref(list,subsetsize,loc.size,sreg,loc.reference);
  1485. LOC_REGISTER,LOC_CREGISTER:
  1486. a_load_subsetreg_reg(list,subsetsize,loc.size,sreg,loc.register);
  1487. LOC_SUBSETREG,LOC_CSUBSETREG:
  1488. a_load_subsetreg_subsetreg(list,subsetsize,loc.size,sreg,loc.sreg);
  1489. LOC_SUBSETREF,LOC_CSUBSETREF:
  1490. a_load_subsetreg_subsetref(list,subsetsize,loc.size,sreg,loc.sref);
  1491. else
  1492. internalerror(2006051510);
  1493. end;
  1494. end;
  1495. procedure tcg.optimize_op_const(var op: topcg; var a : aint);
  1496. var
  1497. powerval : longint;
  1498. begin
  1499. case op of
  1500. OP_OR :
  1501. begin
  1502. { or with zero returns same result }
  1503. if a = 0 then
  1504. op:=OP_NONE
  1505. else
  1506. { or with max returns max }
  1507. if a = -1 then
  1508. op:=OP_MOVE;
  1509. end;
  1510. OP_AND :
  1511. begin
  1512. { and with max returns same result }
  1513. if (a = -1) then
  1514. op:=OP_NONE
  1515. else
  1516. { and with 0 returns 0 }
  1517. if a=0 then
  1518. op:=OP_MOVE;
  1519. end;
  1520. OP_DIV :
  1521. begin
  1522. { division by 1 returns result }
  1523. if a = 1 then
  1524. op:=OP_NONE
  1525. else if ispowerof2(int64(a), powerval) and not(cs_check_overflow in aktlocalswitches) then
  1526. begin
  1527. a := powerval;
  1528. op:= OP_SHR;
  1529. end;
  1530. end;
  1531. OP_IDIV:
  1532. begin
  1533. if a = 1 then
  1534. op:=OP_NONE;
  1535. end;
  1536. OP_MUL,OP_IMUL:
  1537. begin
  1538. if a = 1 then
  1539. op:=OP_NONE
  1540. else
  1541. if a=0 then
  1542. op:=OP_MOVE
  1543. else if ispowerof2(int64(a), powerval) and not(cs_check_overflow in aktlocalswitches) then
  1544. begin
  1545. a := powerval;
  1546. op:= OP_SHL;
  1547. end;
  1548. end;
  1549. OP_ADD,OP_SUB:
  1550. begin
  1551. if a = 0 then
  1552. op:=OP_NONE;
  1553. end;
  1554. OP_SAR,OP_SHL,OP_SHR:
  1555. begin
  1556. if a = 0 then
  1557. op:=OP_NONE;
  1558. end;
  1559. end;
  1560. end;
  1561. procedure tcg.a_loadfpu_loc_reg(list: TAsmList; const loc: tlocation; const reg: tregister);
  1562. begin
  1563. case loc.loc of
  1564. LOC_REFERENCE, LOC_CREFERENCE:
  1565. a_loadfpu_ref_reg(list,loc.size,loc.reference,reg);
  1566. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1567. a_loadfpu_reg_reg(list,loc.size,loc.register,reg);
  1568. else
  1569. internalerror(200203301);
  1570. end;
  1571. end;
  1572. procedure tcg.a_loadfpu_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation);
  1573. begin
  1574. case loc.loc of
  1575. LOC_REFERENCE, LOC_CREFERENCE:
  1576. a_loadfpu_reg_ref(list,size,reg,loc.reference);
  1577. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1578. a_loadfpu_reg_reg(list,size,reg,loc.register);
  1579. else
  1580. internalerror(48991);
  1581. end;
  1582. end;
  1583. procedure tcg.a_paramfpu_reg(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);
  1584. var
  1585. ref : treference;
  1586. begin
  1587. case cgpara.location^.loc of
  1588. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1589. begin
  1590. cgpara.check_simple_location;
  1591. a_loadfpu_reg_reg(list,size,r,cgpara.location^.register);
  1592. end;
  1593. LOC_REFERENCE,LOC_CREFERENCE:
  1594. begin
  1595. cgpara.check_simple_location;
  1596. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  1597. a_loadfpu_reg_ref(list,size,r,ref);
  1598. end;
  1599. LOC_REGISTER,LOC_CREGISTER:
  1600. begin
  1601. { paramfpu_ref does the check_simpe_location check here if necessary }
  1602. tg.GetTemp(list,TCGSize2Size[size],tt_normal,ref);
  1603. a_loadfpu_reg_ref(list,size,r,ref);
  1604. a_paramfpu_ref(list,size,ref,cgpara);
  1605. tg.Ungettemp(list,ref);
  1606. end;
  1607. else
  1608. internalerror(2002071004);
  1609. end;
  1610. end;
  1611. procedure tcg.a_paramfpu_ref(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);
  1612. var
  1613. href : treference;
  1614. begin
  1615. cgpara.check_simple_location;
  1616. case cgpara.location^.loc of
  1617. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1618. a_loadfpu_ref_reg(list,size,ref,cgpara.location^.register);
  1619. LOC_REFERENCE,LOC_CREFERENCE:
  1620. begin
  1621. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  1622. { concatcopy should choose the best way to copy the data }
  1623. g_concatcopy(list,ref,href,tcgsize2size[size]);
  1624. end;
  1625. else
  1626. internalerror(200402201);
  1627. end;
  1628. end;
  1629. procedure tcg.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; const ref: TReference);
  1630. var
  1631. tmpreg : tregister;
  1632. begin
  1633. tmpreg:=getintregister(list,size);
  1634. a_load_ref_reg(list,size,size,ref,tmpreg);
  1635. a_op_const_reg(list,op,size,a,tmpreg);
  1636. a_load_reg_ref(list,size,size,tmpreg,ref);
  1637. end;
  1638. procedure tcg.a_op_const_subsetreg(list : TAsmList; Op : TOpCG; size, subsetsize : TCGSize; a : aint; const sreg: tsubsetregister);
  1639. var
  1640. tmpreg: tregister;
  1641. begin
  1642. tmpreg := cg.getintregister(list, size);
  1643. a_load_subsetreg_reg(list,subsetsize,size,sreg,tmpreg);
  1644. a_op_const_reg(list,op,size,a,tmpreg);
  1645. a_load_reg_subsetreg(list,size,subsetsize,tmpreg,sreg);
  1646. end;
  1647. procedure tcg.a_op_const_subsetref(list : TAsmList; Op : TOpCG; size, subsetsize : TCGSize; a : aint; const sref: tsubsetreference);
  1648. var
  1649. tmpreg: tregister;
  1650. begin
  1651. tmpreg := cg.getintregister(list, size);
  1652. a_load_subsetref_reg(list,subsetsize,size,sref,tmpreg);
  1653. a_op_const_reg(list,op,size,a,tmpreg);
  1654. a_load_reg_subsetref(list,size,subsetsize,tmpreg,sref);
  1655. end;
  1656. procedure tcg.a_op_const_loc(list : TAsmList; Op: TOpCG; a: aint; const loc: tlocation);
  1657. begin
  1658. case loc.loc of
  1659. LOC_REGISTER, LOC_CREGISTER:
  1660. a_op_const_reg(list,op,loc.size,a,loc.register);
  1661. LOC_REFERENCE, LOC_CREFERENCE:
  1662. a_op_const_ref(list,op,loc.size,a,loc.reference);
  1663. LOC_SUBSETREG, LOC_CSUBSETREG:
  1664. a_op_const_subsetreg(list,op,loc.size,loc.size,a,loc.sreg);
  1665. LOC_SUBSETREF, LOC_CSUBSETREF:
  1666. a_op_const_subsetref(list,op,loc.size,loc.size,a,loc.sref);
  1667. else
  1668. internalerror(200109061);
  1669. end;
  1670. end;
  1671. procedure tcg.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1672. var
  1673. tmpreg : tregister;
  1674. begin
  1675. tmpreg:=getintregister(list,size);
  1676. a_load_ref_reg(list,size,size,ref,tmpreg);
  1677. a_op_reg_reg(list,op,size,reg,tmpreg);
  1678. a_load_reg_ref(list,size,size,tmpreg,ref);
  1679. end;
  1680. procedure tcg.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1681. var
  1682. tmpreg: tregister;
  1683. begin
  1684. case op of
  1685. OP_NOT,OP_NEG:
  1686. { handle it as "load ref,reg; op reg" }
  1687. begin
  1688. a_load_ref_reg(list,size,size,ref,reg);
  1689. a_op_reg_reg(list,op,size,reg,reg);
  1690. end;
  1691. else
  1692. begin
  1693. tmpreg:=getintregister(list,size);
  1694. a_load_ref_reg(list,size,size,ref,tmpreg);
  1695. a_op_reg_reg(list,op,size,tmpreg,reg);
  1696. end;
  1697. end;
  1698. end;
  1699. procedure tcg.a_op_reg_subsetreg(list : TAsmList; Op : TOpCG; opsize, subsetsize : TCGSize; reg: TRegister; const sreg: tsubsetregister);
  1700. var
  1701. tmpreg: tregister;
  1702. begin
  1703. tmpreg := cg.getintregister(list, opsize);
  1704. a_load_subsetreg_reg(list,subsetsize,opsize,sreg,tmpreg);
  1705. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  1706. a_load_reg_subsetreg(list,opsize,subsetsize,tmpreg,sreg);
  1707. end;
  1708. procedure tcg.a_op_reg_subsetref(list : TAsmList; Op : TOpCG; opsize, subsetsize : TCGSize; reg: TRegister; const sref: tsubsetreference);
  1709. var
  1710. tmpreg: tregister;
  1711. begin
  1712. tmpreg := cg.getintregister(list, opsize);
  1713. a_load_subsetref_reg(list,subsetsize,opsize,sref,tmpreg);
  1714. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  1715. a_load_reg_subsetref(list,opsize,subsetsize,tmpreg,sref);
  1716. end;
  1717. procedure tcg.a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  1718. begin
  1719. case loc.loc of
  1720. LOC_REGISTER, LOC_CREGISTER:
  1721. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  1722. LOC_REFERENCE, LOC_CREFERENCE:
  1723. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  1724. LOC_SUBSETREG, LOC_CSUBSETREG:
  1725. a_op_reg_subsetreg(list,op,loc.size,loc.size,reg,loc.sreg);
  1726. LOC_SUBSETREF, LOC_CSUBSETREF:
  1727. a_op_reg_subsetref(list,op,loc.size,loc.size,reg,loc.sref);
  1728. else
  1729. internalerror(200109061);
  1730. end;
  1731. end;
  1732. procedure tcg.a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  1733. var
  1734. tmpreg: tregister;
  1735. begin
  1736. case loc.loc of
  1737. LOC_REGISTER,LOC_CREGISTER:
  1738. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  1739. LOC_REFERENCE,LOC_CREFERENCE:
  1740. begin
  1741. tmpreg:=getintregister(list,loc.size);
  1742. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  1743. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  1744. end;
  1745. LOC_SUBSETREG, LOC_CSUBSETREG:
  1746. begin
  1747. tmpreg:=getintregister(list,loc.size);
  1748. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  1749. a_op_ref_reg(list,op,loc.size,ref,tmpreg);
  1750. a_load_reg_subsetreg(list,loc.size,loc.size,tmpreg,loc.sreg);
  1751. end;
  1752. LOC_SUBSETREF, LOC_CSUBSETREF:
  1753. begin
  1754. tmpreg:=getintregister(list,loc.size);
  1755. a_load_subsetreF_reg(list,loc.size,loc.size,loc.sref,tmpreg);
  1756. a_op_ref_reg(list,op,loc.size,ref,tmpreg);
  1757. a_load_reg_subsetref(list,loc.size,loc.size,tmpreg,loc.sref);
  1758. end;
  1759. else
  1760. internalerror(200109061);
  1761. end;
  1762. end;
  1763. procedure Tcg.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1764. a:aint;src,dst:Tregister);
  1765. begin
  1766. a_load_reg_reg(list,size,size,src,dst);
  1767. a_op_const_reg(list,op,size,a,dst);
  1768. end;
  1769. procedure tcg.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1770. size: tcgsize; src1, src2, dst: tregister);
  1771. var
  1772. tmpreg: tregister;
  1773. begin
  1774. if (dst<>src1) then
  1775. begin
  1776. a_load_reg_reg(list,size,size,src2,dst);
  1777. a_op_reg_reg(list,op,size,src1,dst);
  1778. end
  1779. else
  1780. begin
  1781. tmpreg:=getintregister(list,size);
  1782. a_load_reg_reg(list,size,size,src2,tmpreg);
  1783. a_op_reg_reg(list,op,size,src1,tmpreg);
  1784. a_load_reg_reg(list,size,size,tmpreg,dst);
  1785. end;
  1786. end;
  1787. procedure tcg.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1788. begin
  1789. a_op_const_reg_reg(list,op,size,a,src,dst);
  1790. ovloc.loc:=LOC_VOID;
  1791. end;
  1792. procedure tcg.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1793. begin
  1794. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1795. ovloc.loc:=LOC_VOID;
  1796. end;
  1797. procedure tcg.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  1798. l : tasmlabel);
  1799. var
  1800. tmpreg: tregister;
  1801. begin
  1802. tmpreg:=getintregister(list,size);
  1803. a_load_ref_reg(list,size,size,ref,tmpreg);
  1804. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1805. end;
  1806. procedure tcg.a_cmp_const_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;const loc : tlocation;
  1807. l : tasmlabel);
  1808. var
  1809. tmpreg : tregister;
  1810. begin
  1811. case loc.loc of
  1812. LOC_REGISTER,LOC_CREGISTER:
  1813. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1814. LOC_REFERENCE,LOC_CREFERENCE:
  1815. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1816. LOC_SUBSETREG, LOC_CSUBSETREG:
  1817. begin
  1818. tmpreg:=getintregister(list,size);
  1819. a_load_subsetreg_reg(list,loc.size,size,loc.sreg,tmpreg);
  1820. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1821. end;
  1822. LOC_SUBSETREF, LOC_CSUBSETREF:
  1823. begin
  1824. tmpreg:=getintregister(list,size);
  1825. a_load_subsetref_reg(list,loc.size,size,loc.sref,tmpreg);
  1826. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1827. end;
  1828. else
  1829. internalerror(200109061);
  1830. end;
  1831. end;
  1832. procedure tcg.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  1833. var
  1834. tmpreg: tregister;
  1835. begin
  1836. tmpreg:=getintregister(list,size);
  1837. a_load_ref_reg(list,size,size,ref,tmpreg);
  1838. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1839. end;
  1840. procedure tcg.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg : tregister; const ref: treference; l : tasmlabel);
  1841. var
  1842. tmpreg: tregister;
  1843. begin
  1844. tmpreg:=getintregister(list,size);
  1845. a_load_ref_reg(list,size,size,ref,tmpreg);
  1846. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  1847. end;
  1848. procedure tcg.a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  1849. begin
  1850. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  1851. end;
  1852. procedure tcg.a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  1853. begin
  1854. case loc.loc of
  1855. LOC_REGISTER,
  1856. LOC_CREGISTER:
  1857. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1858. LOC_REFERENCE,
  1859. LOC_CREFERENCE :
  1860. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1861. LOC_CONSTANT:
  1862. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1863. LOC_SUBSETREG,
  1864. LOC_CSUBSETREG:
  1865. a_cmp_subsetreg_reg_label(list,loc.size,size,cmp_op,loc.sreg,reg,l);
  1866. LOC_SUBSETREF,
  1867. LOC_CSUBSETREF:
  1868. a_cmp_subsetref_reg_label(list,loc.size,size,cmp_op,loc.sref,reg,l);
  1869. else
  1870. internalerror(200203231);
  1871. end;
  1872. end;
  1873. procedure tcg.a_cmp_subsetreg_reg_label(list : TAsmList; subsetsize : tcgsize; cmpsize : tcgsize; cmp_op : topcmp; const sreg: tsubsetregister; reg : tregister; l : tasmlabel);
  1874. var
  1875. tmpreg: tregister;
  1876. begin
  1877. tmpreg:=getintregister(list, cmpsize);
  1878. a_load_subsetreg_reg(list,subsetsize,cmpsize,sreg,tmpreg);
  1879. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  1880. end;
  1881. procedure tcg.a_cmp_subsetref_reg_label(list : TAsmList; subsetsize : tcgsize; cmpsize : tcgsize; cmp_op : topcmp; const sref: tsubsetreference; reg : tregister; l : tasmlabel);
  1882. var
  1883. tmpreg: tregister;
  1884. begin
  1885. tmpreg:=getintregister(list, cmpsize);
  1886. a_load_subsetref_reg(list,subsetsize,cmpsize,sref,tmpreg);
  1887. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  1888. end;
  1889. procedure tcg.a_cmp_ref_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1890. l : tasmlabel);
  1891. var
  1892. tmpreg: tregister;
  1893. begin
  1894. case loc.loc of
  1895. LOC_REGISTER,LOC_CREGISTER:
  1896. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1897. LOC_REFERENCE,LOC_CREFERENCE:
  1898. begin
  1899. tmpreg:=getintregister(list,size);
  1900. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1901. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1902. end;
  1903. LOC_SUBSETREG, LOC_CSUBSETREG:
  1904. begin
  1905. tmpreg:=getintregister(list, size);
  1906. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1907. a_cmp_subsetreg_reg_label(list,loc.size,size,swap_opcmp(cmp_op),loc.sreg,tmpreg,l);
  1908. end;
  1909. LOC_SUBSETREF, LOC_CSUBSETREF:
  1910. begin
  1911. tmpreg:=getintregister(list, size);
  1912. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1913. a_cmp_subsetref_reg_label(list,loc.size,size,swap_opcmp(cmp_op),loc.sref,tmpreg,l);
  1914. end;
  1915. else
  1916. internalerror(200109061);
  1917. end;
  1918. end;
  1919. procedure tcg.a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  1920. begin
  1921. case loc.loc of
  1922. LOC_MMREGISTER,LOC_CMMREGISTER:
  1923. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1924. LOC_REFERENCE,LOC_CREFERENCE:
  1925. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  1926. else
  1927. internalerror(200310121);
  1928. end;
  1929. end;
  1930. procedure tcg.a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  1931. begin
  1932. case loc.loc of
  1933. LOC_MMREGISTER,LOC_CMMREGISTER:
  1934. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  1935. LOC_REFERENCE,LOC_CREFERENCE:
  1936. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  1937. else
  1938. internalerror(200310122);
  1939. end;
  1940. end;
  1941. procedure tcg.a_parammm_reg(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle);
  1942. var
  1943. href : treference;
  1944. begin
  1945. cgpara.check_simple_location;
  1946. case cgpara.location^.loc of
  1947. LOC_MMREGISTER,LOC_CMMREGISTER:
  1948. a_loadmm_reg_reg(list,size,cgpara.location^.size,reg,cgpara.location^.register,shuffle);
  1949. LOC_REFERENCE,LOC_CREFERENCE:
  1950. begin
  1951. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  1952. a_loadmm_reg_ref(list,size,cgpara.location^.size,reg,href,shuffle);
  1953. end
  1954. else
  1955. internalerror(200310123);
  1956. end;
  1957. end;
  1958. procedure tcg.a_parammm_ref(list: TAsmList; size: tcgsize;const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle);
  1959. var
  1960. hr : tregister;
  1961. hs : tmmshuffle;
  1962. begin
  1963. cgpara.check_simple_location;
  1964. hr:=getmmregister(list,cgpara.location^.size);
  1965. a_loadmm_ref_reg(list,size,cgpara.location^.size,ref,hr,shuffle);
  1966. if realshuffle(shuffle) then
  1967. begin
  1968. hs:=shuffle^;
  1969. removeshuffles(hs);
  1970. a_parammm_reg(list,cgpara.location^.size,hr,cgpara,@hs);
  1971. end
  1972. else
  1973. a_parammm_reg(list,cgpara.location^.size,hr,cgpara,shuffle);
  1974. end;
  1975. procedure tcg.a_parammm_loc(list: TAsmList;const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle);
  1976. begin
  1977. case loc.loc of
  1978. LOC_MMREGISTER,LOC_CMMREGISTER:
  1979. a_parammm_reg(list,loc.size,loc.register,cgpara,shuffle);
  1980. LOC_REFERENCE,LOC_CREFERENCE:
  1981. a_parammm_ref(list,loc.size,loc.reference,cgpara,shuffle);
  1982. else
  1983. internalerror(200310123);
  1984. end;
  1985. end;
  1986. procedure tcg.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1987. var
  1988. hr : tregister;
  1989. hs : tmmshuffle;
  1990. begin
  1991. hr:=getmmregister(list,size);
  1992. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1993. if realshuffle(shuffle) then
  1994. begin
  1995. hs:=shuffle^;
  1996. removeshuffles(hs);
  1997. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  1998. end
  1999. else
  2000. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  2001. end;
  2002. procedure tcg.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  2003. var
  2004. hr : tregister;
  2005. hs : tmmshuffle;
  2006. begin
  2007. hr:=getmmregister(list,size);
  2008. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  2009. if realshuffle(shuffle) then
  2010. begin
  2011. hs:=shuffle^;
  2012. removeshuffles(hs);
  2013. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  2014. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  2015. end
  2016. else
  2017. begin
  2018. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  2019. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  2020. end;
  2021. end;
  2022. procedure tcg.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  2023. begin
  2024. case loc.loc of
  2025. LOC_CMMREGISTER,LOC_MMREGISTER:
  2026. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  2027. LOC_CREFERENCE,LOC_REFERENCE:
  2028. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  2029. else
  2030. internalerror(200312232);
  2031. end;
  2032. end;
  2033. procedure tcg.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : aint);
  2034. begin
  2035. g_concatcopy(list,source,dest,len);
  2036. end;
  2037. procedure tcg.g_copyshortstring(list : TAsmList;const source,dest : treference;len:byte);
  2038. var
  2039. cgpara1,cgpara2,cgpara3 : TCGPara;
  2040. begin
  2041. cgpara1.init;
  2042. cgpara2.init;
  2043. cgpara3.init;
  2044. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2045. paramanager.getintparaloc(pocall_default,2,cgpara2);
  2046. paramanager.getintparaloc(pocall_default,3,cgpara3);
  2047. paramanager.allocparaloc(list,cgpara3);
  2048. a_paramaddr_ref(list,dest,cgpara3);
  2049. paramanager.allocparaloc(list,cgpara2);
  2050. a_paramaddr_ref(list,source,cgpara2);
  2051. paramanager.allocparaloc(list,cgpara1);
  2052. a_param_const(list,OS_INT,len,cgpara1);
  2053. paramanager.freeparaloc(list,cgpara3);
  2054. paramanager.freeparaloc(list,cgpara2);
  2055. paramanager.freeparaloc(list,cgpara1);
  2056. allocallcpuregisters(list);
  2057. a_call_name(list,'FPC_SHORTSTR_ASSIGN');
  2058. deallocallcpuregisters(list);
  2059. cgpara3.done;
  2060. cgpara2.done;
  2061. cgpara1.done;
  2062. end;
  2063. procedure tcg.g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);
  2064. var
  2065. href : treference;
  2066. incrfunc : string;
  2067. cgpara1,cgpara2 : TCGPara;
  2068. begin
  2069. cgpara1.init;
  2070. cgpara2.init;
  2071. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2072. paramanager.getintparaloc(pocall_default,2,cgpara2);
  2073. if is_interfacecom(t) then
  2074. incrfunc:='FPC_INTF_INCR_REF'
  2075. else if is_ansistring(t) then
  2076. incrfunc:='FPC_ANSISTR_INCR_REF'
  2077. else if is_widestring(t) then
  2078. incrfunc:='FPC_WIDESTR_INCR_REF'
  2079. else if is_dynamic_array(t) then
  2080. incrfunc:='FPC_DYNARRAY_INCR_REF'
  2081. else
  2082. incrfunc:='';
  2083. { call the special incr function or the generic addref }
  2084. if incrfunc<>'' then
  2085. begin
  2086. paramanager.allocparaloc(list,cgpara1);
  2087. { widestrings aren't ref. counted on all platforms so we need the address
  2088. to create a real copy }
  2089. if is_widestring(t) then
  2090. a_paramaddr_ref(list,ref,cgpara1)
  2091. else
  2092. { these functions get the pointer by value }
  2093. a_param_ref(list,OS_ADDR,ref,cgpara1);
  2094. paramanager.freeparaloc(list,cgpara1);
  2095. allocallcpuregisters(list);
  2096. a_call_name(list,incrfunc);
  2097. deallocallcpuregisters(list);
  2098. end
  2099. else
  2100. begin
  2101. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  2102. paramanager.allocparaloc(list,cgpara2);
  2103. a_paramaddr_ref(list,href,cgpara2);
  2104. paramanager.allocparaloc(list,cgpara1);
  2105. a_paramaddr_ref(list,ref,cgpara1);
  2106. paramanager.freeparaloc(list,cgpara1);
  2107. paramanager.freeparaloc(list,cgpara2);
  2108. allocallcpuregisters(list);
  2109. a_call_name(list,'FPC_ADDREF');
  2110. deallocallcpuregisters(list);
  2111. end;
  2112. cgpara2.done;
  2113. cgpara1.done;
  2114. end;
  2115. procedure tcg.g_decrrefcount(list : TAsmList;t: tdef; const ref: treference);
  2116. var
  2117. href : treference;
  2118. decrfunc : string;
  2119. needrtti : boolean;
  2120. cgpara1,cgpara2 : TCGPara;
  2121. tempreg1,tempreg2 : TRegister;
  2122. begin
  2123. cgpara1.init;
  2124. cgpara2.init;
  2125. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2126. paramanager.getintparaloc(pocall_default,2,cgpara2);
  2127. needrtti:=false;
  2128. if is_interfacecom(t) then
  2129. decrfunc:='FPC_INTF_DECR_REF'
  2130. else if is_ansistring(t) then
  2131. decrfunc:='FPC_ANSISTR_DECR_REF'
  2132. else if is_widestring(t) then
  2133. decrfunc:='FPC_WIDESTR_DECR_REF'
  2134. else if is_dynamic_array(t) then
  2135. begin
  2136. decrfunc:='FPC_DYNARRAY_DECR_REF';
  2137. needrtti:=true;
  2138. end
  2139. else
  2140. decrfunc:='';
  2141. { call the special decr function or the generic decref }
  2142. if decrfunc<>'' then
  2143. begin
  2144. if needrtti then
  2145. begin
  2146. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  2147. tempreg2:=getaddressregister(list);
  2148. a_loadaddr_ref_reg(list,href,tempreg2);
  2149. end;
  2150. tempreg1:=getaddressregister(list);
  2151. a_loadaddr_ref_reg(list,ref,tempreg1);
  2152. if needrtti then
  2153. begin
  2154. paramanager.allocparaloc(list,cgpara2);
  2155. a_param_reg(list,OS_ADDR,tempreg2,cgpara2);
  2156. paramanager.freeparaloc(list,cgpara2);
  2157. end;
  2158. paramanager.allocparaloc(list,cgpara1);
  2159. a_param_reg(list,OS_ADDR,tempreg1,cgpara1);
  2160. paramanager.freeparaloc(list,cgpara1);
  2161. allocallcpuregisters(list);
  2162. a_call_name(list,decrfunc);
  2163. deallocallcpuregisters(list);
  2164. end
  2165. else
  2166. begin
  2167. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  2168. paramanager.allocparaloc(list,cgpara2);
  2169. a_paramaddr_ref(list,href,cgpara2);
  2170. paramanager.allocparaloc(list,cgpara1);
  2171. a_paramaddr_ref(list,ref,cgpara1);
  2172. paramanager.freeparaloc(list,cgpara1);
  2173. paramanager.freeparaloc(list,cgpara2);
  2174. allocallcpuregisters(list);
  2175. a_call_name(list,'FPC_DECREF');
  2176. deallocallcpuregisters(list);
  2177. end;
  2178. cgpara2.done;
  2179. cgpara1.done;
  2180. end;
  2181. procedure tcg.g_initialize(list : TAsmList;t : tdef;const ref : treference);
  2182. var
  2183. href : treference;
  2184. cgpara1,cgpara2 : TCGPara;
  2185. begin
  2186. cgpara1.init;
  2187. cgpara2.init;
  2188. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2189. paramanager.getintparaloc(pocall_default,2,cgpara2);
  2190. if is_ansistring(t) or
  2191. is_widestring(t) or
  2192. is_interfacecom(t) or
  2193. is_dynamic_array(t) then
  2194. a_load_const_ref(list,OS_ADDR,0,ref)
  2195. else
  2196. begin
  2197. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  2198. paramanager.allocparaloc(list,cgpara2);
  2199. a_paramaddr_ref(list,href,cgpara2);
  2200. paramanager.allocparaloc(list,cgpara1);
  2201. a_paramaddr_ref(list,ref,cgpara1);
  2202. paramanager.freeparaloc(list,cgpara1);
  2203. paramanager.freeparaloc(list,cgpara2);
  2204. allocallcpuregisters(list);
  2205. a_call_name(list,'FPC_INITIALIZE');
  2206. deallocallcpuregisters(list);
  2207. end;
  2208. cgpara1.done;
  2209. cgpara2.done;
  2210. end;
  2211. procedure tcg.g_finalize(list : TAsmList;t : tdef;const ref : treference);
  2212. var
  2213. href : treference;
  2214. cgpara1,cgpara2 : TCGPara;
  2215. begin
  2216. cgpara1.init;
  2217. cgpara2.init;
  2218. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2219. paramanager.getintparaloc(pocall_default,2,cgpara2);
  2220. if is_ansistring(t) or
  2221. is_widestring(t) or
  2222. is_interfacecom(t) then
  2223. begin
  2224. g_decrrefcount(list,t,ref);
  2225. a_load_const_ref(list,OS_ADDR,0,ref);
  2226. end
  2227. else
  2228. begin
  2229. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  2230. paramanager.allocparaloc(list,cgpara2);
  2231. a_paramaddr_ref(list,href,cgpara2);
  2232. paramanager.allocparaloc(list,cgpara1);
  2233. a_paramaddr_ref(list,ref,cgpara1);
  2234. paramanager.freeparaloc(list,cgpara1);
  2235. paramanager.freeparaloc(list,cgpara2);
  2236. allocallcpuregisters(list);
  2237. a_call_name(list,'FPC_FINALIZE');
  2238. deallocallcpuregisters(list);
  2239. end;
  2240. cgpara1.done;
  2241. cgpara2.done;
  2242. end;
  2243. procedure tcg.g_rangecheck(list: TAsmList; const l:tlocation;fromdef,todef: tdef);
  2244. { generate range checking code for the value at location p. The type }
  2245. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  2246. { is the original type used at that location. When both defs are equal }
  2247. { the check is also insert (needed for succ,pref,inc,dec) }
  2248. const
  2249. aintmax=high(aint);
  2250. var
  2251. neglabel : tasmlabel;
  2252. hreg : tregister;
  2253. lto,hto,
  2254. lfrom,hfrom : TConstExprInt;
  2255. fromsize, tosize: cardinal;
  2256. from_signed, to_signed: boolean;
  2257. begin
  2258. { range checking on and range checkable value? }
  2259. if not(cs_check_range in aktlocalswitches) or
  2260. not(fromdef.deftype in [orddef,enumdef]) then
  2261. exit;
  2262. {$ifndef cpu64bit}
  2263. { handle 64bit rangechecks separate for 32bit processors }
  2264. if is_64bit(fromdef) or is_64bit(todef) then
  2265. begin
  2266. cg64.g_rangecheck64(list,l,fromdef,todef);
  2267. exit;
  2268. end;
  2269. {$endif cpu64bit}
  2270. { only check when assigning to scalar, subranges are different, }
  2271. { when todef=fromdef then the check is always generated }
  2272. getrange(fromdef,lfrom,hfrom);
  2273. getrange(todef,lto,hto);
  2274. from_signed := is_signed(fromdef);
  2275. to_signed := is_signed(todef);
  2276. { check the rangetype of the array, not the array itself }
  2277. { (only change now, since getrange needs the arraydef) }
  2278. if (todef.deftype = arraydef) then
  2279. todef := tarraydef(todef).rangetype.def;
  2280. { no range check if from and to are equal and are both longint/dword }
  2281. { no range check if from and to are equal and are both longint/dword }
  2282. { (if we have a 32bit processor) or int64/qword, since such }
  2283. { operations can at most cause overflows (JM) }
  2284. { Note that these checks are mostly processor independent, they only }
  2285. { have to be changed once we introduce 64bit subrange types }
  2286. {$ifdef cpu64bit}
  2287. if (fromdef = todef) and
  2288. (fromdef.deftype=orddef) and
  2289. (((((torddef(fromdef).typ = s64bit) and
  2290. (lfrom = low(int64)) and
  2291. (hfrom = high(int64))) or
  2292. ((torddef(fromdef).typ = u64bit) and
  2293. (lfrom = low(qword)) and
  2294. (hfrom = high(qword))) or
  2295. ((torddef(fromdef).typ = scurrency) and
  2296. (lfrom = low(int64)) and
  2297. (hfrom = high(int64)))))) then
  2298. exit;
  2299. {$else cpu64bit}
  2300. if (fromdef = todef) and
  2301. (fromdef.deftype=orddef) and
  2302. (((((torddef(fromdef).typ = s32bit) and
  2303. (lfrom = low(longint)) and
  2304. (hfrom = high(longint))) or
  2305. ((torddef(fromdef).typ = u32bit) and
  2306. (lfrom = low(cardinal)) and
  2307. (hfrom = high(cardinal)))))) then
  2308. exit;
  2309. {$endif cpu64bit}
  2310. { optimize some range checks away in safe cases }
  2311. fromsize := fromdef.size;
  2312. tosize := todef.size;
  2313. if ((from_signed = to_signed) or
  2314. (not from_signed)) and
  2315. (lto<=lfrom) and (hto>=hfrom) and
  2316. (fromsize <= tosize) then
  2317. begin
  2318. { if fromsize < tosize, and both have the same signed-ness or }
  2319. { fromdef is unsigned, then all bit patterns from fromdef are }
  2320. { valid for todef as well }
  2321. if (fromsize < tosize) then
  2322. exit;
  2323. if (fromsize = tosize) and
  2324. (from_signed = to_signed) then
  2325. { only optimize away if all bit patterns which fit in fromsize }
  2326. { are valid for the todef }
  2327. begin
  2328. {$ifopt Q+}
  2329. {$define overflowon}
  2330. {$Q-}
  2331. {$endif}
  2332. if to_signed then
  2333. begin
  2334. { calculation of the low/high ranges must not overflow 64 bit
  2335. otherwise we end up comparing with zero for 64 bit data types on
  2336. 64 bit processors }
  2337. if (lto = (int64(-1) << (tosize * 8 - 1))) and
  2338. (hto = (-((int64(-1) << (tosize * 8 - 1))+1))) then
  2339. exit
  2340. end
  2341. else
  2342. begin
  2343. { calculation of the low/high ranges must not overflow 64 bit
  2344. otherwise we end up having all zeros for 64 bit data types on
  2345. 64 bit processors }
  2346. if (lto = 0) and
  2347. (qword(hto) = (qword(-1) >> (64-(tosize * 8))) ) then
  2348. exit
  2349. end;
  2350. {$ifdef overflowon}
  2351. {$Q+}
  2352. {$undef overflowon}
  2353. {$endif}
  2354. end
  2355. end;
  2356. { generate the rangecheck code for the def where we are going to }
  2357. { store the result }
  2358. { use the trick that }
  2359. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  2360. { To be able to do that, we have to make sure however that either }
  2361. { fromdef and todef are both signed or unsigned, or that we leave }
  2362. { the parts < 0 and > maxlongint out }
  2363. if from_signed xor to_signed then
  2364. begin
  2365. if from_signed then
  2366. { from is signed, to is unsigned }
  2367. begin
  2368. { if high(from) < 0 -> always range error }
  2369. if (hfrom < 0) or
  2370. { if low(to) > maxlongint also range error }
  2371. (lto > aintmax) then
  2372. begin
  2373. a_call_name(list,'FPC_RANGEERROR');
  2374. exit
  2375. end;
  2376. { from is signed and to is unsigned -> when looking at to }
  2377. { as an signed value, it must be < maxaint (otherwise }
  2378. { it will become negative, which is invalid since "to" is unsigned) }
  2379. if hto > aintmax then
  2380. hto := aintmax;
  2381. end
  2382. else
  2383. { from is unsigned, to is signed }
  2384. begin
  2385. if (lfrom > aintmax) or
  2386. (hto < 0) then
  2387. begin
  2388. a_call_name(list,'FPC_RANGEERROR');
  2389. exit
  2390. end;
  2391. { from is unsigned and to is signed -> when looking at to }
  2392. { as an unsigned value, it must be >= 0 (since negative }
  2393. { values are the same as values > maxlongint) }
  2394. if lto < 0 then
  2395. lto := 0;
  2396. end;
  2397. end;
  2398. hreg:=getintregister(list,OS_INT);
  2399. a_load_loc_reg(list,OS_INT,l,hreg);
  2400. a_op_const_reg(list,OP_SUB,OS_INT,aint(lto),hreg);
  2401. current_asmdata.getjumplabel(neglabel);
  2402. {
  2403. if from_signed then
  2404. a_cmp_const_reg_label(list,OS_INT,OC_GTE,aint(hto-lto),hreg,neglabel)
  2405. else
  2406. }
  2407. {$ifdef cpu64bit}
  2408. if qword(hto-lto)>qword(aintmax) then
  2409. a_cmp_const_reg_label(list,OS_INT,OC_BE,aintmax,hreg,neglabel)
  2410. else
  2411. {$endif cpu64bit}
  2412. a_cmp_const_reg_label(list,OS_INT,OC_BE,aint(hto-lto),hreg,neglabel);
  2413. a_call_name(list,'FPC_RANGEERROR');
  2414. a_label(list,neglabel);
  2415. end;
  2416. procedure tcg.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  2417. begin
  2418. g_overflowCheck(list,loc,def);
  2419. end;
  2420. procedure tcg.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference);
  2421. var
  2422. tmpreg : tregister;
  2423. begin
  2424. tmpreg:=getintregister(list,size);
  2425. g_flags2reg(list,size,f,tmpreg);
  2426. a_load_reg_ref(list,size,size,tmpreg,ref);
  2427. end;
  2428. procedure tcg.g_maybe_testself(list : TAsmList;reg:tregister);
  2429. var
  2430. OKLabel : tasmlabel;
  2431. cgpara1 : TCGPara;
  2432. begin
  2433. if (cs_check_object in aktlocalswitches) or
  2434. (cs_check_range in aktlocalswitches) then
  2435. begin
  2436. current_asmdata.getjumplabel(oklabel);
  2437. a_cmp_const_reg_label(list,OS_ADDR,OC_NE,0,reg,oklabel);
  2438. cgpara1.init;
  2439. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2440. paramanager.allocparaloc(list,cgpara1);
  2441. a_param_const(list,OS_INT,210,cgpara1);
  2442. paramanager.freeparaloc(list,cgpara1);
  2443. a_call_name(list,'FPC_HANDLEERROR');
  2444. a_label(list,oklabel);
  2445. cgpara1.done;
  2446. end;
  2447. end;
  2448. procedure tcg.g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  2449. var
  2450. hrefvmt : treference;
  2451. cgpara1,cgpara2 : TCGPara;
  2452. begin
  2453. cgpara1.init;
  2454. cgpara2.init;
  2455. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2456. paramanager.getintparaloc(pocall_default,2,cgpara2);
  2457. if (cs_check_object in aktlocalswitches) then
  2458. begin
  2459. reference_reset_symbol(hrefvmt,current_asmdata.RefAsmSymbol(objdef.vmt_mangledname),0);
  2460. paramanager.allocparaloc(list,cgpara2);
  2461. a_paramaddr_ref(list,hrefvmt,cgpara2);
  2462. paramanager.allocparaloc(list,cgpara1);
  2463. a_param_reg(list,OS_ADDR,reg,cgpara1);
  2464. paramanager.freeparaloc(list,cgpara1);
  2465. paramanager.freeparaloc(list,cgpara2);
  2466. allocallcpuregisters(list);
  2467. a_call_name(list,'FPC_CHECK_OBJECT_EXT');
  2468. deallocallcpuregisters(list);
  2469. end
  2470. else
  2471. if (cs_check_range in aktlocalswitches) then
  2472. begin
  2473. paramanager.allocparaloc(list,cgpara1);
  2474. a_param_reg(list,OS_ADDR,reg,cgpara1);
  2475. paramanager.freeparaloc(list,cgpara1);
  2476. allocallcpuregisters(list);
  2477. a_call_name(list,'FPC_CHECK_OBJECT');
  2478. deallocallcpuregisters(list);
  2479. end;
  2480. cgpara1.done;
  2481. cgpara2.done;
  2482. end;
  2483. {*****************************************************************************
  2484. Entry/Exit Code Functions
  2485. *****************************************************************************}
  2486. procedure tcg.g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;elesize:aint;destreg:tregister);
  2487. var
  2488. sizereg,sourcereg,lenreg : tregister;
  2489. cgpara1,cgpara2,cgpara3 : TCGPara;
  2490. begin
  2491. { because some abis don't support dynamic stack allocation properly
  2492. open array value parameters are copied onto the heap
  2493. }
  2494. { calculate necessary memory }
  2495. { read/write operations on one register make the life of the register allocator hard }
  2496. if not(lenloc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  2497. begin
  2498. lenreg:=getintregister(list,OS_INT);
  2499. a_load_loc_reg(list,OS_INT,lenloc,lenreg);
  2500. end
  2501. else
  2502. lenreg:=lenloc.register;
  2503. sizereg:=getintregister(list,OS_INT);
  2504. a_op_const_reg_reg(list,OP_ADD,OS_INT,1,lenreg,sizereg);
  2505. a_op_const_reg(list,OP_IMUL,OS_INT,elesize,sizereg);
  2506. { load source }
  2507. sourcereg:=getaddressregister(list);
  2508. a_loadaddr_ref_reg(list,ref,sourcereg);
  2509. { do getmem call }
  2510. cgpara1.init;
  2511. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2512. paramanager.allocparaloc(list,cgpara1);
  2513. a_param_reg(list,OS_INT,sizereg,cgpara1);
  2514. paramanager.freeparaloc(list,cgpara1);
  2515. allocallcpuregisters(list);
  2516. a_call_name(list,'FPC_GETMEM');
  2517. deallocallcpuregisters(list);
  2518. cgpara1.done;
  2519. { return the new address }
  2520. a_load_reg_reg(list,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,destreg);
  2521. { do move call }
  2522. cgpara1.init;
  2523. cgpara2.init;
  2524. cgpara3.init;
  2525. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2526. paramanager.getintparaloc(pocall_default,2,cgpara2);
  2527. paramanager.getintparaloc(pocall_default,3,cgpara3);
  2528. { load size }
  2529. paramanager.allocparaloc(list,cgpara3);
  2530. a_param_reg(list,OS_INT,sizereg,cgpara3);
  2531. { load destination }
  2532. paramanager.allocparaloc(list,cgpara2);
  2533. a_param_reg(list,OS_ADDR,destreg,cgpara2);
  2534. { load source }
  2535. paramanager.allocparaloc(list,cgpara1);
  2536. a_param_reg(list,OS_ADDR,sourcereg,cgpara1);
  2537. paramanager.freeparaloc(list,cgpara3);
  2538. paramanager.freeparaloc(list,cgpara2);
  2539. paramanager.freeparaloc(list,cgpara1);
  2540. allocallcpuregisters(list);
  2541. a_call_name(list,'FPC_MOVE');
  2542. deallocallcpuregisters(list);
  2543. cgpara3.done;
  2544. cgpara2.done;
  2545. cgpara1.done;
  2546. end;
  2547. procedure tcg.g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);
  2548. var
  2549. cgpara1 : TCGPara;
  2550. begin
  2551. { do move call }
  2552. cgpara1.init;
  2553. paramanager.getintparaloc(pocall_default,1,cgpara1);
  2554. { load source }
  2555. paramanager.allocparaloc(list,cgpara1);
  2556. a_param_loc(list,l,cgpara1);
  2557. paramanager.freeparaloc(list,cgpara1);
  2558. allocallcpuregisters(list);
  2559. a_call_name(list,'FPC_FREEMEM');
  2560. deallocallcpuregisters(list);
  2561. cgpara1.done;
  2562. end;
  2563. procedure tcg.g_save_standard_registers(list:TAsmList);
  2564. var
  2565. href : treference;
  2566. size : longint;
  2567. r : integer;
  2568. begin
  2569. { Get temp }
  2570. size:=0;
  2571. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  2572. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2573. inc(size,sizeof(aint));
  2574. if size>0 then
  2575. begin
  2576. tg.GetTemp(list,size,tt_noreuse,current_procinfo.save_regs_ref);
  2577. { Copy registers to temp }
  2578. href:=current_procinfo.save_regs_ref;
  2579. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  2580. begin
  2581. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2582. begin
  2583. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE),href);
  2584. inc(href.offset,sizeof(aint));
  2585. end;
  2586. include(rg[R_INTREGISTER].preserved_by_proc,saved_standard_registers[r]);
  2587. end;
  2588. end;
  2589. end;
  2590. procedure tcg.g_restore_standard_registers(list:TAsmList);
  2591. var
  2592. href : treference;
  2593. r : integer;
  2594. hreg : tregister;
  2595. begin
  2596. { Copy registers from temp }
  2597. href:=current_procinfo.save_regs_ref;
  2598. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  2599. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2600. begin
  2601. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  2602. { Allocate register so the optimizer does not remove the load }
  2603. a_reg_alloc(list,hreg);
  2604. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2605. inc(href.offset,sizeof(aint));
  2606. end;
  2607. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  2608. end;
  2609. procedure tcg.g_profilecode(list : TAsmList);
  2610. begin
  2611. end;
  2612. procedure tcg.g_exception_reason_save(list : TAsmList; const href : treference);
  2613. begin
  2614. a_load_reg_ref(list, OS_INT, OS_INT, NR_FUNCTION_RESULT_REG, href);
  2615. end;
  2616. procedure tcg.g_exception_reason_save_const(list : TAsmList; const href : treference; a: aint);
  2617. begin
  2618. a_load_const_ref(list, OS_INT, a, href);
  2619. end;
  2620. procedure tcg.g_exception_reason_load(list : TAsmList; const href : treference);
  2621. begin
  2622. a_load_ref_reg(list, OS_INT, OS_INT, href, NR_FUNCTION_RESULT_REG);
  2623. end;
  2624. procedure tcg.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);
  2625. var
  2626. hsym : tsym;
  2627. href : treference;
  2628. paraloc : tcgparalocation;
  2629. begin
  2630. { calculate the parameter info for the procdef }
  2631. if not procdef.has_paraloc_info then
  2632. begin
  2633. procdef.requiredargarea:=paramanager.create_paraloc_info(procdef,callerside);
  2634. procdef.has_paraloc_info:=true;
  2635. end;
  2636. hsym:=tsym(procdef.parast.search('self'));
  2637. if not(assigned(hsym) and
  2638. (hsym.typ=paravarsym)) then
  2639. internalerror(200305251);
  2640. paraloc:=tparavarsym(hsym).paraloc[callerside].location^;
  2641. case paraloc.loc of
  2642. LOC_REGISTER:
  2643. cg.a_op_const_reg(list,OP_SUB,paraloc.size,ioffset,paraloc.register);
  2644. LOC_REFERENCE:
  2645. begin
  2646. { offset in the wrapper needs to be adjusted for the stored
  2647. return address }
  2648. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset+sizeof(aint));
  2649. cg.a_op_const_ref(list,OP_SUB,paraloc.size,ioffset,href);
  2650. end
  2651. else
  2652. internalerror(200309189);
  2653. end;
  2654. end;
  2655. procedure tcg.a_call_name_static(list : TAsmList;const s : string);
  2656. begin
  2657. a_call_name(list,s);
  2658. end;
  2659. function tcg.g_indirect_sym_load(list:TAsmList;const symname: string): tregister;
  2660. var
  2661. l: tasmsymbol;
  2662. ref: treference;
  2663. begin
  2664. result := NR_NO;
  2665. case target_info.system of
  2666. system_powerpc_darwin,
  2667. system_i386_darwin:
  2668. begin
  2669. l:=current_asmdata.getasmsymbol('L'+symname+'$non_lazy_ptr');
  2670. if not(assigned(l)) then
  2671. begin
  2672. l:=current_asmdata.DefineAsmSymbol('L'+symname+'$non_lazy_ptr',AB_COMMON,AT_DATA);
  2673. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create(l,0));
  2674. current_asmdata.asmlists[al_picdata].concat(tai_const.create_indirect_sym(current_asmdata.RefAsmSymbol(symname)));
  2675. current_asmdata.asmlists[al_picdata].concat(tai_const.create_32bit(0));
  2676. end;
  2677. result := cg.getaddressregister(list);
  2678. reference_reset_symbol(ref,l,0);
  2679. { ref.base:=current_procinfo.got;
  2680. ref.relsymbol:=current_procinfo.CurrGOTLabel;}
  2681. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,result);
  2682. end;
  2683. end;
  2684. end;
  2685. {*****************************************************************************
  2686. TCG64
  2687. *****************************************************************************}
  2688. {$ifndef cpu64bit}
  2689. procedure tcg64.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64; regsrc,regdst : tregister64);
  2690. begin
  2691. a_load64_reg_reg(list,regsrc,regdst);
  2692. a_op64_const_reg(list,op,size,value,regdst);
  2693. end;
  2694. procedure tcg64.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  2695. var
  2696. tmpreg64 : tregister64;
  2697. begin
  2698. { when src1=dst then we need to first create a temp to prevent
  2699. overwriting src1 with src2 }
  2700. if (regsrc1.reghi=regdst.reghi) or
  2701. (regsrc1.reglo=regdst.reghi) or
  2702. (regsrc1.reghi=regdst.reglo) or
  2703. (regsrc1.reglo=regdst.reglo) then
  2704. begin
  2705. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2706. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2707. a_load64_reg_reg(list,regsrc2,tmpreg64);
  2708. a_op64_reg_reg(list,op,size,regsrc1,tmpreg64);
  2709. a_load64_reg_reg(list,tmpreg64,regdst);
  2710. end
  2711. else
  2712. begin
  2713. a_load64_reg_reg(list,regsrc2,regdst);
  2714. a_op64_reg_reg(list,op,size,regsrc1,regdst);
  2715. end;
  2716. end;
  2717. procedure tcg64.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2718. begin
  2719. a_op64_const_reg_reg(list,op,size,value,regsrc,regdst);
  2720. ovloc.loc:=LOC_VOID;
  2721. end;
  2722. procedure tcg64.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2723. begin
  2724. a_op64_reg_reg_reg(list,op,size,regsrc1,regsrc2,regdst);
  2725. ovloc.loc:=LOC_VOID;
  2726. end;
  2727. {$endif cpu64bit}
  2728. initialization
  2729. ;
  2730. finalization
  2731. cg.free;
  2732. {$ifndef cpu64bit}
  2733. cg64.free;
  2734. {$endif cpu64bit}
  2735. end.