cgobj.pas 87 KB

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