cgobj.pas 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189
  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. { Remarks:
  139. * If a method specifies a size you have only to take care
  140. of that number of bits, i.e. load_const_reg with OP_8 must
  141. only load the lower 8 bit of the specified register
  142. the rest of the register can be undefined
  143. if necessary the compiler will call a method
  144. to zero or sign extend the register
  145. * The a_load_XX_XX with OP_64 needn't to be
  146. implemented for 32 bit
  147. processors, the code generator takes care of that
  148. * the addr size is for work with the natural pointer
  149. size
  150. * the procedures without fpu/mm are only for integer usage
  151. * normally the first location is the source and the
  152. second the destination
  153. }
  154. {# Emits instruction to call the method specified by symbol name.
  155. This routine must be overriden for each new target cpu.
  156. There is no a_call_ref because loading the reference will use
  157. a temp register on most cpu's resulting in conflicts with the
  158. registers used for the parameters (PFV)
  159. }
  160. procedure a_call_name(list : taasmoutput;const s : string);virtual; abstract;
  161. procedure a_call_reg(list : taasmoutput;reg : tregister);virtual;abstract;
  162. { move instructions }
  163. procedure a_load_const_reg(list : taasmoutput;size : tcgsize;a : aint;register : tregister);virtual; abstract;
  164. procedure a_load_const_ref(list : taasmoutput;size : tcgsize;a : aint;const ref : treference);virtual;
  165. procedure a_load_const_loc(list : taasmoutput;a : aint;const loc : tlocation);
  166. procedure a_load_reg_ref(list : taasmoutput;fromsize,tosize : tcgsize;register : tregister;const ref : treference);virtual; abstract;
  167. procedure a_load_reg_reg(list : taasmoutput;fromsize,tosize : tcgsize;reg1,reg2 : tregister);virtual; abstract;
  168. procedure a_load_reg_loc(list : taasmoutput;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  169. procedure a_load_ref_reg(list : taasmoutput;fromsize,tosize : tcgsize;const ref : treference;register : tregister);virtual; abstract;
  170. procedure a_load_ref_ref(list : taasmoutput;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);virtual;
  171. procedure a_load_loc_reg(list : taasmoutput;tosize: tcgsize; const loc: tlocation; reg : tregister);
  172. procedure a_load_loc_ref(list : taasmoutput;tosize: tcgsize; const loc: tlocation; const ref : treference);
  173. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);virtual; abstract;
  174. { fpu move instructions }
  175. procedure a_loadfpu_reg_reg(list: taasmoutput; size:tcgsize; reg1, reg2: tregister); virtual; abstract;
  176. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); virtual; abstract;
  177. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); virtual; abstract;
  178. procedure a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  179. procedure a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  180. procedure a_paramfpu_reg(list : taasmoutput;size : tcgsize;const r : tregister;const cgpara : TCGPara);virtual;
  181. procedure a_paramfpu_ref(list : taasmoutput;size : tcgsize;const ref : treference;const cgpara : TCGPara);virtual;
  182. { vector register move instructions }
  183. procedure a_loadmm_reg_reg(list: taasmoutput; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); virtual; abstract;
  184. procedure a_loadmm_ref_reg(list: taasmoutput; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual; abstract;
  185. procedure a_loadmm_reg_ref(list: taasmoutput; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); virtual; abstract;
  186. procedure a_loadmm_loc_reg(list: taasmoutput; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  187. procedure a_loadmm_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  188. procedure a_parammm_reg(list: taasmoutput; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  189. procedure a_parammm_ref(list: taasmoutput; size: tcgsize; const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  190. procedure a_parammm_loc(list: taasmoutput; const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle); virtual;
  191. procedure a_opmm_reg_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle); virtual;abstract;
  192. procedure a_opmm_ref_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); virtual;
  193. procedure a_opmm_loc_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle); virtual;
  194. procedure a_opmm_reg_ref(list: taasmoutput; Op: TOpCG; size : tcgsize;reg: tregister;const ref: treference; shuffle : pmmshuffle); virtual;
  195. { basic arithmetic operations }
  196. { note: for operators which require only one argument (not, neg), use }
  197. { the op_reg_reg, op_reg_ref or op_reg_loc methods and keep in mind }
  198. { that in this case the *second* operand is used as both source and }
  199. { destination (JM) }
  200. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: Aint; reg: TRegister); virtual; abstract;
  201. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: Aint; const ref: TReference); virtual;
  202. procedure a_op_const_loc(list : taasmoutput; Op: TOpCG; a: Aint; const loc: tlocation);
  203. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual; abstract;
  204. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); virtual;
  205. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); virtual;
  206. procedure a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  207. procedure a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  208. { trinary operations for processors that support them, 'emulated' }
  209. { on others. None with "ref" arguments since I don't think there }
  210. { are any processors that support it (JM) }
  211. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister); virtual;
  212. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg; size: tcgsize; src1, src2, dst: tregister); virtual;
  213. procedure a_op_const_reg_reg_checkoverflow(list: taasmoutput; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  214. procedure a_op_reg_reg_reg_checkoverflow(list: taasmoutput; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); virtual;
  215. { comparison operations }
  216. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  217. l : tasmlabel);virtual; abstract;
  218. procedure a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  219. l : tasmlabel); virtual;
  220. procedure a_cmp_const_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; a: aint; const loc: tlocation;
  221. l : tasmlabel);
  222. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); virtual; abstract;
  223. procedure a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel); virtual;
  224. procedure a_cmp_reg_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); virtual;
  225. procedure a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  226. procedure a_cmp_ref_loc_label(list: taasmoutput; size: tcgsize;cmp_op: topcmp; const ref: treference; const loc: tlocation;
  227. l : tasmlabel);
  228. procedure a_jmp_name(list : taasmoutput;const s : string); virtual; abstract;
  229. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); virtual; abstract;
  230. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); virtual; abstract;
  231. {# Depending on the value to check in the flags, either sets the register reg to one (if the flag is set)
  232. or zero (if the flag is cleared). The size parameter indicates the destination size register.
  233. }
  234. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister); virtual; abstract;
  235. procedure g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference); virtual;
  236. {
  237. This routine tries to optimize the const_reg opcode, and should be
  238. called at the start of a_op_const_reg. It returns the actual opcode
  239. to emit, and the constant value to emit. If this routine returns
  240. TRUE, @var(no) instruction should be emitted (.eg : imul reg by 1 )
  241. @param(op The opcode to emit, returns the opcode which must be emitted)
  242. @param(a The constant which should be emitted, returns the constant which must
  243. be emitted)
  244. @param(reg The register to emit the opcode with, returns the register with
  245. which the opcode will be emitted)
  246. }
  247. function optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aint; var reg: tregister): boolean;virtual;
  248. {#
  249. This routine is used in exception management nodes. It should
  250. save the exception reason currently in the FUNCTION_RETURN_REG. The
  251. save should be done either to a temp (pointed to by href).
  252. or on the stack (pushing the value on the stack).
  253. The size of the value to save is OS_S32. The default version
  254. saves the exception reason to a temp. memory area.
  255. }
  256. procedure g_exception_reason_save(list : taasmoutput; const href : treference);virtual;
  257. {#
  258. This routine is used in exception management nodes. It should
  259. save the exception reason constant. The
  260. save should be done either to a temp (pointed to by href).
  261. or on the stack (pushing the value on the stack).
  262. The size of the value to save is OS_S32. The default version
  263. saves the exception reason to a temp. memory area.
  264. }
  265. procedure g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aint);virtual;
  266. {#
  267. This routine is used in exception management nodes. It should
  268. load the exception reason to the FUNCTION_RETURN_REG. The saved value
  269. should either be in the temp. area (pointed to by href , href should
  270. *NOT* be freed) or on the stack (the value should be popped).
  271. The size of the value to save is OS_S32. The default version
  272. saves the exception reason to a temp. memory area.
  273. }
  274. procedure g_exception_reason_load(list : taasmoutput; const href : treference);virtual;
  275. procedure g_maybe_testself(list : taasmoutput;reg:tregister);
  276. procedure g_maybe_testvmt(list : taasmoutput;reg:tregister;objdef:tobjectdef);
  277. {# This should emit the opcode to copy len bytes from the source
  278. to destination.
  279. It must be overriden for each new target processor.
  280. @param(source Source reference of copy)
  281. @param(dest Destination reference of copy)
  282. }
  283. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint);virtual; abstract;
  284. {# This should emit the opcode to copy len bytes from the an unaligned source
  285. to destination.
  286. It must be overriden for each new target processor.
  287. @param(source Source reference of copy)
  288. @param(dest Destination reference of copy)
  289. }
  290. procedure g_concatcopy_unaligned(list : taasmoutput;const source,dest : treference;len : aint);virtual;
  291. {# This should emit the opcode to a shortrstring from the source
  292. to destination.
  293. @param(source Source reference of copy)
  294. @param(dest Destination reference of copy)
  295. }
  296. procedure g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte);
  297. procedure g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  298. procedure g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  299. procedure g_initialize(list : taasmoutput;t : tdef;const ref : treference);
  300. procedure g_finalize(list : taasmoutput;t : tdef;const ref : treference);
  301. {# Generates range checking code. It is to note
  302. that this routine does not need to be overriden,
  303. as it takes care of everything.
  304. @param(p Node which contains the value to check)
  305. @param(todef Type definition of node to range check)
  306. }
  307. procedure g_rangecheck(list: taasmoutput; const l:tlocation; fromdef,todef: tdef); virtual;
  308. {# Generates overflow checking code for a node }
  309. procedure g_overflowcheck(list: taasmoutput; const Loc:tlocation; def:tdef); virtual;abstract;
  310. procedure g_overflowCheck_loc(List:TAasmOutput;const Loc:TLocation;def:TDef;ovloc : tlocation);virtual;
  311. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;const lenloc:tlocation;elesize:aint;destreg:tregister);virtual;
  312. procedure g_releasevaluepara_openarray(list : taasmoutput;const l:tlocation);virtual;
  313. {# Emits instructions when compilation is done in profile
  314. mode (this is set as a command line option). The default
  315. behavior does nothing, should be overriden as required.
  316. }
  317. procedure g_profilecode(list : taasmoutput);virtual;
  318. {# Emits instruction for allocating @var(size) bytes at the stackpointer
  319. @param(size Number of bytes to allocate)
  320. }
  321. procedure g_stackpointer_alloc(list : taasmoutput;size : longint);virtual; abstract;
  322. {# Emits instruction for allocating the locals in entry
  323. code of a routine. This is one of the first
  324. routine called in @var(genentrycode).
  325. @param(localsize Number of bytes to allocate as locals)
  326. }
  327. procedure g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);virtual; abstract;
  328. {# Emits instructions for returning from a subroutine.
  329. Should also restore the framepointer and stack.
  330. @param(parasize Number of bytes of parameters to deallocate from stack)
  331. }
  332. procedure g_proc_exit(list : taasmoutput;parasize:longint;nostackframe:boolean);virtual;abstract;
  333. {# This routine is called when generating the code for the entry point
  334. of a routine. It should save all registers which are not used in this
  335. routine, and which should be declared as saved in the std_saved_registers
  336. set.
  337. This routine is mainly used when linking to code which is generated
  338. by ABI-compliant compilers (like GCC), to make sure that the reserved
  339. registers of that ABI are not clobbered.
  340. @param(usedinproc Registers which are used in the code of this routine)
  341. }
  342. procedure g_save_standard_registers(list:Taasmoutput);virtual;
  343. {# This routine is called when generating the code for the exit point
  344. of a routine. It should restore all registers which were previously
  345. saved in @var(g_save_standard_registers).
  346. @param(usedinproc Registers which are used in the code of this routine)
  347. }
  348. procedure g_restore_standard_registers(list:Taasmoutput);virtual;
  349. procedure g_intf_wrapper(list: TAAsmoutput; procdef: tprocdef; const labelname: string; ioffset: longint);virtual;abstract;
  350. procedure g_adjust_self_value(list:taasmoutput;procdef: tprocdef;ioffset: aint);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,symsym,
  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. {****************************************************************************
  662. some generic implementations
  663. ****************************************************************************}
  664. procedure tcg.a_load_ref_ref(list : taasmoutput;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  665. var
  666. tmpreg: tregister;
  667. begin
  668. { verify if we have the same reference }
  669. if references_equal(sref,dref) then
  670. exit;
  671. tmpreg:=getintregister(list,tosize);
  672. a_load_ref_reg(list,fromsize,tosize,sref,tmpreg);
  673. a_load_reg_ref(list,tosize,tosize,tmpreg,dref);
  674. end;
  675. procedure tcg.a_load_const_ref(list : taasmoutput;size : tcgsize;a : aint;const ref : treference);
  676. var
  677. tmpreg: tregister;
  678. begin
  679. tmpreg:=getintregister(list,size);
  680. a_load_const_reg(list,size,a,tmpreg);
  681. a_load_reg_ref(list,size,size,tmpreg,ref);
  682. end;
  683. procedure tcg.a_load_const_loc(list : taasmoutput;a : aint;const loc: tlocation);
  684. begin
  685. case loc.loc of
  686. LOC_REFERENCE,LOC_CREFERENCE:
  687. a_load_const_ref(list,loc.size,a,loc.reference);
  688. LOC_REGISTER,LOC_CREGISTER:
  689. a_load_const_reg(list,loc.size,a,loc.register);
  690. else
  691. internalerror(200203272);
  692. end;
  693. end;
  694. procedure tcg.a_load_reg_loc(list : taasmoutput;fromsize : tcgsize;reg : tregister;const loc: tlocation);
  695. begin
  696. case loc.loc of
  697. LOC_REFERENCE,LOC_CREFERENCE:
  698. a_load_reg_ref(list,fromsize,loc.size,reg,loc.reference);
  699. LOC_REGISTER,LOC_CREGISTER:
  700. a_load_reg_reg(list,fromsize,loc.size,reg,loc.register);
  701. else
  702. internalerror(200203271);
  703. end;
  704. end;
  705. procedure tcg.a_load_loc_reg(list : taasmoutput; tosize: tcgsize; const loc: tlocation; reg : tregister);
  706. begin
  707. case loc.loc of
  708. LOC_REFERENCE,LOC_CREFERENCE:
  709. a_load_ref_reg(list,loc.size,tosize,loc.reference,reg);
  710. LOC_REGISTER,LOC_CREGISTER:
  711. a_load_reg_reg(list,loc.size,tosize,loc.register,reg);
  712. LOC_CONSTANT:
  713. a_load_const_reg(list,tosize,loc.value,reg);
  714. else
  715. internalerror(200109092);
  716. end;
  717. end;
  718. procedure tcg.a_load_loc_ref(list : taasmoutput;tosize: tcgsize; const loc: tlocation; const ref : treference);
  719. begin
  720. case loc.loc of
  721. LOC_REFERENCE,LOC_CREFERENCE:
  722. a_load_ref_ref(list,loc.size,tosize,loc.reference,ref);
  723. LOC_REGISTER,LOC_CREGISTER:
  724. a_load_reg_ref(list,loc.size,tosize,loc.register,ref);
  725. LOC_CONSTANT:
  726. a_load_const_ref(list,tosize,loc.value,ref);
  727. else
  728. internalerror(200109302);
  729. end;
  730. end;
  731. function tcg.optimize_op_const_reg(list: taasmoutput; var op: topcg; var a : aint; var reg:tregister): boolean;
  732. var
  733. powerval : longint;
  734. begin
  735. optimize_op_const_reg := false;
  736. case op of
  737. { or with zero returns same result }
  738. OP_OR : if a = 0 then optimize_op_const_reg := true;
  739. { and with max returns same result }
  740. OP_AND : if (a = high(a)) then optimize_op_const_reg := true;
  741. { division by 1 returns result }
  742. OP_DIV :
  743. begin
  744. if a = 1 then
  745. optimize_op_const_reg := true
  746. else if ispowerof2(int64(a), powerval) then
  747. begin
  748. a := powerval;
  749. op:= OP_SHR;
  750. end;
  751. exit;
  752. end;
  753. OP_IDIV:
  754. begin
  755. if a = 1 then
  756. optimize_op_const_reg := true
  757. else if ispowerof2(int64(a), powerval) then
  758. begin
  759. a := powerval;
  760. op:= OP_SAR;
  761. end;
  762. exit;
  763. end;
  764. OP_MUL,OP_IMUL:
  765. begin
  766. if a = 1 then
  767. optimize_op_const_reg := true
  768. else if ispowerof2(int64(a), powerval) then
  769. begin
  770. a := powerval;
  771. op:= OP_SHL;
  772. end;
  773. exit;
  774. end;
  775. OP_SAR,OP_SHL,OP_SHR:
  776. begin
  777. if a = 0 then
  778. optimize_op_const_reg := true;
  779. exit;
  780. end;
  781. end;
  782. end;
  783. procedure tcg.a_loadfpu_loc_reg(list: taasmoutput; const loc: tlocation; const reg: tregister);
  784. begin
  785. case loc.loc of
  786. LOC_REFERENCE, LOC_CREFERENCE:
  787. a_loadfpu_ref_reg(list,loc.size,loc.reference,reg);
  788. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  789. a_loadfpu_reg_reg(list,loc.size,loc.register,reg);
  790. else
  791. internalerror(200203301);
  792. end;
  793. end;
  794. procedure tcg.a_loadfpu_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation);
  795. begin
  796. case loc.loc of
  797. LOC_REFERENCE, LOC_CREFERENCE:
  798. a_loadfpu_reg_ref(list,size,reg,loc.reference);
  799. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  800. a_loadfpu_reg_reg(list,size,reg,loc.register);
  801. else
  802. internalerror(48991);
  803. end;
  804. end;
  805. procedure tcg.a_paramfpu_reg(list : taasmoutput;size : tcgsize;const r : tregister;const cgpara : TCGPara);
  806. var
  807. ref : treference;
  808. begin
  809. cgpara.check_simple_location;
  810. case cgpara.location^.loc of
  811. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  812. a_loadfpu_reg_reg(list,size,r,cgpara.location^.register);
  813. LOC_REFERENCE,LOC_CREFERENCE:
  814. begin
  815. reference_reset_base(ref,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  816. a_loadfpu_reg_ref(list,size,r,ref);
  817. end
  818. else
  819. internalerror(2002071004);
  820. end;
  821. end;
  822. procedure tcg.a_paramfpu_ref(list : taasmoutput;size : tcgsize;const ref : treference;const cgpara : TCGPara);
  823. var
  824. href : treference;
  825. begin
  826. cgpara.check_simple_location;
  827. case cgpara.location^.loc of
  828. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  829. a_loadfpu_ref_reg(list,size,ref,cgpara.location^.register);
  830. LOC_REFERENCE,LOC_CREFERENCE:
  831. begin
  832. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  833. { concatcopy should choose the best way to copy the data }
  834. g_concatcopy(list,ref,href,tcgsize2size[size]);
  835. end
  836. else
  837. internalerror(200402201);
  838. end;
  839. end;
  840. procedure tcg.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; const ref: TReference);
  841. var
  842. tmpreg : tregister;
  843. begin
  844. tmpreg:=getintregister(list,size);
  845. a_load_ref_reg(list,size,size,ref,tmpreg);
  846. a_op_const_reg(list,op,size,a,tmpreg);
  847. a_load_reg_ref(list,size,size,tmpreg,ref);
  848. end;
  849. procedure tcg.a_op_const_loc(list : taasmoutput; Op: TOpCG; a: aint; const loc: tlocation);
  850. begin
  851. case loc.loc of
  852. LOC_REGISTER, LOC_CREGISTER:
  853. a_op_const_reg(list,op,loc.size,a,loc.register);
  854. LOC_REFERENCE, LOC_CREFERENCE:
  855. a_op_const_ref(list,op,loc.size,a,loc.reference);
  856. else
  857. internalerror(200109061);
  858. end;
  859. end;
  860. procedure tcg.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  861. var
  862. tmpreg : tregister;
  863. begin
  864. tmpreg:=getintregister(list,size);
  865. a_load_ref_reg(list,size,size,ref,tmpreg);
  866. a_op_reg_reg(list,op,size,reg,tmpreg);
  867. a_load_reg_ref(list,size,size,tmpreg,ref);
  868. end;
  869. procedure tcg.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  870. var
  871. tmpreg: tregister;
  872. begin
  873. case op of
  874. OP_NOT,OP_NEG:
  875. { handle it as "load ref,reg; op reg" }
  876. begin
  877. a_load_ref_reg(list,size,size,ref,reg);
  878. a_op_reg_reg(list,op,size,reg,reg);
  879. end;
  880. else
  881. begin
  882. tmpreg:=getintregister(list,size);
  883. a_load_ref_reg(list,size,size,ref,tmpreg);
  884. a_op_reg_reg(list,op,size,tmpreg,reg);
  885. end;
  886. end;
  887. end;
  888. procedure tcg.a_op_reg_loc(list : taasmoutput; Op: TOpCG; reg: tregister; const loc: tlocation);
  889. begin
  890. case loc.loc of
  891. LOC_REGISTER, LOC_CREGISTER:
  892. a_op_reg_reg(list,op,loc.size,reg,loc.register);
  893. LOC_REFERENCE, LOC_CREFERENCE:
  894. a_op_reg_ref(list,op,loc.size,reg,loc.reference);
  895. else
  896. internalerror(200109061);
  897. end;
  898. end;
  899. procedure tcg.a_op_ref_loc(list : taasmoutput; Op: TOpCG; const ref: TReference; const loc: tlocation);
  900. var
  901. tmpreg: tregister;
  902. begin
  903. case loc.loc of
  904. LOC_REGISTER,LOC_CREGISTER:
  905. a_op_ref_reg(list,op,loc.size,ref,loc.register);
  906. LOC_REFERENCE,LOC_CREFERENCE:
  907. begin
  908. tmpreg:=getintregister(list,loc.size);
  909. a_load_ref_reg(list,loc.size,loc.size,ref,tmpreg);
  910. a_op_reg_ref(list,op,loc.size,tmpreg,loc.reference);
  911. end;
  912. else
  913. internalerror(200109061);
  914. end;
  915. end;
  916. procedure Tcg.a_op_const_reg_reg(list:Taasmoutput;op:Topcg;size:Tcgsize;
  917. a:aint;src,dst:Tregister);
  918. begin
  919. a_load_reg_reg(list,size,size,src,dst);
  920. a_op_const_reg(list,op,size,a,dst);
  921. end;
  922. procedure tcg.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  923. size: tcgsize; src1, src2, dst: tregister);
  924. var
  925. tmpreg: tregister;
  926. begin
  927. if (dst<>src1) then
  928. begin
  929. a_load_reg_reg(list,size,size,src2,dst);
  930. a_op_reg_reg(list,op,size,src1,dst);
  931. end
  932. else
  933. begin
  934. tmpreg:=getintregister(list,size);
  935. a_load_reg_reg(list,size,size,src2,tmpreg);
  936. a_op_reg_reg(list,op,size,src1,tmpreg);
  937. a_load_reg_reg(list,size,size,tmpreg,dst);
  938. end;
  939. end;
  940. procedure tcg.a_op_const_reg_reg_checkoverflow(list: taasmoutput; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  941. begin
  942. a_op_const_reg_reg(list,op,size,a,src,dst);
  943. ovloc.loc:=LOC_VOID;
  944. end;
  945. procedure tcg.a_op_reg_reg_reg_checkoverflow(list: taasmoutput; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  946. begin
  947. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  948. ovloc.loc:=LOC_VOID;
  949. end;
  950. procedure tcg.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  951. l : tasmlabel);
  952. var
  953. tmpreg: tregister;
  954. begin
  955. tmpreg:=getintregister(list,size);
  956. a_load_ref_reg(list,size,size,ref,tmpreg);
  957. a_cmp_const_reg_label(list,size,cmp_op,a,tmpreg,l);
  958. end;
  959. procedure tcg.a_cmp_const_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;const loc : tlocation;
  960. l : tasmlabel);
  961. begin
  962. case loc.loc of
  963. LOC_REGISTER,LOC_CREGISTER:
  964. a_cmp_const_reg_label(list,size,cmp_op,a,loc.register,l);
  965. LOC_REFERENCE,LOC_CREFERENCE:
  966. a_cmp_const_ref_label(list,size,cmp_op,a,loc.reference,l);
  967. else
  968. internalerror(200109061);
  969. end;
  970. end;
  971. procedure tcg.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const ref: treference; reg : tregister; l : tasmlabel);
  972. var
  973. tmpreg: tregister;
  974. begin
  975. tmpreg:=getintregister(list,size);
  976. a_load_ref_reg(list,size,size,ref,tmpreg);
  977. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  978. end;
  979. procedure tcg.a_cmp_reg_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; reg : tregister; const ref: treference; l : tasmlabel);
  980. var
  981. tmpreg: tregister;
  982. begin
  983. tmpreg:=getintregister(list,size);
  984. a_load_ref_reg(list,size,size,ref,tmpreg);
  985. a_cmp_reg_reg_label(list,size,cmp_op,reg,tmpreg,l);
  986. end;
  987. procedure tcg.a_cmp_loc_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp; const loc: tlocation; reg : tregister; l : tasmlabel);
  988. begin
  989. case loc.loc of
  990. LOC_REGISTER,
  991. LOC_CREGISTER:
  992. a_cmp_reg_reg_label(list,size,cmp_op,loc.register,reg,l);
  993. LOC_REFERENCE,
  994. LOC_CREFERENCE :
  995. a_cmp_ref_reg_label(list,size,cmp_op,loc.reference,reg,l);
  996. LOC_CONSTANT:
  997. a_cmp_const_reg_label(list,size,cmp_op,loc.value,reg,l);
  998. else
  999. internalerror(200203231);
  1000. end;
  1001. end;
  1002. procedure tcg.a_cmp_ref_loc_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference;const loc : tlocation;
  1003. l : tasmlabel);
  1004. var
  1005. tmpreg: tregister;
  1006. begin
  1007. case loc.loc of
  1008. LOC_REGISTER,LOC_CREGISTER:
  1009. a_cmp_ref_reg_label(list,size,cmp_op,ref,loc.register,l);
  1010. LOC_REFERENCE,LOC_CREFERENCE:
  1011. begin
  1012. tmpreg:=getintregister(list,size);
  1013. a_load_ref_reg(list,size,size,loc.reference,tmpreg);
  1014. a_cmp_ref_reg_label(list,size,cmp_op,ref,tmpreg,l);
  1015. end
  1016. else
  1017. internalerror(200109061);
  1018. end;
  1019. end;
  1020. procedure tcg.a_loadmm_loc_reg(list: taasmoutput; size: tcgsize; const loc: tlocation; const reg: tregister;shuffle : pmmshuffle);
  1021. begin
  1022. case loc.loc of
  1023. LOC_MMREGISTER,LOC_CMMREGISTER:
  1024. a_loadmm_reg_reg(list,loc.size,size,loc.register,reg,shuffle);
  1025. LOC_REFERENCE,LOC_CREFERENCE:
  1026. a_loadmm_ref_reg(list,loc.size,size,loc.reference,reg,shuffle);
  1027. else
  1028. internalerror(200310121);
  1029. end;
  1030. end;
  1031. procedure tcg.a_loadmm_reg_loc(list: taasmoutput; size: tcgsize; const reg: tregister; const loc: tlocation;shuffle : pmmshuffle);
  1032. begin
  1033. case loc.loc of
  1034. LOC_MMREGISTER,LOC_CMMREGISTER:
  1035. a_loadmm_reg_reg(list,size,loc.size,reg,loc.register,shuffle);
  1036. LOC_REFERENCE,LOC_CREFERENCE:
  1037. a_loadmm_reg_ref(list,size,loc.size,reg,loc.reference,shuffle);
  1038. else
  1039. internalerror(200310122);
  1040. end;
  1041. end;
  1042. procedure tcg.a_parammm_reg(list: taasmoutput; size: tcgsize; reg: tregister;const cgpara : TCGPara;shuffle : pmmshuffle);
  1043. var
  1044. href : treference;
  1045. begin
  1046. cgpara.check_simple_location;
  1047. case cgpara.location^.loc of
  1048. LOC_MMREGISTER,LOC_CMMREGISTER:
  1049. a_loadmm_reg_reg(list,size,cgpara.location^.size,reg,cgpara.location^.register,shuffle);
  1050. LOC_REFERENCE,LOC_CREFERENCE:
  1051. begin
  1052. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset);
  1053. a_loadmm_reg_ref(list,size,cgpara.location^.size,reg,href,shuffle);
  1054. end
  1055. else
  1056. internalerror(200310123);
  1057. end;
  1058. end;
  1059. procedure tcg.a_parammm_ref(list: taasmoutput; size: tcgsize;const ref: treference;const cgpara : TCGPara;shuffle : pmmshuffle);
  1060. var
  1061. hr : tregister;
  1062. hs : tmmshuffle;
  1063. begin
  1064. cgpara.check_simple_location;
  1065. hr:=getmmregister(list,cgpara.location^.size);
  1066. a_loadmm_ref_reg(list,size,cgpara.location^.size,ref,hr,shuffle);
  1067. if realshuffle(shuffle) then
  1068. begin
  1069. hs:=shuffle^;
  1070. removeshuffles(hs);
  1071. a_parammm_reg(list,cgpara.location^.size,hr,cgpara,@hs);
  1072. end
  1073. else
  1074. a_parammm_reg(list,cgpara.location^.size,hr,cgpara,shuffle);
  1075. end;
  1076. procedure tcg.a_parammm_loc(list: taasmoutput;const loc: tlocation; const cgpara : TCGPara;shuffle : pmmshuffle);
  1077. begin
  1078. case loc.loc of
  1079. LOC_MMREGISTER,LOC_CMMREGISTER:
  1080. a_parammm_reg(list,loc.size,loc.register,cgpara,shuffle);
  1081. LOC_REFERENCE,LOC_CREFERENCE:
  1082. a_parammm_ref(list,loc.size,loc.reference,cgpara,shuffle);
  1083. else
  1084. internalerror(200310123);
  1085. end;
  1086. end;
  1087. procedure tcg.a_opmm_ref_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1088. var
  1089. hr : tregister;
  1090. hs : tmmshuffle;
  1091. begin
  1092. hr:=getmmregister(list,size);
  1093. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1094. if realshuffle(shuffle) then
  1095. begin
  1096. hs:=shuffle^;
  1097. removeshuffles(hs);
  1098. a_opmm_reg_reg(list,op,size,hr,reg,@hs);
  1099. end
  1100. else
  1101. a_opmm_reg_reg(list,op,size,hr,reg,shuffle);
  1102. end;
  1103. procedure tcg.a_opmm_reg_ref(list: taasmoutput; Op: TOpCG; size : tcgsize;reg: tregister; const ref: treference; shuffle : pmmshuffle);
  1104. var
  1105. hr : tregister;
  1106. hs : tmmshuffle;
  1107. begin
  1108. hr:=getmmregister(list,size);
  1109. a_loadmm_ref_reg(list,size,size,ref,hr,shuffle);
  1110. if realshuffle(shuffle) then
  1111. begin
  1112. hs:=shuffle^;
  1113. removeshuffles(hs);
  1114. a_opmm_reg_reg(list,op,size,reg,hr,@hs);
  1115. a_loadmm_reg_ref(list,size,size,hr,ref,@hs);
  1116. end
  1117. else
  1118. begin
  1119. a_opmm_reg_reg(list,op,size,reg,hr,shuffle);
  1120. a_loadmm_reg_ref(list,size,size,hr,ref,shuffle);
  1121. end;
  1122. end;
  1123. procedure tcg.a_opmm_loc_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const loc: tlocation; reg: tregister;shuffle : pmmshuffle);
  1124. begin
  1125. case loc.loc of
  1126. LOC_CMMREGISTER,LOC_MMREGISTER:
  1127. a_opmm_reg_reg(list,op,size,loc.register,reg,shuffle);
  1128. LOC_CREFERENCE,LOC_REFERENCE:
  1129. a_opmm_ref_reg(list,op,size,loc.reference,reg,shuffle);
  1130. else
  1131. internalerror(200312232);
  1132. end;
  1133. end;
  1134. procedure tcg.g_concatcopy_unaligned(list : taasmoutput;const source,dest : treference;len : aint);
  1135. begin
  1136. g_concatcopy(list,source,dest,len);
  1137. end;
  1138. procedure tcg.g_copyshortstring(list : taasmoutput;const source,dest : treference;len:byte);
  1139. var
  1140. cgpara1,cgpara2,cgpara3 : TCGPara;
  1141. begin
  1142. cgpara1.init;
  1143. cgpara2.init;
  1144. cgpara3.init;
  1145. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1146. paramanager.getintparaloc(pocall_default,2,cgpara2);
  1147. paramanager.getintparaloc(pocall_default,3,cgpara3);
  1148. paramanager.allocparaloc(list,cgpara3);
  1149. a_paramaddr_ref(list,dest,cgpara3);
  1150. paramanager.allocparaloc(list,cgpara2);
  1151. a_paramaddr_ref(list,source,cgpara2);
  1152. paramanager.allocparaloc(list,cgpara1);
  1153. a_param_const(list,OS_INT,len,cgpara1);
  1154. paramanager.freeparaloc(list,cgpara3);
  1155. paramanager.freeparaloc(list,cgpara2);
  1156. paramanager.freeparaloc(list,cgpara1);
  1157. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1158. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1159. a_call_name(list,'FPC_SHORTSTR_ASSIGN');
  1160. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1161. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1162. cgpara3.done;
  1163. cgpara2.done;
  1164. cgpara1.done;
  1165. end;
  1166. procedure tcg.g_incrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  1167. var
  1168. href : treference;
  1169. incrfunc : string;
  1170. cgpara1,cgpara2 : TCGPara;
  1171. begin
  1172. cgpara1.init;
  1173. cgpara2.init;
  1174. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1175. paramanager.getintparaloc(pocall_default,2,cgpara2);
  1176. if is_interfacecom(t) then
  1177. incrfunc:='FPC_INTF_INCR_REF'
  1178. else if is_ansistring(t) then
  1179. {$ifdef ansistring_bits}
  1180. begin
  1181. case Tstringdef(t).string_typ of
  1182. st_ansistring16:
  1183. incrfunc:='FPC_ANSISTR16_INCR_REF';
  1184. st_ansistring32:
  1185. incrfunc:='FPC_ANSISTR32_INCR_REF';
  1186. st_ansistring64:
  1187. incrfunc:='FPC_ANSISTR64_INCR_REF';
  1188. end;
  1189. end
  1190. {$else}
  1191. incrfunc:='FPC_ANSISTR_INCR_REF'
  1192. {$endif}
  1193. else if is_widestring(t) then
  1194. incrfunc:='FPC_WIDESTR_INCR_REF'
  1195. else if is_dynamic_array(t) then
  1196. incrfunc:='FPC_DYNARRAY_INCR_REF'
  1197. else
  1198. incrfunc:='';
  1199. { call the special incr function or the generic addref }
  1200. if incrfunc<>'' then
  1201. begin
  1202. paramanager.allocparaloc(list,cgpara1);
  1203. { these functions get the pointer by value }
  1204. a_param_ref(list,OS_ADDR,ref,cgpara1);
  1205. paramanager.freeparaloc(list,cgpara1);
  1206. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1207. a_call_name(list,incrfunc);
  1208. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1209. end
  1210. else
  1211. begin
  1212. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1213. paramanager.allocparaloc(list,cgpara2);
  1214. a_paramaddr_ref(list,href,cgpara2);
  1215. paramanager.allocparaloc(list,cgpara1);
  1216. a_paramaddr_ref(list,ref,cgpara1);
  1217. paramanager.freeparaloc(list,cgpara1);
  1218. paramanager.freeparaloc(list,cgpara2);
  1219. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1220. a_call_name(list,'FPC_ADDREF');
  1221. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1222. end;
  1223. cgpara2.done;
  1224. cgpara1.done;
  1225. end;
  1226. procedure tcg.g_decrrefcount(list : taasmoutput;t: tdef; const ref: treference);
  1227. var
  1228. href : treference;
  1229. decrfunc : string;
  1230. needrtti : boolean;
  1231. cgpara1,cgpara2 : TCGPara;
  1232. begin
  1233. cgpara1.init;
  1234. cgpara2.init;
  1235. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1236. paramanager.getintparaloc(pocall_default,2,cgpara2);
  1237. needrtti:=false;
  1238. if is_interfacecom(t) then
  1239. decrfunc:='FPC_INTF_DECR_REF'
  1240. else if is_ansistring(t) then
  1241. {$ifdef ansistring_bits}
  1242. begin
  1243. case Tstringdef(t).string_typ of
  1244. st_ansistring16:
  1245. decrfunc:='FPC_ANSISTR16_DECR_REF';
  1246. st_ansistring32:
  1247. decrfunc:='FPC_ANSISTR32_DECR_REF';
  1248. st_ansistring64:
  1249. decrfunc:='FPC_ANSISTR64_DECR_REF';
  1250. end;
  1251. end
  1252. {$else}
  1253. decrfunc:='FPC_ANSISTR_DECR_REF'
  1254. {$endif}
  1255. else if is_widestring(t) then
  1256. decrfunc:='FPC_WIDESTR_DECR_REF'
  1257. else if is_dynamic_array(t) then
  1258. begin
  1259. decrfunc:='FPC_DYNARRAY_DECR_REF';
  1260. needrtti:=true;
  1261. end
  1262. else
  1263. decrfunc:='';
  1264. { call the special decr function or the generic decref }
  1265. if decrfunc<>'' then
  1266. begin
  1267. if needrtti then
  1268. begin
  1269. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1270. paramanager.allocparaloc(list,cgpara2);
  1271. a_paramaddr_ref(list,href,cgpara2);
  1272. end;
  1273. paramanager.allocparaloc(list,cgpara1);
  1274. a_paramaddr_ref(list,ref,cgpara1);
  1275. paramanager.freeparaloc(list,cgpara1);
  1276. if needrtti then
  1277. paramanager.freeparaloc(list,cgpara2);
  1278. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1279. a_call_name(list,decrfunc);
  1280. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1281. end
  1282. else
  1283. begin
  1284. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1285. paramanager.allocparaloc(list,cgpara2);
  1286. a_paramaddr_ref(list,href,cgpara2);
  1287. paramanager.allocparaloc(list,cgpara1);
  1288. a_paramaddr_ref(list,ref,cgpara1);
  1289. paramanager.freeparaloc(list,cgpara1);
  1290. paramanager.freeparaloc(list,cgpara2);
  1291. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1292. a_call_name(list,'FPC_DECREF');
  1293. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1294. end;
  1295. cgpara2.done;
  1296. cgpara1.done;
  1297. end;
  1298. procedure tcg.g_initialize(list : taasmoutput;t : tdef;const ref : treference);
  1299. var
  1300. href : treference;
  1301. cgpara1,cgpara2 : TCGPara;
  1302. begin
  1303. cgpara1.init;
  1304. cgpara2.init;
  1305. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1306. paramanager.getintparaloc(pocall_default,2,cgpara2);
  1307. if is_ansistring(t) or
  1308. is_widestring(t) or
  1309. is_interfacecom(t) or
  1310. is_dynamic_array(t) then
  1311. a_load_const_ref(list,OS_ADDR,0,ref)
  1312. else
  1313. begin
  1314. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1315. paramanager.allocparaloc(list,cgpara2);
  1316. a_paramaddr_ref(list,href,cgpara2);
  1317. paramanager.allocparaloc(list,cgpara1);
  1318. a_paramaddr_ref(list,ref,cgpara1);
  1319. paramanager.freeparaloc(list,cgpara1);
  1320. paramanager.freeparaloc(list,cgpara2);
  1321. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1322. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1323. a_call_name(list,'FPC_INITIALIZE');
  1324. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1325. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1326. end;
  1327. cgpara1.done;
  1328. cgpara2.done;
  1329. end;
  1330. procedure tcg.g_finalize(list : taasmoutput;t : tdef;const ref : treference);
  1331. var
  1332. href : treference;
  1333. cgpara1,cgpara2 : TCGPara;
  1334. begin
  1335. cgpara1.init;
  1336. cgpara2.init;
  1337. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1338. paramanager.getintparaloc(pocall_default,2,cgpara2);
  1339. if is_ansistring(t) or
  1340. is_widestring(t) or
  1341. is_interfacecom(t) then
  1342. begin
  1343. g_decrrefcount(list,t,ref);
  1344. { Temp locations are already reset to 0 }
  1345. if not tg.istemp(ref) then
  1346. a_load_const_ref(list,OS_ADDR,0,ref);
  1347. end
  1348. else
  1349. begin
  1350. reference_reset_symbol(href,tstoreddef(t).get_rtti_label(initrtti),0);
  1351. paramanager.allocparaloc(list,cgpara2);
  1352. a_paramaddr_ref(list,href,cgpara2);
  1353. paramanager.allocparaloc(list,cgpara1);
  1354. a_paramaddr_ref(list,ref,cgpara1);
  1355. paramanager.freeparaloc(list,cgpara1);
  1356. paramanager.freeparaloc(list,cgpara2);
  1357. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1358. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1359. a_call_name(list,'FPC_FINALIZE');
  1360. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1361. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1362. end;
  1363. cgpara1.done;
  1364. cgpara2.done;
  1365. end;
  1366. procedure tcg.g_rangecheck(list: taasmoutput; const l:tlocation;fromdef,todef: tdef);
  1367. { generate range checking code for the value at location p. The type }
  1368. { type used is checked against todefs ranges. fromdef (p.resulttype.def) }
  1369. { is the original type used at that location. When both defs are equal }
  1370. { the check is also insert (needed for succ,pref,inc,dec) }
  1371. {$ifndef ver1_0}
  1372. const
  1373. aintmax=high(aint);
  1374. {$endif}
  1375. var
  1376. neglabel : tasmlabel;
  1377. hreg : tregister;
  1378. lto,hto,
  1379. lfrom,hfrom : TConstExprInt;
  1380. from_signed: boolean;
  1381. {$ifdef ver1_0}
  1382. aintmax : aint;
  1383. {$endif ver1_0}
  1384. begin
  1385. {$ifdef ver1_0}
  1386. {$ifdef cpu64bit}
  1387. { this is required to prevent incorrect code }
  1388. aintmax:=$7fffffff;
  1389. aintmax:=int64(aintmax shl 16) or int64($ffff);
  1390. aintmax:=int64(aintmax shl 16) or int64($ffff);
  1391. {$else cpu64bit}
  1392. aintmax:=high(aint);
  1393. {$endif cpu64bit}
  1394. {$endif}
  1395. { range checking on and range checkable value? }
  1396. if not(cs_check_range in aktlocalswitches) or
  1397. not(fromdef.deftype in [orddef,enumdef,arraydef]) then
  1398. exit;
  1399. {$ifndef cpu64bit}
  1400. { handle 64bit rangechecks separate for 32bit processors }
  1401. if is_64bit(fromdef) or is_64bit(todef) then
  1402. begin
  1403. cg64.g_rangecheck64(list,l,fromdef,todef);
  1404. exit;
  1405. end;
  1406. {$endif cpu64bit}
  1407. { only check when assigning to scalar, subranges are different, }
  1408. { when todef=fromdef then the check is always generated }
  1409. getrange(fromdef,lfrom,hfrom);
  1410. getrange(todef,lto,hto);
  1411. from_signed := is_signed(fromdef);
  1412. { no range check if from and to are equal and are both longint/dword }
  1413. { (if we have a 32bit processor) or int64/qword, since such }
  1414. { operations can at most cause overflows (JM) }
  1415. { Note that these checks are mostly processor independent, they only }
  1416. { have to be changed once we introduce 64bit subrange types }
  1417. {$ifdef cpu64bit}
  1418. if (fromdef = todef) and
  1419. (fromdef.deftype=orddef) and
  1420. (((((torddef(fromdef).typ = s64bit) and
  1421. (lfrom = low(int64)) and
  1422. (hfrom = high(int64))) or
  1423. ((torddef(fromdef).typ = u64bit) and
  1424. (lfrom = low(qword)) and
  1425. (hfrom = high(qword)))))) then
  1426. exit;
  1427. {$else cpu64bit}
  1428. if (fromdef = todef) and
  1429. (fromdef.deftype=orddef) and
  1430. (((((torddef(fromdef).typ = s32bit) and
  1431. (lfrom = low(longint)) and
  1432. (hfrom = high(longint))) or
  1433. ((torddef(fromdef).typ = u32bit) and
  1434. (lfrom = low(cardinal)) and
  1435. (hfrom = high(cardinal)))))) then
  1436. exit;
  1437. {$endif cpu64bit}
  1438. { if the from-range falls completely in the to-range, no check }
  1439. { is necessary. Don't do this conversion for the largest unsigned type }
  1440. if (todef<>fromdef) and
  1441. (from_signed or (hfrom>=0)) and
  1442. (lto<=lfrom) and (hto>=hfrom) then
  1443. exit;
  1444. { generate the rangecheck code for the def where we are going to }
  1445. { store the result }
  1446. { use the trick that }
  1447. { a <= x <= b <=> 0 <= x-a <= b-a <=> unsigned(x-a) <= unsigned(b-a) }
  1448. { To be able to do that, we have to make sure however that either }
  1449. { fromdef and todef are both signed or unsigned, or that we leave }
  1450. { the parts < 0 and > maxlongint out }
  1451. { is_signed now also works for arrays (it checks the rangetype) (JM) }
  1452. if from_signed xor is_signed(todef) then
  1453. begin
  1454. if from_signed then
  1455. { from is signed, to is unsigned }
  1456. begin
  1457. { if high(from) < 0 -> always range error }
  1458. if (hfrom < 0) or
  1459. { if low(to) > maxlongint also range error }
  1460. (lto > aintmax) then
  1461. begin
  1462. a_call_name(list,'FPC_RANGEERROR');
  1463. exit
  1464. end;
  1465. { from is signed and to is unsigned -> when looking at to }
  1466. { as an signed value, it must be < maxaint (otherwise }
  1467. { it will become negative, which is invalid since "to" is unsigned) }
  1468. if hto > aintmax then
  1469. hto := aintmax;
  1470. end
  1471. else
  1472. { from is unsigned, to is signed }
  1473. begin
  1474. if (lfrom > aintmax) or
  1475. (hto < 0) then
  1476. begin
  1477. a_call_name(list,'FPC_RANGEERROR');
  1478. exit
  1479. end;
  1480. { from is unsigned and to is signed -> when looking at to }
  1481. { as an unsigned value, it must be >= 0 (since negative }
  1482. { values are the same as values > maxlongint) }
  1483. if lto < 0 then
  1484. lto := 0;
  1485. end;
  1486. end;
  1487. hreg:=getintregister(list,OS_INT);
  1488. a_load_loc_reg(list,OS_INT,l,hreg);
  1489. a_op_const_reg(list,OP_SUB,OS_INT,aint(lto),hreg);
  1490. objectlibrary.getlabel(neglabel);
  1491. {
  1492. if from_signed then
  1493. a_cmp_const_reg_label(list,OS_INT,OC_GTE,aint(hto-lto),hreg,neglabel)
  1494. else
  1495. }
  1496. {$ifdef cpu64bit}
  1497. if qword(hto-lto)>qword(aintmax) then
  1498. a_cmp_const_reg_label(list,OS_INT,OC_BE,aintmax,hreg,neglabel)
  1499. else
  1500. {$endif cpu64bit}
  1501. a_cmp_const_reg_label(list,OS_INT,OC_BE,aint(hto-lto),hreg,neglabel);
  1502. a_call_name(list,'FPC_RANGEERROR');
  1503. a_label(list,neglabel);
  1504. end;
  1505. procedure tcg.g_overflowCheck_loc(List:TAasmOutput;const Loc:TLocation;def:TDef;ovloc : tlocation);
  1506. begin
  1507. g_overflowCheck(list,loc,def);
  1508. end;
  1509. procedure tcg.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref:TReference);
  1510. var
  1511. tmpreg : tregister;
  1512. begin
  1513. tmpreg:=getintregister(list,size);
  1514. g_flags2reg(list,size,f,tmpreg);
  1515. a_load_reg_ref(list,size,size,tmpreg,ref);
  1516. end;
  1517. procedure tcg.g_maybe_testself(list : taasmoutput;reg:tregister);
  1518. var
  1519. OKLabel : tasmlabel;
  1520. cgpara1 : TCGPara;
  1521. begin
  1522. if (cs_check_object in aktlocalswitches) or
  1523. (cs_check_range in aktlocalswitches) then
  1524. begin
  1525. objectlibrary.getlabel(oklabel);
  1526. a_cmp_const_reg_label(list,OS_ADDR,OC_NE,0,reg,oklabel);
  1527. cgpara1.init;
  1528. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1529. paramanager.allocparaloc(list,cgpara1);
  1530. a_param_const(list,OS_INT,210,cgpara1);
  1531. paramanager.freeparaloc(list,cgpara1);
  1532. a_call_name(list,'FPC_HANDLEERROR');
  1533. a_label(list,oklabel);
  1534. cgpara1.done;
  1535. end;
  1536. end;
  1537. procedure tcg.g_maybe_testvmt(list : taasmoutput;reg:tregister;objdef:tobjectdef);
  1538. var
  1539. hrefvmt : treference;
  1540. cgpara1,cgpara2 : TCGPara;
  1541. begin
  1542. cgpara1.init;
  1543. cgpara2.init;
  1544. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1545. paramanager.getintparaloc(pocall_default,2,cgpara2);
  1546. if (cs_check_object in aktlocalswitches) then
  1547. begin
  1548. reference_reset_symbol(hrefvmt,objectlibrary.newasmsymbol(objdef.vmt_mangledname,AB_EXTERNAL,AT_DATA),0);
  1549. paramanager.allocparaloc(list,cgpara2);
  1550. a_paramaddr_ref(list,hrefvmt,cgpara2);
  1551. paramanager.allocparaloc(list,cgpara1);
  1552. a_param_reg(list,OS_ADDR,reg,cgpara1);
  1553. paramanager.freeparaloc(list,cgpara1);
  1554. paramanager.freeparaloc(list,cgpara2);
  1555. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1556. a_call_name(list,'FPC_CHECK_OBJECT_EXT');
  1557. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1558. end
  1559. else
  1560. if (cs_check_range in aktlocalswitches) then
  1561. begin
  1562. paramanager.allocparaloc(list,cgpara1);
  1563. a_param_reg(list,OS_ADDR,reg,cgpara1);
  1564. paramanager.freeparaloc(list,cgpara1);
  1565. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1566. a_call_name(list,'FPC_CHECK_OBJECT');
  1567. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1568. end;
  1569. cgpara1.done;
  1570. cgpara2.done;
  1571. end;
  1572. {*****************************************************************************
  1573. Entry/Exit Code Functions
  1574. *****************************************************************************}
  1575. procedure tcg.g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;const lenloc:tlocation;elesize:aint;destreg:tregister);
  1576. var
  1577. sizereg,sourcereg,lenreg : tregister;
  1578. cgpara1,cgpara2,cgpara3 : TCGPara;
  1579. begin
  1580. { because some abis don't support dynamic stack allocation properly
  1581. open array value parameters are copied onto the heap
  1582. }
  1583. { calculate necessary memory }
  1584. { read/write operations on one register make the life of the register allocator hard }
  1585. if not(lenloc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1586. begin
  1587. lenreg:=getintregister(list,OS_INT);
  1588. a_load_loc_reg(list,OS_INT,lenloc,lenreg);
  1589. end
  1590. else
  1591. lenreg:=lenloc.register;
  1592. sizereg:=getintregister(list,OS_INT);
  1593. a_op_const_reg_reg(list,OP_ADD,OS_INT,1,lenreg,sizereg);
  1594. a_op_const_reg(list,OP_IMUL,OS_INT,elesize,sizereg);
  1595. { load source }
  1596. sourcereg:=getaddressregister(list);
  1597. a_loadaddr_ref_reg(list,ref,sourcereg);
  1598. { do getmem call }
  1599. cgpara1.init;
  1600. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1601. paramanager.allocparaloc(list,cgpara1);
  1602. a_param_reg(list,OS_INT,sizereg,cgpara1);
  1603. paramanager.freeparaloc(list,cgpara1);
  1604. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1605. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1606. a_call_name(list,'FPC_GETMEM');
  1607. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1608. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1609. cgpara1.done;
  1610. { return the new address }
  1611. a_load_reg_reg(list,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,destreg);
  1612. { do move call }
  1613. cgpara1.init;
  1614. cgpara2.init;
  1615. cgpara3.init;
  1616. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1617. paramanager.getintparaloc(pocall_default,2,cgpara2);
  1618. paramanager.getintparaloc(pocall_default,3,cgpara3);
  1619. { load size }
  1620. paramanager.allocparaloc(list,cgpara3);
  1621. a_param_reg(list,OS_INT,sizereg,cgpara3);
  1622. { load destination }
  1623. paramanager.allocparaloc(list,cgpara2);
  1624. a_param_reg(list,OS_ADDR,destreg,cgpara2);
  1625. { load source }
  1626. paramanager.allocparaloc(list,cgpara1);
  1627. a_param_reg(list,OS_ADDR,sourcereg,cgpara1);
  1628. paramanager.freeparaloc(list,cgpara3);
  1629. paramanager.freeparaloc(list,cgpara2);
  1630. paramanager.freeparaloc(list,cgpara1);
  1631. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1632. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1633. a_call_name(list,'FPC_MOVE');
  1634. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1635. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1636. cgpara3.done;
  1637. cgpara2.done;
  1638. cgpara1.done;
  1639. end;
  1640. procedure tcg.g_releasevaluepara_openarray(list : taasmoutput;const l:tlocation);
  1641. var
  1642. cgpara1 : TCGPara;
  1643. begin
  1644. { do move call }
  1645. cgpara1.init;
  1646. paramanager.getintparaloc(pocall_default,1,cgpara1);
  1647. { load source }
  1648. paramanager.allocparaloc(list,cgpara1);
  1649. a_param_loc(list,l,cgpara1);
  1650. paramanager.freeparaloc(list,cgpara1);
  1651. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1652. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1653. a_call_name(list,'FPC_FREEMEM');
  1654. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1655. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1656. cgpara1.done;
  1657. end;
  1658. procedure tcg.g_save_standard_registers(list:Taasmoutput);
  1659. var
  1660. href : treference;
  1661. size : longint;
  1662. r : integer;
  1663. begin
  1664. { Get temp }
  1665. size:=0;
  1666. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1667. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1668. inc(size,sizeof(aint));
  1669. if size>0 then
  1670. begin
  1671. tg.GetTemp(list,size,tt_noreuse,current_procinfo.save_regs_ref);
  1672. { Copy registers to temp }
  1673. href:=current_procinfo.save_regs_ref;
  1674. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1675. begin
  1676. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1677. begin
  1678. a_load_reg_ref(list,OS_ADDR,OS_ADDR,newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE),href);
  1679. inc(href.offset,sizeof(aint));
  1680. end;
  1681. include(rg[R_INTREGISTER].preserved_by_proc,saved_standard_registers[r]);
  1682. end;
  1683. end;
  1684. end;
  1685. procedure tcg.g_restore_standard_registers(list:Taasmoutput);
  1686. var
  1687. href : treference;
  1688. r : integer;
  1689. hreg : tregister;
  1690. begin
  1691. { Copy registers from temp }
  1692. href:=current_procinfo.save_regs_ref;
  1693. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1694. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1695. begin
  1696. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  1697. { Allocate register so the optimizer does remove the load }
  1698. a_reg_alloc(list,hreg);
  1699. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  1700. inc(href.offset,sizeof(aint));
  1701. end;
  1702. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  1703. end;
  1704. procedure tcg.g_profilecode(list : taasmoutput);
  1705. begin
  1706. end;
  1707. procedure tcg.g_exception_reason_save(list : taasmoutput; const href : treference);
  1708. begin
  1709. a_load_reg_ref(list, OS_INT, OS_INT, NR_FUNCTION_RESULT_REG, href);
  1710. end;
  1711. procedure tcg.g_exception_reason_save_const(list : taasmoutput; const href : treference; a: aint);
  1712. begin
  1713. a_load_const_ref(list, OS_INT, a, href);
  1714. end;
  1715. procedure tcg.g_exception_reason_load(list : taasmoutput; const href : treference);
  1716. begin
  1717. a_load_ref_reg(list, OS_INT, OS_INT, href, NR_FUNCTION_RESULT_REG);
  1718. end;
  1719. procedure tcg.g_adjust_self_value(list:taasmoutput;procdef: tprocdef;ioffset: aint);
  1720. var
  1721. hsym : tsym;
  1722. href : treference;
  1723. paraloc : tcgparalocation;
  1724. begin
  1725. { calculate the parameter info for the procdef }
  1726. if not procdef.has_paraloc_info then
  1727. begin
  1728. procdef.requiredargarea:=paramanager.create_paraloc_info(procdef,callerside);
  1729. procdef.has_paraloc_info:=true;
  1730. end;
  1731. hsym:=tsym(procdef.parast.search('self'));
  1732. if not(assigned(hsym) and
  1733. (hsym.typ=paravarsym)) then
  1734. internalerror(200305251);
  1735. paraloc:=tparavarsym(hsym).paraloc[callerside].location^;
  1736. case paraloc.loc of
  1737. LOC_REGISTER:
  1738. cg.a_op_const_reg(list,OP_SUB,paraloc.size,ioffset,paraloc.register);
  1739. LOC_REFERENCE:
  1740. begin
  1741. { offset in the wrapper needs to be adjusted for the stored
  1742. return address }
  1743. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset+sizeof(aint));
  1744. cg.a_op_const_ref(list,OP_SUB,paraloc.size,ioffset,href);
  1745. end
  1746. else
  1747. internalerror(200309189);
  1748. end;
  1749. end;
  1750. {*****************************************************************************
  1751. TCG64
  1752. *****************************************************************************}
  1753. {$ifndef cpu64bit}
  1754. procedure tcg64.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : int64; regsrc,regdst : tregister64);
  1755. begin
  1756. a_load64_reg_reg(list,regsrc,regdst);
  1757. a_op64_const_reg(list,op,value,regdst);
  1758. end;
  1759. procedure tcg64.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1760. var
  1761. tmpreg64 : tregister64;
  1762. begin
  1763. { when src1=dst then we need to first create a temp to prevent
  1764. overwriting src1 with src2 }
  1765. if (regsrc1.reghi=regdst.reghi) or
  1766. (regsrc1.reglo=regdst.reghi) or
  1767. (regsrc1.reghi=regdst.reglo) or
  1768. (regsrc1.reglo=regdst.reglo) then
  1769. begin
  1770. tmpreg64.reglo:=cg.getintregister(list,OS_32);
  1771. tmpreg64.reghi:=cg.getintregister(list,OS_32);
  1772. a_load64_reg_reg(list,regsrc2,tmpreg64);
  1773. a_op64_reg_reg(list,op,regsrc1,tmpreg64);
  1774. a_load64_reg_reg(list,tmpreg64,regdst);
  1775. end
  1776. else
  1777. begin
  1778. a_load64_reg_reg(list,regsrc2,regdst);
  1779. a_op64_reg_reg(list,op,regsrc1,regdst);
  1780. end;
  1781. end;
  1782. {$endif cpu64bit}
  1783. initialization
  1784. ;
  1785. finalization
  1786. cg.free;
  1787. {$ifndef cpu64bit}
  1788. cg64.free;
  1789. {$endif cpu64bit}
  1790. end.
  1791. {
  1792. $Log$
  1793. Revision 1.191 2005-01-24 22:08:32 peter
  1794. * interface wrapper generation moved to cgobj
  1795. * generate interface wrappers after the module is parsed
  1796. Revision 1.190 2005/01/20 17:47:01 peter
  1797. * remove copy_value_on_stack and a_param_copy_ref
  1798. Revision 1.189 2005/01/20 16:38:45 peter
  1799. * load jmp_buf_size from system unit
  1800. Revision 1.188 2005/01/18 22:19:20 peter
  1801. * multiple location support for i386 a_param_ref
  1802. * remove a_param_copy_ref for i386
  1803. Revision 1.187 2004/11/30 18:13:39 jonas
  1804. * patch from Peter to fix inlining of case statements
  1805. Revision 1.186 2004/11/08 21:47:39 florian
  1806. * better code generation for copying of open arrays
  1807. Revision 1.185 2004/11/08 20:23:29 florian
  1808. * fixed open arrays when using register variables
  1809. Revision 1.184 2004/11/02 17:25:36 florian
  1810. * <signed type> to qword range check for 64 bit targets fixed
  1811. Revision 1.183 2004/10/31 21:45:02 peter
  1812. * generic tlocation
  1813. * move tlocation to cgutils
  1814. Revision 1.182 2004/10/25 15:36:47 peter
  1815. * save standard registers moved to tcgobj
  1816. Revision 1.181 2004/10/24 20:01:08 peter
  1817. * remove saveregister calling convention
  1818. Revision 1.180 2004/10/24 11:44:28 peter
  1819. * small regvar fixes
  1820. * loadref parameter removed from concatcopy,incrrefcount,etc
  1821. Revision 1.179 2004/10/15 09:14:16 mazen
  1822. - remove $IFDEF DELPHI and related code
  1823. - remove $IFDEF FPCPROCVAR and related code
  1824. Revision 1.178 2004/10/13 21:12:51 peter
  1825. * -Or fixes for open array
  1826. Revision 1.177 2004/10/11 15:46:45 peter
  1827. * length parameter for copyvaluearray changed to tlocation
  1828. Revision 1.176 2004/10/10 20:31:48 peter
  1829. * concatcopy_unaligned maps by default to concatcopy, sparc will
  1830. override it with call to fpc_move
  1831. Revision 1.175 2004/10/10 20:22:53 peter
  1832. * symtable allocation rewritten
  1833. * loading of parameters to local temps/regs cleanup
  1834. * regvar support for parameters
  1835. * regvar support for staticsymtable (main body)
  1836. Revision 1.174 2004/10/05 20:41:01 peter
  1837. * more spilling rewrites
  1838. Revision 1.173 2004/09/29 18:55:40 florian
  1839. * fixed more sparc overflow stuff
  1840. * fixed some op64 stuff for sparc
  1841. Revision 1.172 2004/09/26 21:04:35 florian
  1842. + partial overflow checking on sparc; multiplication still missing
  1843. Revision 1.171 2004/09/26 17:45:30 peter
  1844. * simple regvar support, not yet finished
  1845. Revision 1.170 2004/09/25 14:23:54 peter
  1846. * ungetregister is now only used for cpuregisters, renamed to
  1847. ungetcpuregister
  1848. * renamed (get|unget)explicitregister(s) to ..cpuregister
  1849. * removed location-release/reference_release
  1850. Revision 1.169 2004/09/21 17:25:12 peter
  1851. * cgpara branch merged
  1852. Revision 1.168.4.4 2004/09/20 20:45:57 peter
  1853. * remove cg64.a_reg_alloc, it should not be used since it
  1854. create more register conflicts
  1855. Revision 1.168.4.3 2004/09/18 20:22:40 jonas
  1856. * allocate the volatile fpu registers around procedures that might use
  1857. them (e.g. FPCMOVE may use them)
  1858. Revision 1.168.4.2 2004/09/12 13:36:40 peter
  1859. * fixed alignment issues
  1860. Revision 1.168.4.1 2004/08/31 20:43:06 peter
  1861. * cgpara patch
  1862. Revision 1.168 2004/07/09 23:41:04 jonas
  1863. * support register parameters for inlined procedures + some inline
  1864. cleanups
  1865. Revision 1.167 2004/07/03 11:47:04 peter
  1866. * fix rangecheck error when assigning u32bit=s32bit
  1867. Revision 1.166 2004/06/20 08:55:28 florian
  1868. * logs truncated
  1869. Revision 1.165 2004/06/16 20:07:07 florian
  1870. * dwarf branch merged
  1871. Revision 1.164 2004/05/22 23:34:27 peter
  1872. tai_regalloc.allocation changed to ratype to notify rgobj of register size changes
  1873. Revision 1.163 2004/04/29 19:56:36 daniel
  1874. * Prepare compiler infrastructure for multiple ansistring types
  1875. Revision 1.162 2004/04/18 07:52:43 florian
  1876. * fixed web bug 3048: comparision of dyn. arrays
  1877. Revision 1.161.2.17 2004/06/13 10:51:16 florian
  1878. * fixed several register allocator problems (sparc/arm)
  1879. }