hlcgobj.pas 129 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025
  1. {
  2. Copyright (c) 1998-2010 by Florian Klaempfl and Jonas Maebe
  3. Member of the Free Pascal development team
  4. This unit implements the basic high level 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. Abstract high level code generator unit. This contains the base class
  20. that either lowers most code to the regular code generator, or that
  21. has to be implemented/overridden for higher level targets (such as LLVM).
  22. }
  23. unit hlcgobj;
  24. {$i fpcdefs.inc}
  25. { define hlcginline}
  26. interface
  27. uses
  28. cclasses,globtype,constexp,
  29. cpubase,cgbase,cgutils,parabase,
  30. aasmbase,aasmtai,aasmdata,aasmcpu,
  31. symconst,symtype,symdef,rgobj,
  32. node
  33. ;
  34. type
  35. {# @abstract(Abstract high level code generator)
  36. This class implements an abstract instruction generator. All
  37. methods of this class are generic and are mapped to low level code
  38. generator methods by default. They have to be overridden for higher
  39. level targets
  40. }
  41. { thlcgobj }
  42. thlcgobj = class
  43. public
  44. {************************************************}
  45. { basic routines }
  46. constructor create;
  47. {# Initialize the register allocators needed for the codegenerator.}
  48. procedure init_register_allocators;virtual;
  49. {# Clean up the register allocators needed for the codegenerator.}
  50. procedure done_register_allocators;virtual;
  51. {# 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. }
  52. procedure set_regalloc_live_range_direction(dir: TRADirection);virtual;
  53. {# Gets a register suitable to do integer operations on.}
  54. function getintregister(list:TAsmList;size:tdef):Tregister;virtual;
  55. {# Gets a register suitable to do integer operations on.}
  56. function getaddressregister(list:TAsmList;size:tdef):Tregister;virtual;
  57. function getfpuregister(list:TAsmList;size:tdef):Tregister;virtual;
  58. // we don't have high level defs yet that translate into all mm cgsizes
  59. // function getmmregister(list:TAsmList;size:tdef):Tregister;virtual;
  60. function getflagregister(list:TAsmList;size:tdef):Tregister;virtual;
  61. function getregisterfordef(list: TAsmList;size:tdef):Tregister;virtual;
  62. {Does the generic cg need SIMD registers, like getmmxregister? Or should
  63. the cpu specific child cg object have such a method?}
  64. function uses_registers(rt:Tregistertype):boolean; inline;
  65. procedure do_register_allocation(list:TAsmList;headertai:tai); inline;
  66. procedure translate_register(var reg : tregister); inline;
  67. {# Returns the kind of register this type should be loaded in (it does not
  68. check whether this is actually possible, but if it's loaded in a register
  69. by the compiler for any purpose other than parameter passing/function
  70. result loading, this is the register type used }
  71. function def2regtyp(def: tdef): tregistertype; virtual;
  72. {# Emit a label to the instruction stream. }
  73. procedure a_label(list : TAsmList;l : tasmlabel); inline;
  74. {# Allocates register r by inserting a pai_realloc record }
  75. procedure a_reg_alloc(list : TAsmList;r : tregister); inline;
  76. {# Deallocates register r by inserting a pa_regdealloc record}
  77. procedure a_reg_dealloc(list : TAsmList;r : tregister); inline;
  78. { Synchronize register, make sure it is still valid }
  79. procedure a_reg_sync(list : TAsmList;r : tregister); inline;
  80. {# Pass a parameter, which is located in a register, to a routine.
  81. This routine should push/send the parameter to the routine, as
  82. required by the specific processor ABI and routine modifiers.
  83. It must generate register allocation information for the cgpara in
  84. case it consists of cpuregisters.
  85. @param(size size of the operand in the register)
  86. @param(r register source of the operand)
  87. @param(cgpara where the parameter will be stored)
  88. }
  89. procedure a_load_reg_cgpara(list : TAsmList;size : tdef;r : tregister;const cgpara : TCGPara);virtual;
  90. {# Pass a parameter, which is a constant, to a routine.
  91. A generic version is provided. This routine should
  92. be overridden for optimization purposes if the cpu
  93. permits directly sending this type of parameter.
  94. It must generate register allocation information for the cgpara in
  95. case it consists of cpuregisters.
  96. @param(size size of the operand in constant)
  97. @param(a value of constant to send)
  98. @param(cgpara where the parameter will be stored)
  99. }
  100. procedure a_load_const_cgpara(list : TAsmList;tosize : tdef;a : aint;const cgpara : TCGPara);virtual;
  101. {# Pass the value of a parameter, which is located in memory, to a routine.
  102. A generic version is provided. This routine should
  103. be overridden for optimization purposes if the cpu
  104. permits directly sending this type of parameter.
  105. It must generate register allocation information for the cgpara in
  106. case it consists of cpuregisters.
  107. @param(size size of the operand in constant)
  108. @param(r Memory reference of value to send)
  109. @param(cgpara where the parameter will be stored)
  110. }
  111. procedure a_load_ref_cgpara(list : TAsmList;size : tdef;const r : treference;const cgpara : TCGPara);virtual;
  112. {# Pass the value of a parameter, which can be located either in a register or memory location,
  113. to a routine.
  114. A generic version is provided.
  115. @param(l location of the operand to send)
  116. @param(nr parameter number (starting from one) of routine (from left to right))
  117. @param(cgpara where the parameter will be stored)
  118. }
  119. procedure a_load_loc_cgpara(list : TAsmList;size : tdef; const l : tlocation;const cgpara : TCGPara);virtual;
  120. {# Pass the address of a reference to a routine. This routine
  121. will calculate the address of the reference, and pass this
  122. calculated address as a parameter.
  123. It must generate register allocation information for the cgpara in
  124. case it consists of cpuregisters.
  125. A generic version is provided. This routine should
  126. be overridden for optimization purposes if the cpu
  127. permits directly sending this type of parameter.
  128. @param(fromsize type of the reference we are taking the address of)
  129. @param(tosize type of the pointer that we get as a result)
  130. @param(r reference to get address from)
  131. }
  132. procedure a_loadaddr_ref_cgpara(list : TAsmList;fromsize, tosize : tdef;const r : treference;const cgpara : TCGPara);virtual;
  133. { Remarks:
  134. * If a method specifies a size you have only to take care
  135. of that number of bits, i.e. load_const_reg with OP_8 must
  136. only load the lower 8 bit of the specified register
  137. the rest of the register can be undefined
  138. if necessary the compiler will call a method
  139. to zero or sign extend the register
  140. * The a_load_XX_XX with OP_64 needn't to be
  141. implemented for 32 bit
  142. processors, the code generator takes care of that
  143. * the addr size is for work with the natural pointer
  144. size
  145. * the procedures without fpu/mm are only for integer usage
  146. * normally the first location is the source and the
  147. second the destination
  148. }
  149. {# Emits instruction to call the method specified by symbol name.
  150. This routine must be overridden for each new target cpu.
  151. }
  152. procedure a_call_name(list : TAsmList;pd : tprocdef;const s : TSymStr; weak: boolean);virtual;abstract;
  153. procedure a_call_reg(list : TAsmList;pd : tabstractprocdef;reg : tregister);virtual;abstract;
  154. procedure a_call_ref(list : TAsmList;pd : tabstractprocdef;ref : treference);virtual;abstract;
  155. { same as a_call_name, might be overridden on certain architectures to emit
  156. static calls without usage of a got trampoline }
  157. procedure a_call_name_static(list : TAsmList;pd : tprocdef;const s : TSymStr);virtual;
  158. { same as a_call_name, might be overridden on certain architectures to emit
  159. special static calls for inherited methods }
  160. procedure a_call_name_inherited(list : TAsmList;pd : tprocdef;const s : TSymStr);virtual;
  161. { move instructions }
  162. procedure a_load_const_reg(list : TAsmList;tosize : tdef;a : aint;register : tregister);virtual;abstract;
  163. procedure a_load_const_ref(list : TAsmList;tosize : tdef;a : aint;const ref : treference);virtual;
  164. procedure a_load_const_loc(list : TAsmList;tosize : tdef;a : aint;const loc : tlocation);virtual;
  165. procedure a_load_reg_ref(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);virtual;abstract;
  166. procedure a_load_reg_ref_unaligned(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);virtual;
  167. procedure a_load_reg_reg(list : TAsmList;fromsize, tosize : tdef;reg1,reg2 : tregister);virtual;abstract;
  168. procedure a_load_reg_loc(list : TAsmList;fromsize, tosize : tdef;reg : tregister;const loc: tlocation);virtual;
  169. procedure a_load_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);virtual;abstract;
  170. procedure a_load_ref_reg_unaligned(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);virtual;
  171. procedure a_load_ref_ref(list : TAsmList;fromsize, tosize : tdef;const sref : treference;const dref : treference);virtual;
  172. procedure a_load_loc_reg(list : TAsmList;fromsize, tosize : tdef; const loc: tlocation; reg : tregister);virtual;
  173. procedure a_load_loc_ref(list : TAsmList;fromsize, tosize: tdef; const loc: tlocation; const ref : treference);virtual;
  174. procedure a_load_loc_subsetreg(list : TAsmList;fromsize, tosize, tosubsetsize: tdef; const loc: tlocation; const sreg : tsubsetregister);virtual;
  175. procedure a_load_loc_subsetref(list : TAsmList;fromsize, tosize, tosubsetsize: tdef; const loc: tlocation; const sref : tsubsetreference);virtual;
  176. procedure a_loadaddr_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;r : tregister);virtual;abstract;
  177. { The subset stuff still need a transformation to thlcgobj }
  178. procedure a_load_subsetreg_reg(list : TAsmList; fromsize, fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister); virtual; abstract;
  179. procedure a_load_reg_subsetreg(list : TAsmList; fromsize, tosize, tosubsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister); virtual; abstract;
  180. procedure a_load_subsetreg_subsetreg(list: TAsmlist; fromsize, fromsubsetsize, tosize, tosubsetsize : tdef; const fromsreg, tosreg: tsubsetregister); virtual; abstract;
  181. procedure a_load_subsetreg_ref(list : TAsmList; fromsize, fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const destref: treference); virtual; abstract;
  182. procedure a_load_ref_subsetreg(list : TAsmList; fromsize, tosize,tosubsetsize: tdef; const fromref: treference; const sreg: tsubsetregister); virtual; abstract;
  183. procedure a_load_const_subsetreg(list: TAsmlist; tosize, tosubsetsize: tdef; a: aint; const sreg: tsubsetregister); virtual; abstract;
  184. procedure a_load_subsetreg_loc(list: TAsmlist; fromsize, fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const loc: tlocation); virtual;
  185. procedure a_load_subsetref_reg(list : TAsmList; fromsize, fromsubsetsize, tosize: tdef; const sref: tsubsetreference; destreg: tregister); virtual; abstract;
  186. procedure a_load_reg_subsetref(list : TAsmList; fromsize, tosubsetsize, tosize: tdef; fromreg: tregister; const sref: tsubsetreference); virtual; abstract;
  187. procedure a_load_subsetref_subsetref(list: TAsmlist; fromsize, fromsubsetsize, tosize, tosubsetsize : tdef; const fromsref, tosref: tsubsetreference); virtual; abstract;
  188. procedure a_load_subsetref_ref(list : TAsmList; fromsize, fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const destref: treference); virtual; abstract;
  189. procedure a_load_ref_subsetref(list : TAsmList; fromsize, tosize, tosubsetsize: tdef; const fromref: treference; const sref: tsubsetreference); virtual; abstract;
  190. procedure a_load_const_subsetref(list: TAsmlist; tosize, tosubsetsize: tdef; a: aint; const sref: tsubsetreference); virtual; abstract;
  191. procedure a_load_subsetref_loc(list: TAsmlist; fromsize, fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const loc: tlocation); virtual;
  192. procedure a_load_subsetref_subsetreg(list: TAsmlist; fromsize, fromsubsetsize, tosize, tosubsetsize : tdef; const fromsref: tsubsetreference; const tosreg: tsubsetregister); virtual; abstract;
  193. procedure a_load_subsetreg_subsetref(list: TAsmlist; fromsize, fromsubsetsize, tosize, tosubsetsize : tdef; const fromsreg: tsubsetregister; const tosref: tsubsetreference); virtual; abstract;
  194. { bit test instructions (still need transformation to thlcgobj) }
  195. procedure a_bit_test_reg_reg_reg(list : TAsmList; bitnumbersize,valuesize,destsize: tdef;bitnumber,value,destreg: tregister); virtual; abstract;
  196. procedure a_bit_test_const_ref_reg(list: TAsmList; fromsize, destsize: tdef; bitnumber: aint; const ref: treference; destreg: tregister); virtual; abstract;
  197. procedure a_bit_test_const_reg_reg(list: TAsmList; setregsize, destsize: tdef; bitnumber: aint; setreg, destreg: tregister); virtual; abstract;
  198. procedure a_bit_test_const_subsetreg_reg(list: TAsmList; fromsize, fromsubsetsize, destsize: tdef; bitnumber: aint; const setreg: tsubsetregister; destreg: tregister); virtual; abstract;
  199. procedure a_bit_test_reg_ref_reg(list: TAsmList; bitnumbersize, refsize, destsize: tdef; bitnumber: tregister; const ref: treference; destreg: tregister); virtual; abstract;
  200. procedure a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, locsize, destsize: tdef; bitnumber: tregister; const loc: tlocation; destreg: tregister);virtual;
  201. procedure a_bit_test_const_loc_reg(list: TAsmList; locsize, destsize: tdef; bitnumber: aint; const loc: tlocation; destreg: tregister);virtual;
  202. { bit set/clear instructions (still need transformation to thlcgobj) }
  203. procedure a_bit_set_reg_reg(list : TAsmList; doset: boolean; bitnumbersize, destsize: tdef; bitnumber,dest: tregister); virtual; abstract;
  204. procedure a_bit_set_const_ref(list: TAsmList; doset: boolean;destsize: tdef; bitnumber: aint; const ref: treference); virtual; abstract;
  205. procedure a_bit_set_const_reg(list: TAsmList; doset: boolean; destsize: tdef; bitnumber: aint; destreg: tregister); virtual; abstract;
  206. procedure a_bit_set_const_subsetreg(list: TAsmList; doset: boolean; destsize, destsubsetsize: tdef; bitnumber: aint; const destreg: tsubsetregister); virtual; abstract;
  207. procedure a_bit_set_reg_ref(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const ref: treference); virtual; abstract;
  208. procedure a_bit_set_reg_loc(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const loc: tlocation);virtual;
  209. procedure a_bit_set_const_loc(list: TAsmList; doset: boolean; tosize: tdef; bitnumber: aint; const loc: tlocation);virtual;
  210. { bit scan instructions (still need transformation to thlcgobj) }
  211. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: tdef; src, dst: tregister); virtual; abstract;
  212. { fpu move instructions }
  213. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister); virtual; abstract;
  214. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister); virtual; abstract;
  215. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference); virtual; abstract;
  216. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1,ref2: treference);virtual;
  217. procedure a_loadfpu_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister);virtual;
  218. procedure a_loadfpu_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation);virtual;
  219. procedure a_loadfpu_reg_cgpara(list : TAsmList;fromsize: tdef;const r : tregister;const cgpara : TCGPara);virtual;
  220. procedure a_loadfpu_ref_cgpara(list : TAsmList;fromsize : tdef;const ref : treference;const cgpara : TCGPara);virtual;
  221. { vector register move instructions }
  222. // we don't have high level defs yet that translate into all mm cgsizes
  223. {
  224. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef;reg1, reg2: tregister;shuffle : pmmshuffle); virtual;
  225. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  226. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual;
  227. procedure a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);virtual;
  228. procedure a_loadmm_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);virtual;
  229. procedure a_loadmm_reg_cgpara(list: TAsmList; fromsize: tdef; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  230. procedure a_loadmm_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  231. procedure a_loadmm_loc_cgpara(list: TAsmList; fromsize: tdef; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  232. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tdef;src,dst: tregister;shuffle : pmmshuffle); virtual;
  233. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tdef;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  234. procedure a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tdef;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  235. procedure a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tdef;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  236. }
  237. // we don't have high level defs yet that translate into all mm cgsizes
  238. // procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tdef; intreg, mmreg: tregister; shuffle: pmmshuffle); virtual;
  239. // procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tdef; mmreg, intreg: tregister; shuffle : pmmshuffle); virtual;
  240. { basic arithmetic operations }
  241. { note: for operators which require only one argument (not, neg), use }
  242. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  243. { that in this case the *second* operand is used as both source and }
  244. { destination (JM) }
  245. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; reg: TRegister); virtual; abstract;
  246. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference); virtual;
  247. procedure a_op_const_subsetreg(list : TAsmList; Op : TOpCG; size, subsetsize : tdef; a : aint; const sreg: tsubsetregister); virtual;
  248. procedure a_op_const_subsetref(list : TAsmList; Op : TOpCG; size, subsetsize : tdef; a : aint; const sref: tsubsetreference); virtual;
  249. procedure a_op_const_loc(list : TAsmList; Op: TOpCG; size: tdef; a: Aint; const loc: tlocation);virtual;
  250. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister); virtual; abstract;
  251. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: tdef; reg: TRegister; const ref: TReference); virtual;
  252. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister); virtual;
  253. procedure a_op_reg_subsetreg(list: TAsmList; Op: TOpCG; opsize, destsize, destsubsetsize: tdef; reg: TRegister; const sreg: tsubsetregister); virtual;
  254. procedure a_op_reg_subsetref(list: TAsmList; Op: TOpCG; opsize, destsize, destsubsetsize: tdef; reg: TRegister; const sref: tsubsetreference); virtual;
  255. procedure a_op_reg_loc(list : TAsmList; Op: TOpCG; size: tdef; reg: tregister; const loc: tlocation);virtual;
  256. procedure a_op_ref_loc(list : TAsmList; Op: TOpCG; size: tdef; const ref: TReference; const loc: tlocation);virtual;
  257. { trinary operations for processors that support them, 'emulated' }
  258. { on others. None with "ref" arguments since I don't think there }
  259. { are any processors that support it (JM) }
  260. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister); virtual;
  261. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister); virtual;
  262. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  263. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  264. { comparison operations }
  265. procedure a_cmp_const_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp;a : aint;reg : tregister;
  266. l : tasmlabel);virtual;
  267. procedure a_cmp_const_ref_label(list : TAsmList;size : tdef;cmp_op : topcmp;a : aint;const ref : treference;
  268. l : tasmlabel); virtual;
  269. procedure a_cmp_const_loc_label(list: TAsmList; size: tdef;cmp_op: topcmp; a: aint; const loc: tlocation;
  270. l : tasmlabel);virtual;
  271. procedure a_cmp_reg_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  272. procedure a_cmp_ref_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  273. procedure a_cmp_reg_ref_label(list : TAsmList;size : tdef;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  274. procedure a_cmp_subsetreg_reg_label(list: TAsmList; fromsize, fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; l: tasmlabel); virtual;
  275. procedure a_cmp_subsetref_reg_label(list: TAsmList; fromsize, fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; l: tasmlabel); virtual;
  276. procedure a_cmp_loc_reg_label(list : TAsmList;size : tdef;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);virtual;
  277. procedure a_cmp_reg_loc_label(list : TAsmList;size : tdef;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);virtual;
  278. procedure a_cmp_ref_loc_label(list: TAsmList; size: tdef;cmp_op: topcmp; const ref: treference; const loc: tlocation; l : tasmlabel);virtual;
  279. procedure a_jmp_always(list : TAsmList;l: tasmlabel); virtual;abstract;
  280. {$ifdef cpuflags}
  281. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); virtual; abstract;
  282. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  283. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  284. }
  285. procedure g_flags2reg(list: TAsmList; size: tdef; const f: tresflags; reg: TRegister); virtual; abstract;
  286. procedure g_flags2ref(list: TAsmList; size: tdef; const f: tresflags; const ref:TReference); virtual; abstract;
  287. {$endif cpuflags}
  288. // procedure g_maybe_testself(list : TAsmList;reg:tregister);
  289. // procedure g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  290. {# This should emit the opcode to copy len bytes from the source
  291. to destination.
  292. It must be overridden for each new target processor.
  293. @param(source Source reference of copy)
  294. @param(dest Destination reference of copy)
  295. }
  296. procedure g_concatcopy(list : TAsmList;size: tdef; const source,dest : treference);virtual;
  297. {# This should emit the opcode to copy len bytes from the an unaligned source
  298. to destination.
  299. It must be overridden for each new target processor.
  300. @param(source Source reference of copy)
  301. @param(dest Destination reference of copy)
  302. }
  303. procedure g_concatcopy_unaligned(list : TAsmList;size: tdef; const source,dest : treference);virtual;
  304. {# This should emit the opcode to a shortrstring from the source
  305. to destination.
  306. @param(source Source reference of copy)
  307. @param(dest Destination reference of copy)
  308. }
  309. procedure g_copyshortstring(list : TAsmList;const source,dest : treference;strdef:tstringdef);virtual;abstract;
  310. procedure g_copyvariant(list : TAsmList;const source,dest : treference;vardef:tvariantdef);virtual;abstract;
  311. procedure g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);virtual;abstract;
  312. procedure g_initialize(list : TAsmList;t : tdef;const ref : treference);virtual;abstract;
  313. procedure g_finalize(list : TAsmList;t : tdef;const ref : treference);virtual;abstract;
  314. procedure g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation;
  315. const name: string);virtual;abstract;
  316. {# Generates range checking code. It is to note
  317. that this routine does not need to be overridden,
  318. as it takes care of everything.
  319. @param(p Node which contains the value to check)
  320. @param(todef Type definition of node to range check)
  321. }
  322. procedure g_rangecheck(list: TAsmList; const l:tlocation; fromdef,todef: tdef); virtual;
  323. {# Generates overflow checking code for a node }
  324. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef); virtual; abstract;
  325. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;var ovloc : tlocation);virtual; abstract;
  326. procedure g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;arrdef: tarraydef;destreg:tregister);virtual;abstract;
  327. procedure g_releasevaluepara_openarray(list : TAsmList;arrdef: tarraydef;const l:tlocation);virtual;abstract;
  328. {# Emits instructions when compilation is done in profile
  329. mode (this is set as a command line option). The default
  330. behavior does nothing, should be overridden as required.
  331. }
  332. procedure g_profilecode(list : TAsmList);virtual;
  333. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  334. @param(size Number of bytes to allocate)
  335. }
  336. procedure g_stackpointer_alloc(list : TAsmList;size : longint);virtual; abstract;
  337. {# Emits instruction for allocating the locals in entry
  338. code of a routine. This is one of the first
  339. routine called in @var(genentrycode).
  340. @param(localsize Number of bytes to allocate as locals)
  341. }
  342. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);virtual; abstract;
  343. {# Emits instructions for returning from a subroutine.
  344. Should also restore the framepointer and stack.
  345. @param(parasize Number of bytes of parameters to deallocate from stack)
  346. }
  347. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);virtual; abstract;
  348. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);virtual; abstract;
  349. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);virtual; abstract;
  350. function g_indirect_sym_load(list:TAsmList;const symname: string; const flags: tindsymflags): tregister;virtual; abstract;
  351. { generate a stub which only purpose is to pass control the given external method,
  352. setting up any additional environment before doing so (if required).
  353. The default implementation issues a jump instruction to the external name. }
  354. // procedure g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string); virtual;
  355. protected
  356. procedure g_allocload_reg_reg(list: TAsmList; regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  357. public
  358. { create "safe copy" of a tlocation that can be used later: all
  359. registers used in the tlocation are copied to new ones, so that
  360. even if the original ones change, things stay the same (except if
  361. the original location was already a register, then the register is
  362. kept). Must only be used on lvalue locations.
  363. It's intended as some kind of replacement for a_loadaddr_ref_reg()
  364. for targets without pointers. }
  365. procedure g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation); virtual;
  366. { routines migrated from ncgutil }
  367. procedure location_force_reg(list:TAsmList;var l:tlocation;src_size,dst_size:tdef;maybeconst:boolean);virtual;
  368. procedure location_force_fpureg(list:TAsmList;var l: tlocation;size: tdef;maybeconst:boolean);virtual;
  369. procedure location_force_mem(list:TAsmList;var l:tlocation;size:tdef);virtual;
  370. // procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;abstract;
  371. // procedure location_force_mmreg(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;abstract;
  372. { Retrieve the location of the data pointed to in location l, when the location is
  373. a register it is expected to contain the address of the data }
  374. procedure location_get_data_ref(list:TAsmList;def: tdef; const l:tlocation;var ref:treference;loadref:boolean; alignment: longint);virtual;
  375. procedure maketojumpbool(list:TAsmList; p : tnode);virtual;
  376. procedure gen_proc_symbol(list:TAsmList);virtual;
  377. procedure gen_proc_symbol_end(list:TAsmList);virtual;
  378. procedure gen_initialize_code(list:TAsmList);virtual;
  379. procedure gen_finalize_code(list:TAsmList);virtual;
  380. procedure gen_entry_code(list:TAsmList);virtual;
  381. procedure gen_exit_code(list:TAsmList);virtual;
  382. protected
  383. { helpers called by gen_initialize_code/gen_finalize_code }
  384. procedure inittempvariables(list:TAsmList);virtual;
  385. procedure initialize_data(p:TObject;arg:pointer);virtual;
  386. procedure finalizetempvariables(list:TAsmList);virtual;
  387. procedure initialize_regvars(p:TObject;arg:pointer);virtual;
  388. procedure finalize_sym(asmlist:TAsmList;sym:tsym);virtual;
  389. { generates the code for finalisation of local variables }
  390. procedure finalize_local_vars(p:TObject;arg:pointer);virtual;
  391. { generates the code for finalization of static symtable and
  392. all local (static) typed consts }
  393. procedure finalize_static_data(p:TObject;arg:pointer);virtual;
  394. { generates the code for decrementing the reference count of parameters }
  395. procedure final_paras(p:TObject;arg:pointer);
  396. public
  397. procedure gen_load_para_value(list:TAsmList);virtual;
  398. protected
  399. { helpers called by gen_load_para_value }
  400. procedure g_copyvalueparas(p:TObject;arg:pointer);virtual;
  401. procedure gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation;const cgpara: tcgpara;locintsize: longint);virtual;
  402. procedure init_paras(p:TObject;arg:pointer);
  403. protected
  404. { Some targets have to put "something" in the function result
  405. location if it's not initialised by the Pascal code, e.g.
  406. stack-based architectures. By default it does nothing }
  407. procedure gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);virtual;
  408. public
  409. { load a tlocation into a cgpara }
  410. procedure gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);virtual;
  411. { load a cgpara into a tlocation }
  412. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);virtual;
  413. { load the function return value into the ABI-defined function return location }
  414. procedure gen_load_return_value(list:TAsmList);virtual;
  415. { extras refactored from other units }
  416. { queue the code/data generated for a procedure for writing out to
  417. the assembler/object file }
  418. procedure record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList); virtual;
  419. { generate a call to a routine in the system unit }
  420. procedure g_call_system_proc(list: TAsmList; const procname: string);
  421. { Generate code to exit an unwind-protected region. The default implementation
  422. produces a simple jump to destination label. }
  423. procedure g_local_unwind(list: TAsmList; l: TAsmLabel);virtual;abstract;
  424. end;
  425. var
  426. {# Main high level code generator class }
  427. hlcg : thlcgobj;
  428. procedure destroy_hlcodegen;
  429. implementation
  430. uses
  431. globals,options,systems,
  432. fmodule,export,
  433. verbose,defutil,paramgr,
  434. symbase,symsym,symtable,
  435. ncon,nld,pass_1,pass_2,
  436. cpuinfo,cgobj,tgobj,cutils,procinfo,
  437. ncgutil,ngenutil;
  438. procedure destroy_hlcodegen;
  439. begin
  440. hlcg.free;
  441. hlcg:=nil;
  442. destroy_codegen;
  443. end;
  444. { thlcgobj }
  445. constructor thlcgobj.create;
  446. begin
  447. end;
  448. procedure thlcgobj.init_register_allocators;
  449. begin
  450. cg.init_register_allocators;
  451. end;
  452. procedure thlcgobj.done_register_allocators;
  453. begin
  454. cg.done_register_allocators;
  455. end;
  456. procedure thlcgobj.set_regalloc_live_range_direction(dir: TRADirection);
  457. begin
  458. cg.set_regalloc_live_range_direction(dir);
  459. end;
  460. function thlcgobj.getintregister(list: TAsmList; size: tdef): Tregister;
  461. begin
  462. result:=cg.getintregister(list,def_cgsize(size));
  463. end;
  464. function thlcgobj.getaddressregister(list: TAsmList; size: tdef): Tregister;
  465. begin
  466. result:=cg.getaddressregister(list);
  467. end;
  468. function thlcgobj.getfpuregister(list: TAsmList; size: tdef): Tregister;
  469. begin
  470. result:=cg.getfpuregister(list,def_cgsize(size));
  471. end;
  472. (*
  473. function thlcgobj.getmmregister(list: TAsmList; size: tdef): Tregister;
  474. begin
  475. result:=cg.getmmregister(list,def_cgsize(size));
  476. end;
  477. *)
  478. function thlcgobj.getflagregister(list: TAsmList; size: tdef): Tregister;
  479. begin
  480. result:=cg.getflagregister(list,def_cgsize(size));
  481. end;
  482. function thlcgobj.getregisterfordef(list: TAsmList; size: tdef): Tregister;
  483. begin
  484. case def2regtyp(size) of
  485. R_INTREGISTER:
  486. result:=getintregister(list,size);
  487. R_ADDRESSREGISTER:
  488. result:=getaddressregister(list,size);
  489. R_FPUREGISTER:
  490. result:=getfpuregister(list,size);
  491. (*
  492. R_MMREGISTER:
  493. result:=getmmregister(list,size);
  494. *)
  495. else
  496. internalerror(2010122901);
  497. end;
  498. end;
  499. function thlcgobj.uses_registers(rt: Tregistertype): boolean;
  500. begin
  501. result:=cg.uses_registers(rt);
  502. end;
  503. procedure thlcgobj.do_register_allocation(list: TAsmList; headertai: tai);
  504. begin
  505. cg.do_register_allocation(list,headertai);
  506. end;
  507. procedure thlcgobj.translate_register(var reg: tregister);
  508. begin
  509. cg.translate_register(reg);
  510. end;
  511. function thlcgobj.def2regtyp(def: tdef): tregistertype;
  512. begin
  513. case def.typ of
  514. enumdef,
  515. orddef,
  516. recorddef,
  517. setdef:
  518. result:=R_INTREGISTER;
  519. stringdef,
  520. pointerdef,
  521. classrefdef,
  522. objectdef,
  523. procvardef,
  524. procdef,
  525. arraydef,
  526. formaldef:
  527. result:=R_ADDRESSREGISTER;
  528. floatdef:
  529. if use_vectorfpu(def) then
  530. result:=R_MMREGISTER
  531. else if cs_fp_emulation in current_settings.moduleswitches then
  532. result:=R_INTREGISTER
  533. else
  534. result:=R_FPUREGISTER;
  535. filedef,
  536. variantdef:
  537. internalerror(2010120507);
  538. else
  539. internalerror(2010120506);
  540. end;
  541. end;
  542. procedure thlcgobj.a_label(list: TAsmList; l: tasmlabel); inline;
  543. begin
  544. cg.a_label(list,l);
  545. end;
  546. procedure thlcgobj.a_reg_alloc(list: TAsmList; r: tregister);
  547. begin
  548. cg.a_reg_alloc(list,r);
  549. end;
  550. procedure thlcgobj.a_reg_dealloc(list: TAsmList; r: tregister);
  551. begin
  552. cg.a_reg_dealloc(list,r);
  553. end;
  554. procedure thlcgobj.a_reg_sync(list: TAsmList; r: tregister);
  555. begin
  556. cg.a_reg_sync(list,r);
  557. end;
  558. procedure thlcgobj.a_load_reg_cgpara(list: TAsmList; size: tdef; r: tregister; const cgpara: TCGPara);
  559. var
  560. ref: treference;
  561. begin
  562. cgpara.check_simple_location;
  563. paramanager.alloccgpara(list,cgpara);
  564. case cgpara.location^.loc of
  565. LOC_REGISTER,LOC_CREGISTER:
  566. a_load_reg_reg(list,size,cgpara.def,r,cgpara.location^.register);
  567. LOC_REFERENCE,LOC_CREFERENCE:
  568. begin
  569. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  570. a_load_reg_ref(list,size,cgpara.def,r,ref);
  571. end;
  572. (*
  573. LOC_MMREGISTER,LOC_CMMREGISTER:
  574. a_loadmm_intreg_reg(list,size,cgpara.def,r,cgpara.location^.register,mms_movescalar);
  575. *)
  576. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  577. begin
  578. tg.gethltemp(list,size,size.size,tt_normal,ref);
  579. a_load_reg_ref(list,size,size,r,ref);
  580. a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  581. tg.ungettemp(list,ref);
  582. end
  583. else
  584. internalerror(2010120415);
  585. end;
  586. end;
  587. procedure thlcgobj.a_load_const_cgpara(list: TAsmList; tosize: tdef; a: aint; const cgpara: TCGPara);
  588. var
  589. ref : treference;
  590. begin
  591. cgpara.check_simple_location;
  592. paramanager.alloccgpara(list,cgpara);
  593. case cgpara.location^.loc of
  594. LOC_REGISTER,LOC_CREGISTER:
  595. a_load_const_reg(list,cgpara.def,a,cgpara.location^.register);
  596. LOC_REFERENCE,LOC_CREFERENCE:
  597. begin
  598. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  599. a_load_const_ref(list,cgpara.def,a,ref);
  600. end
  601. else
  602. internalerror(2010120416);
  603. end;
  604. end;
  605. procedure thlcgobj.a_load_ref_cgpara(list: TAsmList; size: tdef; const r: treference; const cgpara: TCGPara);
  606. var
  607. ref: treference;
  608. begin
  609. cgpara.check_simple_location;
  610. paramanager.alloccgpara(list,cgpara);
  611. case cgpara.location^.loc of
  612. LOC_REGISTER,LOC_CREGISTER:
  613. a_load_ref_reg(list,size,cgpara.def,r,cgpara.location^.register);
  614. LOC_REFERENCE,LOC_CREFERENCE:
  615. begin
  616. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  617. a_load_ref_ref(list,size,cgpara.def,r,ref);
  618. end
  619. (*
  620. LOC_MMREGISTER,LOC_CMMREGISTER:
  621. begin
  622. case location^.size of
  623. OS_F32,
  624. OS_F64,
  625. OS_F128:
  626. a_loadmm_ref_reg(list,cgpara.def,cgpara.def,r,location^.register,mms_movescalar);
  627. OS_M8..OS_M128,
  628. OS_MS8..OS_MS128:
  629. a_loadmm_ref_reg(list,cgpara.def,cgpara.def,r,location^.register,nil);
  630. else
  631. internalerror(2010120417);
  632. end;
  633. end
  634. *)
  635. else
  636. internalerror(2010120418);
  637. end;
  638. end;
  639. procedure thlcgobj.a_load_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: TCGPara);
  640. begin
  641. case l.loc of
  642. LOC_REGISTER,
  643. LOC_CREGISTER :
  644. a_load_reg_cgpara(list,size,l.register,cgpara);
  645. LOC_CONSTANT :
  646. a_load_const_cgpara(list,size,l.value,cgpara);
  647. LOC_CREFERENCE,
  648. LOC_REFERENCE :
  649. a_load_ref_cgpara(list,size,l.reference,cgpara);
  650. else
  651. internalerror(2010120419);
  652. end;
  653. end;
  654. procedure thlcgobj.a_loadaddr_ref_cgpara(list: TAsmList; fromsize, tosize: tdef; const r: treference; const cgpara: TCGPara);
  655. var
  656. hr : tregister;
  657. begin
  658. cgpara.check_simple_location;
  659. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  660. begin
  661. paramanager.allocparaloc(list,cgpara.location);
  662. a_loadaddr_ref_reg(list,fromsize,tosize,r,cgpara.location^.register)
  663. end
  664. else
  665. begin
  666. hr:=getaddressregister(list,tosize);
  667. a_loadaddr_ref_reg(list,fromsize,tosize,r,hr);
  668. a_load_reg_cgpara(list,tosize,hr,cgpara);
  669. end;
  670. end;
  671. procedure thlcgobj.a_call_name_static(list: TAsmList; pd: tprocdef; const s: TSymStr);
  672. begin
  673. a_call_name(list,pd,s,false);
  674. end;
  675. procedure thlcgobj.a_call_name_inherited(list: TAsmList; pd: tprocdef; const s: TSymStr);
  676. begin
  677. a_call_name(list,pd,s,false);
  678. end;
  679. procedure thlcgobj.a_load_const_ref(list: TAsmList; tosize: tdef; a: aint; const ref: treference);
  680. var
  681. tmpreg: tregister;
  682. begin
  683. tmpreg:=getintregister(list,tosize);
  684. a_load_const_reg(list,tosize,a,tmpreg);
  685. a_load_reg_ref(list,tosize,tosize,tmpreg,ref);
  686. end;
  687. procedure thlcgobj.a_load_const_loc(list: TAsmList; tosize: tdef; a: aint; const loc: tlocation);
  688. begin
  689. case loc.loc of
  690. LOC_REFERENCE,LOC_CREFERENCE:
  691. a_load_const_ref(list,tosize,a,loc.reference);
  692. LOC_REGISTER,LOC_CREGISTER:
  693. a_load_const_reg(list,tosize,a,loc.register);
  694. { we don't have enough type information to handle these here
  695. LOC_SUBSETREG,LOC_CSUBSETREG:
  696. a_load_const_subsetreg(list,loc.size,a,loc.sreg);
  697. LOC_SUBSETREF,LOC_CSUBSETREF:
  698. a_load_const_subsetref(list,loc.size,a,loc.sref);
  699. }
  700. else
  701. internalerror(2010120401);
  702. end;
  703. end;
  704. procedure thlcgobj.a_load_reg_ref_unaligned(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  705. begin
  706. a_load_reg_ref(list,fromsize,tosize,register,ref);
  707. end;
  708. procedure thlcgobj.a_load_reg_loc(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const loc: tlocation);
  709. begin
  710. case loc.loc of
  711. LOC_REFERENCE,LOC_CREFERENCE:
  712. a_load_reg_ref(list,fromsize,tosize,reg,loc.reference);
  713. LOC_REGISTER,LOC_CREGISTER:
  714. a_load_reg_reg(list,fromsize,tosize,reg,loc.register);
  715. { we don't have enough type information to handle these here
  716. LOC_SUBSETREG,LOC_CSUBSETREG:
  717. a_load_reg_subsetreg(list,fromsize,tosize,reg,loc.sreg);
  718. LOC_SUBSETREF,LOC_CSUBSETREF:
  719. a_load_reg_subsetref(list,fromsize,loc.size,reg,loc.sref);
  720. LOC_MMREGISTER,LOC_CMMREGISTER:
  721. a_loadmm_intreg_reg(list,fromsize,loc.size,reg,loc.register,mms_movescalar);
  722. }
  723. else
  724. internalerror(2010120402);
  725. end;
  726. end;
  727. procedure thlcgobj.a_load_ref_reg_unaligned(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  728. begin
  729. a_load_ref_reg(list,fromsize,tosize,ref,register);
  730. end;
  731. procedure thlcgobj.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  732. var
  733. tmpreg: tregister;
  734. begin
  735. { verify if we have the same reference }
  736. if references_equal(sref,dref) then
  737. exit;
  738. tmpreg:=getintregister(list,tosize);
  739. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  740. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  741. end;
  742. procedure thlcgobj.a_load_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; reg: tregister);
  743. begin
  744. case loc.loc of
  745. LOC_REFERENCE,LOC_CREFERENCE:
  746. a_load_ref_reg(list,fromsize,tosize,loc.reference,reg);
  747. LOC_REGISTER,LOC_CREGISTER:
  748. a_load_reg_reg(list,fromsize,tosize,loc.register,reg);
  749. LOC_CONSTANT:
  750. a_load_const_reg(list,tosize,loc.value,reg);
  751. { we don't have enough type information to handle these here
  752. LOC_SUBSETREG,LOC_CSUBSETREG:
  753. a_load_subsetreg_reg(list,fromsize,tosize,loc.sreg,reg);
  754. LOC_SUBSETREF,LOC_CSUBSETREF:
  755. a_load_subsetref_reg(list,fromsize,tosize,loc.sref,reg);
  756. }
  757. else
  758. internalerror(2010120201);
  759. end;
  760. end;
  761. procedure thlcgobj.a_load_loc_ref(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const ref: treference);
  762. begin
  763. case loc.loc of
  764. LOC_REFERENCE,LOC_CREFERENCE:
  765. a_load_ref_ref(list,fromsize,tosize,loc.reference,ref);
  766. LOC_REGISTER,LOC_CREGISTER:
  767. a_load_reg_ref(list,fromsize,tosize,loc.register,ref);
  768. LOC_CONSTANT:
  769. a_load_const_ref(list,tosize,loc.value,ref);
  770. { we don't have enough type information to handle these here
  771. LOC_SUBSETREG,LOC_CSUBSETREG:
  772. a_load_subsetreg_ref(list,loc.size,tosize,loc.sreg,ref);
  773. LOC_SUBSETREF,LOC_CSUBSETREF:
  774. a_load_subsetref_ref(list,loc.size,tosize,loc.sref,ref);
  775. }
  776. else
  777. internalerror(2010120403);
  778. end;
  779. end;
  780. procedure thlcgobj.a_load_loc_subsetreg(list: TAsmList; fromsize, tosize, tosubsetsize: tdef; const loc: tlocation; const sreg: tsubsetregister);
  781. begin
  782. case loc.loc of
  783. LOC_REFERENCE,LOC_CREFERENCE:
  784. a_load_ref_subsetreg(list,fromsize,tosize,tosubsetsize,loc.reference,sreg);
  785. LOC_REGISTER,LOC_CREGISTER:
  786. a_load_reg_subsetreg(list,fromsize,tosize,tosubsetsize,loc.register,sreg);
  787. LOC_CONSTANT:
  788. a_load_const_subsetreg(list,tosize,tosubsetsize,loc.value,sreg);
  789. { we don't have enough type information to handle these here
  790. LOC_SUBSETREG,LOC_CSUBSETREG:
  791. a_load_subsetreg_subsetreg(list,loc.size,subsetsize,loc.sreg,sreg);
  792. LOC_SUBSETREF,LOC_CSUBSETREF:
  793. a_load_subsetref_subsetreg(list,loc.size,subsetsize,loc.sref,sreg);
  794. }
  795. else
  796. internalerror(2010120404);
  797. end;
  798. end;
  799. procedure thlcgobj.a_load_loc_subsetref(list: TAsmList; fromsize, tosize, tosubsetsize: tdef; const loc: tlocation; const sref: tsubsetreference);
  800. begin
  801. case loc.loc of
  802. LOC_REFERENCE,LOC_CREFERENCE:
  803. a_load_ref_subsetref(list,fromsize,tosize,tosubsetsize,loc.reference,sref);
  804. LOC_REGISTER,LOC_CREGISTER:
  805. a_load_reg_subsetref(list,fromsize,tosize,tosubsetsize,loc.register,sref);
  806. LOC_CONSTANT:
  807. a_load_const_subsetref(list,tosize,tosubsetsize,loc.value,sref);
  808. { we don't have enough type information to handle these here
  809. LOC_SUBSETREG,LOC_CSUBSETREG:
  810. a_load_subsetreg_subsetref(list,loc.size,subsetsize,loc.sreg,sref);
  811. LOC_SUBSETREF,LOC_CSUBSETREF:
  812. a_load_subsetref_subsetref(list,loc.size,subsetsize,loc.sref,sref);
  813. }
  814. else
  815. internalerror(2010120405);
  816. end;
  817. end;
  818. procedure thlcgobj.a_load_subsetreg_loc(list: TAsmlist; fromsize, fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const loc: tlocation);
  819. begin
  820. case loc.loc of
  821. LOC_REFERENCE,LOC_CREFERENCE:
  822. a_load_subsetreg_ref(list,fromsize,fromsubsetsize,tosize,sreg,loc.reference);
  823. LOC_REGISTER,LOC_CREGISTER:
  824. a_load_subsetreg_reg(list,fromsize,fromsubsetsize,tosize,sreg,loc.register);
  825. { we don't have enough type information to handle these here
  826. LOC_SUBSETREG,LOC_CSUBSETREG:
  827. a_load_subsetreg_subsetreg(list,subsetsize,loc.size,sreg,loc.sreg);
  828. LOC_SUBSETREF,LOC_CSUBSETREF:
  829. a_load_subsetreg_subsetref(list,subsetsize,loc.size,sreg,loc.sref);
  830. }
  831. else
  832. internalerror(2010120406);
  833. end;
  834. end;
  835. procedure thlcgobj.a_load_subsetref_loc(list: TAsmlist; fromsize, fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const loc: tlocation);
  836. begin
  837. case loc.loc of
  838. LOC_REFERENCE,LOC_CREFERENCE:
  839. a_load_subsetref_ref(list,fromsize,fromsubsetsize,tosize,sref,loc.reference);
  840. LOC_REGISTER,LOC_CREGISTER:
  841. a_load_subsetref_reg(list,fromsize,fromsubsetsize,tosize,sref,loc.register);
  842. { we don't have enough type information to handle these here
  843. LOC_SUBSETREG,LOC_CSUBSETREG:
  844. a_load_subsetref_subsetreg(list,subsetsize,loc.size,sref,loc.sreg);
  845. LOC_SUBSETREF,LOC_CSUBSETREF:
  846. a_load_subsetref_subsetref(list,subsetsize,loc.size,sref,loc.sref);
  847. }
  848. else
  849. internalerror(2010120407);
  850. end;
  851. end;
  852. procedure thlcgobj.a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, locsize, destsize: tdef; bitnumber: tregister; const loc: tlocation; destreg: tregister);
  853. var
  854. tmpreg: tregister;
  855. begin
  856. case loc.loc of
  857. LOC_REFERENCE,LOC_CREFERENCE:
  858. a_bit_test_reg_ref_reg(list,bitnumbersize,locsize,destsize,bitnumber,loc.reference,destreg);
  859. LOC_REGISTER,LOC_CREGISTER,
  860. LOC_SUBSETREG,LOC_CSUBSETREG,
  861. LOC_CONSTANT:
  862. begin
  863. case loc.loc of
  864. LOC_REGISTER,LOC_CREGISTER:
  865. tmpreg:=loc.register;
  866. (* we don't have enough type information to handle this here
  867. LOC_SUBSETREG,LOC_CSUBSETREG:
  868. begin
  869. tmpreg:=getintregister(list,loc.size);
  870. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  871. end;
  872. *)
  873. LOC_CONSTANT:
  874. begin
  875. tmpreg:=getintregister(list,locsize);
  876. a_load_const_reg(list,locsize,loc.value,tmpreg);
  877. end;
  878. end;
  879. a_bit_test_reg_reg_reg(list,bitnumbersize,locsize,destsize,bitnumber,tmpreg,destreg);
  880. end;
  881. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  882. else
  883. internalerror(2010120411);
  884. end;
  885. end;
  886. procedure thlcgobj.a_bit_test_const_loc_reg(list: TAsmList; locsize, destsize: tdef; bitnumber: aint; const loc: tlocation; destreg: tregister);
  887. begin
  888. case loc.loc of
  889. LOC_REFERENCE,LOC_CREFERENCE:
  890. a_bit_test_const_ref_reg(list,locsize,destsize,bitnumber,loc.reference,destreg);
  891. LOC_REGISTER,LOC_CREGISTER:
  892. a_bit_test_const_reg_reg(list,locsize,destsize,bitnumber,loc.register,destreg);
  893. (* we don't have enough type information to handle this here
  894. LOC_SUBSETREG,LOC_CSUBSETREG:
  895. a_bit_test_const_subsetreg_reg(list,loc.size,destsize,bitnumber,loc.sreg,destreg);
  896. *)
  897. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  898. else
  899. internalerror(2010120410);
  900. end;
  901. end;
  902. procedure thlcgobj.a_bit_set_reg_loc(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const loc: tlocation);
  903. begin
  904. case loc.loc of
  905. LOC_REFERENCE:
  906. a_bit_set_reg_ref(list,doset,fromsize,tosize,bitnumber,loc.reference);
  907. LOC_CREGISTER:
  908. a_bit_set_reg_reg(list,doset,fromsize,tosize,bitnumber,loc.register);
  909. (* we don't have enough type information to handle this here
  910. { e.g. a 2-byte set in a record regvar }
  911. LOC_CSUBSETREG:
  912. begin
  913. { hard to do in-place in a generic way, so operate on a copy }
  914. tmpreg:=getintregister(list,loc.size);
  915. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  916. a_bit_set_reg_reg(list,doset,bitnumbersize,loc.size,bitnumber,tmpreg);
  917. a_load_reg_subsetreg(list,loc.size,loc.size,tmpreg,loc.sreg);
  918. end;
  919. *)
  920. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  921. else
  922. internalerror(2010120408)
  923. end;
  924. end;
  925. procedure thlcgobj.a_bit_set_const_loc(list: TAsmList; doset: boolean; tosize: tdef; bitnumber: aint; const loc: tlocation);
  926. begin
  927. case loc.loc of
  928. LOC_REFERENCE:
  929. a_bit_set_const_ref(list,doset,tosize,bitnumber,loc.reference);
  930. LOC_CREGISTER:
  931. a_bit_set_const_reg(list,doset,tosize,bitnumber,loc.register);
  932. (* we don't have enough type information to handle this here
  933. LOC_CSUBSETREG:
  934. a_bit_set_const_subsetreg(list,doset,loc.size,bitnumber,loc.sreg);
  935. *)
  936. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  937. else
  938. internalerror(2010120409)
  939. end;
  940. end;
  941. procedure thlcgobj.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  942. var
  943. reg: tregister;
  944. regsize: tdef;
  945. begin
  946. if (fromsize.size>=tosize.size) then
  947. regsize:=fromsize
  948. else
  949. regsize:=tosize;
  950. reg:=getfpuregister(list,regsize);
  951. a_loadfpu_ref_reg(list,fromsize,regsize,ref1,reg);
  952. a_loadfpu_reg_ref(list,regsize,tosize,reg,ref2);
  953. end;
  954. procedure thlcgobj.a_loadfpu_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister);
  955. begin
  956. case loc.loc of
  957. LOC_REFERENCE, LOC_CREFERENCE:
  958. a_loadfpu_ref_reg(list,fromsize,tosize,loc.reference,reg);
  959. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  960. a_loadfpu_reg_reg(list,fromsize,tosize,loc.register,reg);
  961. else
  962. internalerror(2010120412);
  963. end;
  964. end;
  965. procedure thlcgobj.a_loadfpu_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation);
  966. begin
  967. case loc.loc of
  968. LOC_REFERENCE, LOC_CREFERENCE:
  969. a_loadfpu_reg_ref(list,fromsize,tosize,reg,loc.reference);
  970. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  971. a_loadfpu_reg_reg(list,fromsize,tosize,reg,loc.register);
  972. else
  973. internalerror(2010120413);
  974. end;
  975. end;
  976. procedure thlcgobj.a_loadfpu_reg_cgpara(list: TAsmList; fromsize: tdef; const r: tregister; const cgpara: TCGPara);
  977. var
  978. ref : treference;
  979. begin
  980. paramanager.alloccgpara(list,cgpara);
  981. case cgpara.location^.loc of
  982. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  983. begin
  984. cgpara.check_simple_location;
  985. a_loadfpu_reg_reg(list,fromsize,cgpara.def,r,cgpara.location^.register);
  986. end;
  987. LOC_REFERENCE,LOC_CREFERENCE:
  988. begin
  989. cgpara.check_simple_location;
  990. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  991. a_loadfpu_reg_ref(list,fromsize,cgpara.def,r,ref);
  992. end;
  993. LOC_REGISTER,LOC_CREGISTER:
  994. begin
  995. { paramfpu_ref does the check_simpe_location check here if necessary }
  996. tg.gethltemp(list,fromsize,fromsize.size,tt_normal,ref);
  997. a_loadfpu_reg_ref(list,fromsize,fromsize,r,ref);
  998. a_loadfpu_ref_cgpara(list,fromsize,ref,cgpara);
  999. tg.Ungettemp(list,ref);
  1000. end;
  1001. else
  1002. internalerror(2010120422);
  1003. end;
  1004. end;
  1005. procedure thlcgobj.a_loadfpu_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference; const cgpara: TCGPara);
  1006. var
  1007. href : treference;
  1008. // hsize: tcgsize;
  1009. begin
  1010. case cgpara.location^.loc of
  1011. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1012. begin
  1013. cgpara.check_simple_location;
  1014. paramanager.alloccgpara(list,cgpara);
  1015. a_loadfpu_ref_reg(list,fromsize,cgpara.def,ref,cgpara.location^.register);
  1016. end;
  1017. LOC_REFERENCE,LOC_CREFERENCE:
  1018. begin
  1019. cgpara.check_simple_location;
  1020. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1021. { concatcopy should choose the best way to copy the data }
  1022. g_concatcopy(list,fromsize,ref,href);
  1023. end;
  1024. (* not yet supported
  1025. LOC_REGISTER,LOC_CREGISTER:
  1026. begin
  1027. { force integer size }
  1028. hsize:=int_cgsize(tcgsize2size[size]);
  1029. {$ifndef cpu64bitalu}
  1030. if (hsize in [OS_S64,OS_64]) then
  1031. cg64.a_load64_ref_cgpara(list,ref,cgpara)
  1032. else
  1033. {$endif not cpu64bitalu}
  1034. begin
  1035. cgpara.check_simple_location;
  1036. a_load_ref_cgpara(list,hsize,ref,cgpara)
  1037. end;
  1038. end
  1039. *)
  1040. else
  1041. internalerror(2010120423);
  1042. end;
  1043. end;
  1044. (*
  1045. procedure thlcgobj.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle);
  1046. begin
  1047. cg.a_loadmm_reg_reg(list,def_cgsize(fromsize),def_cgsize(tosize),reg1,reg2,shuffle);
  1048. end;
  1049. procedure thlcgobj.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  1050. begin
  1051. cg.a_loadmm_ref_reg(list,def_cgsize(fromsize),def_cgsize(tosize),ref,reg,shuffle);
  1052. end;
  1053. procedure thlcgobj.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  1054. begin
  1055. cg.a_loadmm_reg_ref(list,def_cgsize(fromsize),def_cgsize(tosize),reg,ref,shuffle);
  1056. end;
  1057. procedure thlcgobj.a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle);
  1058. begin
  1059. case loc.loc of
  1060. LOC_MMREGISTER,LOC_CMMREGISTER:
  1061. a_loadmm_reg_reg(list,fromsize,tosize,loc.register,reg,shuffle);
  1062. LOC_REFERENCE,LOC_CREFERENCE:
  1063. a_loadmm_ref_reg(list,fromsize,tosize,loc.reference,reg,shuffle);
  1064. LOC_REGISTER,LOC_CREGISTER:
  1065. a_loadmm_intreg_reg(list,fromsize,tosize,loc.register,reg,shuffle);
  1066. else
  1067. internalerror(2010120414);
  1068. end;
  1069. end;
  1070. procedure thlcgobj.a_loadmm_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation; shuffle: pmmshuffle);
  1071. begin
  1072. case loc.loc of
  1073. LOC_MMREGISTER,LOC_CMMREGISTER:
  1074. a_loadmm_reg_reg(list,fromsize,tosize,reg,loc.register,shuffle);
  1075. LOC_REFERENCE,LOC_CREFERENCE:
  1076. a_loadmm_reg_ref(list,fromsize,tosize,reg,loc.reference,shuffle);
  1077. else
  1078. internalerror(2010120415);
  1079. end;
  1080. end;
  1081. procedure thlcgobj.a_loadmm_reg_cgpara(list: TAsmList; fromsize: tdef; reg: tregister; const cgpara: TCGPara; shuffle: pmmshuffle);
  1082. var
  1083. href : treference;
  1084. begin
  1085. cgpara.check_simple_location;
  1086. paramanager.alloccgpara(list,cgpara);
  1087. case cgpara.location^.loc of
  1088. LOC_MMREGISTER,LOC_CMMREGISTER:
  1089. a_loadmm_reg_reg(list,fromsize,cgpara.def,reg,cgpara.location^.register,shuffle);
  1090. LOC_REFERENCE,LOC_CREFERENCE:
  1091. begin
  1092. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1093. a_loadmm_reg_ref(list,fromsize,cgpara.def,reg,href,shuffle);
  1094. end;
  1095. LOC_REGISTER,LOC_CREGISTER:
  1096. begin
  1097. if assigned(shuffle) and
  1098. not shufflescalar(shuffle) then
  1099. internalerror(2009112510);
  1100. a_loadmm_reg_intreg(list,deomsize,cgpara.def,reg,cgpara.location^.register,mms_movescalar);
  1101. end
  1102. else
  1103. internalerror(2010120427);
  1104. end;
  1105. end;
  1106. procedure thlcgobj.a_loadmm_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference; const cgpara: TCGPara; shuffle: pmmshuffle);
  1107. var
  1108. hr : tregister;
  1109. hs : tmmshuffle;
  1110. begin
  1111. cgpara.check_simple_location;
  1112. hr:=cg.getmmregister(list,cgpara.size);
  1113. a_loadmm_ref_reg(list,deomsize,cgpara.def,ref,hr,shuffle);
  1114. if realshuffle(shuffle) then
  1115. begin
  1116. hs:=shuffle^;
  1117. removeshuffles(hs);
  1118. a_loadmm_reg_cgpara(list,cgpara.def,hr,cgpara,@hs);
  1119. end
  1120. else
  1121. a_loadmm_reg_cgpara(list,cgpara.def,hr,cgpara,shuffle);
  1122. end;
  1123. procedure thlcgobj.a_loadmm_loc_cgpara(list: TAsmList; fromsize: tdef; const loc: tlocation; const cgpara: TCGPara; shuffle: pmmshuffle);
  1124. begin
  1125. {$ifdef extdebug}
  1126. if def_cgsize(fromsize)<>loc.size then
  1127. internalerror(2010112105);
  1128. {$endif}
  1129. cg.a_loadmm_loc_cgpara(list,loc,cgpara,shuffle);
  1130. end;
  1131. procedure thlcgobj.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle);
  1132. begin
  1133. cg.a_opmm_reg_reg(list,op,def_cgsize(size),src,dst,shuffle);
  1134. end;
  1135. procedure thlcgobj.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  1136. begin
  1137. cg.a_opmm_ref_reg(list,op,def_cgsize(size),ref,reg,shuffle)
  1138. end;
  1139. procedure thlcgobj.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size: tdef; const loc: tlocation; reg: tregister; shuffle: pmmshuffle);
  1140. begin
  1141. cg.a_opmm_loc_reg(list,op,def_cgsize(size),loc,reg,shuffle);
  1142. end;
  1143. procedure thlcgobj.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  1144. begin
  1145. cg.a_opmm_reg_ref(list,op,def_cgsize(size),reg,ref,shuffle);
  1146. end;
  1147. *)
  1148. (*
  1149. procedure thlcgobj.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle);
  1150. begin
  1151. cg.a_loadmm_intreg_reg(list,def_cgsize(fromsize),def_cgsize(tosize),intreg,mmreg,shuffle);
  1152. end;
  1153. procedure thlcgobj.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle);
  1154. begin
  1155. cg.a_loadmm_reg_intreg(list,def_cgsize(fromsize),def_cgsize(tosize),mmreg,intreg,shuffle);
  1156. end;
  1157. *)
  1158. procedure thlcgobj.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference);
  1159. var
  1160. tmpreg : tregister;
  1161. begin
  1162. tmpreg:=getintregister(list,size);
  1163. a_load_ref_reg(list,size,size,ref,tmpreg);
  1164. a_op_const_reg(list,op,size,a,tmpreg);
  1165. a_load_reg_ref(list,size,size,tmpreg,ref);
  1166. end;
  1167. procedure thlcgobj.a_op_const_subsetreg(list: TAsmList; Op: TOpCG; size, subsetsize: tdef; a: aint; const sreg: tsubsetregister);
  1168. var
  1169. tmpreg: tregister;
  1170. begin
  1171. tmpreg:=getintregister(list,size);
  1172. a_load_subsetreg_reg(list,size,subsetsize,size,sreg,tmpreg);
  1173. a_op_const_reg(list,op,size,a,tmpreg);
  1174. a_load_reg_subsetreg(list,size,size,subsetsize,tmpreg,sreg);
  1175. end;
  1176. procedure thlcgobj.a_op_const_subsetref(list: TAsmList; Op: TOpCG; size, subsetsize: tdef; a: aint; const sref: tsubsetreference);
  1177. var
  1178. tmpreg: tregister;
  1179. begin
  1180. tmpreg:=getintregister(list,size);
  1181. a_load_subsetref_reg(list,size,subsetsize,size,sref,tmpreg);
  1182. a_op_const_reg(list,op,size,a,tmpreg);
  1183. a_load_reg_subsetref(list,size,size,subsetsize,tmpreg,sref);
  1184. end;
  1185. procedure thlcgobj.a_op_const_loc(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const loc: tlocation);
  1186. begin
  1187. case loc.loc of
  1188. LOC_REGISTER, LOC_CREGISTER:
  1189. a_op_const_reg(list,op,size,a,loc.register);
  1190. LOC_REFERENCE, LOC_CREFERENCE:
  1191. a_op_const_ref(list,op,size,a,loc.reference);
  1192. { we don't have enough type information to handle these here
  1193. LOC_SUBSETREG, LOC_CSUBSETREG:
  1194. a_op_const_subsetreg(list,op,loc.size,loc.size,a,loc.sreg);
  1195. LOC_SUBSETREF, LOC_CSUBSETREF:
  1196. a_op_const_subsetref(list,op,loc.size,loc.size,a,loc.sref);
  1197. }
  1198. else
  1199. internalerror(2010120428);
  1200. end;
  1201. end;
  1202. procedure thlcgobj.a_op_reg_ref(list: TAsmList; Op: TOpCG; size: tdef; reg: TRegister; const ref: TReference);
  1203. var
  1204. tmpreg: tregister;
  1205. begin
  1206. tmpreg:=getintregister(list,size);
  1207. a_load_ref_reg(list,size,size,ref,tmpreg);
  1208. case op of
  1209. OP_NOT,OP_NEG:
  1210. begin
  1211. a_op_reg_reg(list,op,size,tmpreg,tmpreg);
  1212. end;
  1213. else
  1214. begin
  1215. a_op_reg_reg(list,op,size,reg,tmpreg);
  1216. end;
  1217. end;
  1218. a_load_reg_ref(list,size,size,tmpreg,ref);
  1219. end;
  1220. procedure thlcgobj.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  1221. var
  1222. tmpreg: tregister;
  1223. begin
  1224. case op of
  1225. OP_NOT,OP_NEG:
  1226. { handle it as "load ref,reg; op reg" }
  1227. begin
  1228. a_load_ref_reg(list,size,size,ref,reg);
  1229. a_op_reg_reg(list,op,size,reg,reg);
  1230. end;
  1231. else
  1232. begin
  1233. tmpreg:=getintregister(list,size);
  1234. a_load_ref_reg(list,size,size,ref,tmpreg);
  1235. a_op_reg_reg(list,op,size,tmpreg,reg);
  1236. end;
  1237. end;
  1238. end;
  1239. procedure thlcgobj.a_op_reg_subsetreg(list: TAsmList; Op: TOpCG; opsize, destsize, destsubsetsize: tdef; reg: TRegister; const sreg: tsubsetregister);
  1240. var
  1241. tmpreg: tregister;
  1242. begin
  1243. tmpreg:=getintregister(list,opsize);
  1244. a_load_subsetreg_reg(list,destsize,destsubsetsize,opsize,sreg,tmpreg);
  1245. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  1246. a_load_reg_subsetreg(list,opsize,destsize,destsubsetsize,tmpreg,sreg);
  1247. end;
  1248. procedure thlcgobj.a_op_reg_subsetref(list: TAsmList; Op: TOpCG; opsize, destsize, destsubsetsize: tdef; reg: TRegister; const sref: tsubsetreference);
  1249. var
  1250. tmpreg: tregister;
  1251. begin
  1252. tmpreg:=getintregister(list,opsize);
  1253. a_load_subsetref_reg(list,destsize,destsubsetsize,opsize,sref,tmpreg);
  1254. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  1255. a_load_reg_subsetref(list,opsize,destsize,destsubsetsize,tmpreg,sref);
  1256. end;
  1257. procedure thlcgobj.a_op_reg_loc(list: TAsmList; Op: TOpCG; size: tdef; reg: tregister; const loc: tlocation);
  1258. begin
  1259. case loc.loc of
  1260. LOC_REGISTER, LOC_CREGISTER:
  1261. a_op_reg_reg(list,op,size,reg,loc.register);
  1262. LOC_REFERENCE, LOC_CREFERENCE:
  1263. a_op_reg_ref(list,op,size,reg,loc.reference);
  1264. { we don't have enough type information to handle these here
  1265. LOC_SUBSETREG, LOC_CSUBSETREG:
  1266. a_op_reg_subsetreg(list,op,loc.size,loc.size,reg,loc.sreg);
  1267. LOC_SUBSETREF, LOC_CSUBSETREF:
  1268. a_op_reg_subsetref(list,op,loc.size,loc.size,reg,loc.sref);
  1269. }
  1270. else
  1271. internalerror(2010120429);
  1272. end;
  1273. end;
  1274. procedure thlcgobj.a_op_ref_loc(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; const loc: tlocation);
  1275. var
  1276. tmpreg: tregister;
  1277. begin
  1278. case loc.loc of
  1279. LOC_REGISTER,LOC_CREGISTER:
  1280. a_op_ref_reg(list,op,size,ref,loc.register);
  1281. LOC_REFERENCE,LOC_CREFERENCE:
  1282. begin
  1283. tmpreg:=getintregister(list,size);
  1284. a_load_ref_reg(list,size,size,ref,tmpreg);
  1285. a_op_reg_ref(list,op,size,tmpreg,loc.reference);
  1286. end;
  1287. { we don't have enough type information to handle these here
  1288. LOC_SUBSETREG, LOC_CSUBSETREG:
  1289. begin
  1290. tmpreg:=getintregister(list,loc.size);
  1291. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  1292. a_op_ref_reg(list,op,loc.size,ref,tmpreg);
  1293. a_load_reg_subsetreg(list,loc.size,loc.size,tmpreg,loc.sreg);
  1294. end;
  1295. LOC_SUBSETREF, LOC_CSUBSETREF:
  1296. begin
  1297. tmpreg:=getintregister(list,loc.size);
  1298. a_load_subsetreF_reg(list,loc.size,loc.size,loc.sref,tmpreg);
  1299. a_op_ref_reg(list,op,loc.size,ref,tmpreg);
  1300. a_load_reg_subsetref(list,loc.size,loc.size,tmpreg,loc.sref);
  1301. end;
  1302. }
  1303. else
  1304. internalerror(2010120429);
  1305. end;
  1306. end;
  1307. procedure thlcgobj.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister);
  1308. begin
  1309. a_load_reg_reg(list,size,size,src,dst);
  1310. a_op_const_reg(list,op,size,a,dst);
  1311. end;
  1312. procedure thlcgobj.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  1313. var
  1314. tmpreg: tregister;
  1315. begin
  1316. if (dst<>src1) then
  1317. begin
  1318. a_load_reg_reg(list,size,size,src2,dst);
  1319. a_op_reg_reg(list,op,size,src1,dst);
  1320. end
  1321. else
  1322. begin
  1323. { can we do a direct operation on the target register ? }
  1324. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  1325. a_op_reg_reg(list,op,size,src2,dst)
  1326. else
  1327. begin
  1328. tmpreg:=getintregister(list,size);
  1329. a_load_reg_reg(list,size,size,src2,tmpreg);
  1330. a_op_reg_reg(list,op,size,src1,tmpreg);
  1331. a_load_reg_reg(list,size,size,tmpreg,dst);
  1332. end;
  1333. end;
  1334. end;
  1335. procedure thlcgobj.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1336. begin
  1337. if not setflags then
  1338. a_op_const_reg_reg(list,op,size,a,src,dst)
  1339. else
  1340. internalerror(2010122910);
  1341. end;
  1342. procedure thlcgobj.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1343. begin
  1344. if not setflags then
  1345. a_op_reg_reg_reg(list,op,size,src1,src2,dst)
  1346. else
  1347. internalerror(2010122911);
  1348. end;
  1349. procedure thlcgobj.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  1350. var
  1351. tmpreg: tregister;
  1352. begin
  1353. tmpreg:=getintregister(list,size);
  1354. a_load_const_reg(list,size,a,tmpreg);
  1355. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1356. end;
  1357. procedure thlcgobj.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const ref: treference; l: tasmlabel);
  1358. var
  1359. tmpreg: tregister;
  1360. begin
  1361. tmpreg:=getintregister(list,size);
  1362. a_load_ref_reg(list,size,size,ref,tmpreg);
  1363. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1364. end;
  1365. procedure thlcgobj.a_cmp_const_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const loc: tlocation; l: tasmlabel);
  1366. begin
  1367. case loc.loc of
  1368. LOC_REGISTER,LOC_CREGISTER:
  1369. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1370. LOC_REFERENCE,LOC_CREFERENCE:
  1371. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1372. { we don't have enough type information to handle these here
  1373. LOC_SUBSETREG, LOC_CSUBSETREG:
  1374. begin
  1375. tmpreg:=getintregister(list,size);
  1376. a_load_subsetreg_reg(list,loc.size,size,loc.sreg,tmpreg);
  1377. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1378. end;
  1379. LOC_SUBSETREF, LOC_CSUBSETREF:
  1380. begin
  1381. tmpreg:=getintregister(list,size);
  1382. a_load_subsetref_reg(list,loc.size,size,loc.sref,tmpreg);
  1383. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1384. end;
  1385. }
  1386. else
  1387. internalerror(2010120430);
  1388. end;
  1389. end;
  1390. procedure thlcgobj.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  1391. var
  1392. tmpreg: tregister;
  1393. begin
  1394. tmpreg:=getintregister(list,size);
  1395. a_load_ref_reg(list,size,size,ref,tmpreg);
  1396. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1397. end;
  1398. procedure thlcgobj.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  1399. var
  1400. tmpreg: tregister;
  1401. begin
  1402. tmpreg:=getintregister(list,size);
  1403. a_load_ref_reg(list,size,size,ref,tmpreg);
  1404. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  1405. end;
  1406. procedure thlcgobj.a_cmp_subsetreg_reg_label(list: TAsmList; fromsize, fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; l: tasmlabel);
  1407. var
  1408. tmpreg: tregister;
  1409. begin
  1410. tmpreg:=getintregister(list,cmpsize);
  1411. a_load_subsetreg_reg(list,fromsize,fromsubsetsize,cmpsize,sreg,tmpreg);
  1412. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  1413. end;
  1414. procedure thlcgobj.a_cmp_subsetref_reg_label(list: TAsmList; fromsize, fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; l: tasmlabel);
  1415. var
  1416. tmpreg: tregister;
  1417. begin
  1418. tmpreg:=getintregister(list,cmpsize);
  1419. a_load_subsetref_reg(list,fromsize,fromsubsetsize,cmpsize,sref,tmpreg);
  1420. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  1421. end;
  1422. procedure thlcgobj.a_cmp_loc_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const loc: tlocation; reg: tregister; l: tasmlabel);
  1423. begin
  1424. case loc.loc of
  1425. LOC_REGISTER,
  1426. LOC_CREGISTER:
  1427. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1428. LOC_REFERENCE,
  1429. LOC_CREFERENCE :
  1430. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1431. LOC_CONSTANT:
  1432. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1433. { we don't have enough type information to handle these here
  1434. LOC_SUBSETREG,
  1435. LOC_CSUBSETREG:
  1436. a_cmp_subsetreg_reg_label(list,loc.size,size,cmp_op,loc.sreg,reg,l);
  1437. LOC_SUBSETREF,
  1438. LOC_CSUBSETREF:
  1439. a_cmp_subsetref_reg_label(list,loc.size,size,cmp_op,loc.sref,reg,l);
  1440. }
  1441. else
  1442. internalerror(2010120431);
  1443. end;
  1444. end;
  1445. procedure thlcgobj.a_cmp_reg_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const loc: tlocation; l: tasmlabel);
  1446. begin
  1447. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  1448. end;
  1449. procedure thlcgobj.a_cmp_ref_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; const loc: tlocation; l: tasmlabel);
  1450. var
  1451. tmpreg: tregister;
  1452. begin
  1453. case loc.loc of
  1454. LOC_REGISTER,LOC_CREGISTER:
  1455. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1456. LOC_REFERENCE,LOC_CREFERENCE:
  1457. begin
  1458. tmpreg:=getintregister(list,size);
  1459. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1460. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1461. end;
  1462. LOC_CONSTANT:
  1463. begin
  1464. a_cmp_const_ref_label(list,size,swap_opcmp(cmp_op),loc.value,ref,l);
  1465. end
  1466. { we don't have enough type information to handle these here
  1467. LOC_SUBSETREG, LOC_CSUBSETREG:
  1468. begin
  1469. tmpreg:=getintregister(list, size);
  1470. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1471. a_cmp_subsetreg_reg_label(list,loc.size,size,swap_opcmp(cmp_op),loc.sreg,tmpreg,l);
  1472. end;
  1473. LOC_SUBSETREF, LOC_CSUBSETREF:
  1474. begin
  1475. tmpreg:=getintregister(list, size);
  1476. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1477. a_cmp_subsetref_reg_label(list,loc.size,size,swap_opcmp(cmp_op),loc.sref,tmpreg,l);
  1478. end;
  1479. }
  1480. else
  1481. internalerror(2010120432);
  1482. end;
  1483. end;
  1484. procedure thlcgobj.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  1485. begin
  1486. {
  1487. if use_vectorfpu(size) then
  1488. a_loadmm_ref_ref()
  1489. else
  1490. }
  1491. if size.typ<>floatdef then
  1492. a_load_ref_ref(list,size,size,source,dest)
  1493. else
  1494. a_loadfpu_ref_ref(list,size,size,source,dest);
  1495. end;
  1496. procedure thlcgobj.g_concatcopy_unaligned(list: TAsmList; size: tdef; const source, dest: treference);
  1497. begin
  1498. g_concatcopy(list,size,source,dest);
  1499. end;
  1500. procedure thlcgobj.g_rangecheck(list: TAsmList; const l: tlocation; fromdef, todef: tdef);
  1501. var
  1502. {$if defined(cpu64bitalu) or defined(cpu32bitalu)}
  1503. aintmax: aint;
  1504. {$else}
  1505. aintmax: longint;
  1506. {$endif}
  1507. neglabel : tasmlabel;
  1508. hreg : tregister;
  1509. lto,hto,
  1510. lfrom,hfrom : TConstExprInt;
  1511. fromsize, tosize: cardinal;
  1512. maxdef: tdef;
  1513. from_signed, to_signed: boolean;
  1514. begin
  1515. { range checking on and range checkable value? }
  1516. if not(cs_check_range in current_settings.localswitches) or
  1517. not(fromdef.typ in [orddef,enumdef]) or
  1518. { C-style booleans can't really fail range checks, }
  1519. { all values are always valid }
  1520. is_cbool(todef) then
  1521. exit;
  1522. { only check when assigning to scalar, subranges are different, }
  1523. { when todef=fromdef then the check is always generated }
  1524. getrange(fromdef,lfrom,hfrom);
  1525. getrange(todef,lto,hto);
  1526. from_signed := is_signed(fromdef);
  1527. to_signed := is_signed(todef);
  1528. { check the rangedef of the array, not the array itself }
  1529. { (only change now, since getrange needs the arraydef) }
  1530. if (todef.typ = arraydef) then
  1531. todef := tarraydef(todef).rangedef;
  1532. { no range check if from and to are equal and are both longint/dword }
  1533. { (if we have a 32bit processor) or int64/qword, since such }
  1534. { operations can at most cause overflows (JM) }
  1535. { Note that these checks are mostly processor independent, they only }
  1536. { have to be changed once we introduce 64bit subrange types }
  1537. if (fromdef = todef) and
  1538. (fromdef.typ=orddef) and
  1539. (((((torddef(fromdef).ordtype = s64bit) and
  1540. (lfrom = low(int64)) and
  1541. (hfrom = high(int64))) or
  1542. ((torddef(fromdef).ordtype = u64bit) and
  1543. (lfrom = low(qword)) and
  1544. (hfrom = high(qword))) or
  1545. ((torddef(fromdef).ordtype = scurrency) and
  1546. (lfrom = low(int64)) and
  1547. (hfrom = high(int64)))))) then
  1548. exit;
  1549. { 32 bit operations are automatically widened to 64 bit on 64 bit addr
  1550. targets }
  1551. {$ifdef cpu32bitaddr}
  1552. if (fromdef = todef) and
  1553. (fromdef.typ=orddef) and
  1554. (((((torddef(fromdef).ordtype = s32bit) and
  1555. (lfrom = int64(low(longint))) and
  1556. (hfrom = int64(high(longint)))) or
  1557. ((torddef(fromdef).ordtype = u32bit) and
  1558. (lfrom = low(cardinal)) and
  1559. (hfrom = high(cardinal)))))) then
  1560. exit;
  1561. {$endif cpu32bitaddr}
  1562. { optimize some range checks away in safe cases }
  1563. fromsize := fromdef.size;
  1564. tosize := todef.size;
  1565. if ((from_signed = to_signed) or
  1566. (not from_signed)) and
  1567. (lto<=lfrom) and (hto>=hfrom) and
  1568. (fromsize <= tosize) then
  1569. begin
  1570. { if fromsize < tosize, and both have the same signed-ness or }
  1571. { fromdef is unsigned, then all bit patterns from fromdef are }
  1572. { valid for todef as well }
  1573. if (fromsize < tosize) then
  1574. exit;
  1575. if (fromsize = tosize) and
  1576. (from_signed = to_signed) then
  1577. { only optimize away if all bit patterns which fit in fromsize }
  1578. { are valid for the todef }
  1579. begin
  1580. {$ifopt Q+}
  1581. {$define overflowon}
  1582. {$Q-}
  1583. {$endif}
  1584. {$ifopt R+}
  1585. {$define rangeon}
  1586. {$R-}
  1587. {$endif}
  1588. if to_signed then
  1589. begin
  1590. { calculation of the low/high ranges must not overflow 64 bit
  1591. otherwise we end up comparing with zero for 64 bit data types on
  1592. 64 bit processors }
  1593. if (lto = (int64(-1) << (tosize * 8 - 1))) and
  1594. (hto = (-((int64(-1) << (tosize * 8 - 1))+1))) then
  1595. exit
  1596. end
  1597. else
  1598. begin
  1599. { calculation of the low/high ranges must not overflow 64 bit
  1600. otherwise we end up having all zeros for 64 bit data types on
  1601. 64 bit processors }
  1602. if (lto = 0) and
  1603. (qword(hto) = (qword(-1) >> (64-(tosize * 8))) ) then
  1604. exit
  1605. end;
  1606. {$ifdef overflowon}
  1607. {$Q+}
  1608. {$undef overflowon}
  1609. {$endif}
  1610. {$ifdef rangeon}
  1611. {$R+}
  1612. {$undef rangeon}
  1613. {$endif}
  1614. end
  1615. end;
  1616. { depending on the types involved, we perform the range check for 64 or
  1617. for 32 bit }
  1618. if fromsize=8 then
  1619. maxdef:=fromdef
  1620. else
  1621. maxdef:=todef;
  1622. {$if sizeof(aintmax) = 8}
  1623. if maxdef.size=8 then
  1624. aintmax:=high(int64)
  1625. else
  1626. {$endif}
  1627. begin
  1628. aintmax:=high(longint);
  1629. maxdef:=u32inttype;
  1630. end;
  1631. { generate the rangecheck code for the def where we are going to }
  1632. { store the result }
  1633. { use the trick that }
  1634. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  1635. { To be able to do that, we have to make sure however that either }
  1636. { fromdef and todef are both signed or unsigned, or that we leave }
  1637. { the parts < 0 and > maxlongint out }
  1638. if from_signed xor to_signed then
  1639. begin
  1640. if from_signed then
  1641. { from is signed, to is unsigned }
  1642. begin
  1643. { if high(from) < 0 -> always range error }
  1644. if (hfrom < 0) or
  1645. { if low(to) > maxlongint also range error }
  1646. (lto > aintmax) then
  1647. begin
  1648. g_call_system_proc(list,'fpc_rangeerror');
  1649. exit
  1650. end;
  1651. { from is signed and to is unsigned -> when looking at to }
  1652. { as an signed value, it must be < maxaint (otherwise }
  1653. { it will become negative, which is invalid since "to" is unsigned) }
  1654. if hto > aintmax then
  1655. hto := aintmax;
  1656. end
  1657. else
  1658. { from is unsigned, to is signed }
  1659. begin
  1660. if (lfrom > aintmax) or
  1661. (hto < 0) then
  1662. begin
  1663. g_call_system_proc(list,'fpc_rangeerror');
  1664. exit
  1665. end;
  1666. { from is unsigned and to is signed -> when looking at to }
  1667. { as an unsigned value, it must be >= 0 (since negative }
  1668. { values are the same as values > maxlongint) }
  1669. if lto < 0 then
  1670. lto := 0;
  1671. end;
  1672. end;
  1673. hreg:=getintregister(list,maxdef);
  1674. a_load_loc_reg(list,fromdef,maxdef,l,hreg);
  1675. a_op_const_reg(list,OP_SUB,maxdef,tcgint(int64(lto)),hreg);
  1676. current_asmdata.getjumplabel(neglabel);
  1677. {
  1678. if from_signed then
  1679. a_cmp_const_reg_label(list,OS_INT,OC_GTE,aint(hto-lto),hreg,neglabel)
  1680. else
  1681. }
  1682. if qword(hto-lto)>qword(aintmax) then
  1683. a_cmp_const_reg_label(list,maxdef,OC_BE,aintmax,hreg,neglabel)
  1684. else
  1685. a_cmp_const_reg_label(list,maxdef,OC_BE,tcgint(int64(hto-lto)),hreg,neglabel);
  1686. g_call_system_proc(list,'fpc_rangeerror');
  1687. a_label(list,neglabel);
  1688. end;
  1689. procedure thlcgobj.g_profilecode(list: TAsmList);
  1690. begin
  1691. end;
  1692. procedure thlcgobj.g_allocload_reg_reg(list: TAsmList; regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  1693. begin
  1694. case regtyp of
  1695. R_INTREGISTER:
  1696. toreg:=getintregister(list,regsize);
  1697. R_ADDRESSREGISTER:
  1698. toreg:=getaddressregister(list,regsize);
  1699. R_FPUREGISTER:
  1700. toreg:=getfpuregister(list,regsize);
  1701. end;
  1702. a_load_reg_reg(list,regsize,regsize,fromreg,toreg);
  1703. end;
  1704. procedure thlcgobj.g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation);
  1705. procedure handle_reg_move(regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  1706. begin
  1707. case regtyp of
  1708. R_INTREGISTER:
  1709. toreg:=getintregister(list,regsize);
  1710. R_ADDRESSREGISTER:
  1711. toreg:=getaddressregister(list,regsize);
  1712. R_FPUREGISTER:
  1713. toreg:=getfpuregister(list,regsize);
  1714. end;
  1715. a_load_reg_reg(list,regsize,regsize,fromreg,toreg);
  1716. end;
  1717. begin
  1718. toloc:=fromloc;
  1719. case fromloc.loc of
  1720. { volatile location, can't get a permanent reference }
  1721. LOC_REGISTER,
  1722. LOC_FPUREGISTER:
  1723. internalerror(2012012702);
  1724. LOC_CONSTANT:
  1725. { finished }
  1726. ;
  1727. LOC_CREGISTER:
  1728. handle_reg_move(def,fromloc.reference.index,toloc.reference.index,R_INTREGISTER);
  1729. LOC_CFPUREGISTER:
  1730. handle_reg_move(def,fromloc.reference.index,toloc.reference.index,R_FPUREGISTER);
  1731. { although LOC_CREFERENCE cannot be an lvalue, we may want to take a
  1732. reference to such a location for multiple reading }
  1733. LOC_CREFERENCE,
  1734. LOC_REFERENCE:
  1735. begin
  1736. if (fromloc.reference.base<>NR_NO) and
  1737. (fromloc.reference.base<>current_procinfo.framepointer) and
  1738. (fromloc.reference.base<>NR_STACK_POINTER_REG) then
  1739. handle_reg_move(voidpointertype,fromloc.reference.base,toloc.reference.base,getregtype(fromloc.reference.base));
  1740. if (fromloc.reference.index<>NR_NO) and
  1741. (fromloc.reference.index<>current_procinfo.framepointer) and
  1742. (fromloc.reference.index<>NR_STACK_POINTER_REG) then
  1743. handle_reg_move(voidpointertype,fromloc.reference.index,toloc.reference.index,getregtype(fromloc.reference.index));
  1744. end;
  1745. else
  1746. internalerror(2012012701);
  1747. end;
  1748. end;
  1749. procedure thlcgobj.location_force_reg(list: TAsmList; var l: tlocation; src_size, dst_size: tdef; maybeconst: boolean);
  1750. var
  1751. hregister,
  1752. hregister2: tregister;
  1753. hl : tasmlabel;
  1754. oldloc : tlocation;
  1755. begin
  1756. oldloc:=l;
  1757. hregister:=getregisterfordef(list,dst_size);
  1758. { load value in new register }
  1759. case l.loc of
  1760. {$ifdef cpuflags}
  1761. LOC_FLAGS :
  1762. g_flags2reg(list,dst_size,l.resflags,hregister);
  1763. {$endif cpuflags}
  1764. LOC_JUMP :
  1765. begin
  1766. a_label(list,current_procinfo.CurrTrueLabel);
  1767. a_load_const_reg(list,dst_size,1,hregister);
  1768. current_asmdata.getjumplabel(hl);
  1769. a_jmp_always(list,hl);
  1770. a_label(list,current_procinfo.CurrFalseLabel);
  1771. a_load_const_reg(list,dst_size,0,hregister);
  1772. a_label(list,hl);
  1773. end;
  1774. else
  1775. begin
  1776. { load_loc_reg can only handle size >= l.size, when the
  1777. new size is smaller then we need to adjust the size
  1778. of the orignal and maybe recalculate l.register for i386 }
  1779. if (dst_size.size<src_size.size) then
  1780. begin
  1781. hregister2:=getregisterfordef(list,src_size);
  1782. { prevent problems with memory locations -- at this high
  1783. level we cannot twiddle with the reference offset, since
  1784. that may not mean anything (e.g., it refers to fixed-sized
  1785. stack slots on Java) }
  1786. a_load_loc_reg(list,src_size,src_size,l,hregister2);
  1787. a_load_reg_reg(list,src_size,dst_size,hregister2,hregister);
  1788. end
  1789. else
  1790. a_load_loc_reg(list,src_size,dst_size,l,hregister);
  1791. end;
  1792. end;
  1793. if (l.loc <> LOC_CREGISTER) or
  1794. not maybeconst then
  1795. location_reset(l,LOC_REGISTER,def_cgsize(dst_size))
  1796. else
  1797. location_reset(l,LOC_CREGISTER,def_cgsize(dst_size));
  1798. l.register:=hregister;
  1799. { Release temp if it was a reference }
  1800. if oldloc.loc=LOC_REFERENCE then
  1801. location_freetemp(list,oldloc);
  1802. end;
  1803. procedure thlcgobj.location_force_fpureg(list: TAsmList; var l: tlocation; size: tdef; maybeconst: boolean);
  1804. var
  1805. reg : tregister;
  1806. begin
  1807. if (l.loc<>LOC_FPUREGISTER) and
  1808. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  1809. begin
  1810. { if it's in an mm register, store to memory first }
  1811. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  1812. internalerror(2011012903);
  1813. reg:=getfpuregister(list,size);
  1814. a_loadfpu_loc_reg(list,size,size,l,reg);
  1815. location_freetemp(list,l);
  1816. location_reset(l,LOC_FPUREGISTER,l.size);
  1817. l.register:=reg;
  1818. end;
  1819. end;
  1820. procedure thlcgobj.location_force_mem(list: TAsmList; var l: tlocation; size: tdef);
  1821. var
  1822. r : treference;
  1823. forcesize: aint;
  1824. begin
  1825. case l.loc of
  1826. LOC_FPUREGISTER,
  1827. LOC_CFPUREGISTER :
  1828. begin
  1829. tg.gethltemp(list,size,size.size,tt_normal,r);
  1830. a_loadfpu_reg_ref(list,size,size,l.register,r);
  1831. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1832. l.reference:=r;
  1833. end;
  1834. (*
  1835. LOC_MMREGISTER,
  1836. LOC_CMMREGISTER:
  1837. begin
  1838. tg.gethltemp(list,size,size.size,tt_normal,r);
  1839. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  1840. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1841. l.reference:=r;
  1842. end;
  1843. *)
  1844. LOC_CONSTANT,
  1845. LOC_REGISTER,
  1846. LOC_CREGISTER :
  1847. begin
  1848. if not is_dynamic_array(size) and
  1849. not is_open_array(size) then
  1850. forcesize:=size.size
  1851. else
  1852. forcesize:=voidpointertype.size;
  1853. tg.gethltemp(list,size,forcesize,tt_normal,r);
  1854. a_load_loc_ref(list,size,size,l,r);
  1855. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1856. l.reference:=r;
  1857. end;
  1858. (*
  1859. LOC_SUBSETREG,
  1860. LOC_CSUBSETREG,
  1861. LOC_SUBSETREF,
  1862. LOC_CSUBSETREF:
  1863. begin
  1864. tg.gethltemp(list,size,size.size,tt_normal,r);
  1865. cg.a_load_loc_ref(list,l.size,l,r);
  1866. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1867. l.reference:=r;
  1868. end;
  1869. *)
  1870. LOC_CREFERENCE,
  1871. LOC_REFERENCE : ;
  1872. else
  1873. internalerror(2011010304);
  1874. end;
  1875. end;
  1876. procedure thlcgobj.location_get_data_ref(list: TAsmList; def: tdef; const l: tlocation; var ref: treference; loadref: boolean; alignment: longint);
  1877. begin
  1878. case l.loc of
  1879. LOC_REGISTER,
  1880. LOC_CREGISTER :
  1881. begin
  1882. if not loadref then
  1883. internalerror(200410231);
  1884. reference_reset_base(ref,l.register,0,alignment);
  1885. end;
  1886. LOC_REFERENCE,
  1887. LOC_CREFERENCE :
  1888. begin
  1889. if loadref then
  1890. begin
  1891. reference_reset_base(ref,getaddressregister(list,voidpointertype),0,alignment);
  1892. { it's a pointer to def }
  1893. a_load_ref_reg(list,voidpointertype,voidpointertype,l.reference,ref.base);
  1894. end
  1895. else
  1896. ref:=l.reference;
  1897. end;
  1898. else
  1899. internalerror(200309181);
  1900. end;
  1901. end;
  1902. procedure thlcgobj.maketojumpbool(list: TAsmList; p: tnode);
  1903. {
  1904. produces jumps to true respectively false labels using boolean expressions
  1905. depending on whether the loading of regvars is currently being
  1906. synchronized manually (such as in an if-node) or automatically (most of
  1907. the other cases where this procedure is called), loadregvars can be
  1908. "lr_load_regvars" or "lr_dont_load_regvars"
  1909. }
  1910. var
  1911. storepos : tfileposinfo;
  1912. begin
  1913. if nf_error in p.flags then
  1914. exit;
  1915. storepos:=current_filepos;
  1916. current_filepos:=p.fileinfo;
  1917. if is_boolean(p.resultdef) then
  1918. begin
  1919. if is_constboolnode(p) then
  1920. begin
  1921. if Tordconstnode(p).value.uvalue<>0 then
  1922. a_jmp_always(list,current_procinfo.CurrTrueLabel)
  1923. else
  1924. a_jmp_always(list,current_procinfo.CurrFalseLabel)
  1925. end
  1926. else
  1927. begin
  1928. case p.location.loc of
  1929. (*
  1930. LOC_SUBSETREG,LOC_CSUBSETREG,
  1931. LOC_SUBSETREF,LOC_CSUBSETREF:
  1932. begin
  1933. tmpreg := cg.getintregister(list,OS_INT);
  1934. cg.a_load_loc_reg(list,OS_INT,p.location,tmpreg);
  1935. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,tmpreg,current_procinfo.CurrTrueLabel);
  1936. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  1937. end;
  1938. *)
  1939. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  1940. begin
  1941. a_cmp_const_loc_label(list,p.resultdef,OC_NE,0,p.location,current_procinfo.CurrTrueLabel);
  1942. a_jmp_always(list,current_procinfo.CurrFalseLabel);
  1943. end;
  1944. LOC_JUMP:
  1945. ;
  1946. {$ifdef cpuflags}
  1947. LOC_FLAGS :
  1948. begin
  1949. a_jmp_flags(list,p.location.resflags,current_procinfo.CurrTrueLabel);
  1950. a_jmp_always(list,current_procinfo.CurrFalseLabel);
  1951. end;
  1952. {$endif cpuflags}
  1953. else
  1954. begin
  1955. printnode(output,p);
  1956. internalerror(2011010418);
  1957. end;
  1958. end;
  1959. end;
  1960. end
  1961. else
  1962. internalerror(2011010419);
  1963. current_filepos:=storepos;
  1964. end;
  1965. procedure thlcgobj.gen_proc_symbol(list: TAsmList);
  1966. var
  1967. item,
  1968. previtem : TCmdStrListItem;
  1969. begin
  1970. previtem:=nil;
  1971. item := TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
  1972. while assigned(item) do
  1973. begin
  1974. {$ifdef arm}
  1975. if current_settings.cputype in cpu_thumb2 then
  1976. list.concat(tai_thumb_func.create);
  1977. {$endif arm}
  1978. { "double link" all procedure entry symbols via .reference }
  1979. { directives on darwin, because otherwise the linker }
  1980. { sometimes strips the procedure if only on of the symbols }
  1981. { is referenced }
  1982. if assigned(previtem) and
  1983. (target_info.system in systems_darwin) then
  1984. list.concat(tai_directive.create(asd_reference,item.str));
  1985. if (cs_profile in current_settings.moduleswitches) or
  1986. (po_global in current_procinfo.procdef.procoptions) then
  1987. list.concat(Tai_symbol.createname_global(item.str,AT_FUNCTION,0))
  1988. else
  1989. list.concat(Tai_symbol.createname(item.str,AT_FUNCTION,0));
  1990. if assigned(previtem) and
  1991. (target_info.system in systems_darwin) then
  1992. list.concat(tai_directive.create(asd_reference,previtem.str));
  1993. if not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  1994. list.concat(Tai_function_name.create(item.str));
  1995. previtem:=item;
  1996. item := TCmdStrListItem(item.next);
  1997. end;
  1998. current_procinfo.procdef.procstarttai:=tai(list.last);
  1999. end;
  2000. procedure thlcgobj.gen_proc_symbol_end(list: TAsmList);
  2001. begin
  2002. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  2003. current_procinfo.procdef.procendtai:=tai(list.last);
  2004. if (current_module.islibrary) then
  2005. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  2006. { setinitname may generate a new section -> don't add to the
  2007. current list, because we assume this remains a text section }
  2008. exportlib.setinitname(current_asmdata.AsmLists[al_exports],current_procinfo.procdef.mangledname);
  2009. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  2010. begin
  2011. if (target_info.system in (systems_darwin+[system_powerpc_macos])) and
  2012. not(current_module.islibrary) then
  2013. begin
  2014. new_section(list,sec_code,'',4);
  2015. list.concat(tai_symbol.createname_global(
  2016. target_info.cprefix+mainaliasname,AT_FUNCTION,0));
  2017. { keep argc, argv and envp properly on the stack }
  2018. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  2019. end;
  2020. end;
  2021. end;
  2022. procedure thlcgobj.gen_initialize_code(list: TAsmList);
  2023. begin
  2024. { initialize local data like ansistrings }
  2025. case current_procinfo.procdef.proctypeoption of
  2026. potype_unitinit:
  2027. begin
  2028. { this is also used for initialization of variables in a
  2029. program which does not have a globalsymtable }
  2030. if assigned(current_module.globalsymtable) then
  2031. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@initialize_data,list);
  2032. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_data,list);
  2033. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  2034. end;
  2035. { units have seperate code for initilization and finalization }
  2036. potype_unitfinalize: ;
  2037. { program init/final is generated in separate procedure }
  2038. potype_proginit:
  2039. begin
  2040. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  2041. end;
  2042. else
  2043. current_procinfo.procdef.localst.SymList.ForEachCall(@initialize_data,list);
  2044. end;
  2045. { initialises temp. ansi/wide string data }
  2046. if (current_procinfo.procdef.proctypeoption<>potype_exceptfilter) then
  2047. inittempvariables(list);
  2048. {$ifdef OLDREGVARS}
  2049. load_regvars(list,nil);
  2050. {$endif OLDREGVARS}
  2051. end;
  2052. procedure thlcgobj.gen_finalize_code(list: TAsmList);
  2053. var
  2054. old_current_procinfo: tprocinfo;
  2055. begin
  2056. old_current_procinfo:=current_procinfo;
  2057. if (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  2058. begin
  2059. if (current_procinfo.parent.finalize_procinfo<>current_procinfo) then
  2060. exit;
  2061. current_procinfo:=current_procinfo.parent;
  2062. end;
  2063. {$ifdef OLDREGVARS}
  2064. cleanup_regvars(list);
  2065. {$endif OLDREGVARS}
  2066. { finalize temporary data }
  2067. finalizetempvariables(list);
  2068. { finalize local data like ansistrings}
  2069. case current_procinfo.procdef.proctypeoption of
  2070. potype_unitfinalize:
  2071. begin
  2072. { this is also used for initialization of variables in a
  2073. program which does not have a globalsymtable }
  2074. if assigned(current_module.globalsymtable) then
  2075. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@finalize_static_data,list);
  2076. TSymtable(current_module.localsymtable).SymList.ForEachCall(@finalize_static_data,list);
  2077. end;
  2078. { units/progs have separate code for initialization and finalization }
  2079. potype_unitinit: ;
  2080. { program init/final is generated in separate procedure }
  2081. potype_proginit: ;
  2082. else
  2083. current_procinfo.procdef.localst.SymList.ForEachCall(@finalize_local_vars,list);
  2084. end;
  2085. { finalize paras data }
  2086. if assigned(current_procinfo.procdef.parast) and
  2087. not(po_assembler in current_procinfo.procdef.procoptions) then
  2088. current_procinfo.procdef.parast.SymList.ForEachCall(@final_paras,list);
  2089. current_procinfo:=old_current_procinfo;
  2090. end;
  2091. procedure thlcgobj.gen_entry_code(list: TAsmList);
  2092. begin
  2093. { the actual profile code can clobber some registers,
  2094. therefore if the context must be saved, do it before
  2095. the actual call to the profile code
  2096. }
  2097. if (cs_profile in current_settings.moduleswitches) and
  2098. not(po_assembler in current_procinfo.procdef.procoptions) then
  2099. begin
  2100. { non-win32 can call mcout even in main }
  2101. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  2102. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  2103. begin
  2104. g_profilecode(list);
  2105. end;
  2106. end;
  2107. { TODO: create high level version (create compilerprocs in system unit,
  2108. look up procdef, use hlcgobj.a_call_name()) }
  2109. { call startup helpers from main program }
  2110. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  2111. begin
  2112. { initialize units }
  2113. cg.allocallcpuregisters(list);
  2114. if not(current_module.islibrary) then
  2115. cg.a_call_name(list,'FPC_INITIALIZEUNITS',false)
  2116. else
  2117. cg.a_call_name(list,'FPC_LIBINITIALIZEUNITS',false);
  2118. cg.deallocallcpuregisters(list);
  2119. end;
  2120. list.concat(Tai_force_line.Create);
  2121. {$ifdef OLDREGVARS}
  2122. load_regvars(list,nil);
  2123. {$endif OLDREGVARS}
  2124. end;
  2125. procedure thlcgobj.gen_exit_code(list: TAsmList);
  2126. begin
  2127. { TODO: create high level version (create compilerproc in system unit,
  2128. look up procdef, use hlcgobj.a_call_name()) }
  2129. { call __EXIT for main program }
  2130. if (not DLLsource) and
  2131. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  2132. cg.a_call_name(list,'FPC_DO_EXIT',false);
  2133. end;
  2134. procedure thlcgobj.inittempvariables(list: TAsmList);
  2135. var
  2136. hp : ptemprecord;
  2137. href : treference;
  2138. begin
  2139. hp:=tg.templist;
  2140. while assigned(hp) do
  2141. begin
  2142. if assigned(hp^.def) and
  2143. is_managed_type(hp^.def) then
  2144. begin
  2145. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  2146. g_initialize(list,hp^.def,href);
  2147. end;
  2148. hp:=hp^.next;
  2149. end;
  2150. end;
  2151. procedure thlcgobj.initialize_data(p: TObject; arg: pointer);
  2152. var
  2153. OldAsmList : TAsmList;
  2154. hp : tnode;
  2155. begin
  2156. if (tsym(p).typ = localvarsym) and
  2157. { local (procedure or unit) variables only need initialization if
  2158. they are used }
  2159. ((tabstractvarsym(p).refs>0) or
  2160. { managed return symbols must be inited }
  2161. ((tsym(p).typ=localvarsym) and (vo_is_funcret in tlocalvarsym(p).varoptions))
  2162. ) and
  2163. not(vo_is_typed_const in tabstractvarsym(p).varoptions) and
  2164. not(vo_is_external in tabstractvarsym(p).varoptions) and
  2165. not(vo_is_default_var in tabstractvarsym(p).varoptions) and
  2166. (is_managed_type(tabstractvarsym(p).vardef) or
  2167. ((m_iso in current_settings.modeswitches) and (tabstractvarsym(p).vardef.typ=filedef))
  2168. ) then
  2169. begin
  2170. OldAsmList:=current_asmdata.CurrAsmList;
  2171. current_asmdata.CurrAsmList:=TAsmList(arg);
  2172. hp:=cnodeutils.initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner),false);
  2173. firstpass(hp);
  2174. secondpass(hp);
  2175. hp.free;
  2176. current_asmdata.CurrAsmList:=OldAsmList;
  2177. end;
  2178. end;
  2179. procedure thlcgobj.finalizetempvariables(list: TAsmList);
  2180. var
  2181. hp : ptemprecord;
  2182. href : treference;
  2183. begin
  2184. hp:=tg.templist;
  2185. while assigned(hp) do
  2186. begin
  2187. if assigned(hp^.def) and
  2188. is_managed_type(hp^.def) then
  2189. begin
  2190. include(current_procinfo.flags,pi_needs_implicit_finally);
  2191. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  2192. g_finalize(list,hp^.def,href);
  2193. end;
  2194. hp:=hp^.next;
  2195. end;
  2196. end;
  2197. procedure thlcgobj.initialize_regvars(p: TObject; arg: pointer);
  2198. var
  2199. href : treference;
  2200. begin
  2201. if (tsym(p).typ=staticvarsym) then
  2202. begin
  2203. { Static variables can have the initialloc only set to LOC_CxREGISTER
  2204. or LOC_INVALID, for explaination see gen_alloc_symtable (PFV) }
  2205. case tstaticvarsym(p).initialloc.loc of
  2206. LOC_CREGISTER :
  2207. begin
  2208. {$ifndef cpu64bitalu}
  2209. if (tstaticvarsym(p).initialloc.size in [OS_64,OS_S64]) then
  2210. cg64.a_load64_const_reg(TAsmList(arg),0,tstaticvarsym(p).initialloc.register64)
  2211. else
  2212. {$endif not cpu64bitalu}
  2213. a_load_const_reg(TAsmList(arg),tstaticvarsym(p).vardef,0,
  2214. tstaticvarsym(p).initialloc.register);
  2215. end;
  2216. (*
  2217. LOC_CMMREGISTER :
  2218. { clear the whole register }
  2219. cg.a_opmm_reg_reg(TAsmList(arg),OP_XOR,reg_cgsize(tstaticvarsym(p).initialloc.register),
  2220. tstaticvarsym(p).initialloc.register,
  2221. tstaticvarsym(p).initialloc.register,
  2222. nil);
  2223. *)
  2224. LOC_CFPUREGISTER :
  2225. begin
  2226. { initialize fpu regvar by loading from memory }
  2227. reference_reset_symbol(href,
  2228. current_asmdata.RefAsmSymbol(tstaticvarsym(p).mangledname), 0,
  2229. var_align(tstaticvarsym(p).vardef.alignment));
  2230. a_loadfpu_ref_reg(TAsmList(arg), tstaticvarsym(p).vardef,
  2231. tstaticvarsym(p).vardef, href, tstaticvarsym(p).initialloc.register);
  2232. end;
  2233. LOC_INVALID :
  2234. ;
  2235. else
  2236. internalerror(200410124);
  2237. end;
  2238. end;
  2239. end;
  2240. procedure thlcgobj.finalize_sym(asmlist: TAsmList; sym: tsym);
  2241. var
  2242. hp : tnode;
  2243. OldAsmList : TAsmList;
  2244. begin
  2245. include(current_procinfo.flags,pi_needs_implicit_finally);
  2246. OldAsmList:=current_asmdata.CurrAsmList;
  2247. current_asmdata.CurrAsmList:=asmlist;
  2248. hp:=cloadnode.create(sym,sym.owner);
  2249. if (sym.typ=staticvarsym) and (vo_force_finalize in tstaticvarsym(sym).varoptions) then
  2250. include(tloadnode(hp).loadnodeflags,loadnf_isinternal_ignoreconst);
  2251. hp:=cnodeutils.finalize_data_node(hp);
  2252. firstpass(hp);
  2253. secondpass(hp);
  2254. hp.free;
  2255. current_asmdata.CurrAsmList:=OldAsmList;
  2256. end;
  2257. procedure thlcgobj.finalize_local_vars(p: TObject; arg: pointer);
  2258. begin
  2259. if (tsym(p).typ=localvarsym) and
  2260. (tlocalvarsym(p).refs>0) and
  2261. not(vo_is_external in tlocalvarsym(p).varoptions) and
  2262. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  2263. not(vo_is_default_var in tabstractvarsym(p).varoptions) and
  2264. is_managed_type(tlocalvarsym(p).vardef) then
  2265. finalize_sym(TAsmList(arg),tsym(p));
  2266. end;
  2267. procedure thlcgobj.finalize_static_data(p: TObject; arg: pointer);
  2268. var
  2269. i : longint;
  2270. pd : tprocdef;
  2271. begin
  2272. case tsym(p).typ of
  2273. staticvarsym :
  2274. begin
  2275. { local (procedure or unit) variables only need finalization
  2276. if they are used
  2277. }
  2278. if ((tstaticvarsym(p).refs>0) or
  2279. { global (unit) variables always need finalization, since
  2280. they may also be used in another unit
  2281. }
  2282. (tstaticvarsym(p).owner.symtabletype=globalsymtable)) and
  2283. (
  2284. (tstaticvarsym(p).varspez<>vs_const) or
  2285. (vo_force_finalize in tstaticvarsym(p).varoptions)
  2286. ) and
  2287. not(vo_is_funcret in tstaticvarsym(p).varoptions) and
  2288. not(vo_is_external in tstaticvarsym(p).varoptions) and
  2289. is_managed_type(tstaticvarsym(p).vardef) then
  2290. finalize_sym(TAsmList(arg),tsym(p));
  2291. end;
  2292. procsym :
  2293. begin
  2294. for i:=0 to tprocsym(p).ProcdefList.Count-1 do
  2295. begin
  2296. pd:=tprocdef(tprocsym(p).ProcdefList[i]);
  2297. if assigned(pd.localst) and
  2298. (pd.procsym=tprocsym(p)) and
  2299. (pd.localst.symtabletype<>staticsymtable) then
  2300. pd.localst.SymList.ForEachCall(@finalize_static_data,arg);
  2301. end;
  2302. end;
  2303. end;
  2304. end;
  2305. procedure thlcgobj.final_paras(p: TObject; arg: pointer);
  2306. var
  2307. list : TAsmList;
  2308. href : treference;
  2309. hsym : tparavarsym;
  2310. eldef : tdef;
  2311. highloc : tlocation;
  2312. begin
  2313. if not(tsym(p).typ=paravarsym) then
  2314. exit;
  2315. list:=TAsmList(arg);
  2316. if is_managed_type(tparavarsym(p).vardef) then
  2317. begin
  2318. if (tparavarsym(p).varspez=vs_value) then
  2319. begin
  2320. include(current_procinfo.flags,pi_needs_implicit_finally);
  2321. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  2322. if is_open_array(tparavarsym(p).vardef) then
  2323. begin
  2324. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  2325. begin
  2326. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  2327. if not assigned(hsym) then
  2328. internalerror(201003032);
  2329. highloc:=hsym.initialloc
  2330. end
  2331. else
  2332. highloc.loc:=LOC_INVALID;
  2333. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  2334. g_array_rtti_helper(list,eldef,href,highloc,'FPC_FINALIZE_ARRAY');
  2335. end
  2336. else
  2337. g_finalize(list,tparavarsym(p).vardef,href);
  2338. end;
  2339. end;
  2340. { open arrays can contain elements requiring init/final code, so the else has been removed here }
  2341. if (tparavarsym(p).varspez=vs_value) and
  2342. (is_open_array(tparavarsym(p).vardef) or
  2343. is_array_of_const(tparavarsym(p).vardef)) then
  2344. begin
  2345. { cdecl functions don't have a high pointer so it is not possible to generate
  2346. a local copy }
  2347. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  2348. g_releasevaluepara_openarray(list,tarraydef(tparavarsym(p).vardef),tparavarsym(p).localloc);
  2349. end;
  2350. end;
  2351. { generates the code for incrementing the reference count of parameters and
  2352. initialize out parameters }
  2353. { generates the code for incrementing the reference count of parameters and
  2354. initialize out parameters }
  2355. procedure thlcgobj.init_paras(p:TObject;arg:pointer);
  2356. var
  2357. href : treference;
  2358. hsym : tparavarsym;
  2359. eldef : tdef;
  2360. list : TAsmList;
  2361. highloc : tlocation;
  2362. needs_inittable (*,
  2363. do_trashing *) : boolean;
  2364. begin
  2365. list:=TAsmList(arg);
  2366. if (tsym(p).typ=paravarsym) then
  2367. begin
  2368. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  2369. (*
  2370. do_trashing:=
  2371. (localvartrashing <> -1) and
  2372. (not assigned(tparavarsym(p).defaultconstsym)) and
  2373. not needs_inittable;
  2374. *)
  2375. case tparavarsym(p).varspez of
  2376. vs_value :
  2377. if needs_inittable then
  2378. begin
  2379. { variants are already handled by the call to fpc_variant_copy_overwrite if
  2380. they are passed by reference }
  2381. if not((tparavarsym(p).vardef.typ=variantdef) and
  2382. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  2383. begin
  2384. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  2385. if is_open_array(tparavarsym(p).vardef) then
  2386. begin
  2387. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  2388. begin
  2389. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  2390. if not assigned(hsym) then
  2391. internalerror(201003032);
  2392. highloc:=hsym.initialloc
  2393. end
  2394. else
  2395. highloc.loc:=LOC_INVALID;
  2396. { open arrays do not contain correct element count in their rtti,
  2397. the actual count must be passed separately. }
  2398. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  2399. g_array_rtti_helper(list,eldef,href,highloc,'FPC_ADDREF_ARRAY');
  2400. end
  2401. else
  2402. g_incrrefcount(list,tparavarsym(p).vardef,href);
  2403. end;
  2404. end;
  2405. vs_out :
  2406. begin
  2407. if needs_inittable (*or
  2408. do_trashing*) then
  2409. begin
  2410. { we have no idea about the alignment at the callee side,
  2411. and the user also cannot specify "unaligned" here, so
  2412. assume worst case }
  2413. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  2414. (*
  2415. if do_trashing and
  2416. { needs separate implementation to trash open arrays }
  2417. { since their size is only known at run time }
  2418. not is_special_array(tparavarsym(p).vardef) then
  2419. { may be an open string, even if is_open_string() returns }
  2420. { false (for some helpers in the system unit) }
  2421. if not is_shortstring(tparavarsym(p).vardef) then
  2422. trash_reference(list,href,tparavarsym(p).vardef.size)
  2423. else
  2424. trash_reference(list,href,2);
  2425. *)
  2426. if needs_inittable then
  2427. begin
  2428. if is_open_array(tparavarsym(p).vardef) then
  2429. begin
  2430. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  2431. begin
  2432. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  2433. if not assigned(hsym) then
  2434. internalerror(201003032);
  2435. highloc:=hsym.initialloc
  2436. end
  2437. else
  2438. highloc.loc:=LOC_INVALID;
  2439. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  2440. g_array_rtti_helper(list,eldef,href,highloc,'FPC_INITIALIZE_ARRAY');
  2441. end
  2442. else
  2443. g_initialize(list,tparavarsym(p).vardef,href);
  2444. end;
  2445. end;
  2446. end;
  2447. (*
  2448. else if do_trashing and
  2449. ([vo_is_funcret,vo_is_hidden_para] * tparavarsym(p).varoptions = [vo_is_funcret,vo_is_hidden_para]) then
  2450. begin
  2451. { should always have standard alignment. If a function is assigned
  2452. to a non-aligned variable, the optimisation to pass this variable
  2453. directly as hidden function result must/cannot be performed
  2454. (see tcallnode.funcret_can_be_reused)
  2455. }
  2456. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,
  2457. used_align(tparavarsym(p).vardef.alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax));
  2458. { may be an open string, even if is_open_string() returns }
  2459. { false (for some helpers in the system unit) }
  2460. if not is_shortstring(tparavarsym(p).vardef) then
  2461. trash_reference(list,href,tparavarsym(p).vardef.size)
  2462. else
  2463. { an open string has at least size 2 }
  2464. trash_reference(list,href,2);
  2465. end
  2466. *)
  2467. end;
  2468. end;
  2469. end;
  2470. procedure thlcgobj.gen_load_para_value(list: TAsmList);
  2471. var
  2472. i: longint;
  2473. currpara: tparavarsym;
  2474. begin
  2475. if (po_assembler in current_procinfo.procdef.procoptions) or
  2476. { exceptfilters have a single hidden 'parentfp' parameter, which
  2477. is handled by tcg.g_proc_entry. }
  2478. (current_procinfo.procdef.proctypeoption=potype_exceptfilter) then
  2479. exit;
  2480. { Copy parameters to local references/registers }
  2481. for i:=0 to current_procinfo.procdef.paras.count-1 do
  2482. begin
  2483. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  2484. gen_load_cgpara_loc(list,currpara.vardef,currpara.paraloc[calleeside],currpara.initialloc,paramanager.param_use_paraloc(currpara.paraloc[calleeside]));
  2485. end;
  2486. { generate copies of call by value parameters, must be done before
  2487. the initialization and body is parsed because the refcounts are
  2488. incremented using the local copies }
  2489. current_procinfo.procdef.parast.SymList.ForEachCall(@g_copyvalueparas,list);
  2490. if not(po_assembler in current_procinfo.procdef.procoptions) then
  2491. begin
  2492. { has to be done here rather than in gen_initialize_code, because
  2493. the initialisation code is generated a) later and b) with
  2494. rad_backwards, so the register allocator would generate
  2495. information as if this code comes before loading the parameters
  2496. from their original registers to their local location }
  2497. // if (localvartrashing <> -1) then
  2498. // current_procinfo.procdef.localst.SymList.ForEachCall(@trash_variable,list);
  2499. { initialize refcounted paras, and trash others. Needed here
  2500. instead of in gen_initialize_code, because when a reference is
  2501. intialised or trashed while the pointer to that reference is kept
  2502. in a regvar, we add a register move and that one again has to
  2503. come after the parameter loading code as far as the register
  2504. allocator is concerned }
  2505. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  2506. end;
  2507. end;
  2508. procedure thlcgobj.g_copyvalueparas(p: TObject; arg: pointer);
  2509. var
  2510. href : treference;
  2511. hreg : tregister;
  2512. list : TAsmList;
  2513. hsym : tparavarsym;
  2514. l : longint;
  2515. highloc,
  2516. localcopyloc : tlocation;
  2517. begin
  2518. list:=TAsmList(arg);
  2519. if (tsym(p).typ=paravarsym) and
  2520. (tparavarsym(p).varspez=vs_value) and
  2521. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  2522. begin
  2523. { we have no idea about the alignment at the caller side }
  2524. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  2525. if is_open_array(tparavarsym(p).vardef) or
  2526. is_array_of_const(tparavarsym(p).vardef) then
  2527. begin
  2528. { cdecl functions don't have a high pointer so it is not possible to generate
  2529. a local copy }
  2530. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  2531. begin
  2532. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  2533. begin
  2534. hsym:=tparavarsym(get_high_value_sym(tparavarsym(p)));
  2535. if not assigned(hsym) then
  2536. internalerror(2011020506);
  2537. highloc:=hsym.initialloc
  2538. end
  2539. else
  2540. highloc.loc:=LOC_INVALID;
  2541. hreg:=getaddressregister(list,voidpointertype);
  2542. if not is_packed_array(tparavarsym(p).vardef) then
  2543. g_copyvaluepara_openarray(list,href,highloc,tarraydef(tparavarsym(p).vardef),hreg)
  2544. else
  2545. internalerror(2011020507);
  2546. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  2547. a_load_reg_loc(list,tparavarsym(p).vardef,tparavarsym(p).vardef,hreg,tparavarsym(p).initialloc);
  2548. end;
  2549. end
  2550. else
  2551. begin
  2552. { Allocate space for the local copy }
  2553. l:=tparavarsym(p).getsize;
  2554. localcopyloc.loc:=LOC_REFERENCE;
  2555. localcopyloc.size:=int_cgsize(l);
  2556. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  2557. { Copy data }
  2558. if is_shortstring(tparavarsym(p).vardef) then
  2559. begin
  2560. { this code is only executed before the code for the body and the entry/exit code is generated
  2561. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  2562. }
  2563. include(current_procinfo.flags,pi_do_call);
  2564. g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef))
  2565. end
  2566. else if tparavarsym(p).vardef.typ=variantdef then
  2567. begin
  2568. { this code is only executed before the code for the body and the entry/exit code is generated
  2569. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  2570. }
  2571. include(current_procinfo.flags,pi_do_call);
  2572. g_copyvariant(list,href,localcopyloc.reference,tvariantdef(tparavarsym(p).vardef))
  2573. end
  2574. else
  2575. begin
  2576. { pass proper alignment info }
  2577. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  2578. g_concatcopy(list,tparavarsym(p).vardef,href,localcopyloc.reference);
  2579. end;
  2580. { update localloc of varsym }
  2581. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  2582. tparavarsym(p).localloc:=localcopyloc;
  2583. tparavarsym(p).initialloc:=localcopyloc;
  2584. end;
  2585. end;
  2586. end;
  2587. procedure thlcgobj.gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint);
  2588. begin
  2589. case l.loc of
  2590. (*
  2591. LOC_MMREGISTER,
  2592. LOC_CMMREGISTER:
  2593. case cgpara.location^.loc of
  2594. LOC_REFERENCE,
  2595. LOC_CREFERENCE,
  2596. LOC_MMREGISTER,
  2597. LOC_CMMREGISTER,
  2598. LOC_REGISTER,
  2599. LOC_CREGISTER :
  2600. cg.a_loadmm_reg_cgpara(list,locsize,l.register,cgpara,mms_movescalar);
  2601. LOC_FPUREGISTER,
  2602. LOC_CFPUREGISTER:
  2603. begin
  2604. tmploc:=l;
  2605. location_force_fpureg(list,tmploc,false);
  2606. cg.a_loadfpu_reg_cgpara(list,tmploc.size,tmploc.register,cgpara);
  2607. end;
  2608. else
  2609. internalerror(200204249);
  2610. end;
  2611. *)
  2612. LOC_FPUREGISTER,
  2613. LOC_CFPUREGISTER:
  2614. case cgpara.location^.loc of
  2615. (*
  2616. LOC_MMREGISTER,
  2617. LOC_CMMREGISTER:
  2618. begin
  2619. tmploc:=l;
  2620. location_force_mmregscalar(list,tmploc,false);
  2621. cg.a_loadmm_reg_cgpara(list,tmploc.size,tmploc.register,cgpara,mms_movescalar);
  2622. end;
  2623. *)
  2624. { Some targets pass floats in normal registers }
  2625. LOC_REGISTER,
  2626. LOC_CREGISTER,
  2627. LOC_REFERENCE,
  2628. LOC_CREFERENCE,
  2629. LOC_FPUREGISTER,
  2630. LOC_CFPUREGISTER:
  2631. a_loadfpu_reg_cgpara(list,size,l.register,cgpara);
  2632. else
  2633. internalerror(2011010210);
  2634. end;
  2635. LOC_REFERENCE,
  2636. LOC_CREFERENCE:
  2637. case cgpara.location^.loc of
  2638. (*
  2639. LOC_MMREGISTER,
  2640. LOC_CMMREGISTER:
  2641. cg.a_loadmm_ref_cgpara(list,locsize,l.reference,cgpara,mms_movescalar);
  2642. *)
  2643. { Some targets pass floats in normal registers }
  2644. LOC_REGISTER,
  2645. LOC_CREGISTER,
  2646. LOC_REFERENCE,
  2647. LOC_CREFERENCE,
  2648. LOC_FPUREGISTER,
  2649. LOC_CFPUREGISTER:
  2650. a_loadfpu_ref_cgpara(list,size,l.reference,cgpara);
  2651. else
  2652. internalerror(2011010211);
  2653. end;
  2654. LOC_REGISTER,
  2655. LOC_CREGISTER :
  2656. a_load_loc_cgpara(list,size,l,cgpara);
  2657. else
  2658. internalerror(2011010212);
  2659. end;
  2660. end;
  2661. procedure thlcgobj.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  2662. begin
  2663. { do nothing by default }
  2664. end;
  2665. procedure thlcgobj.gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);
  2666. begin
  2667. { Handle Floating point types differently
  2668. This doesn't depend on emulator settings, emulator settings should
  2669. be handled by cpupara }
  2670. if (vardef.typ=floatdef) or
  2671. { some ABIs return certain records in an fpu register }
  2672. (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) or
  2673. (assigned(cgpara.location) and
  2674. (cgpara.Location^.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) then
  2675. begin
  2676. gen_loadfpu_loc_cgpara(list,vardef,l,cgpara,vardef.size);
  2677. exit;
  2678. end;
  2679. case l.loc of
  2680. LOC_CONSTANT,
  2681. LOC_REGISTER,
  2682. LOC_CREGISTER,
  2683. LOC_REFERENCE,
  2684. LOC_CREFERENCE :
  2685. begin
  2686. a_load_loc_cgpara(list,vardef,l,cgpara);
  2687. end;
  2688. (*
  2689. LOC_MMREGISTER,
  2690. LOC_CMMREGISTER:
  2691. begin
  2692. case l.size of
  2693. OS_F32,
  2694. OS_F64:
  2695. cg.a_loadmm_loc_cgpara(list,l,cgpara,mms_movescalar);
  2696. else
  2697. cg.a_loadmm_loc_cgpara(list,l,cgpara,nil);
  2698. end;
  2699. end;
  2700. *)
  2701. else
  2702. internalerror(2011010213);
  2703. end;
  2704. end;
  2705. procedure thlcgobj.gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  2706. var
  2707. href : treference;
  2708. begin
  2709. para.check_simple_location;
  2710. { skip e.g. empty records }
  2711. if (para.location^.loc = LOC_VOID) then
  2712. exit;
  2713. case destloc.loc of
  2714. LOC_REFERENCE :
  2715. begin
  2716. { If the parameter location is reused we don't need to copy
  2717. anything }
  2718. if not reusepara then
  2719. begin
  2720. reference_reset_base(href,para.location^.reference.index,para.location^.reference.offset,para.alignment);
  2721. a_load_ref_ref(list,para.def,para.def,href,destloc.reference);
  2722. end;
  2723. end;
  2724. { TODO other possible locations }
  2725. else
  2726. internalerror(2011010308);
  2727. end;
  2728. end;
  2729. procedure thlcgobj.gen_load_return_value(list: TAsmList);
  2730. var
  2731. ressym : tabstractnormalvarsym;
  2732. funcretloc : TCGPara;
  2733. begin
  2734. { Is the loading needed? }
  2735. if is_void(current_procinfo.procdef.returndef) or
  2736. (
  2737. (po_assembler in current_procinfo.procdef.procoptions) and
  2738. (not(assigned(current_procinfo.procdef.funcretsym)) or
  2739. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  2740. ) then
  2741. exit;
  2742. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  2743. { constructors return self }
  2744. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  2745. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.Find('self'))
  2746. else
  2747. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  2748. if (ressym.refs>0) or
  2749. is_managed_type(ressym.vardef) then
  2750. begin
  2751. { was: don't do anything if funcretloc.loc in [LOC_INVALID,LOC_REFERENCE] }
  2752. if not paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  2753. gen_load_loc_cgpara(list,ressym.vardef,ressym.localloc,funcretloc);
  2754. end
  2755. else
  2756. gen_load_uninitialized_function_result(list,current_procinfo.procdef,ressym.vardef,funcretloc)
  2757. end;
  2758. procedure thlcgobj.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  2759. begin
  2760. { add the procedure to the al_procedures }
  2761. maybe_new_object_file(current_asmdata.asmlists[al_procedures]);
  2762. new_section(current_asmdata.asmlists[al_procedures],sec_code,lower(pd.mangledname),getprocalign);
  2763. current_asmdata.asmlists[al_procedures].concatlist(code);
  2764. { save local data (casetable) also in the same file }
  2765. if assigned(data) and
  2766. (not data.empty) then
  2767. current_asmdata.asmlists[al_procedures].concatlist(data);
  2768. end;
  2769. procedure thlcgobj.g_call_system_proc(list: TAsmList; const procname: string);
  2770. var
  2771. srsym: tsym;
  2772. pd: tprocdef;
  2773. begin
  2774. srsym:=tsym(systemunit.find(procname));
  2775. if not assigned(srsym) and
  2776. (cs_compilesystem in current_settings.moduleswitches) then
  2777. srsym:=tsym(systemunit.Find(upper(procname)));
  2778. if not assigned(srsym) or
  2779. (srsym.typ<>procsym) then
  2780. Message1(cg_f_unknown_compilerproc,procname);
  2781. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  2782. a_call_name(list,pd,pd.mangledname,false);
  2783. end;
  2784. end.