cgobj.pas 87 KB

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