cgobj.pas 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172
  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,cpuinfo,cgbase,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:cardinal;
  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 ref:treference);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. { tlocation handling }
  404. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  405. procedure location_freetemp(list: taasmoutput; const l : tlocation);
  406. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  407. procedure location_swap(var destloc,sourceloc : tlocation);
  408. var
  409. {# Main code generator class }
  410. cg : tcg;
  411. {$ifndef cpu64bit}
  412. {# Code generator class for all operations working with 64-Bit operands }
  413. cg64 : tcg64;
  414. {$endif cpu64bit}
  415. implementation
  416. uses
  417. globals,options,systems,
  418. verbose,defutil,paramgr,
  419. tgobj,cutils,procinfo,
  420. cgutils;
  421. const
  422. { Please leave this here, this module should NOT use
  423. exprasmlist, the lists are always passed as arguments.
  424. Declaring it as string here results in an error when compiling (PFV) }
  425. exprasmlist = 'error';
  426. {*****************************************************************************
  427. basic functionallity
  428. ******************************************************************************}
  429. constructor tcg.create;
  430. begin
  431. end;
  432. {*****************************************************************************
  433. register allocation
  434. ******************************************************************************}
  435. procedure tcg.init_register_allocators;
  436. begin
  437. fillchar(rg,sizeof(rg),0);
  438. add_reg_instruction_hook:=@add_reg_instruction;
  439. end;
  440. procedure tcg.done_register_allocators;
  441. begin
  442. { Safety }
  443. fillchar(rg,sizeof(rg),0);
  444. add_reg_instruction_hook:=nil;
  445. end;
  446. {$ifdef flowgraph}
  447. procedure Tcg.init_flowgraph;
  448. begin
  449. aktflownode:=0;
  450. end;
  451. procedure Tcg.done_flowgraph;
  452. begin
  453. end;
  454. {$endif}
  455. function tcg.getintregister(list:Taasmoutput;size:Tcgsize):Tregister;
  456. begin
  457. if not assigned(rg[R_INTREGISTER]) then
  458. internalerror(200312122);
  459. result:=rg[R_INTREGISTER].getregister(list,cgsize2subreg(size));
  460. end;
  461. function tcg.getfpuregister(list:Taasmoutput;size:Tcgsize):Tregister;
  462. begin
  463. if not assigned(rg[R_FPUREGISTER]) then
  464. internalerror(200312123);
  465. result:=rg[R_FPUREGISTER].getregister(list,cgsize2subreg(size));
  466. end;
  467. function tcg.getmmregister(list:Taasmoutput;size:Tcgsize):Tregister;
  468. begin
  469. if not assigned(rg[R_MMREGISTER]) then
  470. internalerror(200312124);
  471. result:=rg[R_MMREGISTER].getregister(list,cgsize2subreg(size));
  472. end;
  473. function tcg.getaddressregister(list:Taasmoutput):Tregister;
  474. begin
  475. if assigned(rg[R_ADDRESSREGISTER]) then
  476. result:=rg[R_ADDRESSREGISTER].getregister(list,R_SUBWHOLE)
  477. else
  478. begin
  479. if not assigned(rg[R_INTREGISTER]) then
  480. internalerror(200312121);
  481. result:=rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  482. end;
  483. end;
  484. function Tcg.makeregsize(list:Taasmoutput;reg:Tregister;size:Tcgsize):Tregister;
  485. var
  486. subreg:Tsubregister;
  487. begin
  488. subreg:=cgsize2subreg(size);
  489. result:=reg;
  490. setsubreg(result,subreg);
  491. { notify RA }
  492. if result<>reg then
  493. list.concat(tai_regalloc.resize(result));
  494. end;
  495. procedure tcg.getcpuregister(list:Taasmoutput;r:Tregister);
  496. begin
  497. if not assigned(rg[getregtype(r)]) then
  498. internalerror(200312125);
  499. rg[getregtype(r)].getcpuregister(list,r);
  500. end;
  501. procedure tcg.ungetcpuregister(list:Taasmoutput;r:Tregister);
  502. begin
  503. if not assigned(rg[getregtype(r)]) then
  504. internalerror(200312126);
  505. rg[getregtype(r)].ungetcpuregister(list,r);
  506. end;
  507. procedure tcg.alloccpuregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);
  508. begin
  509. if assigned(rg[rt]) then
  510. rg[rt].alloccpuregisters(list,r)
  511. else
  512. internalerror(200310092);
  513. end;
  514. procedure tcg.dealloccpuregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);
  515. begin
  516. if assigned(rg[rt]) then
  517. rg[rt].dealloccpuregisters(list,r)
  518. else
  519. internalerror(200310093);
  520. end;
  521. function tcg.uses_registers(rt:Tregistertype):boolean;
  522. begin
  523. if assigned(rg[rt]) then
  524. result:=rg[rt].uses_registers
  525. else
  526. result:=false;
  527. end;
  528. procedure tcg.add_reg_instruction(instr:Tai;r:tregister);
  529. var
  530. rt : tregistertype;
  531. begin
  532. rt:=getregtype(r);
  533. { Only add it when a register allocator is configured.
  534. No IE can be generated, because the VMT is written
  535. without a valid rg[] }
  536. if assigned(rg[rt]) then
  537. rg[rt].add_reg_instruction(instr,r);
  538. end;
  539. procedure tcg.add_move_instruction(instr:Taicpu);
  540. var
  541. rt : tregistertype;
  542. begin
  543. rt:=getregtype(instr.oper[O_MOV_SOURCE]^.reg);
  544. if assigned(rg[rt]) then
  545. rg[rt].add_move_instruction(instr)
  546. else
  547. internalerror(200310095);
  548. end;
  549. procedure tcg.do_register_allocation(list:Taasmoutput;headertai:tai);
  550. var
  551. rt : tregistertype;
  552. begin
  553. for rt:=R_FPUREGISTER to R_SPECIALREGISTER do
  554. begin
  555. if assigned(rg[rt]) then
  556. rg[rt].do_register_allocation(list,headertai);
  557. end;
  558. { running the other register allocator passes could require addition int/addr. registers
  559. when spilling so run int/addr register allocation at the end }
  560. if assigned(rg[R_INTREGISTER]) then
  561. rg[R_INTREGISTER].do_register_allocation(list,headertai);
  562. if assigned(rg[R_ADDRESSREGISTER]) then
  563. rg[R_ADDRESSREGISTER].do_register_allocation(list,headertai);
  564. end;
  565. procedure tcg.a_reg_alloc(list : taasmoutput;r : tregister);
  566. begin
  567. list.concat(tai_regalloc.alloc(r,nil));
  568. end;
  569. procedure tcg.a_reg_dealloc(list : taasmoutput;r : tregister);
  570. begin
  571. list.concat(tai_regalloc.dealloc(r,nil));
  572. end;
  573. procedure tcg.a_reg_sync(list : taasmoutput;r : tregister);
  574. var
  575. instr : tai;
  576. begin
  577. instr:=tai_regalloc.sync(r);
  578. list.concat(instr);
  579. add_reg_instruction(instr,r);
  580. end;
  581. procedure tcg.a_label(list : taasmoutput;l : tasmlabel);
  582. begin
  583. list.concat(tai_label.create(l));
  584. end;
  585. {*****************************************************************************
  586. for better code generation these methods should be overridden
  587. ******************************************************************************}
  588. procedure tcg.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const paraloc : TCGPara);
  589. var
  590. ref : treference;
  591. begin
  592. paraloc.check_simple_location;
  593. case paraloc.location^.loc of
  594. LOC_REGISTER,LOC_CREGISTER:
  595. a_load_reg_reg(list,size,paraloc.location^.size,r,paraloc.location^.register);
  596. LOC_REFERENCE,LOC_CREFERENCE:
  597. begin
  598. reference_reset_base(ref,paraloc.location^.reference.index,paraloc.location^.reference.offset);
  599. a_load_reg_ref(list,size,paraloc.location^.size,r,ref);
  600. end
  601. else
  602. internalerror(2002071004);
  603. end;
  604. end;
  605. procedure tcg.a_param_const(list : taasmoutput;size : tcgsize;a : aint;const paraloc : TCGPara);
  606. var
  607. ref : treference;
  608. begin
  609. paraloc.check_simple_location;
  610. case paraloc.location^.loc of
  611. LOC_REGISTER,LOC_CREGISTER:
  612. a_load_const_reg(list,paraloc.location^.size,a,paraloc.location^.register);
  613. LOC_REFERENCE,LOC_CREFERENCE:
  614. begin
  615. reference_reset_base(ref,paraloc.location^.reference.index,paraloc.location^.reference.offset);
  616. a_load_const_ref(list,paraloc.location^.size,a,ref);
  617. end
  618. else
  619. internalerror(2002071004);
  620. end;
  621. end;
  622. procedure tcg.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const paraloc : TCGPara);
  623. var
  624. ref : treference;
  625. begin
  626. paraloc.check_simple_location;
  627. case paraloc.location^.loc of
  628. LOC_REGISTER,LOC_CREGISTER:
  629. a_load_ref_reg(list,size,paraloc.location^.size,r,paraloc.location^.register);
  630. LOC_REFERENCE,LOC_CREFERENCE:
  631. begin
  632. reference_reset(ref);
  633. ref.base:=paraloc.location^.reference.index;
  634. ref.offset:=paraloc.location^.reference.offset;
  635. { use concatcopy, because it can also be a float which fails when
  636. load_ref_ref is used }
  637. g_concatcopy(list,r,ref,tcgsize2size[size]);
  638. end
  639. else
  640. internalerror(2002071004);
  641. end;
  642. end;
  643. procedure tcg.a_param_loc(list : taasmoutput;const l:tlocation;const paraloc : TCGPara);
  644. begin
  645. case l.loc of
  646. LOC_REGISTER,
  647. LOC_CREGISTER :
  648. a_param_reg(list,l.size,l.register,paraloc);
  649. LOC_CONSTANT :
  650. a_param_const(list,l.size,l.value,paraloc);
  651. LOC_CREFERENCE,
  652. LOC_REFERENCE :
  653. a_param_ref(list,l.size,l.reference,paraloc);
  654. else
  655. internalerror(2002032211);
  656. end;
  657. end;
  658. procedure tcg.a_paramaddr_ref(list : taasmoutput;const r : treference;const paraloc : TCGPara);
  659. var
  660. hr : tregister;
  661. begin
  662. hr:=getaddressregister(list);
  663. a_loadaddr_ref_reg(list,r,hr);
  664. a_param_reg(list,OS_ADDR,hr,paraloc);
  665. end;
  666. procedure tcg.a_param_copy_ref(list : taasmoutput;size : aint;const r : treference;const paraloc : TCGPara);
  667. var
  668. ref : treference;
  669. begin
  670. paraloc.check_simple_location;
  671. if not(paraloc.location^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  672. internalerror(2003010901);
  673. reference_reset_base(ref,paraloc.location^.reference.index,paraloc.location^.reference.offset);
  674. g_concatcopy(list,r,ref,size);
  675. end;
  676. {****************************************************************************
  677. some generic implementations
  678. ****************************************************************************}
  679. procedure tcg.a_load_ref_ref(list : taasmoutput;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  680. var
  681. tmpreg: tregister;
  682. begin
  683. { verify if we have the same reference }
  684. if references_equal(sref,dref) then
  685. exit;
  686. tmpreg:=getintregister(list,tosize);
  687. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  688. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  689. end;
  690. procedure tcg.a_load_const_ref(list : taasmoutput;size : tcgsize;a : aint;const ref : treference);
  691. var
  692. tmpreg: tregister;
  693. begin
  694. tmpreg:=getintregister(list,size);
  695. a_load_const_reg(list,size,a,tmpreg);
  696. a_load_reg_ref(list,size,size,tmpreg,ref);
  697. end;
  698. procedure tcg.a_load_const_loc(list : taasmoutput;a : aint;const loc: tlocation);
  699. begin
  700. case loc.loc of
  701. LOC_REFERENCE,LOC_CREFERENCE:
  702. a_load_const_ref(list,loc.size,a,loc.reference);
  703. LOC_REGISTER,LOC_CREGISTER:
  704. a_load_const_reg(list,loc.size,a,loc.register);
  705. else
  706. internalerror(200203272);
  707. end;
  708. end;
  709. procedure tcg.a_load_reg_loc(list : taasmoutput;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  710. begin
  711. case loc.loc of
  712. LOC_REFERENCE,LOC_CREFERENCE:
  713. a_load_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  714. LOC_REGISTER,LOC_CREGISTER:
  715. a_load_reg_reg(list,fromsize,loc.size,reg,loc.register);
  716. else
  717. internalerror(200203271);
  718. end;
  719. end;
  720. procedure tcg.a_load_loc_reg(list : taasmoutput; tosize: tcgsize; const loc: tlocation; reg : tregister);
  721. begin
  722. case loc.loc of
  723. LOC_REFERENCE,LOC_CREFERENCE:
  724. a_load_ref_reg(list,loc.size,tosize,loc.reference,reg);
  725. LOC_REGISTER,LOC_CREGISTER:
  726. a_load_reg_reg(list,loc.size,tosize,loc.register,reg);
  727. LOC_CONSTANT:
  728. a_load_const_reg(list,tosize,loc.value,reg);
  729. else
  730. internalerror(200109092);
  731. end;
  732. end;
  733. procedure tcg.a_load_loc_ref(list : taasmoutput;tosize: tcgsize; const loc: tlocation; const ref : treference);
  734. begin
  735. case loc.loc of
  736. LOC_REFERENCE,LOC_CREFERENCE:
  737. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  738. LOC_REGISTER,LOC_CREGISTER:
  739. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  740. LOC_CONSTANT:
  741. a_load_const_ref(list,tosize,loc.value,ref);
  742. else
  743. internalerror(200109302);
  744. end;
  745. end;
  746. function tcg.optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aint; var reg:tregister): boolean;
  747. var
  748. powerval : longint;
  749. begin
  750. optimize_op_const_reg := false;
  751. case op of
  752. { or with zero returns same result }
  753. OP_OR : if a = 0 then optimize_op_const_reg := true;
  754. { and with max returns same result }
  755. OP_AND : if (a = high(a)) then optimize_op_const_reg := true;
  756. { division by 1 returns result }
  757. OP_DIV :
  758. begin
  759. if a = 1 then
  760. optimize_op_const_reg := true
  761. else if ispowerof2(int64(a), powerval) then
  762. begin
  763. a := powerval;
  764. op:= OP_SHR;
  765. end;
  766. exit;
  767. end;
  768. OP_IDIV:
  769. begin
  770. if a = 1 then
  771. optimize_op_const_reg := true
  772. else if ispowerof2(int64(a), powerval) then
  773. begin
  774. a := powerval;
  775. op:= OP_SAR;
  776. end;
  777. exit;
  778. end;
  779. OP_MUL,OP_IMUL:
  780. begin
  781. if a = 1 then
  782. optimize_op_const_reg := true
  783. else if ispowerof2(int64(a), powerval) then
  784. begin
  785. a := powerval;
  786. op:= OP_SHL;
  787. end;
  788. exit;
  789. end;
  790. OP_SAR,OP_SHL,OP_SHR:
  791. begin
  792. if a = 0 then
  793. optimize_op_const_reg := true;
  794. exit;
  795. end;
  796. end;
  797. end;
  798. procedure tcg.a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  799. begin
  800. case loc.loc of
  801. LOC_REFERENCE, LOC_CREFERENCE:
  802. a_loadfpu_ref_reg(list,loc.size,loc.reference,reg);
  803. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  804. a_loadfpu_reg_reg(list,loc.size,loc.register,reg);
  805. else
  806. internalerror(200203301);
  807. end;
  808. end;
  809. procedure tcg.a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  810. begin
  811. case loc.loc of
  812. LOC_REFERENCE, LOC_CREFERENCE:
  813. a_loadfpu_reg_ref(list,size,reg,loc.reference);
  814. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  815. a_loadfpu_reg_reg(list,size,reg,loc.register);
  816. else
  817. internalerror(48991);
  818. end;
  819. end;
  820. procedure tcg.a_paramfpu_reg(list : taasmoutput;size : tcgsize;const r : tregister;const paraloc : TCGPara);
  821. var
  822. ref : treference;
  823. begin
  824. paraloc.check_simple_location;
  825. case paraloc.location^.loc of
  826. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  827. a_loadfpu_reg_reg(list,size,r,paraloc.location^.register);
  828. LOC_REFERENCE,LOC_CREFERENCE:
  829. begin
  830. reference_reset_base(ref,paraloc.location^.reference.index,paraloc.location^.reference.offset);
  831. a_loadfpu_reg_ref(list,size,r,ref);
  832. end
  833. else
  834. internalerror(2002071004);
  835. end;
  836. end;
  837. procedure tcg.a_paramfpu_ref(list : taasmoutput;size : tcgsize;const ref : treference;const paraloc : TCGPara);
  838. var
  839. href : treference;
  840. begin
  841. paraloc.check_simple_location;
  842. case paraloc.location^.loc of
  843. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  844. a_loadfpu_ref_reg(list,size,ref,paraloc.location^.register);
  845. LOC_REFERENCE,LOC_CREFERENCE:
  846. begin
  847. reference_reset_base(href,paraloc.location^.reference.index,paraloc.location^.reference.offset);
  848. { concatcopy should choose the best way to copy the data }
  849. g_concatcopy(list,ref,href,tcgsize2size[size]);
  850. end
  851. else
  852. internalerror(200402201);
  853. end;
  854. end;
  855. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; const ref: TReference);
  856. var
  857. tmpreg : tregister;
  858. begin
  859. tmpreg:=getintregister(list,size);
  860. a_load_ref_reg(list,size,size,ref,tmpreg);
  861. a_op_const_reg(list,op,size,a,tmpreg);
  862. a_load_reg_ref(list,size,size,tmpreg,ref);
  863. end;
  864. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; a: aint; const loc: tlocation);
  865. begin
  866. case loc.loc of
  867. LOC_REGISTER, LOC_CREGISTER:
  868. a_op_const_reg(list,op,loc.size,a,loc.register);
  869. LOC_REFERENCE, LOC_CREFERENCE:
  870. a_op_const_ref(list,op,loc.size,a,loc.reference);
  871. else
  872. internalerror(200109061);
  873. end;
  874. end;
  875. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  876. var
  877. tmpreg : tregister;
  878. begin
  879. tmpreg:=getintregister(list,size);
  880. a_load_ref_reg(list,size,size,ref,tmpreg);
  881. a_op_reg_reg(list,op,size,reg,tmpreg);
  882. a_load_reg_ref(list,size,size,tmpreg,ref);
  883. end;
  884. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  885. var
  886. tmpreg: tregister;
  887. begin
  888. case op of
  889. OP_NOT,OP_NEG:
  890. { handle it as "load ref,reg; op reg" }
  891. begin
  892. a_load_ref_reg(list,size,size,ref,reg);
  893. a_op_reg_reg(list,op,size,reg,reg);
  894. end;
  895. else
  896. begin
  897. tmpreg:=getintregister(list,size);
  898. a_load_ref_reg(list,size,size,ref,tmpreg);
  899. a_op_reg_reg(list,op,size,tmpreg,reg);
  900. end;
  901. end;
  902. end;
  903. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  904. begin
  905. case loc.loc of
  906. LOC_REGISTER, LOC_CREGISTER:
  907. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  908. LOC_REFERENCE, LOC_CREFERENCE:
  909. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  910. else
  911. internalerror(200109061);
  912. end;
  913. end;
  914. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  915. var
  916. tmpreg: tregister;
  917. begin
  918. case loc.loc of
  919. LOC_REGISTER,LOC_CREGISTER:
  920. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  921. LOC_REFERENCE,LOC_CREFERENCE:
  922. begin
  923. tmpreg:=getintregister(list,loc.size);
  924. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  925. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  926. end;
  927. else
  928. internalerror(200109061);
  929. end;
  930. end;
  931. procedure Tcg.a_op_const_reg_reg(list:Taasmoutput;op:Topcg;size:Tcgsize;
  932. a:aint;src,dst:Tregister);
  933. begin
  934. a_load_reg_reg(list,size,size,src,dst);
  935. a_op_const_reg(list,op,size,a,dst);
  936. end;
  937. procedure tcg.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  938. size: tcgsize; src1, src2, dst: tregister);
  939. var
  940. tmpreg: tregister;
  941. begin
  942. if (dst<>src1) then
  943. begin
  944. a_load_reg_reg(list,size,size,src2,dst);
  945. a_op_reg_reg(list,op,size,src1,dst);
  946. end
  947. else
  948. begin
  949. tmpreg:=getintregister(list,size);
  950. a_load_reg_reg(list,size,size,src2,tmpreg);
  951. a_op_reg_reg(list,op,size,src1,tmpreg);
  952. a_load_reg_reg(list,size,size,tmpreg,dst);
  953. end;
  954. end;
  955. procedure tcg.a_op_const_reg_reg_checkoverflow(list: taasmoutput; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  956. begin
  957. a_op_const_reg_reg(list,op,size,a,src,dst);
  958. ovloc.loc:=LOC_VOID;
  959. end;
  960. procedure tcg.a_op_reg_reg_reg_checkoverflow(list: taasmoutput; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  961. begin
  962. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  963. ovloc.loc:=LOC_VOID;
  964. end;
  965. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  966. l : tasmlabel);
  967. var
  968. tmpreg: tregister;
  969. begin
  970. tmpreg:=getintregister(list,size);
  971. a_load_ref_reg(list,size,size,ref,tmpreg);
  972. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  973. end;
  974. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;const loc : tlocation;
  975. l : tasmlabel);
  976. begin
  977. case loc.loc of
  978. LOC_REGISTER,LOC_CREGISTER:
  979. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  980. LOC_REFERENCE,LOC_CREFERENCE:
  981. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  982. else
  983. internalerror(200109061);
  984. end;
  985. end;
  986. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  987. var
  988. tmpreg: tregister;
  989. begin
  990. tmpreg:=getintregister(list,size);
  991. a_load_ref_reg(list,size,size,ref,tmpreg);
  992. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  993. end;
  994. procedure tcg.a_cmp_reg_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; reg : tregister; const ref: treference; l : tasmlabel);
  995. var
  996. tmpreg: tregister;
  997. begin
  998. tmpreg:=getintregister(list,size);
  999. a_load_ref_reg(list,size,size,ref,tmpreg);
  1000. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  1001. end;
  1002. procedure tcg.a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  1003. begin
  1004. case loc.loc of
  1005. LOC_REGISTER,
  1006. LOC_CREGISTER:
  1007. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  1008. LOC_REFERENCE,
  1009. LOC_CREFERENCE :
  1010. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  1011. LOC_CONSTANT:
  1012. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  1013. else
  1014. internalerror(200203231);
  1015. end;
  1016. end;
  1017. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1018. l : tasmlabel);
  1019. var
  1020. tmpreg: tregister;
  1021. begin
  1022. case loc.loc of
  1023. LOC_REGISTER,LOC_CREGISTER:
  1024. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1025. LOC_REFERENCE,LOC_CREFERENCE:
  1026. begin
  1027. tmpreg:=getintregister(list,size);
  1028. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1029. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1030. end
  1031. else
  1032. internalerror(200109061);
  1033. end;
  1034. end;
  1035. procedure tcg.a_loadmm_loc_reg(list: taasmoutput; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  1036. begin
  1037. case loc.loc of
  1038. LOC_MMREGISTER,LOC_CMMREGISTER:
  1039. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1040. LOC_REFERENCE,LOC_CREFERENCE:
  1041. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  1042. else
  1043. internalerror(200310121);
  1044. end;
  1045. end;
  1046. procedure tcg.a_loadmm_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  1047. begin
  1048. case loc.loc of
  1049. LOC_MMREGISTER,LOC_CMMREGISTER:
  1050. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  1051. LOC_REFERENCE,LOC_CREFERENCE:
  1052. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  1053. else
  1054. internalerror(200310122);
  1055. end;
  1056. end;
  1057. procedure tcg.a_parammm_reg(list: taasmoutput; size: tcgsize; reg: tregister;const paraloc : TCGPara;shuffle : pmmshuffle);
  1058. var
  1059. href : treference;
  1060. begin
  1061. paraloc.check_simple_location;
  1062. case paraloc.location^.loc of
  1063. LOC_MMREGISTER,LOC_CMMREGISTER:
  1064. a_loadmm_reg_reg(list,size,paraloc.location^.size,reg,paraloc.location^.register,shuffle);
  1065. LOC_REFERENCE,LOC_CREFERENCE:
  1066. begin
  1067. reference_reset_base(href,paraloc.location^.reference.index,paraloc.location^.reference.offset);
  1068. a_loadmm_reg_ref(list,size,paraloc.location^.size,reg,href,shuffle);
  1069. end
  1070. else
  1071. internalerror(200310123);
  1072. end;
  1073. end;
  1074. procedure tcg.a_parammm_ref(list: taasmoutput; size: tcgsize;const ref: treference;const paraloc : TCGPara;shuffle : pmmshuffle);
  1075. var
  1076. hr : tregister;
  1077. hs : tmmshuffle;
  1078. begin
  1079. paraloc.check_simple_location;
  1080. hr:=getmmregister(list,paraloc.location^.size);
  1081. a_loadmm_ref_reg(list,size,paraloc.location^.size,ref,hr,shuffle);
  1082. if realshuffle(shuffle) then
  1083. begin
  1084. hs:=shuffle^;
  1085. removeshuffles(hs);
  1086. a_parammm_reg(list,paraloc.location^.size,hr,paraloc,@hs);
  1087. end
  1088. else
  1089. a_parammm_reg(list,paraloc.location^.size,hr,paraloc,shuffle);
  1090. end;
  1091. procedure tcg.a_parammm_loc(list: taasmoutput;const loc: tlocation; const paraloc : TCGPara;shuffle : pmmshuffle);
  1092. begin
  1093. case loc.loc of
  1094. LOC_MMREGISTER,LOC_CMMREGISTER:
  1095. a_parammm_reg(list,loc.size,loc.register,paraloc,shuffle);
  1096. LOC_REFERENCE,LOC_CREFERENCE:
  1097. a_parammm_ref(list,loc.size,loc.reference,paraloc,shuffle);
  1098. else
  1099. internalerror(200310123);
  1100. end;
  1101. end;
  1102. procedure tcg.a_opmm_ref_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1103. var
  1104. hr : tregister;
  1105. hs : tmmshuffle;
  1106. begin
  1107. hr:=getmmregister(list,size);
  1108. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1109. if realshuffle(shuffle) then
  1110. begin
  1111. hs:=shuffle^;
  1112. removeshuffles(hs);
  1113. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  1114. end
  1115. else
  1116. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  1117. end;
  1118. procedure tcg.a_opmm_reg_ref(list: taasmoutput; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  1119. var
  1120. hr : tregister;
  1121. hs : tmmshuffle;
  1122. begin
  1123. hr:=getmmregister(list,size);
  1124. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1125. if realshuffle(shuffle) then
  1126. begin
  1127. hs:=shuffle^;
  1128. removeshuffles(hs);
  1129. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  1130. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  1131. end
  1132. else
  1133. begin
  1134. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  1135. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  1136. end;
  1137. end;
  1138. procedure tcg.a_opmm_loc_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  1139. begin
  1140. case loc.loc of
  1141. LOC_CMMREGISTER,LOC_MMREGISTER:
  1142. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  1143. LOC_CREFERENCE,LOC_REFERENCE:
  1144. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  1145. else
  1146. internalerror(200312232);
  1147. end;
  1148. end;
  1149. procedure tcg.g_concatcopy_unaligned(list : taasmoutput;const source,dest : treference;len : aint);
  1150. begin
  1151. g_concatcopy(list,source,dest,len);
  1152. end;
  1153. procedure tcg.g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte);
  1154. var
  1155. paraloc1,paraloc2,paraloc3 : TCGPara;
  1156. begin
  1157. paraloc1.init;
  1158. paraloc2.init;
  1159. paraloc3.init;
  1160. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1161. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1162. paramanager.getintparaloc(pocall_default,3,paraloc3);
  1163. paramanager.allocparaloc(list,paraloc3);
  1164. a_paramaddr_ref(list,dest,paraloc3);
  1165. paramanager.allocparaloc(list,paraloc2);
  1166. a_paramaddr_ref(list,source,paraloc2);
  1167. paramanager.allocparaloc(list,paraloc1);
  1168. a_param_const(list,OS_INT,len,paraloc1);
  1169. paramanager.freeparaloc(list,paraloc3);
  1170. paramanager.freeparaloc(list,paraloc2);
  1171. paramanager.freeparaloc(list,paraloc1);
  1172. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1173. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1174. a_call_name(list,'FPC_SHORTSTR_ASSIGN');
  1175. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1176. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1177. paraloc3.done;
  1178. paraloc2.done;
  1179. paraloc1.done;
  1180. end;
  1181. procedure tcg.g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  1182. var
  1183. href : treference;
  1184. incrfunc : string;
  1185. paraloc1,paraloc2 : TCGPara;
  1186. begin
  1187. paraloc1.init;
  1188. paraloc2.init;
  1189. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1190. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1191. if is_interfacecom(t) then
  1192. incrfunc:='FPC_INTF_INCR_REF'
  1193. else if is_ansistring(t) then
  1194. {$ifdef ansistring_bits}
  1195. begin
  1196. case Tstringdef(t).string_typ of
  1197. st_ansistring16:
  1198. incrfunc:='FPC_ANSISTR16_INCR_REF';
  1199. st_ansistring32:
  1200. incrfunc:='FPC_ANSISTR32_INCR_REF';
  1201. st_ansistring64:
  1202. incrfunc:='FPC_ANSISTR64_INCR_REF';
  1203. end;
  1204. end
  1205. {$else}
  1206. incrfunc:='FPC_ANSISTR_INCR_REF'
  1207. {$endif}
  1208. else if is_widestring(t) then
  1209. incrfunc:='FPC_WIDESTR_INCR_REF'
  1210. else if is_dynamic_array(t) then
  1211. incrfunc:='FPC_DYNARRAY_INCR_REF'
  1212. else
  1213. incrfunc:='';
  1214. { call the special incr function or the generic addref }
  1215. if incrfunc<>'' then
  1216. begin
  1217. paramanager.allocparaloc(list,paraloc1);
  1218. { these functions get the pointer by value }
  1219. a_param_ref(list,OS_ADDR,ref,paraloc1);
  1220. paramanager.freeparaloc(list,paraloc1);
  1221. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1222. a_call_name(list,incrfunc);
  1223. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1224. end
  1225. else
  1226. begin
  1227. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1228. paramanager.allocparaloc(list,paraloc2);
  1229. a_paramaddr_ref(list,href,paraloc2);
  1230. paramanager.allocparaloc(list,paraloc1);
  1231. a_paramaddr_ref(list,ref,paraloc1);
  1232. paramanager.freeparaloc(list,paraloc1);
  1233. paramanager.freeparaloc(list,paraloc2);
  1234. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1235. a_call_name(list,'FPC_ADDREF');
  1236. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1237. end;
  1238. paraloc2.done;
  1239. paraloc1.done;
  1240. end;
  1241. procedure tcg.g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  1242. var
  1243. href : treference;
  1244. decrfunc : string;
  1245. needrtti : boolean;
  1246. paraloc1,paraloc2 : TCGPara;
  1247. begin
  1248. paraloc1.init;
  1249. paraloc2.init;
  1250. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1251. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1252. needrtti:=false;
  1253. if is_interfacecom(t) then
  1254. decrfunc:='FPC_INTF_DECR_REF'
  1255. else if is_ansistring(t) then
  1256. {$ifdef ansistring_bits}
  1257. begin
  1258. case Tstringdef(t).string_typ of
  1259. st_ansistring16:
  1260. decrfunc:='FPC_ANSISTR16_DECR_REF';
  1261. st_ansistring32:
  1262. decrfunc:='FPC_ANSISTR32_DECR_REF';
  1263. st_ansistring64:
  1264. decrfunc:='FPC_ANSISTR64_DECR_REF';
  1265. end;
  1266. end
  1267. {$else}
  1268. decrfunc:='FPC_ANSISTR_DECR_REF'
  1269. {$endif}
  1270. else if is_widestring(t) then
  1271. decrfunc:='FPC_WIDESTR_DECR_REF'
  1272. else if is_dynamic_array(t) then
  1273. begin
  1274. decrfunc:='FPC_DYNARRAY_DECR_REF';
  1275. needrtti:=true;
  1276. end
  1277. else
  1278. decrfunc:='';
  1279. { call the special decr function or the generic decref }
  1280. if decrfunc<>'' then
  1281. begin
  1282. if needrtti then
  1283. begin
  1284. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1285. paramanager.allocparaloc(list,paraloc2);
  1286. a_paramaddr_ref(list,href,paraloc2);
  1287. end;
  1288. paramanager.allocparaloc(list,paraloc1);
  1289. a_paramaddr_ref(list,ref,paraloc1);
  1290. paramanager.freeparaloc(list,paraloc1);
  1291. if needrtti then
  1292. paramanager.freeparaloc(list,paraloc2);
  1293. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1294. a_call_name(list,decrfunc);
  1295. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1296. end
  1297. else
  1298. begin
  1299. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1300. paramanager.allocparaloc(list,paraloc2);
  1301. a_paramaddr_ref(list,href,paraloc2);
  1302. paramanager.allocparaloc(list,paraloc1);
  1303. a_paramaddr_ref(list,ref,paraloc1);
  1304. paramanager.freeparaloc(list,paraloc1);
  1305. paramanager.freeparaloc(list,paraloc2);
  1306. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1307. a_call_name(list,'FPC_DECREF');
  1308. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1309. end;
  1310. { Temp locations need always to be reset to 0 }
  1311. if tg.istemp(ref) then
  1312. a_load_const_ref(list,OS_ADDR,0,ref);
  1313. paraloc2.done;
  1314. paraloc1.done;
  1315. end;
  1316. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference);
  1317. var
  1318. href : treference;
  1319. paraloc1,paraloc2 : TCGPara;
  1320. begin
  1321. paraloc1.init;
  1322. paraloc2.init;
  1323. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1324. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1325. if is_ansistring(t) or
  1326. is_widestring(t) or
  1327. is_interfacecom(t) or
  1328. is_dynamic_array(t) then
  1329. a_load_const_ref(list,OS_ADDR,0,ref)
  1330. else
  1331. begin
  1332. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1333. paramanager.allocparaloc(list,paraloc2);
  1334. a_paramaddr_ref(list,href,paraloc2);
  1335. paramanager.allocparaloc(list,paraloc1);
  1336. a_paramaddr_ref(list,ref,paraloc1);
  1337. paramanager.freeparaloc(list,paraloc1);
  1338. paramanager.freeparaloc(list,paraloc2);
  1339. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1340. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1341. a_call_name(list,'FPC_INITIALIZE');
  1342. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1343. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1344. end;
  1345. paraloc1.done;
  1346. paraloc2.done;
  1347. end;
  1348. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference);
  1349. var
  1350. href : treference;
  1351. paraloc1,paraloc2 : TCGPara;
  1352. begin
  1353. paraloc1.init;
  1354. paraloc2.init;
  1355. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1356. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1357. if is_ansistring(t) or
  1358. is_widestring(t) or
  1359. is_interfacecom(t) then
  1360. begin
  1361. g_decrrefcount(list,t,ref);
  1362. { Temp locations are already reset to 0 }
  1363. if not tg.istemp(ref) then
  1364. a_load_const_ref(list,OS_ADDR,0,ref);
  1365. end
  1366. else
  1367. begin
  1368. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1369. paramanager.allocparaloc(list,paraloc2);
  1370. a_paramaddr_ref(list,href,paraloc2);
  1371. paramanager.allocparaloc(list,paraloc1);
  1372. a_paramaddr_ref(list,ref,paraloc1);
  1373. paramanager.freeparaloc(list,paraloc1);
  1374. paramanager.freeparaloc(list,paraloc2);
  1375. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1376. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1377. a_call_name(list,'FPC_FINALIZE');
  1378. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1379. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1380. end;
  1381. paraloc1.done;
  1382. paraloc2.done;
  1383. end;
  1384. procedure tcg.g_rangecheck(list: taasmoutput; const l:tlocation;fromdef,todef: tdef);
  1385. { generate range checking code for the value at location p. The type }
  1386. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  1387. { is the original type used at that location. When both defs are equal }
  1388. { the check is also insert (needed for succ,pref,inc,dec) }
  1389. {$ifndef ver1_0}
  1390. const
  1391. aintmax=high(aint);
  1392. {$endif}
  1393. var
  1394. neglabel : tasmlabel;
  1395. hreg : tregister;
  1396. lto,hto,
  1397. lfrom,hfrom : TConstExprInt;
  1398. from_signed: boolean;
  1399. {$ifdef ver1_0}
  1400. aintmax : aint;
  1401. {$endif ver1_0}
  1402. begin
  1403. {$ifdef ver1_0}
  1404. {$ifdef cpu64bit}
  1405. { this is required to prevent incorrect code }
  1406. aintmax:=$7fffffff;
  1407. aintmax:=int64(aintmax shl 16) or int64($ffff);
  1408. aintmax:=int64(aintmax shl 16) or int64($ffff);
  1409. {$else cpu64bit}
  1410. aintmax:=high(aint);
  1411. {$endif cpu64bit}
  1412. {$endif}
  1413. { range checking on and range checkable value? }
  1414. if not(cs_check_range in aktlocalswitches) or
  1415. not(fromdef.deftype in [orddef,enumdef,arraydef]) then
  1416. exit;
  1417. {$ifndef cpu64bit}
  1418. { handle 64bit rangechecks separate for 32bit processors }
  1419. if is_64bit(fromdef) or is_64bit(todef) then
  1420. begin
  1421. cg64.g_rangecheck64(list,l,fromdef,todef);
  1422. exit;
  1423. end;
  1424. {$endif cpu64bit}
  1425. { only check when assigning to scalar, subranges are different, }
  1426. { when todef=fromdef then the check is always generated }
  1427. getrange(fromdef,lfrom,hfrom);
  1428. getrange(todef,lto,hto);
  1429. from_signed := is_signed(fromdef);
  1430. { no range check if from and to are equal and are both longint/dword }
  1431. { (if we have a 32bit processor) or int64/qword, since such }
  1432. { operations can at most cause overflows (JM) }
  1433. { Note that these checks are mostly processor independent, they only }
  1434. { have to be changed once we introduce 64bit subrange types }
  1435. {$ifdef cpu64bit}
  1436. if (fromdef = todef) and
  1437. (fromdef.deftype=orddef) and
  1438. (((((torddef(fromdef).typ = s64bit) and
  1439. (lfrom = low(int64)) and
  1440. (hfrom = high(int64))) or
  1441. ((torddef(fromdef).typ = u64bit) and
  1442. (lfrom = low(qword)) and
  1443. (hfrom = high(qword)))))) then
  1444. exit;
  1445. {$else cpu64bit}
  1446. if (fromdef = todef) and
  1447. (fromdef.deftype=orddef) and
  1448. (((((torddef(fromdef).typ = s32bit) and
  1449. (lfrom = low(longint)) and
  1450. (hfrom = high(longint))) or
  1451. ((torddef(fromdef).typ = u32bit) and
  1452. (lfrom = low(cardinal)) and
  1453. (hfrom = high(cardinal)))))) then
  1454. exit;
  1455. {$endif cpu64bit}
  1456. { if the from-range falls completely in the to-range, no check }
  1457. { is necessary. Don't do this conversion for the largest unsigned type }
  1458. if (todef<>fromdef) and
  1459. (from_signed or (hfrom>=0)) and
  1460. (lto<=lfrom) and (hto>=hfrom) then
  1461. exit;
  1462. { generate the rangecheck code for the def where we are going to }
  1463. { store the result }
  1464. { use the trick that }
  1465. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  1466. { To be able to do that, we have to make sure however that either }
  1467. { fromdef and todef are both signed or unsigned, or that we leave }
  1468. { the parts < 0 and > maxlongint out }
  1469. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  1470. if from_signed xor is_signed(todef) then
  1471. begin
  1472. if from_signed then
  1473. { from is signed, to is unsigned }
  1474. begin
  1475. { if high(from) < 0 -> always range error }
  1476. if (hfrom < 0) or
  1477. { if low(to) > maxlongint also range error }
  1478. (lto > aintmax) then
  1479. begin
  1480. a_call_name(list,'FPC_RANGEERROR');
  1481. exit
  1482. end;
  1483. { from is signed and to is unsigned -> when looking at to }
  1484. { as an signed value, it must be < maxaint (otherwise }
  1485. { it will become negative, which is invalid since "to" is unsigned) }
  1486. if hto > aintmax then
  1487. hto := aintmax;
  1488. end
  1489. else
  1490. { from is unsigned, to is signed }
  1491. begin
  1492. if (lfrom > aintmax) or
  1493. (hto < 0) then
  1494. begin
  1495. a_call_name(list,'FPC_RANGEERROR');
  1496. exit
  1497. end;
  1498. { from is unsigned and to is signed -> when looking at to }
  1499. { as an unsigned value, it must be >= 0 (since negative }
  1500. { values are the same as values > maxlongint) }
  1501. if lto < 0 then
  1502. lto := 0;
  1503. end;
  1504. end;
  1505. hreg:=getintregister(list,OS_INT);
  1506. a_load_loc_reg(list,OS_INT,l,hreg);
  1507. a_op_const_reg(list,OP_SUB,OS_INT,aint(lto),hreg);
  1508. objectlibrary.getlabel(neglabel);
  1509. a_cmp_const_reg_label(list,OS_INT,OC_BE,aint(hto-lto),hreg,neglabel);
  1510. a_call_name(list,'FPC_RANGEERROR');
  1511. a_label(list,neglabel);
  1512. end;
  1513. procedure tcg.g_overflowCheck_loc(List:TAasmOutput;const Loc:TLocation;def:TDef;ovloc : tlocation);
  1514. begin
  1515. g_overflowCheck(list,loc,def);
  1516. end;
  1517. procedure tcg.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference);
  1518. var
  1519. tmpreg : tregister;
  1520. begin
  1521. tmpreg:=getintregister(list,size);
  1522. g_flags2reg(list,size,f,tmpreg);
  1523. a_load_reg_ref(list,size,size,tmpreg,ref);
  1524. end;
  1525. procedure tcg.g_maybe_testself(list : taasmoutput;reg:tregister);
  1526. var
  1527. OKLabel : tasmlabel;
  1528. paraloc1 : TCGPara;
  1529. begin
  1530. if (cs_check_object in aktlocalswitches) or
  1531. (cs_check_range in aktlocalswitches) then
  1532. begin
  1533. objectlibrary.getlabel(oklabel);
  1534. a_cmp_const_reg_label(list,OS_ADDR,OC_NE,0,reg,oklabel);
  1535. paraloc1.init;
  1536. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1537. paramanager.allocparaloc(list,paraloc1);
  1538. a_param_const(list,OS_INT,210,paraloc1);
  1539. paramanager.freeparaloc(list,paraloc1);
  1540. a_call_name(list,'FPC_HANDLEERROR');
  1541. a_label(list,oklabel);
  1542. paraloc1.done;
  1543. end;
  1544. end;
  1545. procedure tcg.g_maybe_testvmt(list : taasmoutput;reg:tregister;objdef:tobjectdef);
  1546. var
  1547. hrefvmt : treference;
  1548. paraloc1,paraloc2 : TCGPara;
  1549. begin
  1550. paraloc1.init;
  1551. paraloc2.init;
  1552. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1553. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1554. if (cs_check_object in aktlocalswitches) then
  1555. begin
  1556. reference_reset_symbol(hrefvmt,objectlibrary.newasmsymbol(objdef.vmt_mangledname,AB_EXTERNAL,AT_DATA),0);
  1557. paramanager.allocparaloc(list,paraloc2);
  1558. a_paramaddr_ref(list,hrefvmt,paraloc2);
  1559. paramanager.allocparaloc(list,paraloc1);
  1560. a_param_reg(list,OS_ADDR,reg,paraloc1);
  1561. paramanager.freeparaloc(list,paraloc1);
  1562. paramanager.freeparaloc(list,paraloc2);
  1563. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1564. a_call_name(list,'FPC_CHECK_OBJECT_EXT');
  1565. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1566. end
  1567. else
  1568. if (cs_check_range in aktlocalswitches) then
  1569. begin
  1570. paramanager.allocparaloc(list,paraloc1);
  1571. a_param_reg(list,OS_ADDR,reg,paraloc1);
  1572. paramanager.freeparaloc(list,paraloc1);
  1573. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1574. a_call_name(list,'FPC_CHECK_OBJECT');
  1575. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1576. end;
  1577. paraloc1.done;
  1578. paraloc2.done;
  1579. end;
  1580. {*****************************************************************************
  1581. Entry/Exit Code Functions
  1582. *****************************************************************************}
  1583. procedure tcg.g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;const lenloc:tlocation;elesize:aint;destreg:tregister);
  1584. var
  1585. sizereg,sourcereg : tregister;
  1586. paraloc1,paraloc2,paraloc3 : TCGPara;
  1587. begin
  1588. { because ppc abi doesn't support dynamic stack allocation properly
  1589. open array value parameters are copied onto the heap
  1590. }
  1591. { allocate two registers for len and source }
  1592. sizereg:=getintregister(list,OS_INT);
  1593. sourcereg:=getaddressregister(list);
  1594. { calculate necessary memory }
  1595. a_load_loc_reg(list,OS_INT,lenloc,sizereg);
  1596. a_op_const_reg(list,OP_ADD,OS_INT,1,sizereg);
  1597. a_op_const_reg(list,OP_IMUL,OS_INT,elesize,sizereg);
  1598. { load source }
  1599. a_loadaddr_ref_reg(list,ref,sourcereg);
  1600. { do getmem call }
  1601. paraloc1.init;
  1602. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1603. paramanager.allocparaloc(list,paraloc1);
  1604. a_param_reg(list,OS_INT,sizereg,paraloc1);
  1605. paramanager.freeparaloc(list,paraloc1);
  1606. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1607. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1608. a_call_name(list,'FPC_GETMEM');
  1609. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1610. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1611. paraloc1.done;
  1612. { return the new address }
  1613. a_load_reg_reg(list,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,destreg);
  1614. { do move call }
  1615. paraloc1.init;
  1616. paraloc2.init;
  1617. paraloc3.init;
  1618. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1619. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1620. paramanager.getintparaloc(pocall_default,3,paraloc3);
  1621. { load size }
  1622. paramanager.allocparaloc(list,paraloc3);
  1623. a_param_reg(list,OS_INT,sizereg,paraloc3);
  1624. { load destination }
  1625. paramanager.allocparaloc(list,paraloc2);
  1626. a_param_reg(list,OS_ADDR,destreg,paraloc2);
  1627. { load source }
  1628. paramanager.allocparaloc(list,paraloc1);
  1629. a_param_reg(list,OS_ADDR,sourcereg,paraloc1);
  1630. paramanager.freeparaloc(list,paraloc3);
  1631. paramanager.freeparaloc(list,paraloc2);
  1632. paramanager.freeparaloc(list,paraloc1);
  1633. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1634. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1635. a_call_name(list,'FPC_MOVE');
  1636. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1637. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1638. paraloc3.done;
  1639. paraloc2.done;
  1640. paraloc1.done;
  1641. end;
  1642. procedure tcg.g_releasevaluepara_openarray(list : taasmoutput;const ref:treference);
  1643. var
  1644. paraloc1 : TCGPara;
  1645. begin
  1646. { do move call }
  1647. paraloc1.init;
  1648. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1649. { load source }
  1650. paramanager.allocparaloc(list,paraloc1);
  1651. a_param_ref(list,OS_ADDR,ref,paraloc1);
  1652. paramanager.freeparaloc(list,paraloc1);
  1653. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1654. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1655. a_call_name(list,'FPC_FREEMEM');
  1656. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1657. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1658. paraloc1.done;
  1659. end;
  1660. procedure tcg.g_save_standard_registers(list:Taasmoutput);
  1661. var
  1662. href : treference;
  1663. size : longint;
  1664. r : integer;
  1665. begin
  1666. { Get temp }
  1667. size:=0;
  1668. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1669. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1670. inc(size,sizeof(aint));
  1671. if size>0 then
  1672. begin
  1673. tg.GetTemp(list,size,tt_noreuse,current_procinfo.save_regs_ref);
  1674. { Copy registers to temp }
  1675. href:=current_procinfo.save_regs_ref;
  1676. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1677. begin
  1678. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1679. begin
  1680. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE),href);
  1681. inc(href.offset,sizeof(aint));
  1682. end;
  1683. include(rg[R_INTREGISTER].preserved_by_proc,saved_standard_registers[r]);
  1684. end;
  1685. end;
  1686. end;
  1687. procedure tcg.g_restore_standard_registers(list:Taasmoutput);
  1688. var
  1689. href : treference;
  1690. r : integer;
  1691. hreg : tregister;
  1692. begin
  1693. { Copy registers from temp }
  1694. href:=current_procinfo.save_regs_ref;
  1695. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1696. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1697. begin
  1698. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  1699. { Allocate register so the optimizer does remove the load }
  1700. a_reg_alloc(list,hreg);
  1701. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  1702. inc(href.offset,sizeof(aint));
  1703. end;
  1704. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  1705. end;
  1706. procedure tcg.g_profilecode(list : taasmoutput);
  1707. begin
  1708. end;
  1709. procedure tcg.g_exception_reason_save(list : taasmoutput; const href : treference);
  1710. begin
  1711. a_load_reg_ref(list, OS_INT, OS_INT, NR_FUNCTION_RESULT_REG, href);
  1712. end;
  1713. procedure tcg.g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aint);
  1714. begin
  1715. a_load_const_ref(list, OS_INT, a, href);
  1716. end;
  1717. procedure tcg.g_exception_reason_load(list : taasmoutput; const href : treference);
  1718. begin
  1719. a_load_ref_reg(list, OS_INT, OS_INT, href, NR_FUNCTION_RESULT_REG);
  1720. end;
  1721. {*****************************************************************************
  1722. TCG64
  1723. *****************************************************************************}
  1724. {$ifndef cpu64bit}
  1725. procedure tcg64.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : int64; regsrc,regdst : tregister64);
  1726. begin
  1727. a_load64_reg_reg(list,regsrc,regdst);
  1728. a_op64_const_reg(list,op,value,regdst);
  1729. end;
  1730. procedure tcg64.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1731. var
  1732. tmpreg64 : tregister64;
  1733. begin
  1734. { when src1=dst then we need to first create a temp to prevent
  1735. overwriting src1 with src2 }
  1736. if (regsrc1.reghi=regdst.reghi) or
  1737. (regsrc1.reglo=regdst.reghi) or
  1738. (regsrc1.reghi=regdst.reglo) or
  1739. (regsrc1.reglo=regdst.reglo) then
  1740. begin
  1741. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  1742. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  1743. a_load64_reg_reg(list,regsrc2,tmpreg64);
  1744. a_op64_reg_reg(list,op,regsrc1,tmpreg64);
  1745. a_load64_reg_reg(list,tmpreg64,regdst);
  1746. end
  1747. else
  1748. begin
  1749. a_load64_reg_reg(list,regsrc2,regdst);
  1750. a_op64_reg_reg(list,op,regsrc1,regdst);
  1751. end;
  1752. end;
  1753. {$endif cpu64bit}
  1754. {****************************************************************************
  1755. TLocation
  1756. ****************************************************************************}
  1757. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  1758. begin
  1759. FillChar(l,sizeof(tlocation),0);
  1760. l.loc:=lt;
  1761. l.size:=lsize;
  1762. {$ifdef arm}
  1763. if l.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  1764. l.reference.signindex:=1;
  1765. {$endif arm}
  1766. end;
  1767. procedure location_freetemp(list:taasmoutput; const l : tlocation);
  1768. begin
  1769. if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1770. tg.ungetiftemp(list,l.reference);
  1771. end;
  1772. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  1773. begin
  1774. destloc:=sourceloc;
  1775. end;
  1776. procedure location_swap(var destloc,sourceloc : tlocation);
  1777. var
  1778. swapl : tlocation;
  1779. begin
  1780. swapl := destloc;
  1781. destloc := sourceloc;
  1782. sourceloc := swapl;
  1783. end;
  1784. initialization
  1785. ;
  1786. finalization
  1787. cg.free;
  1788. {$ifndef cpu64bit}
  1789. cg64.free;
  1790. {$endif cpu64bit}
  1791. end.
  1792. {
  1793. $Log$
  1794. Revision 1.182 2004-10-25 15:36:47 peter
  1795. * save standard registers moved to tcgobj
  1796. Revision 1.181 2004/10/24 20:01:08 peter
  1797. * remove saveregister calling convention
  1798. Revision 1.180 2004/10/24 11:44:28 peter
  1799. * small regvar fixes
  1800. * loadref parameter removed from concatcopy,incrrefcount,etc
  1801. Revision 1.179 2004/10/15 09:14:16 mazen
  1802. - remove $IFDEF DELPHI and related code
  1803. - remove $IFDEF FPCPROCVAR and related code
  1804. Revision 1.178 2004/10/13 21:12:51 peter
  1805. * -Or fixes for open array
  1806. Revision 1.177 2004/10/11 15:46:45 peter
  1807. * length parameter for copyvaluearray changed to tlocation
  1808. Revision 1.176 2004/10/10 20:31:48 peter
  1809. * concatcopy_unaligned maps by default to concatcopy, sparc will
  1810. override it with call to fpc_move
  1811. Revision 1.175 2004/10/10 20:22:53 peter
  1812. * symtable allocation rewritten
  1813. * loading of parameters to local temps/regs cleanup
  1814. * regvar support for parameters
  1815. * regvar support for staticsymtable (main body)
  1816. Revision 1.174 2004/10/05 20:41:01 peter
  1817. * more spilling rewrites
  1818. Revision 1.173 2004/09/29 18:55:40 florian
  1819. * fixed more sparc overflow stuff
  1820. * fixed some op64 stuff for sparc
  1821. Revision 1.172 2004/09/26 21:04:35 florian
  1822. + partial overflow checking on sparc; multiplication still missing
  1823. Revision 1.171 2004/09/26 17:45:30 peter
  1824. * simple regvar support, not yet finished
  1825. Revision 1.170 2004/09/25 14:23:54 peter
  1826. * ungetregister is now only used for cpuregisters, renamed to
  1827. ungetcpuregister
  1828. * renamed (get|unget)explicitregister(s) to ..cpuregister
  1829. * removed location-release/reference_release
  1830. Revision 1.169 2004/09/21 17:25:12 peter
  1831. * paraloc branch merged
  1832. Revision 1.168.4.4 2004/09/20 20:45:57 peter
  1833. * remove cg64.a_reg_alloc, it should not be used since it
  1834. create more register conflicts
  1835. Revision 1.168.4.3 2004/09/18 20:22:40 jonas
  1836. * allocate the volatile fpu registers around procedures that might use
  1837. them (e.g. FPCMOVE may use them)
  1838. Revision 1.168.4.2 2004/09/12 13:36:40 peter
  1839. * fixed alignment issues
  1840. Revision 1.168.4.1 2004/08/31 20:43:06 peter
  1841. * paraloc patch
  1842. Revision 1.168 2004/07/09 23:41:04 jonas
  1843. * support register parameters for inlined procedures + some inline
  1844. cleanups
  1845. Revision 1.167 2004/07/03 11:47:04 peter
  1846. * fix rangecheck error when assigning u32bit=s32bit
  1847. Revision 1.166 2004/06/20 08:55:28 florian
  1848. * logs truncated
  1849. Revision 1.165 2004/06/16 20:07:07 florian
  1850. * dwarf branch merged
  1851. Revision 1.164 2004/05/22 23:34:27 peter
  1852. tai_regalloc.allocation changed to ratype to notify rgobj of register size changes
  1853. Revision 1.163 2004/04/29 19:56:36 daniel
  1854. * Prepare compiler infrastructure for multiple ansistring types
  1855. Revision 1.162 2004/04/18 07:52:43 florian
  1856. * fixed web bug 3048: comparision of dyn. arrays
  1857. Revision 1.161.2.17 2004/06/13 10:51:16 florian
  1858. * fixed several register allocator problems (sparc/arm)
  1859. }