hlcgobj.pas 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728
  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 : string; 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 : string);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 : string);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_decrrefcount(list : TAsmList;t: tdef; const ref: treference);virtual;abstract;
  313. procedure g_initialize(list : TAsmList;t : tdef;const ref : treference);virtual;abstract;
  314. procedure g_finalize(list : TAsmList;t : tdef;const ref : treference);virtual;abstract;
  315. procedure g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation;
  316. const name: string);virtual;abstract;
  317. {# Generates range checking code. It is to note
  318. that this routine does not need to be overridden,
  319. as it takes care of everything.
  320. @param(p Node which contains the value to check)
  321. @param(todef Type definition of node to range check)
  322. }
  323. procedure g_rangecheck(list: TAsmList; const l:tlocation; fromdef,todef: tdef); virtual;
  324. {# Generates overflow checking code for a node }
  325. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef); virtual; abstract;
  326. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;var ovloc : tlocation);virtual; abstract;
  327. procedure g_copyvaluepara_openarray(list : TAsmList;const ref:treference;const lenloc:tlocation;arrdef: tarraydef;destreg:tregister);virtual;abstract;
  328. procedure g_releasevaluepara_openarray(list : TAsmList;arrdef: tarraydef;const l:tlocation);virtual;abstract;
  329. {# Emits instructions when compilation is done in profile
  330. mode (this is set as a command line option). The default
  331. behavior does nothing, should be overridden as required.
  332. }
  333. procedure g_profilecode(list : TAsmList);virtual;
  334. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  335. @param(size Number of bytes to allocate)
  336. }
  337. procedure g_stackpointer_alloc(list : TAsmList;size : longint);virtual; abstract;
  338. {# Emits instruction for allocating the locals in entry
  339. code of a routine. This is one of the first
  340. routine called in @var(genentrycode).
  341. @param(localsize Number of bytes to allocate as locals)
  342. }
  343. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);virtual; abstract;
  344. {# Emits instructions for returning from a subroutine.
  345. Should also restore the framepointer and stack.
  346. @param(parasize Number of bytes of parameters to deallocate from stack)
  347. }
  348. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);virtual; abstract;
  349. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);virtual; abstract;
  350. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);virtual; abstract;
  351. function g_indirect_sym_load(list:TAsmList;const symname: string; weak: boolean): tregister;virtual; abstract;
  352. { generate a stub which only purpose is to pass control the given external method,
  353. setting up any additional environment before doing so (if required).
  354. The default implementation issues a jump instruction to the external name. }
  355. // procedure g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string); virtual;
  356. { create "safe copy" of a tlocation that can be used later: all
  357. registers used in the tlocation are copied to new ones, so that
  358. even if the original ones change, things stay the same (except if
  359. the original location was already a register, then the register is
  360. kept). Must only be used on lvalue locations.
  361. It's intended as some kind of replacement for a_loadaddr_ref_reg()
  362. for targets without pointers. }
  363. procedure g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation); virtual; abstract;
  364. { routines migrated from ncgutil }
  365. procedure location_force_reg(list:TAsmList;var l:tlocation;src_size,dst_size:tdef;maybeconst:boolean);virtual;
  366. procedure location_force_fpureg(list:TAsmList;var l: tlocation;size: tdef;maybeconst:boolean);virtual;
  367. procedure location_force_mem(list:TAsmList;var l:tlocation;size:tdef);virtual;
  368. // procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;abstract;
  369. // procedure location_force_mmreg(list:TAsmList;var l: tlocation;size:tdef;maybeconst:boolean);virtual;abstract;
  370. { Retrieve the location of the data pointed to in location l, when the location is
  371. a register it is expected to contain the address of the data }
  372. procedure location_get_data_ref(list:TAsmList;def: tdef; const l:tlocation;var ref:treference;loadref:boolean; alignment: longint);virtual;
  373. procedure maketojumpbool(list:TAsmList; p : tnode);virtual;
  374. procedure gen_proc_symbol(list:TAsmList);virtual;
  375. procedure gen_proc_symbol_end(list:TAsmList);virtual;
  376. procedure gen_initialize_code(list:TAsmList);virtual;
  377. procedure gen_finalize_code(list:TAsmList);virtual;
  378. procedure gen_entry_code(list:TAsmList);virtual;
  379. procedure gen_exit_code(list:TAsmList);virtual;
  380. protected
  381. { helpers called by gen_initialize_code/gen_finalize_code }
  382. procedure inittempvariables(list:TAsmList);virtual;
  383. procedure initialize_data(p:TObject;arg:pointer);virtual;
  384. procedure finalizetempvariables(list:TAsmList);virtual;
  385. procedure initialize_regvars(p:TObject;arg:pointer);virtual;
  386. procedure finalize_sym(asmlist:TAsmList;sym:tsym);virtual;
  387. { generates the code for finalisation of local variables }
  388. procedure finalize_local_vars(p:TObject;arg:pointer);virtual;
  389. { generates the code for finalization of static symtable and
  390. all local (static) typed consts }
  391. procedure finalize_static_data(p:TObject;arg:pointer);virtual;
  392. { generates the code for decrementing the reference count of parameters }
  393. procedure final_paras(p:TObject;arg:pointer);
  394. public
  395. procedure gen_load_para_value(list:TAsmList);virtual;
  396. protected
  397. { helpers called by gen_load_para_value }
  398. procedure g_copyvalueparas(p:TObject;arg:pointer);virtual;
  399. procedure gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation;const cgpara: tcgpara;locintsize: longint);virtual;
  400. procedure init_paras(p:TObject;arg:pointer);
  401. protected
  402. { Some targets have to put "something" in the function result
  403. location if it's not initialised by the Pascal code, e.g.
  404. stack-based architectures. By default it does nothing }
  405. procedure gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);virtual;
  406. public
  407. { load a tlocation into a cgpara }
  408. procedure gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);virtual;
  409. { load a cgpara into a tlocation }
  410. procedure gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);virtual;
  411. { load the function return value into the ABI-defined function return location }
  412. procedure gen_load_return_value(list:TAsmList);virtual;
  413. { extras refactored from other units }
  414. { queue the code/data generated for a procedure for writing out to
  415. the assembler/object file }
  416. procedure record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList); virtual;
  417. end;
  418. var
  419. {# Main high level code generator class }
  420. hlcg : thlcgobj;
  421. procedure destroy_hlcodegen;
  422. implementation
  423. uses
  424. globals,options,systems,
  425. fmodule,export,
  426. verbose,defutil,paramgr,
  427. symbase,symsym,
  428. ncon,nld,pass_1,pass_2,
  429. cpuinfo,cgobj,tgobj,cutils,procinfo,
  430. ncgutil,ngenutil;
  431. procedure destroy_hlcodegen;
  432. begin
  433. hlcg.free;
  434. hlcg:=nil;
  435. end;
  436. { thlcgobj }
  437. constructor thlcgobj.create;
  438. begin
  439. end;
  440. procedure thlcgobj.init_register_allocators;
  441. begin
  442. cg.init_register_allocators;
  443. end;
  444. procedure thlcgobj.done_register_allocators;
  445. begin
  446. cg.done_register_allocators;
  447. end;
  448. procedure thlcgobj.set_regalloc_live_range_direction(dir: TRADirection);
  449. begin
  450. cg.set_regalloc_live_range_direction(dir);
  451. end;
  452. function thlcgobj.getintregister(list: TAsmList; size: tdef): Tregister;
  453. begin
  454. result:=cg.getintregister(list,def_cgsize(size));
  455. end;
  456. function thlcgobj.getaddressregister(list: TAsmList; size: tdef): Tregister;
  457. begin
  458. result:=cg.getaddressregister(list);
  459. end;
  460. function thlcgobj.getfpuregister(list: TAsmList; size: tdef): Tregister;
  461. begin
  462. result:=cg.getfpuregister(list,def_cgsize(size));
  463. end;
  464. (*
  465. function thlcgobj.getmmregister(list: TAsmList; size: tdef): Tregister;
  466. begin
  467. result:=cg.getmmregister(list,def_cgsize(size));
  468. end;
  469. *)
  470. function thlcgobj.getflagregister(list: TAsmList; size: tdef): Tregister;
  471. begin
  472. result:=cg.getflagregister(list,def_cgsize(size));
  473. end;
  474. function thlcgobj.getregisterfordef(list: TAsmList; size: tdef): Tregister;
  475. begin
  476. case def2regtyp(size) of
  477. R_INTREGISTER:
  478. result:=getintregister(list,size);
  479. R_ADDRESSREGISTER:
  480. result:=getaddressregister(list,size);
  481. R_FPUREGISTER:
  482. result:=getfpuregister(list,size);
  483. (*
  484. R_MMREGISTER:
  485. result:=getmmregister(list,size);
  486. *)
  487. else
  488. internalerror(2010122901);
  489. end;
  490. end;
  491. function thlcgobj.uses_registers(rt: Tregistertype): boolean;
  492. begin
  493. result:=cg.uses_registers(rt);
  494. end;
  495. procedure thlcgobj.do_register_allocation(list: TAsmList; headertai: tai);
  496. begin
  497. cg.do_register_allocation(list,headertai);
  498. end;
  499. procedure thlcgobj.translate_register(var reg: tregister);
  500. begin
  501. cg.translate_register(reg);
  502. end;
  503. function thlcgobj.def2regtyp(def: tdef): tregistertype;
  504. begin
  505. case def.typ of
  506. enumdef,
  507. orddef,
  508. recorddef,
  509. setdef:
  510. result:=R_INTREGISTER;
  511. stringdef,
  512. pointerdef,
  513. classrefdef,
  514. objectdef,
  515. procvardef,
  516. procdef,
  517. arraydef,
  518. formaldef:
  519. result:=R_ADDRESSREGISTER;
  520. floatdef:
  521. if use_vectorfpu(def) then
  522. result:=R_MMREGISTER
  523. else if cs_fp_emulation in current_settings.moduleswitches then
  524. result:=R_INTREGISTER
  525. else
  526. result:=R_FPUREGISTER;
  527. filedef,
  528. variantdef:
  529. internalerror(2010120507);
  530. else
  531. internalerror(2010120506);
  532. end;
  533. end;
  534. procedure thlcgobj.a_label(list: TAsmList; l: tasmlabel); inline;
  535. begin
  536. cg.a_label(list,l);
  537. end;
  538. procedure thlcgobj.a_reg_alloc(list: TAsmList; r: tregister);
  539. begin
  540. cg.a_reg_alloc(list,r);
  541. end;
  542. procedure thlcgobj.a_reg_dealloc(list: TAsmList; r: tregister);
  543. begin
  544. cg.a_reg_dealloc(list,r);
  545. end;
  546. procedure thlcgobj.a_reg_sync(list: TAsmList; r: tregister);
  547. begin
  548. cg.a_reg_sync(list,r);
  549. end;
  550. procedure thlcgobj.a_load_reg_cgpara(list: TAsmList; size: tdef; r: tregister; const cgpara: TCGPara);
  551. var
  552. ref: treference;
  553. begin
  554. cgpara.check_simple_location;
  555. paramanager.alloccgpara(list,cgpara);
  556. case cgpara.location^.loc of
  557. LOC_REGISTER,LOC_CREGISTER:
  558. a_load_reg_reg(list,size,cgpara.def,r,cgpara.location^.register);
  559. LOC_REFERENCE,LOC_CREFERENCE:
  560. begin
  561. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  562. a_load_reg_ref(list,size,cgpara.def,r,ref);
  563. end;
  564. (*
  565. LOC_MMREGISTER,LOC_CMMREGISTER:
  566. a_loadmm_intreg_reg(list,size,cgpara.def,r,cgpara.location^.register,mms_movescalar);
  567. *)
  568. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  569. begin
  570. tg.gethltemp(list,size,size.size,tt_normal,ref);
  571. a_load_reg_ref(list,size,size,r,ref);
  572. a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  573. tg.ungettemp(list,ref);
  574. end
  575. else
  576. internalerror(2010120415);
  577. end;
  578. end;
  579. procedure thlcgobj.a_load_const_cgpara(list: TAsmList; tosize: tdef; a: aint; const cgpara: TCGPara);
  580. var
  581. ref : treference;
  582. begin
  583. cgpara.check_simple_location;
  584. paramanager.alloccgpara(list,cgpara);
  585. case cgpara.location^.loc of
  586. LOC_REGISTER,LOC_CREGISTER:
  587. a_load_const_reg(list,cgpara.def,a,cgpara.location^.register);
  588. LOC_REFERENCE,LOC_CREFERENCE:
  589. begin
  590. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  591. a_load_const_ref(list,cgpara.def,a,ref);
  592. end
  593. else
  594. internalerror(2010120416);
  595. end;
  596. end;
  597. procedure thlcgobj.a_load_ref_cgpara(list: TAsmList; size: tdef; const r: treference; const cgpara: TCGPara);
  598. var
  599. ref: treference;
  600. begin
  601. cgpara.check_simple_location;
  602. paramanager.alloccgpara(list,cgpara);
  603. case cgpara.location^.loc of
  604. LOC_REGISTER,LOC_CREGISTER:
  605. a_load_ref_reg(list,size,cgpara.def,r,cgpara.location^.register);
  606. LOC_REFERENCE,LOC_CREFERENCE:
  607. begin
  608. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  609. a_load_ref_ref(list,size,cgpara.def,r,ref);
  610. end
  611. (*
  612. LOC_MMREGISTER,LOC_CMMREGISTER:
  613. begin
  614. case location^.size of
  615. OS_F32,
  616. OS_F64,
  617. OS_F128:
  618. a_loadmm_ref_reg(list,cgpara.def,cgpara.def,r,location^.register,mms_movescalar);
  619. OS_M8..OS_M128,
  620. OS_MS8..OS_MS128:
  621. a_loadmm_ref_reg(list,cgpara.def,cgpara.def,r,location^.register,nil);
  622. else
  623. internalerror(2010120417);
  624. end;
  625. end
  626. *)
  627. else
  628. internalerror(2010120418);
  629. end;
  630. end;
  631. procedure thlcgobj.a_load_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: TCGPara);
  632. begin
  633. case l.loc of
  634. LOC_REGISTER,
  635. LOC_CREGISTER :
  636. a_load_reg_cgpara(list,size,l.register,cgpara);
  637. LOC_CONSTANT :
  638. a_load_const_cgpara(list,size,l.value,cgpara);
  639. LOC_CREFERENCE,
  640. LOC_REFERENCE :
  641. a_load_ref_cgpara(list,size,l.reference,cgpara);
  642. else
  643. internalerror(2010120419);
  644. end;
  645. end;
  646. procedure thlcgobj.a_loadaddr_ref_cgpara(list: TAsmList; fromsize, tosize: tdef; const r: treference; const cgpara: TCGPara);
  647. var
  648. hr : tregister;
  649. begin
  650. cgpara.check_simple_location;
  651. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  652. begin
  653. paramanager.allocparaloc(list,cgpara.location);
  654. a_loadaddr_ref_reg(list,fromsize,tosize,r,cgpara.location^.register)
  655. end
  656. else
  657. begin
  658. hr:=getaddressregister(list,tosize);
  659. a_loadaddr_ref_reg(list,fromsize,tosize,r,hr);
  660. a_load_reg_cgpara(list,tosize,hr,cgpara);
  661. end;
  662. end;
  663. procedure thlcgobj.a_call_name_static(list: TAsmList; pd: tprocdef; const s: string);
  664. begin
  665. a_call_name(list,pd,s,false);
  666. end;
  667. procedure thlcgobj.a_call_name_inherited(list: TAsmList; pd: tprocdef; const s: string);
  668. begin
  669. a_call_name(list,pd,s,false);
  670. end;
  671. procedure thlcgobj.a_load_const_ref(list: TAsmList; tosize: tdef; a: aint; const ref: treference);
  672. var
  673. tmpreg: tregister;
  674. begin
  675. tmpreg:=getintregister(list,tosize);
  676. a_load_const_reg(list,tosize,a,tmpreg);
  677. a_load_reg_ref(list,tosize,tosize,tmpreg,ref);
  678. end;
  679. procedure thlcgobj.a_load_const_loc(list: TAsmList; tosize: tdef; a: aint; const loc: tlocation);
  680. begin
  681. case loc.loc of
  682. LOC_REFERENCE,LOC_CREFERENCE:
  683. a_load_const_ref(list,tosize,a,loc.reference);
  684. LOC_REGISTER,LOC_CREGISTER:
  685. a_load_const_reg(list,tosize,a,loc.register);
  686. { we don't have enough type information to handle these here
  687. LOC_SUBSETREG,LOC_CSUBSETREG:
  688. a_load_const_subsetreg(list,loc.size,a,loc.sreg);
  689. LOC_SUBSETREF,LOC_CSUBSETREF:
  690. a_load_const_subsetref(list,loc.size,a,loc.sref);
  691. }
  692. else
  693. internalerror(2010120401);
  694. end;
  695. end;
  696. procedure thlcgobj.a_load_reg_ref_unaligned(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  697. begin
  698. a_load_reg_ref(list,fromsize,tosize,register,ref);
  699. end;
  700. procedure thlcgobj.a_load_reg_loc(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const loc: tlocation);
  701. begin
  702. case loc.loc of
  703. LOC_REFERENCE,LOC_CREFERENCE:
  704. a_load_reg_ref(list,fromsize,tosize,reg,loc.reference);
  705. LOC_REGISTER,LOC_CREGISTER:
  706. a_load_reg_reg(list,fromsize,tosize,reg,loc.register);
  707. { we don't have enough type information to handle these here
  708. LOC_SUBSETREG,LOC_CSUBSETREG:
  709. a_load_reg_subsetreg(list,fromsize,tosize,reg,loc.sreg);
  710. LOC_SUBSETREF,LOC_CSUBSETREF:
  711. a_load_reg_subsetref(list,fromsize,loc.size,reg,loc.sref);
  712. LOC_MMREGISTER,LOC_CMMREGISTER:
  713. a_loadmm_intreg_reg(list,fromsize,loc.size,reg,loc.register,mms_movescalar);
  714. }
  715. else
  716. internalerror(2010120402);
  717. end;
  718. end;
  719. procedure thlcgobj.a_load_ref_reg_unaligned(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  720. begin
  721. a_load_ref_reg(list,fromsize,tosize,ref,register);
  722. end;
  723. procedure thlcgobj.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  724. var
  725. tmpreg: tregister;
  726. begin
  727. { verify if we have the same reference }
  728. if references_equal(sref,dref) then
  729. exit;
  730. tmpreg:=getintregister(list,tosize);
  731. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  732. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  733. end;
  734. procedure thlcgobj.a_load_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; reg: tregister);
  735. begin
  736. case loc.loc of
  737. LOC_REFERENCE,LOC_CREFERENCE:
  738. a_load_ref_reg(list,fromsize,tosize,loc.reference,reg);
  739. LOC_REGISTER,LOC_CREGISTER:
  740. a_load_reg_reg(list,fromsize,tosize,loc.register,reg);
  741. LOC_CONSTANT:
  742. a_load_const_reg(list,tosize,loc.value,reg);
  743. { we don't have enough type information to handle these here
  744. LOC_SUBSETREG,LOC_CSUBSETREG:
  745. a_load_subsetreg_reg(list,fromsize,tosize,loc.sreg,reg);
  746. LOC_SUBSETREF,LOC_CSUBSETREF:
  747. a_load_subsetref_reg(list,fromsize,tosize,loc.sref,reg);
  748. }
  749. else
  750. internalerror(2010120201);
  751. end;
  752. end;
  753. procedure thlcgobj.a_load_loc_ref(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const ref: treference);
  754. begin
  755. case loc.loc of
  756. LOC_REFERENCE,LOC_CREFERENCE:
  757. a_load_ref_ref(list,fromsize,tosize,loc.reference,ref);
  758. LOC_REGISTER,LOC_CREGISTER:
  759. a_load_reg_ref(list,fromsize,tosize,loc.register,ref);
  760. LOC_CONSTANT:
  761. a_load_const_ref(list,tosize,loc.value,ref);
  762. { we don't have enough type information to handle these here
  763. LOC_SUBSETREG,LOC_CSUBSETREG:
  764. a_load_subsetreg_ref(list,loc.size,tosize,loc.sreg,ref);
  765. LOC_SUBSETREF,LOC_CSUBSETREF:
  766. a_load_subsetref_ref(list,loc.size,tosize,loc.sref,ref);
  767. }
  768. else
  769. internalerror(2010120403);
  770. end;
  771. end;
  772. procedure thlcgobj.a_load_loc_subsetreg(list: TAsmList; fromsize, tosize, tosubsetsize: tdef; const loc: tlocation; const sreg: tsubsetregister);
  773. begin
  774. case loc.loc of
  775. LOC_REFERENCE,LOC_CREFERENCE:
  776. a_load_ref_subsetreg(list,fromsize,tosize,tosubsetsize,loc.reference,sreg);
  777. LOC_REGISTER,LOC_CREGISTER:
  778. a_load_reg_subsetreg(list,fromsize,tosize,tosubsetsize,loc.register,sreg);
  779. LOC_CONSTANT:
  780. a_load_const_subsetreg(list,tosize,tosubsetsize,loc.value,sreg);
  781. { we don't have enough type information to handle these here
  782. LOC_SUBSETREG,LOC_CSUBSETREG:
  783. a_load_subsetreg_subsetreg(list,loc.size,subsetsize,loc.sreg,sreg);
  784. LOC_SUBSETREF,LOC_CSUBSETREF:
  785. a_load_subsetref_subsetreg(list,loc.size,subsetsize,loc.sref,sreg);
  786. }
  787. else
  788. internalerror(2010120404);
  789. end;
  790. end;
  791. procedure thlcgobj.a_load_loc_subsetref(list: TAsmList; fromsize, tosize, tosubsetsize: tdef; const loc: tlocation; const sref: tsubsetreference);
  792. begin
  793. case loc.loc of
  794. LOC_REFERENCE,LOC_CREFERENCE:
  795. a_load_ref_subsetref(list,fromsize,tosize,tosubsetsize,loc.reference,sref);
  796. LOC_REGISTER,LOC_CREGISTER:
  797. a_load_reg_subsetref(list,fromsize,tosize,tosubsetsize,loc.register,sref);
  798. LOC_CONSTANT:
  799. a_load_const_subsetref(list,tosize,tosubsetsize,loc.value,sref);
  800. { we don't have enough type information to handle these here
  801. LOC_SUBSETREG,LOC_CSUBSETREG:
  802. a_load_subsetreg_subsetref(list,loc.size,subsetsize,loc.sreg,sref);
  803. LOC_SUBSETREF,LOC_CSUBSETREF:
  804. a_load_subsetref_subsetref(list,loc.size,subsetsize,loc.sref,sref);
  805. }
  806. else
  807. internalerror(2010120405);
  808. end;
  809. end;
  810. procedure thlcgobj.a_load_subsetreg_loc(list: TAsmlist; fromsize, fromsubsetsize, tosize: tdef; const sreg: tsubsetregister; const loc: tlocation);
  811. begin
  812. case loc.loc of
  813. LOC_REFERENCE,LOC_CREFERENCE:
  814. a_load_subsetreg_ref(list,fromsize,fromsubsetsize,tosize,sreg,loc.reference);
  815. LOC_REGISTER,LOC_CREGISTER:
  816. a_load_subsetreg_reg(list,fromsize,fromsubsetsize,tosize,sreg,loc.register);
  817. { we don't have enough type information to handle these here
  818. LOC_SUBSETREG,LOC_CSUBSETREG:
  819. a_load_subsetreg_subsetreg(list,subsetsize,loc.size,sreg,loc.sreg);
  820. LOC_SUBSETREF,LOC_CSUBSETREF:
  821. a_load_subsetreg_subsetref(list,subsetsize,loc.size,sreg,loc.sref);
  822. }
  823. else
  824. internalerror(2010120406);
  825. end;
  826. end;
  827. procedure thlcgobj.a_load_subsetref_loc(list: TAsmlist; fromsize, fromsubsetsize, tosize: tdef; const sref: tsubsetreference; const loc: tlocation);
  828. begin
  829. case loc.loc of
  830. LOC_REFERENCE,LOC_CREFERENCE:
  831. a_load_subsetref_ref(list,fromsize,fromsubsetsize,tosize,sref,loc.reference);
  832. LOC_REGISTER,LOC_CREGISTER:
  833. a_load_subsetref_reg(list,fromsize,fromsubsetsize,tosize,sref,loc.register);
  834. { we don't have enough type information to handle these here
  835. LOC_SUBSETREG,LOC_CSUBSETREG:
  836. a_load_subsetref_subsetreg(list,subsetsize,loc.size,sref,loc.sreg);
  837. LOC_SUBSETREF,LOC_CSUBSETREF:
  838. a_load_subsetref_subsetref(list,subsetsize,loc.size,sref,loc.sref);
  839. }
  840. else
  841. internalerror(2010120407);
  842. end;
  843. end;
  844. procedure thlcgobj.a_bit_test_reg_loc_reg(list: TAsmList; bitnumbersize, locsize, destsize: tdef; bitnumber: tregister; const loc: tlocation; destreg: tregister);
  845. var
  846. tmpreg: tregister;
  847. begin
  848. case loc.loc of
  849. LOC_REFERENCE,LOC_CREFERENCE:
  850. a_bit_test_reg_ref_reg(list,bitnumbersize,locsize,destsize,bitnumber,loc.reference,destreg);
  851. LOC_REGISTER,LOC_CREGISTER,
  852. LOC_SUBSETREG,LOC_CSUBSETREG,
  853. LOC_CONSTANT:
  854. begin
  855. case loc.loc of
  856. LOC_REGISTER,LOC_CREGISTER:
  857. tmpreg:=loc.register;
  858. (* we don't have enough type information to handle this here
  859. LOC_SUBSETREG,LOC_CSUBSETREG:
  860. begin
  861. tmpreg:=getintregister(list,loc.size);
  862. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  863. end;
  864. *)
  865. LOC_CONSTANT:
  866. begin
  867. tmpreg:=getintregister(list,locsize);
  868. a_load_const_reg(list,locsize,loc.value,tmpreg);
  869. end;
  870. end;
  871. a_bit_test_reg_reg_reg(list,bitnumbersize,locsize,destsize,bitnumber,tmpreg,destreg);
  872. end;
  873. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  874. else
  875. internalerror(2010120411);
  876. end;
  877. end;
  878. procedure thlcgobj.a_bit_test_const_loc_reg(list: TAsmList; locsize, destsize: tdef; bitnumber: aint; const loc: tlocation; destreg: tregister);
  879. begin
  880. case loc.loc of
  881. LOC_REFERENCE,LOC_CREFERENCE:
  882. a_bit_test_const_ref_reg(list,locsize,destsize,bitnumber,loc.reference,destreg);
  883. LOC_REGISTER,LOC_CREGISTER:
  884. a_bit_test_const_reg_reg(list,locsize,destsize,bitnumber,loc.register,destreg);
  885. (* we don't have enough type information to handle this here
  886. LOC_SUBSETREG,LOC_CSUBSETREG:
  887. a_bit_test_const_subsetreg_reg(list,loc.size,destsize,bitnumber,loc.sreg,destreg);
  888. *)
  889. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  890. else
  891. internalerror(2010120410);
  892. end;
  893. end;
  894. procedure thlcgobj.a_bit_set_reg_loc(list: TAsmList; doset: boolean; fromsize, tosize: tdef; bitnumber: tregister; const loc: tlocation);
  895. begin
  896. case loc.loc of
  897. LOC_REFERENCE:
  898. a_bit_set_reg_ref(list,doset,fromsize,tosize,bitnumber,loc.reference);
  899. LOC_CREGISTER:
  900. a_bit_set_reg_reg(list,doset,fromsize,tosize,bitnumber,loc.register);
  901. (* we don't have enough type information to handle this here
  902. { e.g. a 2-byte set in a record regvar }
  903. LOC_CSUBSETREG:
  904. begin
  905. { hard to do in-place in a generic way, so operate on a copy }
  906. tmpreg:=getintregister(list,loc.size);
  907. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  908. a_bit_set_reg_reg(list,doset,bitnumbersize,loc.size,bitnumber,tmpreg);
  909. a_load_reg_subsetreg(list,loc.size,loc.size,tmpreg,loc.sreg);
  910. end;
  911. *)
  912. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  913. else
  914. internalerror(2010120408)
  915. end;
  916. end;
  917. procedure thlcgobj.a_bit_set_const_loc(list: TAsmList; doset: boolean; tosize: tdef; bitnumber: aint; const loc: tlocation);
  918. begin
  919. case loc.loc of
  920. LOC_REFERENCE:
  921. a_bit_set_const_ref(list,doset,tosize,bitnumber,loc.reference);
  922. LOC_CREGISTER:
  923. a_bit_set_const_reg(list,doset,tosize,bitnumber,loc.register);
  924. (* we don't have enough type information to handle this here
  925. LOC_CSUBSETREG:
  926. a_bit_set_const_subsetreg(list,doset,loc.size,bitnumber,loc.sreg);
  927. *)
  928. { LOC_SUBSETREF is not possible, because sets are not (yet) bitpacked }
  929. else
  930. internalerror(2010120409)
  931. end;
  932. end;
  933. procedure thlcgobj.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  934. var
  935. reg: tregister;
  936. regsize: tdef;
  937. begin
  938. if (fromsize.size>=tosize.size) then
  939. regsize:=fromsize
  940. else
  941. regsize:=tosize;
  942. reg:=getfpuregister(list,regsize);
  943. a_loadfpu_ref_reg(list,fromsize,regsize,ref1,reg);
  944. a_loadfpu_reg_ref(list,regsize,tosize,reg,ref2);
  945. end;
  946. procedure thlcgobj.a_loadfpu_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister);
  947. begin
  948. case loc.loc of
  949. LOC_REFERENCE, LOC_CREFERENCE:
  950. a_loadfpu_ref_reg(list,fromsize,tosize,loc.reference,reg);
  951. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  952. a_loadfpu_reg_reg(list,fromsize,tosize,loc.register,reg);
  953. else
  954. internalerror(2010120412);
  955. end;
  956. end;
  957. procedure thlcgobj.a_loadfpu_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation);
  958. begin
  959. case loc.loc of
  960. LOC_REFERENCE, LOC_CREFERENCE:
  961. a_loadfpu_reg_ref(list,fromsize,tosize,reg,loc.reference);
  962. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  963. a_loadfpu_reg_reg(list,fromsize,tosize,reg,loc.register);
  964. else
  965. internalerror(2010120413);
  966. end;
  967. end;
  968. procedure thlcgobj.a_loadfpu_reg_cgpara(list: TAsmList; fromsize: tdef; const r: tregister; const cgpara: TCGPara);
  969. var
  970. ref : treference;
  971. begin
  972. paramanager.alloccgpara(list,cgpara);
  973. case cgpara.location^.loc of
  974. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  975. begin
  976. cgpara.check_simple_location;
  977. a_loadfpu_reg_reg(list,fromsize,cgpara.def,r,cgpara.location^.register);
  978. end;
  979. LOC_REFERENCE,LOC_CREFERENCE:
  980. begin
  981. cgpara.check_simple_location;
  982. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  983. a_loadfpu_reg_ref(list,fromsize,cgpara.def,r,ref);
  984. end;
  985. LOC_REGISTER,LOC_CREGISTER:
  986. begin
  987. { paramfpu_ref does the check_simpe_location check here if necessary }
  988. tg.gethltemp(list,fromsize,fromsize.size,tt_normal,ref);
  989. a_loadfpu_reg_ref(list,fromsize,fromsize,r,ref);
  990. a_loadfpu_ref_cgpara(list,fromsize,ref,cgpara);
  991. tg.Ungettemp(list,ref);
  992. end;
  993. else
  994. internalerror(2010120422);
  995. end;
  996. end;
  997. procedure thlcgobj.a_loadfpu_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference; const cgpara: TCGPara);
  998. var
  999. href : treference;
  1000. // hsize: tcgsize;
  1001. begin
  1002. case cgpara.location^.loc of
  1003. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1004. begin
  1005. cgpara.check_simple_location;
  1006. paramanager.alloccgpara(list,cgpara);
  1007. a_loadfpu_ref_reg(list,fromsize,cgpara.def,ref,cgpara.location^.register);
  1008. end;
  1009. LOC_REFERENCE,LOC_CREFERENCE:
  1010. begin
  1011. cgpara.check_simple_location;
  1012. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1013. { concatcopy should choose the best way to copy the data }
  1014. g_concatcopy(list,fromsize,ref,href);
  1015. end;
  1016. (* not yet supported
  1017. LOC_REGISTER,LOC_CREGISTER:
  1018. begin
  1019. { force integer size }
  1020. hsize:=int_cgsize(tcgsize2size[size]);
  1021. {$ifndef cpu64bitalu}
  1022. if (hsize in [OS_S64,OS_64]) then
  1023. cg64.a_load64_ref_cgpara(list,ref,cgpara)
  1024. else
  1025. {$endif not cpu64bitalu}
  1026. begin
  1027. cgpara.check_simple_location;
  1028. a_load_ref_cgpara(list,hsize,ref,cgpara)
  1029. end;
  1030. end
  1031. *)
  1032. else
  1033. internalerror(2010120423);
  1034. end;
  1035. end;
  1036. (*
  1037. procedure thlcgobj.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle);
  1038. begin
  1039. cg.a_loadmm_reg_reg(list,def_cgsize(fromsize),def_cgsize(tosize),reg1,reg2,shuffle);
  1040. end;
  1041. procedure thlcgobj.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  1042. begin
  1043. cg.a_loadmm_ref_reg(list,def_cgsize(fromsize),def_cgsize(tosize),ref,reg,shuffle);
  1044. end;
  1045. procedure thlcgobj.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  1046. begin
  1047. cg.a_loadmm_reg_ref(list,def_cgsize(fromsize),def_cgsize(tosize),reg,ref,shuffle);
  1048. end;
  1049. procedure thlcgobj.a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle);
  1050. begin
  1051. case loc.loc of
  1052. LOC_MMREGISTER,LOC_CMMREGISTER:
  1053. a_loadmm_reg_reg(list,fromsize,tosize,loc.register,reg,shuffle);
  1054. LOC_REFERENCE,LOC_CREFERENCE:
  1055. a_loadmm_ref_reg(list,fromsize,tosize,loc.reference,reg,shuffle);
  1056. LOC_REGISTER,LOC_CREGISTER:
  1057. a_loadmm_intreg_reg(list,fromsize,tosize,loc.register,reg,shuffle);
  1058. else
  1059. internalerror(2010120414);
  1060. end;
  1061. end;
  1062. procedure thlcgobj.a_loadmm_reg_loc(list: TAsmList; fromsize, tosize: tdef; const reg: tregister; const loc: tlocation; shuffle: pmmshuffle);
  1063. begin
  1064. case loc.loc of
  1065. LOC_MMREGISTER,LOC_CMMREGISTER:
  1066. a_loadmm_reg_reg(list,fromsize,tosize,reg,loc.register,shuffle);
  1067. LOC_REFERENCE,LOC_CREFERENCE:
  1068. a_loadmm_reg_ref(list,fromsize,tosize,reg,loc.reference,shuffle);
  1069. else
  1070. internalerror(2010120415);
  1071. end;
  1072. end;
  1073. procedure thlcgobj.a_loadmm_reg_cgpara(list: TAsmList; fromsize: tdef; reg: tregister; const cgpara: TCGPara; shuffle: pmmshuffle);
  1074. var
  1075. href : treference;
  1076. begin
  1077. cgpara.check_simple_location;
  1078. paramanager.alloccgpara(list,cgpara);
  1079. case cgpara.location^.loc of
  1080. LOC_MMREGISTER,LOC_CMMREGISTER:
  1081. a_loadmm_reg_reg(list,fromsize,cgpara.def,reg,cgpara.location^.register,shuffle);
  1082. LOC_REFERENCE,LOC_CREFERENCE:
  1083. begin
  1084. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1085. a_loadmm_reg_ref(list,fromsize,cgpara.def,reg,href,shuffle);
  1086. end;
  1087. LOC_REGISTER,LOC_CREGISTER:
  1088. begin
  1089. if assigned(shuffle) and
  1090. not shufflescalar(shuffle) then
  1091. internalerror(2009112510);
  1092. a_loadmm_reg_intreg(list,deomsize,cgpara.def,reg,cgpara.location^.register,mms_movescalar);
  1093. end
  1094. else
  1095. internalerror(2010120427);
  1096. end;
  1097. end;
  1098. procedure thlcgobj.a_loadmm_ref_cgpara(list: TAsmList; fromsize: tdef; const ref: treference; const cgpara: TCGPara; shuffle: pmmshuffle);
  1099. var
  1100. hr : tregister;
  1101. hs : tmmshuffle;
  1102. begin
  1103. cgpara.check_simple_location;
  1104. hr:=cg.getmmregister(list,cgpara.size);
  1105. a_loadmm_ref_reg(list,deomsize,cgpara.def,ref,hr,shuffle);
  1106. if realshuffle(shuffle) then
  1107. begin
  1108. hs:=shuffle^;
  1109. removeshuffles(hs);
  1110. a_loadmm_reg_cgpara(list,cgpara.def,hr,cgpara,@hs);
  1111. end
  1112. else
  1113. a_loadmm_reg_cgpara(list,cgpara.def,hr,cgpara,shuffle);
  1114. end;
  1115. procedure thlcgobj.a_loadmm_loc_cgpara(list: TAsmList; fromsize: tdef; const loc: tlocation; const cgpara: TCGPara; shuffle: pmmshuffle);
  1116. begin
  1117. {$ifdef extdebug}
  1118. if def_cgsize(fromsize)<>loc.size then
  1119. internalerror(2010112105);
  1120. {$endif}
  1121. cg.a_loadmm_loc_cgpara(list,loc,cgpara,shuffle);
  1122. end;
  1123. procedure thlcgobj.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle);
  1124. begin
  1125. cg.a_opmm_reg_reg(list,op,def_cgsize(size),src,dst,shuffle);
  1126. end;
  1127. procedure thlcgobj.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  1128. begin
  1129. cg.a_opmm_ref_reg(list,op,def_cgsize(size),ref,reg,shuffle)
  1130. end;
  1131. procedure thlcgobj.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size: tdef; const loc: tlocation; reg: tregister; shuffle: pmmshuffle);
  1132. begin
  1133. cg.a_opmm_loc_reg(list,op,def_cgsize(size),loc,reg,shuffle);
  1134. end;
  1135. procedure thlcgobj.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  1136. begin
  1137. cg.a_opmm_reg_ref(list,op,def_cgsize(size),reg,ref,shuffle);
  1138. end;
  1139. *)
  1140. (*
  1141. procedure thlcgobj.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle);
  1142. begin
  1143. cg.a_loadmm_intreg_reg(list,def_cgsize(fromsize),def_cgsize(tosize),intreg,mmreg,shuffle);
  1144. end;
  1145. procedure thlcgobj.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle);
  1146. begin
  1147. cg.a_loadmm_reg_intreg(list,def_cgsize(fromsize),def_cgsize(tosize),mmreg,intreg,shuffle);
  1148. end;
  1149. *)
  1150. procedure thlcgobj.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference);
  1151. var
  1152. tmpreg : tregister;
  1153. begin
  1154. tmpreg:=getintregister(list,size);
  1155. a_load_ref_reg(list,size,size,ref,tmpreg);
  1156. a_op_const_reg(list,op,size,a,tmpreg);
  1157. a_load_reg_ref(list,size,size,tmpreg,ref);
  1158. end;
  1159. procedure thlcgobj.a_op_const_subsetreg(list: TAsmList; Op: TOpCG; size, subsetsize: tdef; a: aint; const sreg: tsubsetregister);
  1160. var
  1161. tmpreg: tregister;
  1162. begin
  1163. tmpreg:=getintregister(list,size);
  1164. a_load_subsetreg_reg(list,size,subsetsize,size,sreg,tmpreg);
  1165. a_op_const_reg(list,op,size,a,tmpreg);
  1166. a_load_reg_subsetreg(list,size,size,subsetsize,tmpreg,sreg);
  1167. end;
  1168. procedure thlcgobj.a_op_const_subsetref(list: TAsmList; Op: TOpCG; size, subsetsize: tdef; a: aint; const sref: tsubsetreference);
  1169. var
  1170. tmpreg: tregister;
  1171. begin
  1172. tmpreg:=getintregister(list,size);
  1173. a_load_subsetref_reg(list,size,subsetsize,size,sref,tmpreg);
  1174. a_op_const_reg(list,op,size,a,tmpreg);
  1175. a_load_reg_subsetref(list,size,size,subsetsize,tmpreg,sref);
  1176. end;
  1177. procedure thlcgobj.a_op_const_loc(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const loc: tlocation);
  1178. begin
  1179. case loc.loc of
  1180. LOC_REGISTER, LOC_CREGISTER:
  1181. a_op_const_reg(list,op,size,a,loc.register);
  1182. LOC_REFERENCE, LOC_CREFERENCE:
  1183. a_op_const_ref(list,op,size,a,loc.reference);
  1184. { we don't have enough type information to handle these here
  1185. LOC_SUBSETREG, LOC_CSUBSETREG:
  1186. a_op_const_subsetreg(list,op,loc.size,loc.size,a,loc.sreg);
  1187. LOC_SUBSETREF, LOC_CSUBSETREF:
  1188. a_op_const_subsetref(list,op,loc.size,loc.size,a,loc.sref);
  1189. }
  1190. else
  1191. internalerror(2010120428);
  1192. end;
  1193. end;
  1194. procedure thlcgobj.a_op_reg_ref(list: TAsmList; Op: TOpCG; size: tdef; reg: TRegister; const ref: TReference);
  1195. var
  1196. tmpreg: tregister;
  1197. begin
  1198. case op of
  1199. OP_NOT,OP_NEG:
  1200. { handle it as "load ref,reg; op reg" }
  1201. begin
  1202. a_load_ref_reg(list,size,size,ref,reg);
  1203. a_op_reg_reg(list,op,size,reg,reg);
  1204. end;
  1205. else
  1206. begin
  1207. tmpreg:=getintregister(list,size);
  1208. a_load_ref_reg(list,size,size,ref,tmpreg);
  1209. a_op_reg_reg(list,op,size,tmpreg,reg);
  1210. end;
  1211. end;
  1212. end;
  1213. procedure thlcgobj.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  1214. var
  1215. tmpreg: tregister;
  1216. begin
  1217. case op of
  1218. OP_NOT,OP_NEG:
  1219. { handle it as "load ref,reg; op reg" }
  1220. begin
  1221. a_load_ref_reg(list,size,size,ref,reg);
  1222. a_op_reg_reg(list,op,size,reg,reg);
  1223. end;
  1224. else
  1225. begin
  1226. tmpreg:=getintregister(list,size);
  1227. a_load_ref_reg(list,size,size,ref,tmpreg);
  1228. a_op_reg_reg(list,op,size,tmpreg,reg);
  1229. end;
  1230. end;
  1231. end;
  1232. procedure thlcgobj.a_op_reg_subsetreg(list: TAsmList; Op: TOpCG; opsize, destsize, destsubsetsize: tdef; reg: TRegister; const sreg: tsubsetregister);
  1233. var
  1234. tmpreg: tregister;
  1235. begin
  1236. tmpreg:=getintregister(list,opsize);
  1237. a_load_subsetreg_reg(list,destsize,destsubsetsize,opsize,sreg,tmpreg);
  1238. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  1239. a_load_reg_subsetreg(list,opsize,destsize,destsubsetsize,tmpreg,sreg);
  1240. end;
  1241. procedure thlcgobj.a_op_reg_subsetref(list: TAsmList; Op: TOpCG; opsize, destsize, destsubsetsize: tdef; reg: TRegister; const sref: tsubsetreference);
  1242. var
  1243. tmpreg: tregister;
  1244. begin
  1245. tmpreg:=getintregister(list,opsize);
  1246. a_load_subsetref_reg(list,destsize,destsubsetsize,opsize,sref,tmpreg);
  1247. a_op_reg_reg(list,op,opsize,reg,tmpreg);
  1248. a_load_reg_subsetref(list,opsize,destsize,destsubsetsize,tmpreg,sref);
  1249. end;
  1250. procedure thlcgobj.a_op_reg_loc(list: TAsmList; Op: TOpCG; size: tdef; reg: tregister; const loc: tlocation);
  1251. begin
  1252. case loc.loc of
  1253. LOC_REGISTER, LOC_CREGISTER:
  1254. a_op_reg_reg(list,op,size,reg,loc.register);
  1255. LOC_REFERENCE, LOC_CREFERENCE:
  1256. a_op_reg_ref(list,op,size,reg,loc.reference);
  1257. { we don't have enough type information to handle these here
  1258. LOC_SUBSETREG, LOC_CSUBSETREG:
  1259. a_op_reg_subsetreg(list,op,loc.size,loc.size,reg,loc.sreg);
  1260. LOC_SUBSETREF, LOC_CSUBSETREF:
  1261. a_op_reg_subsetref(list,op,loc.size,loc.size,reg,loc.sref);
  1262. }
  1263. else
  1264. internalerror(2010120429);
  1265. end;
  1266. end;
  1267. procedure thlcgobj.a_op_ref_loc(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; const loc: tlocation);
  1268. var
  1269. tmpreg: tregister;
  1270. begin
  1271. case loc.loc of
  1272. LOC_REGISTER,LOC_CREGISTER:
  1273. a_op_ref_reg(list,op,size,ref,loc.register);
  1274. LOC_REFERENCE,LOC_CREFERENCE:
  1275. begin
  1276. tmpreg:=getintregister(list,size);
  1277. a_load_ref_reg(list,size,size,ref,tmpreg);
  1278. a_op_reg_ref(list,op,size,tmpreg,loc.reference);
  1279. end;
  1280. { we don't have enough type information to handle these here
  1281. LOC_SUBSETREG, LOC_CSUBSETREG:
  1282. begin
  1283. tmpreg:=getintregister(list,loc.size);
  1284. a_load_subsetreg_reg(list,loc.size,loc.size,loc.sreg,tmpreg);
  1285. a_op_ref_reg(list,op,loc.size,ref,tmpreg);
  1286. a_load_reg_subsetreg(list,loc.size,loc.size,tmpreg,loc.sreg);
  1287. end;
  1288. LOC_SUBSETREF, LOC_CSUBSETREF:
  1289. begin
  1290. tmpreg:=getintregister(list,loc.size);
  1291. a_load_subsetreF_reg(list,loc.size,loc.size,loc.sref,tmpreg);
  1292. a_op_ref_reg(list,op,loc.size,ref,tmpreg);
  1293. a_load_reg_subsetref(list,loc.size,loc.size,tmpreg,loc.sref);
  1294. end;
  1295. }
  1296. else
  1297. internalerror(2010120429);
  1298. end;
  1299. end;
  1300. procedure thlcgobj.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister);
  1301. begin
  1302. a_load_reg_reg(list,size,size,src,dst);
  1303. a_op_const_reg(list,op,size,a,dst);
  1304. end;
  1305. procedure thlcgobj.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  1306. var
  1307. tmpreg: tregister;
  1308. begin
  1309. if (dst<>src1) then
  1310. begin
  1311. a_load_reg_reg(list,size,size,src2,dst);
  1312. a_op_reg_reg(list,op,size,src1,dst);
  1313. end
  1314. else
  1315. begin
  1316. { can we do a direct operation on the target register ? }
  1317. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  1318. a_op_reg_reg(list,op,size,src2,dst)
  1319. else
  1320. begin
  1321. tmpreg:=getintregister(list,size);
  1322. a_load_reg_reg(list,size,size,src2,tmpreg);
  1323. a_op_reg_reg(list,op,size,src1,tmpreg);
  1324. a_load_reg_reg(list,size,size,tmpreg,dst);
  1325. end;
  1326. end;
  1327. end;
  1328. procedure thlcgobj.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1329. begin
  1330. if not setflags then
  1331. a_op_const_reg_reg(list,op,size,a,src,dst)
  1332. else
  1333. internalerror(2010122910);
  1334. end;
  1335. procedure thlcgobj.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1336. begin
  1337. if not setflags then
  1338. a_op_reg_reg_reg(list,op,size,src1,src2,dst)
  1339. else
  1340. internalerror(2010122911);
  1341. end;
  1342. procedure thlcgobj.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  1343. var
  1344. tmpreg: tregister;
  1345. begin
  1346. tmpreg:=getintregister(list,size);
  1347. a_load_const_reg(list,size,a,tmpreg);
  1348. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1349. end;
  1350. procedure thlcgobj.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const ref: treference; l: tasmlabel);
  1351. var
  1352. tmpreg: tregister;
  1353. begin
  1354. tmpreg:=getintregister(list,size);
  1355. a_load_ref_reg(list,size,size,ref,tmpreg);
  1356. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1357. end;
  1358. procedure thlcgobj.a_cmp_const_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const loc: tlocation; l: tasmlabel);
  1359. begin
  1360. case loc.loc of
  1361. LOC_REGISTER,LOC_CREGISTER:
  1362. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1363. LOC_REFERENCE,LOC_CREFERENCE:
  1364. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1365. { we don't have enough type information to handle these here
  1366. LOC_SUBSETREG, LOC_CSUBSETREG:
  1367. begin
  1368. tmpreg:=getintregister(list,size);
  1369. a_load_subsetreg_reg(list,loc.size,size,loc.sreg,tmpreg);
  1370. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1371. end;
  1372. LOC_SUBSETREF, LOC_CSUBSETREF:
  1373. begin
  1374. tmpreg:=getintregister(list,size);
  1375. a_load_subsetref_reg(list,loc.size,size,loc.sref,tmpreg);
  1376. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1377. end;
  1378. }
  1379. else
  1380. internalerror(2010120430);
  1381. end;
  1382. end;
  1383. procedure thlcgobj.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  1384. var
  1385. tmpreg: tregister;
  1386. begin
  1387. tmpreg:=getintregister(list,size);
  1388. a_load_ref_reg(list,size,size,ref,tmpreg);
  1389. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1390. end;
  1391. procedure thlcgobj.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  1392. var
  1393. tmpreg: tregister;
  1394. begin
  1395. tmpreg:=getintregister(list,size);
  1396. a_load_ref_reg(list,size,size,ref,tmpreg);
  1397. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  1398. end;
  1399. procedure thlcgobj.a_cmp_subsetreg_reg_label(list: TAsmList; fromsize, fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sreg: tsubsetregister; reg: tregister; l: tasmlabel);
  1400. var
  1401. tmpreg: tregister;
  1402. begin
  1403. tmpreg:=getintregister(list,cmpsize);
  1404. a_load_subsetreg_reg(list,fromsize,fromsubsetsize,cmpsize,sreg,tmpreg);
  1405. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  1406. end;
  1407. procedure thlcgobj.a_cmp_subsetref_reg_label(list: TAsmList; fromsize, fromsubsetsize, cmpsize: tdef; cmp_op: topcmp; const sref: tsubsetreference; reg: tregister; l: tasmlabel);
  1408. var
  1409. tmpreg: tregister;
  1410. begin
  1411. tmpreg:=getintregister(list,cmpsize);
  1412. a_load_subsetref_reg(list,fromsize,fromsubsetsize,cmpsize,sref,tmpreg);
  1413. a_cmp_reg_reg_label(list,cmpsize,cmp_op,tmpreg,reg,l);
  1414. end;
  1415. procedure thlcgobj.a_cmp_loc_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const loc: tlocation; reg: tregister; l: tasmlabel);
  1416. begin
  1417. case loc.loc of
  1418. LOC_REGISTER,
  1419. LOC_CREGISTER:
  1420. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1421. LOC_REFERENCE,
  1422. LOC_CREFERENCE :
  1423. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1424. LOC_CONSTANT:
  1425. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1426. { we don't have enough type information to handle these here
  1427. LOC_SUBSETREG,
  1428. LOC_CSUBSETREG:
  1429. a_cmp_subsetreg_reg_label(list,loc.size,size,cmp_op,loc.sreg,reg,l);
  1430. LOC_SUBSETREF,
  1431. LOC_CSUBSETREF:
  1432. a_cmp_subsetref_reg_label(list,loc.size,size,cmp_op,loc.sref,reg,l);
  1433. }
  1434. else
  1435. internalerror(2010120431);
  1436. end;
  1437. end;
  1438. procedure thlcgobj.a_cmp_reg_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const loc: tlocation; l: tasmlabel);
  1439. begin
  1440. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  1441. end;
  1442. procedure thlcgobj.a_cmp_ref_loc_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; const loc: tlocation; l: tasmlabel);
  1443. var
  1444. tmpreg: tregister;
  1445. begin
  1446. case loc.loc of
  1447. LOC_REGISTER,LOC_CREGISTER:
  1448. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1449. LOC_REFERENCE,LOC_CREFERENCE:
  1450. begin
  1451. tmpreg:=getintregister(list,size);
  1452. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1453. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1454. end;
  1455. LOC_CONSTANT:
  1456. begin
  1457. a_cmp_const_ref_label(list,size,swap_opcmp(cmp_op),loc.value,ref,l);
  1458. end
  1459. { we don't have enough type information to handle these here
  1460. LOC_SUBSETREG, LOC_CSUBSETREG:
  1461. begin
  1462. tmpreg:=getintregister(list, size);
  1463. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1464. a_cmp_subsetreg_reg_label(list,loc.size,size,swap_opcmp(cmp_op),loc.sreg,tmpreg,l);
  1465. end;
  1466. LOC_SUBSETREF, LOC_CSUBSETREF:
  1467. begin
  1468. tmpreg:=getintregister(list, size);
  1469. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1470. a_cmp_subsetref_reg_label(list,loc.size,size,swap_opcmp(cmp_op),loc.sref,tmpreg,l);
  1471. end;
  1472. }
  1473. else
  1474. internalerror(2010120432);
  1475. end;
  1476. end;
  1477. procedure thlcgobj.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  1478. begin
  1479. {
  1480. if use_vectorfpu(size) then
  1481. a_loadmm_ref_ref()
  1482. else
  1483. }
  1484. if size.typ<>floatdef then
  1485. a_load_ref_ref(list,size,size,source,dest)
  1486. else
  1487. a_loadfpu_ref_ref(list,size,size,source,dest);
  1488. end;
  1489. procedure thlcgobj.g_concatcopy_unaligned(list: TAsmList; size: tdef; const source, dest: treference);
  1490. begin
  1491. g_concatcopy(list,size,source,dest);
  1492. end;
  1493. procedure thlcgobj.g_rangecheck(list: TAsmList; const l: tlocation; fromdef, todef: tdef);
  1494. begin
  1495. if not(cs_check_range in current_settings.localswitches) then
  1496. exit;
  1497. internalerror(2011010610);
  1498. end;
  1499. procedure thlcgobj.g_profilecode(list: TAsmList);
  1500. begin
  1501. end;
  1502. procedure thlcgobj.location_force_reg(list: TAsmList; var l: tlocation; src_size, dst_size: tdef; maybeconst: boolean);
  1503. var
  1504. hregister,
  1505. hregister2: tregister;
  1506. hl : tasmlabel;
  1507. oldloc : tlocation;
  1508. begin
  1509. oldloc:=l;
  1510. hregister:=getregisterfordef(list,dst_size);
  1511. { load value in new register }
  1512. case l.loc of
  1513. {$ifdef cpuflags}
  1514. LOC_FLAGS :
  1515. g_flags2reg(list,dst_size,l.resflags,hregister);
  1516. {$endif cpuflags}
  1517. LOC_JUMP :
  1518. begin
  1519. a_label(list,current_procinfo.CurrTrueLabel);
  1520. a_load_const_reg(list,dst_size,1,hregister);
  1521. current_asmdata.getjumplabel(hl);
  1522. a_jmp_always(list,hl);
  1523. a_label(list,current_procinfo.CurrFalseLabel);
  1524. a_load_const_reg(list,dst_size,0,hregister);
  1525. a_label(list,hl);
  1526. end;
  1527. else
  1528. begin
  1529. { load_loc_reg can only handle size >= l.size, when the
  1530. new size is smaller then we need to adjust the size
  1531. of the orignal and maybe recalculate l.register for i386 }
  1532. if (dst_size.size<src_size.size) then
  1533. begin
  1534. hregister2:=getregisterfordef(list,src_size);
  1535. { prevent problems with memory locations -- at this high
  1536. level we cannot twiddle with the reference offset, since
  1537. that may not mean anything (e.g., it refers to fixed-sized
  1538. stack slots on Java) }
  1539. a_load_loc_reg(list,src_size,src_size,l,hregister2);
  1540. a_load_reg_reg(list,src_size,dst_size,hregister2,hregister);
  1541. end
  1542. else
  1543. a_load_loc_reg(list,src_size,dst_size,l,hregister);
  1544. end;
  1545. end;
  1546. if (l.loc <> LOC_CREGISTER) or
  1547. not maybeconst then
  1548. location_reset(l,LOC_REGISTER,def_cgsize(dst_size))
  1549. else
  1550. location_reset(l,LOC_CREGISTER,def_cgsize(dst_size));
  1551. l.register:=hregister;
  1552. { Release temp if it was a reference }
  1553. if oldloc.loc=LOC_REFERENCE then
  1554. location_freetemp(list,oldloc);
  1555. end;
  1556. procedure thlcgobj.location_force_fpureg(list: TAsmList; var l: tlocation; size: tdef; maybeconst: boolean);
  1557. var
  1558. reg : tregister;
  1559. begin
  1560. if (l.loc<>LOC_FPUREGISTER) and
  1561. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  1562. begin
  1563. { if it's in an mm register, store to memory first }
  1564. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  1565. internalerror(2011012903);
  1566. reg:=getfpuregister(list,size);
  1567. a_loadfpu_loc_reg(list,size,size,l,reg);
  1568. location_freetemp(list,l);
  1569. location_reset(l,LOC_FPUREGISTER,l.size);
  1570. l.register:=reg;
  1571. end;
  1572. end;
  1573. procedure thlcgobj.location_force_mem(list: TAsmList; var l: tlocation; size: tdef);
  1574. var
  1575. r : treference;
  1576. begin
  1577. case l.loc of
  1578. LOC_FPUREGISTER,
  1579. LOC_CFPUREGISTER :
  1580. begin
  1581. tg.gethltemp(list,size,size.size,tt_normal,r);
  1582. a_loadfpu_reg_ref(list,size,size,l.register,r);
  1583. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1584. l.reference:=r;
  1585. end;
  1586. (*
  1587. LOC_MMREGISTER,
  1588. LOC_CMMREGISTER:
  1589. begin
  1590. tg.gethltemp(list,size,size.size,tt_normal,r);
  1591. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  1592. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1593. l.reference:=r;
  1594. end;
  1595. *)
  1596. LOC_CONSTANT,
  1597. LOC_REGISTER,
  1598. LOC_CREGISTER :
  1599. begin
  1600. tg.gethltemp(list,size,size.size,tt_normal,r);
  1601. a_load_loc_ref(list,size,size,l,r);
  1602. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1603. l.reference:=r;
  1604. end;
  1605. (*
  1606. LOC_SUBSETREG,
  1607. LOC_CSUBSETREG,
  1608. LOC_SUBSETREF,
  1609. LOC_CSUBSETREF:
  1610. begin
  1611. tg.gethltemp(list,size,size.size,tt_normal,r);
  1612. cg.a_load_loc_ref(list,l.size,l,r);
  1613. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1614. l.reference:=r;
  1615. end;
  1616. *)
  1617. LOC_CREFERENCE,
  1618. LOC_REFERENCE : ;
  1619. else
  1620. internalerror(2011010304);
  1621. end;
  1622. end;
  1623. procedure thlcgobj.location_get_data_ref(list: TAsmList; def: tdef; const l: tlocation; var ref: treference; loadref: boolean; alignment: longint);
  1624. begin
  1625. case l.loc of
  1626. LOC_REGISTER,
  1627. LOC_CREGISTER :
  1628. begin
  1629. if not loadref then
  1630. internalerror(200410231);
  1631. reference_reset_base(ref,l.register,0,alignment);
  1632. end;
  1633. LOC_REFERENCE,
  1634. LOC_CREFERENCE :
  1635. begin
  1636. if loadref then
  1637. begin
  1638. reference_reset_base(ref,getaddressregister(list,voidpointertype),0,alignment);
  1639. { it's a pointer to def }
  1640. a_load_ref_reg(list,voidpointertype,voidpointertype,l.reference,ref.base);
  1641. end
  1642. else
  1643. ref:=l.reference;
  1644. end;
  1645. else
  1646. internalerror(200309181);
  1647. end;
  1648. end;
  1649. procedure thlcgobj.maketojumpbool(list: TAsmList; p: tnode);
  1650. {
  1651. produces jumps to true respectively false labels using boolean expressions
  1652. depending on whether the loading of regvars is currently being
  1653. synchronized manually (such as in an if-node) or automatically (most of
  1654. the other cases where this procedure is called), loadregvars can be
  1655. "lr_load_regvars" or "lr_dont_load_regvars"
  1656. }
  1657. var
  1658. storepos : tfileposinfo;
  1659. begin
  1660. if nf_error in p.flags then
  1661. exit;
  1662. storepos:=current_filepos;
  1663. current_filepos:=p.fileinfo;
  1664. if is_boolean(p.resultdef) then
  1665. begin
  1666. if is_constboolnode(p) then
  1667. begin
  1668. if Tordconstnode(p).value.uvalue<>0 then
  1669. a_jmp_always(list,current_procinfo.CurrTrueLabel)
  1670. else
  1671. a_jmp_always(list,current_procinfo.CurrFalseLabel)
  1672. end
  1673. else
  1674. begin
  1675. case p.location.loc of
  1676. (*
  1677. LOC_SUBSETREG,LOC_CSUBSETREG,
  1678. LOC_SUBSETREF,LOC_CSUBSETREF:
  1679. begin
  1680. tmpreg := cg.getintregister(list,OS_INT);
  1681. cg.a_load_loc_reg(list,OS_INT,p.location,tmpreg);
  1682. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,tmpreg,current_procinfo.CurrTrueLabel);
  1683. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  1684. end;
  1685. *)
  1686. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  1687. begin
  1688. a_cmp_const_loc_label(list,p.resultdef,OC_NE,0,p.location,current_procinfo.CurrTrueLabel);
  1689. a_jmp_always(list,current_procinfo.CurrFalseLabel);
  1690. end;
  1691. LOC_JUMP:
  1692. ;
  1693. {$ifdef cpuflags}
  1694. LOC_FLAGS :
  1695. begin
  1696. a_jmp_flags(list,p.location.resflags,current_procinfo.CurrTrueLabel);
  1697. a_jmp_always(list,current_procinfo.CurrFalseLabel);
  1698. end;
  1699. {$endif cpuflags}
  1700. else
  1701. begin
  1702. printnode(output,p);
  1703. internalerror(2011010418);
  1704. end;
  1705. end;
  1706. end;
  1707. end
  1708. else
  1709. internalerror(2011010419);
  1710. current_filepos:=storepos;
  1711. end;
  1712. procedure thlcgobj.gen_proc_symbol(list: TAsmList);
  1713. var
  1714. item,
  1715. previtem : TCmdStrListItem;
  1716. begin
  1717. previtem:=nil;
  1718. item := TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
  1719. while assigned(item) do
  1720. begin
  1721. {$ifdef arm}
  1722. if current_settings.cputype in cpu_thumb2 then
  1723. list.concat(tai_thumb_func.create);
  1724. {$endif arm}
  1725. { "double link" all procedure entry symbols via .reference }
  1726. { directives on darwin, because otherwise the linker }
  1727. { sometimes strips the procedure if only on of the symbols }
  1728. { is referenced }
  1729. if assigned(previtem) and
  1730. (target_info.system in systems_darwin) then
  1731. list.concat(tai_directive.create(asd_reference,item.str));
  1732. if (cs_profile in current_settings.moduleswitches) or
  1733. (po_global in current_procinfo.procdef.procoptions) then
  1734. list.concat(Tai_symbol.createname_global(item.str,AT_FUNCTION,0))
  1735. else
  1736. list.concat(Tai_symbol.createname(item.str,AT_FUNCTION,0));
  1737. if assigned(previtem) and
  1738. (target_info.system in systems_darwin) then
  1739. list.concat(tai_directive.create(asd_reference,previtem.str));
  1740. if not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  1741. list.concat(Tai_function_name.create(item.str));
  1742. previtem:=item;
  1743. item := TCmdStrListItem(item.next);
  1744. end;
  1745. current_procinfo.procdef.procstarttai:=tai(list.last);
  1746. end;
  1747. procedure thlcgobj.gen_proc_symbol_end(list: TAsmList);
  1748. begin
  1749. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1750. current_procinfo.procdef.procendtai:=tai(list.last);
  1751. if (current_module.islibrary) then
  1752. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  1753. { setinitname may generate a new section -> don't add to the
  1754. current list, because we assume this remains a text section }
  1755. exportlib.setinitname(current_asmdata.AsmLists[al_exports],current_procinfo.procdef.mangledname);
  1756. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1757. begin
  1758. if (target_info.system in (systems_darwin+[system_powerpc_macos])) and
  1759. not(current_module.islibrary) then
  1760. begin
  1761. new_section(list,sec_code,'',4);
  1762. list.concat(tai_symbol.createname_global(
  1763. target_info.cprefix+mainaliasname,AT_FUNCTION,0));
  1764. { keep argc, argv and envp properly on the stack }
  1765. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  1766. end;
  1767. end;
  1768. end;
  1769. procedure thlcgobj.gen_initialize_code(list: TAsmList);
  1770. begin
  1771. { initialize local data like ansistrings }
  1772. case current_procinfo.procdef.proctypeoption of
  1773. potype_unitinit:
  1774. begin
  1775. { this is also used for initialization of variables in a
  1776. program which does not have a globalsymtable }
  1777. if assigned(current_module.globalsymtable) then
  1778. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@initialize_data,list);
  1779. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_data,list);
  1780. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  1781. end;
  1782. { units have seperate code for initilization and finalization }
  1783. potype_unitfinalize: ;
  1784. { program init/final is generated in separate procedure }
  1785. potype_proginit:
  1786. begin
  1787. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  1788. end;
  1789. else
  1790. current_procinfo.procdef.localst.SymList.ForEachCall(@initialize_data,list);
  1791. end;
  1792. { initialisizes temp. ansi/wide string data }
  1793. inittempvariables(list);
  1794. {$ifdef OLDREGVARS}
  1795. load_regvars(list,nil);
  1796. {$endif OLDREGVARS}
  1797. end;
  1798. procedure thlcgobj.gen_finalize_code(list: TAsmList);
  1799. begin
  1800. {$ifdef OLDREGVARS}
  1801. cleanup_regvars(list);
  1802. {$endif OLDREGVARS}
  1803. { finalize temporary data }
  1804. finalizetempvariables(list);
  1805. { finalize local data like ansistrings}
  1806. case current_procinfo.procdef.proctypeoption of
  1807. potype_unitfinalize:
  1808. begin
  1809. { this is also used for initialization of variables in a
  1810. program which does not have a globalsymtable }
  1811. if assigned(current_module.globalsymtable) then
  1812. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@finalize_static_data,list);
  1813. TSymtable(current_module.localsymtable).SymList.ForEachCall(@finalize_static_data,list);
  1814. end;
  1815. { units/progs have separate code for initialization and finalization }
  1816. potype_unitinit: ;
  1817. { program init/final is generated in separate procedure }
  1818. potype_proginit: ;
  1819. else
  1820. current_procinfo.procdef.localst.SymList.ForEachCall(@finalize_local_vars,list);
  1821. end;
  1822. { finalize paras data }
  1823. if assigned(current_procinfo.procdef.parast) and
  1824. not(po_assembler in current_procinfo.procdef.procoptions) then
  1825. current_procinfo.procdef.parast.SymList.ForEachCall(@final_paras,list);
  1826. end;
  1827. procedure thlcgobj.gen_entry_code(list: TAsmList);
  1828. begin
  1829. { the actual profile code can clobber some registers,
  1830. therefore if the context must be saved, do it before
  1831. the actual call to the profile code
  1832. }
  1833. if (cs_profile in current_settings.moduleswitches) and
  1834. not(po_assembler in current_procinfo.procdef.procoptions) then
  1835. begin
  1836. { non-win32 can call mcout even in main }
  1837. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  1838. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1839. begin
  1840. g_profilecode(list);
  1841. end;
  1842. end;
  1843. { TODO: create high level version (create compilerprocs in system unit,
  1844. look up procdef, use hlcgobj.a_call_name()) }
  1845. { call startup helpers from main program }
  1846. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1847. begin
  1848. { initialize units }
  1849. cg.allocallcpuregisters(list);
  1850. if not(current_module.islibrary) then
  1851. cg.a_call_name(list,'FPC_INITIALIZEUNITS',false)
  1852. else
  1853. cg.a_call_name(list,'FPC_LIBINITIALIZEUNITS',false);
  1854. cg.deallocallcpuregisters(list);
  1855. end;
  1856. list.concat(Tai_force_line.Create);
  1857. {$ifdef OLDREGVARS}
  1858. load_regvars(list,nil);
  1859. {$endif OLDREGVARS}
  1860. end;
  1861. procedure thlcgobj.gen_exit_code(list: TAsmList);
  1862. begin
  1863. { TODO: create high level version (create compilerproc in system unit,
  1864. look up procdef, use hlcgobj.a_call_name()) }
  1865. { call __EXIT for main program }
  1866. if (not DLLsource) and
  1867. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1868. cg.a_call_name(list,'FPC_DO_EXIT',false);
  1869. end;
  1870. procedure thlcgobj.inittempvariables(list: TAsmList);
  1871. var
  1872. hp : ptemprecord;
  1873. href : treference;
  1874. begin
  1875. hp:=tg.templist;
  1876. while assigned(hp) do
  1877. begin
  1878. if assigned(hp^.def) and
  1879. is_managed_type(hp^.def) then
  1880. begin
  1881. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  1882. g_initialize(list,hp^.def,href);
  1883. end;
  1884. hp:=hp^.next;
  1885. end;
  1886. end;
  1887. procedure thlcgobj.initialize_data(p: TObject; arg: pointer);
  1888. var
  1889. OldAsmList : TAsmList;
  1890. hp : tnode;
  1891. begin
  1892. if (tsym(p).typ = localvarsym) and
  1893. { local (procedure or unit) variables only need initialization if
  1894. they are used }
  1895. ((tabstractvarsym(p).refs>0) or
  1896. { managed return symbols must be inited }
  1897. ((tsym(p).typ=localvarsym) and (vo_is_funcret in tlocalvarsym(p).varoptions))
  1898. ) and
  1899. not(vo_is_typed_const in tabstractvarsym(p).varoptions) and
  1900. not(vo_is_external in tabstractvarsym(p).varoptions) and
  1901. (is_managed_type(tabstractvarsym(p).vardef) or
  1902. ((m_iso in current_settings.modeswitches) and (tabstractvarsym(p).vardef.typ=filedef))
  1903. ) then
  1904. begin
  1905. OldAsmList:=current_asmdata.CurrAsmList;
  1906. current_asmdata.CurrAsmList:=TAsmList(arg);
  1907. hp:=cnodeutils.initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner));
  1908. firstpass(hp);
  1909. secondpass(hp);
  1910. hp.free;
  1911. current_asmdata.CurrAsmList:=OldAsmList;
  1912. end;
  1913. end;
  1914. procedure thlcgobj.finalizetempvariables(list: TAsmList);
  1915. var
  1916. hp : ptemprecord;
  1917. href : treference;
  1918. begin
  1919. hp:=tg.templist;
  1920. while assigned(hp) do
  1921. begin
  1922. if assigned(hp^.def) and
  1923. is_managed_type(hp^.def) then
  1924. begin
  1925. include(current_procinfo.flags,pi_needs_implicit_finally);
  1926. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  1927. g_finalize(list,hp^.def,href);
  1928. end;
  1929. hp:=hp^.next;
  1930. end;
  1931. end;
  1932. procedure thlcgobj.initialize_regvars(p: TObject; arg: pointer);
  1933. var
  1934. href : treference;
  1935. begin
  1936. if (tsym(p).typ=staticvarsym) then
  1937. begin
  1938. { Static variables can have the initialloc only set to LOC_CxREGISTER
  1939. or LOC_INVALID, for explaination see gen_alloc_symtable (PFV) }
  1940. case tstaticvarsym(p).initialloc.loc of
  1941. LOC_CREGISTER :
  1942. begin
  1943. {$ifndef cpu64bitalu}
  1944. if (tstaticvarsym(p).initialloc.size in [OS_64,OS_S64]) then
  1945. cg64.a_load64_const_reg(TAsmList(arg),0,tstaticvarsym(p).initialloc.register64)
  1946. else
  1947. {$endif not cpu64bitalu}
  1948. a_load_const_reg(TAsmList(arg),tstaticvarsym(p).vardef,0,
  1949. tstaticvarsym(p).initialloc.register);
  1950. end;
  1951. (*
  1952. LOC_CMMREGISTER :
  1953. { clear the whole register }
  1954. cg.a_opmm_reg_reg(TAsmList(arg),OP_XOR,reg_cgsize(tstaticvarsym(p).initialloc.register),
  1955. tstaticvarsym(p).initialloc.register,
  1956. tstaticvarsym(p).initialloc.register,
  1957. nil);
  1958. *)
  1959. LOC_CFPUREGISTER :
  1960. begin
  1961. { initialize fpu regvar by loading from memory }
  1962. reference_reset_symbol(href,
  1963. current_asmdata.RefAsmSymbol(tstaticvarsym(p).mangledname), 0,
  1964. var_align(tstaticvarsym(p).vardef.alignment));
  1965. a_loadfpu_ref_reg(TAsmList(arg), tstaticvarsym(p).vardef,
  1966. tstaticvarsym(p).vardef, href, tstaticvarsym(p).initialloc.register);
  1967. end;
  1968. LOC_INVALID :
  1969. ;
  1970. else
  1971. internalerror(200410124);
  1972. end;
  1973. end;
  1974. end;
  1975. procedure thlcgobj.finalize_sym(asmlist: TAsmList; sym: tsym);
  1976. var
  1977. hp : tnode;
  1978. OldAsmList : TAsmList;
  1979. begin
  1980. include(current_procinfo.flags,pi_needs_implicit_finally);
  1981. OldAsmList:=current_asmdata.CurrAsmList;
  1982. current_asmdata.CurrAsmList:=asmlist;
  1983. hp:=cloadnode.create(sym,sym.owner);
  1984. if (sym.typ=staticvarsym) and (vo_force_finalize in tstaticvarsym(sym).varoptions) then
  1985. include(hp.flags,nf_isinternal_ignoreconst);
  1986. hp:=cnodeutils.finalize_data_node(hp);
  1987. firstpass(hp);
  1988. secondpass(hp);
  1989. hp.free;
  1990. current_asmdata.CurrAsmList:=OldAsmList;
  1991. end;
  1992. procedure thlcgobj.finalize_local_vars(p: TObject; arg: pointer);
  1993. begin
  1994. if (tsym(p).typ=localvarsym) and
  1995. (tlocalvarsym(p).refs>0) and
  1996. not(vo_is_external in tlocalvarsym(p).varoptions) and
  1997. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  1998. is_managed_type(tlocalvarsym(p).vardef) then
  1999. finalize_sym(TAsmList(arg),tsym(p));
  2000. end;
  2001. procedure thlcgobj.finalize_static_data(p: TObject; arg: pointer);
  2002. var
  2003. i : longint;
  2004. pd : tprocdef;
  2005. begin
  2006. case tsym(p).typ of
  2007. staticvarsym :
  2008. begin
  2009. { local (procedure or unit) variables only need finalization
  2010. if they are used
  2011. }
  2012. if ((tstaticvarsym(p).refs>0) or
  2013. { global (unit) variables always need finalization, since
  2014. they may also be used in another unit
  2015. }
  2016. (tstaticvarsym(p).owner.symtabletype=globalsymtable)) and
  2017. (
  2018. (tstaticvarsym(p).varspez<>vs_const) or
  2019. (vo_force_finalize in tstaticvarsym(p).varoptions)
  2020. ) and
  2021. not(vo_is_funcret in tstaticvarsym(p).varoptions) and
  2022. not(vo_is_external in tstaticvarsym(p).varoptions) and
  2023. is_managed_type(tstaticvarsym(p).vardef) then
  2024. finalize_sym(TAsmList(arg),tsym(p));
  2025. end;
  2026. procsym :
  2027. begin
  2028. for i:=0 to tprocsym(p).ProcdefList.Count-1 do
  2029. begin
  2030. pd:=tprocdef(tprocsym(p).ProcdefList[i]);
  2031. if assigned(pd.localst) and
  2032. (pd.procsym=tprocsym(p)) and
  2033. (pd.localst.symtabletype<>staticsymtable) then
  2034. pd.localst.SymList.ForEachCall(@finalize_static_data,arg);
  2035. end;
  2036. end;
  2037. end;
  2038. end;
  2039. procedure thlcgobj.final_paras(p: TObject; arg: pointer);
  2040. var
  2041. list : TAsmList;
  2042. href : treference;
  2043. hsym : tparavarsym;
  2044. eldef : tdef;
  2045. highloc : tlocation;
  2046. begin
  2047. if not(tsym(p).typ=paravarsym) then
  2048. exit;
  2049. list:=TAsmList(arg);
  2050. if is_managed_type(tparavarsym(p).vardef) then
  2051. begin
  2052. if (tparavarsym(p).varspez=vs_value) then
  2053. begin
  2054. include(current_procinfo.flags,pi_needs_implicit_finally);
  2055. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  2056. if is_open_array(tparavarsym(p).vardef) then
  2057. begin
  2058. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  2059. begin
  2060. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  2061. if not assigned(hsym) then
  2062. internalerror(201003032);
  2063. highloc:=hsym.initialloc
  2064. end
  2065. else
  2066. highloc.loc:=LOC_INVALID;
  2067. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  2068. g_array_rtti_helper(list,eldef,href,highloc,'FPC_DECREF_ARRAY');
  2069. end
  2070. else
  2071. g_decrrefcount(list,tparavarsym(p).vardef,href);
  2072. end;
  2073. end;
  2074. { open arrays can contain elements requiring init/final code, so the else has been removed here }
  2075. if (tparavarsym(p).varspez=vs_value) and
  2076. (is_open_array(tparavarsym(p).vardef) or
  2077. is_array_of_const(tparavarsym(p).vardef)) then
  2078. begin
  2079. { cdecl functions don't have a high pointer so it is not possible to generate
  2080. a local copy }
  2081. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  2082. g_releasevaluepara_openarray(list,tarraydef(tparavarsym(p).vardef),tparavarsym(p).localloc);
  2083. end;
  2084. end;
  2085. { generates the code for incrementing the reference count of parameters and
  2086. initialize out parameters }
  2087. { generates the code for incrementing the reference count of parameters and
  2088. initialize out parameters }
  2089. procedure thlcgobj.init_paras(p:TObject;arg:pointer);
  2090. var
  2091. href : treference;
  2092. hsym : tparavarsym;
  2093. eldef : tdef;
  2094. list : TAsmList;
  2095. highloc : tlocation;
  2096. needs_inittable (*,
  2097. do_trashing *) : boolean;
  2098. begin
  2099. list:=TAsmList(arg);
  2100. if (tsym(p).typ=paravarsym) then
  2101. begin
  2102. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  2103. (*
  2104. do_trashing:=
  2105. (localvartrashing <> -1) and
  2106. (not assigned(tparavarsym(p).defaultconstsym)) and
  2107. not needs_inittable;
  2108. *)
  2109. case tparavarsym(p).varspez of
  2110. vs_value :
  2111. if needs_inittable then
  2112. begin
  2113. { variants are already handled by the call to fpc_variant_copy_overwrite if
  2114. they are passed by reference }
  2115. if not((tparavarsym(p).vardef.typ=variantdef) and
  2116. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  2117. begin
  2118. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  2119. if is_open_array(tparavarsym(p).vardef) then
  2120. begin
  2121. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  2122. begin
  2123. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  2124. if not assigned(hsym) then
  2125. internalerror(201003032);
  2126. highloc:=hsym.initialloc
  2127. end
  2128. else
  2129. highloc.loc:=LOC_INVALID;
  2130. { open arrays do not contain correct element count in their rtti,
  2131. the actual count must be passed separately. }
  2132. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  2133. if not assigned(hsym) then
  2134. internalerror(201003031);
  2135. g_array_rtti_helper(list,eldef,href,highloc,'FPC_ADDREF_ARRAY');
  2136. end
  2137. else
  2138. g_incrrefcount(list,tparavarsym(p).vardef,href);
  2139. end;
  2140. end;
  2141. vs_out :
  2142. begin
  2143. if needs_inittable (*or
  2144. do_trashing*) then
  2145. begin
  2146. { we have no idea about the alignment at the callee side,
  2147. and the user also cannot specify "unaligned" here, so
  2148. assume worst case }
  2149. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  2150. (*
  2151. if do_trashing and
  2152. { needs separate implementation to trash open arrays }
  2153. { since their size is only known at run time }
  2154. not is_special_array(tparavarsym(p).vardef) then
  2155. { may be an open string, even if is_open_string() returns }
  2156. { false (for some helpers in the system unit) }
  2157. if not is_shortstring(tparavarsym(p).vardef) then
  2158. trash_reference(list,href,tparavarsym(p).vardef.size)
  2159. else
  2160. trash_reference(list,href,2);
  2161. *)
  2162. if needs_inittable then
  2163. begin
  2164. if is_open_array(tparavarsym(p).vardef) then
  2165. begin
  2166. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  2167. begin
  2168. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  2169. if not assigned(hsym) then
  2170. internalerror(201003032);
  2171. highloc:=hsym.initialloc
  2172. end
  2173. else
  2174. highloc.loc:=LOC_INVALID;
  2175. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  2176. g_array_rtti_helper(list,eldef,href,highloc,'FPC_INITIALIZE_ARRAY');
  2177. end
  2178. else
  2179. g_initialize(list,tparavarsym(p).vardef,href);
  2180. end;
  2181. end;
  2182. end;
  2183. (*
  2184. else if do_trashing and
  2185. ([vo_is_funcret,vo_is_hidden_para] * tparavarsym(p).varoptions = [vo_is_funcret,vo_is_hidden_para]) then
  2186. begin
  2187. { should always have standard alignment. If a function is assigned
  2188. to a non-aligned variable, the optimisation to pass this variable
  2189. directly as hidden function result must/cannot be performed
  2190. (see tcallnode.funcret_can_be_reused)
  2191. }
  2192. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,
  2193. used_align(tparavarsym(p).vardef.alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax));
  2194. { may be an open string, even if is_open_string() returns }
  2195. { false (for some helpers in the system unit) }
  2196. if not is_shortstring(tparavarsym(p).vardef) then
  2197. trash_reference(list,href,tparavarsym(p).vardef.size)
  2198. else
  2199. { an open string has at least size 2 }
  2200. trash_reference(list,href,2);
  2201. end
  2202. *)
  2203. end;
  2204. end;
  2205. end;
  2206. procedure thlcgobj.gen_load_para_value(list: TAsmList);
  2207. var
  2208. i: longint;
  2209. currpara: tparavarsym;
  2210. begin
  2211. if (po_assembler in current_procinfo.procdef.procoptions) then
  2212. exit;
  2213. { Copy parameters to local references/registers }
  2214. for i:=0 to current_procinfo.procdef.paras.count-1 do
  2215. begin
  2216. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  2217. gen_load_cgpara_loc(list,currpara.vardef,currpara.paraloc[calleeside],currpara.initialloc,paramanager.param_use_paraloc(currpara.paraloc[calleeside]));
  2218. end;
  2219. { generate copies of call by value parameters, must be done before
  2220. the initialization and body is parsed because the refcounts are
  2221. incremented using the local copies }
  2222. current_procinfo.procdef.parast.SymList.ForEachCall(@g_copyvalueparas,list);
  2223. if not(po_assembler in current_procinfo.procdef.procoptions) then
  2224. begin
  2225. { has to be done here rather than in gen_initialize_code, because
  2226. the initialisation code is generated a) later and b) with
  2227. rad_backwards, so the register allocator would generate
  2228. information as if this code comes before loading the parameters
  2229. from their original registers to their local location }
  2230. // if (localvartrashing <> -1) then
  2231. // current_procinfo.procdef.localst.SymList.ForEachCall(@trash_variable,list);
  2232. { initialize refcounted paras, and trash others. Needed here
  2233. instead of in gen_initialize_code, because when a reference is
  2234. intialised or trashed while the pointer to that reference is kept
  2235. in a regvar, we add a register move and that one again has to
  2236. come after the parameter loading code as far as the register
  2237. allocator is concerned }
  2238. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  2239. end;
  2240. end;
  2241. procedure thlcgobj.g_copyvalueparas(p: TObject; arg: pointer);
  2242. var
  2243. href : treference;
  2244. hreg : tregister;
  2245. list : TAsmList;
  2246. hsym : tparavarsym;
  2247. l : longint;
  2248. highloc,
  2249. localcopyloc : tlocation;
  2250. begin
  2251. list:=TAsmList(arg);
  2252. if (tsym(p).typ=paravarsym) and
  2253. (tparavarsym(p).varspez=vs_value) and
  2254. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  2255. begin
  2256. { we have no idea about the alignment at the caller side }
  2257. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  2258. if is_open_array(tparavarsym(p).vardef) or
  2259. is_array_of_const(tparavarsym(p).vardef) then
  2260. begin
  2261. { cdecl functions don't have a high pointer so it is not possible to generate
  2262. a local copy }
  2263. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  2264. begin
  2265. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  2266. begin
  2267. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  2268. if not assigned(hsym) then
  2269. internalerror(2011020506);
  2270. highloc:=hsym.initialloc
  2271. end
  2272. else
  2273. highloc.loc:=LOC_INVALID;
  2274. hreg:=getaddressregister(list,voidpointertype);
  2275. if not is_packed_array(tparavarsym(p).vardef) then
  2276. g_copyvaluepara_openarray(list,href,highloc,tarraydef(tparavarsym(p).vardef),hreg)
  2277. else
  2278. internalerror(2011020507);
  2279. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  2280. a_load_reg_loc(list,tparavarsym(p).vardef,tparavarsym(p).vardef,hreg,tparavarsym(p).initialloc);
  2281. end;
  2282. end
  2283. else
  2284. begin
  2285. { Allocate space for the local copy }
  2286. l:=tparavarsym(p).getsize;
  2287. localcopyloc.loc:=LOC_REFERENCE;
  2288. localcopyloc.size:=int_cgsize(l);
  2289. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  2290. { Copy data }
  2291. if is_shortstring(tparavarsym(p).vardef) then
  2292. begin
  2293. { this code is only executed before the code for the body and the entry/exit code is generated
  2294. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  2295. }
  2296. include(current_procinfo.flags,pi_do_call);
  2297. g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef))
  2298. end
  2299. else if tparavarsym(p).vardef.typ=variantdef then
  2300. begin
  2301. { this code is only executed before the code for the body and the entry/exit code is generated
  2302. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  2303. }
  2304. include(current_procinfo.flags,pi_do_call);
  2305. g_copyvariant(list,href,localcopyloc.reference,tvariantdef(tparavarsym(p).vardef))
  2306. end
  2307. else
  2308. begin
  2309. { pass proper alignment info }
  2310. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  2311. g_concatcopy(list,tparavarsym(p).vardef,href,localcopyloc.reference);
  2312. end;
  2313. { update localloc of varsym }
  2314. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  2315. tparavarsym(p).localloc:=localcopyloc;
  2316. tparavarsym(p).initialloc:=localcopyloc;
  2317. end;
  2318. end;
  2319. end;
  2320. procedure thlcgobj.gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint);
  2321. begin
  2322. case l.loc of
  2323. (*
  2324. LOC_MMREGISTER,
  2325. LOC_CMMREGISTER:
  2326. case cgpara.location^.loc of
  2327. LOC_REFERENCE,
  2328. LOC_CREFERENCE,
  2329. LOC_MMREGISTER,
  2330. LOC_CMMREGISTER,
  2331. LOC_REGISTER,
  2332. LOC_CREGISTER :
  2333. cg.a_loadmm_reg_cgpara(list,locsize,l.register,cgpara,mms_movescalar);
  2334. LOC_FPUREGISTER,
  2335. LOC_CFPUREGISTER:
  2336. begin
  2337. tmploc:=l;
  2338. location_force_fpureg(list,tmploc,false);
  2339. cg.a_loadfpu_reg_cgpara(list,tmploc.size,tmploc.register,cgpara);
  2340. end;
  2341. else
  2342. internalerror(200204249);
  2343. end;
  2344. *)
  2345. LOC_FPUREGISTER,
  2346. LOC_CFPUREGISTER:
  2347. case cgpara.location^.loc of
  2348. (*
  2349. LOC_MMREGISTER,
  2350. LOC_CMMREGISTER:
  2351. begin
  2352. tmploc:=l;
  2353. location_force_mmregscalar(list,tmploc,false);
  2354. cg.a_loadmm_reg_cgpara(list,tmploc.size,tmploc.register,cgpara,mms_movescalar);
  2355. end;
  2356. *)
  2357. { Some targets pass floats in normal registers }
  2358. LOC_REGISTER,
  2359. LOC_CREGISTER,
  2360. LOC_REFERENCE,
  2361. LOC_CREFERENCE,
  2362. LOC_FPUREGISTER,
  2363. LOC_CFPUREGISTER:
  2364. a_loadfpu_reg_cgpara(list,size,l.register,cgpara);
  2365. else
  2366. internalerror(2011010210);
  2367. end;
  2368. LOC_REFERENCE,
  2369. LOC_CREFERENCE:
  2370. case cgpara.location^.loc of
  2371. (*
  2372. LOC_MMREGISTER,
  2373. LOC_CMMREGISTER:
  2374. cg.a_loadmm_ref_cgpara(list,locsize,l.reference,cgpara,mms_movescalar);
  2375. *)
  2376. { Some targets pass floats in normal registers }
  2377. LOC_REGISTER,
  2378. LOC_CREGISTER,
  2379. LOC_REFERENCE,
  2380. LOC_CREFERENCE,
  2381. LOC_FPUREGISTER,
  2382. LOC_CFPUREGISTER:
  2383. a_loadfpu_ref_cgpara(list,size,l.reference,cgpara);
  2384. else
  2385. internalerror(2011010211);
  2386. end;
  2387. LOC_REGISTER,
  2388. LOC_CREGISTER :
  2389. a_load_loc_cgpara(list,size,l,cgpara);
  2390. else
  2391. internalerror(2011010212);
  2392. end;
  2393. end;
  2394. procedure thlcgobj.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  2395. begin
  2396. { do nothing by default }
  2397. end;
  2398. procedure thlcgobj.gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);
  2399. begin
  2400. { Handle Floating point types differently
  2401. This doesn't depend on emulator settings, emulator settings should
  2402. be handled by cpupara }
  2403. if (vardef.typ=floatdef) or
  2404. { some ABIs return certain records in an fpu register }
  2405. (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) or
  2406. (assigned(cgpara.location) and
  2407. (cgpara.Location^.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) then
  2408. begin
  2409. gen_loadfpu_loc_cgpara(list,vardef,l,cgpara,vardef.size);
  2410. exit;
  2411. end;
  2412. case l.loc of
  2413. LOC_CONSTANT,
  2414. LOC_REGISTER,
  2415. LOC_CREGISTER,
  2416. LOC_REFERENCE,
  2417. LOC_CREFERENCE :
  2418. begin
  2419. a_load_loc_cgpara(list,vardef,l,cgpara);
  2420. end;
  2421. (*
  2422. LOC_MMREGISTER,
  2423. LOC_CMMREGISTER:
  2424. begin
  2425. case l.size of
  2426. OS_F32,
  2427. OS_F64:
  2428. cg.a_loadmm_loc_cgpara(list,l,cgpara,mms_movescalar);
  2429. else
  2430. cg.a_loadmm_loc_cgpara(list,l,cgpara,nil);
  2431. end;
  2432. end;
  2433. *)
  2434. else
  2435. internalerror(2011010213);
  2436. end;
  2437. end;
  2438. procedure thlcgobj.gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  2439. var
  2440. href : treference;
  2441. begin
  2442. para.check_simple_location;
  2443. { skip e.g. empty records }
  2444. if (para.location^.loc = LOC_VOID) then
  2445. exit;
  2446. case destloc.loc of
  2447. LOC_REFERENCE :
  2448. begin
  2449. { If the parameter location is reused we don't need to copy
  2450. anything }
  2451. if not reusepara then
  2452. begin
  2453. reference_reset_base(href,para.location^.reference.index,para.location^.reference.offset,para.alignment);
  2454. a_load_ref_ref(list,para.def,para.def,href,destloc.reference);
  2455. end;
  2456. end;
  2457. { TODO other possible locations }
  2458. else
  2459. internalerror(2011010308);
  2460. end;
  2461. end;
  2462. procedure thlcgobj.gen_load_return_value(list: TAsmList);
  2463. var
  2464. ressym : tabstractnormalvarsym;
  2465. funcretloc : TCGPara;
  2466. begin
  2467. { Is the loading needed? }
  2468. if is_void(current_procinfo.procdef.returndef) or
  2469. (
  2470. (po_assembler in current_procinfo.procdef.procoptions) and
  2471. (not(assigned(current_procinfo.procdef.funcretsym)) or
  2472. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  2473. ) then
  2474. exit;
  2475. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  2476. { constructors return self }
  2477. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  2478. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.Find('self'))
  2479. else
  2480. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  2481. if (ressym.refs>0) or
  2482. is_managed_type(ressym.vardef) then
  2483. begin
  2484. { was: don't do anything if funcretloc.loc in [LOC_INVALID,LOC_REFERENCE] }
  2485. if not paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  2486. gen_load_loc_cgpara(list,ressym.vardef,ressym.localloc,funcretloc);
  2487. end
  2488. else
  2489. gen_load_uninitialized_function_result(list,current_procinfo.procdef,ressym.vardef,funcretloc)
  2490. end;
  2491. procedure thlcgobj.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  2492. begin
  2493. { add the procedure to the al_procedures }
  2494. maybe_new_object_file(current_asmdata.asmlists[al_procedures]);
  2495. new_section(current_asmdata.asmlists[al_procedures],sec_code,lower(pd.mangledname),getprocalign);
  2496. current_asmdata.asmlists[al_procedures].concatlist(code);
  2497. { save local data (casetable) also in the same file }
  2498. if assigned(data) and
  2499. (not data.empty) then
  2500. current_asmdata.asmlists[al_procedures].concatlist(data);
  2501. end;
  2502. end.