cgobj.pas 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946
  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,fromsize,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,fromsize,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,fromsize,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. if op in [OP_NEG,OP_NOT] then
  1506. begin
  1507. if reg<>NR_NO then
  1508. internalerror(2017040901);
  1509. a_op_reg_reg(list,op,size,tmpreg,tmpreg);
  1510. end
  1511. else
  1512. a_op_reg_reg(list,op,size,reg,tmpreg);
  1513. a_load_reg_ref(list,size,size,tmpreg,ref);
  1514. end;
  1515. procedure tcg.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1516. var
  1517. tmpreg: tregister;
  1518. begin
  1519. case op of
  1520. OP_NOT,OP_NEG:
  1521. { handle it as "load ref,reg; op reg" }
  1522. begin
  1523. a_load_ref_reg(list,size,size,ref,reg);
  1524. a_op_reg_reg(list,op,size,reg,reg);
  1525. end;
  1526. else
  1527. begin
  1528. tmpreg:=getintregister(list,size);
  1529. a_load_ref_reg(list,size,size,ref,tmpreg);
  1530. a_op_reg_reg(list,op,size,tmpreg,reg);
  1531. end;
  1532. end;
  1533. end;
  1534. procedure tcg.a_op_reg_loc(list : TAsmList; Op: TOpCG; reg: tregister; const loc: tlocation);
  1535. begin
  1536. case loc.loc of
  1537. LOC_REGISTER, LOC_CREGISTER:
  1538. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  1539. LOC_REFERENCE, LOC_CREFERENCE:
  1540. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  1541. else
  1542. internalerror(200109061);
  1543. end;
  1544. end;
  1545. procedure tcg.a_op_ref_loc(list : TAsmList; Op: TOpCG; const ref: TReference; const loc: tlocation);
  1546. var
  1547. tmpreg: tregister;
  1548. begin
  1549. case loc.loc of
  1550. LOC_REGISTER,LOC_CREGISTER:
  1551. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  1552. LOC_REFERENCE,LOC_CREFERENCE:
  1553. begin
  1554. tmpreg:=getintregister(list,loc.size);
  1555. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  1556. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  1557. end;
  1558. else
  1559. internalerror(200109061);
  1560. end;
  1561. end;
  1562. procedure Tcg.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1563. a:tcgint;src,dst:Tregister);
  1564. begin
  1565. optimize_op_const(size, op, a);
  1566. case op of
  1567. OP_NONE:
  1568. begin
  1569. if src <> dst then
  1570. a_load_reg_reg(list, size, size, src, dst);
  1571. exit;
  1572. end;
  1573. OP_MOVE:
  1574. begin
  1575. a_load_const_reg(list, size, a, dst);
  1576. exit;
  1577. end;
  1578. end;
  1579. a_load_reg_reg(list,size,size,src,dst);
  1580. a_op_const_reg(list,op,size,a,dst);
  1581. end;
  1582. procedure tcg.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1583. size: tcgsize; src1, src2, dst: tregister);
  1584. var
  1585. tmpreg: tregister;
  1586. begin
  1587. if (dst<>src1) then
  1588. begin
  1589. a_load_reg_reg(list,size,size,src2,dst);
  1590. a_op_reg_reg(list,op,size,src1,dst);
  1591. end
  1592. else
  1593. begin
  1594. { can we do a direct operation on the target register ? }
  1595. if op in [OP_ADD,OP_MUL,OP_AND,OP_MOVE,OP_XOR,OP_IMUL,OP_OR] then
  1596. a_op_reg_reg(list,op,size,src2,dst)
  1597. else
  1598. begin
  1599. tmpreg:=getintregister(list,size);
  1600. a_load_reg_reg(list,size,size,src2,tmpreg);
  1601. a_op_reg_reg(list,op,size,src1,tmpreg);
  1602. a_load_reg_reg(list,size,size,tmpreg,dst);
  1603. end;
  1604. end;
  1605. end;
  1606. procedure tcg.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1607. begin
  1608. a_op_const_reg_reg(list,op,size,a,src,dst);
  1609. ovloc.loc:=LOC_VOID;
  1610. end;
  1611. procedure tcg.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1612. begin
  1613. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1614. ovloc.loc:=LOC_VOID;
  1615. end;
  1616. procedure tcg.a_cmp_const_reg_label(list: TAsmList; size: tcgsize;
  1617. cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1618. var
  1619. tmpreg: tregister;
  1620. begin
  1621. tmpreg:=getintregister(list,size);
  1622. a_load_const_reg(list,size,a,tmpreg);
  1623. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1624. end;
  1625. procedure tcg.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  1626. l : tasmlabel);
  1627. var
  1628. tmpreg: tregister;
  1629. begin
  1630. tmpreg:=getintregister(list,size);
  1631. a_load_ref_reg(list,size,size,ref,tmpreg);
  1632. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1633. end;
  1634. procedure tcg.a_cmp_const_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const loc : tlocation;
  1635. l : tasmlabel);
  1636. begin
  1637. case loc.loc of
  1638. LOC_REGISTER,LOC_CREGISTER:
  1639. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1640. LOC_REFERENCE,LOC_CREFERENCE:
  1641. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1642. else
  1643. internalerror(200109061);
  1644. end;
  1645. end;
  1646. procedure tcg.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  1647. var
  1648. tmpreg: tregister;
  1649. begin
  1650. tmpreg:=getintregister(list,size);
  1651. a_load_ref_reg(list,size,size,ref,tmpreg);
  1652. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1653. end;
  1654. procedure tcg.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg : tregister; const ref: treference; l : tasmlabel);
  1655. var
  1656. tmpreg: tregister;
  1657. begin
  1658. tmpreg:=getintregister(list,size);
  1659. a_load_ref_reg(list,size,size,ref,tmpreg);
  1660. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  1661. end;
  1662. procedure tcg.a_cmp_reg_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  1663. begin
  1664. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  1665. end;
  1666. procedure tcg.a_cmp_loc_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  1667. begin
  1668. case loc.loc of
  1669. LOC_REGISTER,
  1670. LOC_CREGISTER:
  1671. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1672. LOC_REFERENCE,
  1673. LOC_CREFERENCE :
  1674. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1675. LOC_CONSTANT:
  1676. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1677. else
  1678. internalerror(200203231);
  1679. end;
  1680. end;
  1681. procedure tcg.a_cmp_ref_loc_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1682. l : tasmlabel);
  1683. var
  1684. tmpreg: tregister;
  1685. begin
  1686. case loc.loc of
  1687. LOC_REGISTER,LOC_CREGISTER:
  1688. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1689. LOC_REFERENCE,LOC_CREFERENCE:
  1690. begin
  1691. tmpreg:=getintregister(list,size);
  1692. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1693. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1694. end;
  1695. else
  1696. internalerror(200109061);
  1697. end;
  1698. end;
  1699. procedure tcg.a_loadmm_loc_reg(list: TAsmList; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  1700. begin
  1701. case loc.loc of
  1702. LOC_MMREGISTER,LOC_CMMREGISTER:
  1703. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1704. LOC_REFERENCE,LOC_CREFERENCE:
  1705. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  1706. LOC_REGISTER,LOC_CREGISTER:
  1707. a_loadmm_intreg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1708. else
  1709. internalerror(200310121);
  1710. end;
  1711. end;
  1712. procedure tcg.a_loadmm_reg_loc(list: TAsmList; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  1713. begin
  1714. case loc.loc of
  1715. LOC_MMREGISTER,LOC_CMMREGISTER:
  1716. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  1717. LOC_REFERENCE,LOC_CREFERENCE:
  1718. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  1719. else
  1720. internalerror(200310122);
  1721. end;
  1722. end;
  1723. procedure tcg.a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle);
  1724. var
  1725. href : treference;
  1726. {$ifndef cpu64bitalu}
  1727. tmpreg : tregister;
  1728. reg64 : tregister64;
  1729. {$endif not cpu64bitalu}
  1730. begin
  1731. {$ifndef cpu64bitalu}
  1732. if not(cgpara.location^.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  1733. (size<>OS_F64) then
  1734. {$endif not cpu64bitalu}
  1735. cgpara.check_simple_location;
  1736. paramanager.alloccgpara(list,cgpara);
  1737. case cgpara.location^.loc of
  1738. LOC_MMREGISTER,LOC_CMMREGISTER:
  1739. a_loadmm_reg_reg(list,size,cgpara.location^.size,reg,cgpara.location^.register,shuffle);
  1740. LOC_REFERENCE,LOC_CREFERENCE:
  1741. begin
  1742. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,cgpara.alignment,[]);
  1743. a_loadmm_reg_ref(list,size,cgpara.location^.size,reg,href,shuffle);
  1744. end;
  1745. LOC_REGISTER,LOC_CREGISTER:
  1746. begin
  1747. if assigned(shuffle) and
  1748. not shufflescalar(shuffle) then
  1749. internalerror(2009112510);
  1750. {$ifndef cpu64bitalu}
  1751. if (size=OS_F64) then
  1752. begin
  1753. if not assigned(cgpara.location^.next) or
  1754. assigned(cgpara.location^.next^.next) then
  1755. internalerror(2009112512);
  1756. case cgpara.location^.next^.loc of
  1757. LOC_REGISTER,LOC_CREGISTER:
  1758. tmpreg:=cgpara.location^.next^.register;
  1759. LOC_REFERENCE,LOC_CREFERENCE:
  1760. tmpreg:=getintregister(list,OS_32);
  1761. else
  1762. internalerror(2009112910);
  1763. end;
  1764. if (target_info.endian=ENDIAN_BIG) then
  1765. begin
  1766. { paraloc^ -> high
  1767. paraloc^.next -> low }
  1768. reg64.reghi:=cgpara.location^.register;
  1769. reg64.reglo:=tmpreg;
  1770. end
  1771. else
  1772. begin
  1773. { paraloc^ -> low
  1774. paraloc^.next -> high }
  1775. reg64.reglo:=cgpara.location^.register;
  1776. reg64.reghi:=tmpreg;
  1777. end;
  1778. cg64.a_loadmm_reg_intreg64(list,size,reg,reg64);
  1779. if (cgpara.location^.next^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1780. begin
  1781. if not(cgpara.location^.next^.size in [OS_32,OS_S32]) then
  1782. internalerror(2009112911);
  1783. reference_reset_base(href,cgpara.location^.next^.reference.index,cgpara.location^.next^.reference.offset,cgpara.alignment,[]);
  1784. a_load_reg_ref(list,OS_32,cgpara.location^.next^.size,tmpreg,href);
  1785. end;
  1786. end
  1787. else
  1788. {$endif not cpu64bitalu}
  1789. a_loadmm_reg_intreg(list,size,cgpara.location^.size,reg,cgpara.location^.register,mms_movescalar);
  1790. end
  1791. else
  1792. internalerror(200310123);
  1793. end;
  1794. end;
  1795. procedure tcg.a_loadmm_ref_cgpara(list: TAsmList; size: tcgsize;const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle);
  1796. var
  1797. hr : tregister;
  1798. hs : tmmshuffle;
  1799. begin
  1800. cgpara.check_simple_location;
  1801. hr:=getmmregister(list,cgpara.location^.size);
  1802. a_loadmm_ref_reg(list,size,cgpara.location^.size,ref,hr,shuffle);
  1803. if realshuffle(shuffle) then
  1804. begin
  1805. hs:=shuffle^;
  1806. removeshuffles(hs);
  1807. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,@hs);
  1808. end
  1809. else
  1810. a_loadmm_reg_cgpara(list,cgpara.location^.size,hr,cgpara,shuffle);
  1811. end;
  1812. procedure tcg.a_loadmm_loc_cgpara(list: TAsmList;const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle);
  1813. begin
  1814. case loc.loc of
  1815. LOC_MMREGISTER,LOC_CMMREGISTER:
  1816. a_loadmm_reg_cgpara(list,loc.size,loc.register,cgpara,shuffle);
  1817. LOC_REFERENCE,LOC_CREFERENCE:
  1818. a_loadmm_ref_cgpara(list,loc.size,loc.reference,cgpara,shuffle);
  1819. else
  1820. internalerror(200310123);
  1821. end;
  1822. end;
  1823. procedure tcg.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1824. var
  1825. hr : tregister;
  1826. hs : tmmshuffle;
  1827. begin
  1828. hr:=getmmregister(list,size);
  1829. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1830. if realshuffle(shuffle) then
  1831. begin
  1832. hs:=shuffle^;
  1833. removeshuffles(hs);
  1834. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  1835. end
  1836. else
  1837. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  1838. end;
  1839. procedure tcg.a_opmm_reg_ref(list: TAsmList; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  1840. var
  1841. hr : tregister;
  1842. hs : tmmshuffle;
  1843. begin
  1844. hr:=getmmregister(list,size);
  1845. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1846. if realshuffle(shuffle) then
  1847. begin
  1848. hs:=shuffle^;
  1849. removeshuffles(hs);
  1850. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  1851. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  1852. end
  1853. else
  1854. begin
  1855. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  1856. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  1857. end;
  1858. end;
  1859. procedure tcg.a_loadmm_intreg_reg(list: tasmlist; fromsize,tosize: tcgsize; intreg,mmreg: tregister; shuffle: pmmshuffle);
  1860. var
  1861. tmpref: treference;
  1862. begin
  1863. if (tcgsize2size[fromsize]<>4) or
  1864. (tcgsize2size[tosize]<>4) then
  1865. internalerror(2009112503);
  1866. tg.gettemp(list,4,4,tt_normal,tmpref);
  1867. a_load_reg_ref(list,fromsize,fromsize,intreg,tmpref);
  1868. a_loadmm_ref_reg(list,tosize,tosize,tmpref,mmreg,shuffle);
  1869. tg.ungettemp(list,tmpref);
  1870. end;
  1871. procedure tcg.a_loadmm_reg_intreg(list: tasmlist; fromsize,tosize: tcgsize; mmreg,intreg: tregister; shuffle: pmmshuffle);
  1872. var
  1873. tmpref: treference;
  1874. begin
  1875. if (tcgsize2size[fromsize]<>4) or
  1876. (tcgsize2size[tosize]<>4) then
  1877. internalerror(2009112504);
  1878. tg.gettemp(list,8,8,tt_normal,tmpref);
  1879. a_loadmm_reg_ref(list,fromsize,fromsize,mmreg,tmpref,shuffle);
  1880. a_load_ref_reg(list,tosize,tosize,tmpref,intreg);
  1881. tg.ungettemp(list,tmpref);
  1882. end;
  1883. procedure tcg.a_opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  1884. begin
  1885. case loc.loc of
  1886. LOC_CMMREGISTER,LOC_MMREGISTER:
  1887. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  1888. LOC_CREFERENCE,LOC_REFERENCE:
  1889. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  1890. else
  1891. internalerror(200312232);
  1892. end;
  1893. end;
  1894. procedure tcg.a_opmm_loc_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const loc: tlocation; src,dst: tregister;shuffle : pmmshuffle);
  1895. begin
  1896. case loc.loc of
  1897. LOC_CMMREGISTER,LOC_MMREGISTER:
  1898. a_opmm_reg_reg_reg(list,op,size,loc.register,src,dst,shuffle);
  1899. LOC_CREFERENCE,LOC_REFERENCE:
  1900. a_opmm_ref_reg_reg(list,op,size,loc.reference,src,dst,shuffle);
  1901. else
  1902. internalerror(200312232);
  1903. end;
  1904. end;
  1905. procedure tcg.a_opmm_reg_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;
  1906. src1,src2,dst : tregister;shuffle : pmmshuffle);
  1907. begin
  1908. internalerror(2013061102);
  1909. end;
  1910. procedure tcg.a_opmm_ref_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;
  1911. const ref : treference;src,dst : tregister;shuffle : pmmshuffle);
  1912. begin
  1913. internalerror(2013061101);
  1914. end;
  1915. procedure tcg.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  1916. begin
  1917. g_concatcopy(list,source,dest,len);
  1918. end;
  1919. procedure tcg.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  1920. begin
  1921. g_overflowCheck(list,loc,def);
  1922. end;
  1923. {$ifdef cpuflags}
  1924. procedure tcg.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref:TReference);
  1925. var
  1926. tmpreg : tregister;
  1927. begin
  1928. tmpreg:=getintregister(list,size);
  1929. g_flags2reg(list,size,f,tmpreg);
  1930. a_load_reg_ref(list,size,size,tmpreg,ref);
  1931. end;
  1932. {$endif cpuflags}
  1933. {*****************************************************************************
  1934. Entry/Exit Code Functions
  1935. *****************************************************************************}
  1936. procedure tcg.g_save_registers(list:TAsmList);
  1937. var
  1938. href : treference;
  1939. size : longint;
  1940. r : integer;
  1941. begin
  1942. { calculate temp. size }
  1943. size:=0;
  1944. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1945. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1946. inc(size,sizeof(aint));
  1947. if uses_registers(R_ADDRESSREGISTER) then
  1948. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1949. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1950. inc(size,sizeof(aint));
  1951. { mm registers }
  1952. if uses_registers(R_MMREGISTER) then
  1953. begin
  1954. { Make sure we reserve enough space to do the alignment based on the offset
  1955. later on. We can't use the size for this, because the alignment of the start
  1956. of the temp is smaller than needed for an OS_VECTOR }
  1957. inc(size,tcgsize2size[OS_VECTOR]);
  1958. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  1959. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  1960. inc(size,tcgsize2size[OS_VECTOR]);
  1961. end;
  1962. if size>0 then
  1963. begin
  1964. tg.GetTemp(list,size,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  1965. include(current_procinfo.flags,pi_has_saved_regs);
  1966. { Copy registers to temp }
  1967. href:=current_procinfo.save_regs_ref;
  1968. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1969. begin
  1970. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1971. begin
  1972. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE),href);
  1973. inc(href.offset,sizeof(aint));
  1974. end;
  1975. include(rg[R_INTREGISTER].preserved_by_proc,saved_standard_registers[r]);
  1976. end;
  1977. if uses_registers(R_ADDRESSREGISTER) then
  1978. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1979. begin
  1980. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1981. begin
  1982. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE),href);
  1983. inc(href.offset,sizeof(aint));
  1984. end;
  1985. include(rg[R_ADDRESSREGISTER].preserved_by_proc,saved_address_registers[r]);
  1986. end;
  1987. if uses_registers(R_MMREGISTER) then
  1988. begin
  1989. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  1990. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  1991. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  1992. begin
  1993. { the array has to be declared even if no MM registers are saved
  1994. (such as with SSE on i386), and since 0-element arrays don't
  1995. exist, they contain a single RS_INVALID element in that case
  1996. }
  1997. if saved_mm_registers[r]<>RS_INVALID then
  1998. begin
  1999. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  2000. begin
  2001. a_loadmm_reg_ref(list,OS_VECTOR,OS_VECTOR,newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBMMWHOLE),href,nil);
  2002. inc(href.offset,tcgsize2size[OS_VECTOR]);
  2003. end;
  2004. include(rg[R_MMREGISTER].preserved_by_proc,saved_mm_registers[r]);
  2005. end;
  2006. end;
  2007. end;
  2008. end;
  2009. end;
  2010. procedure tcg.g_restore_registers(list:TAsmList);
  2011. var
  2012. href : treference;
  2013. r : integer;
  2014. hreg : tregister;
  2015. begin
  2016. if not(pi_has_saved_regs in current_procinfo.flags) then
  2017. exit;
  2018. { Copy registers from temp }
  2019. href:=current_procinfo.save_regs_ref;
  2020. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  2021. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2022. begin
  2023. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  2024. { Allocate register so the optimizer does not remove the load }
  2025. a_reg_alloc(list,hreg);
  2026. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2027. inc(href.offset,sizeof(aint));
  2028. end;
  2029. if uses_registers(R_ADDRESSREGISTER) then
  2030. for r:=low(saved_address_registers) to high(saved_address_registers) do
  2031. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  2032. begin
  2033. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  2034. { Allocate register so the optimizer does not remove the load }
  2035. a_reg_alloc(list,hreg);
  2036. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2037. inc(href.offset,sizeof(aint));
  2038. end;
  2039. if uses_registers(R_MMREGISTER) then
  2040. begin
  2041. if (href.offset mod tcgsize2size[OS_VECTOR])<>0 then
  2042. inc(href.offset,tcgsize2size[OS_VECTOR]-(href.offset mod tcgsize2size[OS_VECTOR]));
  2043. for r:=low(saved_mm_registers) to high(saved_mm_registers) do
  2044. begin
  2045. if saved_mm_registers[r] in rg[R_MMREGISTER].used_in_proc then
  2046. begin
  2047. hreg:=newreg(R_MMREGISTER,saved_mm_registers[r],R_SUBMMWHOLE);
  2048. { Allocate register so the optimizer does not remove the load }
  2049. a_reg_alloc(list,hreg);
  2050. a_loadmm_ref_reg(list,OS_VECTOR,OS_VECTOR,href,hreg,nil);
  2051. inc(href.offset,tcgsize2size[OS_VECTOR]);
  2052. end;
  2053. end;
  2054. end;
  2055. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  2056. end;
  2057. procedure tcg.g_profilecode(list : TAsmList);
  2058. begin
  2059. end;
  2060. procedure tcg.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  2061. var
  2062. hsym : tsym;
  2063. href : treference;
  2064. paraloc : Pcgparalocation;
  2065. begin
  2066. { calculate the parameter info for the procdef }
  2067. procdef.init_paraloc_info(callerside);
  2068. hsym:=tsym(procdef.parast.Find('self'));
  2069. if not(assigned(hsym) and
  2070. (hsym.typ=paravarsym)) then
  2071. internalerror(200305251);
  2072. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  2073. while paraloc<>nil do
  2074. with paraloc^ do
  2075. begin
  2076. case loc of
  2077. LOC_REGISTER:
  2078. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  2079. LOC_REFERENCE:
  2080. begin
  2081. { offset in the wrapper needs to be adjusted for the stored
  2082. return address }
  2083. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),sizeof(pint),[]);
  2084. a_op_const_ref(list,OP_SUB,size,ioffset,href);
  2085. end
  2086. else
  2087. internalerror(200309189);
  2088. end;
  2089. paraloc:=next;
  2090. end;
  2091. end;
  2092. procedure tcg.a_call_name_static(list : TAsmList;const s : string);
  2093. begin
  2094. a_call_name(list,s,false);
  2095. end;
  2096. function tcg.g_indirect_sym_load(list:TAsmList;const symname: string; const flags: tindsymflags): tregister;
  2097. var
  2098. l: tasmsymbol;
  2099. ref: treference;
  2100. nlsymname: string;
  2101. symtyp: TAsmsymtype;
  2102. begin
  2103. result := NR_NO;
  2104. case target_info.system of
  2105. system_powerpc_darwin,
  2106. system_i386_darwin,
  2107. system_i386_iphonesim,
  2108. system_powerpc64_darwin,
  2109. system_arm_darwin:
  2110. begin
  2111. nlsymname:='L'+symname+'$non_lazy_ptr';
  2112. l:=current_asmdata.getasmsymbol(nlsymname);
  2113. if not(assigned(l)) then
  2114. begin
  2115. if is_data in flags then
  2116. symtyp:=AT_DATA
  2117. else
  2118. symtyp:=AT_FUNCTION;
  2119. new_section(current_asmdata.asmlists[al_picdata],sec_data_nonlazy,'',sizeof(pint));
  2120. l:=current_asmdata.DefineAsmSymbol(nlsymname,AB_LOCAL,AT_DATA,voidpointertype);
  2121. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create(l,0));
  2122. if not(is_weak in flags) then
  2123. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.RefAsmSymbol(symname,symtyp).Name))
  2124. else
  2125. current_asmdata.asmlists[al_picdata].concat(tai_directive.Create(asd_indirect_symbol,current_asmdata.WeakRefAsmSymbol(symname,symtyp).Name));
  2126. {$ifdef cpu64bitaddr}
  2127. current_asmdata.asmlists[al_picdata].concat(tai_const.create_64bit(0));
  2128. {$else cpu64bitaddr}
  2129. current_asmdata.asmlists[al_picdata].concat(tai_const.create_32bit(0));
  2130. {$endif cpu64bitaddr}
  2131. end;
  2132. result := getaddressregister(list);
  2133. reference_reset_symbol(ref,l,0,sizeof(pint),[]);
  2134. { a_load_ref_reg will turn this into a pic-load if needed }
  2135. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,result);
  2136. end;
  2137. end;
  2138. end;
  2139. procedure tcg.g_maybe_got_init(list: TAsmList);
  2140. begin
  2141. end;
  2142. procedure tcg.g_call(list: TAsmList;const s: string);
  2143. begin
  2144. allocallcpuregisters(list);
  2145. a_call_name(list,s,false);
  2146. deallocallcpuregisters(list);
  2147. end;
  2148. procedure tcg.g_local_unwind(list: TAsmList; l: TAsmLabel);
  2149. begin
  2150. a_jmp_always(list,l);
  2151. end;
  2152. procedure tcg.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  2153. begin
  2154. internalerror(200807231);
  2155. end;
  2156. procedure tcg.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2157. begin
  2158. internalerror(200807232);
  2159. end;
  2160. procedure tcg.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2161. begin
  2162. internalerror(200807233);
  2163. end;
  2164. procedure tcg.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  2165. begin
  2166. internalerror(200807234);
  2167. end;
  2168. function tcg.getflagregister(list: TAsmList; size: Tcgsize): Tregister;
  2169. begin
  2170. Result:=TRegister(0);
  2171. internalerror(200807238);
  2172. end;
  2173. procedure tcg.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tcgsize; src, dst: TRegister);
  2174. begin
  2175. internalerror(2014070601);
  2176. end;
  2177. procedure tcg.g_stackpointer_alloc(list: TAsmList; size: longint);
  2178. begin
  2179. internalerror(2014070602);
  2180. end;
  2181. procedure tcg.a_mul_reg_reg_pair(list: TAsmList; size: TCgSize; src1,src2,dstlo,dsthi: TRegister);
  2182. begin
  2183. internalerror(2014060801);
  2184. end;
  2185. procedure tcg.g_div_const_reg_reg(list:tasmlist; size: TCgSize; a: tcgint; src,dst: tregister);
  2186. var
  2187. divreg: tregister;
  2188. magic: aInt;
  2189. u_magic: aWord;
  2190. u_shift: byte;
  2191. u_add: boolean;
  2192. begin
  2193. divreg:=getintregister(list,OS_INT);
  2194. if (size in [OS_S32,OS_S64]) then
  2195. begin
  2196. calc_divconst_magic_signed(tcgsize2size[size]*8,a,magic,u_shift);
  2197. { load magic value }
  2198. a_load_const_reg(list,OS_INT,magic,divreg);
  2199. { multiply, discarding low bits }
  2200. a_mul_reg_reg_pair(list,size,src,divreg,NR_NO,dst);
  2201. { add/subtract numerator }
  2202. if (a>0) and (magic<0) then
  2203. a_op_reg_reg_reg(list,OP_ADD,OS_INT,src,dst,dst)
  2204. else if (a<0) and (magic>0) then
  2205. a_op_reg_reg_reg(list,OP_SUB,OS_INT,src,dst,dst);
  2206. { shift shift places to the right (arithmetic) }
  2207. a_op_const_reg_reg(list,OP_SAR,OS_INT,u_shift,dst,dst);
  2208. { extract and add sign bit }
  2209. if (a>=0) then
  2210. a_op_const_reg_reg(list,OP_SHR,OS_INT,tcgsize2size[size]*8-1,src,divreg)
  2211. else
  2212. a_op_const_reg_reg(list,OP_SHR,OS_INT,tcgsize2size[size]*8-1,dst,divreg);
  2213. a_op_reg_reg_reg(list,OP_ADD,OS_INT,dst,divreg,dst);
  2214. end
  2215. else if (size in [OS_32,OS_64]) then
  2216. begin
  2217. calc_divconst_magic_unsigned(tcgsize2size[size]*8,a,u_magic,u_add,u_shift);
  2218. { load magic in divreg }
  2219. a_load_const_reg(list,OS_INT,tcgint(u_magic),divreg);
  2220. { multiply, discarding low bits }
  2221. a_mul_reg_reg_pair(list,size,src,divreg,NR_NO,dst);
  2222. if (u_add) then
  2223. begin
  2224. { Calculate "(numerator+result) shr u_shift", avoiding possible overflow }
  2225. a_op_reg_reg_reg(list,OP_SUB,OS_INT,dst,src,divreg);
  2226. { divreg=(numerator-result) }
  2227. a_op_const_reg_reg(list,OP_SHR,OS_INT,1,divreg,divreg);
  2228. { divreg=(numerator-result)/2 }
  2229. a_op_reg_reg_reg(list,OP_ADD,OS_INT,divreg,dst,divreg);
  2230. { divreg=(numerator+result)/2, already shifted by 1, so decrease u_shift. }
  2231. a_op_const_reg_reg(list,OP_SHR,OS_INT,u_shift-1,divreg,dst);
  2232. end
  2233. else
  2234. a_op_const_reg_reg(list,OP_SHR,OS_INT,u_shift,dst,dst);
  2235. end
  2236. else
  2237. InternalError(2014060601);
  2238. end;
  2239. {*****************************************************************************
  2240. TCG64
  2241. *****************************************************************************}
  2242. {$ifndef cpu64bitalu}
  2243. function joinreg64(reglo,reghi : tregister) : tregister64;
  2244. begin
  2245. result.reglo:=reglo;
  2246. result.reghi:=reghi;
  2247. end;
  2248. procedure tcg64.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64; regsrc,regdst : tregister64);
  2249. begin
  2250. a_load64_reg_reg(list,regsrc,regdst);
  2251. a_op64_const_reg(list,op,size,value,regdst);
  2252. end;
  2253. procedure tcg64.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  2254. var
  2255. tmpreg64 : tregister64;
  2256. begin
  2257. { when src1=dst then we need to first create a temp to prevent
  2258. overwriting src1 with src2 }
  2259. if (regsrc1.reghi=regdst.reghi) or
  2260. (regsrc1.reglo=regdst.reghi) or
  2261. (regsrc1.reghi=regdst.reglo) or
  2262. (regsrc1.reglo=regdst.reglo) then
  2263. begin
  2264. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2265. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2266. a_load64_reg_reg(list,regsrc2,tmpreg64);
  2267. a_op64_reg_reg(list,op,size,regsrc1,tmpreg64);
  2268. a_load64_reg_reg(list,tmpreg64,regdst);
  2269. end
  2270. else
  2271. begin
  2272. a_load64_reg_reg(list,regsrc2,regdst);
  2273. a_op64_reg_reg(list,op,size,regsrc1,regdst);
  2274. end;
  2275. end;
  2276. procedure tcg64.a_op64_const_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; a : int64; const sref: tsubsetreference);
  2277. var
  2278. tmpreg64 : tregister64;
  2279. begin
  2280. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2281. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2282. a_load64_subsetref_reg(list,sref,tmpreg64);
  2283. a_op64_const_reg(list,op,size,a,tmpreg64);
  2284. a_load64_reg_subsetref(list,tmpreg64,sref);
  2285. end;
  2286. procedure tcg64.a_op64_reg_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; reg: tregister64; const sref: tsubsetreference);
  2287. var
  2288. tmpreg64 : tregister64;
  2289. begin
  2290. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2291. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2292. a_load64_subsetref_reg(list,sref,tmpreg64);
  2293. a_op64_reg_reg(list,op,size,reg,tmpreg64);
  2294. a_load64_reg_subsetref(list,tmpreg64,sref);
  2295. end;
  2296. procedure tcg64.a_op64_ref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ref: treference; const sref: tsubsetreference);
  2297. var
  2298. tmpreg64 : tregister64;
  2299. begin
  2300. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2301. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2302. a_load64_subsetref_reg(list,sref,tmpreg64);
  2303. a_op64_ref_reg(list,op,size,ref,tmpreg64);
  2304. a_load64_reg_subsetref(list,tmpreg64,sref);
  2305. end;
  2306. procedure tcg64.a_op64_subsetref_subsetref(list : TAsmList; Op : TOpCG; size : TCGSize; const ssref,dsref: tsubsetreference);
  2307. var
  2308. tmpreg64 : tregister64;
  2309. begin
  2310. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  2311. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  2312. a_load64_subsetref_reg(list,ssref,tmpreg64);
  2313. a_op64_reg_subsetref(list,op,size,tmpreg64,dsref);
  2314. end;
  2315. procedure tcg64.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2316. begin
  2317. a_op64_const_reg_reg(list,op,size,value,regsrc,regdst);
  2318. ovloc.loc:=LOC_VOID;
  2319. end;
  2320. procedure tcg64.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2321. begin
  2322. a_op64_reg_reg_reg(list,op,size,regsrc1,regsrc2,regdst);
  2323. ovloc.loc:=LOC_VOID;
  2324. end;
  2325. procedure tcg64.a_load64_loc_subsetref(list : TAsmList;const l: tlocation; const sref : tsubsetreference);
  2326. begin
  2327. case l.loc of
  2328. LOC_REFERENCE, LOC_CREFERENCE:
  2329. a_load64_ref_subsetref(list,l.reference,sref);
  2330. LOC_REGISTER,LOC_CREGISTER:
  2331. a_load64_reg_subsetref(list,l.register64,sref);
  2332. LOC_CONSTANT :
  2333. a_load64_const_subsetref(list,l.value64,sref);
  2334. LOC_SUBSETREF,LOC_CSUBSETREF:
  2335. a_load64_subsetref_subsetref(list,l.sref,sref);
  2336. else
  2337. internalerror(2006082210);
  2338. end;
  2339. end;
  2340. procedure tcg64.a_load64_subsetref_loc(list: TAsmlist; const sref: tsubsetreference; const l: tlocation);
  2341. begin
  2342. case l.loc of
  2343. LOC_REFERENCE, LOC_CREFERENCE:
  2344. a_load64_subsetref_ref(list,sref,l.reference);
  2345. LOC_REGISTER,LOC_CREGISTER:
  2346. a_load64_subsetref_reg(list,sref,l.register64);
  2347. LOC_SUBSETREF,LOC_CSUBSETREF:
  2348. a_load64_subsetref_subsetref(list,sref,l.sref);
  2349. else
  2350. internalerror(2006082211);
  2351. end;
  2352. end;
  2353. {$else cpu64bitalu}
  2354. function joinreg128(reglo, reghi: tregister): tregister128;
  2355. begin
  2356. result.reglo:=reglo;
  2357. result.reghi:=reghi;
  2358. end;
  2359. procedure splitparaloc128(const cgpara:tcgpara;var cgparalo,cgparahi:tcgpara);
  2360. var
  2361. paraloclo,
  2362. paralochi : pcgparalocation;
  2363. begin
  2364. if not(cgpara.size in [OS_128,OS_S128]) then
  2365. internalerror(2012090604);
  2366. if not assigned(cgpara.location) then
  2367. internalerror(2012090605);
  2368. { init lo/hi para }
  2369. cgparahi.reset;
  2370. if cgpara.size=OS_S128 then
  2371. cgparahi.size:=OS_S64
  2372. else
  2373. cgparahi.size:=OS_64;
  2374. cgparahi.intsize:=8;
  2375. cgparahi.alignment:=cgpara.alignment;
  2376. paralochi:=cgparahi.add_location;
  2377. cgparalo.reset;
  2378. cgparalo.size:=OS_64;
  2379. cgparalo.intsize:=8;
  2380. cgparalo.alignment:=cgpara.alignment;
  2381. paraloclo:=cgparalo.add_location;
  2382. { 2 parameter fields? }
  2383. if assigned(cgpara.location^.next) then
  2384. begin
  2385. { Order for multiple locations is always
  2386. paraloc^ -> high
  2387. paraloc^.next -> low }
  2388. if (target_info.endian=ENDIAN_BIG) then
  2389. begin
  2390. { paraloc^ -> high
  2391. paraloc^.next -> low }
  2392. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2393. move(cgpara.location^.next^,paraloclo^,sizeof(paraloclo^));
  2394. end
  2395. else
  2396. begin
  2397. { paraloc^ -> low
  2398. paraloc^.next -> high }
  2399. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2400. move(cgpara.location^.next^,paralochi^,sizeof(paralochi^));
  2401. end;
  2402. end
  2403. else
  2404. begin
  2405. { single parameter, this can only be in memory }
  2406. if cgpara.location^.loc<>LOC_REFERENCE then
  2407. internalerror(2012090606);
  2408. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  2409. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  2410. { for big endian low is at +8, for little endian high }
  2411. if target_info.endian = endian_big then
  2412. begin
  2413. inc(cgparalo.location^.reference.offset,8);
  2414. cgparalo.alignment:=newalignment(cgparalo.alignment,8);
  2415. end
  2416. else
  2417. begin
  2418. inc(cgparahi.location^.reference.offset,8);
  2419. cgparahi.alignment:=newalignment(cgparahi.alignment,8);
  2420. end;
  2421. end;
  2422. { fix size }
  2423. paraloclo^.size:=cgparalo.size;
  2424. paraloclo^.next:=nil;
  2425. paralochi^.size:=cgparahi.size;
  2426. paralochi^.next:=nil;
  2427. end;
  2428. procedure tcg128.a_load128_reg_reg(list: TAsmList; regsrc,
  2429. regdst: tregister128);
  2430. begin
  2431. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reglo,regdst.reglo);
  2432. cg.a_load_reg_reg(list,OS_64,OS_64,regsrc.reghi,regdst.reghi);
  2433. end;
  2434. procedure tcg128.a_load128_reg_ref(list: TAsmList; reg: tregister128;
  2435. const ref: treference);
  2436. var
  2437. tmpreg: tregister;
  2438. tmpref: treference;
  2439. begin
  2440. if target_info.endian = endian_big then
  2441. begin
  2442. tmpreg:=reg.reglo;
  2443. reg.reglo:=reg.reghi;
  2444. reg.reghi:=tmpreg;
  2445. end;
  2446. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reglo,ref);
  2447. tmpref := ref;
  2448. inc(tmpref.offset,8);
  2449. cg.a_load_reg_ref(list,OS_64,OS_64,reg.reghi,tmpref);
  2450. end;
  2451. procedure tcg128.a_load128_ref_reg(list: TAsmList; const ref: treference;
  2452. reg: tregister128);
  2453. var
  2454. tmpreg: tregister;
  2455. tmpref: treference;
  2456. begin
  2457. if target_info.endian = endian_big then
  2458. begin
  2459. tmpreg := reg.reglo;
  2460. reg.reglo := reg.reghi;
  2461. reg.reghi := tmpreg;
  2462. end;
  2463. tmpref := ref;
  2464. if (tmpref.base=reg.reglo) then
  2465. begin
  2466. tmpreg:=cg.getaddressregister(list);
  2467. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.base,tmpreg);
  2468. tmpref.base:=tmpreg;
  2469. end
  2470. else
  2471. { this works only for the i386, thus the i386 needs to override }
  2472. { this method and this method must be replaced by a more generic }
  2473. { implementation FK }
  2474. if (tmpref.index=reg.reglo) then
  2475. begin
  2476. tmpreg:=cg.getaddressregister(list);
  2477. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.index,tmpreg);
  2478. tmpref.index:=tmpreg;
  2479. end;
  2480. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reglo);
  2481. inc(tmpref.offset,8);
  2482. cg.a_load_ref_reg(list,OS_64,OS_64,tmpref,reg.reghi);
  2483. end;
  2484. procedure tcg128.a_load128_loc_ref(list: TAsmList; const l: tlocation;
  2485. const ref: treference);
  2486. begin
  2487. case l.loc of
  2488. LOC_REGISTER,LOC_CREGISTER:
  2489. a_load128_reg_ref(list,l.register128,ref);
  2490. { not yet implemented:
  2491. LOC_CONSTANT :
  2492. a_load128_const_ref(list,l.value128,ref);
  2493. LOC_SUBSETREF, LOC_CSUBSETREF:
  2494. a_load64_subsetref_ref(list,l.sref,ref); }
  2495. else
  2496. internalerror(201209061);
  2497. end;
  2498. end;
  2499. procedure tcg128.a_load128_reg_loc(list: TAsmList; reg: tregister128;
  2500. const l: tlocation);
  2501. begin
  2502. case l.loc of
  2503. LOC_REFERENCE, LOC_CREFERENCE:
  2504. a_load128_reg_ref(list,reg,l.reference);
  2505. LOC_REGISTER,LOC_CREGISTER:
  2506. a_load128_reg_reg(list,reg,l.register128);
  2507. { not yet implemented:
  2508. LOC_SUBSETREF, LOC_CSUBSETREF:
  2509. a_load64_reg_subsetref(list,reg,l.sref);
  2510. LOC_MMREGISTER, LOC_CMMREGISTER:
  2511. a_loadmm_intreg64_reg(list,l.size,reg,l.register); }
  2512. else
  2513. internalerror(201209062);
  2514. end;
  2515. end;
  2516. procedure tcg128.a_load128_const_reg(list: TAsmList; valuelo,
  2517. valuehi: int64; reg: tregister128);
  2518. begin
  2519. cg.a_load_const_reg(list,OS_64,aint(valuelo),reg.reglo);
  2520. cg.a_load_const_reg(list,OS_64,aint(valuehi),reg.reghi);
  2521. end;
  2522. procedure tcg128.a_load128_loc_cgpara(list: TAsmList; const l: tlocation;
  2523. const paraloc: TCGPara);
  2524. begin
  2525. case l.loc of
  2526. LOC_REGISTER,
  2527. LOC_CREGISTER :
  2528. a_load128_reg_cgpara(list,l.register128,paraloc);
  2529. {not yet implemented:
  2530. LOC_CONSTANT :
  2531. a_load128_const_cgpara(list,l.value64,paraloc);
  2532. }
  2533. LOC_CREFERENCE,
  2534. LOC_REFERENCE :
  2535. a_load128_ref_cgpara(list,l.reference,paraloc);
  2536. else
  2537. internalerror(2012090603);
  2538. end;
  2539. end;
  2540. procedure tcg128.a_load128_reg_cgpara(list : TAsmList;reg : tregister128;const paraloc : tcgpara);
  2541. var
  2542. tmplochi,tmploclo: tcgpara;
  2543. begin
  2544. tmploclo.init;
  2545. tmplochi.init;
  2546. splitparaloc128(paraloc,tmploclo,tmplochi);
  2547. cg.a_load_reg_cgpara(list,OS_64,reg.reghi,tmplochi);
  2548. cg.a_load_reg_cgpara(list,OS_64,reg.reglo,tmploclo);
  2549. tmploclo.done;
  2550. tmplochi.done;
  2551. end;
  2552. procedure tcg128.a_load128_ref_cgpara(list : TAsmList;const r : treference;const paraloc : tcgpara);
  2553. var
  2554. tmprefhi,tmpreflo : treference;
  2555. tmploclo,tmplochi : tcgpara;
  2556. begin
  2557. tmploclo.init;
  2558. tmplochi.init;
  2559. splitparaloc128(paraloc,tmploclo,tmplochi);
  2560. tmprefhi:=r;
  2561. tmpreflo:=r;
  2562. if target_info.endian=endian_big then
  2563. inc(tmpreflo.offset,8)
  2564. else
  2565. inc(tmprefhi.offset,8);
  2566. cg.a_load_ref_cgpara(list,OS_64,tmprefhi,tmplochi);
  2567. cg.a_load_ref_cgpara(list,OS_64,tmpreflo,tmploclo);
  2568. tmploclo.done;
  2569. tmplochi.done;
  2570. end;
  2571. {$endif cpu64bitalu}
  2572. function asmsym2indsymflags(sym: TAsmSymbol): tindsymflags;
  2573. begin
  2574. result:=[];
  2575. if sym.typ<>AT_FUNCTION then
  2576. include(result,is_data);
  2577. if sym.bind=AB_WEAK_EXTERNAL then
  2578. include(result,is_weak);
  2579. end;
  2580. procedure destroy_codegen;
  2581. begin
  2582. cg.free;
  2583. cg:=nil;
  2584. {$ifdef cpu64bitalu}
  2585. cg128.free;
  2586. cg128:=nil;
  2587. {$else cpu64bitalu}
  2588. cg64.free;
  2589. cg64:=nil;
  2590. {$endif cpu64bitalu}
  2591. end;
  2592. end.