hlcgobj.pas 127 KB

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