cgobj.pas 117 KB

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