cgobj.pas 110 KB

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