cgobj.pas 97 KB

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