hlcgobj.pas 116 KB

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