cgobj.pas 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006
  1. {
  2. Copyright (c) 1998-2005 by Florian Klaempfl
  3. Member of the Free Pascal development team
  4. This unit implements the basic 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. Abstreact code generator unit. This contains the base class
  20. to implement for all new supported processors.
  21. WARNING: None of the routines implemented in these modules,
  22. or their descendants, should use the temp. allocator, as
  23. these routines may be called inside genentrycode, and the
  24. stack frame is already setup!
  25. }
  26. unit cgobj;
  27. {$i fpcdefs.inc}
  28. interface
  29. uses
  30. globtype,constexp,
  31. cpubase,cgbase,cgutils,parabase,
  32. aasmbase,aasmtai,aasmdata,aasmcpu,
  33. symconst,symtype,symdef,rgobj
  34. ;
  35. type
  36. talignment = (AM_NATURAL,AM_NONE,AM_2BYTE,AM_4BYTE,AM_8BYTE);
  37. {# @abstract(Abstract code generator)
  38. This class implements an abstract instruction generator. Some of
  39. the methods of this class are generic, while others must
  40. be overridden for all new processors which will be supported
  41. by Free Pascal. For 32-bit processors, the base class
  42. should be @link(tcg64f32) and not @var(tcg).
  43. }
  44. { tcg }
  45. tcg = class
  46. { how many times is this current code executed }
  47. executionweight : longint;
  48. alignment : talignment;
  49. rg : array[tregistertype] of trgobj;
  50. {$ifdef flowgraph}
  51. aktflownode:word;
  52. {$endif}
  53. {************************************************}
  54. { basic routines }
  55. constructor create;
  56. {# Initialize the register allocators needed for the codegenerator.}
  57. procedure init_register_allocators;virtual;
  58. {# Clean up the register allocators needed for the codegenerator.}
  59. procedure done_register_allocators;virtual;
  60. {# 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. }
  61. procedure set_regalloc_live_range_direction(dir: TRADirection);
  62. {$ifdef flowgraph}
  63. procedure init_flowgraph;
  64. procedure done_flowgraph;
  65. {$endif}
  66. {# Gets a register suitable to do integer operations on.}
  67. function getintregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  68. {# Gets a register suitable to do integer operations on.}
  69. function getaddressregister(list:TAsmList):Tregister;virtual;
  70. function getfpuregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  71. function getmmregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  72. function getflagregister(list:TAsmList;size:Tcgsize):Tregister;virtual;
  73. {Does the generic cg need SIMD registers, like getmmxregister? Or should
  74. the cpu specific child cg object have such a method?}
  75. procedure add_reg_instruction(instr:Tai;r:tregister);virtual;
  76. procedure add_move_instruction(instr:Taicpu);virtual;
  77. function uses_registers(rt:Tregistertype):boolean;virtual;
  78. {# Get a specific register.}
  79. procedure getcpuregister(list:TAsmList;r:Tregister);virtual;
  80. procedure ungetcpuregister(list:TAsmList;r:Tregister);virtual;
  81. {# Get multiple registers specified.}
  82. procedure alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  83. {# Free multiple registers specified.}
  84. procedure dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  85. procedure allocallcpuregisters(list:TAsmList);virtual;
  86. procedure deallocallcpuregisters(list:TAsmList);virtual;
  87. procedure do_register_allocation(list:TAsmList;headertai:tai);virtual;
  88. procedure translate_register(var reg : tregister);
  89. function makeregsize(list:TAsmList;reg:Tregister;size:Tcgsize):Tregister;
  90. {# Emit a label to the instruction stream. }
  91. procedure a_label(list : TAsmList;l : tasmlabel);virtual;
  92. {# Allocates register r by inserting a pai_realloc record }
  93. procedure a_reg_alloc(list : TAsmList;r : tregister);
  94. {# Deallocates register r by inserting a pa_regdealloc record}
  95. procedure a_reg_dealloc(list : TAsmList;r : tregister);
  96. { Synchronize register, make sure it is still valid }
  97. procedure a_reg_sync(list : TAsmList;r : tregister);
  98. {# Pass a parameter, which is located in a register, to a routine.
  99. This routine should push/send the parameter to the routine, as
  100. required by the specific processor ABI and routine modifiers.
  101. It must generate register allocation information for the cgpara in
  102. case it consists of cpuregisters.
  103. @param(size size of the operand in the register)
  104. @param(r register source of the operand)
  105. @param(cgpara where the parameter will be stored)
  106. }
  107. procedure a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : TCGPara);virtual;
  108. {# Pass a parameter, which is a constant, to a routine.
  109. A generic version is provided. This routine should
  110. be overridden for optimization purposes if the cpu
  111. permits directly sending this type of parameter.
  112. It must generate register allocation information for the cgpara in
  113. case it consists of cpuregisters.
  114. @param(size size of the operand in constant)
  115. @param(a value of constant to send)
  116. @param(cgpara where the parameter will be stored)
  117. }
  118. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : TCGPara);virtual;
  119. {# Pass the value of a parameter, which is located in memory, to a routine.
  120. A generic version is provided. This routine should
  121. be overridden for optimization purposes if the cpu
  122. permits directly sending this type of parameter.
  123. It must generate register allocation information for the cgpara in
  124. case it consists of cpuregisters.
  125. @param(size size of the operand in constant)
  126. @param(r Memory reference of value to send)
  127. @param(cgpara where the parameter will be stored)
  128. }
  129. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : TCGPara);virtual;
  130. {# Pass the value of a parameter, which can be located either in a register or memory location,
  131. to a routine.
  132. A generic version is provided.
  133. @param(l location of the operand to send)
  134. @param(nr parameter number (starting from one) of routine (from left to right))
  135. @param(cgpara where the parameter will be stored)
  136. }
  137. procedure a_load_loc_cgpara(list : TAsmList;const l : tlocation;const cgpara : TCGPara);
  138. {# Pass the address of a reference to a routine. This routine
  139. will calculate the address of the reference, and pass this
  140. calculated address as a parameter.
  141. It must generate register allocation information for the cgpara in
  142. case it consists of cpuregisters.
  143. A generic version is provided. This routine should
  144. be overridden for optimization purposes if the cpu
  145. permits directly sending this type of parameter.
  146. @param(r reference to get address from)
  147. @param(nr parameter number (starting from one) of routine (from left to right))
  148. }
  149. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : TCGPara);virtual;
  150. {# Load a cgparaloc into a memory reference.
  151. It must generate register allocation information for the cgpara in
  152. case it consists of cpuregisters.
  153. @param(paraloc the source parameter sublocation)
  154. @param(ref the destination reference)
  155. @param(sizeleft indicates the total number of bytes left in all of
  156. the remaining sublocations of this parameter (the current
  157. sublocation and all of the sublocations coming after it).
  158. In case this location is also a reference, it is assumed
  159. to be the final part sublocation of the parameter and that it
  160. contains all of the "sizeleft" bytes).)
  161. @param(align the alignment of the paraloc in case it's a reference)
  162. }
  163. procedure a_load_cgparaloc_ref(list : TAsmList;const paraloc : TCGParaLocation;const ref : treference;sizeleft : tcgint;align : longint);
  164. {# Load a cgparaloc into any kind of register (int, fp, mm).
  165. @param(regsize the size of the destination register)
  166. @param(paraloc the source parameter sublocation)
  167. @param(reg the destination register)
  168. @param(align the alignment of the paraloc in case it's a reference)
  169. }
  170. procedure a_load_cgparaloc_anyreg(list : TAsmList;regsize : tcgsize;const paraloc : TCGParaLocation;reg : tregister;align : longint);
  171. { Remarks:
  172. * If a method specifies a size you have only to take care
  173. of that number of bits, i.e. load_const_reg with OP_8 must
  174. only load the lower 8 bit of the specified register
  175. the rest of the register can be undefined
  176. if necessary the compiler will call a method
  177. to zero or sign extend the register
  178. * The a_load_XX_XX with OP_64 needn't to be
  179. implemented for 32 bit
  180. processors, the code generator takes care of that
  181. * the addr size is for work with the natural pointer
  182. size
  183. * the procedures without fpu/mm are only for integer usage
  184. * normally the first location is the source and the
  185. second the destination
  186. }
  187. {# Emits instruction to call the method specified by symbol name.
  188. This routine must be overridden for each new target cpu.
  189. }
  190. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);virtual; abstract;
  191. procedure a_call_reg(list : TAsmList;reg : tregister);virtual; abstract;
  192. { same as a_call_name, might be overridden on certain architectures to emit
  193. static calls without usage of a got trampoline }
  194. procedure a_call_name_static(list : TAsmList;const s : string);virtual;
  195. { move instructions }
  196. procedure a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);virtual; abstract;
  197. procedure a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : treference);virtual;
  198. procedure a_load_const_loc(list : TAsmList;a : tcgint;const loc : tlocation);
  199. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  200. procedure a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual;
  201. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  202. procedure a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  203. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  204. procedure a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual;
  205. procedure a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);virtual;
  206. procedure a_load_loc_reg(list : TAsmList;tosize: tcgsize; const loc: tlocation; reg : tregister);
  207. procedure a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  208. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);virtual; abstract;
  209. { bit scan instructions }
  210. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tcgsize; src, dst: TRegister); virtual;
  211. { Multiplication with doubling result size.
  212. dstlo or dsthi may be NR_NO, in which case corresponding half of result is discarded. }
  213. procedure a_mul_reg_reg_pair(list: TAsmList; size: tcgsize; src1,src2,dstlo,dsthi: TRegister);virtual;
  214. { fpu move instructions }
  215. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize:tcgsize; reg1, reg2: tregister); virtual; abstract;
  216. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  217. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  218. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tcgsize; const ref1,ref2: treference);
  219. procedure a_loadfpu_loc_reg(list: TAsmList; tosize: tcgsize; const loc: tlocation; const reg: tregister);
  220. procedure a_loadfpu_reg_loc(list: TAsmList; fromsize: tcgsize; const reg: tregister; const loc: tlocation);
  221. procedure a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);virtual;
  222. procedure a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);virtual;
  223. { vector register move instructions }
  224. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); virtual;
  225. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  226. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual;
  227. procedure a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  228. procedure a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  229. procedure a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  230. procedure a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  231. procedure a_loadmm_loc_cgpara(list: TAsmList; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  232. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle); virtual;
  233. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  234. procedure a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  235. procedure a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  236. procedure a_opmm_loc_reg_reg(list: TAsmList;Op : TOpCG;size : tcgsize;const loc : tlocation;src,dst : tregister;shuffle : pmmshuffle); virtual;
  237. procedure a_opmm_reg_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src1,src2,dst: tregister;shuffle : pmmshuffle); virtual;
  238. procedure a_opmm_ref_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; src,dst: tregister;shuffle : pmmshuffle); virtual;
  239. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle); virtual;
  240. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize; mmreg, intreg: tregister; shuffle : pmmshuffle); virtual;
  241. { basic arithmetic operations }
  242. { note: for operators which require only one argument (not, neg), use }
  243. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  244. { that in this case the *second* operand is used as both source and }
  245. { destination (JM) }
  246. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); virtual; abstract;
  247. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); virtual;
  248. procedure a_op_const_loc(list : TAsmList; Op: TOpCG; a: tcgint; const loc: tlocation);
  249. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  250. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  251. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  252. procedure a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  253. procedure a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  254. { trinary operations for processors that support them, 'emulated' }
  255. { on others. None with "ref" arguments since I don't think there }
  256. { are any processors that support it (JM) }
  257. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister); virtual;
  258. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister); virtual;
  259. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  260. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  261. { comparison operations }
  262. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  263. l : tasmlabel); virtual;
  264. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  265. l : tasmlabel); virtual;
  266. procedure a_cmp_const_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; a: tcgint; const loc: tlocation;
  267. l : tasmlabel);
  268. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  269. procedure a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  270. procedure a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  271. procedure a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  272. procedure a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  273. procedure a_cmp_ref_loc_label(list: TAsmList; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  274. l : tasmlabel);
  275. procedure a_jmp_name(list : TAsmList;const s : string); virtual; abstract;
  276. procedure a_jmp_always(list : TAsmList;l: tasmlabel); virtual; abstract;
  277. {$ifdef cpuflags}
  278. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); virtual; abstract;
  279. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  280. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  281. }
  282. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  283. procedure g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  284. {$endif cpuflags}
  285. {
  286. This routine tries to optimize the op_const_reg/ref opcode, and should be
  287. called at the start of a_op_const_reg/ref. It returns the actual opcode
  288. to emit, and the constant value to emit. This function can opcode OP_NONE to
  289. remove the opcode and OP_MOVE to replace it with a simple load
  290. @param(size Size of the operand in constant)
  291. @param(op The opcode to emit, returns the opcode which must be emitted)
  292. @param(a The constant which should be emitted, returns the constant which must
  293. be emitted)
  294. }
  295. procedure optimize_op_const(size: TCGSize; var op: topcg; var a : tcgint);virtual;
  296. {#
  297. This routine is used in exception management nodes. It should
  298. save the exception reason currently in the FUNCTION_RETURN_REG. The
  299. save should be done either to a temp (pointed to by href).
  300. or on the stack (pushing the value on the stack).
  301. The size of the value to save is OS_S32. The default version
  302. saves the exception reason to a temp. memory area.
  303. }
  304. procedure g_exception_reason_save(list : TAsmList; const href : treference);virtual;
  305. {#
  306. This routine is used in exception management nodes. It should
  307. save the exception reason constant. The
  308. save should be done either to a temp (pointed to by href).
  309. or on the stack (pushing the value on the stack).
  310. The size of the value to save is OS_S32. The default version
  311. saves the exception reason to a temp. memory area.
  312. }
  313. procedure g_exception_reason_save_const(list : TAsmList; const href : treference; a: tcgint);virtual;
  314. {#
  315. This routine is used in exception management nodes. It should
  316. load the exception reason to the FUNCTION_RETURN_REG. The saved value
  317. should either be in the temp. area (pointed to by href , href should
  318. *NOT* be freed) or on the stack (the value should be popped).
  319. The size of the value to save is OS_S32. The default version
  320. saves the exception reason to a temp. memory area.
  321. }
  322. procedure g_exception_reason_load(list : TAsmList; const href : treference);virtual;
  323. procedure g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  324. {# This should emit the opcode to copy len bytes from the source
  325. to destination.
  326. It must be overridden for each new target processor.
  327. @param(source Source reference of copy)
  328. @param(dest Destination reference of copy)
  329. }
  330. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);virtual; abstract;
  331. {# This should emit the opcode to copy len bytes from the an unaligned source
  332. to destination.
  333. It must be overridden for each new target processor.
  334. @param(source Source reference of copy)
  335. @param(dest Destination reference of copy)
  336. }
  337. procedure g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);virtual;
  338. {# Generates overflow checking code for a node }
  339. procedure g_overflowcheck(list: TAsmList; const Loc:tlocation; def:tdef); virtual;abstract;
  340. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);virtual;
  341. {# Emits instructions when compilation is done in profile
  342. mode (this is set as a command line option). The default
  343. behavior does nothing, should be overridden as required.
  344. }
  345. procedure g_profilecode(list : TAsmList);virtual;
  346. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  347. @param(size Number of bytes to allocate)
  348. }
  349. procedure g_stackpointer_alloc(list : TAsmList;size : longint);virtual;
  350. {# Emits instruction for allocating the locals in entry
  351. code of a routine. This is one of the first
  352. routine called in @var(genentrycode).
  353. @param(localsize Number of bytes to allocate as locals)
  354. }
  355. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);virtual; abstract;
  356. {# Emits instructions for returning from a subroutine.
  357. Should also restore the framepointer and stack.
  358. @param(parasize Number of bytes of parameters to deallocate from stack)
  359. }
  360. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);virtual;abstract;
  361. {# This routine is called when generating the code for the entry point
  362. of a routine. It should save all registers which are not used in this
  363. routine, and which should be declared as saved in the std_saved_registers
  364. set.
  365. This routine is mainly used when linking to code which is generated
  366. by ABI-compliant compilers (like GCC), to make sure that the reserved
  367. registers of that ABI are not clobbered.
  368. @param(usedinproc Registers which are used in the code of this routine)
  369. }
  370. procedure g_save_registers(list:TAsmList);virtual;
  371. {# This routine is called when generating the code for the exit point
  372. of a routine. It should restore all registers which were previously
  373. saved in @var(g_save_standard_registers).
  374. @param(usedinproc Registers which are used in the code of this routine)
  375. }
  376. procedure g_restore_registers(list:TAsmList);virtual;
  377. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);virtual;abstract;
  378. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);virtual;
  379. { generate a stub which only purpose is to pass control the given external method,
  380. setting up any additional environment before doing so (if required).
  381. The default implementation issues a jump instruction to the external name. }
  382. procedure g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string); virtual;
  383. { initialize the pic/got register }
  384. procedure g_maybe_got_init(list: TAsmList); virtual;
  385. { allocallcpuregisters, a_call_name, deallocallcpuregisters sequence }
  386. procedure g_call(list: TAsmList; const s: string);
  387. { Generate code to exit an unwind-protected region. The default implementation
  388. produces a simple jump to destination label. }
  389. procedure g_local_unwind(list: TAsmList; l: TAsmLabel);virtual;
  390. { Generate code for integer division by constant,
  391. generic version is suitable for 3-address CPUs }
  392. procedure g_div_const_reg_reg(list:tasmlist; size: TCgSize; a: tcgint; src,dst: tregister); virtual;
  393. protected
  394. function g_indirect_sym_load(list:TAsmList;const symname: string; const flags: tindsymflags): tregister;virtual;
  395. end;
  396. {$ifdef cpu64bitalu}
  397. { This class implements an abstract code generator class
  398. for 128 Bit operations, it applies currently only to 64 Bit CPUs and supports only simple operations
  399. }
  400. tcg128 = class
  401. procedure a_load128_reg_reg(list : TAsmList;regsrc,regdst : tregister128);virtual;
  402. procedure a_load128_reg_ref(list : TAsmList;reg : tregister128;const ref : treference);virtual;
  403. procedure a_load128_ref_reg(list : TAsmList;const ref : treference;reg : tregister128);virtual;
  404. procedure a_load128_loc_ref(list : TAsmList;const l : tlocation;const ref : treference);virtual;
  405. procedure a_load128_reg_loc(list : TAsmList;reg : tregister128;const l : tlocation);virtual;
  406. procedure a_load128_const_reg(list : TAsmList;valuelo,valuehi : int64;reg : tregister128);virtual;
  407. procedure a_load128_loc_cgpara(list : TAsmList;const l : tlocation;const paraloc : TCGPara);virtual;
  408. procedure a_load128_ref_cgpara(list: TAsmList; const r: treference;const paraloc: tcgpara);
  409. procedure a_load128_reg_cgpara(list: TAsmList; reg: tregister128;const paraloc: tcgpara);
  410. end;
  411. { Creates a tregister128 record from 2 64 Bit registers. }
  412. function joinreg128(reglo,reghi : tregister) : tregister128;
  413. {$else cpu64bitalu}
  414. {# @abstract(Abstract code generator for 64 Bit operations)
  415. This class implements an abstract code generator class
  416. for 64 Bit operations.
  417. }
  418. tcg64 = class
  419. procedure a_load64_const_ref(list : TAsmList;value : int64;const ref : treference);virtual;abstract;
  420. procedure a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference);virtual;abstract;
  421. procedure a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64);virtual;abstract;
  422. procedure a_load64_reg_reg(list : TAsmList;regsrc,regdst : tregister64);virtual;abstract;
  423. procedure a_load64_const_reg(list : TAsmList;value : int64;reg : tregister64);virtual;abstract;
  424. procedure a_load64_loc_reg(list : TAsmList;const l : tlocation;reg : tregister64);virtual;abstract;
  425. procedure a_load64_loc_ref(list : TAsmList;const l : tlocation;const ref : treference);virtual;abstract;
  426. procedure a_load64_const_loc(list : TAsmList;value : int64;const l : tlocation);virtual;abstract;
  427. procedure a_load64_reg_loc(list : TAsmList;reg : tregister64;const l : tlocation);virtual;abstract;
  428. procedure a_load64_subsetref_reg(list : TAsmList; const sref: tsubsetreference; destreg: tregister64);virtual;abstract;
  429. procedure a_load64_reg_subsetref(list : TAsmList; fromreg: tregister64; const sref: tsubsetreference);virtual;abstract;
  430. procedure a_load64_const_subsetref(list: TAsmlist; a: int64; const sref: tsubsetreference);virtual;abstract;
  431. procedure a_load64_ref_subsetref(list : TAsmList; const fromref: treference; const sref: tsubsetreference);virtual;abstract;
  432. procedure a_load64_subsetref_subsetref(list: TAsmlist; const fromsref, tosref: tsubsetreference); virtual;abstract;
  433. procedure a_load64_subsetref_ref(list : TAsmList; const sref: tsubsetreference; const destref: treference); virtual;abstract;
  434. procedure a_load64_loc_subsetref(list : TAsmList; const l: tlocation; const sref : tsubsetreference);
  435. procedure a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  436. procedure a_load64high_reg_ref(list : TAsmList;reg : tregister;const ref : treference);virtual;abstract;
  437. procedure a_load64low_reg_ref(list : TAsmList;reg : tregister;const ref : treference);virtual;abstract;
  438. procedure a_load64high_ref_reg(list : TAsmList;const ref : treference;reg : tregister);virtual;abstract;
  439. procedure a_load64low_ref_reg(list : TAsmList;const ref : treference;reg : tregister);virtual;abstract;
  440. procedure a_load64high_loc_reg(list : TAsmList;const l : tlocation;reg : tregister);virtual;abstract;
  441. procedure a_load64low_loc_reg(list : TAsmList;const l : tlocation;reg : tregister);virtual;abstract;
  442. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);virtual;abstract;
  443. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);virtual;abstract;
  444. procedure a_op64_reg_ref(list : TAsmList;op:TOpCG;size : tcgsize;regsrc : tregister64;const ref : treference);virtual;abstract;
  445. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;regdst : tregister64);virtual;abstract;
  446. procedure a_op64_const_ref(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const ref : treference);virtual;abstract;
  447. procedure a_op64_const_loc(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;const l: tlocation);virtual;abstract;
  448. procedure a_op64_reg_loc(list : TAsmList;op:TOpCG;size : tcgsize;reg : tregister64;const l : tlocation);virtual;abstract;
  449. procedure a_op64_loc_reg(list : TAsmList;op:TOpCG;size : tcgsize;const l : tlocation;reg64 : tregister64);virtual;abstract;
  450. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);virtual;
  451. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);virtual;
  452. procedure a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  453. procedure a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  454. procedure a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  455. procedure a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  456. procedure a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  457. procedure a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  458. procedure a_load64_reg_cgpara(list : TAsmList;reg64 : tregister64;const loc : TCGPara);virtual;abstract;
  459. procedure a_load64_const_cgpara(list : TAsmList;value : int64;const loc : TCGPara);virtual;abstract;
  460. procedure a_load64_ref_cgpara(list : TAsmList;const r : treference;const loc : TCGPara);virtual;abstract;
  461. procedure a_load64_loc_cgpara(list : TAsmList;const l : tlocation;const loc : TCGPara);virtual;abstract;
  462. procedure a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister); virtual;abstract;
  463. procedure a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64); virtual;abstract;
  464. {
  465. This routine tries to optimize the const_reg opcode, and should be
  466. called at the start of a_op64_const_reg. It returns the actual opcode
  467. to emit, and the constant value to emit. If this routine returns
  468. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  469. @param(op The opcode to emit, returns the opcode which must be emitted)
  470. @param(a The constant which should be emitted, returns the constant which must
  471. be emitted)
  472. @param(reg The register to emit the opcode with, returns the register with
  473. which the opcode will be emitted)
  474. }
  475. function optimize64_op_const_reg(list: TAsmList; var op: topcg; var a : int64; var reg: tregister64): boolean;virtual;abstract;
  476. { override to catch 64bit rangechecks }
  477. procedure g_rangecheck64(list: TAsmList; const l:tlocation; fromdef,todef: tdef);virtual;abstract;
  478. end;
  479. { Creates a tregister64 record from 2 32 Bit registers. }
  480. function joinreg64(reglo,reghi : tregister) : tregister64;
  481. {$endif cpu64bitalu}
  482. var
  483. { Main code generator class }
  484. cg : tcg;
  485. {$ifdef cpu64bitalu}
  486. { Code generator class for all operations working with 128-Bit operands }
  487. cg128 : tcg128;
  488. {$else cpu64bitalu}
  489. { Code generator class for all operations working with 64-Bit operands }
  490. cg64 : tcg64;
  491. {$endif cpu64bitalu}
  492. function asmsym2indsymflags(sym: TAsmSymbol): tindsymflags;
  493. procedure destroy_codegen;
  494. implementation
  495. uses
  496. globals,systems,
  497. verbose,paramgr,symtable,symsym,
  498. tgobj,cutils,procinfo;
  499. {*****************************************************************************
  500. basic functionallity
  501. ******************************************************************************}
  502. constructor tcg.create;
  503. begin
  504. end;
  505. {*****************************************************************************
  506. register allocation
  507. ******************************************************************************}
  508. procedure tcg.init_register_allocators;
  509. begin
  510. fillchar(rg,sizeof(rg),0);
  511. add_reg_instruction_hook:=@add_reg_instruction;
  512. executionweight:=1;
  513. end;
  514. procedure tcg.done_register_allocators;
  515. begin
  516. { Safety }
  517. fillchar(rg,sizeof(rg),0);
  518. add_reg_instruction_hook:=nil;
  519. end;
  520. {$ifdef flowgraph}
  521. procedure Tcg.init_flowgraph;
  522. begin
  523. aktflownode:=0;
  524. end;
  525. procedure Tcg.done_flowgraph;
  526. begin
  527. end;
  528. {$endif}
  529. function tcg.getintregister(list:TAsmList;size:Tcgsize):Tregister;
  530. begin
  531. if not assigned(rg[R_INTREGISTER]) then
  532. internalerror(200312122);
  533. result:=rg[R_INTREGISTER].getregister(list,cgsize2subreg(R_INTREGISTER,size));
  534. end;
  535. function tcg.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  536. begin
  537. if not assigned(rg[R_FPUREGISTER]) then
  538. internalerror(200312123);
  539. result:=rg[R_FPUREGISTER].getregister(list,cgsize2subreg(R_FPUREGISTER,size));
  540. end;
  541. function tcg.getmmregister(list:TAsmList;size:Tcgsize):Tregister;
  542. begin
  543. if not assigned(rg[R_MMREGISTER]) then
  544. internalerror(2003121214);
  545. result:=rg[R_MMREGISTER].getregister(list,cgsize2subreg(R_MMREGISTER,size));
  546. end;
  547. function tcg.getaddressregister(list:TAsmList):Tregister;
  548. begin
  549. if assigned(rg[R_ADDRESSREGISTER]) then
  550. result:=rg[R_ADDRESSREGISTER].getregister(list,R_SUBWHOLE)
  551. else
  552. begin
  553. if not assigned(rg[R_INTREGISTER]) then
  554. internalerror(200312121);
  555. result:=rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  556. end;
  557. end;
  558. function Tcg.makeregsize(list:TAsmList;reg:Tregister;size:Tcgsize):Tregister;
  559. var
  560. subreg:Tsubregister;
  561. begin
  562. subreg:=cgsize2subreg(getregtype(reg),size);
  563. result:=reg;
  564. setsubreg(result,subreg);
  565. { notify RA }
  566. if result<>reg then
  567. list.concat(tai_regalloc.resize(result));
  568. end;
  569. procedure tcg.getcpuregister(list:TAsmList;r:Tregister);
  570. begin
  571. if not assigned(rg[getregtype(r)]) then
  572. internalerror(200312125);
  573. rg[getregtype(r)].getcpuregister(list,r);
  574. end;
  575. procedure tcg.ungetcpuregister(list:TAsmList;r:Tregister);
  576. begin
  577. if not assigned(rg[getregtype(r)]) then
  578. internalerror(200312126);
  579. rg[getregtype(r)].ungetcpuregister(list,r);
  580. end;
  581. procedure tcg.alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  582. begin
  583. if assigned(rg[rt]) then
  584. rg[rt].alloccpuregisters(list,r)
  585. else
  586. internalerror(200310092);
  587. end;
  588. procedure tcg.allocallcpuregisters(list:TAsmList);
  589. begin
  590. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  591. if uses_registers(R_ADDRESSREGISTER) then
  592. alloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  593. {$if not(defined(i386)) and not(defined(i8086)) and not(defined(avr))}
  594. if uses_registers(R_FPUREGISTER) then
  595. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  596. {$ifdef cpumm}
  597. if uses_registers(R_MMREGISTER) then
  598. alloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  599. {$endif cpumm}
  600. {$endif not(defined(i386)) and not(defined(i8086)) and not(defined(avr))}
  601. end;
  602. procedure tcg.dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  603. begin
  604. if assigned(rg[rt]) then
  605. rg[rt].dealloccpuregisters(list,r)
  606. else
  607. internalerror(200310093);
  608. end;
  609. procedure tcg.deallocallcpuregisters(list:TAsmList);
  610. begin
  611. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  612. if uses_registers(R_ADDRESSREGISTER) then
  613. dealloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  614. {$if not(defined(i386)) and not(defined(i8086)) and not(defined(avr))}
  615. if uses_registers(R_FPUREGISTER) then
  616. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  617. {$ifdef cpumm}
  618. if uses_registers(R_MMREGISTER) then
  619. dealloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  620. {$endif cpumm}
  621. {$endif not(defined(i386)) and not(defined(i8086)) and not(defined(avr))}
  622. end;
  623. function tcg.uses_registers(rt:Tregistertype):boolean;
  624. begin
  625. if assigned(rg[rt]) then
  626. result:=rg[rt].uses_registers
  627. else
  628. result:=false;
  629. end;
  630. procedure tcg.add_reg_instruction(instr:Tai;r:tregister);
  631. var
  632. rt : tregistertype;
  633. begin
  634. rt:=getregtype(r);
  635. { Only add it when a register allocator is configured.
  636. No IE can be generated, because the VMT is written
  637. without a valid rg[] }
  638. if assigned(rg[rt]) then
  639. rg[rt].add_reg_instruction(instr,r,executionweight);
  640. end;
  641. procedure tcg.add_move_instruction(instr:Taicpu);
  642. var
  643. rt : tregistertype;
  644. begin
  645. rt:=getregtype(instr.oper[O_MOV_SOURCE]^.reg);
  646. if assigned(rg[rt]) then
  647. rg[rt].add_move_instruction(instr)
  648. else
  649. internalerror(200310095);
  650. end;
  651. procedure tcg.set_regalloc_live_range_direction(dir: TRADirection);
  652. var
  653. rt : tregistertype;
  654. begin
  655. for rt:=low(rg) to high(rg) do
  656. begin
  657. if assigned(rg[rt]) then
  658. rg[rt].live_range_direction:=dir;
  659. end;
  660. end;
  661. procedure tcg.do_register_allocation(list:TAsmList;headertai:tai);
  662. var
  663. rt : tregistertype;
  664. begin
  665. for rt:=R_FPUREGISTER to R_SPECIALREGISTER do
  666. begin
  667. if assigned(rg[rt]) then
  668. rg[rt].do_register_allocation(list,headertai);
  669. end;
  670. { running the other register allocator passes could require addition int/addr. registers
  671. when spilling so run int/addr register allocation at the end }
  672. if assigned(rg[R_INTREGISTER]) then
  673. rg[R_INTREGISTER].do_register_allocation(list,headertai);
  674. if assigned(rg[R_ADDRESSREGISTER]) then
  675. rg[R_ADDRESSREGISTER].do_register_allocation(list,headertai);
  676. end;
  677. procedure tcg.translate_register(var reg : tregister);
  678. var
  679. rt: tregistertype;
  680. begin
  681. { Getting here without assigned rg is possible for an "assembler nostackframe"
  682. function returning x87 float, compiler tries to translate NR_ST which is used for
  683. result. }
  684. rt:=getregtype(reg);
  685. if assigned(rg[rt]) then
  686. rg[rt].translate_register(reg);
  687. end;
  688. procedure tcg.a_reg_alloc(list : TAsmList;r : tregister);
  689. begin
  690. list.concat(tai_regalloc.alloc(r,nil));
  691. end;
  692. procedure tcg.a_reg_dealloc(list : TAsmList;r : tregister);
  693. begin
  694. if (r<>NR_NO) then
  695. list.concat(tai_regalloc.dealloc(r,nil));
  696. end;
  697. procedure tcg.a_reg_sync(list : TAsmList;r : tregister);
  698. var
  699. instr : tai;
  700. begin
  701. instr:=tai_regalloc.sync(r);
  702. list.concat(instr);
  703. add_reg_instruction(instr,r);
  704. end;
  705. procedure tcg.a_label(list : TAsmList;l : tasmlabel);
  706. begin
  707. list.concat(tai_label.create(l));
  708. end;
  709. {*****************************************************************************
  710. for better code generation these methods should be overridden
  711. ******************************************************************************}
  712. procedure tcg.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : TCGPara);
  713. var
  714. ref : treference;
  715. tmpreg : tregister;
  716. begin
  717. if assigned(cgpara.location^.next) then
  718. begin
  719. tg.gethltemp(list,cgpara.def,cgpara.def.size,tt_persistent,ref);
  720. a_load_reg_ref(list,size,size,r,ref);
  721. a_load_ref_cgpara(list,size,ref,cgpara);
  722. tg.ungettemp(list,ref);
  723. exit;
  724. end;
  725. paramanager.alloccgpara(list,cgpara);
  726. if cgpara.location^.shiftval<0 then
  727. begin
  728. tmpreg:=getintregister(list,cgpara.location^.size);
  729. a_op_const_reg_reg(list,OP_SHL,cgpara.location^.size,-cgpara.location^.shiftval,r,tmpreg);
  730. r:=tmpreg;
  731. end;
  732. case cgpara.location^.loc of
  733. LOC_REGISTER,LOC_CREGISTER:
  734. a_load_reg_reg(list,size,cgpara.location^.size,r,cgpara.location^.register);
  735. LOC_REFERENCE,LOC_CREFERENCE:
  736. begin
  737. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  738. a_load_reg_ref(list,size,cgpara.location^.size,r,ref);
  739. end;
  740. LOC_MMREGISTER,LOC_CMMREGISTER:
  741. a_loadmm_intreg_reg(list,size,cgpara.location^.size,r,cgpara.location^.register,mms_movescalar);
  742. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  743. begin
  744. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,ref);
  745. a_load_reg_ref(list,size,size,r,ref);
  746. a_loadfpu_ref_cgpara(list,cgpara.location^.size,ref,cgpara);
  747. tg.Ungettemp(list,ref);
  748. end
  749. else
  750. internalerror(2002071004);
  751. end;
  752. end;
  753. procedure tcg.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : TCGPara);
  754. var
  755. ref : treference;
  756. begin
  757. cgpara.check_simple_location;
  758. paramanager.alloccgpara(list,cgpara);
  759. if cgpara.location^.shiftval<0 then
  760. a:=a shl -cgpara.location^.shiftval;
  761. case cgpara.location^.loc of
  762. LOC_REGISTER,LOC_CREGISTER:
  763. a_load_const_reg(list,cgpara.location^.size,a,cgpara.location^.register);
  764. LOC_REFERENCE,LOC_CREFERENCE:
  765. begin
  766. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  767. a_load_const_ref(list,cgpara.location^.size,a,ref);
  768. end
  769. else
  770. internalerror(2010053109);
  771. end;
  772. end;
  773. procedure tcg.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : TCGPara);
  774. var
  775. tmpref, ref: treference;
  776. tmpreg: tregister;
  777. location: pcgparalocation;
  778. orgsizeleft,
  779. sizeleft: tcgint;
  780. reghasvalue: boolean;
  781. begin
  782. location:=cgpara.location;
  783. tmpref:=r;
  784. sizeleft:=cgpara.intsize;
  785. while assigned(location) do
  786. begin
  787. paramanager.allocparaloc(list,location);
  788. case location^.loc of
  789. LOC_REGISTER,LOC_CREGISTER:
  790. begin
  791. { Parameter locations are often allocated in multiples of
  792. entire registers. If a parameter only occupies a part of
  793. such a register (e.g. a 16 bit int on a 32 bit
  794. architecture), the size of this parameter can only be
  795. determined by looking at the "size" parameter of this
  796. method -> if the size parameter is <= sizeof(aint), then
  797. we check that there is only one parameter location and
  798. then use this "size" to load the value into the parameter
  799. location }
  800. if (size<>OS_NO) and
  801. (tcgsize2size[size]<=sizeof(aint)) then
  802. begin
  803. cgpara.check_simple_location;
  804. a_load_ref_reg(list,size,location^.size,tmpref,location^.register);
  805. if location^.shiftval<0 then
  806. a_op_const_reg(list,OP_SHL,location^.size,-location^.shiftval,location^.register);
  807. end
  808. { there's a lot more data left, and the current paraloc's
  809. register is entirely filled with part of that data }
  810. else if (sizeleft>sizeof(aint)) then
  811. begin
  812. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  813. end
  814. { we're at the end of the data, and it can be loaded into
  815. the current location's register with a single regular
  816. load }
  817. else if sizeleft in [1,2,4,8] then
  818. begin
  819. a_load_ref_reg(list,int_cgsize(sizeleft),location^.size,tmpref,location^.register);
  820. if location^.shiftval<0 then
  821. a_op_const_reg(list,OP_SHL,location^.size,-location^.shiftval,location^.register);
  822. end
  823. { we're at the end of the data, and we need multiple loads
  824. to get it in the register because it's an irregular size }
  825. else
  826. begin
  827. { should be the last part }
  828. if assigned(location^.next) then
  829. internalerror(2010052907);
  830. { load the value piecewise to get it into the register }
  831. orgsizeleft:=sizeleft;
  832. reghasvalue:=false;
  833. {$ifdef cpu64bitalu}
  834. if sizeleft>=4 then
  835. begin
  836. a_load_ref_reg(list,OS_32,location^.size,tmpref,location^.register);
  837. dec(sizeleft,4);
  838. if target_info.endian=endian_big then
  839. a_op_const_reg(list,OP_SHL,location^.size,sizeleft*8,location^.register);
  840. inc(tmpref.offset,4);
  841. reghasvalue:=true;
  842. end;
  843. {$endif cpu64bitalu}
  844. if sizeleft>=2 then
  845. begin
  846. tmpreg:=getintregister(list,location^.size);
  847. a_load_ref_reg(list,OS_16,location^.size,tmpref,tmpreg);
  848. dec(sizeleft,2);
  849. if reghasvalue then
  850. begin
  851. if target_info.endian=endian_big then
  852. a_op_const_reg(list,OP_SHL,location^.size,sizeleft*8,tmpreg)
  853. else
  854. a_op_const_reg(list,OP_SHL,location^.size,(orgsizeleft-(sizeleft+2))*8,tmpreg);
  855. a_op_reg_reg(list,OP_OR,location^.size,tmpreg,location^.register);
  856. end
  857. else
  858. begin
  859. if target_info.endian=endian_big then
  860. a_op_const_reg_reg(list,OP_SHL,location^.size,sizeleft*8,tmpreg,location^.register)
  861. else
  862. a_load_reg_reg(list,location^.size,location^.size,tmpreg,location^.register);
  863. end;
  864. inc(tmpref.offset,2);
  865. reghasvalue:=true;
  866. end;
  867. if sizeleft=1 then
  868. begin
  869. tmpreg:=getintregister(list,location^.size);
  870. a_load_ref_reg(list,OS_8,location^.size,tmpref,tmpreg);
  871. dec(sizeleft,1);
  872. if reghasvalue then
  873. begin
  874. if target_info.endian=endian_little then
  875. a_op_const_reg(list,OP_SHL,location^.size,(orgsizeleft-(sizeleft+1))*8,tmpreg);
  876. a_op_reg_reg(list,OP_OR,location^.size,tmpreg,location^.register)
  877. end
  878. else
  879. a_load_reg_reg(list,location^.size,location^.size,tmpreg,location^.register);
  880. inc(tmpref.offset);
  881. end;
  882. if location^.shiftval<0 then
  883. a_op_const_reg(list,OP_SHL,location^.size,-location^.shiftval,location^.register);
  884. { the loop will already adjust the offset and sizeleft }
  885. dec(tmpref.offset,orgsizeleft);
  886. sizeleft:=orgsizeleft;
  887. end;
  888. end;
  889. LOC_REFERENCE,LOC_CREFERENCE:
  890. begin
  891. if assigned(location^.next) then
  892. internalerror(2010052906);
  893. reference_reset_base(ref,location^.reference.index,location^.reference.offset,newalignment(cgpara.alignment,cgpara.intsize-sizeleft));
  894. if (size <> OS_NO) and
  895. (tcgsize2size[size] <= sizeof(aint)) then
  896. a_load_ref_ref(list,size,location^.size,tmpref,ref)
  897. else
  898. { use concatcopy, because the parameter can be larger than }
  899. { what the OS_* constants can handle }
  900. g_concatcopy(list,tmpref,ref,sizeleft);
  901. end;
  902. LOC_MMREGISTER,LOC_CMMREGISTER:
  903. begin
  904. case location^.size of
  905. OS_F32,
  906. OS_F64,
  907. OS_F128:
  908. a_loadmm_ref_reg(list,location^.size,location^.size,tmpref,location^.register,mms_movescalar);
  909. OS_M8..OS_M128,
  910. OS_MS8..OS_MS128:
  911. a_loadmm_ref_reg(list,location^.size,location^.size,tmpref,location^.register,nil);
  912. else
  913. internalerror(2010053101);
  914. end;
  915. end
  916. else
  917. internalerror(2010053111);
  918. end;
  919. inc(tmpref.offset,tcgsize2size[location^.size]);
  920. dec(sizeleft,tcgsize2size[location^.size]);
  921. location:=location^.next;
  922. end;
  923. end;
  924. procedure tcg.a_load_loc_cgpara(list : TAsmList;const l:tlocation;const cgpara : TCGPara);
  925. begin
  926. case l.loc of
  927. LOC_REGISTER,
  928. LOC_CREGISTER :
  929. a_load_reg_cgpara(list,l.size,l.register,cgpara);
  930. LOC_CONSTANT :
  931. a_load_const_cgpara(list,l.size,l.value,cgpara);
  932. LOC_CREFERENCE,
  933. LOC_REFERENCE :
  934. a_load_ref_cgpara(list,l.size,l.reference,cgpara);
  935. else
  936. internalerror(2002032211);
  937. end;
  938. end;
  939. procedure tcg.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : TCGPara);
  940. var
  941. hr : tregister;
  942. begin
  943. cgpara.check_simple_location;
  944. if cgpara.location^.loc in [LOC_CREGISTER,LOC_REGISTER] then
  945. begin
  946. paramanager.allocparaloc(list,cgpara.location);
  947. a_loadaddr_ref_reg(list,r,cgpara.location^.register)
  948. end
  949. else
  950. begin
  951. hr:=getaddressregister(list);
  952. a_loadaddr_ref_reg(list,r,hr);
  953. a_load_reg_cgpara(list,OS_ADDR,hr,cgpara);
  954. end;
  955. end;
  956. procedure tcg.a_load_cgparaloc_ref(list : TAsmList;const paraloc : TCGParaLocation;const ref : treference;sizeleft : tcgint;align : longint);
  957. var
  958. href : treference;
  959. hreg : tregister;
  960. cgsize: tcgsize;
  961. begin
  962. case paraloc.loc of
  963. LOC_REGISTER :
  964. begin
  965. hreg:=paraloc.register;
  966. cgsize:=paraloc.size;
  967. if paraloc.shiftval>0 then
  968. a_op_const_reg_reg(list,OP_SHL,OS_INT,paraloc.shiftval,paraloc.register,paraloc.register)
  969. { in case the original size was 3 or 5/6/7 bytes, the value was
  970. shifted to the top of the to 4 resp. 8 byte register on the
  971. caller side and needs to be stored with those bytes at the
  972. start of the reference -> don't shift right }
  973. else if (paraloc.shiftval<0) and
  974. ((-paraloc.shiftval) in [1,2,4]) then
  975. begin
  976. a_op_const_reg_reg(list,OP_SHR,OS_INT,-paraloc.shiftval,paraloc.register,paraloc.register);
  977. { convert to a register of 1/2/4 bytes in size, since the
  978. original register had to be made larger to be able to hold
  979. the shifted value }
  980. cgsize:=int_cgsize(tcgsize2size[OS_INT]-(-paraloc.shiftval div 8));
  981. hreg:=getintregister(list,cgsize);
  982. a_load_reg_reg(list,OS_INT,cgsize,paraloc.register,hreg);
  983. end;
  984. a_load_reg_ref(list,paraloc.size,cgsize,hreg,ref);
  985. end;
  986. LOC_MMREGISTER :
  987. begin
  988. case paraloc.size of
  989. OS_F32,
  990. OS_F64,
  991. OS_F128:
  992. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  993. OS_M8..OS_M128,
  994. OS_MS8..OS_MS128:
  995. a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,nil);
  996. else
  997. internalerror(2010053102);
  998. end;
  999. end;
  1000. LOC_FPUREGISTER :
  1001. a_loadfpu_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1002. LOC_REFERENCE :
  1003. begin
  1004. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,align);
  1005. { use concatcopy, because it can also be a float which fails when
  1006. load_ref_ref is used. Don't copy data when the references are equal }
  1007. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1008. g_concatcopy(list,href,ref,sizeleft);
  1009. end;
  1010. else
  1011. internalerror(2002081302);
  1012. end;
  1013. end;
  1014. procedure tcg.a_load_cgparaloc_anyreg(list: TAsmList;regsize: tcgsize;const paraloc: TCGParaLocation;reg: tregister;align: longint);
  1015. var
  1016. href : treference;
  1017. begin
  1018. case paraloc.loc of
  1019. LOC_REGISTER :
  1020. begin
  1021. if paraloc.shiftval<0 then
  1022. a_op_const_reg_reg(list,OP_SHR,OS_INT,-paraloc.shiftval,paraloc.register,paraloc.register);
  1023. case getregtype(reg) of
  1024. R_ADDRESSREGISTER,
  1025. R_INTREGISTER:
  1026. a_load_reg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1027. R_MMREGISTER:
  1028. a_loadmm_intreg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1029. else
  1030. internalerror(2009112422);
  1031. end;
  1032. end;
  1033. LOC_MMREGISTER :
  1034. begin
  1035. case getregtype(reg) of
  1036. R_ADDRESSREGISTER,
  1037. R_INTREGISTER:
  1038. a_loadmm_reg_intreg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1039. R_MMREGISTER:
  1040. begin
  1041. case paraloc.size of
  1042. OS_F32,
  1043. OS_F64,
  1044. OS_F128:
  1045. a_loadmm_reg_reg(list,paraloc.size,regsize,paraloc.register,reg,mms_movescalar);
  1046. OS_M8..OS_M128,
  1047. OS_MS8..OS_MS128:
  1048. a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,nil);
  1049. else
  1050. internalerror(2010053102);
  1051. end;
  1052. end;
  1053. else
  1054. internalerror(2010053104);
  1055. end;
  1056. end;
  1057. LOC_FPUREGISTER :
  1058. a_loadfpu_reg_reg(list,paraloc.size,regsize,paraloc.register,reg);
  1059. LOC_REFERENCE :
  1060. begin
  1061. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,align);
  1062. case getregtype(reg) of
  1063. R_ADDRESSREGISTER,
  1064. R_INTREGISTER :
  1065. a_load_ref_reg(list,paraloc.size,regsize,href,reg);
  1066. R_FPUREGISTER :
  1067. a_loadfpu_ref_reg(list,paraloc.size,regsize,href,reg);
  1068. R_MMREGISTER :
  1069. { not paraloc.size, because it may be OS_64 instead of
  1070. OS_F64 in case the parameter is passed using integer
  1071. conventions (e.g., on ARM) }
  1072. a_loadmm_ref_reg(list,regsize,regsize,href,reg,mms_movescalar);
  1073. else
  1074. internalerror(2004101012);
  1075. end;
  1076. end;
  1077. else
  1078. internalerror(2002081302);
  1079. end;
  1080. end;
  1081. {****************************************************************************
  1082. some generic implementations
  1083. ****************************************************************************}
  1084. { memory/register loading }
  1085. procedure tcg.a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  1086. var
  1087. tmpref : treference;
  1088. tmpreg : tregister;
  1089. i : longint;
  1090. begin
  1091. if ref.alignment<tcgsize2size[fromsize] then
  1092. begin
  1093. tmpref:=ref;
  1094. { we take care of the alignment now }
  1095. tmpref.alignment:=0;
  1096. case FromSize of
  1097. OS_16,OS_S16:
  1098. begin
  1099. tmpreg:=getintregister(list,OS_16);
  1100. a_load_reg_reg(list,fromsize,OS_16,register,tmpreg);
  1101. if target_info.endian=endian_big then
  1102. inc(tmpref.offset);
  1103. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1104. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1105. tmpreg:=makeregsize(list,tmpreg,OS_16);
  1106. a_op_const_reg(list,OP_SHR,OS_16,8,tmpreg);
  1107. if target_info.endian=endian_big then
  1108. dec(tmpref.offset)
  1109. else
  1110. inc(tmpref.offset);
  1111. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1112. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1113. end;
  1114. OS_32,OS_S32:
  1115. begin
  1116. { could add an optimised case for ref.alignment=2 }
  1117. tmpreg:=getintregister(list,OS_32);
  1118. a_load_reg_reg(list,fromsize,OS_32,register,tmpreg);
  1119. if target_info.endian=endian_big then
  1120. inc(tmpref.offset,3);
  1121. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1122. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1123. tmpreg:=makeregsize(list,tmpreg,OS_32);
  1124. for i:=1 to 3 do
  1125. begin
  1126. a_op_const_reg(list,OP_SHR,OS_32,8,tmpreg);
  1127. if target_info.endian=endian_big then
  1128. dec(tmpref.offset)
  1129. else
  1130. inc(tmpref.offset);
  1131. tmpreg:=makeregsize(list,tmpreg,OS_8);
  1132. a_load_reg_ref(list,OS_8,OS_8,tmpreg,tmpref);
  1133. tmpreg:=makeregsize(list,tmpreg,OS_32);
  1134. end;
  1135. end
  1136. else
  1137. a_load_reg_ref(list,fromsize,tosize,register,tmpref);
  1138. end;
  1139. end
  1140. else
  1141. a_load_reg_ref(list,fromsize,tosize,register,ref);
  1142. end;
  1143. procedure tcg.a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  1144. var
  1145. tmpref : treference;
  1146. tmpreg,
  1147. tmpreg2 : tregister;
  1148. i : longint;
  1149. hisize : tcgsize;
  1150. begin
  1151. if ref.alignment in [1,2] then
  1152. begin
  1153. tmpref:=ref;
  1154. { we take care of the alignment now }
  1155. tmpref.alignment:=0;
  1156. case FromSize of
  1157. OS_16,OS_S16:
  1158. if ref.alignment=2 then
  1159. a_load_ref_reg(list,fromsize,tosize,tmpref,register)
  1160. else
  1161. begin
  1162. if FromSize=OS_16 then
  1163. hisize:=OS_8
  1164. else
  1165. hisize:=OS_S8;
  1166. { first load in tmpreg, because the target register }
  1167. { may be used in ref as well }
  1168. if target_info.endian=endian_little then
  1169. inc(tmpref.offset);
  1170. tmpreg:=getintregister(list,OS_8);
  1171. a_load_ref_reg(list,hisize,hisize,tmpref,tmpreg);
  1172. tmpreg:=makeregsize(list,tmpreg,FromSize);
  1173. a_op_const_reg(list,OP_SHL,FromSize,8,tmpreg);
  1174. if target_info.endian=endian_little then
  1175. dec(tmpref.offset)
  1176. else
  1177. inc(tmpref.offset);
  1178. tmpreg2:=makeregsize(list,register,OS_16);
  1179. a_load_ref_reg(list,OS_8,OS_16,tmpref,tmpreg2);
  1180. a_op_reg_reg(list,OP_OR,OS_16,tmpreg,tmpreg2);
  1181. a_load_reg_reg(list,fromsize,tosize,tmpreg2,register);
  1182. end;
  1183. OS_32,OS_S32:
  1184. if ref.alignment=2 then
  1185. begin
  1186. if target_info.endian=endian_little then
  1187. inc(tmpref.offset,2);
  1188. tmpreg:=getintregister(list,OS_32);
  1189. a_load_ref_reg(list,OS_16,OS_32,tmpref,tmpreg);
  1190. a_op_const_reg(list,OP_SHL,OS_32,16,tmpreg);
  1191. if target_info.endian=endian_little then
  1192. dec(tmpref.offset,2)
  1193. else
  1194. inc(tmpref.offset,2);
  1195. tmpreg2:=makeregsize(list,register,OS_32);
  1196. a_load_ref_reg(list,OS_16,OS_32,tmpref,tmpreg2);
  1197. a_op_reg_reg(list,OP_OR,OS_32,tmpreg,tmpreg2);
  1198. a_load_reg_reg(list,fromsize,tosize,tmpreg2,register);
  1199. end
  1200. else
  1201. begin
  1202. if target_info.endian=endian_little then
  1203. inc(tmpref.offset,3);
  1204. tmpreg:=getintregister(list,OS_32);
  1205. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg);
  1206. tmpreg2:=getintregister(list,OS_32);
  1207. for i:=1 to 3 do
  1208. begin
  1209. a_op_const_reg(list,OP_SHL,OS_32,8,tmpreg);
  1210. if target_info.endian=endian_little then
  1211. dec(tmpref.offset)
  1212. else
  1213. inc(tmpref.offset);
  1214. a_load_ref_reg(list,OS_8,OS_32,tmpref,tmpreg2);
  1215. a_op_reg_reg(list,OP_OR,OS_32,tmpreg2,tmpreg);
  1216. end;
  1217. a_load_reg_reg(list,fromsize,tosize,tmpreg,register);
  1218. end
  1219. else
  1220. a_load_ref_reg(list,fromsize,tosize,tmpref,register);
  1221. end;
  1222. end
  1223. else
  1224. a_load_ref_reg(list,fromsize,tosize,ref,register);
  1225. end;
  1226. procedure tcg.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  1227. var
  1228. tmpreg: tregister;
  1229. begin
  1230. { verify if we have the same reference }
  1231. if references_equal(sref,dref) then
  1232. exit;
  1233. tmpreg:=getintregister(list,tosize);
  1234. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  1235. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  1236. end;
  1237. procedure tcg.a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : treference);
  1238. var
  1239. tmpreg: tregister;
  1240. begin
  1241. tmpreg:=getintregister(list,size);
  1242. a_load_const_reg(list,size,a,tmpreg);
  1243. a_load_reg_ref(list,size,size,tmpreg,ref);
  1244. end;
  1245. procedure tcg.a_load_const_loc(list : TAsmList;a : tcgint;const loc: tlocation);
  1246. begin
  1247. case loc.loc of
  1248. LOC_REFERENCE,LOC_CREFERENCE:
  1249. a_load_const_ref(list,loc.size,a,loc.reference);
  1250. LOC_REGISTER,LOC_CREGISTER:
  1251. a_load_const_reg(list,loc.size,a,loc.register);
  1252. else
  1253. internalerror(200203272);
  1254. end;
  1255. end;
  1256. procedure tcg.a_load_reg_loc(list : TAsmList;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  1257. begin
  1258. case loc.loc of
  1259. LOC_REFERENCE,LOC_CREFERENCE:
  1260. a_load_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  1261. LOC_REGISTER,LOC_CREGISTER:
  1262. a_load_reg_reg(list,fromsize,loc.size,reg,loc.register);
  1263. LOC_MMREGISTER,LOC_CMMREGISTER:
  1264. a_loadmm_intreg_reg(list,fromsize,loc.size,reg,loc.register,mms_movescalar);
  1265. else
  1266. internalerror(200203271);
  1267. end;
  1268. end;
  1269. procedure tcg.a_load_loc_reg(list : TAsmList; tosize: tcgsize; const loc: tlocation; reg : tregister);
  1270. begin
  1271. case loc.loc of
  1272. LOC_REFERENCE,LOC_CREFERENCE:
  1273. a_load_ref_reg(list,loc.size,tosize,loc.reference,reg);
  1274. LOC_REGISTER,LOC_CREGISTER:
  1275. a_load_reg_reg(list,loc.size,tosize,loc.register,reg);
  1276. LOC_CONSTANT:
  1277. a_load_const_reg(list,tosize,loc.value,reg);
  1278. else
  1279. internalerror(200109092);
  1280. end;
  1281. end;
  1282. procedure tcg.a_load_loc_ref(list : TAsmList;tosize: tcgsize; const loc: tlocation; const ref : treference);
  1283. begin
  1284. case loc.loc of
  1285. LOC_REFERENCE,LOC_CREFERENCE:
  1286. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  1287. LOC_REGISTER,LOC_CREGISTER:
  1288. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  1289. LOC_CONSTANT:
  1290. a_load_const_ref(list,tosize,loc.value,ref);
  1291. else
  1292. internalerror(200109302);
  1293. end;
  1294. end;
  1295. procedure tcg.optimize_op_const(size: TCGSize; var op: topcg; var a : tcgint);
  1296. var
  1297. powerval : longint;
  1298. signext_a, zeroext_a: tcgint;
  1299. begin
  1300. case size of
  1301. OS_64,OS_S64:
  1302. begin
  1303. signext_a:=int64(a);
  1304. zeroext_a:=int64(a);
  1305. end;
  1306. OS_32,OS_S32:
  1307. begin
  1308. signext_a:=longint(a);
  1309. zeroext_a:=dword(a);
  1310. end;
  1311. OS_16,OS_S16:
  1312. begin
  1313. signext_a:=smallint(a);
  1314. zeroext_a:=word(a);
  1315. end;
  1316. OS_8,OS_S8:
  1317. begin
  1318. signext_a:=shortint(a);
  1319. zeroext_a:=byte(a);
  1320. end
  1321. else
  1322. begin
  1323. { Should we internalerror() here instead? }
  1324. signext_a:=a;
  1325. zeroext_a:=a;
  1326. end;
  1327. end;
  1328. case op of
  1329. OP_OR :
  1330. begin
  1331. { or with zero returns same result }
  1332. if a = 0 then
  1333. op:=OP_NONE
  1334. else
  1335. { or with max returns max }
  1336. if signext_a = -1 then
  1337. op:=OP_MOVE;
  1338. end;
  1339. OP_AND :
  1340. begin
  1341. { and with max returns same result }
  1342. if (signext_a = -1) then
  1343. op:=OP_NONE
  1344. else
  1345. { and with 0 returns 0 }
  1346. if a=0 then
  1347. op:=OP_MOVE;
  1348. end;
  1349. OP_DIV :
  1350. begin
  1351. { division by 1 returns result }
  1352. if a = 1 then
  1353. op:=OP_NONE
  1354. else if ispowerof2(int64(zeroext_a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  1355. begin
  1356. a := powerval;
  1357. op:= OP_SHR;
  1358. end;
  1359. end;
  1360. OP_IDIV:
  1361. begin
  1362. if a = 1 then
  1363. op:=OP_NONE;
  1364. end;
  1365. OP_MUL,OP_IMUL:
  1366. begin
  1367. if a = 1 then
  1368. op:=OP_NONE
  1369. else
  1370. if a=0 then
  1371. op:=OP_MOVE
  1372. else if ispowerof2(int64(zeroext_a), powerval) and not(cs_check_overflow in current_settings.localswitches) then
  1373. begin
  1374. a := powerval;
  1375. op:= OP_SHL;
  1376. end;
  1377. end;
  1378. OP_ADD,OP_SUB:
  1379. begin
  1380. if a = 0 then
  1381. op:=OP_NONE;
  1382. end;
  1383. OP_SAR,OP_SHL,OP_SHR,OP_ROL,OP_ROR:
  1384. begin
  1385. if a = 0 then
  1386. op:=OP_NONE;
  1387. end;
  1388. end;
  1389. end;
  1390. procedure tcg.a_loadfpu_loc_reg(list: TAsmList; tosize: tcgsize; const loc: tlocation; const reg: tregister);
  1391. begin
  1392. case loc.loc of
  1393. LOC_REFERENCE, LOC_CREFERENCE:
  1394. a_loadfpu_ref_reg(list,loc.size,tosize,loc.reference,reg);
  1395. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1396. a_loadfpu_reg_reg(list,loc.size,tosize,loc.register,reg);
  1397. else
  1398. internalerror(200203301);
  1399. end;
  1400. end;
  1401. procedure tcg.a_loadfpu_reg_loc(list: TAsmList; fromsize: tcgsize; const reg: tregister; const loc: tlocation);
  1402. begin
  1403. case loc.loc of
  1404. LOC_REFERENCE, LOC_CREFERENCE:
  1405. a_loadfpu_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  1406. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1407. a_loadfpu_reg_reg(list,fromsize,loc.size,reg,loc.register);
  1408. else
  1409. internalerror(48991);
  1410. end;
  1411. end;
  1412. procedure tcg.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tcgsize; const ref1,ref2: treference);
  1413. var
  1414. reg: tregister;
  1415. regsize: tcgsize;
  1416. begin
  1417. if (fromsize>=tosize) then
  1418. regsize:=fromsize
  1419. else
  1420. regsize:=tosize;
  1421. reg:=getfpuregister(list,regsize);
  1422. a_loadfpu_ref_reg(list,fromsize,regsize,ref1,reg);
  1423. a_loadfpu_reg_ref(list,regsize,tosize,reg,ref2);
  1424. end;
  1425. procedure tcg.a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const cgpara : TCGPara);
  1426. var
  1427. ref : treference;
  1428. begin
  1429. paramanager.alloccgpara(list,cgpara);
  1430. case cgpara.location^.loc of
  1431. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1432. begin
  1433. cgpara.check_simple_location;
  1434. a_loadfpu_reg_reg(list,size,size,r,cgpara.location^.register);
  1435. end;
  1436. LOC_REFERENCE,LOC_CREFERENCE:
  1437. begin
  1438. cgpara.check_simple_location;
  1439. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1440. a_loadfpu_reg_ref(list,size,size,r,ref);
  1441. end;
  1442. LOC_REGISTER,LOC_CREGISTER:
  1443. begin
  1444. { paramfpu_ref does the check_simpe_location check here if necessary }
  1445. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,ref);
  1446. a_loadfpu_reg_ref(list,size,size,r,ref);
  1447. a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  1448. tg.Ungettemp(list,ref);
  1449. end;
  1450. else
  1451. internalerror(2010053112);
  1452. end;
  1453. end;
  1454. procedure tcg.a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara);
  1455. var
  1456. href : treference;
  1457. hsize: tcgsize;
  1458. begin
  1459. case cgpara.location^.loc of
  1460. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1461. begin
  1462. cgpara.check_simple_location;
  1463. paramanager.alloccgpara(list,cgpara);
  1464. a_loadfpu_ref_reg(list,size,size,ref,cgpara.location^.register);
  1465. end;
  1466. LOC_REFERENCE,LOC_CREFERENCE:
  1467. begin
  1468. cgpara.check_simple_location;
  1469. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1470. { concatcopy should choose the best way to copy the data }
  1471. g_concatcopy(list,ref,href,tcgsize2size[size]);
  1472. end;
  1473. LOC_REGISTER,LOC_CREGISTER:
  1474. begin
  1475. { force integer size }
  1476. hsize:=int_cgsize(tcgsize2size[size]);
  1477. {$ifndef cpu64bitalu}
  1478. if (hsize in [OS_S64,OS_64]) then
  1479. cg64.a_load64_ref_cgpara(list,ref,cgpara)
  1480. else
  1481. {$endif not cpu64bitalu}
  1482. begin
  1483. cgpara.check_simple_location;
  1484. a_load_ref_cgpara(list,hsize,ref,cgpara)
  1485. end;
  1486. end
  1487. else
  1488. internalerror(200402201);
  1489. end;
  1490. end;
  1491. procedure tcg.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1492. var
  1493. tmpreg : tregister;
  1494. begin
  1495. tmpreg:=getintregister(list,size);
  1496. a_load_ref_reg(list,size,size,ref,tmpreg);
  1497. a_op_const_reg(list,op,size,a,tmpreg);
  1498. a_load_reg_ref(list,size,size,tmpreg,ref);
  1499. end;
  1500. procedure tcg.a_op_const_loc(list : TAsmList; Op: TOpCG; a: tcgint; const loc: tlocation);
  1501. begin
  1502. case loc.loc of
  1503. LOC_REGISTER, LOC_CREGISTER:
  1504. a_op_const_reg(list,op,loc.size,a,loc.register);
  1505. LOC_REFERENCE, LOC_CREFERENCE:
  1506. a_op_const_ref(list,op,loc.size,a,loc.reference);
  1507. else
  1508. internalerror(200109061);
  1509. end;
  1510. end;
  1511. procedure tcg.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1512. var
  1513. tmpreg : tregister;
  1514. begin
  1515. tmpreg:=getintregister(list,size);
  1516. a_load_ref_reg(list,size,size,ref,tmpreg);
  1517. a_op_reg_reg(list,op,size,reg,tmpreg);
  1518. a_load_reg_ref(list,size,size,tmpreg,ref);
  1519. end;
  1520. procedure tcg.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1521. var
  1522. tmpreg: tregister;
  1523. begin
  1524. case op of
  1525. OP_NOT,OP_NEG:
  1526. { handle it as "load ref,reg; op reg" }
  1527. begin
  1528. a_load_ref_reg(list,size,size,ref,reg);
  1529. a_op_reg_reg(list,op,size,reg,reg);
  1530. end;
  1531. else
  1532. begin
  1533. tmpreg:=getintregister(list,size);
  1534. a_load_ref_reg(list,size,size,ref,tmpreg);
  1535. a_op_reg_reg(list,op,size,tmpreg,reg);
  1536. end;
  1537. end;
  1538. end;
  1539. procedure tcg.a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  1540. begin
  1541. case loc.loc of
  1542. LOC_REGISTER, LOC_CREGISTER:
  1543. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  1544. LOC_REFERENCE, LOC_CREFERENCE:
  1545. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  1546. else
  1547. internalerror(200109061);
  1548. end;
  1549. end;
  1550. procedure tcg.a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  1551. var
  1552. tmpreg: tregister;
  1553. begin
  1554. case loc.loc of
  1555. LOC_REGISTER,LOC_CREGISTER:
  1556. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  1557. LOC_REFERENCE,LOC_CREFERENCE:
  1558. begin
  1559. tmpreg:=getintregister(list,loc.size);
  1560. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  1561. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  1562. end;
  1563. else
  1564. internalerror(200109061);
  1565. end;
  1566. end;
  1567. procedure Tcg.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1568. a:tcgint;src,dst:Tregister);
  1569. begin
  1570. a_load_reg_reg(list,size,size,src,dst);
  1571. a_op_const_reg(list,op,size,a,dst);
  1572. end;
  1573. procedure tcg.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1574. size: tcgsize; src1, src2, dst: tregister);
  1575. var
  1576. tmpreg: tregister;
  1577. begin
  1578. if (dst<>src1) then
  1579. begin
  1580. a_load_reg_reg(list,size,size,src2,dst);
  1581. a_op_reg_reg(list,op,size,src1,dst);
  1582. end
  1583. else
  1584. begin
  1585. { can we do a direct operation on the target register ? }
  1586. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  1587. a_op_reg_reg(list,op,size,src2,dst)
  1588. else
  1589. begin
  1590. tmpreg:=getintregister(list,size);
  1591. a_load_reg_reg(list,size,size,src2,tmpreg);
  1592. a_op_reg_reg(list,op,size,src1,tmpreg);
  1593. a_load_reg_reg(list,size,size,tmpreg,dst);
  1594. end;
  1595. end;
  1596. end;
  1597. procedure tcg.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1598. begin
  1599. a_op_const_reg_reg(list,op,size,a,src,dst);
  1600. ovloc.loc:=LOC_VOID;
  1601. end;
  1602. procedure tcg.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1603. begin
  1604. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1605. ovloc.loc:=LOC_VOID;
  1606. end;
  1607. procedure tcg.a_cmp_const_reg_label(list: TAsmList; size: tcgsize;
  1608. cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1609. var
  1610. tmpreg: tregister;
  1611. begin
  1612. tmpreg:=getintregister(list,size);
  1613. a_load_const_reg(list,size,a,tmpreg);
  1614. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1615. end;
  1616. procedure tcg.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  1617. l : tasmlabel);
  1618. var
  1619. tmpreg: tregister;
  1620. begin
  1621. tmpreg:=getintregister(list,size);
  1622. a_load_ref_reg(list,size,size,ref,tmpreg);
  1623. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1624. end;
  1625. procedure tcg.a_cmp_const_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const loc : tlocation;
  1626. l : tasmlabel);
  1627. begin
  1628. case loc.loc of
  1629. LOC_REGISTER,LOC_CREGISTER:
  1630. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1631. LOC_REFERENCE,LOC_CREFERENCE:
  1632. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1633. else
  1634. internalerror(200109061);
  1635. end;
  1636. end;
  1637. procedure tcg.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  1638. var
  1639. tmpreg: tregister;
  1640. begin
  1641. tmpreg:=getintregister(list,size);
  1642. a_load_ref_reg(list,size,size,ref,tmpreg);
  1643. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1644. end;
  1645. procedure tcg.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg : tregister; const ref: treference; l : tasmlabel);
  1646. var
  1647. tmpreg: tregister;
  1648. begin
  1649. tmpreg:=getintregister(list,size);
  1650. a_load_ref_reg(list,size,size,ref,tmpreg);
  1651. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  1652. end;
  1653. procedure tcg.a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  1654. begin
  1655. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  1656. end;
  1657. procedure tcg.a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  1658. begin
  1659. case loc.loc of
  1660. LOC_REGISTER,
  1661. LOC_CREGISTER:
  1662. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1663. LOC_REFERENCE,
  1664. LOC_CREFERENCE :
  1665. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1666. LOC_CONSTANT:
  1667. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1668. else
  1669. internalerror(200203231);
  1670. end;
  1671. end;
  1672. procedure tcg.a_cmp_ref_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1673. l : tasmlabel);
  1674. var
  1675. tmpreg: tregister;
  1676. begin
  1677. case loc.loc of
  1678. LOC_REGISTER,LOC_CREGISTER:
  1679. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1680. LOC_REFERENCE,LOC_CREFERENCE:
  1681. begin
  1682. tmpreg:=getintregister(list,size);
  1683. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1684. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1685. end;
  1686. else
  1687. internalerror(200109061);
  1688. end;
  1689. end;
  1690. procedure tcg.a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  1691. begin
  1692. case loc.loc of
  1693. LOC_MMREGISTER,LOC_CMMREGISTER:
  1694. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1695. LOC_REFERENCE,LOC_CREFERENCE:
  1696. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  1697. LOC_REGISTER,LOC_CREGISTER:
  1698. a_loadmm_intreg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1699. else
  1700. internalerror(200310121);
  1701. end;
  1702. end;
  1703. procedure tcg.a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  1704. begin
  1705. case loc.loc of
  1706. LOC_MMREGISTER,LOC_CMMREGISTER:
  1707. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  1708. LOC_REFERENCE,LOC_CREFERENCE:
  1709. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  1710. else
  1711. internalerror(200310122);
  1712. end;
  1713. end;
  1714. procedure tcg.a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle);
  1715. var
  1716. href : treference;
  1717. {$ifndef cpu64bitalu}
  1718. tmpreg : tregister;
  1719. reg64 : tregister64;
  1720. {$endif not cpu64bitalu}
  1721. begin
  1722. {$ifndef cpu64bitalu}
  1723. if not(cgpara.location^.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  1724. (size<>OS_F64) then
  1725. {$endif not cpu64bitalu}
  1726. cgpara.check_simple_location;
  1727. paramanager.alloccgpara(list,cgpara);
  1728. case cgpara.location^.loc of
  1729. LOC_MMREGISTER,LOC_CMMREGISTER:
  1730. a_loadmm_reg_reg(list,size,cgpara.location^.size,reg,cgpara.location^.register,shuffle);
  1731. LOC_REFERENCE,LOC_CREFERENCE:
  1732. begin
  1733. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment);
  1734. a_loadmm_reg_ref(list,size,cgpara.location^.size,reg,href,shuffle);
  1735. end;
  1736. LOC_REGISTER,LOC_CREGISTER:
  1737. begin
  1738. if assigned(shuffle) and
  1739. not shufflescalar(shuffle) then
  1740. internalerror(2009112510);
  1741. {$ifndef cpu64bitalu}
  1742. if (size=OS_F64) then
  1743. begin
  1744. if not assigned(cgpara.location^.next) or
  1745. assigned(cgpara.location^.next^.next) then
  1746. internalerror(2009112512);
  1747. case cgpara.location^.next^.loc of
  1748. LOC_REGISTER,LOC_CREGISTER:
  1749. tmpreg:=cgpara.location^.next^.register;
  1750. LOC_REFERENCE,LOC_CREFERENCE:
  1751. tmpreg:=getintregister(list,OS_32);
  1752. else
  1753. internalerror(2009112910);
  1754. end;
  1755. if (target_info.endian=ENDIAN_BIG) then
  1756. begin
  1757. { paraloc^ -> high
  1758. paraloc^.next -> low }
  1759. reg64.reghi:=cgpara.location^.register;
  1760. reg64.reglo:=tmpreg;
  1761. end
  1762. else
  1763. begin
  1764. { paraloc^ -> low
  1765. paraloc^.next -> high }
  1766. reg64.reglo:=cgpara.location^.register;
  1767. reg64.reghi:=tmpreg;
  1768. end;
  1769. cg64.a_loadmm_reg_intreg64(list,size,reg,reg64);
  1770. if (cgpara.location^.next^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1771. begin
  1772. if not(cgpara.location^.next^.size in [OS_32,OS_S32]) then
  1773. internalerror(2009112911);
  1774. reference_reset_base(href,cgpara.location^.next^.reference.index,cgpara.location^.next^.reference.offset,cgpara.alignment);
  1775. a_load_reg_ref(list,OS_32,cgpara.location^.next^.size,tmpreg,href);
  1776. end;
  1777. end
  1778. else
  1779. {$endif not cpu64bitalu}
  1780. a_loadmm_reg_intreg(list,size,cgpara.location^.size,reg,cgpara.location^.register,mms_movescalar);
  1781. end
  1782. else
  1783. internalerror(200310123);
  1784. end;
  1785. end;
  1786. procedure tcg.a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize;const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle);
  1787. var
  1788. hr : tregister;
  1789. hs : tmmshuffle;
  1790. begin
  1791. cgpara.check_simple_location;
  1792. hr:=getmmregister(list,cgpara.location^.size);
  1793. a_loadmm_ref_reg(list,size,cgpara.location^.size,ref,hr,shuffle);
  1794. if realshuffle(shuffle) then
  1795. begin
  1796. hs:=shuffle^;
  1797. removeshuffles(hs);
  1798. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,@hs);
  1799. end
  1800. else
  1801. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,shuffle);
  1802. end;
  1803. procedure tcg.a_loadmm_loc_cgpara(list: TAsmList;const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle);
  1804. begin
  1805. case loc.loc of
  1806. LOC_MMREGISTER,LOC_CMMREGISTER:
  1807. a_loadmm_reg_cgpara(list,loc.size,loc.register,cgpara,shuffle);
  1808. LOC_REFERENCE,LOC_CREFERENCE:
  1809. a_loadmm_ref_cgpara(list,loc.size,loc.reference,cgpara,shuffle);
  1810. else
  1811. internalerror(200310123);
  1812. end;
  1813. end;
  1814. procedure tcg.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1815. var
  1816. hr : tregister;
  1817. hs : tmmshuffle;
  1818. begin
  1819. hr:=getmmregister(list,size);
  1820. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1821. if realshuffle(shuffle) then
  1822. begin
  1823. hs:=shuffle^;
  1824. removeshuffles(hs);
  1825. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  1826. end
  1827. else
  1828. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  1829. end;
  1830. procedure tcg.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  1831. var
  1832. hr : tregister;
  1833. hs : tmmshuffle;
  1834. begin
  1835. hr:=getmmregister(list,size);
  1836. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1837. if realshuffle(shuffle) then
  1838. begin
  1839. hs:=shuffle^;
  1840. removeshuffles(hs);
  1841. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  1842. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  1843. end
  1844. else
  1845. begin
  1846. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  1847. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  1848. end;
  1849. end;
  1850. procedure tcg.a_loadmm_intreg_reg(list: tasmlist; fromsize,tosize: tcgsize; intreg,mmreg: tregister; shuffle: pmmshuffle);
  1851. var
  1852. tmpref: treference;
  1853. begin
  1854. if (tcgsize2size[fromsize]<>4) or
  1855. (tcgsize2size[tosize]<>4) then
  1856. internalerror(2009112503);
  1857. tg.gettemp(list,4,4,tt_normal,tmpref);
  1858. a_load_reg_ref(list,fromsize,fromsize,intreg,tmpref);
  1859. a_loadmm_ref_reg(list,tosize,tosize,tmpref,mmreg,shuffle);
  1860. tg.ungettemp(list,tmpref);
  1861. end;
  1862. procedure tcg.a_loadmm_reg_intreg(list: tasmlist; fromsize,tosize: tcgsize; mmreg,intreg: tregister; shuffle: pmmshuffle);
  1863. var
  1864. tmpref: treference;
  1865. begin
  1866. if (tcgsize2size[fromsize]<>4) or
  1867. (tcgsize2size[tosize]<>4) then
  1868. internalerror(2009112504);
  1869. tg.gettemp(list,8,8,tt_normal,tmpref);
  1870. a_loadmm_reg_ref(list,fromsize,fromsize,mmreg,tmpref,shuffle);
  1871. a_load_ref_reg(list,tosize,tosize,tmpref,intreg);
  1872. tg.ungettemp(list,tmpref);
  1873. end;
  1874. procedure tcg.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  1875. begin
  1876. case loc.loc of
  1877. LOC_CMMREGISTER,LOC_MMREGISTER:
  1878. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  1879. LOC_CREFERENCE,LOC_REFERENCE:
  1880. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  1881. else
  1882. internalerror(200312232);
  1883. end;
  1884. end;
  1885. procedure tcg.a_opmm_loc_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; src,dst: tregister;shuffle : pmmshuffle);
  1886. begin
  1887. case loc.loc of
  1888. LOC_CMMREGISTER,LOC_MMREGISTER:
  1889. a_opmm_reg_reg_reg(list,op,size,loc.register,src,dst,shuffle);
  1890. LOC_CREFERENCE,LOC_REFERENCE:
  1891. a_opmm_ref_reg_reg(list,op,size,loc.reference,src,dst,shuffle);
  1892. else
  1893. internalerror(200312232);
  1894. end;
  1895. end;
  1896. procedure tcg.a_opmm_reg_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;
  1897. src1,src2,dst : tregister;shuffle : pmmshuffle);
  1898. begin
  1899. internalerror(2013061102);
  1900. end;
  1901. procedure tcg.a_opmm_ref_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;
  1902. const ref : treference;src,dst : tregister;shuffle : pmmshuffle);
  1903. begin
  1904. internalerror(2013061101);
  1905. end;
  1906. procedure tcg.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  1907. begin
  1908. g_concatcopy(list,source,dest,len);
  1909. end;
  1910. procedure tcg.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  1911. begin
  1912. g_overflowCheck(list,loc,def);
  1913. end;
  1914. {$ifdef cpuflags}
  1915. procedure tcg.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference);
  1916. var
  1917. tmpreg : tregister;
  1918. begin
  1919. tmpreg:=getintregister(list,size);
  1920. g_flags2reg(list,size,f,tmpreg);
  1921. a_load_reg_ref(list,size,size,tmpreg,ref);
  1922. end;
  1923. {$endif cpuflags}
  1924. procedure tcg.g_maybe_testvmt(list : TAsmList;reg:tregister;objdef:tobjectdef);
  1925. var
  1926. hrefvmt : treference;
  1927. cgpara1,cgpara2 : TCGPara;
  1928. pd: tprocdef;
  1929. begin
  1930. cgpara1.init;
  1931. cgpara2.init;
  1932. if (cs_check_object in current_settings.localswitches) then
  1933. begin
  1934. pd:=search_system_proc('fpc_check_object_ext');
  1935. paramanager.getintparaloc(pd,1,cgpara1);
  1936. paramanager.getintparaloc(pd,2,cgpara2);
  1937. reference_reset_symbol(hrefvmt,current_asmdata.RefAsmSymbol(objdef.vmt_mangledname,AT_DATA),0,sizeof(pint));
  1938. if pd.is_pushleftright then
  1939. begin
  1940. a_load_reg_cgpara(list,OS_ADDR,reg,cgpara1);
  1941. a_loadaddr_ref_cgpara(list,hrefvmt,cgpara2);
  1942. end
  1943. else
  1944. begin
  1945. a_loadaddr_ref_cgpara(list,hrefvmt,cgpara2);
  1946. a_load_reg_cgpara(list,OS_ADDR,reg,cgpara1);
  1947. end;
  1948. paramanager.freecgpara(list,cgpara1);
  1949. paramanager.freecgpara(list,cgpara2);
  1950. allocallcpuregisters(list);
  1951. a_call_name(list,'fpc_check_object_ext',false);
  1952. deallocallcpuregisters(list);
  1953. end
  1954. else
  1955. if (cs_check_range in current_settings.localswitches) then
  1956. begin
  1957. pd:=search_system_proc('fpc_check_object');
  1958. paramanager.getintparaloc(pd,1,cgpara1);
  1959. a_load_reg_cgpara(list,OS_ADDR,reg,cgpara1);
  1960. paramanager.freecgpara(list,cgpara1);
  1961. allocallcpuregisters(list);
  1962. a_call_name(list,'fpc_check_object',false);
  1963. deallocallcpuregisters(list);
  1964. end;
  1965. cgpara1.done;
  1966. cgpara2.done;
  1967. end;
  1968. {*****************************************************************************
  1969. Entry/Exit Code Functions
  1970. *****************************************************************************}
  1971. procedure tcg.g_save_registers(list:TAsmList);
  1972. var
  1973. href : treference;
  1974. size : longint;
  1975. r : integer;
  1976. begin
  1977. { calculate temp. size }
  1978. size:=0;
  1979. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1980. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1981. inc(size,sizeof(aint));
  1982. if uses_registers(R_ADDRESSREGISTER) then
  1983. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1984. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1985. inc(size,sizeof(aint));
  1986. { mm registers }
  1987. if uses_registers(R_MMREGISTER) then
  1988. begin
  1989. { Make sure we reserve enough space to do the alignment based on the offset
  1990. later on. We can't use the size for this, because the alignment of the start
  1991. of the temp is smaller than needed for an OS_VECTOR }
  1992. inc(size,tcgsize2size[OS_VECTOR]);
  1993. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  1994. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  1995. inc(size,tcgsize2size[OS_VECTOR]);
  1996. end;
  1997. if size>0 then
  1998. begin
  1999. tg.GetTemp(list,size,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  2000. include(current_procinfo.flags,pi_has_saved_regs);
  2001. { Copy registers to temp }
  2002. href:=current_procinfo.save_regs_ref;
  2003. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  2004. begin
  2005. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2006. begin
  2007. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE),href);
  2008. inc(href.offset,sizeof(aint));
  2009. end;
  2010. include(rg[R_INTREGISTER].preserved_by_proc,saved_standard_registers[r]);
  2011. end;
  2012. if uses_registers(R_ADDRESSREGISTER) then
  2013. for r:=low(saved_address_registers) to high(saved_address_registers) do
  2014. begin
  2015. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2016. begin
  2017. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE),href);
  2018. inc(href.offset,sizeof(aint));
  2019. end;
  2020. include(rg[R_ADDRESSREGISTER].preserved_by_proc,saved_address_registers[r]);
  2021. end;
  2022. if uses_registers(R_MMREGISTER) then
  2023. begin
  2024. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  2025. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  2026. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  2027. begin
  2028. { the array has to be declared even if no MM registers are saved
  2029. (such as with SSE on i386), and since 0-element arrays don't
  2030. exist, they contain a single RS_INVALID element in that case
  2031. }
  2032. if saved_mm_registers[r]<>RS_INVALID then
  2033. begin
  2034. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  2035. begin
  2036. a_loadmm_reg_ref(list,OS_VECTOR,OS_VECTOR,newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBMMWHOLE),href,nil);
  2037. inc(href.offset,tcgsize2size[OS_VECTOR]);
  2038. end;
  2039. include(rg[R_MMREGISTER].preserved_by_proc,saved_mm_registers[r]);
  2040. end;
  2041. end;
  2042. end;
  2043. end;
  2044. end;
  2045. procedure tcg.g_restore_registers(list:TAsmList);
  2046. var
  2047. href : treference;
  2048. r : integer;
  2049. hreg : tregister;
  2050. begin
  2051. if not(pi_has_saved_regs in current_procinfo.flags) then
  2052. exit;
  2053. { Copy registers from temp }
  2054. href:=current_procinfo.save_regs_ref;
  2055. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  2056. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2057. begin
  2058. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  2059. { Allocate register so the optimizer does not remove the load }
  2060. a_reg_alloc(list,hreg);
  2061. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2062. inc(href.offset,sizeof(aint));
  2063. end;
  2064. if uses_registers(R_ADDRESSREGISTER) then
  2065. for r:=low(saved_address_registers) to high(saved_address_registers) do
  2066. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2067. begin
  2068. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  2069. { Allocate register so the optimizer does not remove the load }
  2070. a_reg_alloc(list,hreg);
  2071. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2072. inc(href.offset,sizeof(aint));
  2073. end;
  2074. if uses_registers(R_MMREGISTER) then
  2075. begin
  2076. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  2077. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  2078. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  2079. begin
  2080. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  2081. begin
  2082. hreg:=newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBMMWHOLE);
  2083. { Allocate register so the optimizer does not remove the load }
  2084. a_reg_alloc(list,hreg);
  2085. a_loadmm_ref_reg(list,OS_VECTOR,OS_VECTOR,href,hreg,nil);
  2086. inc(href.offset,tcgsize2size[OS_VECTOR]);
  2087. end;
  2088. end;
  2089. end;
  2090. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  2091. end;
  2092. procedure tcg.g_profilecode(list : TAsmList);
  2093. begin
  2094. end;
  2095. procedure tcg.g_exception_reason_save(list : TAsmList; const href : treference);
  2096. begin
  2097. a_load_reg_ref(list, OS_INT, OS_INT, NR_FUNCTION_RESULT_REG, href);
  2098. end;
  2099. procedure tcg.g_exception_reason_save_const(list : TAsmList; const href : treference; a: tcgint);
  2100. begin
  2101. a_load_const_ref(list, OS_INT, a, href);
  2102. end;
  2103. procedure tcg.g_exception_reason_load(list : TAsmList; const href : treference);
  2104. begin
  2105. a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  2106. a_load_ref_reg(list, OS_INT, OS_INT, href, NR_FUNCTION_RESULT_REG);
  2107. end;
  2108. procedure tcg.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  2109. var
  2110. hsym : tsym;
  2111. href : treference;
  2112. paraloc : Pcgparalocation;
  2113. begin
  2114. { calculate the parameter info for the procdef }
  2115. procdef.init_paraloc_info(callerside);
  2116. hsym:=tsym(procdef.parast.Find('self'));
  2117. if not(assigned(hsym) and
  2118. (hsym.typ=paravarsym)) then
  2119. internalerror(200305251);
  2120. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  2121. while paraloc<>nil do
  2122. with paraloc^ do
  2123. begin
  2124. case loc of
  2125. LOC_REGISTER:
  2126. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  2127. LOC_REFERENCE:
  2128. begin
  2129. { offset in the wrapper needs to be adjusted for the stored
  2130. return address }
  2131. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),sizeof(pint));
  2132. a_op_const_ref(list,OP_SUB,size,ioffset,href);
  2133. end
  2134. else
  2135. internalerror(200309189);
  2136. end;
  2137. paraloc:=next;
  2138. end;
  2139. end;
  2140. procedure tcg.g_external_wrapper(list : TAsmList; procdef: tprocdef; const externalname: string);
  2141. begin
  2142. a_jmp_name(list,externalname);
  2143. end;
  2144. procedure tcg.a_call_name_static(list : TAsmList;const s : string);
  2145. begin
  2146. a_call_name(list,s,false);
  2147. end;
  2148. function tcg.g_indirect_sym_load(list:TAsmList;const symname: string; const flags: tindsymflags): tregister;
  2149. var
  2150. l: tasmsymbol;
  2151. ref: treference;
  2152. nlsymname: string;
  2153. begin
  2154. result := NR_NO;
  2155. case target_info.system of
  2156. system_powerpc_darwin,
  2157. system_i386_darwin,
  2158. system_i386_iphonesim,
  2159. system_powerpc64_darwin,
  2160. system_arm_darwin:
  2161. begin
  2162. nlsymname:='L'+symname+'$non_lazy_ptr';
  2163. l:=current_asmdata.getasmsymbol(nlsymname);
  2164. if not(assigned(l)) then
  2165. begin
  2166. new_section(current_asmdata.asmlists[al_picdata],sec_data_nonlazy,'',sizeof(pint));
  2167. l:=current_asmdata.DefineAsmSymbol(nlsymname,AB_LOCAL,AT_DATA);
  2168. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create(l,0));
  2169. if not(is_weak in flags) then
  2170. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.RefAsmSymbol(symname).Name))
  2171. else
  2172. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.WeakRefAsmSymbol(symname).Name));
  2173. {$ifdef cpu64bitaddr}
  2174. current_asmdata.asmlists[al_picdata].concat(tai_const.create_64bit(0));
  2175. {$else cpu64bitaddr}
  2176. current_asmdata.asmlists[al_picdata].concat(tai_const.create_32bit(0));
  2177. {$endif cpu64bitaddr}
  2178. end;
  2179. result := getaddressregister(list);
  2180. reference_reset_symbol(ref,l,0,sizeof(pint));
  2181. { a_load_ref_reg will turn this into a pic-load if needed }
  2182. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,result);
  2183. end;
  2184. end;
  2185. end;
  2186. procedure tcg.g_maybe_got_init(list: TAsmList);
  2187. begin
  2188. end;
  2189. procedure tcg.g_call(list: TAsmList;const s: string);
  2190. begin
  2191. allocallcpuregisters(list);
  2192. a_call_name(list,s,false);
  2193. deallocallcpuregisters(list);
  2194. end;
  2195. procedure tcg.g_local_unwind(list: TAsmList; l: TAsmLabel);
  2196. begin
  2197. a_jmp_always(list,l);
  2198. end;
  2199. procedure tcg.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  2200. begin
  2201. internalerror(200807231);
  2202. end;
  2203. procedure tcg.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2204. begin
  2205. internalerror(200807232);
  2206. end;
  2207. procedure tcg.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2208. begin
  2209. internalerror(200807233);
  2210. end;
  2211. procedure tcg.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  2212. begin
  2213. internalerror(200807234);
  2214. end;
  2215. function tcg.getflagregister(list: TAsmList; size: Tcgsize): Tregister;
  2216. begin
  2217. Result:=TRegister(0);
  2218. internalerror(200807238);
  2219. end;
  2220. procedure tcg.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tcgsize; src, dst: TRegister);
  2221. begin
  2222. internalerror(2014070601);
  2223. end;
  2224. procedure tcg.g_stackpointer_alloc(list: TAsmList; size: longint);
  2225. begin
  2226. internalerror(2014070602);
  2227. end;
  2228. procedure tcg.a_mul_reg_reg_pair(list: TAsmList; size: TCgSize; src1,src2,dstlo,dsthi: TRegister);
  2229. begin
  2230. internalerror(2014060801);
  2231. end;
  2232. procedure tcg.g_div_const_reg_reg(list:tasmlist; size: TCgSize; a: tcgint; src,dst: tregister);
  2233. var
  2234. divreg: tregister;
  2235. magic: aInt;
  2236. u_magic: aWord;
  2237. u_shift: byte;
  2238. u_add: boolean;
  2239. begin
  2240. divreg:=getintregister(list,OS_INT);
  2241. if (size in [OS_S32,OS_S64]) then
  2242. begin
  2243. calc_divconst_magic_signed(tcgsize2size[size]*8,a,magic,u_shift);
  2244. { load magic value }
  2245. a_load_const_reg(list,OS_INT,magic,divreg);
  2246. { multiply, discarding low bits }
  2247. a_mul_reg_reg_pair(list,size,src,divreg,NR_NO,dst);
  2248. { add/subtract numerator }
  2249. if (a>0) and (magic<0) then
  2250. a_op_reg_reg_reg(list,OP_ADD,OS_INT,src,dst,dst)
  2251. else if (a<0) and (magic>0) then
  2252. a_op_reg_reg_reg(list,OP_SUB,OS_INT,src,dst,dst);
  2253. { shift shift places to the right (arithmetic) }
  2254. a_op_const_reg_reg(list,OP_SAR,OS_INT,u_shift,dst,dst);
  2255. { extract and add sign bit }
  2256. if (a>=0) then
  2257. a_op_const_reg_reg(list,OP_SHR,OS_INT,tcgsize2size[size]*8-1,src,divreg)
  2258. else
  2259. a_op_const_reg_reg(list,OP_SHR,OS_INT,tcgsize2size[size]*8-1,dst,divreg);
  2260. a_op_reg_reg_reg(list,OP_ADD,OS_INT,dst,divreg,dst);
  2261. end
  2262. else if (size in [OS_32,OS_64]) then
  2263. begin
  2264. calc_divconst_magic_unsigned(tcgsize2size[size]*8,a,u_magic,u_add,u_shift);
  2265. { load magic in divreg }
  2266. a_load_const_reg(list,OS_INT,tcgint(u_magic),divreg);
  2267. { multiply, discarding low bits }
  2268. a_mul_reg_reg_pair(list,size,src,divreg,NR_NO,dst);
  2269. if (u_add) then
  2270. begin
  2271. { Calculate "(numerator+result) shr u_shift", avoiding possible overflow }
  2272. a_op_reg_reg_reg(list,OP_SUB,OS_INT,dst,src,divreg);
  2273. { divreg=(numerator-result) }
  2274. a_op_const_reg_reg(list,OP_SHR,OS_INT,1,divreg,divreg);
  2275. { divreg=(numerator-result)/2 }
  2276. a_op_reg_reg_reg(list,OP_ADD,OS_INT,divreg,dst,divreg);
  2277. { divreg=(numerator+result)/2, already shifted by 1, so decrease u_shift. }
  2278. a_op_const_reg_reg(list,OP_SHR,OS_INT,u_shift-1,divreg,dst);
  2279. end
  2280. else
  2281. a_op_const_reg_reg(list,OP_SHR,OS_INT,u_shift,dst,dst);
  2282. end
  2283. else
  2284. InternalError(2014060601);
  2285. end;
  2286. {*****************************************************************************
  2287. TCG64
  2288. *****************************************************************************}
  2289. {$ifndef cpu64bitalu}
  2290. function joinreg64(reglo,reghi : tregister) : tregister64;
  2291. begin
  2292. result.reglo:=reglo;
  2293. result.reghi:=reghi;
  2294. end;
  2295. procedure tcg64.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64; regsrc,regdst : tregister64);
  2296. begin
  2297. a_load64_reg_reg(list,regsrc,regdst);
  2298. a_op64_const_reg(list,op,size,value,regdst);
  2299. end;
  2300. procedure tcg64.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  2301. var
  2302. tmpreg64 : tregister64;
  2303. begin
  2304. { when src1=dst then we need to first create a temp to prevent
  2305. overwriting src1 with src2 }
  2306. if (regsrc1.reghi=regdst.reghi) or
  2307. (regsrc1.reglo=regdst.reghi) or
  2308. (regsrc1.reghi=regdst.reglo) or
  2309. (regsrc1.reglo=regdst.reglo) then
  2310. begin
  2311. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2312. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2313. a_load64_reg_reg(list,regsrc2,tmpreg64);
  2314. a_op64_reg_reg(list,op,size,regsrc1,tmpreg64);
  2315. a_load64_reg_reg(list,tmpreg64,regdst);
  2316. end
  2317. else
  2318. begin
  2319. a_load64_reg_reg(list,regsrc2,regdst);
  2320. a_op64_reg_reg(list,op,size,regsrc1,regdst);
  2321. end;
  2322. end;
  2323. procedure tcg64.a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  2324. var
  2325. tmpreg64 : tregister64;
  2326. begin
  2327. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2328. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2329. a_load64_subsetref_reg(list,sref,tmpreg64);
  2330. a_op64_const_reg(list,op,size,a,tmpreg64);
  2331. a_load64_reg_subsetref(list,tmpreg64,sref);
  2332. end;
  2333. procedure tcg64.a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  2334. var
  2335. tmpreg64 : tregister64;
  2336. begin
  2337. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2338. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2339. a_load64_subsetref_reg(list,sref,tmpreg64);
  2340. a_op64_reg_reg(list,op,size,reg,tmpreg64);
  2341. a_load64_reg_subsetref(list,tmpreg64,sref);
  2342. end;
  2343. procedure tcg64.a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  2344. var
  2345. tmpreg64 : tregister64;
  2346. begin
  2347. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2348. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2349. a_load64_subsetref_reg(list,sref,tmpreg64);
  2350. a_op64_ref_reg(list,op,size,ref,tmpreg64);
  2351. a_load64_reg_subsetref(list,tmpreg64,sref);
  2352. end;
  2353. procedure tcg64.a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  2354. var
  2355. tmpreg64 : tregister64;
  2356. begin
  2357. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2358. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2359. a_load64_subsetref_reg(list,ssref,tmpreg64);
  2360. a_op64_reg_subsetref(list,op,size,tmpreg64,dsref);
  2361. end;
  2362. procedure tcg64.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2363. begin
  2364. a_op64_const_reg_reg(list,op,size,value,regsrc,regdst);
  2365. ovloc.loc:=LOC_VOID;
  2366. end;
  2367. procedure tcg64.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2368. begin
  2369. a_op64_reg_reg_reg(list,op,size,regsrc1,regsrc2,regdst);
  2370. ovloc.loc:=LOC_VOID;
  2371. end;
  2372. procedure tcg64.a_load64_loc_subsetref(list : TAsmList;const l: tlocation; const sref : tsubsetreference);
  2373. begin
  2374. case l.loc of
  2375. LOC_REFERENCE, LOC_CREFERENCE:
  2376. a_load64_ref_subsetref(list,l.reference,sref);
  2377. LOC_REGISTER,LOC_CREGISTER:
  2378. a_load64_reg_subsetref(list,l.register64,sref);
  2379. LOC_CONSTANT :
  2380. a_load64_const_subsetref(list,l.value64,sref);
  2381. LOC_SUBSETREF,LOC_CSUBSETREF:
  2382. a_load64_subsetref_subsetref(list,l.sref,sref);
  2383. else
  2384. internalerror(2006082210);
  2385. end;
  2386. end;
  2387. procedure tcg64.a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  2388. begin
  2389. case l.loc of
  2390. LOC_REFERENCE, LOC_CREFERENCE:
  2391. a_load64_subsetref_ref(list,sref,l.reference);
  2392. LOC_REGISTER,LOC_CREGISTER:
  2393. a_load64_subsetref_reg(list,sref,l.register64);
  2394. LOC_SUBSETREF,LOC_CSUBSETREF:
  2395. a_load64_subsetref_subsetref(list,sref,l.sref);
  2396. else
  2397. internalerror(2006082211);
  2398. end;
  2399. end;
  2400. {$else cpu64bitalu}
  2401. function joinreg128(reglo, reghi: tregister): tregister128;
  2402. begin
  2403. result.reglo:=reglo;
  2404. result.reghi:=reghi;
  2405. end;
  2406. procedure splitparaloc128(const cgpara:tcgpara;var cgparalo,cgparahi:tcgpara);
  2407. var
  2408. paraloclo,
  2409. paralochi : pcgparalocation;
  2410. begin
  2411. if not(cgpara.size in [OS_128,OS_S128]) then
  2412. internalerror(2012090604);
  2413. if not assigned(cgpara.location) then
  2414. internalerror(2012090605);
  2415. { init lo/hi para }
  2416. cgparahi.reset;
  2417. if cgpara.size=OS_S128 then
  2418. cgparahi.size:=OS_S64
  2419. else
  2420. cgparahi.size:=OS_64;
  2421. cgparahi.intsize:=8;
  2422. cgparahi.alignment:=cgpara.alignment;
  2423. paralochi:=cgparahi.add_location;
  2424. cgparalo.reset;
  2425. cgparalo.size:=OS_64;
  2426. cgparalo.intsize:=8;
  2427. cgparalo.alignment:=cgpara.alignment;
  2428. paraloclo:=cgparalo.add_location;
  2429. { 2 parameter fields? }
  2430. if assigned(cgpara.location^.next) then
  2431. begin
  2432. { Order for multiple locations is always
  2433. paraloc^ -> high
  2434. paraloc^.next -> low }
  2435. if (target_info.endian=ENDIAN_BIG) then
  2436. begin
  2437. { paraloc^ -> high
  2438. paraloc^.next -> low }
  2439. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2440. move(cgpara.location^.next^,paraloclo^,sizeof(paraloclo^));
  2441. end
  2442. else
  2443. begin
  2444. { paraloc^ -> low
  2445. paraloc^.next -> high }
  2446. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2447. move(cgpara.location^.next^,paralochi^,sizeof(paralochi^));
  2448. end;
  2449. end
  2450. else
  2451. begin
  2452. { single parameter, this can only be in memory }
  2453. if cgpara.location^.loc<>LOC_REFERENCE then
  2454. internalerror(2012090606);
  2455. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2456. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2457. { for big endian low is at +8, for little endian high }
  2458. if target_info.endian = endian_big then
  2459. begin
  2460. inc(cgparalo.location^.reference.offset,8);
  2461. cgparalo.alignment:=newalignment(cgparalo.alignment,8);
  2462. end
  2463. else
  2464. begin
  2465. inc(cgparahi.location^.reference.offset,8);
  2466. cgparahi.alignment:=newalignment(cgparahi.alignment,8);
  2467. end;
  2468. end;
  2469. { fix size }
  2470. paraloclo^.size:=cgparalo.size;
  2471. paraloclo^.next:=nil;
  2472. paralochi^.size:=cgparahi.size;
  2473. paralochi^.next:=nil;
  2474. end;
  2475. procedure tcg128.a_load128_reg_reg(list: TAsmList; regsrc,
  2476. regdst: tregister128);
  2477. begin
  2478. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reglo,regdst.reglo);
  2479. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reghi,regdst.reghi);
  2480. end;
  2481. procedure tcg128.a_load128_reg_ref(list: TAsmList; reg: tregister128;
  2482. const ref: treference);
  2483. var
  2484. tmpreg: tregister;
  2485. tmpref: treference;
  2486. begin
  2487. if target_info.endian = endian_big then
  2488. begin
  2489. tmpreg:=reg.reglo;
  2490. reg.reglo:=reg.reghi;
  2491. reg.reghi:=tmpreg;
  2492. end;
  2493. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reglo,ref);
  2494. tmpref := ref;
  2495. inc(tmpref.offset,8);
  2496. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reghi,tmpref);
  2497. end;
  2498. procedure tcg128.a_load128_ref_reg(list: TAsmList; const ref: treference;
  2499. reg: tregister128);
  2500. var
  2501. tmpreg: tregister;
  2502. tmpref: treference;
  2503. begin
  2504. if target_info.endian = endian_big then
  2505. begin
  2506. tmpreg := reg.reglo;
  2507. reg.reglo := reg.reghi;
  2508. reg.reghi := tmpreg;
  2509. end;
  2510. tmpref := ref;
  2511. if (tmpref.base=reg.reglo) then
  2512. begin
  2513. tmpreg:=cg.getaddressregister(list);
  2514. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.base,tmpreg);
  2515. tmpref.base:=tmpreg;
  2516. end
  2517. else
  2518. { this works only for the i386, thus the i386 needs to override }
  2519. { this method and this method must be replaced by a more generic }
  2520. { implementation FK }
  2521. if (tmpref.index=reg.reglo) then
  2522. begin
  2523. tmpreg:=cg.getaddressregister(list);
  2524. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.index,tmpreg);
  2525. tmpref.index:=tmpreg;
  2526. end;
  2527. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reglo);
  2528. inc(tmpref.offset,8);
  2529. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reghi);
  2530. end;
  2531. procedure tcg128.a_load128_loc_ref(list: TAsmList; const l: tlocation;
  2532. const ref: treference);
  2533. begin
  2534. case l.loc of
  2535. LOC_REGISTER,LOC_CREGISTER:
  2536. a_load128_reg_ref(list,l.register128,ref);
  2537. { not yet implemented:
  2538. LOC_CONSTANT :
  2539. a_load128_const_ref(list,l.value128,ref);
  2540. LOC_SUBSETREF, LOC_CSUBSETREF:
  2541. a_load64_subsetref_ref(list,l.sref,ref); }
  2542. else
  2543. internalerror(201209061);
  2544. end;
  2545. end;
  2546. procedure tcg128.a_load128_reg_loc(list: TAsmList; reg: tregister128;
  2547. const l: tlocation);
  2548. begin
  2549. case l.loc of
  2550. LOC_REFERENCE, LOC_CREFERENCE:
  2551. a_load128_reg_ref(list,reg,l.reference);
  2552. LOC_REGISTER,LOC_CREGISTER:
  2553. a_load128_reg_reg(list,reg,l.register128);
  2554. { not yet implemented:
  2555. LOC_SUBSETREF, LOC_CSUBSETREF:
  2556. a_load64_reg_subsetref(list,reg,l.sref);
  2557. LOC_MMREGISTER, LOC_CMMREGISTER:
  2558. a_loadmm_intreg64_reg(list,l.size,reg,l.register); }
  2559. else
  2560. internalerror(201209062);
  2561. end;
  2562. end;
  2563. procedure tcg128.a_load128_const_reg(list: TAsmList; valuelo,
  2564. valuehi: int64; reg: tregister128);
  2565. begin
  2566. cg.a_load_const_reg(list,OS_64,aint(valuelo),reg.reglo);
  2567. cg.a_load_const_reg(list,OS_64,aint(valuehi),reg.reghi);
  2568. end;
  2569. procedure tcg128.a_load128_loc_cgpara(list: TAsmList; const l: tlocation;
  2570. const paraloc: TCGPara);
  2571. begin
  2572. case l.loc of
  2573. LOC_REGISTER,
  2574. LOC_CREGISTER :
  2575. a_load128_reg_cgpara(list,l.register128,paraloc);
  2576. {not yet implemented:
  2577. LOC_CONSTANT :
  2578. a_load128_const_cgpara(list,l.value64,paraloc);
  2579. }
  2580. LOC_CREFERENCE,
  2581. LOC_REFERENCE :
  2582. a_load128_ref_cgpara(list,l.reference,paraloc);
  2583. else
  2584. internalerror(2012090603);
  2585. end;
  2586. end;
  2587. procedure tcg128.a_load128_reg_cgpara(list : TAsmList;reg : tregister128;const paraloc : tcgpara);
  2588. var
  2589. tmplochi,tmploclo: tcgpara;
  2590. begin
  2591. tmploclo.init;
  2592. tmplochi.init;
  2593. splitparaloc128(paraloc,tmploclo,tmplochi);
  2594. cg.a_load_reg_cgpara(list,OS_64,reg.reghi,tmplochi);
  2595. cg.a_load_reg_cgpara(list,OS_64,reg.reglo,tmploclo);
  2596. tmploclo.done;
  2597. tmplochi.done;
  2598. end;
  2599. procedure tcg128.a_load128_ref_cgpara(list : TAsmList;const r : treference;const paraloc : tcgpara);
  2600. var
  2601. tmprefhi,tmpreflo : treference;
  2602. tmploclo,tmplochi : tcgpara;
  2603. begin
  2604. tmploclo.init;
  2605. tmplochi.init;
  2606. splitparaloc128(paraloc,tmploclo,tmplochi);
  2607. tmprefhi:=r;
  2608. tmpreflo:=r;
  2609. if target_info.endian=endian_big then
  2610. inc(tmpreflo.offset,8)
  2611. else
  2612. inc(tmprefhi.offset,8);
  2613. cg.a_load_ref_cgpara(list,OS_64,tmprefhi,tmplochi);
  2614. cg.a_load_ref_cgpara(list,OS_64,tmpreflo,tmploclo);
  2615. tmploclo.done;
  2616. tmplochi.done;
  2617. end;
  2618. {$endif cpu64bitalu}
  2619. function asmsym2indsymflags(sym: TAsmSymbol): tindsymflags;
  2620. begin
  2621. result:=[];
  2622. if sym.typ<>AT_FUNCTION then
  2623. include(result,is_data);
  2624. if sym.bind=AB_WEAK_EXTERNAL then
  2625. include(result,is_weak);
  2626. end;
  2627. procedure destroy_codegen;
  2628. begin
  2629. cg.free;
  2630. cg:=nil;
  2631. {$ifdef cpu64bitalu}
  2632. cg128.free;
  2633. cg128:=nil;
  2634. {$else cpu64bitalu}
  2635. cg64.free;
  2636. cg64:=nil;
  2637. {$endif cpu64bitalu}
  2638. end;
  2639. end.