cgobj.pas 94 KB

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