cgobj.pas 84 KB

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