cgobj.pas 120 KB

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