cgobj.pas 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109
  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,
  31. cpubase,cgbase,cgutils,parabase,
  32. aasmbase,aasmtai,aasmcpu,
  33. symconst,symbase,symtype,symdef,symtable,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 overriden for all new processors which will be supported
  41. by Free Pascal. For 32-bit processors, the base class
  42. sould be @link(tcg64f32) and not @var(tcg).
  43. }
  44. tcg = class
  45. public
  46. alignment : talignment;
  47. rg : array[tregistertype] of trgobj;
  48. t_times : longint;
  49. {$ifdef flowgraph}
  50. aktflownode:word;
  51. {$endif}
  52. {************************************************}
  53. { basic routines }
  54. constructor create;
  55. {# Initialize the register allocators needed for the codegenerator.}
  56. procedure init_register_allocators;virtual;
  57. {# Clean up the register allocators needed for the codegenerator.}
  58. procedure done_register_allocators;virtual;
  59. {# 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. }
  60. procedure set_regalloc_extend_backwards(b: boolean);
  61. {$ifdef flowgraph}
  62. procedure init_flowgraph;
  63. procedure done_flowgraph;
  64. {$endif}
  65. {# Gets a register suitable to do integer operations on.}
  66. function getintregister(list:Taasmoutput;size:Tcgsize):Tregister;virtual;
  67. {# Gets a register suitable to do integer operations on.}
  68. function getaddressregister(list:Taasmoutput):Tregister;virtual;
  69. function getfpuregister(list:Taasmoutput;size:Tcgsize):Tregister;virtual;
  70. function getmmregister(list:Taasmoutput;size:Tcgsize):Tregister;virtual;
  71. function getflagregister(list:Taasmoutput;size:Tcgsize):Tregister;virtual;abstract;
  72. {Does the generic cg need SIMD registers, like getmmxregister? Or should
  73. the cpu specific child cg object have such a method?}
  74. procedure add_reg_instruction(instr:Tai;r:tregister);virtual;
  75. procedure add_move_instruction(instr:Taicpu);virtual;
  76. function uses_registers(rt:Tregistertype):boolean;virtual;
  77. {# Get a specific register.}
  78. procedure getcpuregister(list:Taasmoutput;r:Tregister);virtual;
  79. procedure ungetcpuregister(list:Taasmoutput;r:Tregister);virtual;
  80. {# Get multiple registers specified.}
  81. procedure alloccpuregisters(list:Taasmoutput;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  82. {# Free multiple registers specified.}
  83. procedure dealloccpuregisters(list:Taasmoutput;rt:Tregistertype;const r:Tcpuregisterset);virtual;
  84. procedure allocallcpuregisters(list:Taasmoutput);virtual;
  85. procedure deallocallcpuregisters(list:Taasmoutput);virtual;
  86. procedure do_register_allocation(list:Taasmoutput;headertai:tai);virtual;
  87. procedure translate_register(var reg : tregister);
  88. function makeregsize(list:Taasmoutput;reg:Tregister;size:Tcgsize):Tregister;
  89. {# Emit a label to the instruction stream. }
  90. procedure a_label(list : taasmoutput;l : tasmlabel);virtual;
  91. {# Allocates register r by inserting a pai_realloc record }
  92. procedure a_reg_alloc(list : taasmoutput;r : tregister);
  93. {# Deallocates register r by inserting a pa_regdealloc record}
  94. procedure a_reg_dealloc(list : taasmoutput;r : tregister);
  95. { Synchronize register, make sure it is still valid }
  96. procedure a_reg_sync(list : taasmoutput;r : tregister);
  97. {# Pass a parameter, which is located in a register, to a routine.
  98. This routine should push/send the parameter to the routine, as
  99. required by the specific processor ABI and routine modifiers.
  100. This must be overriden for each CPU target.
  101. @param(size size of the operand in the register)
  102. @param(r register source of the operand)
  103. @param(cgpara where the parameter will be stored)
  104. }
  105. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const cgpara : TCGPara);virtual;
  106. {# Pass a parameter, which is a constant, to a routine.
  107. A generic version is provided. This routine should
  108. be overriden for optimization purposes if the cpu
  109. permits directly sending this type of parameter.
  110. @param(size size of the operand in constant)
  111. @param(a value of constant to send)
  112. @param(cgpara where the parameter will be stored)
  113. }
  114. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aint;const cgpara : TCGPara);virtual;
  115. {# Pass the value of a parameter, which is located in memory, to a routine.
  116. A generic version is provided. This routine should
  117. be overriden for optimization purposes if the cpu
  118. permits directly sending this type of parameter.
  119. @param(size size of the operand in constant)
  120. @param(r Memory reference of value to send)
  121. @param(cgpara where the parameter will be stored)
  122. }
  123. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const cgpara : TCGPara);virtual;
  124. {# Pass the value of a parameter, which can be located either in a register or memory location,
  125. to a routine.
  126. A generic version is provided.
  127. @param(l location of the operand to send)
  128. @param(nr parameter number (starting from one) of routine (from left to right))
  129. @param(cgpara where the parameter will be stored)
  130. }
  131. procedure a_param_loc(list : taasmoutput;const l : tlocation;const cgpara : TCGPara);
  132. {# Pass the address of a reference to a routine. This routine
  133. will calculate the address of the reference, and pass this
  134. calculated address as a parameter.
  135. A generic version is provided. This routine should
  136. be overriden for optimization purposes if the cpu
  137. permits directly sending this type of parameter.
  138. @param(r reference to get address from)
  139. @param(nr parameter number (starting from one) of routine (from left to right))
  140. }
  141. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const cgpara : TCGPara);virtual;
  142. { Remarks:
  143. * If a method specifies a size you have only to take care
  144. of that number of bits, i.e. load_const_reg with OP_8 must
  145. only load the lower 8 bit of the specified register
  146. the rest of the register can be undefined
  147. if necessary the compiler will call a method
  148. to zero or sign extend the register
  149. * The a_load_XX_XX with OP_64 needn't to be
  150. implemented for 32 bit
  151. processors, the code generator takes care of that
  152. * the addr size is for work with the natural pointer
  153. size
  154. * the procedures without fpu/mm are only for integer usage
  155. * normally the first location is the source and the
  156. second the destination
  157. }
  158. {# Emits instruction to call the method specified by symbol name.
  159. This routine must be overriden for each new target cpu.
  160. There is no a_call_ref because loading the reference will use
  161. a temp register on most cpu's resulting in conflicts with the
  162. registers used for the parameters (PFV)
  163. }
  164. procedure a_call_name(list : taasmoutput;const s : string);virtual; abstract;
  165. procedure a_call_reg(list : taasmoutput;reg : tregister);virtual; abstract;
  166. procedure a_call_ref(list : taasmoutput;ref : treference);virtual; abstract;
  167. { same as a_call_name, might be overriden on certain architectures to emit
  168. static calls without usage of a got trampoline }
  169. procedure a_call_name_static(list : taasmoutput;const s : string);virtual;
  170. { move instructions }
  171. procedure a_load_const_reg(list : taasmoutput;size : tcgsize;a : aint;register : tregister);virtual; abstract;
  172. procedure a_load_const_ref(list : taasmoutput;size : tcgsize;a : aint;const ref : treference);virtual;
  173. procedure a_load_const_loc(list : taasmoutput;a : aint;const loc : tlocation);
  174. procedure a_load_reg_ref(list : taasmoutput;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  175. procedure a_load_reg_reg(list : taasmoutput;fromsize,tosize : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  176. procedure a_load_reg_loc(list : taasmoutput;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  177. procedure a_load_ref_reg(list : taasmoutput;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  178. procedure a_load_ref_ref(list : taasmoutput;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);virtual;
  179. procedure a_load_loc_reg(list : taasmoutput;tosize: tcgsize; const loc: tlocation; reg : tregister);
  180. procedure a_load_loc_ref(list : taasmoutput;tosize: tcgsize; const loc: tlocation; const ref : treference);
  181. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);virtual; abstract;
  182. { fpu move instructions }
  183. procedure a_loadfpu_reg_reg(list: taasmoutput; size:tcgsize; reg1, reg2: tregister); virtual; abstract;
  184. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  185. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  186. procedure a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  187. procedure a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  188. procedure a_paramfpu_reg(list : taasmoutput;size : tcgsize;const r : tregister;const cgpara : TCGPara);virtual;
  189. procedure a_paramfpu_ref(list : taasmoutput;size : tcgsize;const ref : treference;const cgpara : TCGPara);virtual;
  190. { vector register move instructions }
  191. procedure a_loadmm_reg_reg(list: taasmoutput; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); virtual; abstract;
  192. procedure a_loadmm_ref_reg(list: taasmoutput; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual; abstract;
  193. procedure a_loadmm_reg_ref(list: taasmoutput; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual; abstract;
  194. procedure a_loadmm_loc_reg(list: taasmoutput; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  195. procedure a_loadmm_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  196. procedure a_parammm_reg(list: taasmoutput; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  197. procedure a_parammm_ref(list: taasmoutput; size: tcgsize; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  198. procedure a_parammm_loc(list: taasmoutput; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  199. procedure a_opmm_reg_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle); virtual;abstract;
  200. procedure a_opmm_ref_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  201. procedure a_opmm_loc_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  202. procedure a_opmm_reg_ref(list: taasmoutput; Op: TOpCG; size : tcgsize;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  203. { basic arithmetic operations }
  204. { note: for operators which require only one argument (not, neg), use }
  205. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  206. { that in this case the *second* operand is used as both source and }
  207. { destination (JM) }
  208. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: Aint; reg: TRegister); virtual; abstract;
  209. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: Aint; const ref: TReference); virtual;
  210. procedure a_op_const_loc(list : taasmoutput; Op: TOpCG; a: Aint; const loc: tlocation);
  211. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  212. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  213. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  214. procedure a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  215. procedure a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  216. { trinary operations for processors that support them, 'emulated' }
  217. { on others. None with "ref" arguments since I don't think there }
  218. { are any processors that support it (JM) }
  219. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister); virtual;
  220. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg; size: tcgsize; src1, src2, dst: tregister); virtual;
  221. procedure a_op_const_reg_reg_checkoverflow(list: taasmoutput; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  222. procedure a_op_reg_reg_reg_checkoverflow(list: taasmoutput; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  223. { comparison operations }
  224. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  225. l : tasmlabel);virtual; abstract;
  226. procedure a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  227. l : tasmlabel); virtual;
  228. procedure a_cmp_const_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; a: aint; const loc: tlocation;
  229. l : tasmlabel);
  230. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  231. procedure a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  232. procedure a_cmp_reg_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  233. procedure a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  234. procedure a_cmp_reg_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  235. procedure a_cmp_ref_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  236. l : tasmlabel);
  237. procedure a_jmp_name(list : taasmoutput;const s : string); virtual; abstract;
  238. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); virtual; abstract;
  239. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); virtual; abstract;
  240. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  241. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  242. }
  243. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  244. procedure g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  245. {
  246. This routine tries to optimize the const_reg opcode, and should be
  247. called at the start of a_op_const_reg. It returns the actual opcode
  248. to emit, and the constant value to emit. If this routine returns
  249. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  250. @param(op The opcode to emit, returns the opcode which must be emitted)
  251. @param(a The constant which should be emitted, returns the constant which must
  252. be emitted)
  253. @param(reg The register to emit the opcode with, returns the register with
  254. which the opcode will be emitted)
  255. }
  256. function optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aint; var reg: tregister): boolean;virtual;
  257. {#
  258. This routine is used in exception management nodes. It should
  259. save the exception reason currently in the FUNCTION_RETURN_REG. The
  260. save should be done either to a temp (pointed to by href).
  261. or on the stack (pushing the value on the stack).
  262. The size of the value to save is OS_S32. The default version
  263. saves the exception reason to a temp. memory area.
  264. }
  265. procedure g_exception_reason_save(list : taasmoutput; const href : treference);virtual;
  266. {#
  267. This routine is used in exception management nodes. It should
  268. save the exception reason constant. The
  269. save should be done either to a temp (pointed to by href).
  270. or on the stack (pushing the value on the stack).
  271. The size of the value to save is OS_S32. The default version
  272. saves the exception reason to a temp. memory area.
  273. }
  274. procedure g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aint);virtual;
  275. {#
  276. This routine is used in exception management nodes. It should
  277. load the exception reason to the FUNCTION_RETURN_REG. The saved value
  278. should either be in the temp. area (pointed to by href , href should
  279. *NOT* be freed) or on the stack (the value should be popped).
  280. The size of the value to save is OS_S32. The default version
  281. saves the exception reason to a temp. memory area.
  282. }
  283. procedure g_exception_reason_load(list : taasmoutput; const href : treference);virtual;
  284. procedure g_maybe_testself(list : taasmoutput;reg:tregister);
  285. procedure g_maybe_testvmt(list : taasmoutput;reg:tregister;objdef:tobjectdef);
  286. {# This should emit the opcode to copy len bytes from the source
  287. to destination.
  288. It must be overriden for each new target processor.
  289. @param(source Source reference of copy)
  290. @param(dest Destination reference of copy)
  291. }
  292. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint);virtual; abstract;
  293. {# This should emit the opcode to copy len bytes from the an unaligned source
  294. to destination.
  295. It must be overriden for each new target processor.
  296. @param(source Source reference of copy)
  297. @param(dest Destination reference of copy)
  298. }
  299. procedure g_concatcopy_unaligned(list : taasmoutput;const source,dest : treference;len : aint);virtual;
  300. {# This should emit the opcode to a shortrstring from the source
  301. to destination.
  302. @param(source Source reference of copy)
  303. @param(dest Destination reference of copy)
  304. }
  305. procedure g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte);
  306. procedure g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  307. procedure g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  308. procedure g_initialize(list : taasmoutput;t : tdef;const ref : treference);
  309. procedure g_finalize(list : taasmoutput;t : tdef;const ref : treference);
  310. {# Generates range checking code. It is to note
  311. that this routine does not need to be overriden,
  312. as it takes care of everything.
  313. @param(p Node which contains the value to check)
  314. @param(todef Type definition of node to range check)
  315. }
  316. procedure g_rangecheck(list: taasmoutput; const l:tlocation; fromdef,todef: tdef); virtual;
  317. {# Generates overflow checking code for a node }
  318. procedure g_overflowcheck(list: taasmoutput; const Loc:tlocation; def:tdef); virtual;abstract;
  319. procedure g_overflowCheck_loc(List:TAasmOutput;const Loc:TLocation;def:TDef;ovloc : tlocation);virtual;
  320. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;const lenloc:tlocation;elesize:aint;destreg:tregister);virtual;
  321. procedure g_releasevaluepara_openarray(list : taasmoutput;const l:tlocation);virtual;
  322. {# Emits instructions when compilation is done in profile
  323. mode (this is set as a command line option). The default
  324. behavior does nothing, should be overriden as required.
  325. }
  326. procedure g_profilecode(list : taasmoutput);virtual;
  327. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  328. @param(size Number of bytes to allocate)
  329. }
  330. procedure g_stackpointer_alloc(list : taasmoutput;size : longint);virtual; abstract;
  331. {# Emits instruction for allocating the locals in entry
  332. code of a routine. This is one of the first
  333. routine called in @var(genentrycode).
  334. @param(localsize Number of bytes to allocate as locals)
  335. }
  336. procedure g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);virtual; abstract;
  337. {# Emits instructions for returning from a subroutine.
  338. Should also restore the framepointer and stack.
  339. @param(parasize Number of bytes of parameters to deallocate from stack)
  340. }
  341. procedure g_proc_exit(list : taasmoutput;parasize:longint;nostackframe:boolean);virtual;abstract;
  342. {# This routine is called when generating the code for the entry point
  343. of a routine. It should save all registers which are not used in this
  344. routine, and which should be declared as saved in the std_saved_registers
  345. set.
  346. This routine is mainly used when linking to code which is generated
  347. by ABI-compliant compilers (like GCC), to make sure that the reserved
  348. registers of that ABI are not clobbered.
  349. @param(usedinproc Registers which are used in the code of this routine)
  350. }
  351. procedure g_save_standard_registers(list:Taasmoutput);virtual;
  352. {# This routine is called when generating the code for the exit point
  353. of a routine. It should restore all registers which were previously
  354. saved in @var(g_save_standard_registers).
  355. @param(usedinproc Registers which are used in the code of this routine)
  356. }
  357. procedure g_restore_standard_registers(list:Taasmoutput);virtual;
  358. procedure g_intf_wrapper(list: TAAsmoutput; procdef: tprocdef; const labelname: string; ioffset: longint);virtual;abstract;
  359. procedure g_adjust_self_value(list:taasmoutput;procdef: tprocdef;ioffset: aint);virtual;
  360. end;
  361. {$ifndef cpu64bit}
  362. {# @abstract(Abstract code generator for 64 Bit operations)
  363. This class implements an abstract code generator class
  364. for 64 Bit operations.
  365. }
  366. tcg64 = class
  367. procedure a_load64_const_ref(list : taasmoutput;value : int64;const ref : treference);virtual;abstract;
  368. procedure a_load64_reg_ref(list : taasmoutput;reg : tregister64;const ref : treference);virtual;abstract;
  369. procedure a_load64_ref_reg(list : taasmoutput;const ref : treference;reg : tregister64);virtual;abstract;
  370. procedure a_load64_reg_reg(list : taasmoutput;regsrc,regdst : tregister64);virtual;abstract;
  371. procedure a_load64_const_reg(list : taasmoutput;value : int64;reg : tregister64);virtual;abstract;
  372. procedure a_load64_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister64);virtual;abstract;
  373. procedure a_load64_loc_ref(list : taasmoutput;const l : tlocation;const ref : treference);virtual;abstract;
  374. procedure a_load64_const_loc(list : taasmoutput;value : int64;const l : tlocation);virtual;abstract;
  375. procedure a_load64_reg_loc(list : taasmoutput;reg : tregister64;const l : tlocation);virtual;abstract;
  376. procedure a_load64high_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  377. procedure a_load64low_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);virtual;abstract;
  378. procedure a_load64high_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  379. procedure a_load64low_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);virtual;abstract;
  380. procedure a_load64high_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  381. procedure a_load64low_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);virtual;abstract;
  382. procedure a_op64_ref_reg(list : taasmoutput;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);virtual;abstract;
  383. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);virtual;abstract;
  384. procedure a_op64_reg_ref(list : taasmoutput;op:TOpCG;size : tcgsize;regsrc : tregister64;const ref : treference);virtual;abstract;
  385. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;size : tcgsize;value : int64;regdst : tregister64);virtual;abstract;
  386. procedure a_op64_const_ref(list : taasmoutput;op:TOpCG;size : tcgsize;value : int64;const ref : treference);virtual;abstract;
  387. procedure a_op64_const_loc(list : taasmoutput;op:TOpCG;size : tcgsize;value : int64;const l: tlocation);virtual;abstract;
  388. procedure a_op64_reg_loc(list : taasmoutput;op:TOpCG;size : tcgsize;reg : tregister64;const l : tlocation);virtual;abstract;
  389. procedure a_op64_loc_reg(list : taasmoutput;op:TOpCG;size : tcgsize;const l : tlocation;reg64 : tregister64);virtual;abstract;
  390. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);virtual;
  391. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);virtual;
  392. procedure a_op64_const_reg_reg_checkoverflow(list: taasmoutput;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  393. procedure a_op64_reg_reg_reg_checkoverflow(list: taasmoutput;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);virtual;
  394. procedure a_param64_reg(list : taasmoutput;reg64 : tregister64;const loc : TCGPara);virtual;abstract;
  395. procedure a_param64_const(list : taasmoutput;value : int64;const loc : TCGPara);virtual;abstract;
  396. procedure a_param64_ref(list : taasmoutput;const r : treference;const loc : TCGPara);virtual;abstract;
  397. procedure a_param64_loc(list : taasmoutput;const l : tlocation;const loc : TCGPara);virtual;abstract;
  398. {
  399. This routine tries to optimize the const_reg opcode, and should be
  400. called at the start of a_op64_const_reg. It returns the actual opcode
  401. to emit, and the constant value to emit. If this routine returns
  402. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  403. @param(op The opcode to emit, returns the opcode which must be emitted)
  404. @param(a The constant which should be emitted, returns the constant which must
  405. be emitted)
  406. @param(reg The register to emit the opcode with, returns the register with
  407. which the opcode will be emitted)
  408. }
  409. function optimize64_op_const_reg(list: taasmoutput; var op: topcg; var a : int64; var reg: tregister64): boolean;virtual;abstract;
  410. { override to catch 64bit rangechecks }
  411. procedure g_rangecheck64(list: taasmoutput; const l:tlocation; fromdef,todef: tdef);virtual;abstract;
  412. end;
  413. {$endif cpu64bit}
  414. var
  415. {# Main code generator class }
  416. cg : tcg;
  417. {$ifndef cpu64bit}
  418. {# Code generator class for all operations working with 64-Bit operands }
  419. cg64 : tcg64;
  420. {$endif cpu64bit}
  421. implementation
  422. uses
  423. globals,options,systems,
  424. verbose,defutil,paramgr,symsym,
  425. tgobj,cutils,procinfo;
  426. const
  427. { Please leave this here, this module should NOT use
  428. exprasmlist, the lists are always passed as arguments.
  429. Declaring it as string here results in an error when compiling (PFV) }
  430. exprasmlist = 'error';
  431. {*****************************************************************************
  432. basic functionallity
  433. ******************************************************************************}
  434. constructor tcg.create;
  435. begin
  436. end;
  437. {*****************************************************************************
  438. register allocation
  439. ******************************************************************************}
  440. procedure tcg.init_register_allocators;
  441. begin
  442. fillchar(rg,sizeof(rg),0);
  443. add_reg_instruction_hook:=@add_reg_instruction;
  444. end;
  445. procedure tcg.done_register_allocators;
  446. begin
  447. { Safety }
  448. fillchar(rg,sizeof(rg),0);
  449. add_reg_instruction_hook:=nil;
  450. end;
  451. {$ifdef flowgraph}
  452. procedure Tcg.init_flowgraph;
  453. begin
  454. aktflownode:=0;
  455. end;
  456. procedure Tcg.done_flowgraph;
  457. begin
  458. end;
  459. {$endif}
  460. function tcg.getintregister(list:Taasmoutput;size:Tcgsize):Tregister;
  461. begin
  462. if not assigned(rg[R_INTREGISTER]) then
  463. internalerror(200312122);
  464. result:=rg[R_INTREGISTER].getregister(list,cgsize2subreg(size));
  465. end;
  466. function tcg.getfpuregister(list:Taasmoutput;size:Tcgsize):Tregister;
  467. begin
  468. if not assigned(rg[R_FPUREGISTER]) then
  469. internalerror(200312123);
  470. result:=rg[R_FPUREGISTER].getregister(list,cgsize2subreg(size));
  471. end;
  472. function tcg.getmmregister(list:Taasmoutput;size:Tcgsize):Tregister;
  473. begin
  474. if not assigned(rg[R_MMREGISTER]) then
  475. internalerror(2003121214);
  476. result:=rg[R_MMREGISTER].getregister(list,cgsize2subreg(size));
  477. end;
  478. function tcg.getaddressregister(list:Taasmoutput):Tregister;
  479. begin
  480. if assigned(rg[R_ADDRESSREGISTER]) then
  481. result:=rg[R_ADDRESSREGISTER].getregister(list,R_SUBWHOLE)
  482. else
  483. begin
  484. if not assigned(rg[R_INTREGISTER]) then
  485. internalerror(200312121);
  486. result:=rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  487. end;
  488. end;
  489. function Tcg.makeregsize(list:Taasmoutput;reg:Tregister;size:Tcgsize):Tregister;
  490. var
  491. subreg:Tsubregister;
  492. begin
  493. subreg:=cgsize2subreg(size);
  494. result:=reg;
  495. setsubreg(result,subreg);
  496. { notify RA }
  497. if result<>reg then
  498. list.concat(tai_regalloc.resize(result));
  499. end;
  500. procedure tcg.getcpuregister(list:Taasmoutput;r:Tregister);
  501. begin
  502. if not assigned(rg[getregtype(r)]) then
  503. internalerror(200312125);
  504. rg[getregtype(r)].getcpuregister(list,r);
  505. end;
  506. procedure tcg.ungetcpuregister(list:Taasmoutput;r:Tregister);
  507. begin
  508. if not assigned(rg[getregtype(r)]) then
  509. internalerror(200312126);
  510. rg[getregtype(r)].ungetcpuregister(list,r);
  511. end;
  512. procedure tcg.alloccpuregisters(list:Taasmoutput;rt:Tregistertype;const r:Tcpuregisterset);
  513. begin
  514. if assigned(rg[rt]) then
  515. rg[rt].alloccpuregisters(list,r)
  516. else
  517. internalerror(200310092);
  518. end;
  519. procedure tcg.allocallcpuregisters(list:Taasmoutput);
  520. begin
  521. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  522. {$ifndef i386}
  523. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  524. {$ifdef cpumm}
  525. alloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  526. {$endif cpumm}
  527. {$endif i386}
  528. end;
  529. procedure tcg.dealloccpuregisters(list:Taasmoutput;rt:Tregistertype;const r:Tcpuregisterset);
  530. begin
  531. if assigned(rg[rt]) then
  532. rg[rt].dealloccpuregisters(list,r)
  533. else
  534. internalerror(200310093);
  535. end;
  536. procedure tcg.deallocallcpuregisters(list:Taasmoutput);
  537. begin
  538. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  539. {$ifndef i386}
  540. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  541. {$ifdef cpumm}
  542. dealloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  543. {$endif cpumm}
  544. {$endif i386}
  545. end;
  546. function tcg.uses_registers(rt:Tregistertype):boolean;
  547. begin
  548. if assigned(rg[rt]) then
  549. result:=rg[rt].uses_registers
  550. else
  551. result:=false;
  552. end;
  553. procedure tcg.add_reg_instruction(instr:Tai;r:tregister);
  554. var
  555. rt : tregistertype;
  556. begin
  557. rt:=getregtype(r);
  558. { Only add it when a register allocator is configured.
  559. No IE can be generated, because the VMT is written
  560. without a valid rg[] }
  561. if assigned(rg[rt]) then
  562. rg[rt].add_reg_instruction(instr,r);
  563. end;
  564. procedure tcg.add_move_instruction(instr:Taicpu);
  565. var
  566. rt : tregistertype;
  567. begin
  568. rt:=getregtype(instr.oper[O_MOV_SOURCE]^.reg);
  569. if assigned(rg[rt]) then
  570. rg[rt].add_move_instruction(instr)
  571. else
  572. internalerror(200310095);
  573. end;
  574. procedure tcg.set_regalloc_extend_backwards(b: boolean);
  575. var
  576. rt : tregistertype;
  577. begin
  578. for rt:=low(rg) to high(rg) do
  579. begin
  580. if assigned(rg[rt]) then
  581. rg[rt].extend_live_range_backwards := b;;
  582. end;
  583. end;
  584. procedure tcg.do_register_allocation(list:Taasmoutput;headertai:tai);
  585. var
  586. rt : tregistertype;
  587. begin
  588. for rt:=R_FPUREGISTER to R_SPECIALREGISTER do
  589. begin
  590. if assigned(rg[rt]) then
  591. rg[rt].do_register_allocation(list,headertai);
  592. end;
  593. { running the other register allocator passes could require addition int/addr. registers
  594. when spilling so run int/addr register allocation at the end }
  595. if assigned(rg[R_INTREGISTER]) then
  596. rg[R_INTREGISTER].do_register_allocation(list,headertai);
  597. if assigned(rg[R_ADDRESSREGISTER]) then
  598. rg[R_ADDRESSREGISTER].do_register_allocation(list,headertai);
  599. end;
  600. procedure tcg.translate_register(var reg : tregister);
  601. begin
  602. rg[getregtype(reg)].translate_register(reg);
  603. end;
  604. procedure tcg.a_reg_alloc(list : taasmoutput;r : tregister);
  605. begin
  606. list.concat(tai_regalloc.alloc(r,nil));
  607. end;
  608. procedure tcg.a_reg_dealloc(list : taasmoutput;r : tregister);
  609. begin
  610. list.concat(tai_regalloc.dealloc(r,nil));
  611. end;
  612. procedure tcg.a_reg_sync(list : taasmoutput;r : tregister);
  613. var
  614. instr : tai;
  615. begin
  616. instr:=tai_regalloc.sync(r);
  617. list.concat(instr);
  618. add_reg_instruction(instr,r);
  619. end;
  620. procedure tcg.a_label(list : taasmoutput;l : tasmlabel);
  621. begin
  622. list.concat(tai_label.create(l));
  623. end;
  624. {*****************************************************************************
  625. for better code generation these methods should be overridden
  626. ******************************************************************************}
  627. procedure tcg.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const cgpara : TCGPara);
  628. var
  629. ref : treference;
  630. begin
  631. cgpara.check_simple_location;
  632. case cgpara.location^.loc of
  633. LOC_REGISTER,LOC_CREGISTER:
  634. a_load_reg_reg(list,size,cgpara.location^.size,r,cgpara.location^.register);
  635. LOC_REFERENCE,LOC_CREFERENCE:
  636. begin
  637. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  638. a_load_reg_ref(list,size,cgpara.location^.size,r,ref);
  639. end
  640. else
  641. internalerror(2002071004);
  642. end;
  643. end;
  644. procedure tcg.a_param_const(list : taasmoutput;size : tcgsize;a : aint;const cgpara : TCGPara);
  645. var
  646. ref : treference;
  647. begin
  648. cgpara.check_simple_location;
  649. case cgpara.location^.loc of
  650. LOC_REGISTER,LOC_CREGISTER:
  651. a_load_const_reg(list,cgpara.location^.size,a,cgpara.location^.register);
  652. LOC_REFERENCE,LOC_CREFERENCE:
  653. begin
  654. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  655. a_load_const_ref(list,cgpara.location^.size,a,ref);
  656. end
  657. else
  658. internalerror(2002071004);
  659. end;
  660. end;
  661. procedure tcg.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const cgpara : TCGPara);
  662. var
  663. ref : treference;
  664. begin
  665. cgpara.check_simple_location;
  666. case cgpara.location^.loc of
  667. LOC_REGISTER,LOC_CREGISTER:
  668. a_load_ref_reg(list,size,cgpara.location^.size,r,cgpara.location^.register);
  669. LOC_REFERENCE,LOC_CREFERENCE:
  670. begin
  671. reference_reset(ref);
  672. ref.base:=cgpara.location^.reference.index;
  673. ref.offset:=cgpara.location^.reference.offset;
  674. { use concatcopy, because it can also be a float which fails when
  675. load_ref_ref is used }
  676. g_concatcopy(list,r,ref,tcgsize2size[size]);
  677. end
  678. else
  679. internalerror(2002071004);
  680. end;
  681. end;
  682. procedure tcg.a_param_loc(list : taasmoutput;const l:tlocation;const cgpara : TCGPara);
  683. begin
  684. case l.loc of
  685. LOC_REGISTER,
  686. LOC_CREGISTER :
  687. a_param_reg(list,l.size,l.register,cgpara);
  688. LOC_CONSTANT :
  689. a_param_const(list,l.size,l.value,cgpara);
  690. LOC_CREFERENCE,
  691. LOC_REFERENCE :
  692. a_param_ref(list,l.size,l.reference,cgpara);
  693. else
  694. internalerror(2002032211);
  695. end;
  696. end;
  697. procedure tcg.a_paramaddr_ref(list : taasmoutput;const r : treference;const cgpara : TCGPara);
  698. var
  699. hr : tregister;
  700. begin
  701. cgpara.check_simple_location;
  702. hr:=getaddressregister(list);
  703. a_loadaddr_ref_reg(list,r,hr);
  704. a_param_reg(list,OS_ADDR,hr,cgpara);
  705. end;
  706. {****************************************************************************
  707. some generic implementations
  708. ****************************************************************************}
  709. procedure tcg.a_load_ref_ref(list : taasmoutput;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  710. var
  711. tmpreg: tregister;
  712. begin
  713. { verify if we have the same reference }
  714. if references_equal(sref,dref) then
  715. exit;
  716. tmpreg:=getintregister(list,tosize);
  717. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  718. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  719. end;
  720. procedure tcg.a_load_const_ref(list : taasmoutput;size : tcgsize;a : aint;const ref : treference);
  721. var
  722. tmpreg: tregister;
  723. begin
  724. tmpreg:=getintregister(list,size);
  725. a_load_const_reg(list,size,a,tmpreg);
  726. a_load_reg_ref(list,size,size,tmpreg,ref);
  727. end;
  728. procedure tcg.a_load_const_loc(list : taasmoutput;a : aint;const loc: tlocation);
  729. begin
  730. case loc.loc of
  731. LOC_REFERENCE,LOC_CREFERENCE:
  732. a_load_const_ref(list,loc.size,a,loc.reference);
  733. LOC_REGISTER,LOC_CREGISTER:
  734. a_load_const_reg(list,loc.size,a,loc.register);
  735. else
  736. internalerror(200203272);
  737. end;
  738. end;
  739. procedure tcg.a_load_reg_loc(list : taasmoutput;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  740. begin
  741. case loc.loc of
  742. LOC_REFERENCE,LOC_CREFERENCE:
  743. a_load_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  744. LOC_REGISTER,LOC_CREGISTER:
  745. a_load_reg_reg(list,fromsize,loc.size,reg,loc.register);
  746. else
  747. internalerror(200203271);
  748. end;
  749. end;
  750. procedure tcg.a_load_loc_reg(list : taasmoutput; tosize: tcgsize; const loc: tlocation; reg : tregister);
  751. begin
  752. case loc.loc of
  753. LOC_REFERENCE,LOC_CREFERENCE:
  754. a_load_ref_reg(list,loc.size,tosize,loc.reference,reg);
  755. LOC_REGISTER,LOC_CREGISTER:
  756. a_load_reg_reg(list,loc.size,tosize,loc.register,reg);
  757. LOC_CONSTANT:
  758. a_load_const_reg(list,tosize,loc.value,reg);
  759. else
  760. internalerror(200109092);
  761. end;
  762. end;
  763. procedure tcg.a_load_loc_ref(list : taasmoutput;tosize: tcgsize; const loc: tlocation; const ref : treference);
  764. begin
  765. case loc.loc of
  766. LOC_REFERENCE,LOC_CREFERENCE:
  767. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  768. LOC_REGISTER,LOC_CREGISTER:
  769. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  770. LOC_CONSTANT:
  771. a_load_const_ref(list,tosize,loc.value,ref);
  772. else
  773. internalerror(200109302);
  774. end;
  775. end;
  776. function tcg.optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aint; var reg:tregister): boolean;
  777. var
  778. powerval : longint;
  779. begin
  780. optimize_op_const_reg := false;
  781. case op of
  782. { or with zero returns same result }
  783. OP_OR : if a = 0 then optimize_op_const_reg := true;
  784. { and with max returns same result }
  785. OP_AND : if (a = high(a)) then optimize_op_const_reg := true;
  786. { division by 1 returns result }
  787. OP_DIV :
  788. begin
  789. if a = 1 then
  790. optimize_op_const_reg := true
  791. else if ispowerof2(int64(a), powerval) then
  792. begin
  793. a := powerval;
  794. op:= OP_SHR;
  795. end;
  796. exit;
  797. end;
  798. OP_IDIV:
  799. begin
  800. if a = 1 then
  801. optimize_op_const_reg := true
  802. else if ispowerof2(int64(a), powerval) then
  803. begin
  804. a := powerval;
  805. op:= OP_SAR;
  806. end;
  807. exit;
  808. end;
  809. OP_MUL,OP_IMUL:
  810. begin
  811. if a = 1 then
  812. optimize_op_const_reg := true
  813. else if ispowerof2(int64(a), powerval) then
  814. begin
  815. a := powerval;
  816. op:= OP_SHL;
  817. end;
  818. exit;
  819. end;
  820. OP_SAR,OP_SHL,OP_SHR:
  821. begin
  822. if a = 0 then
  823. optimize_op_const_reg := true;
  824. exit;
  825. end;
  826. end;
  827. end;
  828. procedure tcg.a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  829. begin
  830. case loc.loc of
  831. LOC_REFERENCE, LOC_CREFERENCE:
  832. a_loadfpu_ref_reg(list,loc.size,loc.reference,reg);
  833. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  834. a_loadfpu_reg_reg(list,loc.size,loc.register,reg);
  835. else
  836. internalerror(200203301);
  837. end;
  838. end;
  839. procedure tcg.a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  840. begin
  841. case loc.loc of
  842. LOC_REFERENCE, LOC_CREFERENCE:
  843. a_loadfpu_reg_ref(list,size,reg,loc.reference);
  844. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  845. a_loadfpu_reg_reg(list,size,reg,loc.register);
  846. else
  847. internalerror(48991);
  848. end;
  849. end;
  850. procedure tcg.a_paramfpu_reg(list : taasmoutput;size : tcgsize;const r : tregister;const cgpara : TCGPara);
  851. var
  852. ref : treference;
  853. begin
  854. case cgpara.location^.loc of
  855. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  856. begin
  857. cgpara.check_simple_location;
  858. a_loadfpu_reg_reg(list,size,r,cgpara.location^.register);
  859. end;
  860. LOC_REFERENCE,LOC_CREFERENCE:
  861. begin
  862. cgpara.check_simple_location;
  863. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  864. a_loadfpu_reg_ref(list,size,r,ref);
  865. end;
  866. LOC_REGISTER,LOC_CREGISTER:
  867. begin
  868. { paramfpu_ref does the check_simpe_location check here if necessary }
  869. tg.GetTemp(list,TCGSize2Size[size],tt_normal,ref);
  870. a_loadfpu_reg_ref(list,size,r,ref);
  871. a_paramfpu_ref(list,size,ref,cgpara);
  872. tg.Ungettemp(list,ref);
  873. end;
  874. else
  875. internalerror(2002071004);
  876. end;
  877. end;
  878. procedure tcg.a_paramfpu_ref(list : taasmoutput;size : tcgsize;const ref : treference;const cgpara : TCGPara);
  879. var
  880. href : treference;
  881. begin
  882. cgpara.check_simple_location;
  883. case cgpara.location^.loc of
  884. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  885. a_loadfpu_ref_reg(list,size,ref,cgpara.location^.register);
  886. LOC_REFERENCE,LOC_CREFERENCE:
  887. begin
  888. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  889. { concatcopy should choose the best way to copy the data }
  890. g_concatcopy(list,ref,href,tcgsize2size[size]);
  891. end;
  892. else
  893. internalerror(200402201);
  894. end;
  895. end;
  896. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; const ref: TReference);
  897. var
  898. tmpreg : tregister;
  899. begin
  900. tmpreg:=getintregister(list,size);
  901. a_load_ref_reg(list,size,size,ref,tmpreg);
  902. a_op_const_reg(list,op,size,a,tmpreg);
  903. a_load_reg_ref(list,size,size,tmpreg,ref);
  904. end;
  905. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; a: aint; const loc: tlocation);
  906. begin
  907. case loc.loc of
  908. LOC_REGISTER, LOC_CREGISTER:
  909. a_op_const_reg(list,op,loc.size,a,loc.register);
  910. LOC_REFERENCE, LOC_CREFERENCE:
  911. a_op_const_ref(list,op,loc.size,a,loc.reference);
  912. else
  913. internalerror(200109061);
  914. end;
  915. end;
  916. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  917. var
  918. tmpreg : tregister;
  919. begin
  920. tmpreg:=getintregister(list,size);
  921. a_load_ref_reg(list,size,size,ref,tmpreg);
  922. a_op_reg_reg(list,op,size,reg,tmpreg);
  923. a_load_reg_ref(list,size,size,tmpreg,ref);
  924. end;
  925. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  926. var
  927. tmpreg: tregister;
  928. begin
  929. case op of
  930. OP_NOT,OP_NEG:
  931. { handle it as "load ref,reg; op reg" }
  932. begin
  933. a_load_ref_reg(list,size,size,ref,reg);
  934. a_op_reg_reg(list,op,size,reg,reg);
  935. end;
  936. else
  937. begin
  938. tmpreg:=getintregister(list,size);
  939. a_load_ref_reg(list,size,size,ref,tmpreg);
  940. a_op_reg_reg(list,op,size,tmpreg,reg);
  941. end;
  942. end;
  943. end;
  944. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  945. begin
  946. case loc.loc of
  947. LOC_REGISTER, LOC_CREGISTER:
  948. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  949. LOC_REFERENCE, LOC_CREFERENCE:
  950. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  951. else
  952. internalerror(200109061);
  953. end;
  954. end;
  955. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  956. var
  957. tmpreg: tregister;
  958. begin
  959. case loc.loc of
  960. LOC_REGISTER,LOC_CREGISTER:
  961. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  962. LOC_REFERENCE,LOC_CREFERENCE:
  963. begin
  964. tmpreg:=getintregister(list,loc.size);
  965. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  966. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  967. end;
  968. else
  969. internalerror(200109061);
  970. end;
  971. end;
  972. procedure Tcg.a_op_const_reg_reg(list:Taasmoutput;op:Topcg;size:Tcgsize;
  973. a:aint;src,dst:Tregister);
  974. begin
  975. a_load_reg_reg(list,size,size,src,dst);
  976. a_op_const_reg(list,op,size,a,dst);
  977. end;
  978. procedure tcg.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  979. size: tcgsize; src1, src2, dst: tregister);
  980. var
  981. tmpreg: tregister;
  982. begin
  983. if (dst<>src1) then
  984. begin
  985. a_load_reg_reg(list,size,size,src2,dst);
  986. a_op_reg_reg(list,op,size,src1,dst);
  987. end
  988. else
  989. begin
  990. tmpreg:=getintregister(list,size);
  991. a_load_reg_reg(list,size,size,src2,tmpreg);
  992. a_op_reg_reg(list,op,size,src1,tmpreg);
  993. a_load_reg_reg(list,size,size,tmpreg,dst);
  994. end;
  995. end;
  996. procedure tcg.a_op_const_reg_reg_checkoverflow(list: taasmoutput; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  997. begin
  998. a_op_const_reg_reg(list,op,size,a,src,dst);
  999. ovloc.loc:=LOC_VOID;
  1000. end;
  1001. procedure tcg.a_op_reg_reg_reg_checkoverflow(list: taasmoutput; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  1002. begin
  1003. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1004. ovloc.loc:=LOC_VOID;
  1005. end;
  1006. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  1007. l : tasmlabel);
  1008. var
  1009. tmpreg: tregister;
  1010. begin
  1011. tmpreg:=getintregister(list,size);
  1012. a_load_ref_reg(list,size,size,ref,tmpreg);
  1013. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  1014. end;
  1015. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;const loc : tlocation;
  1016. l : tasmlabel);
  1017. begin
  1018. case loc.loc of
  1019. LOC_REGISTER,LOC_CREGISTER:
  1020. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  1021. LOC_REFERENCE,LOC_CREFERENCE:
  1022. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  1023. else
  1024. internalerror(200109061);
  1025. end;
  1026. end;
  1027. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  1028. var
  1029. tmpreg: tregister;
  1030. begin
  1031. tmpreg:=getintregister(list,size);
  1032. a_load_ref_reg(list,size,size,ref,tmpreg);
  1033. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1034. end;
  1035. procedure tcg.a_cmp_reg_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; reg : tregister; const ref: treference; l : tasmlabel);
  1036. var
  1037. tmpreg: tregister;
  1038. begin
  1039. tmpreg:=getintregister(list,size);
  1040. a_load_ref_reg(list,size,size,ref,tmpreg);
  1041. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  1042. end;
  1043. procedure tcg.a_cmp_reg_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; reg: tregister; const loc: tlocation; l : tasmlabel);
  1044. begin
  1045. a_cmp_loc_reg_label(list,size,swap_opcmp(cmp_op),loc,reg,l);
  1046. end;
  1047. procedure tcg.a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  1048. begin
  1049. case loc.loc of
  1050. LOC_REGISTER,
  1051. LOC_CREGISTER:
  1052. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1053. LOC_REFERENCE,
  1054. LOC_CREFERENCE :
  1055. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1056. LOC_CONSTANT:
  1057. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1058. else
  1059. internalerror(200203231);
  1060. end;
  1061. end;
  1062. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1063. l : tasmlabel);
  1064. var
  1065. tmpreg: tregister;
  1066. begin
  1067. case loc.loc of
  1068. LOC_REGISTER,LOC_CREGISTER:
  1069. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1070. LOC_REFERENCE,LOC_CREFERENCE:
  1071. begin
  1072. tmpreg:=getintregister(list,size);
  1073. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1074. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1075. end
  1076. else
  1077. internalerror(200109061);
  1078. end;
  1079. end;
  1080. procedure tcg.a_loadmm_loc_reg(list: taasmoutput; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  1081. begin
  1082. case loc.loc of
  1083. LOC_MMREGISTER,LOC_CMMREGISTER:
  1084. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1085. LOC_REFERENCE,LOC_CREFERENCE:
  1086. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  1087. else
  1088. internalerror(200310121);
  1089. end;
  1090. end;
  1091. procedure tcg.a_loadmm_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  1092. begin
  1093. case loc.loc of
  1094. LOC_MMREGISTER,LOC_CMMREGISTER:
  1095. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  1096. LOC_REFERENCE,LOC_CREFERENCE:
  1097. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  1098. else
  1099. internalerror(200310122);
  1100. end;
  1101. end;
  1102. procedure tcg.a_parammm_reg(list: taasmoutput; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle);
  1103. var
  1104. href : treference;
  1105. begin
  1106. cgpara.check_simple_location;
  1107. case cgpara.location^.loc of
  1108. LOC_MMREGISTER,LOC_CMMREGISTER:
  1109. a_loadmm_reg_reg(list,size,cgpara.location^.size,reg,cgpara.location^.register,shuffle);
  1110. LOC_REFERENCE,LOC_CREFERENCE:
  1111. begin
  1112. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  1113. a_loadmm_reg_ref(list,size,cgpara.location^.size,reg,href,shuffle);
  1114. end
  1115. else
  1116. internalerror(200310123);
  1117. end;
  1118. end;
  1119. procedure tcg.a_parammm_ref(list: taasmoutput; size: tcgsize;const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle);
  1120. var
  1121. hr : tregister;
  1122. hs : tmmshuffle;
  1123. begin
  1124. cgpara.check_simple_location;
  1125. hr:=getmmregister(list,cgpara.location^.size);
  1126. a_loadmm_ref_reg(list,size,cgpara.location^.size,ref,hr,shuffle);
  1127. if realshuffle(shuffle) then
  1128. begin
  1129. hs:=shuffle^;
  1130. removeshuffles(hs);
  1131. a_parammm_reg(list,cgpara.location^.size,hr,cgpara,@hs);
  1132. end
  1133. else
  1134. a_parammm_reg(list,cgpara.location^.size,hr,cgpara,shuffle);
  1135. end;
  1136. procedure tcg.a_parammm_loc(list: taasmoutput;const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle);
  1137. begin
  1138. case loc.loc of
  1139. LOC_MMREGISTER,LOC_CMMREGISTER:
  1140. a_parammm_reg(list,loc.size,loc.register,cgpara,shuffle);
  1141. LOC_REFERENCE,LOC_CREFERENCE:
  1142. a_parammm_ref(list,loc.size,loc.reference,cgpara,shuffle);
  1143. else
  1144. internalerror(200310123);
  1145. end;
  1146. end;
  1147. procedure tcg.a_opmm_ref_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1148. var
  1149. hr : tregister;
  1150. hs : tmmshuffle;
  1151. begin
  1152. hr:=getmmregister(list,size);
  1153. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1154. if realshuffle(shuffle) then
  1155. begin
  1156. hs:=shuffle^;
  1157. removeshuffles(hs);
  1158. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  1159. end
  1160. else
  1161. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  1162. end;
  1163. procedure tcg.a_opmm_reg_ref(list: taasmoutput; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  1164. var
  1165. hr : tregister;
  1166. hs : tmmshuffle;
  1167. begin
  1168. hr:=getmmregister(list,size);
  1169. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1170. if realshuffle(shuffle) then
  1171. begin
  1172. hs:=shuffle^;
  1173. removeshuffles(hs);
  1174. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  1175. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  1176. end
  1177. else
  1178. begin
  1179. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  1180. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  1181. end;
  1182. end;
  1183. procedure tcg.a_opmm_loc_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  1184. begin
  1185. case loc.loc of
  1186. LOC_CMMREGISTER,LOC_MMREGISTER:
  1187. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  1188. LOC_CREFERENCE,LOC_REFERENCE:
  1189. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  1190. else
  1191. internalerror(200312232);
  1192. end;
  1193. end;
  1194. procedure tcg.g_concatcopy_unaligned(list : taasmoutput;const source,dest : treference;len : aint);
  1195. begin
  1196. g_concatcopy(list,source,dest,len);
  1197. end;
  1198. procedure tcg.g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte);
  1199. var
  1200. cgpara1,cgpara2,cgpara3 : TCGPara;
  1201. begin
  1202. cgpara1.init;
  1203. cgpara2.init;
  1204. cgpara3.init;
  1205. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1206. paramanager.getintparaloc(pocall_default,2,cgpara2);
  1207. paramanager.getintparaloc(pocall_default,3,cgpara3);
  1208. paramanager.allocparaloc(list,cgpara3);
  1209. a_paramaddr_ref(list,dest,cgpara3);
  1210. paramanager.allocparaloc(list,cgpara2);
  1211. a_paramaddr_ref(list,source,cgpara2);
  1212. paramanager.allocparaloc(list,cgpara1);
  1213. a_param_const(list,OS_INT,len,cgpara1);
  1214. paramanager.freeparaloc(list,cgpara3);
  1215. paramanager.freeparaloc(list,cgpara2);
  1216. paramanager.freeparaloc(list,cgpara1);
  1217. allocallcpuregisters(list);
  1218. a_call_name(list,'FPC_SHORTSTR_ASSIGN');
  1219. deallocallcpuregisters(list);
  1220. cgpara3.done;
  1221. cgpara2.done;
  1222. cgpara1.done;
  1223. end;
  1224. procedure tcg.g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  1225. var
  1226. href : treference;
  1227. incrfunc : string;
  1228. cgpara1,cgpara2 : TCGPara;
  1229. begin
  1230. cgpara1.init;
  1231. cgpara2.init;
  1232. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1233. paramanager.getintparaloc(pocall_default,2,cgpara2);
  1234. if is_interfacecom(t) then
  1235. incrfunc:='FPC_INTF_INCR_REF'
  1236. else if is_ansistring(t) then
  1237. incrfunc:='FPC_ANSISTR_INCR_REF'
  1238. else if is_widestring(t) then
  1239. incrfunc:='FPC_WIDESTR_INCR_REF'
  1240. else if is_dynamic_array(t) then
  1241. incrfunc:='FPC_DYNARRAY_INCR_REF'
  1242. else
  1243. incrfunc:='';
  1244. { call the special incr function or the generic addref }
  1245. if incrfunc<>'' then
  1246. begin
  1247. paramanager.allocparaloc(list,cgpara1);
  1248. { these functions get the pointer by value }
  1249. a_param_ref(list,OS_ADDR,ref,cgpara1);
  1250. paramanager.freeparaloc(list,cgpara1);
  1251. allocallcpuregisters(list);
  1252. a_call_name(list,incrfunc);
  1253. deallocallcpuregisters(list);
  1254. end
  1255. else
  1256. begin
  1257. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1258. paramanager.allocparaloc(list,cgpara2);
  1259. a_paramaddr_ref(list,href,cgpara2);
  1260. paramanager.allocparaloc(list,cgpara1);
  1261. a_paramaddr_ref(list,ref,cgpara1);
  1262. paramanager.freeparaloc(list,cgpara1);
  1263. paramanager.freeparaloc(list,cgpara2);
  1264. allocallcpuregisters(list);
  1265. a_call_name(list,'FPC_ADDREF');
  1266. deallocallcpuregisters(list);
  1267. end;
  1268. cgpara2.done;
  1269. cgpara1.done;
  1270. end;
  1271. procedure tcg.g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  1272. var
  1273. href : treference;
  1274. decrfunc : string;
  1275. needrtti : boolean;
  1276. cgpara1,cgpara2 : TCGPara;
  1277. tempreg1,tempreg2 : TRegister;
  1278. begin
  1279. cgpara1.init;
  1280. cgpara2.init;
  1281. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1282. paramanager.getintparaloc(pocall_default,2,cgpara2);
  1283. needrtti:=false;
  1284. if is_interfacecom(t) then
  1285. decrfunc:='FPC_INTF_DECR_REF'
  1286. else if is_ansistring(t) then
  1287. decrfunc:='FPC_ANSISTR_DECR_REF'
  1288. else if is_widestring(t) then
  1289. decrfunc:='FPC_WIDESTR_DECR_REF'
  1290. else if is_dynamic_array(t) then
  1291. begin
  1292. decrfunc:='FPC_DYNARRAY_DECR_REF';
  1293. needrtti:=true;
  1294. end
  1295. else
  1296. decrfunc:='';
  1297. { call the special decr function or the generic decref }
  1298. if decrfunc<>'' then
  1299. begin
  1300. if needrtti then
  1301. begin
  1302. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1303. tempreg2:=getaddressregister(list);
  1304. a_loadaddr_ref_reg(list,href,tempreg2);
  1305. end;
  1306. tempreg1:=getaddressregister(list);
  1307. a_loadaddr_ref_reg(list,ref,tempreg1);
  1308. if needrtti then
  1309. begin
  1310. paramanager.allocparaloc(list,cgpara2);
  1311. a_param_reg(list,OS_ADDR,tempreg2,cgpara2);
  1312. paramanager.freeparaloc(list,cgpara2);
  1313. end;
  1314. paramanager.allocparaloc(list,cgpara1);
  1315. a_param_reg(list,OS_ADDR,tempreg1,cgpara1);
  1316. paramanager.freeparaloc(list,cgpara1);
  1317. allocallcpuregisters(list);
  1318. a_call_name(list,decrfunc);
  1319. deallocallcpuregisters(list);
  1320. end
  1321. else
  1322. begin
  1323. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1324. paramanager.allocparaloc(list,cgpara2);
  1325. a_paramaddr_ref(list,href,cgpara2);
  1326. paramanager.allocparaloc(list,cgpara1);
  1327. a_paramaddr_ref(list,ref,cgpara1);
  1328. paramanager.freeparaloc(list,cgpara1);
  1329. paramanager.freeparaloc(list,cgpara2);
  1330. allocallcpuregisters(list);
  1331. a_call_name(list,'FPC_DECREF');
  1332. deallocallcpuregisters(list);
  1333. end;
  1334. cgpara2.done;
  1335. cgpara1.done;
  1336. end;
  1337. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference);
  1338. var
  1339. href : treference;
  1340. cgpara1,cgpara2 : TCGPara;
  1341. begin
  1342. cgpara1.init;
  1343. cgpara2.init;
  1344. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1345. paramanager.getintparaloc(pocall_default,2,cgpara2);
  1346. if is_ansistring(t) or
  1347. is_widestring(t) or
  1348. is_interfacecom(t) or
  1349. is_dynamic_array(t) then
  1350. a_load_const_ref(list,OS_ADDR,0,ref)
  1351. else
  1352. begin
  1353. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1354. paramanager.allocparaloc(list,cgpara2);
  1355. a_paramaddr_ref(list,href,cgpara2);
  1356. paramanager.allocparaloc(list,cgpara1);
  1357. a_paramaddr_ref(list,ref,cgpara1);
  1358. paramanager.freeparaloc(list,cgpara1);
  1359. paramanager.freeparaloc(list,cgpara2);
  1360. allocallcpuregisters(list);
  1361. a_call_name(list,'FPC_INITIALIZE');
  1362. deallocallcpuregisters(list);
  1363. end;
  1364. cgpara1.done;
  1365. cgpara2.done;
  1366. end;
  1367. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference);
  1368. var
  1369. href : treference;
  1370. cgpara1,cgpara2 : TCGPara;
  1371. begin
  1372. cgpara1.init;
  1373. cgpara2.init;
  1374. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1375. paramanager.getintparaloc(pocall_default,2,cgpara2);
  1376. if is_ansistring(t) or
  1377. is_widestring(t) or
  1378. is_interfacecom(t) then
  1379. begin
  1380. g_decrrefcount(list,t,ref);
  1381. a_load_const_ref(list,OS_ADDR,0,ref);
  1382. end
  1383. else
  1384. begin
  1385. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1386. paramanager.allocparaloc(list,cgpara2);
  1387. a_paramaddr_ref(list,href,cgpara2);
  1388. paramanager.allocparaloc(list,cgpara1);
  1389. a_paramaddr_ref(list,ref,cgpara1);
  1390. paramanager.freeparaloc(list,cgpara1);
  1391. paramanager.freeparaloc(list,cgpara2);
  1392. allocallcpuregisters(list);
  1393. a_call_name(list,'FPC_FINALIZE');
  1394. deallocallcpuregisters(list);
  1395. end;
  1396. cgpara1.done;
  1397. cgpara2.done;
  1398. end;
  1399. procedure tcg.g_rangecheck(list: taasmoutput; const l:tlocation;fromdef,todef: tdef);
  1400. { generate range checking code for the value at location p. The type }
  1401. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  1402. { is the original type used at that location. When both defs are equal }
  1403. { the check is also insert (needed for succ,pref,inc,dec) }
  1404. const
  1405. aintmax=high(aint);
  1406. var
  1407. neglabel : tasmlabel;
  1408. hreg : tregister;
  1409. lto,hto,
  1410. lfrom,hfrom : TConstExprInt;
  1411. from_signed: boolean;
  1412. begin
  1413. { range checking on and range checkable value? }
  1414. if not(cs_check_range in aktlocalswitches) or
  1415. not(fromdef.deftype in [orddef,enumdef,arraydef]) then
  1416. exit;
  1417. {$ifndef cpu64bit}
  1418. { handle 64bit rangechecks separate for 32bit processors }
  1419. if is_64bit(fromdef) or is_64bit(todef) then
  1420. begin
  1421. cg64.g_rangecheck64(list,l,fromdef,todef);
  1422. exit;
  1423. end;
  1424. {$endif cpu64bit}
  1425. { only check when assigning to scalar, subranges are different, }
  1426. { when todef=fromdef then the check is always generated }
  1427. getrange(fromdef,lfrom,hfrom);
  1428. getrange(todef,lto,hto);
  1429. from_signed := is_signed(fromdef);
  1430. { no range check if from and to are equal and are both longint/dword }
  1431. { (if we have a 32bit processor) or int64/qword, since such }
  1432. { operations can at most cause overflows (JM) }
  1433. { Note that these checks are mostly processor independent, they only }
  1434. { have to be changed once we introduce 64bit subrange types }
  1435. {$ifdef cpu64bit}
  1436. if (fromdef = todef) and
  1437. (fromdef.deftype=orddef) and
  1438. (((((torddef(fromdef).typ = s64bit) and
  1439. (lfrom = low(int64)) and
  1440. (hfrom = high(int64))) or
  1441. ((torddef(fromdef).typ = u64bit) and
  1442. (lfrom = low(qword)) and
  1443. (hfrom = high(qword)))))) then
  1444. exit;
  1445. {$else cpu64bit}
  1446. if (fromdef = todef) and
  1447. (fromdef.deftype=orddef) and
  1448. (((((torddef(fromdef).typ = s32bit) and
  1449. (lfrom = low(longint)) and
  1450. (hfrom = high(longint))) or
  1451. ((torddef(fromdef).typ = u32bit) and
  1452. (lfrom = low(cardinal)) and
  1453. (hfrom = high(cardinal)))))) then
  1454. exit;
  1455. {$endif cpu64bit}
  1456. { if the from-range falls completely in the to-range, no check }
  1457. { is necessary. Don't do this conversion for the largest unsigned type }
  1458. if (todef<>fromdef) and
  1459. (from_signed or (hfrom>=0)) and
  1460. (lto<=lfrom) and (hto>=hfrom) then
  1461. exit;
  1462. { generate the rangecheck code for the def where we are going to }
  1463. { store the result }
  1464. { use the trick that }
  1465. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  1466. { To be able to do that, we have to make sure however that either }
  1467. { fromdef and todef are both signed or unsigned, or that we leave }
  1468. { the parts < 0 and > maxlongint out }
  1469. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  1470. if from_signed xor is_signed(todef) then
  1471. begin
  1472. if from_signed then
  1473. { from is signed, to is unsigned }
  1474. begin
  1475. { if high(from) < 0 -> always range error }
  1476. if (hfrom < 0) or
  1477. { if low(to) > maxlongint also range error }
  1478. (lto > aintmax) then
  1479. begin
  1480. a_call_name(list,'FPC_RANGEERROR');
  1481. exit
  1482. end;
  1483. { from is signed and to is unsigned -> when looking at to }
  1484. { as an signed value, it must be < maxaint (otherwise }
  1485. { it will become negative, which is invalid since "to" is unsigned) }
  1486. if hto > aintmax then
  1487. hto := aintmax;
  1488. end
  1489. else
  1490. { from is unsigned, to is signed }
  1491. begin
  1492. if (lfrom > aintmax) or
  1493. (hto < 0) then
  1494. begin
  1495. a_call_name(list,'FPC_RANGEERROR');
  1496. exit
  1497. end;
  1498. { from is unsigned and to is signed -> when looking at to }
  1499. { as an unsigned value, it must be >= 0 (since negative }
  1500. { values are the same as values > maxlongint) }
  1501. if lto < 0 then
  1502. lto := 0;
  1503. end;
  1504. end;
  1505. hreg:=getintregister(list,OS_INT);
  1506. a_load_loc_reg(list,OS_INT,l,hreg);
  1507. a_op_const_reg(list,OP_SUB,OS_INT,aint(lto),hreg);
  1508. objectlibrary.getjumplabel(neglabel);
  1509. {
  1510. if from_signed then
  1511. a_cmp_const_reg_label(list,OS_INT,OC_GTE,aint(hto-lto),hreg,neglabel)
  1512. else
  1513. }
  1514. {$ifdef cpu64bit}
  1515. if qword(hto-lto)>qword(aintmax) then
  1516. a_cmp_const_reg_label(list,OS_INT,OC_BE,aintmax,hreg,neglabel)
  1517. else
  1518. {$endif cpu64bit}
  1519. a_cmp_const_reg_label(list,OS_INT,OC_BE,aint(hto-lto),hreg,neglabel);
  1520. a_call_name(list,'FPC_RANGEERROR');
  1521. a_label(list,neglabel);
  1522. end;
  1523. procedure tcg.g_overflowCheck_loc(List:TAasmOutput;const Loc:TLocation;def:TDef;ovloc : tlocation);
  1524. begin
  1525. g_overflowCheck(list,loc,def);
  1526. end;
  1527. procedure tcg.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference);
  1528. var
  1529. tmpreg : tregister;
  1530. begin
  1531. tmpreg:=getintregister(list,size);
  1532. g_flags2reg(list,size,f,tmpreg);
  1533. a_load_reg_ref(list,size,size,tmpreg,ref);
  1534. end;
  1535. procedure tcg.g_maybe_testself(list : taasmoutput;reg:tregister);
  1536. var
  1537. OKLabel : tasmlabel;
  1538. cgpara1 : TCGPara;
  1539. begin
  1540. if (cs_check_object in aktlocalswitches) or
  1541. (cs_check_range in aktlocalswitches) then
  1542. begin
  1543. objectlibrary.getjumplabel(oklabel);
  1544. a_cmp_const_reg_label(list,OS_ADDR,OC_NE,0,reg,oklabel);
  1545. cgpara1.init;
  1546. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1547. paramanager.allocparaloc(list,cgpara1);
  1548. a_param_const(list,OS_INT,210,cgpara1);
  1549. paramanager.freeparaloc(list,cgpara1);
  1550. a_call_name(list,'FPC_HANDLEERROR');
  1551. a_label(list,oklabel);
  1552. cgpara1.done;
  1553. end;
  1554. end;
  1555. procedure tcg.g_maybe_testvmt(list : taasmoutput;reg:tregister;objdef:tobjectdef);
  1556. var
  1557. hrefvmt : treference;
  1558. cgpara1,cgpara2 : TCGPara;
  1559. begin
  1560. cgpara1.init;
  1561. cgpara2.init;
  1562. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1563. paramanager.getintparaloc(pocall_default,2,cgpara2);
  1564. if (cs_check_object in aktlocalswitches) then
  1565. begin
  1566. reference_reset_symbol(hrefvmt,objectlibrary.newasmsymbol(objdef.vmt_mangledname,AB_EXTERNAL,AT_DATA),0);
  1567. paramanager.allocparaloc(list,cgpara2);
  1568. a_paramaddr_ref(list,hrefvmt,cgpara2);
  1569. paramanager.allocparaloc(list,cgpara1);
  1570. a_param_reg(list,OS_ADDR,reg,cgpara1);
  1571. paramanager.freeparaloc(list,cgpara1);
  1572. paramanager.freeparaloc(list,cgpara2);
  1573. allocallcpuregisters(list);
  1574. a_call_name(list,'FPC_CHECK_OBJECT_EXT');
  1575. deallocallcpuregisters(list);
  1576. end
  1577. else
  1578. if (cs_check_range in aktlocalswitches) then
  1579. begin
  1580. paramanager.allocparaloc(list,cgpara1);
  1581. a_param_reg(list,OS_ADDR,reg,cgpara1);
  1582. paramanager.freeparaloc(list,cgpara1);
  1583. allocallcpuregisters(list);
  1584. a_call_name(list,'FPC_CHECK_OBJECT');
  1585. deallocallcpuregisters(list);
  1586. end;
  1587. cgpara1.done;
  1588. cgpara2.done;
  1589. end;
  1590. {*****************************************************************************
  1591. Entry/Exit Code Functions
  1592. *****************************************************************************}
  1593. procedure tcg.g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;const lenloc:tlocation;elesize:aint;destreg:tregister);
  1594. var
  1595. sizereg,sourcereg,lenreg : tregister;
  1596. cgpara1,cgpara2,cgpara3 : TCGPara;
  1597. begin
  1598. { because some abis don't support dynamic stack allocation properly
  1599. open array value parameters are copied onto the heap
  1600. }
  1601. { calculate necessary memory }
  1602. { read/write operations on one register make the life of the register allocator hard }
  1603. if not(lenloc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1604. begin
  1605. lenreg:=getintregister(list,OS_INT);
  1606. a_load_loc_reg(list,OS_INT,lenloc,lenreg);
  1607. end
  1608. else
  1609. lenreg:=lenloc.register;
  1610. sizereg:=getintregister(list,OS_INT);
  1611. a_op_const_reg_reg(list,OP_ADD,OS_INT,1,lenreg,sizereg);
  1612. a_op_const_reg(list,OP_IMUL,OS_INT,elesize,sizereg);
  1613. { load source }
  1614. sourcereg:=getaddressregister(list);
  1615. a_loadaddr_ref_reg(list,ref,sourcereg);
  1616. { do getmem call }
  1617. cgpara1.init;
  1618. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1619. paramanager.allocparaloc(list,cgpara1);
  1620. a_param_reg(list,OS_INT,sizereg,cgpara1);
  1621. paramanager.freeparaloc(list,cgpara1);
  1622. allocallcpuregisters(list);
  1623. a_call_name(list,'FPC_GETMEM');
  1624. deallocallcpuregisters(list);
  1625. cgpara1.done;
  1626. { return the new address }
  1627. a_load_reg_reg(list,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,destreg);
  1628. { do move call }
  1629. cgpara1.init;
  1630. cgpara2.init;
  1631. cgpara3.init;
  1632. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1633. paramanager.getintparaloc(pocall_default,2,cgpara2);
  1634. paramanager.getintparaloc(pocall_default,3,cgpara3);
  1635. { load size }
  1636. paramanager.allocparaloc(list,cgpara3);
  1637. a_param_reg(list,OS_INT,sizereg,cgpara3);
  1638. { load destination }
  1639. paramanager.allocparaloc(list,cgpara2);
  1640. a_param_reg(list,OS_ADDR,destreg,cgpara2);
  1641. { load source }
  1642. paramanager.allocparaloc(list,cgpara1);
  1643. a_param_reg(list,OS_ADDR,sourcereg,cgpara1);
  1644. paramanager.freeparaloc(list,cgpara3);
  1645. paramanager.freeparaloc(list,cgpara2);
  1646. paramanager.freeparaloc(list,cgpara1);
  1647. allocallcpuregisters(list);
  1648. a_call_name(list,'FPC_MOVE');
  1649. deallocallcpuregisters(list);
  1650. cgpara3.done;
  1651. cgpara2.done;
  1652. cgpara1.done;
  1653. end;
  1654. procedure tcg.g_releasevaluepara_openarray(list : taasmoutput;const l:tlocation);
  1655. var
  1656. cgpara1 : TCGPara;
  1657. begin
  1658. { do move call }
  1659. cgpara1.init;
  1660. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1661. { load source }
  1662. paramanager.allocparaloc(list,cgpara1);
  1663. a_param_loc(list,l,cgpara1);
  1664. paramanager.freeparaloc(list,cgpara1);
  1665. allocallcpuregisters(list);
  1666. a_call_name(list,'FPC_FREEMEM');
  1667. deallocallcpuregisters(list);
  1668. cgpara1.done;
  1669. end;
  1670. procedure tcg.g_save_standard_registers(list:Taasmoutput);
  1671. var
  1672. href : treference;
  1673. size : longint;
  1674. r : integer;
  1675. begin
  1676. { Get temp }
  1677. size:=0;
  1678. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1679. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1680. inc(size,sizeof(aint));
  1681. if size>0 then
  1682. begin
  1683. tg.GetTemp(list,size,tt_noreuse,current_procinfo.save_regs_ref);
  1684. { Copy registers to temp }
  1685. href:=current_procinfo.save_regs_ref;
  1686. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1687. begin
  1688. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1689. begin
  1690. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE),href);
  1691. inc(href.offset,sizeof(aint));
  1692. end;
  1693. include(rg[R_INTREGISTER].preserved_by_proc,saved_standard_registers[r]);
  1694. end;
  1695. end;
  1696. end;
  1697. procedure tcg.g_restore_standard_registers(list:Taasmoutput);
  1698. var
  1699. href : treference;
  1700. r : integer;
  1701. hreg : tregister;
  1702. begin
  1703. { Copy registers from temp }
  1704. href:=current_procinfo.save_regs_ref;
  1705. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1706. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1707. begin
  1708. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  1709. { Allocate register so the optimizer does not remove the load }
  1710. a_reg_alloc(list,hreg);
  1711. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  1712. inc(href.offset,sizeof(aint));
  1713. end;
  1714. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  1715. end;
  1716. procedure tcg.g_profilecode(list : taasmoutput);
  1717. begin
  1718. end;
  1719. procedure tcg.g_exception_reason_save(list : taasmoutput; const href : treference);
  1720. begin
  1721. a_load_reg_ref(list, OS_INT, OS_INT, NR_FUNCTION_RESULT_REG, href);
  1722. end;
  1723. procedure tcg.g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aint);
  1724. begin
  1725. a_load_const_ref(list, OS_INT, a, href);
  1726. end;
  1727. procedure tcg.g_exception_reason_load(list : taasmoutput; const href : treference);
  1728. begin
  1729. a_load_ref_reg(list, OS_INT, OS_INT, href, NR_FUNCTION_RESULT_REG);
  1730. end;
  1731. procedure tcg.g_adjust_self_value(list:taasmoutput;procdef: tprocdef;ioffset: aint);
  1732. var
  1733. hsym : tsym;
  1734. href : treference;
  1735. paraloc : tcgparalocation;
  1736. begin
  1737. { calculate the parameter info for the procdef }
  1738. if not procdef.has_paraloc_info then
  1739. begin
  1740. procdef.requiredargarea:=paramanager.create_paraloc_info(procdef,callerside);
  1741. procdef.has_paraloc_info:=true;
  1742. end;
  1743. hsym:=tsym(procdef.parast.search('self'));
  1744. if not(assigned(hsym) and
  1745. (hsym.typ=paravarsym)) then
  1746. internalerror(200305251);
  1747. paraloc:=tparavarsym(hsym).paraloc[callerside].location^;
  1748. case paraloc.loc of
  1749. LOC_REGISTER:
  1750. cg.a_op_const_reg(list,OP_SUB,paraloc.size,ioffset,paraloc.register);
  1751. LOC_REFERENCE:
  1752. begin
  1753. { offset in the wrapper needs to be adjusted for the stored
  1754. return address }
  1755. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset+sizeof(aint));
  1756. cg.a_op_const_ref(list,OP_SUB,paraloc.size,ioffset,href);
  1757. end
  1758. else
  1759. internalerror(200309189);
  1760. end;
  1761. end;
  1762. procedure tcg.a_call_name_static(list : taasmoutput;const s : string);
  1763. begin
  1764. a_call_name(list,s);
  1765. end;
  1766. {*****************************************************************************
  1767. TCG64
  1768. *****************************************************************************}
  1769. {$ifndef cpu64bit}
  1770. procedure tcg64.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;size : tcgsize;value : int64; regsrc,regdst : tregister64);
  1771. begin
  1772. a_load64_reg_reg(list,regsrc,regdst);
  1773. a_op64_const_reg(list,op,size,value,regdst);
  1774. end;
  1775. procedure tcg64.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  1776. var
  1777. tmpreg64 : tregister64;
  1778. begin
  1779. { when src1=dst then we need to first create a temp to prevent
  1780. overwriting src1 with src2 }
  1781. if (regsrc1.reghi=regdst.reghi) or
  1782. (regsrc1.reglo=regdst.reghi) or
  1783. (regsrc1.reghi=regdst.reglo) or
  1784. (regsrc1.reglo=regdst.reglo) then
  1785. begin
  1786. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  1787. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  1788. a_load64_reg_reg(list,regsrc2,tmpreg64);
  1789. a_op64_reg_reg(list,op,size,regsrc1,tmpreg64);
  1790. a_load64_reg_reg(list,tmpreg64,regdst);
  1791. end
  1792. else
  1793. begin
  1794. a_load64_reg_reg(list,regsrc2,regdst);
  1795. a_op64_reg_reg(list,op,size,regsrc1,regdst);
  1796. end;
  1797. end;
  1798. procedure tcg64.a_op64_const_reg_reg_checkoverflow(list: taasmoutput;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  1799. begin
  1800. a_op64_const_reg_reg(list,op,size,value,regsrc,regdst);
  1801. ovloc.loc:=LOC_VOID;
  1802. end;
  1803. procedure tcg64.a_op64_reg_reg_reg_checkoverflow(list: taasmoutput;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  1804. begin
  1805. a_op64_reg_reg_reg(list,op,size,regsrc1,regsrc2,regdst);
  1806. ovloc.loc:=LOC_VOID;
  1807. end;
  1808. {$endif cpu64bit}
  1809. initialization
  1810. ;
  1811. finalization
  1812. cg.free;
  1813. {$ifndef cpu64bit}
  1814. cg64.free;
  1815. {$endif cpu64bit}
  1816. end.