hlcgobj.pas 118 KB

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