cgobj.pas 97 KB

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