hlcgobj.pas 130 KB

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