cgobj.pas 85 KB

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