hlcgobj.pas 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928
  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. begin
  1758. case l.loc of
  1759. LOC_FPUREGISTER,
  1760. LOC_CFPUREGISTER :
  1761. begin
  1762. tg.gethltemp(list,size,size.size,tt_normal,r);
  1763. a_loadfpu_reg_ref(list,size,size,l.register,r);
  1764. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1765. l.reference:=r;
  1766. end;
  1767. (*
  1768. LOC_MMREGISTER,
  1769. LOC_CMMREGISTER:
  1770. begin
  1771. tg.gethltemp(list,size,size.size,tt_normal,r);
  1772. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  1773. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1774. l.reference:=r;
  1775. end;
  1776. *)
  1777. LOC_CONSTANT,
  1778. LOC_REGISTER,
  1779. LOC_CREGISTER :
  1780. begin
  1781. tg.gethltemp(list,size,size.size,tt_normal,r);
  1782. a_load_loc_ref(list,size,size,l,r);
  1783. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1784. l.reference:=r;
  1785. end;
  1786. (*
  1787. LOC_SUBSETREG,
  1788. LOC_CSUBSETREG,
  1789. LOC_SUBSETREF,
  1790. LOC_CSUBSETREF:
  1791. begin
  1792. tg.gethltemp(list,size,size.size,tt_normal,r);
  1793. cg.a_load_loc_ref(list,l.size,l,r);
  1794. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  1795. l.reference:=r;
  1796. end;
  1797. *)
  1798. LOC_CREFERENCE,
  1799. LOC_REFERENCE : ;
  1800. else
  1801. internalerror(2011010304);
  1802. end;
  1803. end;
  1804. procedure thlcgobj.location_get_data_ref(list: TAsmList; def: tdef; const l: tlocation; var ref: treference; loadref: boolean; alignment: longint);
  1805. begin
  1806. case l.loc of
  1807. LOC_REGISTER,
  1808. LOC_CREGISTER :
  1809. begin
  1810. if not loadref then
  1811. internalerror(200410231);
  1812. reference_reset_base(ref,l.register,0,alignment);
  1813. end;
  1814. LOC_REFERENCE,
  1815. LOC_CREFERENCE :
  1816. begin
  1817. if loadref then
  1818. begin
  1819. reference_reset_base(ref,getaddressregister(list,voidpointertype),0,alignment);
  1820. { it's a pointer to def }
  1821. a_load_ref_reg(list,voidpointertype,voidpointertype,l.reference,ref.base);
  1822. end
  1823. else
  1824. ref:=l.reference;
  1825. end;
  1826. else
  1827. internalerror(200309181);
  1828. end;
  1829. end;
  1830. procedure thlcgobj.maketojumpbool(list: TAsmList; p: tnode);
  1831. {
  1832. produces jumps to true respectively false labels using boolean expressions
  1833. depending on whether the loading of regvars is currently being
  1834. synchronized manually (such as in an if-node) or automatically (most of
  1835. the other cases where this procedure is called), loadregvars can be
  1836. "lr_load_regvars" or "lr_dont_load_regvars"
  1837. }
  1838. var
  1839. storepos : tfileposinfo;
  1840. begin
  1841. if nf_error in p.flags then
  1842. exit;
  1843. storepos:=current_filepos;
  1844. current_filepos:=p.fileinfo;
  1845. if is_boolean(p.resultdef) then
  1846. begin
  1847. if is_constboolnode(p) then
  1848. begin
  1849. if Tordconstnode(p).value.uvalue<>0 then
  1850. a_jmp_always(list,current_procinfo.CurrTrueLabel)
  1851. else
  1852. a_jmp_always(list,current_procinfo.CurrFalseLabel)
  1853. end
  1854. else
  1855. begin
  1856. case p.location.loc of
  1857. (*
  1858. LOC_SUBSETREG,LOC_CSUBSETREG,
  1859. LOC_SUBSETREF,LOC_CSUBSETREF:
  1860. begin
  1861. tmpreg := cg.getintregister(list,OS_INT);
  1862. cg.a_load_loc_reg(list,OS_INT,p.location,tmpreg);
  1863. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,tmpreg,current_procinfo.CurrTrueLabel);
  1864. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  1865. end;
  1866. *)
  1867. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  1868. begin
  1869. a_cmp_const_loc_label(list,p.resultdef,OC_NE,0,p.location,current_procinfo.CurrTrueLabel);
  1870. a_jmp_always(list,current_procinfo.CurrFalseLabel);
  1871. end;
  1872. LOC_JUMP:
  1873. ;
  1874. {$ifdef cpuflags}
  1875. LOC_FLAGS :
  1876. begin
  1877. a_jmp_flags(list,p.location.resflags,current_procinfo.CurrTrueLabel);
  1878. a_jmp_always(list,current_procinfo.CurrFalseLabel);
  1879. end;
  1880. {$endif cpuflags}
  1881. else
  1882. begin
  1883. printnode(output,p);
  1884. internalerror(2011010418);
  1885. end;
  1886. end;
  1887. end;
  1888. end
  1889. else
  1890. internalerror(2011010419);
  1891. current_filepos:=storepos;
  1892. end;
  1893. procedure thlcgobj.gen_proc_symbol(list: TAsmList);
  1894. var
  1895. item,
  1896. previtem : TCmdStrListItem;
  1897. begin
  1898. previtem:=nil;
  1899. item := TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
  1900. while assigned(item) do
  1901. begin
  1902. {$ifdef arm}
  1903. if current_settings.cputype in cpu_thumb2 then
  1904. list.concat(tai_thumb_func.create);
  1905. {$endif arm}
  1906. { "double link" all procedure entry symbols via .reference }
  1907. { directives on darwin, because otherwise the linker }
  1908. { sometimes strips the procedure if only on of the symbols }
  1909. { is referenced }
  1910. if assigned(previtem) and
  1911. (target_info.system in systems_darwin) then
  1912. list.concat(tai_directive.create(asd_reference,item.str));
  1913. if (cs_profile in current_settings.moduleswitches) or
  1914. (po_global in current_procinfo.procdef.procoptions) then
  1915. list.concat(Tai_symbol.createname_global(item.str,AT_FUNCTION,0))
  1916. else
  1917. list.concat(Tai_symbol.createname(item.str,AT_FUNCTION,0));
  1918. if assigned(previtem) and
  1919. (target_info.system in systems_darwin) then
  1920. list.concat(tai_directive.create(asd_reference,previtem.str));
  1921. if not(af_stabs_use_function_absolute_addresses in target_asm.flags) then
  1922. list.concat(Tai_function_name.create(item.str));
  1923. previtem:=item;
  1924. item := TCmdStrListItem(item.next);
  1925. end;
  1926. current_procinfo.procdef.procstarttai:=tai(list.last);
  1927. end;
  1928. procedure thlcgobj.gen_proc_symbol_end(list: TAsmList);
  1929. begin
  1930. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1931. current_procinfo.procdef.procendtai:=tai(list.last);
  1932. if (current_module.islibrary) then
  1933. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  1934. { setinitname may generate a new section -> don't add to the
  1935. current list, because we assume this remains a text section }
  1936. exportlib.setinitname(current_asmdata.AsmLists[al_exports],current_procinfo.procdef.mangledname);
  1937. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1938. begin
  1939. if (target_info.system in (systems_darwin+[system_powerpc_macos])) and
  1940. not(current_module.islibrary) then
  1941. begin
  1942. new_section(list,sec_code,'',4);
  1943. list.concat(tai_symbol.createname_global(
  1944. target_info.cprefix+mainaliasname,AT_FUNCTION,0));
  1945. { keep argc, argv and envp properly on the stack }
  1946. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  1947. end;
  1948. end;
  1949. end;
  1950. procedure thlcgobj.gen_initialize_code(list: TAsmList);
  1951. begin
  1952. { initialize local data like ansistrings }
  1953. case current_procinfo.procdef.proctypeoption of
  1954. potype_unitinit:
  1955. begin
  1956. { this is also used for initialization of variables in a
  1957. program which does not have a globalsymtable }
  1958. if assigned(current_module.globalsymtable) then
  1959. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@initialize_data,list);
  1960. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_data,list);
  1961. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  1962. end;
  1963. { units have seperate code for initilization and finalization }
  1964. potype_unitfinalize: ;
  1965. { program init/final is generated in separate procedure }
  1966. potype_proginit:
  1967. begin
  1968. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  1969. end;
  1970. else
  1971. current_procinfo.procdef.localst.SymList.ForEachCall(@initialize_data,list);
  1972. end;
  1973. { initialisizes temp. ansi/wide string data }
  1974. inittempvariables(list);
  1975. {$ifdef OLDREGVARS}
  1976. load_regvars(list,nil);
  1977. {$endif OLDREGVARS}
  1978. end;
  1979. procedure thlcgobj.gen_finalize_code(list: TAsmList);
  1980. begin
  1981. {$ifdef OLDREGVARS}
  1982. cleanup_regvars(list);
  1983. {$endif OLDREGVARS}
  1984. { finalize temporary data }
  1985. finalizetempvariables(list);
  1986. { finalize local data like ansistrings}
  1987. case current_procinfo.procdef.proctypeoption of
  1988. potype_unitfinalize:
  1989. begin
  1990. { this is also used for initialization of variables in a
  1991. program which does not have a globalsymtable }
  1992. if assigned(current_module.globalsymtable) then
  1993. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@finalize_static_data,list);
  1994. TSymtable(current_module.localsymtable).SymList.ForEachCall(@finalize_static_data,list);
  1995. end;
  1996. { units/progs have separate code for initialization and finalization }
  1997. potype_unitinit: ;
  1998. { program init/final is generated in separate procedure }
  1999. potype_proginit: ;
  2000. else
  2001. current_procinfo.procdef.localst.SymList.ForEachCall(@finalize_local_vars,list);
  2002. end;
  2003. { finalize paras data }
  2004. if assigned(current_procinfo.procdef.parast) and
  2005. not(po_assembler in current_procinfo.procdef.procoptions) then
  2006. current_procinfo.procdef.parast.SymList.ForEachCall(@final_paras,list);
  2007. end;
  2008. procedure thlcgobj.gen_entry_code(list: TAsmList);
  2009. begin
  2010. { the actual profile code can clobber some registers,
  2011. therefore if the context must be saved, do it before
  2012. the actual call to the profile code
  2013. }
  2014. if (cs_profile in current_settings.moduleswitches) and
  2015. not(po_assembler in current_procinfo.procdef.procoptions) then
  2016. begin
  2017. { non-win32 can call mcout even in main }
  2018. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  2019. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  2020. begin
  2021. g_profilecode(list);
  2022. end;
  2023. end;
  2024. { TODO: create high level version (create compilerprocs in system unit,
  2025. look up procdef, use hlcgobj.a_call_name()) }
  2026. { call startup helpers from main program }
  2027. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  2028. begin
  2029. { initialize units }
  2030. cg.allocallcpuregisters(list);
  2031. if not(current_module.islibrary) then
  2032. cg.a_call_name(list,'FPC_INITIALIZEUNITS',false)
  2033. else
  2034. cg.a_call_name(list,'FPC_LIBINITIALIZEUNITS',false);
  2035. cg.deallocallcpuregisters(list);
  2036. end;
  2037. list.concat(Tai_force_line.Create);
  2038. {$ifdef OLDREGVARS}
  2039. load_regvars(list,nil);
  2040. {$endif OLDREGVARS}
  2041. end;
  2042. procedure thlcgobj.gen_exit_code(list: TAsmList);
  2043. begin
  2044. { TODO: create high level version (create compilerproc in system unit,
  2045. look up procdef, use hlcgobj.a_call_name()) }
  2046. { call __EXIT for main program }
  2047. if (not DLLsource) and
  2048. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  2049. cg.a_call_name(list,'FPC_DO_EXIT',false);
  2050. end;
  2051. procedure thlcgobj.inittempvariables(list: TAsmList);
  2052. var
  2053. hp : ptemprecord;
  2054. href : treference;
  2055. begin
  2056. hp:=tg.templist;
  2057. while assigned(hp) do
  2058. begin
  2059. if assigned(hp^.def) and
  2060. is_managed_type(hp^.def) then
  2061. begin
  2062. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  2063. g_initialize(list,hp^.def,href);
  2064. end;
  2065. hp:=hp^.next;
  2066. end;
  2067. end;
  2068. procedure thlcgobj.initialize_data(p: TObject; arg: pointer);
  2069. var
  2070. OldAsmList : TAsmList;
  2071. hp : tnode;
  2072. begin
  2073. if (tsym(p).typ = localvarsym) and
  2074. { local (procedure or unit) variables only need initialization if
  2075. they are used }
  2076. ((tabstractvarsym(p).refs>0) or
  2077. { managed return symbols must be inited }
  2078. ((tsym(p).typ=localvarsym) and (vo_is_funcret in tlocalvarsym(p).varoptions))
  2079. ) and
  2080. not(vo_is_typed_const in tabstractvarsym(p).varoptions) and
  2081. not(vo_is_external in tabstractvarsym(p).varoptions) and
  2082. (is_managed_type(tabstractvarsym(p).vardef) or
  2083. ((m_iso in current_settings.modeswitches) and (tabstractvarsym(p).vardef.typ=filedef))
  2084. ) then
  2085. begin
  2086. OldAsmList:=current_asmdata.CurrAsmList;
  2087. current_asmdata.CurrAsmList:=TAsmList(arg);
  2088. hp:=cnodeutils.initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner),false);
  2089. firstpass(hp);
  2090. secondpass(hp);
  2091. hp.free;
  2092. current_asmdata.CurrAsmList:=OldAsmList;
  2093. end;
  2094. end;
  2095. procedure thlcgobj.finalizetempvariables(list: TAsmList);
  2096. var
  2097. hp : ptemprecord;
  2098. href : treference;
  2099. begin
  2100. hp:=tg.templist;
  2101. while assigned(hp) do
  2102. begin
  2103. if assigned(hp^.def) and
  2104. is_managed_type(hp^.def) then
  2105. begin
  2106. include(current_procinfo.flags,pi_needs_implicit_finally);
  2107. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  2108. g_finalize(list,hp^.def,href);
  2109. end;
  2110. hp:=hp^.next;
  2111. end;
  2112. end;
  2113. procedure thlcgobj.initialize_regvars(p: TObject; arg: pointer);
  2114. var
  2115. href : treference;
  2116. begin
  2117. if (tsym(p).typ=staticvarsym) then
  2118. begin
  2119. { Static variables can have the initialloc only set to LOC_CxREGISTER
  2120. or LOC_INVALID, for explaination see gen_alloc_symtable (PFV) }
  2121. case tstaticvarsym(p).initialloc.loc of
  2122. LOC_CREGISTER :
  2123. begin
  2124. {$ifndef cpu64bitalu}
  2125. if (tstaticvarsym(p).initialloc.size in [OS_64,OS_S64]) then
  2126. cg64.a_load64_const_reg(TAsmList(arg),0,tstaticvarsym(p).initialloc.register64)
  2127. else
  2128. {$endif not cpu64bitalu}
  2129. a_load_const_reg(TAsmList(arg),tstaticvarsym(p).vardef,0,
  2130. tstaticvarsym(p).initialloc.register);
  2131. end;
  2132. (*
  2133. LOC_CMMREGISTER :
  2134. { clear the whole register }
  2135. cg.a_opmm_reg_reg(TAsmList(arg),OP_XOR,reg_cgsize(tstaticvarsym(p).initialloc.register),
  2136. tstaticvarsym(p).initialloc.register,
  2137. tstaticvarsym(p).initialloc.register,
  2138. nil);
  2139. *)
  2140. LOC_CFPUREGISTER :
  2141. begin
  2142. { initialize fpu regvar by loading from memory }
  2143. reference_reset_symbol(href,
  2144. current_asmdata.RefAsmSymbol(tstaticvarsym(p).mangledname), 0,
  2145. var_align(tstaticvarsym(p).vardef.alignment));
  2146. a_loadfpu_ref_reg(TAsmList(arg), tstaticvarsym(p).vardef,
  2147. tstaticvarsym(p).vardef, href, tstaticvarsym(p).initialloc.register);
  2148. end;
  2149. LOC_INVALID :
  2150. ;
  2151. else
  2152. internalerror(200410124);
  2153. end;
  2154. end;
  2155. end;
  2156. procedure thlcgobj.finalize_sym(asmlist: TAsmList; sym: tsym);
  2157. var
  2158. hp : tnode;
  2159. OldAsmList : TAsmList;
  2160. begin
  2161. include(current_procinfo.flags,pi_needs_implicit_finally);
  2162. OldAsmList:=current_asmdata.CurrAsmList;
  2163. current_asmdata.CurrAsmList:=asmlist;
  2164. hp:=cloadnode.create(sym,sym.owner);
  2165. if (sym.typ=staticvarsym) and (vo_force_finalize in tstaticvarsym(sym).varoptions) then
  2166. include(hp.flags,nf_isinternal_ignoreconst);
  2167. hp:=cnodeutils.finalize_data_node(hp);
  2168. firstpass(hp);
  2169. secondpass(hp);
  2170. hp.free;
  2171. current_asmdata.CurrAsmList:=OldAsmList;
  2172. end;
  2173. procedure thlcgobj.finalize_local_vars(p: TObject; arg: pointer);
  2174. begin
  2175. if (tsym(p).typ=localvarsym) and
  2176. (tlocalvarsym(p).refs>0) and
  2177. not(vo_is_external in tlocalvarsym(p).varoptions) and
  2178. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  2179. is_managed_type(tlocalvarsym(p).vardef) then
  2180. finalize_sym(TAsmList(arg),tsym(p));
  2181. end;
  2182. procedure thlcgobj.finalize_static_data(p: TObject; arg: pointer);
  2183. var
  2184. i : longint;
  2185. pd : tprocdef;
  2186. begin
  2187. case tsym(p).typ of
  2188. staticvarsym :
  2189. begin
  2190. { local (procedure or unit) variables only need finalization
  2191. if they are used
  2192. }
  2193. if ((tstaticvarsym(p).refs>0) or
  2194. { global (unit) variables always need finalization, since
  2195. they may also be used in another unit
  2196. }
  2197. (tstaticvarsym(p).owner.symtabletype=globalsymtable)) and
  2198. (
  2199. (tstaticvarsym(p).varspez<>vs_const) or
  2200. (vo_force_finalize in tstaticvarsym(p).varoptions)
  2201. ) and
  2202. not(vo_is_funcret in tstaticvarsym(p).varoptions) and
  2203. not(vo_is_external in tstaticvarsym(p).varoptions) and
  2204. is_managed_type(tstaticvarsym(p).vardef) then
  2205. finalize_sym(TAsmList(arg),tsym(p));
  2206. end;
  2207. procsym :
  2208. begin
  2209. for i:=0 to tprocsym(p).ProcdefList.Count-1 do
  2210. begin
  2211. pd:=tprocdef(tprocsym(p).ProcdefList[i]);
  2212. if assigned(pd.localst) and
  2213. (pd.procsym=tprocsym(p)) and
  2214. (pd.localst.symtabletype<>staticsymtable) then
  2215. pd.localst.SymList.ForEachCall(@finalize_static_data,arg);
  2216. end;
  2217. end;
  2218. end;
  2219. end;
  2220. procedure thlcgobj.final_paras(p: TObject; arg: pointer);
  2221. var
  2222. list : TAsmList;
  2223. href : treference;
  2224. hsym : tparavarsym;
  2225. eldef : tdef;
  2226. highloc : tlocation;
  2227. begin
  2228. if not(tsym(p).typ=paravarsym) then
  2229. exit;
  2230. list:=TAsmList(arg);
  2231. if is_managed_type(tparavarsym(p).vardef) then
  2232. begin
  2233. if (tparavarsym(p).varspez=vs_value) then
  2234. begin
  2235. include(current_procinfo.flags,pi_needs_implicit_finally);
  2236. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  2237. if is_open_array(tparavarsym(p).vardef) then
  2238. begin
  2239. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  2240. begin
  2241. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  2242. if not assigned(hsym) then
  2243. internalerror(201003032);
  2244. highloc:=hsym.initialloc
  2245. end
  2246. else
  2247. highloc.loc:=LOC_INVALID;
  2248. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  2249. g_array_rtti_helper(list,eldef,href,highloc,'FPC_DECREF_ARRAY');
  2250. end
  2251. else
  2252. g_decrrefcount(list,tparavarsym(p).vardef,href);
  2253. end;
  2254. end;
  2255. { open arrays can contain elements requiring init/final code, so the else has been removed here }
  2256. if (tparavarsym(p).varspez=vs_value) and
  2257. (is_open_array(tparavarsym(p).vardef) or
  2258. is_array_of_const(tparavarsym(p).vardef)) then
  2259. begin
  2260. { cdecl functions don't have a high pointer so it is not possible to generate
  2261. a local copy }
  2262. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  2263. g_releasevaluepara_openarray(list,tarraydef(tparavarsym(p).vardef),tparavarsym(p).localloc);
  2264. end;
  2265. end;
  2266. { generates the code for incrementing the reference count of parameters and
  2267. initialize out parameters }
  2268. { generates the code for incrementing the reference count of parameters and
  2269. initialize out parameters }
  2270. procedure thlcgobj.init_paras(p:TObject;arg:pointer);
  2271. var
  2272. href : treference;
  2273. hsym : tparavarsym;
  2274. eldef : tdef;
  2275. list : TAsmList;
  2276. highloc : tlocation;
  2277. needs_inittable (*,
  2278. do_trashing *) : boolean;
  2279. begin
  2280. list:=TAsmList(arg);
  2281. if (tsym(p).typ=paravarsym) then
  2282. begin
  2283. needs_inittable:=is_managed_type(tparavarsym(p).vardef);
  2284. (*
  2285. do_trashing:=
  2286. (localvartrashing <> -1) and
  2287. (not assigned(tparavarsym(p).defaultconstsym)) and
  2288. not needs_inittable;
  2289. *)
  2290. case tparavarsym(p).varspez of
  2291. vs_value :
  2292. if needs_inittable then
  2293. begin
  2294. { variants are already handled by the call to fpc_variant_copy_overwrite if
  2295. they are passed by reference }
  2296. if not((tparavarsym(p).vardef.typ=variantdef) and
  2297. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  2298. begin
  2299. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  2300. if is_open_array(tparavarsym(p).vardef) then
  2301. begin
  2302. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  2303. begin
  2304. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  2305. if not assigned(hsym) then
  2306. internalerror(201003032);
  2307. highloc:=hsym.initialloc
  2308. end
  2309. else
  2310. highloc.loc:=LOC_INVALID;
  2311. { open arrays do not contain correct element count in their rtti,
  2312. the actual count must be passed separately. }
  2313. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  2314. g_array_rtti_helper(list,eldef,href,highloc,'FPC_ADDREF_ARRAY');
  2315. end
  2316. else
  2317. g_incrrefcount(list,tparavarsym(p).vardef,href);
  2318. end;
  2319. end;
  2320. vs_out :
  2321. begin
  2322. if needs_inittable (*or
  2323. do_trashing*) then
  2324. begin
  2325. { we have no idea about the alignment at the callee side,
  2326. and the user also cannot specify "unaligned" here, so
  2327. assume worst case }
  2328. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  2329. (*
  2330. if do_trashing and
  2331. { needs separate implementation to trash open arrays }
  2332. { since their size is only known at run time }
  2333. not is_special_array(tparavarsym(p).vardef) then
  2334. { may be an open string, even if is_open_string() returns }
  2335. { false (for some helpers in the system unit) }
  2336. if not is_shortstring(tparavarsym(p).vardef) then
  2337. trash_reference(list,href,tparavarsym(p).vardef.size)
  2338. else
  2339. trash_reference(list,href,2);
  2340. *)
  2341. if needs_inittable then
  2342. begin
  2343. if is_open_array(tparavarsym(p).vardef) then
  2344. begin
  2345. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  2346. begin
  2347. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  2348. if not assigned(hsym) then
  2349. internalerror(201003032);
  2350. highloc:=hsym.initialloc
  2351. end
  2352. else
  2353. highloc.loc:=LOC_INVALID;
  2354. eldef:=tarraydef(tparavarsym(p).vardef).elementdef;
  2355. g_array_rtti_helper(list,eldef,href,highloc,'FPC_INITIALIZE_ARRAY');
  2356. end
  2357. else
  2358. g_initialize(list,tparavarsym(p).vardef,href);
  2359. end;
  2360. end;
  2361. end;
  2362. (*
  2363. else if do_trashing and
  2364. ([vo_is_funcret,vo_is_hidden_para] * tparavarsym(p).varoptions = [vo_is_funcret,vo_is_hidden_para]) then
  2365. begin
  2366. { should always have standard alignment. If a function is assigned
  2367. to a non-aligned variable, the optimisation to pass this variable
  2368. directly as hidden function result must/cannot be performed
  2369. (see tcallnode.funcret_can_be_reused)
  2370. }
  2371. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,
  2372. used_align(tparavarsym(p).vardef.alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax));
  2373. { may be an open string, even if is_open_string() returns }
  2374. { false (for some helpers in the system unit) }
  2375. if not is_shortstring(tparavarsym(p).vardef) then
  2376. trash_reference(list,href,tparavarsym(p).vardef.size)
  2377. else
  2378. { an open string has at least size 2 }
  2379. trash_reference(list,href,2);
  2380. end
  2381. *)
  2382. end;
  2383. end;
  2384. end;
  2385. procedure thlcgobj.gen_load_para_value(list: TAsmList);
  2386. var
  2387. i: longint;
  2388. currpara: tparavarsym;
  2389. begin
  2390. if (po_assembler in current_procinfo.procdef.procoptions) then
  2391. exit;
  2392. { Copy parameters to local references/registers }
  2393. for i:=0 to current_procinfo.procdef.paras.count-1 do
  2394. begin
  2395. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  2396. gen_load_cgpara_loc(list,currpara.vardef,currpara.paraloc[calleeside],currpara.initialloc,paramanager.param_use_paraloc(currpara.paraloc[calleeside]));
  2397. end;
  2398. { generate copies of call by value parameters, must be done before
  2399. the initialization and body is parsed because the refcounts are
  2400. incremented using the local copies }
  2401. current_procinfo.procdef.parast.SymList.ForEachCall(@g_copyvalueparas,list);
  2402. if not(po_assembler in current_procinfo.procdef.procoptions) then
  2403. begin
  2404. { has to be done here rather than in gen_initialize_code, because
  2405. the initialisation code is generated a) later and b) with
  2406. rad_backwards, so the register allocator would generate
  2407. information as if this code comes before loading the parameters
  2408. from their original registers to their local location }
  2409. // if (localvartrashing <> -1) then
  2410. // current_procinfo.procdef.localst.SymList.ForEachCall(@trash_variable,list);
  2411. { initialize refcounted paras, and trash others. Needed here
  2412. instead of in gen_initialize_code, because when a reference is
  2413. intialised or trashed while the pointer to that reference is kept
  2414. in a regvar, we add a register move and that one again has to
  2415. come after the parameter loading code as far as the register
  2416. allocator is concerned }
  2417. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  2418. end;
  2419. end;
  2420. procedure thlcgobj.g_copyvalueparas(p: TObject; arg: pointer);
  2421. var
  2422. href : treference;
  2423. hreg : tregister;
  2424. list : TAsmList;
  2425. hsym : tparavarsym;
  2426. l : longint;
  2427. highloc,
  2428. localcopyloc : tlocation;
  2429. begin
  2430. list:=TAsmList(arg);
  2431. if (tsym(p).typ=paravarsym) and
  2432. (tparavarsym(p).varspez=vs_value) and
  2433. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  2434. begin
  2435. { we have no idea about the alignment at the caller side }
  2436. location_get_data_ref(list,tparavarsym(p).vardef,tparavarsym(p).initialloc,href,true,1);
  2437. if is_open_array(tparavarsym(p).vardef) or
  2438. is_array_of_const(tparavarsym(p).vardef) then
  2439. begin
  2440. { cdecl functions don't have a high pointer so it is not possible to generate
  2441. a local copy }
  2442. if not(current_procinfo.procdef.proccalloption in cdecl_pocalls) then
  2443. begin
  2444. if paramanager.push_high_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption) then
  2445. begin
  2446. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  2447. if not assigned(hsym) then
  2448. internalerror(2011020506);
  2449. highloc:=hsym.initialloc
  2450. end
  2451. else
  2452. highloc.loc:=LOC_INVALID;
  2453. hreg:=getaddressregister(list,voidpointertype);
  2454. if not is_packed_array(tparavarsym(p).vardef) then
  2455. g_copyvaluepara_openarray(list,href,highloc,tarraydef(tparavarsym(p).vardef),hreg)
  2456. else
  2457. internalerror(2011020507);
  2458. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  2459. a_load_reg_loc(list,tparavarsym(p).vardef,tparavarsym(p).vardef,hreg,tparavarsym(p).initialloc);
  2460. end;
  2461. end
  2462. else
  2463. begin
  2464. { Allocate space for the local copy }
  2465. l:=tparavarsym(p).getsize;
  2466. localcopyloc.loc:=LOC_REFERENCE;
  2467. localcopyloc.size:=int_cgsize(l);
  2468. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  2469. { Copy data }
  2470. if is_shortstring(tparavarsym(p).vardef) then
  2471. begin
  2472. { this code is only executed before the code for the body and the entry/exit code is generated
  2473. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  2474. }
  2475. include(current_procinfo.flags,pi_do_call);
  2476. g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef))
  2477. end
  2478. else if tparavarsym(p).vardef.typ=variantdef then
  2479. begin
  2480. { this code is only executed before the code for the body and the entry/exit code is generated
  2481. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  2482. }
  2483. include(current_procinfo.flags,pi_do_call);
  2484. g_copyvariant(list,href,localcopyloc.reference,tvariantdef(tparavarsym(p).vardef))
  2485. end
  2486. else
  2487. begin
  2488. { pass proper alignment info }
  2489. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  2490. g_concatcopy(list,tparavarsym(p).vardef,href,localcopyloc.reference);
  2491. end;
  2492. { update localloc of varsym }
  2493. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  2494. tparavarsym(p).localloc:=localcopyloc;
  2495. tparavarsym(p).initialloc:=localcopyloc;
  2496. end;
  2497. end;
  2498. end;
  2499. procedure thlcgobj.gen_loadfpu_loc_cgpara(list: TAsmList; size: tdef; const l: tlocation; const cgpara: tcgpara; locintsize: longint);
  2500. begin
  2501. case l.loc of
  2502. (*
  2503. LOC_MMREGISTER,
  2504. LOC_CMMREGISTER:
  2505. case cgpara.location^.loc of
  2506. LOC_REFERENCE,
  2507. LOC_CREFERENCE,
  2508. LOC_MMREGISTER,
  2509. LOC_CMMREGISTER,
  2510. LOC_REGISTER,
  2511. LOC_CREGISTER :
  2512. cg.a_loadmm_reg_cgpara(list,locsize,l.register,cgpara,mms_movescalar);
  2513. LOC_FPUREGISTER,
  2514. LOC_CFPUREGISTER:
  2515. begin
  2516. tmploc:=l;
  2517. location_force_fpureg(list,tmploc,false);
  2518. cg.a_loadfpu_reg_cgpara(list,tmploc.size,tmploc.register,cgpara);
  2519. end;
  2520. else
  2521. internalerror(200204249);
  2522. end;
  2523. *)
  2524. LOC_FPUREGISTER,
  2525. LOC_CFPUREGISTER:
  2526. case cgpara.location^.loc of
  2527. (*
  2528. LOC_MMREGISTER,
  2529. LOC_CMMREGISTER:
  2530. begin
  2531. tmploc:=l;
  2532. location_force_mmregscalar(list,tmploc,false);
  2533. cg.a_loadmm_reg_cgpara(list,tmploc.size,tmploc.register,cgpara,mms_movescalar);
  2534. end;
  2535. *)
  2536. { Some targets pass floats in normal registers }
  2537. LOC_REGISTER,
  2538. LOC_CREGISTER,
  2539. LOC_REFERENCE,
  2540. LOC_CREFERENCE,
  2541. LOC_FPUREGISTER,
  2542. LOC_CFPUREGISTER:
  2543. a_loadfpu_reg_cgpara(list,size,l.register,cgpara);
  2544. else
  2545. internalerror(2011010210);
  2546. end;
  2547. LOC_REFERENCE,
  2548. LOC_CREFERENCE:
  2549. case cgpara.location^.loc of
  2550. (*
  2551. LOC_MMREGISTER,
  2552. LOC_CMMREGISTER:
  2553. cg.a_loadmm_ref_cgpara(list,locsize,l.reference,cgpara,mms_movescalar);
  2554. *)
  2555. { Some targets pass floats in normal registers }
  2556. LOC_REGISTER,
  2557. LOC_CREGISTER,
  2558. LOC_REFERENCE,
  2559. LOC_CREFERENCE,
  2560. LOC_FPUREGISTER,
  2561. LOC_CFPUREGISTER:
  2562. a_loadfpu_ref_cgpara(list,size,l.reference,cgpara);
  2563. else
  2564. internalerror(2011010211);
  2565. end;
  2566. LOC_REGISTER,
  2567. LOC_CREGISTER :
  2568. a_load_loc_cgpara(list,size,l,cgpara);
  2569. else
  2570. internalerror(2011010212);
  2571. end;
  2572. end;
  2573. procedure thlcgobj.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  2574. begin
  2575. { do nothing by default }
  2576. end;
  2577. procedure thlcgobj.gen_load_loc_cgpara(list: TAsmList; vardef: tdef; const l: tlocation; const cgpara: tcgpara);
  2578. begin
  2579. { Handle Floating point types differently
  2580. This doesn't depend on emulator settings, emulator settings should
  2581. be handled by cpupara }
  2582. if (vardef.typ=floatdef) or
  2583. { some ABIs return certain records in an fpu register }
  2584. (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) or
  2585. (assigned(cgpara.location) and
  2586. (cgpara.Location^.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) then
  2587. begin
  2588. gen_loadfpu_loc_cgpara(list,vardef,l,cgpara,vardef.size);
  2589. exit;
  2590. end;
  2591. case l.loc of
  2592. LOC_CONSTANT,
  2593. LOC_REGISTER,
  2594. LOC_CREGISTER,
  2595. LOC_REFERENCE,
  2596. LOC_CREFERENCE :
  2597. begin
  2598. a_load_loc_cgpara(list,vardef,l,cgpara);
  2599. end;
  2600. (*
  2601. LOC_MMREGISTER,
  2602. LOC_CMMREGISTER:
  2603. begin
  2604. case l.size of
  2605. OS_F32,
  2606. OS_F64:
  2607. cg.a_loadmm_loc_cgpara(list,l,cgpara,mms_movescalar);
  2608. else
  2609. cg.a_loadmm_loc_cgpara(list,l,cgpara,nil);
  2610. end;
  2611. end;
  2612. *)
  2613. else
  2614. internalerror(2011010213);
  2615. end;
  2616. end;
  2617. procedure thlcgobj.gen_load_cgpara_loc(list: TAsmList; vardef: tdef; const para: TCGPara; var destloc: tlocation; reusepara: boolean);
  2618. var
  2619. href : treference;
  2620. begin
  2621. para.check_simple_location;
  2622. { skip e.g. empty records }
  2623. if (para.location^.loc = LOC_VOID) then
  2624. exit;
  2625. case destloc.loc of
  2626. LOC_REFERENCE :
  2627. begin
  2628. { If the parameter location is reused we don't need to copy
  2629. anything }
  2630. if not reusepara then
  2631. begin
  2632. reference_reset_base(href,para.location^.reference.index,para.location^.reference.offset,para.alignment);
  2633. a_load_ref_ref(list,para.def,para.def,href,destloc.reference);
  2634. end;
  2635. end;
  2636. { TODO other possible locations }
  2637. else
  2638. internalerror(2011010308);
  2639. end;
  2640. end;
  2641. procedure thlcgobj.gen_load_return_value(list: TAsmList);
  2642. var
  2643. ressym : tabstractnormalvarsym;
  2644. funcretloc : TCGPara;
  2645. begin
  2646. { Is the loading needed? }
  2647. if is_void(current_procinfo.procdef.returndef) or
  2648. (
  2649. (po_assembler in current_procinfo.procdef.procoptions) and
  2650. (not(assigned(current_procinfo.procdef.funcretsym)) or
  2651. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  2652. ) then
  2653. exit;
  2654. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  2655. { constructors return self }
  2656. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  2657. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.Find('self'))
  2658. else
  2659. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  2660. if (ressym.refs>0) or
  2661. is_managed_type(ressym.vardef) then
  2662. begin
  2663. { was: don't do anything if funcretloc.loc in [LOC_INVALID,LOC_REFERENCE] }
  2664. if not paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  2665. gen_load_loc_cgpara(list,ressym.vardef,ressym.localloc,funcretloc);
  2666. end
  2667. else
  2668. gen_load_uninitialized_function_result(list,current_procinfo.procdef,ressym.vardef,funcretloc)
  2669. end;
  2670. procedure thlcgobj.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  2671. begin
  2672. { add the procedure to the al_procedures }
  2673. maybe_new_object_file(current_asmdata.asmlists[al_procedures]);
  2674. new_section(current_asmdata.asmlists[al_procedures],sec_code,lower(pd.mangledname),getprocalign);
  2675. current_asmdata.asmlists[al_procedures].concatlist(code);
  2676. { save local data (casetable) also in the same file }
  2677. if assigned(data) and
  2678. (not data.empty) then
  2679. current_asmdata.asmlists[al_procedures].concatlist(data);
  2680. end;
  2681. procedure thlcgobj.g_call_system_proc(list: TAsmList; const procname: string);
  2682. var
  2683. srsym: tsym;
  2684. pd: tprocdef;
  2685. begin
  2686. srsym:=tsym(systemunit.find(procname));
  2687. if not assigned(srsym) or
  2688. (srsym.typ<>procsym) then
  2689. Message1(cg_f_unknown_compilerproc,procname);
  2690. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  2691. a_call_name(list,pd,pd.mangledname,false);
  2692. end;
  2693. end.