cgobj.pas 120 KB

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