hlcgobj.pas 125 KB

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