cgobj.pas 93 KB

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