cgobj.pas 119 KB

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