cgobj.pas 91 KB

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